xref: /freebsd/lib/libc/stdio/wscanf.3 (revision ee12faa062c04a49bf6fe4e6867bad8606e2413f)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)scanf.3	8.2 (Berkeley) 12/11/93
33.\" FreeBSD: src/lib/libc/stdio/scanf.3,v 1.24 2003/06/28 09:03:25 das Exp
34.\"
35.Dd July 5, 2003
36.Dt WSCANF 3
37.Os
38.Sh NAME
39.Nm wscanf ,
40.Nm fwscanf ,
41.Nm swscanf ,
42.Nm vwscanf ,
43.Nm vswscanf ,
44.Nm vfwscanf
45.Nd wide character input format conversion
46.Sh LIBRARY
47.Lb libc
48.Sh SYNOPSIS
49.In stdio.h
50.In wchar.h
51.Ft int
52.Fn wscanf "const wchar_t * restrict format" ...
53.Ft int
54.Fn fwscanf "FILE * restrict stream" "const wchar_t * restrict format" ...
55.Ft int
56.Fn swscanf "const wchar_t * restrict str" "const wchar_t * restrict format" ...
57.In stdarg.h
58.Ft int
59.Fn vwscanf "const wchar_t * restrict format" "va_list ap"
60.Ft int
61.Fn vswscanf "const wchar_t * restrict str" "const wchar_t * restrict format" "va_list ap"
62.Ft int
63.Fn vfwscanf "FILE * restrict stream" "const wchar_t * restrict format" "va_list ap"
64.Sh DESCRIPTION
65The
66.Fn wscanf
67family of functions scans input according to a
68.Fa format
69as described below.
70This format may contain
71.Em conversion specifiers ;
72the results from such conversions, if any,
73are stored through the
74.Em pointer
75arguments.
76The
77.Fn wscanf
78function
79reads input from the standard input stream
80.Dv stdin ,
81.Fn fwscanf
82reads input from the stream pointer
83.Fa stream ,
84and
85.Fn swscanf
86reads its input from the wide character string pointed to by
87.Fa str .
88The
89.Fn vfwscanf
90function
91is analogous to
92.Xr vfwprintf 3
93and reads input from the stream pointer
94.Fa stream
95using a variable argument list of pointers (see
96.Xr stdarg 3 ) .
97The
98.Fn vwscanf
99function scans a variable argument list from the standard input and
100the
101.Fn vswscanf
102function scans it from a wide character string;
103these are analogous to
104the
105.Fn vwprintf
106and
107.Fn vswprintf
108functions respectively.
109Each successive
110.Em pointer
111argument must correspond properly with
112each successive conversion specifier
113(but see the
114.Cm *
115conversion below).
116All conversions are introduced by the
117.Cm %
118(percent sign) character.
119The
120.Fa format
121string
122may also contain other characters.
123White space (such as blanks, tabs, or newlines) in the
124.Fa format
125string match any amount of white space, including none, in the input.
126Everything else
127matches only itself.
128Scanning stops
129when an input character does not match such a format character.
130Scanning also stops
131when an input conversion cannot be made (see below).
132.Sh CONVERSIONS
133Following the
134.Cm %
135character introducing a conversion
136there may be a number of
137.Em flag
138characters, as follows:
139.Bl -tag -width ".Cm l No (ell)"
140.It Cm *
141Suppresses assignment.
142The conversion that follows occurs as usual, but no pointer is used;
143the result of the conversion is simply discarded.
144.It Cm hh
145Indicates that the conversion will be one of
146.Cm dioux
147or
148.Cm n
149and the next pointer is a pointer to a
150.Vt char
151(rather than
152.Vt int ) .
153.It Cm h
154Indicates that the conversion will be one of
155.Cm dioux
156or
157.Cm n
158and the next pointer is a pointer to a
159.Vt "short int"
160(rather than
161.Vt int ) .
162.It Cm l No (ell)
163Indicates that the conversion will be one of
164.Cm dioux
165or
166.Cm n
167and the next pointer is a pointer to a
168.Vt "long int"
169(rather than
170.Vt int ) ,
171that the conversion will be one of
172.Cm a , e , f ,
173or
174.Cm g
175and the next pointer is a pointer to
176.Vt double
177(rather than
178.Vt float ) ,
179or that the conversion will be one of
180.Cm c
181or
182.Cm s
183and the next pointer is a pointer to an array of
184.Vt wchar_t
185(rather than
186.Vt char ) .
187.It Cm ll No (ell ell)
188Indicates that the conversion will be one of
189.Cm dioux
190or
191.Cm n
192and the next pointer is a pointer to a
193.Vt "long long int"
194(rather than
195.Vt int ) .
196.It Cm L
197Indicates that the conversion will be one of
198.Cm a , e , f ,
199or
200.Cm g
201and the next pointer is a pointer to
202.Vt "long double" .
203.It Cm j
204Indicates that the conversion will be one of
205.Cm dioux
206or
207.Cm n
208and the next pointer is a pointer to a
209.Vt intmax_t
210(rather than
211.Vt int ) .
212.It Cm t
213Indicates that the conversion will be one of
214.Cm dioux
215or
216.Cm n
217and the next pointer is a pointer to a
218.Vt ptrdiff_t
219(rather than
220.Vt int ) .
221.It Cm z
222Indicates that the conversion will be one of
223.Cm dioux
224or
225.Cm n
226and the next pointer is a pointer to a
227.Vt size_t
228(rather than
229.Vt int ) .
230.It Cm q
231(deprecated.)
232Indicates that the conversion will be one of
233.Cm dioux
234or
235.Cm n
236and the next pointer is a pointer to a
237.Vt "long long int"
238(rather than
239.Vt int ) .
240.El
241.Pp
242In addition to these flags,
243there may be an optional maximum field width,
244expressed as a decimal integer,
245between the
246.Cm %
247and the conversion.
248If no width is given,
249a default of
250.Dq infinity
251is used (with one exception, below);
252otherwise at most this many characters are scanned
253in processing the conversion.
254Before conversion begins,
255most conversions skip white space;
256this white space is not counted against the field width.
257.Pp
258The following conversions are available:
259.Bl -tag -width XXXX
260.It Cm %
261Matches a literal
262.Ql % .
263That is,
264.Dq Li %%
265in the format string
266matches a single input
267.Ql %
268character.
269No conversion is done, and assignment does not occur.
270.It Cm d
271Matches an optionally signed decimal integer;
272the next pointer must be a pointer to
273.Vt int .
274.It Cm i
275Matches an optionally signed integer;
276the next pointer must be a pointer to
277.Vt int .
278The integer is read in base 16 if it begins
279with
280.Ql 0x
281or
282.Ql 0X ,
283in base 8 if it begins with
284.Ql 0 ,
285and in base 10 otherwise.
286Only characters that correspond to the base are used.
287.It Cm o
288Matches an octal integer;
289the next pointer must be a pointer to
290.Vt "unsigned int" .
291.It Cm u
292Matches an optionally signed decimal integer;
293the next pointer must be a pointer to
294.Vt "unsigned int" .
295.It Cm x , X
296Matches an optionally signed hexadecimal integer;
297the next pointer must be a pointer to
298.Vt "unsigned int" .
299.It Cm a , A , e , E , f , F , g , G
300Matches a floating-point number in the style of
301.Xr wcstod 3 .
302The next pointer must be a pointer to
303.Vt float
304(unless
305.Cm l
306or
307.Cm L
308is specified.)
309.It Cm s
310Matches a sequence of non-white-space wide characters;
311the next pointer must be a pointer to
312.Vt char ,
313and the array must be large enough to accept the multibyte representation
314of all the sequence and the
315terminating
316.Dv NUL
317character.
318The input string stops at white space
319or at the maximum field width, whichever occurs first.
320.Pp
321If an
322.Cm l
323qualifier is present, the next pointer must be a pointer to
324.Vt wchar_t ,
325into which the input will be placed.
326.It Cm S
327The same as
328.Cm ls .
329.It Cm c
330Matches a sequence of
331.Em width
332count
333wide characters (default 1);
334the next pointer must be a pointer to
335.Vt char ,
336and there must be enough room for the multibyte representation
337of all the characters
338(no terminating
339.Dv NUL
340is added).
341The usual skip of leading white space is suppressed.
342To skip white space first, use an explicit space in the format.
343.Pp
344If an
345.Cm l
346qualifier is present, the next pointer must be a pointer to
347.Vt wchar_t ,
348into which the input will be placed.
349.It Cm C
350The same as
351.Cm lc .
352.It Cm \&[
353Matches a nonempty sequence of characters from the specified set
354of accepted characters;
355the next pointer must be a pointer to
356.Vt char ,
357and there must be enough room for the multibyte representation of
358all the characters in the string,
359plus a terminating
360.Dv NUL
361character.
362The usual skip of leading white space is suppressed.
363The string is to be made up of characters in
364(or not in)
365a particular set;
366the set is defined by the characters between the open bracket
367.Cm \&[
368character
369and a close bracket
370.Cm \&]
371character.
372The set
373.Em excludes
374those characters
375if the first character after the open bracket is a circumflex
376.Cm ^ .
377To include a close bracket in the set,
378make it the first character after the open bracket
379or the circumflex;
380any other position will end the set.
381To include a hyphen in the set,
382make it the last character before the final close bracket;
383some implementations of
384.Fn wscanf
385use
386.Dq Li A-Z
387to represent the range of characters between
388.Ql A
389and
390.Ql Z .
391The string ends with the appearance of a character not in the
392(or, with a circumflex, in) set
393or when the field width runs out.
394.Pp
395If an
396.Cm l
397qualifier is present, the next pointer must be a pointer to
398.Vt wchar_t ,
399into which the input will be placed.
400.It Cm p
401Matches a pointer value (as printed by
402.Ql %p
403in
404.Xr wprintf 3 ) ;
405the next pointer must be a pointer to
406.Vt void .
407.It Cm n
408Nothing is expected;
409instead, the number of characters consumed thus far from the input
410is stored through the next pointer,
411which must be a pointer to
412.Vt int .
413This is
414.Em not
415a conversion, although it can be suppressed with the
416.Cm *
417flag.
418.El
419.Pp
420The decimal point
421character is defined in the program's locale (category
422.Dv LC_NUMERIC ) .
423.Pp
424For backwards compatibility, a
425.Dq conversion
426of
427.Ql %\e0
428causes an immediate return of
429.Dv EOF .
430.Sh RETURN VALUES
431These
432functions
433return
434the number of input items assigned, which can be fewer than provided
435for, or even zero, in the event of a matching failure.
436Zero
437indicates that, while there was input available,
438no conversions were assigned;
439typically this is due to an invalid input character,
440such as an alphabetic character for a
441.Ql %d
442conversion.
443The value
444.Dv EOF
445is returned if an input failure occurs before any conversion such as an
446end-of-file occurs.
447If an error or end-of-file occurs after conversion
448has begun,
449the number of conversions which were successfully completed is returned.
450.Sh SEE ALSO
451.Xr fgetwc 3 ,
452.Xr scanf 3 ,
453.Xr wcrtomb 3 ,
454.Xr wcstod 3 ,
455.Xr wcstol 3 ,
456.Xr wcstoul 3 ,
457.Xr wprintf 3
458.Sh STANDARDS
459The
460.Fn fwscanf ,
461.Fn wscanf ,
462.Fn swscanf ,
463.Fn vfwscanf ,
464.Fn vwscanf
465and
466.Fn vswscanf
467functions
468conform to
469.St -isoC-99 .
470.Sh BUGS
471In addition to the bugs documented in
472.Xr scanf 3 ,
473.Fn wscanf
474does not support the
475.Dq Li A-Z
476notation for specifying character ranges with the character
477class conversion
478.Pq Sq Cm %[ .
479