Rev 4874 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 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 | FUNCTION |
||
20 | < |
||
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 |
||
37 | |||
4921 | Serge | 38 | int scanf(const char *restrict <[format]>, ...); |
39 | int fscanf(FILE *restrict <[fd]>, const char *restrict <[format]>, ...); |
||
40 | int sscanf(const char *restrict <[str]>, const char *restrict <[format]>, ...); |
||
4349 | Serge | 41 | |
4921 | Serge | 42 | int _scanf_r(struct _reent *<[ptr]>, const char *restrict <[format]>, ...); |
43 | int _fscanf_r(struct _reent *<[ptr]>, FILE *restrict <[fd]>, |
||
44 | const char *restrict <[format]>, ...); |
||
45 | int _sscanf_r(struct _reent *<[ptr]>, const char *restrict <[str]>, |
||
46 | const char *restrict <[format]>, ...); |
||
4349 | Serge | 47 | |
48 | |||
49 | TRAD_SYNOPSIS |
||
50 | #include |
||
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 | < |
||
80 | one character at a time. Each field is interpreted according to |
||
81 | a format specifier passed to < |
||
82 | <<*<[format]>>>. < |
||
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 | < |
||
92 | an expected pattern. Since the combination of < |
||
93 | followed by < |
||
94 | to be certain that a program is synchronized with input at the end |
||
95 | of a line. |
||
96 | |||
97 | < |
||
98 | source of input: < |
||
99 | from a string. |
||
100 | |||
101 | The routines <<_scanf_r>>, <<_fscanf_r>>, and <<_sscanf_r>> are reentrant |
||
102 | versions of < |
||
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 < |
||
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 < |
||
118 | character in the format string it will read, but not store |
||
119 | a matching non-whitespace character. |
||
120 | |||
121 | Format specifications tell < |
||
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, < |
||
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 < |
||
152 | |||
153 | o size |
||
154 | < |
||
155 | characters which override the default way that < |
||
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 | < |
||
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 < |
||
286 | characters which define a search set of possible characters making up |
||
287 | the < |
||
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 <>, <>, < |
||
299 | |||
300 | o %[^abcd] |
||
301 | matches strings containing any characters except <>, <>, |
||
302 | < |
||
303 | |||
304 | o %[A-DW-Z] |
||
305 | matches strings containing <>, <>, < |
||
306 | < |
||
307 | |||
308 | o %[z-a] |
||
309 | matches the characters < |
||
310 | o- |
||
311 | |||
312 | Floating point numbers (for field types < |
||
313 | < |
||
314 | |||
315 | . [+/-] ddddd[.]ddd [E|e[+|-]ddd] |
||
316 | |||
317 | where objects inclosed in square brackets are optional, and < |
||
318 | represents decimal, octal, or hexadecimal digits. |
||
319 | o- |
||
320 | |||
321 | RETURNS |
||
322 | < |
||
323 | scanned, converted and stored; the return value does |
||
324 | not include scanned fields which were not stored. |
||
325 | |||
326 | If < |
||
327 | value is < |
||
328 | |||
329 | If no fields were stored, the return value is <<0>>. |
||
330 | |||
331 | < |
||
332 | reaching the normal field end character, or may |
||
333 | terminate entirely. |
||
334 | |||
335 | < |
||
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 < |
||
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 < |
||
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 | < |
||
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 < |
||
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; < |
||
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 | < |
||
381 | |||
382 | Supporting OS subroutines required: < |
||
383 | < |
||
384 | */ |
||
385 | |||
386 | #include <_ansi.h> |
||
387 | #include |
||
388 | #include |
||
389 | #include |
||
390 | #ifdef _HAVE_STDC |
||
391 | #include |
||
392 | #else |
||
393 | #include |
||
394 | #endif |
||
395 | #include "local.h" |
||
396 | |||
397 | #ifndef _REENT_ONLY |
||
398 | |||
399 | #ifdef _HAVE_STDC |
||
400 | int |
||
401 | _DEFUN(sscanf, (str, fmt), |
||
4921 | Serge | 402 | _CONST char *__restrict str _AND |
403 | _CONST char * fmt _DOTS) |
||
4349 | Serge | 404 | #else |
405 | int |
||
406 | sscanf(str, fmt, va_alist) |
||
407 | _CONST char *str; |
||
408 | _CONST char *fmt; |
||
409 | va_dcl |
||
410 | #endif |
||
411 | { |
||
412 | int ret; |
||
413 | va_list ap; |
||
414 | FILE f; |
||
415 | |||
416 | f._flags = __SRD | __SSTR; |
||
417 | f._bf._base = f._p = (unsigned char *) str; |
||
418 | f._bf._size = f._r = strlen (str); |
||
419 | f._read = __seofread; |
||
420 | f._ub._base = NULL; |
||
421 | f._lb._base = NULL; |
||
422 | f._file = -1; /* No file. */ |
||
423 | #ifdef _HAVE_STDC |
||
424 | va_start (ap, fmt); |
||
425 | #else |
||
426 | va_start (ap); |
||
427 | #endif |
||
428 | ret = __ssvfscanf_r (_REENT, &f, fmt, ap); |
||
429 | va_end (ap); |
||
430 | return ret; |
||
431 | } |
||
432 | |||
433 | #endif /* !_REENT_ONLY */ |
||
434 | |||
435 | #ifdef _HAVE_STDC |
||
436 | int |
||
437 | _DEFUN(_sscanf_r, (ptr, str, fmt), |
||
438 | struct _reent *ptr _AND |
||
4921 | Serge | 439 | _CONST char *__restrict str _AND |
440 | _CONST char *__restrict fmt _DOTS) |
||
4349 | Serge | 441 | #else |
442 | int |
||
443 | _sscanf_r(ptr, str, fmt, va_alist) |
||
444 | struct _reent *ptr; |
||
4921 | Serge | 445 | _CONST char *__restrict str; |
446 | _CONST char *__restrict fmt; |
||
4349 | Serge | 447 | va_dcl |
448 | #endif |
||
449 | { |
||
450 | int ret; |
||
451 | va_list ap; |
||
452 | FILE f; |
||
453 | |||
454 | f._flags = __SRD | __SSTR; |
||
455 | f._bf._base = f._p = (unsigned char *) str; |
||
456 | f._bf._size = f._r = strlen (str); |
||
457 | f._read = __seofread; |
||
458 | f._ub._base = NULL; |
||
459 | f._lb._base = NULL; |
||
460 | f._file = -1; /* No file. */ |
||
461 | #ifdef _HAVE_STDC |
||
462 | va_start (ap, fmt); |
||
463 | #else |
||
464 | va_start (ap); |
||
465 | #endif |
||
466 | ret = __ssvfscanf_r (ptr, &f, fmt, ap); |
||
467 | va_end (ap); |
||
468 | return ret; |
||
469 | } |