]> git.saurik.com Git - apple/libc.git/blobdiff - stdio/scanf.3
Libc-594.9.5.tar.gz
[apple/libc.git] / stdio / scanf.3
index 776ab49628ee13f085153ec975ce6925a60c9df9..763a75300c1dd18d08521f58f87590e9fcbe2ca8 100644 (file)
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)scanf.3    8.2 (Berkeley) 12/11/93
-.\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.13 2001/10/01 16:08:59 ru Exp $
+.\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.24 2003/06/28 09:03:25 das Exp $
 .\"
-.Dd December 11, 1993
+.Dd January 4, 2003
 .Dt SCANF 3
 .Os
 .Sh NAME
-.Nm scanf ,
 .Nm fscanf ,
+.Nm scanf ,
 .Nm sscanf ,
+.Nm vfscanf ,
 .Nm vscanf ,
-.Nm vsscanf ,
-.Nm vfscanf
+.Nm vsscanf
 .Nd input format conversion
 .Sh LIBRARY
 .Lb libc
 .Sh SYNOPSIS
 .In stdio.h
 .Ft int
-.Fn scanf "const char *format" ...
+.Fo fscanf
+.Fa "FILE *restrict stream"
+.Fa "const char *restrict format" ...
+.Fc
 .Ft int
-.Fn fscanf "FILE *stream" "const char *format" ...
+.Fo scanf
+.Fa "const char *restrict format" ...
+.Fc
 .Ft int
-.Fn sscanf "const char *str" "const char *format" ...
+.Fo sscanf
+.Fa "const char *restrict s"
+.Fa "const char *restrict format" ...
+.Fc
 .In stdarg.h
+.In stdio.h
 .Ft int
-.Fn vscanf "const char *format" "va_list ap"
+.Fo vfscanf
+.Fa "FILE *restrict stream"
+.Fa "const char *restrict format"
+.Fa "va_list arg"
+.Fc
 .Ft int
-.Fn vsscanf "const char *str" "const char *format" "va_list ap"
+.Fo vscanf
+.Fa "const char *restrict format"
+.Fa "va_list arg"
+.Fc
 .Ft int
-.Fn vfscanf "FILE *stream" "const char *format" "va_list ap"
+.Fo vsscanf
+.Fa "const char *restrict s"
+.Fa "const char *restrict format"
+.Fa "va_list arg"
+.Fc
 .Sh DESCRIPTION
 The
 .Fn scanf
 family of functions scans input according to a
-.Fa format
+.Fa format ,
 as described below.
 This format may contain
 .Em conversion specifiers ;
@@ -80,14 +100,15 @@ The
 .Fn scanf
 function
 reads input from the standard input stream
-.Em stdin ,
+.Dv stdin ,
 .Fn fscanf
 reads input from the stream pointer
 .Fa stream ,
 and
 .Fn sscanf
 reads its input from the character string pointed to by
-.Fa str .
+.Fa s .
+.Pp
 The
 .Fn vfscanf
 function
@@ -108,12 +129,15 @@ the
 .Fn vprintf
 and
 .Fn vsprintf
-functions respectively.
+functions, respectively.
+.Pp
 Each successive
 .Em pointer
 argument must correspond properly with
 each successive conversion specifier
-(but see `suppression' below).
+(but see the
+.Cm *
+conversion below).
 All conversions are introduced by the
 .Cm %
 (percent sign) character.
@@ -130,59 +154,121 @@ Scanning stops
 when an input character does not match such a format character.
 Scanning also stops
 when an input conversion cannot be made (see below).
+.Pp
+Extended locale versions of these functions are documented in
+.Xr scanf_l 3 .
+See
+.Xr xlocale 3
+for more information.
 .Sh CONVERSIONS
 Following the
 .Cm %
-character introducing a conversion
+character introducing a conversion,
 there may be a number of
 .Em flag
 characters, as follows:
-.Bl -tag -width indent
+.Bl -tag -width ".Cm l No (ell)"
 .It Cm *
 Suppresses assignment.
 The conversion that follows occurs as usual, but no pointer is used;
 the result of the conversion is simply discarded.
+.It Cm hh
+Indicates that the conversion will be one of
+.Cm dioux
+or
+.Cm n
+and the next pointer is a pointer to a
+.Vt char
+(rather than
+.Vt int ) .
 .It Cm h
 Indicates that the conversion will be one of
 .Cm dioux
 or
 .Cm n
 and the next pointer is a pointer to a
-.Em short  int
+.Vt "short int"
 (rather than
-.Em int ) .
-.It Cm l
-Indicates either that the conversion will be one of
+.Vt int ) .
+.It Cm l No (ell)
+Indicates that the conversion will be one of
 .Cm dioux
 or
 .Cm n
 and the next pointer is a pointer to a
-.Em long  int
+.Vt "long int"
 (rather than
-.Em int ) ,
-or that the conversion will be one of
-.Cm efg
+.Vt int ) ,
+that the conversion will be one of
+.Cm a , e , f ,
+or
+.Cm g
 and the next pointer is a pointer to
-.Em double
+.Vt double
+(rather than
+.Vt float ) ,
+or that the conversion will be one of
+.Cm c ,
+.Cm s
+or
+.Cm \&[
+and the next pointer is a pointer to an array of
+.Vt wchar_t
+(rather than
+.Vt char ) .
+.It Cm ll No (ell ell)
+Indicates that the conversion will be one of
+.Cm dioux
+or
+.Cm n
+and the next pointer is a pointer to a
+.Vt "long long int"
 (rather than
-.Em float ) .
+.Vt int ) .
 .It Cm L
-Indicates that the conversion will be
-.Cm efg
+Indicates that the conversion will be one of
+.Cm a , e , f ,
+or
+.Cm g
 and the next pointer is a pointer to
-.Em long double .
-(This type is not implemented; the
-.Cm L
-flag is currently ignored.)
+.Vt "long double" .
+.It Cm j
+Indicates that the conversion will be one of
+.Cm dioux
+or
+.Cm n
+and the next pointer is a pointer to a
+.Vt intmax_t
+(rather than
+.Vt int ) .
+.It Cm t
+Indicates that the conversion will be one of
+.Cm dioux
+or
+.Cm n
+and the next pointer is a pointer to a
+.Vt ptrdiff_t
+(rather than
+.Vt int ) .
+.It Cm z
+Indicates that the conversion will be one of
+.Cm dioux
+or
+.Cm n
+and the next pointer is a pointer to a
+.Vt size_t
+(rather than
+.Vt int ) .
 .It Cm q
-Indicates either that the conversion will be one of
+(deprecated.)
+Indicates that the conversion will be one of
 .Cm dioux
 or
 .Cm n
 and the next pointer is a pointer to a
-.Em long long int
+.Vt "long long int"
 (rather than
-.Em int ) ,
+.Vt int ) .
 .El
 .Pp
 In addition to these flags,
@@ -192,9 +278,18 @@ between the
 .Cm %
 and the conversion.
 If no width is given,
-a default of `infinity' is used (with one exception, below);
-otherwise at most this many characters are scanned
+a default of
+.Dq infinity
+is used (with one exception, below);
+otherwise at most this many bytes are scanned
 in processing the conversion.
+In the case of the
+.Cm lc ,
+.Cm ls
+and
+.Cm l[
+conversions, the field width specifies the maximum number
+of multibyte characters that will be scanned.
 Before conversion begins,
 most conversions skip white space;
 this white space is not counted against the field width.
@@ -202,22 +297,23 @@ this white space is not counted against the field width.
 The following conversions are available:
 .Bl -tag -width XXXX
 .It Cm %
-Matches a literal `%'.
-That is, `%\&%' in the format string
-matches a single input `%' character.
+Matches a literal
+.Ql % .
+That is,
+.Dq Li %%
+in the format string
+matches a single input
+.Ql %
+character.
 No conversion is done, and assignment does not occur.
 .It Cm d
 Matches an optionally signed decimal integer;
 the next pointer must be a pointer to
-.Em int .
-.It Cm D
-Equivalent to
-.Cm ld ;
-this exists only for backwards compatibility.
+.Vt int .
 .It Cm i
 Matches an optionally signed integer;
 the next pointer must be a pointer to
-.Em int .
+.Vt int .
 The integer is read in base 16 if it begins
 with
 .Ql 0x
@@ -230,77 +326,73 @@ Only characters that correspond to the base are used.
 .It Cm o
 Matches an octal integer;
 the next pointer must be a pointer to
-.Em unsigned int .
-.It Cm O
-Equivalent to
-.Cm lo ;
-this exists for backwards compatibility.
+.Vt "unsigned int" .
 .It Cm u
 Matches an optionally signed decimal integer;
 the next pointer must be a pointer to
-.Em unsigned int .
-.It Cm x
+.Vt "unsigned int" .
+.It Cm x , X
 Matches an optionally signed hexadecimal integer;
 the next pointer must be a pointer to
-.Em unsigned int .
-.It Cm X
-Equivalent to
-.Cm lx ;
-this violates the
-.St -isoC ,
-but is backwards compatible with previous
-.Ux
-systems.
-.It Cm f
-Matches an optionally signed floating-point number;
-the next pointer must be a pointer to
-.Em float .
-.It Cm e
-Equivalent to
-.Cm f .
-.It Cm g
-Equivalent to
-.Cm f .
-.It Cm E
-Equivalent to
-.Cm lf ;
-this violates the
-.St -isoC ,
-but is backwards compatible with previous
-.Ux
-systems.
-.It Cm F
-Equivalent to
-.Cm lf ;
-this exists only for backwards compatibility.
+.Vt "unsigned int" .
+.It Cm a , A , e , E , f , F , g , G
+Matches a floating-point number in the style of
+.Xr strtod 3 .
+The next pointer must be a pointer to
+.Vt float
+(unless
+.Cm l
+or
+.Cm L
+is specified.)
 .It Cm s
 Matches a sequence of non-white-space characters;
 the next pointer must be a pointer to
-.Em char ,
+.Vt char ,
 and the array must be large enough to accept all the sequence and the
 terminating
 .Dv NUL
 character.
 The input string stops at white space
 or at the maximum field width, whichever occurs first.
+.Pp
+If an
+.Cm l
+qualifier is present, the next pointer must be a pointer to
+.Vt wchar_t ,
+into which the input will be placed after conversion by
+.Xr mbrtowc 3 .
+.It Cm S
+The same as
+.Cm ls .
 .It Cm c
 Matches a sequence of
 .Em width
 count
 characters (default 1);
 the next pointer must be a pointer to
-.Em char ,
+.Vt char ,
 and there must be enough room for all the characters
 (no terminating
 .Dv NUL
 is added).
 The usual skip of leading white space is suppressed.
 To skip white space first, use an explicit space in the format.
+.Pp
+If an
+.Cm l
+qualifier is present, the next pointer must be a pointer to
+.Vt wchar_t ,
+into which the input will be placed after conversion by
+.Xr mbrtowc 3 .
+.It Cm C
+The same as
+.Cm lc .
 .It Cm \&[
 Matches a nonempty sequence of characters from the specified set
 of accepted characters;
 the next pointer must be a pointer to
-.Em char ,
+.Vt char ,
 and there must be enough room for all the characters in the string,
 plus a terminating
 .Dv NUL
@@ -333,24 +425,32 @@ To include a hyphen,
 make it the last character before the final close bracket.
 For instance,
 .Ql [^]0-9-]
-means the set `everything except close bracket, zero through nine,
-and hyphen'.
+means the set
+.Dq "everything except close bracket, zero through nine, and hyphen" .
 The string ends with the appearance of a character not in the
 (or, with a circumflex, in) set
 or when the field width runs out.
+.Pp
+If an
+.Cm l
+qualifier is present, the next pointer must be a pointer to
+.Vt wchar_t ,
+into which the input will be placed after conversion by
+.Xr mbrtowc 3 .
 .It Cm p
 Matches a pointer value (as printed by
 .Ql %p
 in
 .Xr printf 3 ) ;
 the next pointer must be a pointer to
-.Em void .
+.Vt "void *"
+(or other pointer type).
 .It Cm n
 Nothing is expected;
 instead, the number of characters consumed thus far from the input
 is stored through the next pointer,
 which must be a pointer to
-.Em int .
+.Vt int .
 This is
 .Em not
 a conversion, although it can be suppressed with the
@@ -362,38 +462,17 @@ The decimal point
 character is defined in the program's locale (category
 .Dv LC_NUMERIC ) .
 .Pp
-For backwards compatibility,
-other conversion characters (except
-.Ql \e0 )
-are taken as if they were
-.Ql %d
-or, if uppercase,
-.Ql %ld ,
-and a `conversion' of
+For backwards compatibility, a
+.Dq conversion
+of
 .Ql %\e0
 causes an immediate return of
 .Dv EOF .
-The
-.Cm F
-and
-.Cm X
-conversions will be changed in the future
-to conform to the
-.Tn ANSI
-C standard,
-after which they will act like
-.Cm f
-and
-.Cm x
-respectively.
 .Sh RETURN VALUES
-These
-functions
-return
-the number of input items assigned, which can be fewer than provided
-for, or even zero, in the event of a matching failure.
-Zero
-indicates that, while there was input available,
+These functions return the number of input items assigned.
+This can be fewer than provided for, or even zero,
+in the event of a matching failure.
+Zero indicates that, although there was input available,
 no conversions were assigned;
 typically this is due to an invalid input character,
 such as an alphabetic character for a
@@ -408,33 +487,42 @@ has begun,
 the number of conversions which were successfully completed is returned.
 .Sh SEE ALSO
 .Xr getc 3 ,
+.Xr mbrtowc 3 ,
 .Xr printf 3 ,
+.Xr scanf_l 3 ,
 .Xr strtod 3 ,
 .Xr strtol 3 ,
-.Xr strtoul 3
+.Xr strtoul 3 ,
+.Xr wscanf 3
 .Sh STANDARDS
 The functions
 .Fn fscanf ,
 .Fn scanf ,
-and
-.Fn sscanf
-conform to
-.St -isoC .
-.Sh HISTORY
-The functions
+.Fn sscanf ,
+.Fn vfscanf ,
 .Fn vscanf ,
-.Fn vsscanf
 and
-.Fn vfscanf
-are new to this release.
+.Fn vsscanf
+conform to
+.St -isoC-99 .
 .Sh BUGS
-The current situation with
-.Cm %F
+Earlier implementations of
+.Nm
+treated
+.Cm \&%D , \&%E , \&%F , \&%O
 and
-.Cm %X
-conversions is unfortunate.
-.Pp
-All of the backwards compatibility formats will be removed in the future.
+.Cm \&%X
+as their lowercase equivalents with an
+.Cm l
+modifier.
+In addition,
+.Nm
+treated an unknown conversion character as
+.Cm \&%d
+or
+.Cm \&%D ,
+depending on its case.
+This functionality has been removed.
 .Pp
 Numerical strings are truncated to 512 characters; for example,
 .Cm %f
@@ -444,3 +532,13 @@ are implicitly
 .Cm %512f
 and
 .Cm %512d .
+.Pp
+The
+.Cm %n$
+modifiers for positional arguments are not implemented.
+.Pp
+The
+.Nm
+family of functions do not correctly handle multibyte characters in the
+.Fa format
+argument.