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