share/mk/: Remove unused variable
[man-pages.git] / man3 / sscanf.3
blobd488cd8617bd09e8f2e689d4c8b9ed596946698a
1 '\" t
2 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
3 .\" All rights reserved.
4 .\"
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Chris Torek and the American National Standards Committee X3,
7 .\" on Information Processing Systems.
8 .\"
9 .\" SPDX-License-Identifier: BSD-4-Clause-UC
10 .\"
11 .\"     @(#)scanf.3     6.14 (Berkeley) 1/8/93
12 .\"
13 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
14 .\" modified to resemble the GNU libio setup used in the Linux libc
15 .\" used in versions 4.x (x>4) and 5   Helmut.Geyer@iwr.uni-heidelberg.de
16 .\" Modified, aeb, 970121
17 .\" 2005-07-14, mtk, added description of %n$ form; various text
18 .\"     incorporated from the GNU C library documentation ((C) The
19 .\"     Free Software Foundation); other parts substantially rewritten.
20 .\"
21 .\" 2008-06-23, mtk
22 .\"     Add ERRORS section.
23 .\"     Document the 'a' and 'm' modifiers for dynamic string allocation.
24 .\"
25 .TH sscanf 3 (date) "Linux man-pages (unreleased)"
26 .SH NAME
27 sscanf, vsscanf \- input string format conversion
28 .SH LIBRARY
29 Standard C library
30 .RI ( libc ", " \-lc )
31 .SH SYNOPSIS
32 .nf
33 .B #include <stdio.h>
35 .BI "int sscanf(const char *restrict " str ,
36 .BI "           const char *restrict " format ", ...);"
38 .B #include <stdarg.h>
40 .BI "int vsscanf(const char *restrict " str ,
41 .BI "           const char *restrict " format ", va_list " ap );
42 .fi
44 .RS -4
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
47 .RE
49 .BR vsscanf ():
50 .nf
51     _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
52 .fi
53 .SH DESCRIPTION
54 The
55 .BR sscanf ()
56 family of functions scans formatted input according to
57 .I format
58 as described below.
59 This format may contain
60 .IR "conversion specifications" ;
61 the results from such conversions, if any,
62 are stored in the locations pointed to by the
63 .I pointer
64 arguments that follow
65 .IR format .
66 Each
67 .I pointer
68 argument must be of a type that is appropriate for the value returned
69 by the corresponding conversion specification.
71 If the number of conversion specifications in
72 .I format
73 exceeds the number of
74 .I pointer
75 arguments, the results are undefined.
76 If the number of
77 .I pointer
78 arguments exceeds the number of conversion specifications, then the excess
79 .I pointer
80 arguments are evaluated, but are otherwise ignored.
82 .BR sscanf ()
83 These functions
84 read their input from the string pointed to by
85 .IR str .
87 The
88 .BR vsscanf ()
89 function is analogous to
90 .BR vsprintf (3).
92 The
93 .I format
94 string consists of a sequence of
95 .I directives
96 which describe how to process the sequence of input characters.
97 If processing of a directive fails, no further input is read, and
98 .BR sscanf ()
99 returns.
100 A "failure" can be either of the following:
101 .IR "input failure" ,
102 meaning that input characters were unavailable, or
103 .IR "matching failure" ,
104 meaning that the input was inappropriate (see below).
106 A directive is one of the following:
108 \[bu]
109 A sequence of white-space characters (space, tab, newline, etc.; see
110 .BR isspace (3)).
111 This directive matches any amount of white space,
112 including none, in the input.
114 \[bu]
115 An ordinary character (i.e., one other than white space or \[aq]%\[aq]).
116 This character must exactly match the next character of input.
118 \[bu]
119 A conversion specification,
120 which commences with a \[aq]%\[aq] (percent) character.
121 A sequence of characters from the input is converted according to
122 this specification, and the result is placed in the corresponding
123 .I pointer
124 argument.
125 If the next item of input does not match the conversion specification,
126 the conversion fails\[em]this is a
127 .IR "matching failure" .
129 Each
130 .I conversion specification
132 .I format
133 begins with either the character \[aq]%\[aq] or the character sequence
134 "\fB%\fP\fIn\fP\fB$\fP"
135 (see below for the distinction) followed by:
137 \[bu]
138 An optional \[aq]*\[aq] assignment-suppression character:
139 .BR sscanf ()
140 reads input as directed by the conversion specification,
141 but discards the input.
142 No corresponding
143 .I pointer
144 argument is required, and this specification is not
145 included in the count of successful assignments returned by
146 .BR scanf ().
148 \[bu]
149 For decimal conversions, an optional quote character (\[aq]).
150 This specifies that the input number may include thousands'
151 separators as defined by the
152 .B LC_NUMERIC
153 category of the current locale.
154 (See
155 .BR setlocale (3).)
156 The quote character may precede or follow the \[aq]*\[aq]
157 assignment-suppression character.
159 \[bu]
160 An optional \[aq]m\[aq] character.
161 This is used with string conversions
162 .RI ( %s ,
163 .IR %c ,
164 .IR %[ ),
165 and relieves the caller of the
166 need to allocate a corresponding buffer to hold the input: instead,
167 .BR sscanf ()
168 allocates a buffer of sufficient size,
169 and assigns the address of this buffer to the corresponding
170 .I pointer
171 argument, which should be a pointer to a
172 .I "char\ *"
173 variable (this variable does not need to be initialized before the call).
174 The caller should subsequently
175 .BR free (3)
176 this buffer when it is no longer required.
178 \[bu]
179 An optional decimal integer which specifies the
180 .IR "maximum field width" .
181 Reading of characters stops either when this maximum is reached or
182 when a nonmatching character is found, whichever happens first.
183 Most conversions discard initial white space characters (the exceptions
184 are noted below),
185 and these discarded characters don't count toward the maximum field width.
186 String input conversions store a terminating null byte (\[aq]\e0\[aq])
187 to mark the end of the input;
188 the maximum field width does not include this terminator.
190 \[bu]
191 An optional
192 .IR "type modifier character" .
193 For example, the
194 .B l
195 type modifier is used with integer conversions such as
196 .B %d
197 to specify that the corresponding
198 .I pointer
199 argument refers to a
200 .I "long"
201 rather than a pointer to an
202 .IR int .
204 \[bu]
206 .I "conversion specifier"
207 that specifies the type of input conversion to be performed.
209 The conversion specifications in
210 .I format
211 are of two forms, either beginning with \[aq]%\[aq] or beginning with
212 "\fB%\fP\fIn\fP\fB$\fP".
213 The two forms should not be mixed in the same
214 .I format
215 string, except that a string containing
216 "\fB%\fP\fIn\fP\fB$\fP"
217 specifications can include
218 .B %%
220 .BR %* .
222 .I format
223 contains \[aq]%\[aq]
224 specifications, then these correspond in order with successive
225 .I pointer
226 arguments.
227 In the
228 "\fB%\fP\fIn\fP\fB$\fP"
229 form (which is specified in POSIX.1-2001, but not C99),
230 .I n
231 is a decimal integer that specifies that the converted input should
232 be placed in the location referred to by the
233 .IR n -th
234 .I pointer
235 argument following
236 .IR format .
237 .SS Conversions
238 The following
239 .I "type modifier characters"
240 can appear in a conversion specification:
242 .B h
243 Indicates that the conversion will be one of
244 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
245 and the next pointer is a pointer to a
246 .I short
248 .I unsigned short
249 (rather than
250 .IR int ).
252 .B hh
253 As for
254 .BR h ,
255 but the next pointer is a pointer to a
256 .I signed char
258 .IR "unsigned char" .
260 .B j
261 As for
262 .BR h ,
263 but the next pointer is a pointer to an
264 .I intmax_t
265 or a
266 .IR uintmax_t .
267 This modifier was introduced in C99.
269 .B l
270 Indicates either that the conversion will be one of
271 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
272 and the next pointer is a pointer to a
273 .I long
275 .I unsigned long
276 (rather than
277 .IR int ),
278 or that the conversion will be one of
279 \fBe\fP, \fBf\fP, or \fBg\fP
280 and the next pointer is a pointer to
281 .I double
282 (rather than
283 .IR float ).
284 If used with
285 .B %c
287 .BR %s ,
288 the corresponding parameter is considered
289 as a pointer to a wide character or wide-character string respectively.
290 .\" This use of l was introduced in Amendment 1 to ISO C90.
292 .B ll
293 (ell-ell)
294 Indicates that the conversion will be one of
295 .BR b ,
296 .BR d ,
297 .BR i ,
298 .BR o ,
299 .BR u ,
300 .BR x ,
301 .BR X ,
303 .B n
304 and the next pointer is a pointer to a
305 .I long long
307 .I unsigned long long
308 (rather than
309 .IR int ).
311 .B L
312 Indicates that the conversion will be either
313 \fBe\fP, \fBf\fP, or \fBg\fP
314 and the next pointer is a pointer to
315 .I "long double"
317 (as a GNU extension)
318 the conversion will be
319 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, or \fBx\fP
320 and the next pointer is a pointer to
321 .IR "long long" .
322 .\" MTK, Jul 05: The following is no longer true for modern
323 .\" ANSI C (i.e., C99):
324 .\" (Note that long long is not an
325 .\" ANSI C
326 .\" type. Any program using this will not be portable to all
327 .\" architectures).
329 .B q
330 equivalent to
331 .BR L .
332 This specifier does not exist in ANSI C.
334 .B t
335 As for
336 .BR h ,
337 but the next pointer is a pointer to a
338 .IR ptrdiff_t .
339 This modifier was introduced in C99.
341 .B z
342 As for
343 .BR h ,
344 but the next pointer is a pointer to a
345 .IR size_t .
346 This modifier was introduced in C99.
348 The following
349 .I "conversion specifiers"
350 are available:
352 .B %
353 Matches a literal \[aq]%\[aq].
354 That is,
355 .B %\&%
356 in the format string matches a
357 single input \[aq]%\[aq] character.
358 No conversion is done (but initial white space characters are discarded),
359 and assignment does not occur.
361 .B d
362 Matches an optionally signed decimal integer;
363 the next pointer must be a pointer to
364 .IR int .
365 .\" .TP
366 .\" .B D
367 .\" Equivalent to
368 .\" .IR ld ;
369 .\" this exists only for backward compatibility.
370 .\" (Note: thus only in libc4
371 .\" In libc5 and glibc the
372 .\" .B %D
373 .\" is silently ignored, causing old programs to fail mysteriously.)
375 .B i
376 Matches an optionally signed integer; the next pointer must be a pointer to
377 .IR int .
378 The integer is read in base 16 if it begins with
379 .I 0x
381 .IR 0X ,
382 in base 8 if it begins with
383 .IR 0 ,
384 and in base 10 otherwise.
385 Only characters that correspond to the base are used.
387 .B o
388 Matches an unsigned octal integer; the next pointer must be a pointer to
389 .IR "unsigned int" .
391 .B u
392 Matches an unsigned decimal integer; the next pointer must be a
393 pointer to
394 .IR "unsigned int" .
396 .B x
397 Matches an unsigned hexadecimal integer
398 (that may optionally begin with a prefix of
399 .I 0x
401 .IR 0X ,
402 which is discarded); the next pointer must
403 be a pointer to
404 .IR "unsigned int" .
406 .B X
407 Equivalent to
408 .BR x .
410 .B f
411 Matches an optionally signed floating-point number; the next pointer must
412 be a pointer to
413 .IR float .
415 .B e
416 Equivalent to
417 .BR f .
419 .B g
420 Equivalent to
421 .BR f .
423 .B E
424 Equivalent to
425 .BR f .
427 .B a
428 (C99) Equivalent to
429 .BR f .
431 .B s
432 Matches a sequence of non-white-space characters;
433 the next pointer must be a pointer to the initial element of a
434 character array that is long enough to hold the input sequence and
435 the terminating null byte (\[aq]\e0\[aq]), which is added automatically.
436 The input string stops at white space or at the maximum field
437 width, whichever occurs first.
439 .B c
440 Matches a sequence of characters whose length is specified by the
441 .I maximum field width
442 (default 1); the next pointer must be a pointer to
443 .IR char ,
444 and there must be enough room for all the characters
445 (no terminating null byte is added).
446 The usual skip of leading white space is suppressed.
447 To skip white space first, use an explicit space in the format.
449 .B \&[
450 Matches a nonempty sequence of characters from the specified set of
451 accepted characters; the next pointer must be a pointer to
452 .IR char ,
453 and there must be enough room for all the characters in the string, plus a
454 terminating null byte.
455 The usual skip of leading white space is suppressed.
456 The string is to be made up of characters in (or not in) a particular set;
457 the set is defined by the characters between the open bracket
458 .B [
459 character and a close bracket
460 .B ]
461 character.
462 The set
463 .I excludes
464 those characters if the first character after the open bracket is a
465 circumflex
466 .RB ( \[ha] ).
467 To include a close bracket in the set, make it the first character after
468 the open bracket or the circumflex; any other position will end the set.
469 The hyphen character
470 .B \-
471 is also special; when placed between two other characters, it adds all
472 intervening characters to the set.
473 To include a hyphen, make it the last
474 character before the final close bracket.
475 For instance,
476 .B [\[ha]]0\-9\-]
477 means
478 the set "everything except close bracket, zero through nine, and hyphen".
479 The string ends with the appearance of a character not in the (or, with a
480 circumflex, in) set or when the field width runs out.
482 .B p
483 Matches a pointer value (as printed by
484 .B %p
486 .BR printf (3));
487 the next pointer must be a pointer to a pointer to
488 .IR void .
490 .B n
491 Nothing is expected; instead, the number of characters consumed thus far
492 from the input is stored through the next pointer, which must be a pointer
494 .IR int ,
495 or variant whose size matches the (optionally)
496 supplied integer length modifier.
497 This is
498 .I not
499 a conversion and does
500 .I not
501 increase the count returned by the function.
502 The assignment can be suppressed with the
503 .B *
504 assignment-suppression character, but the effect on the
505 return value is undefined.
506 Therefore
507 .B %*n
508 conversions should not be used.
509 .SH RETURN VALUE
510 On success, these functions return the number of input items
511 successfully matched and assigned;
512 this can be fewer than provided for,
513 or even zero, in the event of an early matching failure.
515 The value
516 .B EOF
517 is returned if the end of input is reached before either the first
518 successful conversion or a matching failure occurs.
519 .SH ERRORS
521 .B EILSEQ
522 Input byte sequence does not form a valid character.
524 .B EINVAL
525 Not enough arguments; or
526 .I format
527 is NULL.
529 .B ENOMEM
530 Out of memory.
531 .SH ATTRIBUTES
532 For an explanation of the terms used in this section, see
533 .BR attributes (7).
535 allbox;
536 lbx lb lb
537 l l l.
538 Interface       Attribute       Value
542 .BR sscanf (),
543 .BR vsscanf ()
544 T}      Thread safety   MT-Safe locale
546 .SH STANDARDS
547 C11, POSIX.1-2008.
548 .SH HISTORY
549 C89, POSIX.1-2001.
552 .B q
553 specifier is the 4.4BSD notation for
554 .IR "long long" ,
555 while
556 .B ll
557 or the usage of
558 .B L
559 in integer conversions is the GNU notation.
561 The Linux version of these functions is based on the
562 .I GNU
563 .I libio
564 library.
565 Take a look at the
566 .I info
567 documentation of
568 .I GNU
569 .I libc (glibc-1.08)
570 for a more concise description.
571 .SH NOTES
572 .SS The 'a' assignment-allocation modifier
573 Originally, the GNU C library supported dynamic allocation for string inputs
574 (as a nonstandard extension) via the
575 .B a
576 character.
577 (This feature is present at least as far back as glibc 2.0.)
578 Thus, one could write the following to have
579 .BR sscanf ()
580 allocate a buffer for a string,
581 with a pointer to that buffer being returned in
582 .IR *buf :
584 .in +4n
586 char *buf;
587 sscanf(str, "%as", &buf);
591 The use of the letter
592 .B a
593 for this purpose was problematic, since
594 .B a
595 is also specified by the ISO C standard as a synonym for
596 .B f
597 (floating-point input).
598 POSIX.1-2008 instead specifies the
599 .B m
600 modifier for assignment allocation (as documented in DESCRIPTION, above).
602 Note that the
603 .B a
604 modifier is not available if the program is compiled with
605 .I gcc\~\-std=c99
607 .I gcc\~\-D_ISOC99_SOURCE
608 (unless
609 .B _GNU_SOURCE
610 is also specified), in which case the
611 .B a
612 is interpreted as a specifier for floating-point numbers (see above).
614 Support for the
615 .B m
616 modifier was added to glibc 2.7,
617 and new programs should use that modifier instead of
618 .BR a .
620 As well as being standardized by POSIX, the
621 .B m
622 modifier has the following further advantages over
623 the use of
624 .BR a :
625 .IP \[bu] 3
626 It may also be applied to
627 .B %c
628 conversion specifiers (e.g.,
629 .BR %3mc ).
630 .IP \[bu]
631 It avoids ambiguity with respect to the
632 .B %a
633 floating-point conversion specifier (and is unaffected by
634 .I gcc\~\-std=c99
635 etc.).
636 .SH BUGS
637 .SS Numeric conversion specifiers
638 Use of the numeric conversion specifiers produces Undefined Behavior
639 for invalid input.
641 .UR https://port70.net/\:%7Ensz/\:c/\:c11/\:n1570.html\:#7.21.6.2p10
642 C11 7.21.6.2/10
643 .UE .
644 This is a bug in the ISO C standard,
645 and not an inherent design issue with the API.
646 However,
647 current implementations are not safe from that bug,
648 so it is not recommended to use them.
649 Instead,
650 programs should use functions such as
651 .BR strtol (3)
652 to parse numeric input.
653 Alternatively,
654 mitigate it by specifying a maximum field width.
655 .SS Nonstandard modifiers
656 These functions are fully C99 conformant, but provide the
657 additional modifiers
658 .B q
660 .B a
661 as well as an additional behavior of the
662 .B L
664 .B ll
665 modifiers.
666 The latter may be considered to be a bug, as it changes the
667 behavior of modifiers defined in C99.
669 Some combinations of the type modifiers and conversion
670 specifiers defined by C99 do not make sense
671 (e.g.,
672 .BR "%Ld" ).
673 While they may have a well-defined behavior on Linux, this need not
674 to be so on other architectures.
675 Therefore it usually is better to use
676 modifiers that are not defined by C99 at all, that is, use
677 .B q
678 instead of
679 .B L
680 in combination with
681 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, and \fBX\fP
682 conversions or
683 .BR ll .
685 The usage of
686 .B q
687 is not the same as on 4.4BSD,
688 as it may be used in float conversions equivalently to
689 .BR L .
690 .SH EXAMPLES
691 To use the dynamic allocation conversion specifier, specify
692 .B m
693 as a length modifier (thus
694 .B %ms
696 \fB%m[\fP\fIrange\fP\fB]\fP).
697 The caller must
698 .BR free (3)
699 the returned string, as in the following example:
701 .in +4n
703 char *p;
704 int n;
706 errno = 0;
707 n = sscanf(str, "%m[a\-z]", &p);
708 if (n == 1) {
709     printf("read: %s\en", p);
710     free(p);
711 } else if (errno != 0) {
712     perror("sscanf");
713 } else {
714     fprintf(stderr, "No matching characters\en");
719 As shown in the above example, it is necessary to call
720 .BR free (3)
721 only if the
722 .BR sscanf ()
723 call successfully read a string.
724 .SH SEE ALSO
725 .BR getc (3),
726 .BR printf (3),
727 .BR setlocale (3),
728 .BR strtod (3),
729 .BR strtol (3),
730 .BR strtoul (3)