msvcrt: Make fclose thread safe.
[wine] / dlls / msvcrt / scanf.h
1 /*
2  * general implementation of scanf used by scanf, sscanf, fscanf,
3  * _cscanf, wscanf, swscanf and fwscanf
4  *
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
10  *
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.
15  *
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.
20  *
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
24  */
25
26 #ifdef WIDE_SCANF
27 #define _CHAR_ MSVCRT_wchar_t
28 #define _EOF_ MSVCRT_WEOF
29 #define _EOF_RET 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 */
37 #define _CHAR_ char
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 */
47
48 #ifdef CONSOLE
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)
53 #ifdef WIDE_SCANF
54 #ifdef SECURE
55 #define _FUNCTION_ static int MSVCRT_vcwscanf_s_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
56 #else  /* SECURE */
57 #define _FUNCTION_ static int MSVCRT_vcwscanf_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
58 #endif /* SECURE */
59 #else  /* WIDE_SCANF */
60 #ifdef SECURE
61 #define _FUNCTION_ static int MSVCRT_vcscanf_s_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
62 #else  /* SECURE */
63 #define _FUNCTION_ static int MSVCRT_vcscanf_l(const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
64 #endif /* SECURE */
65 #endif /* WIDE_SCANF */
66 #else
67 #ifdef STRING
68 #undef _EOF_
69 #define _EOF_ 0
70 #ifdef STRING_LEN
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)
75 #ifdef WIDE_SCANF
76 #ifdef SECURE
77 #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)
78 #else  /* SECURE */
79 #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)
80 #endif /* SECURE */
81 #else /* WIDE_SCANF */
82 #ifdef SECURE
83 #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)
84 #else  /* SECURE */
85 #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)
86 #endif /* SECURE */
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)
93 #ifdef WIDE_SCANF
94 #ifdef SECURE
95 #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)
96 #else  /* SECURE */
97 #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)
98 #endif /* SECURE */
99 #else /* WIDE_SCANF */
100 #ifdef SECURE
101 #define _FUNCTION_ static int MSVCRT_vsscanf_s_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
102 #else  /* SECURE */
103 #define _FUNCTION_ static int MSVCRT_vsscanf_l(const char *file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
104 #endif /* SECURE */
105 #endif /* WIDE_SCANF */
106 #endif /* STRING_LEN */
107 #else /* STRING */
108 #ifdef WIDE_SCANF
109 #define _GETC_(file) (consumed++, MSVCRT_fgetwc(file))
110 #define _UNGETC_(nch, file) do { MSVCRT_ungetwc(nch, file); consumed--; } while(0)
111 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
112 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
113 #ifdef SECURE
114 #define _FUNCTION_ static int MSVCRT_vfwscanf_s_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
115 #else  /* SECURE */
116 #define _FUNCTION_ static int MSVCRT_vfwscanf_l(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list ap)
117 #endif /* SECURE */
118 #else /* WIDE_SCANF */
119 #define _GETC_(file) (consumed++, MSVCRT_fgetc(file))
120 #define _UNGETC_(nch, file) do { MSVCRT_ungetc(nch, file); consumed--; } while(0)
121 #define _LOCK_FILE_(file) MSVCRT__lock_file(file)
122 #define _UNLOCK_FILE_(file) MSVCRT__unlock_file(file)
123 #ifdef SECURE
124 #define _FUNCTION_ static int MSVCRT_vfscanf_s_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
125 #else  /* SECURE */
126 #define _FUNCTION_ static int MSVCRT_vfscanf_l(MSVCRT_FILE* file, const char *format, MSVCRT__locale_t locale, __ms_va_list ap)
127 #endif /* SECURE */
128 #endif /* WIDE_SCANF */
129 #endif /* STRING */
130 #endif /* CONSOLE */
131
132 _FUNCTION_ {
133     int rd = 0, consumed = 0;
134     int nch;
135     if (!*format) return 0;
136 #ifndef WIDE_SCANF
137 #ifdef CONSOLE
138     TRACE("(%s):\n", debugstr_a(format));
139 #else /* CONSOLE */
140 #ifdef STRING
141     TRACE("%s (%s)\n", debugstr_a(file), debugstr_a(format));
142 #else /* STRING */
143     TRACE("%p (%s)\n", file, debugstr_a(format));
144 #endif /* STRING */
145 #endif /* CONSOLE */
146 #endif /* WIDE_SCANF */
147     _LOCK_FILE_(file);
148
149     nch = _GETC_(file);
150     if (nch == _EOF_) {
151         _UNLOCK_FILE_(file);
152         return _EOF_RET;
153     }
154
155     if(!locale)
156         locale = get_locale();
157
158     while (*format) {
159         /* a whitespace character in the format string causes scanf to read,
160          * but not store, all consecutive white-space characters in the input
161          * up to the next non-white-space character.  One white space character
162          * in the input matches any number (including zero) and combination of
163          * white-space characters in the input. */
164         if (_ISSPACE_(*format)) {
165             /* skip whitespace */
166             while ((nch!=_EOF_) && _ISSPACE_(nch))
167                 nch = _GETC_(file);
168         }
169         /* a format specification causes scanf to read and convert characters
170          * in the input into values of a specified type.  The value is assigned
171          * to an argument in the argument list.  Format specifications have
172          * the form %[*][width][{h | l | I64 | L}]type */
173         else if (*format == '%') {
174             int st = 0; int suppress = 0; int width = 0;
175             int base;
176             int h_prefix = 0;
177             int l_prefix = 0;
178             int L_prefix = 0;
179             int w_prefix = 0;
180             int prefix_finished = 0;
181             int I64_prefix = 0;
182             format++;
183             /* look for leading asterisk, which means 'suppress assignment of
184              * this field'. */
185             if (*format=='*') {
186                 format++;
187                 suppress=1;
188             }
189             /* look for width specification */
190             while (_ISDIGIT_(*format)) {
191                 width*=10;
192                 width+=*format++ - '0';
193             }
194             if (width==0) width=-1; /* no width spec seen */
195             /* read prefix (if any) */
196             while (!prefix_finished) {
197                 switch(*format) {
198                 case 'h': h_prefix = 1; break;
199                 case 'l': l_prefix = 1; break;
200                 case 'w': w_prefix = 1; break;
201                 case 'L': L_prefix = 1; break;
202                 case 'I':
203                     if (*(format + 1) == '6' &&
204                         *(format + 2) == '4') {
205                         I64_prefix = 1;
206                         format += 2;
207                     }
208                     break;
209                 default:
210                     prefix_finished = 1;
211                 }
212                 if (!prefix_finished) format++;
213             }
214             /* read type */
215             switch(*format) {
216             case 'p':
217             case 'P': /* pointer. */
218                 if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = 1;
219                 /* fall through */
220             case 'x':
221             case 'X': /* hexadecimal integer. */
222                 base = 16;
223                 goto number;
224             case 'o': /* octal integer */
225                 base = 8;
226                 goto number;
227             case 'u': /* unsigned decimal integer */
228                 base = 10;
229                 goto number;
230             case 'd': /* signed decimal integer */
231                 base = 10;
232                 goto number;
233             case 'i': /* generic integer */
234                 base = 0;
235             number: {
236                     /* read an integer */
237                     ULONGLONG cur = 0;
238                     int negative = 0;
239                     int seendigit=0;
240                     /* skip initial whitespace */
241                     while ((nch!=_EOF_) && _ISSPACE_(nch))
242                         nch = _GETC_(file);
243                     /* get sign */
244                     if (nch == '-' || nch == '+') {
245                         negative = (nch=='-');
246                         nch = _GETC_(file);
247                         if (width>0) width--;
248                     }
249                     /* look for leading indication of base */
250                     if (width!=0 && nch == '0' && *format != 'p' && *format != 'P') {
251                         nch = _GETC_(file);
252                         if (width>0) width--;
253                         seendigit=1;
254                         if (width!=0 && (nch=='x' || nch=='X')) {
255                             if (base==0)
256                                 base=16;
257                             if (base==16) {
258                                 nch = _GETC_(file);
259                                 if (width>0) width--;
260                                 seendigit=0;
261                             }
262                         } else if (base==0)
263                             base = 8;
264                     }
265                     /* format %i without indication of base */
266                     if (base==0)
267                         base = 10;
268                     /* throw away leading zeros */
269                     while (width!=0 && nch=='0') {
270                         nch = _GETC_(file);
271                         if (width>0) width--;
272                         seendigit=1;
273                     }
274                     if (width!=0 && _CHAR2DIGIT_(nch, base)!=-1) {
275                         cur = _CHAR2DIGIT_(nch, base);
276                         nch = _GETC_(file);
277                         if (width>0) width--;
278                         seendigit=1;
279                     }
280                     /* read until no more digits */
281                     while (width!=0 && (nch!=_EOF_) && _CHAR2DIGIT_(nch, base)!=-1) {
282                         cur = cur*base + _CHAR2DIGIT_(nch, base);
283                         nch = _GETC_(file);
284                         if (width>0) width--;
285                         seendigit=1;
286                     }
287                     /* okay, done! */
288                     if (!seendigit) break; /* not a valid number */
289                     st = 1;
290                     if (!suppress) {
291 #define _SET_NUMBER_(type) *va_arg(ap, type*) = negative ? -cur : cur
292                         if (I64_prefix) _SET_NUMBER_(LONGLONG);
293                         else if (l_prefix) _SET_NUMBER_(LONG);
294                         else if (h_prefix) _SET_NUMBER_(short int);
295                         else _SET_NUMBER_(int);
296                     }
297                 }
298                 break;
299             case 'e':
300             case 'E':
301             case 'f':
302             case 'g':
303             case 'G': { /* read a float */
304                     long double cur = 0;
305                     int negative = 0;
306                     /* skip initial whitespace */
307                     while ((nch!=_EOF_) && _ISSPACE_(nch))
308                         nch = _GETC_(file);
309                     /* get sign. */
310                     if (nch == '-' || nch == '+') {
311                         negative = (nch=='-');
312                         if (width>0) width--;
313                         if (width==0) break;
314                         nch = _GETC_(file);
315                     }
316                     /* get first digit. */
317                     if (*locale->locinfo->lconv->decimal_point != nch) {
318                       if (!_ISDIGIT_(nch)) break;
319                       cur = (nch - '0');
320                       nch = _GETC_(file);
321                       if (width>0) width--;
322                       /* read until no more digits */
323                       while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
324                         cur = cur*10 + (nch - '0');
325                         nch = _GETC_(file);
326                         if (width>0) width--;
327                       }
328                     } else {
329                       cur = 0; /* Fix: .8 -> 0.8 */
330                     }
331                     /* handle decimals */
332                     if (width!=0 && nch == *locale->locinfo->lconv->decimal_point) {
333                         long double dec = 1;
334                         nch = _GETC_(file);
335                         if (width>0) width--;
336                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
337                             dec /= 10;
338                             cur += dec * (nch - '0');
339                             nch = _GETC_(file);
340                             if (width>0) width--;
341                         }
342                     }
343                     /* handle exponent */
344                     if (width!=0 && (nch == 'e' || nch == 'E')) {
345                         int exponent = 0, negexp = 0;
346                         float expcnt;
347                         nch = _GETC_(file);
348                         if (width>0) width--;
349                         /* possible sign on the exponent */
350                         if (width!=0 && (nch=='+' || nch=='-')) {
351                             negexp = (nch=='-');
352                             nch = _GETC_(file);
353                             if (width>0) width--;
354                         }
355                         /* exponent digits */
356                         while (width!=0 && (nch!=_EOF_) && _ISDIGIT_(nch)) {
357                             exponent *= 10;
358                             exponent += (nch - '0');
359                             nch = _GETC_(file);
360                             if (width>0) width--;
361                         }
362                         /* update 'cur' with this exponent. */
363                         expcnt =  negexp ? .1 : 10;
364                         while (exponent!=0) {
365                             if (exponent&1)
366                                 cur*=expcnt;
367                             exponent/=2;
368                             expcnt=expcnt*expcnt;
369                         }
370                     }
371                     st = 1;
372                     if (!suppress) {
373                         if (L_prefix) _SET_NUMBER_(long double);
374                         else if (l_prefix) _SET_NUMBER_(double);
375                         else _SET_NUMBER_(float);
376                     }
377                 }
378                 break;
379                 /* According to msdn,
380                  * 's' reads a character string in a call to fscanf
381                  * and 'S' a wide character string and vice versa in a
382                  * call to fwscanf. The 'h', 'w' and 'l' prefixes override
383                  * this behaviour. 'h' forces reading char * but 'l' and 'w'
384                  * force reading WCHAR. */
385             case 's':
386                     if (w_prefix || l_prefix) goto widecharstring;
387                     else if (h_prefix) goto charstring;
388 #ifdef WIDE_SCANF
389                     else goto widecharstring;
390 #else /* WIDE_SCANF */
391                     else goto charstring;
392 #endif /* WIDE_SCANF */
393             case 'S':
394                     if (w_prefix || l_prefix) goto widecharstring;
395                     else if (h_prefix) goto charstring;
396 #ifdef WIDE_SCANF
397                     else goto charstring;
398 #else /* WIDE_SCANF */
399                     else goto widecharstring;
400 #endif /* WIDE_SCANF */
401             charstring: { /* read a word into a char */
402                     char *sptr = suppress ? NULL : va_arg(ap, char*);
403                     char *sptr_beg = sptr;
404 #ifdef SECURE
405                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
406 #else
407                     unsigned size = UINT_MAX;
408 #endif
409                     /* skip initial whitespace */
410                     while ((nch!=_EOF_) && _ISSPACE_(nch))
411                         nch = _GETC_(file);
412                     /* read until whitespace */
413                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
414                         if (!suppress) {
415                             *sptr++ = _CHAR2SUPPORTED_(nch);
416                             if(size>1) size--;
417                             else {
418                                 _UNLOCK_FILE_(file);
419                                 *sptr_beg = 0;
420                                 return rd;
421                             }
422                         }
423                         st++;
424                         nch = _GETC_(file);
425                         if (width>0) width--;
426                     }
427                     /* terminate */
428                     if (!suppress) *sptr = 0;
429                 }
430                 break;
431             widecharstring: { /* read a word into a wchar_t* */
432                     MSVCRT_wchar_t *sptr = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
433                     MSVCRT_wchar_t *sptr_beg = sptr;
434 #ifdef SECURE
435                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned);
436 #else
437                     unsigned size = UINT_MAX;
438 #endif
439                     /* skip initial whitespace */
440                     while ((nch!=_EOF_) && _ISSPACE_(nch))
441                         nch = _GETC_(file);
442                     /* read until whitespace */
443                     while (width!=0 && (nch!=_EOF_) && !_ISSPACE_(nch)) {
444                         if (!suppress) {
445                             *sptr++ = _WIDE2SUPPORTED_(nch);
446                             if(size>1) size--;
447                             else {
448                                 _UNLOCK_FILE_(file);
449                                 *sptr_beg = 0;
450                                 return rd;
451                             }
452                         }
453                         st++;
454                         nch = _GETC_(file);
455                         if (width>0) width--;
456                     }
457                     /* terminate */
458                     if (!suppress) *sptr = 0;
459                 }
460                 break;
461             /* 'c' and 'C work analogously to 's' and 'S' as described
462              * above */
463             case 'c':
464                     if (w_prefix || l_prefix) goto widecharacter;
465                     else if (h_prefix) goto character;
466 #ifdef WIDE_SCANF
467                     else goto widecharacter;
468 #else /* WIDE_SCANF */
469                     else goto character;
470 #endif /* WIDE_SCANF */
471             case 'C':
472                     if (w_prefix || l_prefix) goto widecharacter;
473                     else if (h_prefix) goto character;
474 #ifdef WIDE_SCANF
475                     else goto character;
476 #else /* WIDE_SCANF */
477                     else goto widecharacter;
478 #endif /* WIDE_SCANF */
479           character: { /* read single character into char */
480                     char *str = suppress ? NULL : va_arg(ap, char*);
481                     char *pstr = str;
482 #ifdef SECURE
483                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(char);
484 #else
485                     unsigned size = UINT_MAX;
486 #endif
487                     if (width == -1) width = 1;
488                     while (width && (nch != _EOF_))
489                     {
490                         if (!suppress) {
491                             *str++ = _CHAR2SUPPORTED_(nch);
492                             if(size) size--;
493                             else {
494                                 _UNLOCK_FILE_(file);
495                                 *pstr = 0;
496                                 return rd;
497                             }
498                         }
499                         st++;
500                         width--;
501                         nch = _GETC_(file);
502                     }
503                 }
504                 break;
505           widecharacter: { /* read single character into a wchar_t */
506                     MSVCRT_wchar_t *str = suppress ? NULL : va_arg(ap, MSVCRT_wchar_t*);
507                     MSVCRT_wchar_t *pstr = str;
508 #ifdef SECURE
509                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(MSVCRT_wchar_t);
510 #else
511                     unsigned size = UINT_MAX;
512 #endif
513                     if (width == -1) width = 1;
514                     while (width && (nch != _EOF_))
515                     {
516                         if (!suppress) {
517                             *str++ = _WIDE2SUPPORTED_(nch);
518                             if(size) size--;
519                             else {
520                                 _UNLOCK_FILE_(file);
521                                 *pstr = 0;
522                                 return rd;
523                             }
524                         }
525                         st++;
526                         width--;
527                         nch = _GETC_(file);
528                     }
529                 }
530                 break;
531             case 'n': {
532                     if (!suppress) {
533                         int*n = va_arg(ap, int*);
534                         *n = consumed - 1;
535                     }
536                     /* This is an odd one: according to the standard,
537                      * "Execution of a %n directive does not increment the
538                      * assignment count returned at the completion of
539                      * execution" even if it wasn't suppressed with the
540                      * '*' flag.  The Corrigendum to the standard seems
541                      * to contradict this (comment out the assignment to
542                      * suppress below if you want to implement these
543                      * alternate semantics) but the windows program I'm
544                      * looking at expects the behavior I've coded here
545                      * (which happens to be what glibc does as well).
546                      */
547                     suppress = 1;
548                     st = 1;
549                 }
550                 break;
551             case '[': {
552                     _CHAR_ *str = suppress ? NULL : va_arg(ap, _CHAR_*);
553                     _CHAR_ *sptr = str;
554                     RTL_BITMAP bitMask;
555                     ULONG *Mask;
556                     int invert = 0; /* Set if we are NOT to find the chars */
557 #ifdef SECURE
558                     unsigned size = suppress ? UINT_MAX : va_arg(ap, unsigned)/sizeof(_CHAR_);
559 #else
560                     unsigned size = UINT_MAX;
561 #endif
562
563                     /* Init our bitmap */
564                     Mask = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _BITMAPSIZE_/8);
565                     RtlInitializeBitMap(&bitMask, Mask, _BITMAPSIZE_);
566
567                     /* Read the format */
568                     format++;
569                     if(*format == '^') {
570                         invert = 1;
571                         format++;
572                     }
573                     if(*format == ']') {
574                         RtlSetBits(&bitMask, ']', 1);
575                         format++;
576                     }
577                     while(*format && (*format != ']')) {
578                         /* According to msdn:
579                          * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
580                         if((*format == '-') && (*(format + 1) != ']')) {
581                             if ((*(format - 1)) < *(format + 1))
582                                 RtlSetBits(&bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1));
583                             else
584                                 RtlSetBits(&bitMask, *(format + 1)    , *(format - 1) - *(format + 1));                       
585                             format++;
586                         } else
587                             RtlSetBits(&bitMask, *format, 1);
588                         format++;
589                     }
590                     /* read until char is not suitable */
591                     while ((width != 0) && (nch != _EOF_)) {
592                         if(!invert) {
593                             if(RtlAreBitsSet(&bitMask, nch, 1)) {
594                                 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
595                             } else
596                                 break;
597                         } else {
598                             if(RtlAreBitsClear(&bitMask, nch, 1)) {
599                                 if (!suppress) *sptr++ = _CHAR2SUPPORTED_(nch);
600                             } else
601                                 break;
602                         }
603                         st++;
604                         nch = _GETC_(file);
605                         if (width>0) width--;
606                         if(size>1) size--;
607                         else {
608                             _UNLOCK_FILE_(file);
609                             *str = 0;
610                             return rd;
611                         }
612                     }
613                     /* terminate */
614                     if (!suppress) *sptr = 0;
615                     HeapFree(GetProcessHeap(), 0, Mask);
616                 }
617                 break;
618             default:
619                 /* From spec: "if a percent sign is followed by a character
620                  * that has no meaning as a format-control character, that
621                  * character and the following characters are treated as
622                  * an ordinary sequence of characters, that is, a sequence
623                  * of characters that must match the input.  For example,
624                  * to specify that a percent-sign character is to be input,
625                  * use %%." */
626                 while ((nch!=_EOF_) && _ISSPACE_(nch))
627                     nch = _GETC_(file);
628                 if (nch==*format) {
629                     suppress = 1; /* whoops no field to be read */
630                     st = 1; /* but we got what we expected */
631                     nch = _GETC_(file);
632                 }
633                 break;
634             }
635             if (st && !suppress) rd++;
636             else if (!st) break;
637         }
638         /* a non-white-space character causes scanf to read, but not store,
639          * a matching non-white-space character. */
640         else {
641             /* check for character match */
642             if (nch == *format) {
643                 nch = _GETC_(file);
644             } else break;
645         }
646         format++;
647     }
648     if (nch!=_EOF_) {
649         _UNGETC_(nch, file);
650     }
651
652     TRACE("returning %d\n", rd);
653     _UNLOCK_FILE_(file);
654     return rd;
655 }
656
657 #undef _CHAR_
658 #undef _EOF_
659 #undef _EOF_RET
660 #undef _ISSPACE_
661 #undef _ISDIGIT_
662 #undef _CHAR2SUPPORTED_
663 #undef _WIDE2SUPPORTED_
664 #undef _CHAR2DIGIT_
665 #undef _GETC_
666 #undef _UNGETC_
667 #undef _LOCK_FILE_
668 #undef _UNLOCK_FILE_
669 #undef _FUNCTION_
670 #undef _BITMAPSIZE_