OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [sscanf.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/*
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
.   h       d, i, o, u, x     convert input to short,
156
.                             store in short object
157
.
158
.   h       D, I, O, U, X     no effect
159
.           e, f, c, s, n, p
160
.
161
.   l       d, i, o, u, x     convert input to long,
162
.                             store in long object
163
.
164
.   l       e, f, g           convert input to double
165
.                             store in a double object
166
.
167
.   l       D, I, O, U, X     no effect
168
.           c, s, n, p
169
.
170
.   L       d, i, o, u, x     convert to long double,
171
.                             store in long double
172
.
173
.   L      all others         no effect
174
 
175
 
176
        o <[type]>
177
 
178
                A character to specify what kind of conversion
179
                <<scanf>> performs.  Here is a table of the conversion
180
                characters:
181
 
182
                o+
183
                o  %
184
                No conversion is done; the percent character (<<%>>) is stored.
185
 
186
                o c
187
                Scans one character.  Corresponding <[arg]>: <<(char *arg)>>.
188
 
189
                o s
190
                Reads a character string into the array supplied.
191
                Corresponding <[arg]>: <<(char arg[])>>.
192
 
193
                o  [<[pattern]>]
194
                Reads a non-empty character string into memory
195
                starting at <[arg]>.  This area must be large
196
                enough to accept the sequence and a
197
                terminating null character which will be added
198
                automatically.  (<[pattern]> is discussed in the paragraph following
199
                this table). Corresponding <[arg]>: <<(char *arg)>>.
200
 
201
                o d
202
                Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
203
 
204
                o D
205
                Reads a decimal integer into the corresponding
206
                <[arg]>: <<(long *arg)>>.
207
 
208
                o o
209
                Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
210
 
211
                o O
212
                Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
213
 
214
                o u
215
                Reads an unsigned decimal integer into the corresponding
216
                <[arg]>: <<(unsigned int *arg)>>.
217
 
218
 
219
                o U
220
                Reads an unsigned decimal integer into the corresponding <[arg]>:
221
                <<(unsigned long *arg)>>.
222
 
223
                o x,X
224
                Read a hexadecimal integer into the corresponding <[arg]>:
225
                <<(int *arg)>>.
226
 
227
                o e, f, g
228
                Read a floating point number into the corresponding <[arg]>:
229
                <<(float *arg)>>.
230
 
231
                o E, F, G
232
                Read a floating point number into the corresponding <[arg]>:
233
                <<(double *arg)>>.
234
 
235
                o  i
236
                Reads a decimal, octal or hexadecimal integer into the
237
                corresponding <[arg]>: <<(int *arg)>>.
238
 
239
                o  I
240
                Reads a decimal, octal or hexadecimal integer into the
241
                corresponding <[arg]>: <<(long *arg)>>.
242
 
243
                o  n
244
                Stores the number of characters read in the corresponding
245
                <[arg]>: <<(int *arg)>>.
246
 
247
                o  p
248
                Stores a scanned pointer.  ANSI C leaves the details
249
                to each implementation; this implementation treats
250
                <<%p>> exactly the same as <<%U>>.  Corresponding
251
                <[arg]>: <<(void **arg)>>.
252
                o-
253
 
254
        A <[pattern]> of characters surrounded by square brackets can be used
255
        instead of the <<s>> type character.  <[pattern]> is a set of
256
        characters which define a search set of possible characters making up
257
        the <<scanf>> input field.  If the first character in the brackets is a
258
        caret (<<^>>), the search set is inverted to include all ASCII characters
259
        except those between the brackets.  There is also a range facility
260
        which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
261
        The hyphen must not be the first or last character in the set.
262
        The character prior to the hyphen must be lexically less than the
263
        character after it.
264
 
265
        Here are some <[pattern]> examples:
266
                o+
267
                o %[abcd]
268
                matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
269
 
270
                o %[^abcd]
271
                matches strings containing any characters except <<a>>, <<b>>,
272
                <<c>>, or <<d>>
273
 
274
                o %[A-DW-Z]
275
                matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
276
                <<X>>, <<Y>>, <<Z>>
277
 
278
                o %[z-a]
279
                matches the characters  <<z>>, <<->>, and <<a>>
280
                o-
281
 
282
        Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
283
        <<F>>, <<G>>) must correspond to the following general form:
284
 
285
.               [+/-] ddddd[.]ddd [E|e[+|-]ddd]
286
 
287
        where objects inclosed in square brackets are optional, and <<ddd>>
288
        represents decimal, octal, or hexadecimal digits.
289
        o-
290
 
291
RETURNS
292
        <<scanf>> returns the number of input fields successfully
293
        scanned, converted and stored; the return value does
294
        not include scanned fields which were not stored.
295
 
296
        If <<scanf>> attempts to read at end-of-file, the return
297
        value is <<EOF>>.
298
 
299
        If no fields were stored, the return value is <<0>>.
300
 
301
        <<scanf>> might stop scanning a particular field before
302
        reaching the normal field end character, or may
303
        terminate entirely.
304
 
305
        <<scanf>> stops scanning and storing the current field
306
        and moves to the next input field (if any)
307
        in any of the following situations:
308
 
309
        O+
310
        o       The assignment suppressing character (<<*>>) appears
311
        after the <<%>> in the format specification; the current
312
        input field is scanned but not stored.
313
 
314
        o       <[width]> characters have been read (<[width]> is a
315
        width specification, a positive decimal integer).
316
 
317
        o       The next character read cannot be converted
318
        under the the current format (for example,
319
        if a <<Z>> is read when the format is decimal).
320
 
321
        o       The next character in the input field does not appear
322
        in the search set (or does appear in the inverted search set).
323
        O-
324
 
325
        When <<scanf>> stops scanning the current input field for one of
326
        these reasons, the next character is considered unread and
327
        used as the first character of the following input field, or the
328
        first character in a subsequent read operation on the input.
329
 
330
        <<scanf>> will terminate under the following circumstances:
331
 
332
        O+
333
        o       The next character in the input field conflicts
334
        with a corresponding non-whitespace character in the
335
        format string.
336
 
337
        o       The next character in the input field is <<EOF>>.
338
 
339
        o       The format string has been exhausted.
340
        O-
341
 
342
        When the format string contains a character sequence that is
343
        not part of a format specification, the same character
344
        sequence must appear in the input; <<scanf>> will
345
        scan but not store the matched characters.  If a
346
        conflict occurs, the first conflicting character remains in the input
347
        as if it had never been read.
348
 
349
PORTABILITY
350
<<scanf>> is ANSI C.
351
 
352
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
353
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
354
*/
355
 
356
#include <_ansi.h>
357
#include <reent.h>
358
#include <stdio.h>
359
#include <string.h>
360
#ifdef _HAVE_STDC
361
#include <stdarg.h>
362
#else
363
#include <varargs.h>
364
#endif
365
#include "local.h"
366
 
367
/* | ARGSUSED */
368
/*SUPPRESS 590*/
369
static
370
_READ_WRITE_RETURN_TYPE
371
eofread (cookie, buf, len)
372
     _PTR cookie;
373
     char *buf;
374
     int len;
375
{
376
  return 0;
377
}
378
 
379
#ifndef _REENT_ONLY 
380
 
381
#ifdef _HAVE_STDC
382
int
383
_DEFUN (sscanf, (str, fmt), _CONST char *str _AND _CONST char *fmt _DOTS)
384
#else
385
int
386
sscanf (str, fmt, va_alist)
387
     _CONST char *str;
388
     _CONST char *fmt;
389
     va_dcl
390
#endif
391
{
392
  int ret;
393
  va_list ap;
394
  FILE f;
395
 
396
  f._flags = __SRD;
397
  f._bf._base = f._p = (unsigned char *) str;
398
  f._bf._size = f._r = strlen (str);
399
  f._read = eofread;
400
  f._ub._base = NULL;
401
  f._lb._base = NULL;
402
  f._data = _REENT;
403
#ifdef _HAVE_STDC
404
  va_start (ap, fmt);
405
#else
406
  va_start (ap);
407
#endif
408
  ret = __svfscanf_r (_REENT, &f, fmt, ap);
409
  va_end (ap);
410
  return ret;
411
}
412
 
413
#endif /* !_REENT_ONLY */
414
 
415
#ifdef _HAVE_STDC
416
int
417
_DEFUN (_sscanf_r, (ptr, str, fmt), struct _reent *ptr _AND _CONST char *str _AND _CONST char *fmt _DOTS)
418
#else
419
int
420
_sscanf_r (ptr, str, fmt, va_alist)
421
     struct _reent *ptr;
422
     _CONST char *str;
423
     _CONST char *fmt;
424
     va_dcl
425
#endif
426
{
427
  int ret;
428
  va_list ap;
429
  FILE f;
430
 
431
  f._flags = __SRD;
432
  f._bf._base = f._p = (unsigned char *) str;
433
  f._bf._size = f._r = strlen (str);
434
  f._read = eofread;
435
  f._ub._base = NULL;
436
  f._lb._base = NULL;
437
  f._data = _REENT;
438
#ifdef _HAVE_STDC
439
  va_start (ap, fmt);
440
#else
441
  va_start (ap);
442
#endif
443
  ret = __svfscanf_r (ptr, &f, fmt, ap);
444
  va_end (ap);
445
  return ret;
446
}

powered by: WebSVN 2.1.0

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