Contiki 2.6

sscanf.c

00001 /*
00002  * Copyright (c) 1990 The Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms are permitted
00006  * provided that the above copyright notice and this paragraph are
00007  * duplicated in all such forms and that any documentation,
00008  * advertising materials, and other materials related to such
00009  * distribution and use acknowledge that the software was developed
00010  * by the University of California, Berkeley.  The name of the
00011  * University may not be used to endorse or promote products derived
00012  * from this software without specific prior written permission.
00013  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00014  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00015  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00016  */
00017 
00018 /*
00019 
00020 FUNCTION
00021         <<scanf>>, <<fscanf>>, <<sscanf>>---scan and format input
00022 
00023 INDEX
00024         scanf
00025 INDEX
00026         fscanf
00027 INDEX
00028         sscanf
00029 
00030 ANSI_SYNOPSIS
00031         #include <stdio.h>
00032 
00033         int scanf(const char *<[format]> [, <[arg]>, ...]);
00034         int fscanf(FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
00035         int sscanf(const char *<[str]>, const char *<[format]> 
00036                    [, <[arg]>, ...]);
00037 
00038         int _scanf_r(struct _reent *<[ptr]>, const char *<[format]> [, <[arg]>, ...]);
00039         int _fscanf_r(struct _reent *<[ptr]>, FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
00040         int _sscanf_r(struct _reent *<[ptr]>, const char *<[str]>, const char *<[format]> 
00041                    [, <[arg]>, ...]);
00042 
00043 
00044 TRAD_SYNOPSIS
00045         #include <stdio.h>
00046 
00047         int scanf(<[format]> [, <[arg]>, ...])
00048         char *<[format]>;
00049 
00050         int fscanf(<[fd]>, <[format]> [, <[arg]>, ...]);
00051         FILE *<[fd]>;
00052         char *<[format]>;
00053 
00054         int sscanf(<[str]>, <[format]> [, <[arg]>, ...]);
00055         char *<[str]>;
00056         char *<[format]>;
00057 
00058         int _scanf_r(<[ptr]>, <[format]> [, <[arg]>, ...])
00059         struct _reent *<[ptr]>;
00060         char *<[format]>;
00061 
00062         int _fscanf_r(<[ptr]>, <[fd]>, <[format]> [, <[arg]>, ...]);
00063         struct _reent *<[ptr]>;
00064         FILE *<[fd]>;
00065         char *<[format]>;
00066 
00067         int _sscanf_r(<[ptr]>, <[str]>, <[format]> [, <[arg]>, ...]);
00068         struct _reent *<[ptr]>;
00069         char *<[str]>;
00070         char *<[format]>;
00071 
00072 
00073 DESCRIPTION
00074         <<scanf>> scans a series of input fields from standard input,
00075                 one character at a time.  Each field is interpreted according to
00076                 a format specifier passed to <<scanf>> in the format string at
00077         <<*<[format]>>>.  <<scanf>> stores the interpreted input from
00078                 each field at the address passed to it as the corresponding argument
00079                 following <[format]>.  You must supply the same number of
00080                 format specifiers and address arguments as there are input fields.
00081 
00082         There must be sufficient address arguments for the given format
00083         specifiers; if not the results are unpredictable and likely
00084         disasterous.  Excess address arguments are merely ignored.
00085 
00086         <<scanf>> often produces unexpected results if the input diverges from
00087         an expected pattern. Since the combination of <<gets>> or <<fgets>>
00088         followed by <<sscanf>> is safe and easy, that is the preferred way
00089         to be certain that a program is synchronized with input at the end
00090                 of a line.
00091 
00092         <<fscanf>> and <<sscanf>> are identical to <<scanf>>, other than the
00093         source of input: <<fscanf>> reads from a file, and <<sscanf>>
00094                 from a string.
00095 
00096         The routines <<_scanf_r>>, <<_fscanf_r>>, and <<_sscanf_r>> are reentrant
00097         versions of <<scanf>>, <<fscanf>>, and <<sscanf>> that take an additional
00098         first argument pointing to a reentrancy structure.
00099 
00100         The string at <<*<[format]>>> is a character sequence composed
00101         of zero or more directives. Directives are composed of
00102         one or more whitespace characters, non-whitespace characters,
00103         and format specifications.
00104 
00105         Whitespace characters are blank (<< >>), tab (<<\t>>), or
00106                 newline (<<\n>>).
00107         When <<scanf>> encounters a whitespace character in the format string
00108         it will read (but not store) all consecutive whitespace characters
00109         up to the next non-whitespace character in the input.
00110 
00111         Non-whitespace characters are all other ASCII characters except the
00112         percent sign (<<%>>).  When <<scanf>> encounters a non-whitespace
00113         character in the format string it will read, but not store
00114         a matching non-whitespace character.
00115 
00116         Format specifications tell <<scanf>> to read and convert characters
00117         from the input field into specific types of values, and store then
00118         in the locations specified by the address arguments.
00119 
00120         Trailing whitespace is left unread unless explicitly
00121         matched in the format string.
00122 
00123         The format specifiers must begin with a percent sign (<<%>>)
00124         and have the following form:
00125 
00126 .       %[*][<[width]>][<[size]>]<[type]>
00127 
00128         Each format specification begins with the percent character (<<%>>).
00129         The other fields are:
00130         o+
00131                 o *
00132                 an optional marker; if present, it suppresses interpretation and
00133         assignment of this input field.
00134 
00135         o <[width]>
00136                 an optional maximum field width: a decimal integer,
00137                 which controls the maximum number of characters that
00138                 will be read before converting the current input field.  If the
00139                 input field has fewer than <[width]> characters, <<scanf>>
00140                 reads all the characters in the field, and then
00141                 proceeds with the next field and its format specification.
00142 
00143                 If a whitespace or a non-convertable character occurs
00144                 before <[width]> character are read, the characters up
00145                 to that character are read, converted, and stored.
00146                 Then <<scanf>> proceeds to the next format specification.
00147 
00148         o size
00149                 <<h>>, <<l>>, and <<L>> are optional size characters which
00150                 override the default way that <<scanf>> interprets the
00151                 data type of the corresponding argument.
00152 
00153 
00154 .Modifier   Type(s)
00155 .   hh      d, i, o, u, x, n  convert input to char,
00156 .                             store in char object
00157 .
00158 .   h       d, i, o, u, x, n  convert input to short,
00159 .                             store in short object
00160 .
00161 .   h       D, I, O, U, X     no effect
00162 .           e, f, c, s, p
00163 .
00164 .   l       d, i, o, u, x, n  convert input to long,
00165 .                             store in long object
00166 .
00167 .   l       e, f, g           convert input to double
00168 .                             store in a double object
00169 .
00170 .   l       D, I, O, U, X     no effect
00171 .           c, s, p
00172 .
00173 .   ll      d, i, o, u, x, n  convert to long long,
00174 .                             store in long long
00175 .
00176 .   L       d, i, o, u, x, n  convert to long long,
00177 .                             store in long long
00178 .
00179 .   L       e, f, g, E, G     convert to long double,
00180 .                             store in long double
00181 .
00182 .   L      all others         no effect
00183 
00184 
00185         o <[type]>
00186 
00187                 A character to specify what kind of conversion
00188                 <<scanf>> performs.  Here is a table of the conversion
00189                 characters:
00190 
00191                 o+
00192                 o  %
00193                 No conversion is done; the percent character (<<%>>) is stored.
00194 
00195                 o c
00196                 Scans one character.  Corresponding <[arg]>: <<(char *arg)>>.
00197 
00198                 o s
00199                 Reads a character string into the array supplied.
00200                 Corresponding <[arg]>: <<(char arg[])>>.
00201 
00202                 o  [<[pattern]>]
00203                 Reads a non-empty character string into memory
00204                 starting at <[arg]>.  This area must be large
00205                 enough to accept the sequence and a
00206                 terminating null character which will be added
00207                 automatically.  (<[pattern]> is discussed in the paragraph following
00208                 this table). Corresponding <[arg]>: <<(char *arg)>>.
00209 
00210                 o d
00211                 Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
00212 
00213                 o D
00214                 Reads a decimal integer into the corresponding
00215                 <[arg]>: <<(long *arg)>>.
00216 
00217                 o o
00218                 Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
00219 
00220                 o O
00221                 Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
00222 
00223                 o u
00224                 Reads an unsigned decimal integer into the corresponding
00225                 <[arg]>: <<(unsigned int *arg)>>.
00226                         
00227 
00228                 o U
00229                 Reads an unsigned decimal integer into the corresponding <[arg]>:
00230                 <<(unsigned long *arg)>>.
00231 
00232                 o x,X
00233                 Read a hexadecimal integer into the corresponding <[arg]>:
00234                 <<(int *arg)>>.
00235 
00236                 o e, f, g
00237                 Read a floating-point number into the corresponding <[arg]>:
00238                 <<(float *arg)>>.
00239 
00240                 o E, F, G
00241                 Read a floating-point number into the corresponding <[arg]>:
00242                 <<(double *arg)>>.
00243 
00244                 o  i
00245                 Reads a decimal, octal or hexadecimal integer into the
00246                 corresponding <[arg]>: <<(int *arg)>>.
00247 
00248                 o  I
00249                 Reads a decimal, octal or hexadecimal integer into the
00250                 corresponding <[arg]>: <<(long *arg)>>.
00251 
00252                 o  n
00253                 Stores the number of characters read in the corresponding
00254                 <[arg]>: <<(int *arg)>>.
00255 
00256                 o  p
00257                 Stores a scanned pointer.  ANSI C leaves the details
00258                 to each implementation; this implementation treats
00259                 <<%p>> exactly the same as <<%U>>.  Corresponding
00260                 <[arg]>: <<(void **arg)>>.  
00261                 o-
00262 
00263         A <[pattern]> of characters surrounded by square brackets can be used
00264         instead of the <<s>> type character.  <[pattern]> is a set of
00265         characters which define a search set of possible characters making up
00266         the <<scanf>> input field.  If the first character in the brackets is a
00267         caret (<<^>>), the search set is inverted to include all ASCII characters
00268         except those between the brackets.  There is also a range facility
00269         which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
00270         The hyphen must not be the first or last character in the set.
00271         The character prior to the hyphen must be lexically less than the
00272         character after it.
00273 
00274         Here are some <[pattern]> examples:
00275                 o+
00276                 o %[abcd]
00277                 matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
00278 
00279                 o %[^abcd]
00280                 matches strings containing any characters except <<a>>, <<b>>,
00281                 <<c>>, or <<d>>
00282 
00283                 o %[A-DW-Z]
00284                 matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
00285                 <<X>>, <<Y>>, <<Z>>
00286 
00287                 o %[z-a]
00288                 matches the characters  <<z>>, <<->>, and <<a>>
00289                 o-
00290 
00291         Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
00292         <<F>>, <<G>>) must correspond to the following general form:
00293 
00294 .               [+/-] ddddd[.]ddd [E|e[+|-]ddd]
00295 
00296         where objects inclosed in square brackets are optional, and <<ddd>>
00297         represents decimal, octal, or hexadecimal digits.
00298         o-
00299 
00300 RETURNS
00301         <<scanf>> returns the number of input fields successfully
00302         scanned, converted and stored; the return value does
00303         not include scanned fields which were not stored.
00304 
00305         If <<scanf>> attempts to read at end-of-file, the return
00306         value is <<EOF>>.
00307 
00308         If no fields were stored, the return value is <<0>>.
00309 
00310         <<scanf>> might stop scanning a particular field before
00311         reaching the normal field end character, or may
00312         terminate entirely.
00313 
00314         <<scanf>> stops scanning and storing the current field
00315         and moves to the next input field (if any)
00316         in any of the following situations:
00317 
00318         O+
00319         o       The assignment suppressing character (<<*>>) appears
00320         after the <<%>> in the format specification; the current
00321         input field is scanned but not stored.
00322 
00323         o       <[width]> characters have been read (<[width]> is a
00324         width specification, a positive decimal integer).
00325 
00326         o       The next character read cannot be converted
00327         under the the current format (for example,
00328         if a <<Z>> is read when the format is decimal).
00329 
00330         o       The next character in the input field does not appear
00331         in the search set (or does appear in the inverted search set).
00332         O-
00333 
00334         When <<scanf>> stops scanning the current input field for one of
00335         these reasons, the next character is considered unread and
00336         used as the first character of the following input field, or the
00337         first character in a subsequent read operation on the input.
00338 
00339         <<scanf>> will terminate under the following circumstances:
00340 
00341         O+
00342         o       The next character in the input field conflicts
00343         with a corresponding non-whitespace character in the
00344         format string.
00345 
00346         o       The next character in the input field is <<EOF>>.
00347 
00348         o       The format string has been exhausted.
00349         O-
00350 
00351         When the format string contains a character sequence that is
00352         not part of a format specification, the same character
00353         sequence must appear in the input; <<scanf>> will
00354         scan but not store the matched characters.  If a
00355         conflict occurs, the first conflicting character remains in the input
00356         as if it had never been read.
00357 
00358 PORTABILITY
00359 <<scanf>> is ANSI C.
00360 
00361 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
00362 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
00363 */
00364 
00365 #include <_ansi.h>
00366 #include <reent.h>
00367 #include <stdio.h>
00368 #include <string.h>
00369 #ifdef _HAVE_STDC
00370 #include <stdarg.h>
00371 #else
00372 #include <varargs.h>
00373 #endif
00374 #include "local.h"
00375 
00376 /* | ARGSUSED */
00377 /*SUPPRESS 590*/
00378 static
00379 _READ_WRITE_RETURN_TYPE
00380 eofread (cookie, buf, len)
00381      _PTR cookie;
00382      char *buf;
00383      int len;
00384 {
00385   return 0;
00386 }
00387 
00388 #ifndef _REENT_ONLY 
00389 
00390 #ifdef _HAVE_STDC
00391 int 
00392 _DEFUN (sscanf, (str, fmt), _CONST char *str _AND _CONST char *fmt _DOTS)
00393 #else
00394 int 
00395 sscanf (str, fmt, va_alist)
00396      _CONST char *str;
00397      _CONST char *fmt;
00398      va_dcl
00399 #endif
00400 {
00401   int ret;
00402   va_list ap;
00403   FILE f;
00404 
00405   f._flags = __SRD;
00406   f._bf._base = f._p = (unsigned char *) str;
00407   f._bf._size = f._r = strlen (str);
00408   f._read = eofread;
00409   f._ub._base = NULL;
00410   f._lb._base = NULL;
00411 #ifdef _HAVE_STDC
00412   va_start (ap, fmt);
00413 #else
00414   va_start (ap);
00415 #endif
00416   ret = __svfscanf_r (_REENT, &f, fmt, ap);
00417   va_end (ap);
00418   return ret;
00419 }
00420 
00421 #endif /* !_REENT_ONLY */
00422 
00423 #ifdef _HAVE_STDC
00424 int 
00425 _DEFUN (_sscanf_r, (ptr, str, fmt), struct _reent *ptr _AND _CONST char *str _AND _CONST char *fmt _DOTS)
00426 #else
00427 int 
00428 _sscanf_r (ptr, str, fmt, va_alist)
00429      struct _reent *ptr;
00430      _CONST char *str;
00431      _CONST char *fmt;
00432      va_dcl
00433 #endif
00434 {
00435   int ret;
00436   va_list ap;
00437   FILE f;
00438 
00439   f._flags = __SRD;
00440   f._bf._base = f._p = (unsigned char *) str;
00441   f._bf._size = f._r = strlen (str);
00442   f._read = eofread;
00443   f._ub._base = NULL;
00444   f._lb._base = NULL;
00445 #ifdef _HAVE_STDC
00446   va_start (ap, fmt);
00447 #else
00448   va_start (ap);
00449 #endif
00450   ret = __svfscanf_r (ptr, &f, fmt, ap);
00451   va_end (ap);
00452   return ret;
00453 }