Merge from vendor branch TEXINFO:
[dragonfly.git] / lib / libc / stdio / scanf.3
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. All advertising materials mentioning features or use of this software
17 .\"    must display the following acknowledgement:
18 .\"     This product includes software developed by the University of
19 .\"     California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\"    may be used to endorse or promote products derived from this software
22 .\"    without specific prior written permission.
23 .\"
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" SUCH DAMAGE.
35 .\"
36 .\"     @(#)scanf.3     8.2 (Berkeley) 12/11/93
37 .\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.7.2.6 2002/04/12 16:34:38 trhodes Exp $
38 .\" $DragonFly: src/lib/libc/stdio/scanf.3,v 1.2 2003/06/17 04:26:46 dillon Exp $
39 .\"
40 .Dd December 11, 1993
41 .Dt SCANF 3
42 .Os
43 .Sh NAME
44 .Nm scanf ,
45 .Nm fscanf ,
46 .Nm sscanf ,
47 .Nm vscanf ,
48 .Nm vsscanf ,
49 .Nm vfscanf
50 .Nd input format conversion
51 .Sh LIBRARY
52 .Lb libc
53 .Sh SYNOPSIS
54 .In stdio.h
55 .Ft int
56 .Fn scanf "const char *format" ...
57 .Ft int
58 .Fn fscanf "FILE *stream" "const char *format" ...
59 .Ft int
60 .Fn sscanf "const char *str" "const char *format" ...
61 .In stdarg.h
62 .Ft int
63 .Fn vscanf "const char *format" "va_list ap"
64 .Ft int
65 .Fn vsscanf "const char *str" "const char *format" "va_list ap"
66 .Ft int
67 .Fn vfscanf "FILE *stream" "const char *format" "va_list ap"
68 .Sh DESCRIPTION
69 The
70 .Fn scanf
71 family of functions scans input according to a
72 .Fa format
73 as described below.
74 This format may contain
75 .Em conversion specifiers ;
76 the results from such conversions, if any,
77 are stored through the
78 .Em pointer
79 arguments.
80 The
81 .Fn scanf
82 function
83 reads input from the standard input stream
84 .Em stdin ,
85 .Fn fscanf
86 reads input from the stream pointer
87 .Fa stream ,
88 and
89 .Fn sscanf
90 reads its input from the character string pointed to by
91 .Fa str .
92 The
93 .Fn vfscanf
94 function
95 is analogous to
96 .Xr vfprintf 3
97 and reads input from the stream pointer
98 .Fa stream
99 using a variable argument list of pointers (see
100 .Xr stdarg 3 ) .
101 The
102 .Fn vscanf
103 function scans a variable argument list from the standard input and
104 the
105 .Fn vsscanf
106 function scans it from a string;
107 these are analogous to
108 the
109 .Fn vprintf
110 and
111 .Fn vsprintf
112 functions respectively.
113 Each successive
114 .Em pointer
115 argument must correspond properly with
116 each successive conversion specifier
117 (but see the
118 .Cm *
119 conversion below).
120 All conversions are introduced by the
121 .Cm %
122 (percent sign) character.
123 The
124 .Fa format
125 string
126 may also contain other characters.
127 White space (such as blanks, tabs, or newlines) in the
128 .Fa format
129 string match any amount of white space, including none, in the input.
130 Everything else
131 matches only itself.
132 Scanning stops
133 when an input character does not match such a format character.
134 Scanning also stops
135 when an input conversion cannot be made (see below).
136 .Sh CONVERSIONS
137 Following the
138 .Cm %
139 character introducing a conversion
140 there may be a number of
141 .Em flag
142 characters, as follows:
143 .Bl -tag -width indent
144 .It Cm *
145 Suppresses assignment.
146 The conversion that follows occurs as usual, but no pointer is used;
147 the result of the conversion is simply discarded.
148 .It Cm h
149 Indicates that the conversion will be one of
150 .Cm dioux
151 or
152 .Cm n
153 and the next pointer is a pointer to a
154 .Em short  int
155 (rather than
156 .Em int ) .
157 .It Cm l
158 Indicates either that the conversion will be one of
159 .Cm dioux
160 or
161 .Cm n
162 and the next pointer is a pointer to a
163 .Em long  int
164 (rather than
165 .Em int ) ,
166 or that the conversion will be one of
167 .Cm efg
168 and the next pointer is a pointer to
169 .Em double
170 (rather than
171 .Em float ) .
172 .It Cm L
173 Indicates that the conversion will be
174 .Cm efg
175 and the next pointer is a pointer to
176 .Em long double .
177 (This type is not implemented; the
178 .Cm L
179 flag is currently ignored.)
180 .It Cm q
181 Indicates either that the conversion will be one of
182 .Cm dioux
183 or
184 .Cm n
185 and the next pointer is a pointer to a
186 .Em long long int
187 (rather than
188 .Em int ) ,
189 .El
190 .Pp
191 In addition to these flags,
192 there may be an optional maximum field width,
193 expressed as a decimal integer,
194 between the
195 .Cm %
196 and the conversion.
197 If no width is given,
198 a default of `infinity' is used (with one exception, below);
199 otherwise at most this many characters are scanned
200 in processing the conversion.
201 Before conversion begins,
202 most conversions skip white space;
203 this white space is not counted against the field width.
204 .Pp
205 The following conversions are available:
206 .Bl -tag -width XXXX
207 .It Cm %
208 Matches a literal `%'.
209 That is, `%\&%' in the format string
210 matches a single input `%' character.
211 No conversion is done, and assignment does not occur.
212 .It Cm d
213 Matches an optionally signed decimal integer;
214 the next pointer must be a pointer to
215 .Em int .
216 .It Cm D
217 Equivalent to
218 .Cm ld ;
219 this exists only for backwards compatibility.
220 .It Cm i
221 Matches an optionally signed integer;
222 the next pointer must be a pointer to
223 .Em int .
224 The integer is read in base 16 if it begins
225 with
226 .Ql 0x
227 or
228 .Ql 0X ,
229 in base 8 if it begins with
230 .Ql 0 ,
231 and in base 10 otherwise.
232 Only characters that correspond to the base are used.
233 .It Cm o
234 Matches an octal integer;
235 the next pointer must be a pointer to
236 .Em unsigned int .
237 .It Cm O
238 Equivalent to
239 .Cm lo ;
240 this exists for backwards compatibility.
241 .It Cm u
242 Matches an optionally signed decimal integer;
243 the next pointer must be a pointer to
244 .Em unsigned int .
245 .It Cm x
246 Matches an optionally signed hexadecimal integer;
247 the next pointer must be a pointer to
248 .Em unsigned int .
249 .It Cm X
250 Equivalent to
251 .Cm lx ;
252 this violates the
253 .St -isoC ,
254 but is backwards compatible with previous
255 .Ux
256 systems.
257 .It Cm f
258 Matches an optionally signed floating-point number;
259 the next pointer must be a pointer to
260 .Em float .
261 .It Cm e
262 Equivalent to
263 .Cm f .
264 .It Cm g
265 Equivalent to
266 .Cm f .
267 .It Cm E
268 Equivalent to
269 .Cm lf ;
270 this violates the
271 .St -isoC ,
272 but is backwards compatible with previous
273 .Ux
274 systems.
275 .It Cm F
276 Equivalent to
277 .Cm lf ;
278 this exists only for backwards compatibility.
279 .It Cm s
280 Matches a sequence of non-white-space characters;
281 the next pointer must be a pointer to
282 .Em char ,
283 and the array must be large enough to accept all the sequence and the
284 terminating
285 .Dv NUL
286 character.
287 The input string stops at white space
288 or at the maximum field width, whichever occurs first.
289 .It Cm c
290 Matches a sequence of
291 .Em width
292 count
293 characters (default 1);
294 the next pointer must be a pointer to
295 .Em char ,
296 and there must be enough room for all the characters
297 (no terminating
298 .Dv NUL
299 is added).
300 The usual skip of leading white space is suppressed.
301 To skip white space first, use an explicit space in the format.
302 .It Cm \&[
303 Matches a nonempty sequence of characters from the specified set
304 of accepted characters;
305 the next pointer must be a pointer to
306 .Em char ,
307 and there must be enough room for all the characters in the string,
308 plus a terminating
309 .Dv NUL
310 character.
311 The usual skip of leading white space is suppressed.
312 The string is to be made up of characters in
313 (or not in)
314 a particular set;
315 the set is defined by the characters between the open bracket
316 .Cm [
317 character
318 and a close bracket
319 .Cm ]
320 character.
321 The set
322 .Em excludes
323 those characters
324 if the first character after the open bracket is a circumflex
325 .Cm ^ .
326 To include a close bracket in the set,
327 make it the first character after the open bracket
328 or the circumflex;
329 any other position will end the set.
330 The hyphen character
331 .Cm -
332 is also special;
333 when placed between two other characters,
334 it adds all intervening characters to the set.
335 To include a hyphen,
336 make it the last character before the final close bracket.
337 For instance,
338 .Ql [^]0-9-]
339 means the set `everything except close bracket, zero through nine,
340 and hyphen'.
341 The string ends with the appearance of a character not in the
342 (or, with a circumflex, in) set
343 or when the field width runs out.
344 .It Cm p
345 Matches a pointer value (as printed by
346 .Ql %p
347 in
348 .Xr printf 3 ) ;
349 the next pointer must be a pointer to
350 .Em void .
351 .It Cm n
352 Nothing is expected;
353 instead, the number of characters consumed thus far from the input
354 is stored through the next pointer,
355 which must be a pointer to
356 .Em int .
357 This is
358 .Em not
359 a conversion, although it can be suppressed with the
360 .Cm *
361 flag.
362 .El
363 .Pp
364 For backwards compatibility,
365 other conversion characters (except
366 .Ql \e0 )
367 are taken as if they were
368 .Ql %d
369 or, if uppercase,
370 .Ql %ld ,
371 and a `conversion' of
372 .Ql %\e0
373 causes an immediate return of
374 .Dv EOF .
375 The
376 .Cm F
377 and
378 .Cm X
379 conversions will be changed in the future
380 to conform to the
381 .Tn ANSI
382 C standard,
383 after which they will act like
384 .Cm f
385 and
386 .Cm x
387 respectively.
388 .Sh RETURN VALUES
389 These
390 functions
391 return
392 the number of input items assigned, which can be fewer than provided
393 for, or even zero, in the event of a matching failure.
394 Zero
395 indicates that, while there was input available,
396 no conversions were assigned;
397 typically this is due to an invalid input character,
398 such as an alphabetic character for a
399 .Ql %d
400 conversion.
401 The value
402 .Dv EOF
403 is returned if an input failure occurs before any conversion such as an
404 end-of-file occurs.
405 If an error or end-of-file occurs after conversion
406 has begun,
407 the number of conversions which were successfully completed is returned.
408 .Sh SEE ALSO
409 .Xr getc 3 ,
410 .Xr printf 3 ,
411 .Xr strtod 3 ,
412 .Xr strtol 3 ,
413 .Xr strtoul 3
414 .Sh STANDARDS
415 The functions
416 .Fn fscanf ,
417 .Fn scanf ,
418 and
419 .Fn sscanf
420 conform to
421 .St -isoC .
422 .Sh BUGS
423 The current situation with
424 .Cm %F
425 and
426 .Cm %X
427 conversions is unfortunate.
428 .Pp
429 All of the backwards compatibility formats will be removed in the future.
430 .Pp
431 Numerical strings are truncated to 512 characters; for example,
432 .Cm %f
433 and
434 .Cm %d
435 are implicitly
436 .Cm %512f
437 and
438 .Cm %512d .