fe272c9f05b322a2c080032f4e19d5baf8390705
2 * general implementation of scanf used by scanf, sscanf, fscanf,
3 * _cscanf, wscanf, swscanf and fwscanf
5 * Copyright 1996,1998 Marcus Meissner
6 * Copyright 1996 Jukka Iivonen
7 * Copyright 1997,2000, 2003 Uwe Bonnes
8 * Copyright 2000 Jon Griffiths
9 * Copyright 2002 Daniel Gudbjartsson
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define _CHAR_ wchar_t
29 #define _EOF_RET (short)WEOF
30 #define _ISSPACE_(c) iswspace(c)
31 #define _ISDIGIT_(c) iswdigit(c)
32 #define _WIDE2SUPPORTED_(c) c /* No conversion needed (wide to wide) */
33 #define _CHAR2SUPPORTED_(c) c /* FIXME: convert char to wide char */
34 #define _CHAR2DIGIT_(c, base) wchar2digit((c), (base))
35 #define _BITMAPSIZE_ 256*256
36 #else /* WIDE_SCANF */
40 #define _ISSPACE_(c) isspace(c)
41 #define _ISDIGIT_(c) isdigit(c)
42 #define _WIDE2SUPPORTED_(c) c /* FIXME: convert wide char to char */
43 #define _CHAR2SUPPORTED_(c) c /* No conversion needed (char to char) */
44 #define _CHAR2DIGIT_(c, base) char2digit((c), (base))
45 #define _BITMAPSIZE_ 256
46 #endif /* WIDE_SCANF */
49 #define _GETC_(file) (consumed++, _getch())
50 #define _UNGETC_(nch, file) do { _ungetch(nch); consumed--; } while(0)
51 #define _LOCK_FILE_(file) _lock_file(stdin)
52 #define _UNLOCK_FILE_(file) _unlock_file(stdin)
55 #define _FUNCTION_ static int vcwscanf_s_l(const char *format, _locale_t locale, __ms_va_list ap)
57 #define _FUNCTION_ static int vcwscanf_l(const char *format, _locale_t locale, __ms_va_list ap)
59 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int vcscanf_s_l(const char *format, _locale_t locale, __ms_va_list ap)
63 #define _FUNCTION_ static int vcscanf_l(const char *format, _locale_t locale, __ms_va_list ap)
65 #endif /* WIDE_SCANF */
71 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, *file++))
72 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
73 #define _LOCK_FILE_(file) do {} while(0)
74 #define _UNLOCK_FILE_(file) do {} while(0)
77 #define _FUNCTION_ static int vsnwscanf_s_l(const wchar_t *file, size_t length, const wchar_t *format, _locale_t locale, __ms_va_list ap)
79 #define _FUNCTION_ static int vsnwscanf_l(const wchar_t *file, size_t length, const wchar_t *format, _locale_t locale, __ms_va_list ap)
81 #else /* WIDE_SCANF */
83 #define _FUNCTION_ static int vsnscanf_s_l(const char *file, size_t length, const char *format, _locale_t locale, __ms_va_list ap)
85 #define _FUNCTION_ static int vsnscanf_l(const char *file, size_t length, const char *format, _locale_t locale, __ms_va_list ap)
87 #endif /* WIDE_SCANF */
88 #else /* STRING_LEN */
89 #define _GETC_(file) (consumed++, *file++)
90 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
91 #define _LOCK_FILE_(file) do {} while(0)
92 #define _UNLOCK_FILE_(file) do {} while(0)
95 #define _FUNCTION_ static int vswscanf_s_l(const wchar_t *file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
97 #define _FUNCTION_ static int vswscanf_l(const wchar_t *file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
99 #else /* WIDE_SCANF */
101 #define _FUNCTION_ static int vsscanf_s_l(const char *file, const char *format, _locale_t locale, __ms_va_list ap)
103 #define _FUNCTION_ static int vsscanf_l(const char *file, const char *format, _locale_t locale, __ms_va_list ap)
105 #endif /* WIDE_SCANF */
106 #endif /* STRING_LEN */
109 #define _GETC_(file) (consumed++, fgetwc(file))
110 #define _UNGETC_(nch, file) do { ungetwc(nch, file); consumed--; } while(0)
111 #define _LOCK_FILE_(file) _lock_file(file)
112 #define _UNLOCK_FILE_(file) _unlock_file(file)
114 #define _FUNCTION_ static int vfwscanf_s_l(FILE* file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
116 #define _FUNCTION_ static int vfwscanf_l(FILE* file, const wchar_t *format, _locale_t locale, __ms_va_list ap)
118 #else /* WIDE_SCANF */
119 #define _GETC_(file) (consumed++, fgetc(file))
120 #define _UNGETC_(nch, file) do { ungetc(nch, file); consumed--; } while(0)
121 #define _LOCK_FILE_(file) _lock_file(file)
122 #define _UNLOCK_FILE_(file) _unlock_file(file)
124 #define _FUNCTION_ static int vfscanf_s_l(FILE* file, const char *format, _locale_t locale, __ms_va_list ap)
126 #define _FUNCTION_ static int vfscanf_l(FILE* file, const char *format, _locale_t locale, __ms_va_list ap)
128 #endif /* WIDE_SCANF */
133 pthreadlocinfo locinfo
;
134 int rd
= 0, consumed
= 0;
136 if (!*format
) return 0;
139 TRACE("(%s):\n", debugstr_a(format
));
142 TRACE("%s (%s)\n", debugstr_a(file
), debugstr_a(format
));
144 TRACE("%p (%s)\n", file
, debugstr_a(format
));
147 #endif /* WIDE_SCANF */
157 locinfo
= get_locinfo();
159 locinfo
= locale
->locinfo
;
162 /* a whitespace character in the format string causes scanf to read,
163 * but not store, all consecutive white-space characters in the input
164 * up to the next non-white-space character. One white space character
165 * in the input matches any number (including zero) and combination of
166 * white-space characters in the input. */
167 if (_ISSPACE_(*format
)) {
168 /* skip whitespace */
169 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
172 /* a format specification causes scanf to read and convert characters
173 * in the input into values of a specified type. The value is assigned
174 * to an argument in the argument list. Format specifications have
175 * the form %[*][width][{h | l | I64 | L}]type */
176 else if (*format
== '%') {
177 int st
= 0; int suppress
= 0; int width
= 0;
183 int prefix_finished
= 0;
186 /* look for leading asterisk, which means 'suppress assignment of
192 /* look for width specification */
193 while (_ISDIGIT_(*format
)) {
195 width
+=*format
++ - '0';
197 if (width
==0) width
=-1; /* no width spec seen */
198 /* read prefix (if any) */
199 while (!prefix_finished
) {
201 case 'h': h_prefix
++; break;
203 if(*(format
+1) == 'l') {
209 case 'w': w_prefix
= 1; break;
210 case 'L': L_prefix
= 1; break;
212 if (*(format
+ 1) == '6' &&
213 *(format
+ 2) == '4') {
221 if (!prefix_finished
) format
++;
226 case 'P': /* pointer. */
227 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
230 case 'X': /* hexadecimal integer. */
233 case 'o': /* octal integer */
236 case 'u': /* unsigned decimal integer */
239 case 'd': /* signed decimal integer */
242 case 'i': /* generic integer */
245 /* read an integer */
249 /* skip initial whitespace */
250 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
253 if (nch
== '-' || nch
== '+') {
254 negative
= (nch
=='-');
256 if (width
>0) width
--;
258 /* look for leading indication of base */
259 if (width
!=0 && nch
== '0' && *format
!= 'p' && *format
!= 'P') {
261 if (width
>0) width
--;
263 if (width
!=0 && (nch
=='x' || nch
=='X')) {
268 if (width
>0) width
--;
274 /* format %i without indication of base */
277 /* throw away leading zeros */
278 while (width
!=0 && nch
=='0') {
280 if (width
>0) width
--;
283 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
284 cur
= _CHAR2DIGIT_(nch
, base
);
286 if (width
>0) width
--;
289 /* read until no more digits */
290 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
291 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
293 if (width
>0) width
--;
297 if (!seendigit
) break; /* not a valid number */
300 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -(LONGLONG)cur : cur
301 if (I64_prefix
) _SET_NUMBER_(LONGLONG
);
302 else if (l_prefix
) _SET_NUMBER_(LONG
);
303 else if (h_prefix
== 1) _SET_NUMBER_(short int);
304 else _SET_NUMBER_(int);
312 case 'G': { /* read a float */
313 //long double cur = 1, expcnt = 10;
315 int exp
= 0, negative
= 0;
316 //unsigned fpcontrol;
319 /* skip initial whitespace */
320 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
324 if (nch
== '-' || nch
== '+') {
325 negative
= (nch
=='-');
326 if (width
>0) width
--;
331 /* get first digit. */
332 if (*locinfo
->lconv
->decimal_point
!= nch
) {
333 if (!_ISDIGIT_(nch
)) break;
336 if (width
>0) width
--;
337 /* read until no more digits */
338 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
339 hlp
= d
*10 + nch
- '0';
341 if (width
>0) width
--;
342 if(d
> (ULONGLONG
)-1/10 || hlp
<d
) {
349 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
352 if (width
>0) width
--;
355 d
= 0; /* Fix: .8 -> 0.8 */
358 /* handle decimals */
359 if (width
!=0 && nch
== *locinfo
->lconv
->decimal_point
) {
361 if (width
>0) width
--;
363 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
364 hlp
= d
*10 + nch
- '0';
366 if (width
>0) width
--;
367 if(d
> (ULONGLONG
)-1/10 || hlp
<d
)
373 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
375 if (width
>0) width
--;
379 /* handle exponent */
380 if (width
!=0 && (nch
== 'e' || nch
== 'E')) {
384 if (width
>0) width
--;
385 if (width
!=0 && (nch
=='+' || nch
=='-')) {
389 if (width
>0) width
--;
392 /* exponent digits */
393 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
394 if(e
> INT_MAX
/10 || (e
= e
*10 + nch
- '0')<0)
397 if (width
>0) width
--;
401 if(exp
<0 && e
<0 && e
+exp
>0) exp
= INT_MIN
;
402 else if(exp
>0 && e
>0 && e
+exp
<0) exp
= INT_MAX
;
407 /* ReactOS: don't inline float processing (kernel/freeldr don't like that! */
408 _internal_handle_float(negative
, exp
, suppress
, d
, l_prefix
|| L_prefix
, &ap
);
411 fpcontrol
= _control87(0, 0);
412 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
413 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
418 /* update 'cur' with this exponent. */
423 expcnt
= expcnt
*expcnt
;
425 cur
= (negexp
? d
/cur
: d
*cur
);
427 _control87(fpcontrol
, 0xffffffff);
431 if (L_prefix
) _SET_NUMBER_(double);
432 else if (l_prefix
) _SET_NUMBER_(double);
433 else _SET_NUMBER_(float);
435 #endif /* __REACTOS__ */
438 /* According to msdn,
439 * 's' reads a character string in a call to fscanf
440 * and 'S' a wide character string and vice versa in a
441 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
442 * this behaviour. 'h' forces reading char * but 'l' and 'w'
443 * force reading WCHAR. */
445 if (w_prefix
|| l_prefix
) goto widecharstring
;
446 else if (h_prefix
) goto charstring
;
448 else goto widecharstring
;
449 #else /* WIDE_SCANF */
450 else goto charstring
;
451 #endif /* WIDE_SCANF */
453 if (w_prefix
|| l_prefix
) goto widecharstring
;
454 else if (h_prefix
) goto charstring
;
456 else goto charstring
;
457 #else /* WIDE_SCANF */
458 else goto widecharstring
;
459 #endif /* WIDE_SCANF */
460 charstring
: { /* read a word into a char */
461 char *sptr
= suppress
? NULL
: va_arg(ap
, char*);
462 char *sptr_beg
= sptr
;
464 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
466 unsigned size
= UINT_MAX
;
468 /* skip initial whitespace */
469 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
471 /* read until whitespace */
472 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
474 *sptr
++ = _CHAR2SUPPORTED_(nch
);
484 if (width
>0) width
--;
487 if (st
&& !suppress
) *sptr
= 0;
490 widecharstring
: { /* read a word into a wchar_t* */
491 wchar_t *sptr
= suppress
? NULL
: va_arg(ap
, wchar_t*);
492 wchar_t *sptr_beg
= sptr
;
494 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
496 unsigned size
= UINT_MAX
;
498 /* skip initial whitespace */
499 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
501 /* read until whitespace */
502 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
504 *sptr
++ = _WIDE2SUPPORTED_(nch
);
514 if (width
>0) width
--;
517 if (st
&& !suppress
) *sptr
= 0;
520 /* 'c' and 'C work analogously to 's' and 'S' as described
523 if (w_prefix
|| l_prefix
) goto widecharacter
;
524 else if (h_prefix
) goto character
;
526 else goto widecharacter
;
527 #else /* WIDE_SCANF */
529 #endif /* WIDE_SCANF */
531 if (w_prefix
|| l_prefix
) goto widecharacter
;
532 else if (h_prefix
) goto character
;
535 #else /* WIDE_SCANF */
536 else goto widecharacter
;
537 #endif /* WIDE_SCANF */
538 character
: { /* read single character into char */
539 char *str
= suppress
? NULL
: va_arg(ap
, char*);
542 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned)/sizeof(char);
544 unsigned size
= UINT_MAX
;
546 if (width
== -1) width
= 1;
547 while (width
&& (nch
!= _EOF_
))
550 *str
++ = _CHAR2SUPPORTED_(nch
);
564 widecharacter
: { /* read single character into a wchar_t */
565 wchar_t *str
= suppress
? NULL
: va_arg(ap
, wchar_t*);
568 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned)/sizeof(wchar_t);
570 unsigned size
= UINT_MAX
;
572 if (width
== -1) width
= 1;
573 while (width
&& (nch
!= _EOF_
))
576 *str
++ = _WIDE2SUPPORTED_(nch
);
592 int*n
= va_arg(ap
, int*);
595 /* This is an odd one: according to the standard,
596 * "Execution of a %n directive does not increment the
597 * assignment count returned at the completion of
598 * execution" even if it wasn't suppressed with the
599 * '*' flag. The Corrigendum to the standard seems
600 * to contradict this (comment out the assignment to
601 * suppress below if you want to implement these
602 * alternate semantics) but the windows program I'm
603 * looking at expects the behavior I've coded here
604 * (which happens to be what glibc does as well).
611 _CHAR_
*str
= suppress
? NULL
: va_arg(ap
, _CHAR_
*);
615 int invert
= 0; /* Set if we are NOT to find the chars */
617 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned)/sizeof(_CHAR_
);
619 unsigned size
= UINT_MAX
;
622 /* Init our bitmap */
623 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, _BITMAPSIZE_
/8);
624 RtlInitializeBitMap(&bitMask
, Mask
, _BITMAPSIZE_
);
626 /* Read the format */
633 RtlSetBits(&bitMask
, ']', 1);
636 while(*format
&& (*format
!= ']')) {
637 /* According to msdn:
638 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
639 if((*format
== '-') && (*(format
+ 1) != ']')) {
640 if ((*(format
- 1)) < *(format
+ 1))
641 RtlSetBits(&bitMask
, *(format
- 1) +1 , *(format
+ 1) - *(format
- 1));
643 RtlSetBits(&bitMask
, *(format
+ 1) , *(format
- 1) - *(format
+ 1));
646 RtlSetBits(&bitMask
, *format
, 1);
649 /* read until char is not suitable */
650 while ((width
!= 0) && (nch
!= _EOF_
)) {
652 if(RtlAreBitsSet(&bitMask
, nch
, 1)) {
653 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
657 if(RtlAreBitsClear(&bitMask
, nch
, 1)) {
658 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
664 if (width
>0) width
--;
673 if (!suppress
) *sptr
= 0;
674 HeapFree(GetProcessHeap(), 0, Mask
);
678 /* From spec: "if a percent sign is followed by a character
679 * that has no meaning as a format-control character, that
680 * character and the following characters are treated as
681 * an ordinary sequence of characters, that is, a sequence
682 * of characters that must match the input. For example,
683 * to specify that a percent-sign character is to be input,
685 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
688 suppress
= 1; /* whoops no field to be read */
689 st
= 1; /* but we got what we expected */
694 if (st
&& !suppress
) rd
++;
697 /* a non-white-space character causes scanf to read, but not store,
698 * a matching non-white-space character. */
700 /* check for character match */
701 if (nch
== *format
) {
711 TRACE("returning %d\n", rd
);
721 #undef _CHAR2SUPPORTED_
722 #undef _WIDE2SUPPORTED_