Contiki 2.5
sscanf.c
1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley. The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 /*
19 
20 FUNCTION
21  <<scanf>>, <<fscanf>>, <<sscanf>>---scan and format input
22 
23 INDEX
24  scanf
25 INDEX
26  fscanf
27 INDEX
28  sscanf
29 
30 ANSI_SYNOPSIS
31  #include <stdio.h>
32 
33  int scanf(const char *<[format]> [, <[arg]>, ...]);
34  int fscanf(FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
35  int sscanf(const char *<[str]>, const char *<[format]>
36  [, <[arg]>, ...]);
37 
38  int _scanf_r(struct _reent *<[ptr]>, const char *<[format]> [, <[arg]>, ...]);
39  int _fscanf_r(struct _reent *<[ptr]>, FILE *<[fd]>, const char *<[format]> [, <[arg]>, ...]);
40  int _sscanf_r(struct _reent *<[ptr]>, const char *<[str]>, const char *<[format]>
41  [, <[arg]>, ...]);
42 
43 
44 TRAD_SYNOPSIS
45  #include <stdio.h>
46 
47  int scanf(<[format]> [, <[arg]>, ...])
48  char *<[format]>;
49 
50  int fscanf(<[fd]>, <[format]> [, <[arg]>, ...]);
51  FILE *<[fd]>;
52  char *<[format]>;
53 
54  int sscanf(<[str]>, <[format]> [, <[arg]>, ...]);
55  char *<[str]>;
56  char *<[format]>;
57 
58  int _scanf_r(<[ptr]>, <[format]> [, <[arg]>, ...])
59  struct _reent *<[ptr]>;
60  char *<[format]>;
61 
62  int _fscanf_r(<[ptr]>, <[fd]>, <[format]> [, <[arg]>, ...]);
63  struct _reent *<[ptr]>;
64  FILE *<[fd]>;
65  char *<[format]>;
66 
67  int _sscanf_r(<[ptr]>, <[str]>, <[format]> [, <[arg]>, ...]);
68  struct _reent *<[ptr]>;
69  char *<[str]>;
70  char *<[format]>;
71 
72 
73 DESCRIPTION
74  <<scanf>> scans a series of input fields from standard input,
75  one character at a time. Each field is interpreted according to
76  a format specifier passed to <<scanf>> in the format string at
77  <<*<[format]>>>. <<scanf>> stores the interpreted input from
78  each field at the address passed to it as the corresponding argument
79  following <[format]>. You must supply the same number of
80  format specifiers and address arguments as there are input fields.
81 
82  There must be sufficient address arguments for the given format
83  specifiers; if not the results are unpredictable and likely
84  disasterous. Excess address arguments are merely ignored.
85 
86  <<scanf>> often produces unexpected results if the input diverges from
87  an expected pattern. Since the combination of <<gets>> or <<fgets>>
88  followed by <<sscanf>> is safe and easy, that is the preferred way
89  to be certain that a program is synchronized with input at the end
90  of a line.
91 
92  <<fscanf>> and <<sscanf>> are identical to <<scanf>>, other than the
93  source of input: <<fscanf>> reads from a file, and <<sscanf>>
94  from a string.
95 
96  The routines <<_scanf_r>>, <<_fscanf_r>>, and <<_sscanf_r>> are reentrant
97  versions of <<scanf>>, <<fscanf>>, and <<sscanf>> that take an additional
98  first argument pointing to a reentrancy structure.
99 
100  The string at <<*<[format]>>> is a character sequence composed
101  of zero or more directives. Directives are composed of
102  one or more whitespace characters, non-whitespace characters,
103  and format specifications.
104 
105  Whitespace characters are blank (<< >>), tab (<<\t>>), or
106  newline (<<\n>>).
107  When <<scanf>> encounters a whitespace character in the format string
108  it will read (but not store) all consecutive whitespace characters
109  up to the next non-whitespace character in the input.
110 
111  Non-whitespace characters are all other ASCII characters except the
112  percent sign (<<%>>). When <<scanf>> encounters a non-whitespace
113  character in the format string it will read, but not store
114  a matching non-whitespace character.
115 
116  Format specifications tell <<scanf>> to read and convert characters
117  from the input field into specific types of values, and store then
118  in the locations specified by the address arguments.
119 
120  Trailing whitespace is left unread unless explicitly
121  matched in the format string.
122 
123  The format specifiers must begin with a percent sign (<<%>>)
124  and have the following form:
125 
126 . %[*][<[width]>][<[size]>]<[type]>
127 
128  Each format specification begins with the percent character (<<%>>).
129  The other fields are:
130  o+
131  o *
132  an optional marker; if present, it suppresses interpretation and
133  assignment of this input field.
134 
135  o <[width]>
136  an optional maximum field width: a decimal integer,
137  which controls the maximum number of characters that
138  will be read before converting the current input field. If the
139  input field has fewer than <[width]> characters, <<scanf>>
140  reads all the characters in the field, and then
141  proceeds with the next field and its format specification.
142 
143  If a whitespace or a non-convertable character occurs
144  before <[width]> character are read, the characters up
145  to that character are read, converted, and stored.
146  Then <<scanf>> proceeds to the next format specification.
147 
148  o size
149  <<h>>, <<l>>, and <<L>> are optional size characters which
150  override the default way that <<scanf>> interprets the
151  data type of the corresponding argument.
152 
153 
154 .Modifier Type(s)
155 . hh d, i, o, u, x, n convert input to char,
156 . store in char object
157 .
158 . h d, i, o, u, x, n convert input to short,
159 . store in short object
160 .
161 . h D, I, O, U, X no effect
162 . e, f, c, s, p
163 .
164 . l d, i, o, u, x, n convert input to long,
165 . store in long object
166 .
167 . l e, f, g convert input to double
168 . store in a double object
169 .
170 . l D, I, O, U, X no effect
171 . c, s, p
172 .
173 . ll d, i, o, u, x, n convert to long long,
174 . store in long long
175 .
176 . L d, i, o, u, x, n convert to long long,
177 . store in long long
178 .
179 . L e, f, g, E, G convert to long double,
180 . store in long double
181 .
182 . L all others no effect
183 
184 
185  o <[type]>
186 
187  A character to specify what kind of conversion
188  <<scanf>> performs. Here is a table of the conversion
189  characters:
190 
191  o+
192  o %
193  No conversion is done; the percent character (<<%>>) is stored.
194 
195  o c
196  Scans one character. Corresponding <[arg]>: <<(char *arg)>>.
197 
198  o s
199  Reads a character string into the array supplied.
200  Corresponding <[arg]>: <<(char arg[])>>.
201 
202  o [<[pattern]>]
203  Reads a non-empty character string into memory
204  starting at <[arg]>. This area must be large
205  enough to accept the sequence and a
206  terminating null character which will be added
207  automatically. (<[pattern]> is discussed in the paragraph following
208  this table). Corresponding <[arg]>: <<(char *arg)>>.
209 
210  o d
211  Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
212 
213  o D
214  Reads a decimal integer into the corresponding
215  <[arg]>: <<(long *arg)>>.
216 
217  o o
218  Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
219 
220  o O
221  Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
222 
223  o u
224  Reads an unsigned decimal integer into the corresponding
225  <[arg]>: <<(unsigned int *arg)>>.
226 
227 
228  o U
229  Reads an unsigned decimal integer into the corresponding <[arg]>:
230  <<(unsigned long *arg)>>.
231 
232  o x,X
233  Read a hexadecimal integer into the corresponding <[arg]>:
234  <<(int *arg)>>.
235 
236  o e, f, g
237  Read a floating-point number into the corresponding <[arg]>:
238  <<(float *arg)>>.
239 
240  o E, F, G
241  Read a floating-point number into the corresponding <[arg]>:
242  <<(double *arg)>>.
243 
244  o i
245  Reads a decimal, octal or hexadecimal integer into the
246  corresponding <[arg]>: <<(int *arg)>>.
247 
248  o I
249  Reads a decimal, octal or hexadecimal integer into the
250  corresponding <[arg]>: <<(long *arg)>>.
251 
252  o n
253  Stores the number of characters read in the corresponding
254  <[arg]>: <<(int *arg)>>.
255 
256  o p
257  Stores a scanned pointer. ANSI C leaves the details
258  to each implementation; this implementation treats
259  <<%p>> exactly the same as <<%U>>. Corresponding
260  <[arg]>: <<(void **arg)>>.
261  o-
262 
263  A <[pattern]> of characters surrounded by square brackets can be used
264  instead of the <<s>> type character. <[pattern]> is a set of
265  characters which define a search set of possible characters making up
266  the <<scanf>> input field. If the first character in the brackets is a
267  caret (<<^>>), the search set is inverted to include all ASCII characters
268  except those between the brackets. There is also a range facility
269  which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
270  The hyphen must not be the first or last character in the set.
271  The character prior to the hyphen must be lexically less than the
272  character after it.
273 
274  Here are some <[pattern]> examples:
275  o+
276  o %[abcd]
277  matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
278 
279  o %[^abcd]
280  matches strings containing any characters except <<a>>, <<b>>,
281  <<c>>, or <<d>>
282 
283  o %[A-DW-Z]
284  matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
285  <<X>>, <<Y>>, <<Z>>
286 
287  o %[z-a]
288  matches the characters <<z>>, <<->>, and <<a>>
289  o-
290 
291  Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
292  <<F>>, <<G>>) must correspond to the following general form:
293 
294 . [+/-] ddddd[.]ddd [E|e[+|-]ddd]
295 
296  where objects inclosed in square brackets are optional, and <<ddd>>
297  represents decimal, octal, or hexadecimal digits.
298  o-
299 
300 RETURNS
301  <<scanf>> returns the number of input fields successfully
302  scanned, converted and stored; the return value does
303  not include scanned fields which were not stored.
304 
305  If <<scanf>> attempts to read at end-of-file, the return
306  value is <<EOF>>.
307 
308  If no fields were stored, the return value is <<0>>.
309 
310  <<scanf>> might stop scanning a particular field before
311  reaching the normal field end character, or may
312  terminate entirely.
313 
314  <<scanf>> stops scanning and storing the current field
315  and moves to the next input field (if any)
316  in any of the following situations:
317 
318  O+
319  o The assignment suppressing character (<<*>>) appears
320  after the <<%>> in the format specification; the current
321  input field is scanned but not stored.
322 
323  o <[width]> characters have been read (<[width]> is a
324  width specification, a positive decimal integer).
325 
326  o The next character read cannot be converted
327  under the the current format (for example,
328  if a <<Z>> is read when the format is decimal).
329 
330  o The next character in the input field does not appear
331  in the search set (or does appear in the inverted search set).
332  O-
333 
334  When <<scanf>> stops scanning the current input field for one of
335  these reasons, the next character is considered unread and
336  used as the first character of the following input field, or the
337  first character in a subsequent read operation on the input.
338 
339  <<scanf>> will terminate under the following circumstances:
340 
341  O+
342  o The next character in the input field conflicts
343  with a corresponding non-whitespace character in the
344  format string.
345 
346  o The next character in the input field is <<EOF>>.
347 
348  o The format string has been exhausted.
349  O-
350 
351  When the format string contains a character sequence that is
352  not part of a format specification, the same character
353  sequence must appear in the input; <<scanf>> will
354  scan but not store the matched characters. If a
355  conflict occurs, the first conflicting character remains in the input
356  as if it had never been read.
357 
358 PORTABILITY
359 <<scanf>> is ANSI C.
360 
361 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
362 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
363 */
364 
365 #include <_ansi.h>
366 #include <reent.h>
367 #include <stdio.h>
368 #include <string.h>
369 #ifdef _HAVE_STDC
370 #include <stdarg.h>
371 #else
372 #include <varargs.h>
373 #endif
374 #include "local.h"
375 
376 /* | ARGSUSED */
377 /*SUPPRESS 590*/
378 static
379 _READ_WRITE_RETURN_TYPE
380 eofread (cookie, buf, len)
381  _PTR cookie;
382  char *buf;
383  int len;
384 {
385  return 0;
386 }
387 
388 #ifndef _REENT_ONLY
389 
390 #ifdef _HAVE_STDC
391 int
392 _DEFUN (sscanf, (str, fmt), _CONST char *str _AND _CONST char *fmt _DOTS)
393 #else
394 int
395 sscanf (str, fmt, va_alist)
396  _CONST char *str;
397  _CONST char *fmt;
398  va_dcl
399 #endif
400 {
401  int ret;
402  va_list ap;
403  FILE f;
404 
405  f._flags = __SRD;
406  f._bf._base = f._p = (unsigned char *) str;
407  f._bf._size = f._r = strlen (str);
408  f._read = eofread;
409  f._ub._base = NULL;
410  f._lb._base = NULL;
411 #ifdef _HAVE_STDC
412  va_start (ap, fmt);
413 #else
414  va_start (ap);
415 #endif
416  ret = __svfscanf_r (_REENT, &f, fmt, ap);
417  va_end (ap);
418  return ret;
419 }
420 
421 #endif /* !_REENT_ONLY */
422 
423 #ifdef _HAVE_STDC
424 int
425 _DEFUN (_sscanf_r, (ptr, str, fmt), struct _reent *ptr _AND _CONST char *str _AND _CONST char *fmt _DOTS)
426 #else
427 int
428 _sscanf_r (ptr, str, fmt, va_alist)
429  struct _reent *ptr;
430  _CONST char *str;
431  _CONST char *fmt;
432  va_dcl
433 #endif
434 {
435  int ret;
436  va_list ap;
437  FILE f;
438 
439  f._flags = __SRD;
440  f._bf._base = f._p = (unsigned char *) str;
441  f._bf._size = f._r = strlen (str);
442  f._read = eofread;
443  f._ub._base = NULL;
444  f._lb._base = NULL;
445 #ifdef _HAVE_STDC
446  va_start (ap, fmt);
447 #else
448  va_start (ap);
449 #endif
450  ret = __svfscanf_r (ptr, &f, fmt, ap);
451  va_end (ap);
452  return ret;
453 }