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_ MSVCRT_wchar_t
28 #define _EOF_ MSVCRT_WEOF
29 #define _EOF_RET (short)MSVCRT_WEOF
30 #define _ISSPACE_(c) MSVCRT_iswspace(c)
31 #define _ISDIGIT_(c) MSVCRT_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 */
38 #define _EOF_ MSVCRT_EOF
39 #define _EOF_RET MSVCRT_EOF
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) MSVCRT__lock_file(MSVCRT_stdin)
52 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(MSVCRT_stdin)
55 #define _FUNCTION_ static int MSVCRT_vcwscanf_s_l(const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
57 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
59 #else /* WIDE_SCANF */
61 #define _FUNCTION_ static int MSVCRT_vcscanf_s_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
63 #define _FUNCTION_ static int MSVCRT_vcscanf_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
65 #endif /* WIDE_SCANF */
72 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, *file++))
73 #else /* WIDE_SCANF */
74 #define _GETC_(file) (consumed==length ? '\0' : (consumed++, (unsigned char)*file++))
75 #endif /* WIDE_SCANF */
76 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
77 #define _LOCK_FILE_(file) do {} while(0)
78 #define _UNLOCK_FILE_(file) do {} while(0)
81 #define _FUNCTION_ static int MSVCRT_vsnwscanf_s_l(const MSVCRT_wchar_t *file, MSVCRT_size_t length, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
83 #define _FUNCTION_ static int MSVCRT_vsnwscanf_l(const MSVCRT_wchar_t *file, MSVCRT_size_t length, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
85 #else /* WIDE_SCANF */
87 #define _FUNCTION_ static int MSVCRT_vsnscanf_s_l(const char *file, MSVCRT_size_t length, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
89 #define _FUNCTION_ static int MSVCRT_vsnscanf_l(const char *file, MSVCRT_size_t length, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
91 #endif /* WIDE_SCANF */
92 #else /* STRING_LEN */
94 #define _GETC_(file) (consumed++, *file++)
95 #else /* WIDE_SCANF */
96 #define _GETC_(file) (consumed++, (unsigned char)*file++)
97 #endif /* WIDE_SCANF */
98 #define _UNGETC_(nch, file) do { file--; consumed--; } while(0)
99 #define _LOCK_FILE_(file) do {} while(0)
100 #define _UNLOCK_FILE_(file) do {} while(0)
103 #define _FUNCTION_ static int MSVCRT_vswscanf_s_l(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
105 #define _FUNCTION_ static int MSVCRT_vswscanf_l(const MSVCRT_wchar_t *file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
107 #else /* WIDE_SCANF */
109 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
111 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
113 #endif /* WIDE_SCANF */
114 #endif /* STRING_LEN */
117 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
118 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
119 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
120 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
122 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
124 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
126 #else /* WIDE_SCANF */
127 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
128 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
129 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
130 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
132 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
134 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
136 #endif /* WIDE_SCANF */
141 MSVCRT_pthreadlocinfo locinfo
;
142 int rd
= 0, consumed
= 0;
144 if (!*format
) return 0;
147 TRACE("(%s):\n", debugstr_a(format
));
150 TRACE("%s (%s)\n", debugstr_a(file
), debugstr_a(format
));
152 TRACE("%p (%s)\n", file
, debugstr_a(format
));
155 #endif /* WIDE_SCANF */
165 locinfo
= get_locinfo();
167 locinfo
= locale
->locinfo
;
170 /* a whitespace character in the format string causes scanf to read,
171 * but not store, all consecutive white-space characters in the input
172 * up to the next non-white-space character. One white space character
173 * in the input matches any number (including zero) and combination of
174 * white-space characters in the input. */
175 if (_ISSPACE_(*format
)) {
176 /* skip whitespace */
177 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
180 /* a format specification causes scanf to read and convert characters
181 * in the input into values of a specified type. The value is assigned
182 * to an argument in the argument list. Format specifications have
183 * the form %[*][width][{h | l | I64 | L}]type */
184 else if (*format
== '%') {
185 int st
= 0; int suppress
= 0; int width
= 0;
191 int prefix_finished
= 0;
194 /* look for leading asterisk, which means 'suppress assignment of
200 /* look for width specification */
201 while (_ISDIGIT_(*format
)) {
203 width
+=*format
++ - '0';
205 if (width
==0) width
=-1; /* no width spec seen */
206 /* read prefix (if any) */
207 while (!prefix_finished
) {
209 case 'h': h_prefix
++; break;
211 if(*(format
+1) == 'l') {
217 case 'w': w_prefix
= 1; break;
218 case 'L': L_prefix
= 1; break;
220 if (*(format
+ 1) == '6' &&
221 *(format
+ 2) == '4') {
229 if (!prefix_finished
) format
++;
234 case 'P': /* pointer. */
235 if (sizeof(void *) == sizeof(LONGLONG
)) I64_prefix
= 1;
238 case 'X': /* hexadecimal integer. */
241 case 'o': /* octal integer */
244 case 'u': /* unsigned decimal integer */
247 case 'd': /* signed decimal integer */
250 case 'i': /* generic integer */
253 /* read an integer */
257 /* skip initial whitespace */
258 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
261 if (nch
== '-' || nch
== '+') {
262 negative
= (nch
=='-');
264 if (width
>0) width
--;
266 /* look for leading indication of base */
267 if (width
!=0 && nch
== '0' && *format
!= 'p' && *format
!= 'P') {
269 if (width
>0) width
--;
271 if (width
!=0 && (nch
=='x' || nch
=='X')) {
276 if (width
>0) width
--;
282 /* format %i without indication of base */
285 /* throw away leading zeros */
286 while (width
!=0 && nch
=='0') {
288 if (width
>0) width
--;
291 if (width
!=0 && _CHAR2DIGIT_(nch
, base
)!=-1) {
292 cur
= _CHAR2DIGIT_(nch
, base
);
294 if (width
>0) width
--;
297 /* read until no more digits */
298 while (width
!=0 && (nch
!=_EOF_
) && _CHAR2DIGIT_(nch
, base
)!=-1) {
299 cur
= cur
*base
+ _CHAR2DIGIT_(nch
, base
);
301 if (width
>0) width
--;
305 if (!seendigit
) break; /* not a valid number */
308 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
309 if (I64_prefix
) _SET_NUMBER_(LONGLONG
);
310 else if (l_prefix
) _SET_NUMBER_(LONG
);
311 else if (h_prefix
== 1) _SET_NUMBER_(short int);
312 #if _MSVCR_VER >= 140
313 else if (h_prefix
== 2) _SET_NUMBER_(char);
315 else _SET_NUMBER_(int);
323 case 'G': { /* read a float */
324 long double cur
= 1, expcnt
= 10;
326 int exp
= 0, negative
= 0;
330 /* skip initial whitespace */
331 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
335 if (nch
== '-' || nch
== '+') {
336 negative
= (nch
=='-');
337 if (width
>0) width
--;
342 /* get first digit. */
343 if (*locinfo
->lconv
->decimal_point
!= nch
) {
344 if (!_ISDIGIT_(nch
)) break;
347 if (width
>0) width
--;
348 /* read until no more digits */
349 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
350 hlp
= d
*10 + nch
- '0';
352 if (width
>0) width
--;
353 if(d
> (ULONGLONG
)-1/10 || hlp
<d
) {
360 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
363 if (width
>0) width
--;
366 d
= 0; /* Fix: .8 -> 0.8 */
369 /* handle decimals */
370 if (width
!=0 && nch
== *locinfo
->lconv
->decimal_point
) {
372 if (width
>0) width
--;
374 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
375 hlp
= d
*10 + nch
- '0';
377 if (width
>0) width
--;
378 if(d
> (ULONGLONG
)-1/10 || hlp
<d
)
384 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
386 if (width
>0) width
--;
390 /* handle exponent */
391 if (width
!=0 && (nch
== 'e' || nch
== 'E')) {
395 if (width
>0) width
--;
396 if (width
!=0 && (nch
=='+' || nch
=='-')) {
400 if (width
>0) width
--;
403 /* exponent digits */
404 while (width
!=0 && (nch
!=_EOF_
) && _ISDIGIT_(nch
)) {
405 if(e
> INT_MAX
/10 || (e
= e
*10 + nch
- '0')<0)
408 if (width
>0) width
--;
412 if(exp
<0 && e
<0 && e
+exp
>0) exp
= INT_MIN
;
413 else if(exp
>0 && e
>0 && e
+exp
<0) exp
= INT_MAX
;
417 fpcontrol
= _control87(0, 0);
418 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
419 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
424 /* update 'cur' with this exponent. */
429 expcnt
= expcnt
*expcnt
;
431 cur
= (negexp
? d
/cur
: d
*cur
);
433 _control87(fpcontrol
, 0xffffffff);
437 if (L_prefix
|| l_prefix
) _SET_NUMBER_(double);
438 else _SET_NUMBER_(float);
442 /* According to msdn,
443 * 's' reads a character string in a call to fscanf
444 * and 'S' a wide character string and vice versa in a
445 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
446 * this behaviour. 'h' forces reading char * but 'l' and 'w'
447 * force reading WCHAR. */
449 if (w_prefix
|| l_prefix
) goto widecharstring
;
450 else if (h_prefix
) goto charstring
;
452 else goto widecharstring
;
453 #else /* WIDE_SCANF */
454 else goto charstring
;
455 #endif /* WIDE_SCANF */
457 if (w_prefix
|| l_prefix
) goto widecharstring
;
458 else if (h_prefix
) goto charstring
;
460 else goto charstring
;
461 #else /* WIDE_SCANF */
462 else goto widecharstring
;
463 #endif /* WIDE_SCANF */
464 charstring
: { /* read a word into a char */
465 char *sptr
= suppress
? NULL
: va_arg(ap
, char*);
466 char *sptr_beg
= sptr
;
468 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
470 unsigned size
= UINT_MAX
;
472 /* skip initial whitespace */
473 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
475 /* read until whitespace */
476 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
478 *sptr
++ = _CHAR2SUPPORTED_(nch
);
488 if (width
>0) width
--;
490 /* if we have reached the EOF and output nothing then report EOF */
491 if (nch
==_EOF_
&& rd
==0 && st
==0) {
495 if (st
&& !suppress
) *sptr
= 0;
498 widecharstring
: { /* read a word into a wchar_t* */
499 MSVCRT_wchar_t
*sptr
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
500 MSVCRT_wchar_t
*sptr_beg
= sptr
;
502 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
504 unsigned size
= UINT_MAX
;
506 /* skip initial whitespace */
507 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
509 /* read until whitespace */
510 while (width
!=0 && (nch
!=_EOF_
) && !_ISSPACE_(nch
)) {
512 *sptr
++ = _WIDE2SUPPORTED_(nch
);
522 if (width
>0) width
--;
525 /* if we have reached the EOF and output nothing then report EOF */
526 if (nch
==_EOF_
&& rd
==0 && st
==0) {
531 if (st
&& !suppress
) *sptr
= 0;
534 /* 'c' and 'C work analogously to 's' and 'S' as described
537 if (w_prefix
|| l_prefix
) goto widecharacter
;
538 else if (h_prefix
) goto character
;
540 else goto widecharacter
;
541 #else /* WIDE_SCANF */
543 #endif /* WIDE_SCANF */
545 if (w_prefix
|| l_prefix
) goto widecharacter
;
546 else if (h_prefix
) goto character
;
549 #else /* WIDE_SCANF */
550 else goto widecharacter
;
551 #endif /* WIDE_SCANF */
552 character
: { /* read single character into char */
553 char *str
= suppress
? NULL
: va_arg(ap
, char*);
556 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
558 unsigned size
= UINT_MAX
;
560 if (width
== -1) width
= 1;
561 while (width
&& (nch
!= _EOF_
))
570 *str
++ = _CHAR2SUPPORTED_(nch
);
578 widecharacter
: { /* read single character into a wchar_t */
579 MSVCRT_wchar_t
*str
= suppress
? NULL
: va_arg(ap
, MSVCRT_wchar_t
*);
580 MSVCRT_wchar_t
*pstr
= str
;
582 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
584 unsigned size
= UINT_MAX
;
586 if (width
== -1) width
= 1;
587 while (width
&& (nch
!= _EOF_
))
596 *str
++ = _WIDE2SUPPORTED_(nch
);
606 int*n
= va_arg(ap
, int*);
609 /* This is an odd one: according to the standard,
610 * "Execution of a %n directive does not increment the
611 * assignment count returned at the completion of
612 * execution" even if it wasn't suppressed with the
613 * '*' flag. The Corrigendum to the standard seems
614 * to contradict this (comment out the assignment to
615 * suppress below if you want to implement these
616 * alternate semantics) but the windows program I'm
617 * looking at expects the behavior I've coded here
618 * (which happens to be what glibc does as well).
625 _CHAR_
*str
= suppress
? NULL
: va_arg(ap
, _CHAR_
*);
629 int invert
= 0; /* Set if we are NOT to find the chars */
631 unsigned size
= suppress
? UINT_MAX
: va_arg(ap
, unsigned);
633 unsigned size
= UINT_MAX
;
636 /* Init our bitmap */
637 Mask
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, _BITMAPSIZE_
/8);
638 RtlInitializeBitMap(&bitMask
, Mask
, _BITMAPSIZE_
);
640 /* Read the format */
647 RtlSetBits(&bitMask
, ']', 1);
650 while(*format
&& (*format
!= ']')) {
651 /* According to msdn:
652 * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
653 if((*format
== '-') && (*(format
+ 1) != ']')) {
654 if ((*(format
- 1)) < *(format
+ 1))
655 RtlSetBits(&bitMask
, *(format
- 1) +1 , *(format
+ 1) - *(format
- 1));
657 RtlSetBits(&bitMask
, *(format
+ 1) , *(format
- 1) - *(format
+ 1));
660 RtlSetBits(&bitMask
, *format
, 1);
663 /* read until char is not suitable */
664 while ((width
!= 0) && (nch
!= _EOF_
)) {
666 if(RtlAreBitsSet(&bitMask
, nch
, 1)) {
667 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
671 if(RtlAreBitsClear(&bitMask
, nch
, 1)) {
672 if (!suppress
) *sptr
++ = _CHAR2SUPPORTED_(nch
);
678 if (width
>0) width
--;
683 HeapFree(GetProcessHeap(), 0, Mask
);
688 if (!suppress
) *sptr
= 0;
689 HeapFree(GetProcessHeap(), 0, Mask
);
693 /* From spec: "if a percent sign is followed by a character
694 * that has no meaning as a format-control character, that
695 * character and the following characters are treated as
696 * an ordinary sequence of characters, that is, a sequence
697 * of characters that must match the input. For example,
698 * to specify that a percent-sign character is to be input,
700 while ((nch
!=_EOF_
) && _ISSPACE_(nch
))
702 if ((_CHAR_
)nch
== *format
) {
703 suppress
= 1; /* whoops no field to be read */
704 st
= 1; /* but we got what we expected */
709 if (st
&& !suppress
) rd
++;
712 /* a non-white-space character causes scanf to read, but not store,
713 * a matching non-white-space character. */
715 /* check for character match */
716 if ((_CHAR_
)nch
== *format
) {
726 TRACE("returning %d\n", rd
);
736 #undef _CHAR2SUPPORTED_
737 #undef _WIDE2SUPPORTED_