OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [sscanf.c] - Blame information for rev 312

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.