OSDN Git Service

Hide more of stdio,getdents, use internal __raise
[uclinux-h8/uClibc.git] / libc / stdio / scanf.c
1 /*  Copyright (C) 2002-2004     Manuel Novoa III
2  *
3  *  This library is free software; you can redistribute it and/or
4  *  modify it under the terms of the GNU Library General Public
5  *  License as published by the Free Software Foundation; either
6  *  version 2 of the License, or (at your option) any later version.
7  *
8  *  This library is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  *  Library General Public License for more details.
12  *
13  *  You should have received a copy of the GNU Library General Public
14  *  License along with this library; if not, write to the Free
15  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  */
17
18 /* Aug 1, 2003
19  * New *scanf implementation with lots of bug fixes and *wscanf support.
20  * Also now optionally supports hexadecimal float notation, positional
21  * args, and glibc locale-specific digit grouping.  Should now be
22  * standards compliant.
23  *
24  * Aug 18, 2003
25  * Bug fix: scanf %lc,%ls,%l[ would always set mb_fail on eof or error,
26  *   even when just starting a new mb char.
27  * Bug fix: wscanf would incorrectly unget in certain situations.
28  *
29  * Sep 5, 2003
30  * Bug fix: store flag wasn't respected if no positional args.
31  * Implement vs{n}scanf for the non-buffered stdio no-wchar case.
32  *
33  * Sep 13, 2003
34  * Bug fix: Fix a problem reported by Atsushi Nemoto <anemo@mba.ocn.ne.jp>
35  * for environments where long and long long are the same.
36  *
37  * Sep 21, 2003
38  * Ugh... EOF handling by scanf was completely broken.  :-(  Regretably,
39  * I got my mind fixed in one mode and didn't comply with the standards.
40  * Things should be fixed now, but comparision testing is difficult when
41  * glibc's scanf is broken and they stubbornly refuse to even acknowledge
42  * that it is... even when confronted by specific examples from the C99
43  * standards and from an official C standard defect report.
44  */
45
46
47 #define _ISOC99_SOURCE                  /* for LLONG_MAX primarily... */
48 #define _GNU_SOURCE
49 #include <features.h>
50 #include "_stdio.h"
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <ctype.h>
54 #include <string.h>
55 #include <stdarg.h>
56 #include <stdint.h>
57 #include <errno.h>
58 #include <printf.h>
59
60 #ifdef __UCLIBC_HAS_WCHAR__
61 #include <bits/uClibc_uwchar.h>
62 #include <wchar.h>
63 #include <wctype.h>
64 #endif /* __UCLIBC_HAS_WCHAR__ */
65
66 #include <langinfo.h>
67 #include <locale.h>
68
69 #include <assert.h>
70 #include <limits.h>
71
72 #ifdef __UCLIBC_HAS_THREADS__
73 #include <stdio_ext.h>
74 #include <pthread.h>
75 #endif /* __UCLIBC_HAS_THREADS__ */
76
77 #ifdef __UCLIBC_HAS_FLOATS__
78 #include <float.h>
79 #include <bits/uClibc_fpmax.h>
80 #endif /* __UCLIBC_HAS_FLOATS__ */
81
82 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
83 #ifdef L_vfscanf
84 /* only emit this once */
85 #warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
86 #endif
87 #undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
88 #endif
89
90 #undef __STDIO_HAS_VSSCANF
91 #if defined(__STDIO_BUFFERS) || !defined(__UCLIBC_HAS_WCHAR__) || defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
92 #define __STDIO_HAS_VSSCANF 1
93
94 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
95 typedef struct {
96         FILE f;
97         unsigned char *bufread;         /* pointer to 1 past end of buffer */
98         unsigned char *bufpos;
99 } __FILE_vsscanf;
100 #endif
101
102 #endif
103
104 extern void _store_inttype(void *dest, int desttype, uintmax_t val);
105
106 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
107
108 extern unsigned long long
109 _stdlib_strto_ll(register const char * __restrict str,
110                                  char ** __restrict endptr, int base, int sflag);
111 #if (ULLONG_MAX == UINTMAX_MAX)
112 #define STRTOUIM(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
113 #endif
114
115 #else  /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
116
117 extern unsigned long
118 _stdlib_strto_l(register const char * __restrict str,
119                                 char ** __restrict endptr, int base, int sflag);
120
121 #if (ULONG_MAX == UINTMAX_MAX)
122 #define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
123 #endif
124
125 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
126
127 #ifndef STRTOUIM
128 #error STRTOUIM conversion function is undefined!
129 #endif
130
131 /**********************************************************************/
132
133 /* The standards require EOF < 0. */
134 #if EOF >= CHAR_MIN
135 #define __isdigit_char_or_EOF(C)   __isdigit_char((C))
136 #else
137 #define __isdigit_char_or_EOF(C)   __isdigit_int((C))
138 #endif
139
140 /**********************************************************************/
141 #ifdef L_fscanf
142
143 int fscanf(FILE * __restrict stream, const char * __restrict format, ...)
144 {
145         va_list arg;
146         int rv;
147
148         va_start(arg, format);
149         rv = vfscanf(stream, format, arg);
150         va_end(arg);
151
152         return rv;
153 }
154
155 #endif
156 /**********************************************************************/
157 #ifdef L_scanf
158
159 int scanf(const char * __restrict format, ...)
160 {
161         va_list arg;
162         int rv;
163
164         va_start(arg, format);
165         rv = vfscanf(stdin, format, arg);
166         va_end(arg);
167
168         return rv;
169 }
170
171 #endif
172 /**********************************************************************/
173 #ifdef L_sscanf
174
175 #ifdef __STDIO_HAS_VSSCANF
176
177 int sscanf(const char * __restrict str, const char * __restrict format, ...)
178 {
179         va_list arg;
180         int rv;
181
182         va_start(arg, format);
183         rv = vsscanf(str, format, arg);
184         va_end(arg);
185
186         return rv;
187 }
188
189 #else  /* __STDIO_HAS_VSSCANF */
190 #warning Skipping sscanf since no vsscanf!
191 #endif /* __STDIO_HAS_VSSCANF */
192
193 #endif
194 /**********************************************************************/
195 #ifdef L_vscanf
196
197 int vscanf(const char * __restrict format, va_list arg)
198 {
199         return vfscanf(stdin, format, arg);
200 }
201
202 #endif
203 /**********************************************************************/
204 #ifdef L_vsscanf
205
206 #ifdef __UCLIBC_MJN3_ONLY__
207 #warning WISHLIST: Implement vsscanf for non-buf and no custom stream case.
208 #endif /* __UCLIBC_MJN3_ONLY__ */
209
210 #ifdef __STDIO_BUFFERS
211
212 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
213 {
214         FILE f;
215
216 /*      __STDIO_STREAM_RESET_GCS(&f); */
217 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
218         f.__cookie = &(f.__filedes);
219         f.__gcs.read = NULL;
220         f.__gcs.write = NULL;
221         f.__gcs.seek = NULL;
222         f.__gcs.close = NULL;
223 #endif
224
225         f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES;
226         f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
227
228 #ifdef __UCLIBC_HAS_WCHAR__
229         f.__ungot_width[0] = 0;
230 #endif
231 #ifdef __STDIO_MBSTATE
232         __INIT_MBSTATE(&(f.__state));
233 #endif
234
235 #ifdef __UCLIBC_HAS_THREADS__
236         f.__user_locking = 1;           /* Set user locking. */
237         __stdio_init_mutex(&f.__lock);
238 #endif
239         f.__nextopen = NULL;
240
241         /* Set these last since __bufgetc initialization depends on
242          * __user_locking and only gets set if user locking is on. */
243         f.__bufstart = 
244         f.__bufpos = (unsigned char *) ((void *) sp);
245         f.__bufread =
246         f.__bufend = f.__bufstart + strlen(sp);
247         __STDIO_STREAM_ENABLE_GETC(&f);
248         __STDIO_STREAM_DISABLE_PUTC(&f);
249
250         return vfscanf(&f, fmt, ap);
251 }
252
253 #elif !defined(__UCLIBC_HAS_WCHAR__)
254
255 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
256 {
257         __FILE_vsscanf f;
258
259         f.bufpos = (unsigned char *) ((void *) sp);
260         f.bufread = f.bufpos + strlen(sp);
261
262 /*      __STDIO_STREAM_RESET_GCS(&f.f); */
263 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
264         f.f.__cookie = &(f.f.__filedes);
265         f.f.__gcs.read = NULL;
266         f.f.__gcs.write = NULL;
267         f.f.__gcs.seek = NULL;
268         f.f.__gcs.close = NULL;
269 #endif
270
271         f.f.__filedes = __STDIO_STREAM_FAKE_VSSCANF_FILEDES_NB;
272         f.f.__modeflags = (__FLAG_NARROW|__FLAG_READONLY|__FLAG_READING);
273
274 /* #ifdef __UCLIBC_HAS_WCHAR__ */
275 /*      f.f.__ungot_width[0] = 0; */
276 /* #endif */
277 #ifdef __STDIO_MBSTATE
278 #error __STDIO_MBSTATE is defined!
279 /*      __INIT_MBSTATE(&(f.f.__state)); */
280 #endif
281
282 #ifdef __UCLIBC_HAS_THREADS__
283         f.f.__user_locking = 1;         /* Set user locking. */
284         __stdio_init_mutex(&f.f.__lock);
285 #endif
286         f.f.__nextopen = NULL;
287
288         return vfscanf(&f.f, fmt, ap);
289 }
290
291 #elif defined(__UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__)
292
293 int vsscanf(__const char *sp, __const char *fmt, va_list ap)
294 {
295         FILE *f;
296         int rv = EOF;
297
298         if ((f = fmemopen((char *)sp, strlen(sp), "r")) != NULL) {
299                 rv = vfscanf(f, fmt, ap);
300                 fclose(f);
301         }
302
303         return rv;
304 }
305
306 #else
307 #warning Skipping vsscanf since no buffering, no custom streams, and wchar enabled!
308 #ifdef __STDIO_HAS_VSSCANF
309 #error WHOA! __STDIO_HAS_VSSCANF is defined!
310 #endif
311 #endif
312
313 #endif
314 /**********************************************************************/
315 #ifdef L_fwscanf
316
317 int fwscanf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
318 {
319         va_list arg;
320         int rv;
321
322         va_start(arg, format);
323         rv = vfwscanf(stream, format, arg);
324         va_end(arg);
325
326         return rv;
327 }
328
329 #endif
330 /**********************************************************************/
331 #ifdef L_wscanf
332
333 int wscanf(const wchar_t * __restrict format, ...)
334 {
335         va_list arg;
336         int rv;
337
338         va_start(arg, format);
339         rv = vfwscanf(stdin, format, arg);
340         va_end(arg);
341
342         return rv;
343 }
344
345 #endif
346 /**********************************************************************/
347 #ifdef L_swscanf
348
349 #ifdef __STDIO_BUFFERS
350
351 int swscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
352                    ...)
353 {
354         va_list arg;
355         int rv;
356
357         va_start(arg, format);
358         rv = vswscanf(str, format, arg);
359         va_end(arg);
360
361         return rv;
362 }
363 #else  /* __STDIO_BUFFERS */
364 #warning Skipping swscanf since no buffering!
365 #endif /* __STDIO_BUFFERS */
366
367 #endif
368 /**********************************************************************/
369 #ifdef L_vwscanf
370
371 int vwscanf(const wchar_t * __restrict format, va_list arg)
372 {
373         return vfwscanf(stdin, format, arg);
374 }
375
376 #endif
377 /**********************************************************************/
378 #ifdef L_vswscanf
379
380 #ifdef __STDIO_BUFFERS
381
382 int vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
383                         va_list arg)
384 {
385         FILE f;
386
387         f.__bufstart =
388         f.__bufpos = (char *) str;
389         f.__bufread =
390         f.__bufend = (char *)(str + wcslen(str));
391         __STDIO_STREAM_DISABLE_GETC(&f);
392         __STDIO_STREAM_DISABLE_PUTC(&f);
393
394 /*      __STDIO_STREAM_RESET_GCS(&f); */
395 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
396         f.__cookie = &(f.__filedes);
397         f.__gcs.read = NULL;
398         f.__gcs.write = NULL;
399         f.__gcs.seek = NULL;
400         f.__gcs.close = NULL;
401 #endif
402
403         f.__filedes = __STDIO_STREAM_FAKE_VSWSCANF_FILEDES;
404         f.__modeflags = (__FLAG_WIDE|__FLAG_READONLY|__FLAG_READING);
405
406 #ifdef __UCLIBC_HAS_WCHAR__
407         f.__ungot_width[0] = 0;
408 #endif /* __UCLIBC_HAS_WCHAR__ */
409 #ifdef __STDIO_MBSTATE
410         __INIT_MBSTATE(&(f.__state));
411 #endif /* __STDIO_MBSTATE */
412
413 #ifdef __UCLIBC_HAS_THREADS__
414         f.__user_locking = 1;           /* Set user locking. */
415         __stdio_init_mutex(&f.__lock);
416 #endif
417         f.__nextopen = NULL;
418
419         return vfwscanf(&f, format, arg);
420 }
421 #else  /* __STDIO_BUFFERS */
422 #warning Skipping vswscanf since no buffering!
423 #endif /* __STDIO_BUFFERS */
424
425 #endif
426 /**********************************************************************/
427 /**********************************************************************/
428
429
430
431 /* float layout          0123456789012345678901  repeat n for "l[" */
432 #define SPEC_CHARS              "npxXoudifFeEgGaACSncs["
433 /*                       npxXoudif eEgG  CS cs[ */
434
435 /* NOTE: Ordering is important!  In particular, CONV_LEFTBRACKET
436  * must immediately precede CONV_c. */
437
438 enum {
439         CONV_n = 0,
440         CONV_p,
441         CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
442         CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
443         CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
444         CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
445 };
446
447 #ifdef __UCLIBC_HAS_FLOATS__
448 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
449 /*                         p   x   X  o   u   d   i   f   F   e   E   g   G   a   A */
450 #define SPEC_BASE               { 16, 16, 16, 8, 10, 10,  0,  0,  0,  0,  0,  0,  0,  0,  0 }
451 #else
452 /*                         p   x   X  o   u   d   i   f   F   e   E   g   G   a   A */
453 #define SPEC_BASE               { 16, 16, 16, 8, 10, 10,  0, 10, 10, 10, 10, 10, 10, 10, 10 }
454 #endif
455 #else  /* __UCLIBC_HAS_FLOATS__ */
456 /*                         p   x   X  o   u   d   i   f   F   e   E   g   G   a   A */
457 #define SPEC_BASE               { 16, 16, 16, 8, 10, 10,  0 }
458 #endif /* __UCLIBC_HAS_FLOATS__ */
459
460 #ifdef __UCLIBC_MJN3_ONLY__
461 #ifdef L_vfscanf
462 /* emit once */
463 #warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
464 #endif
465 #endif /* __UCLIBC_MJN3_ONLY__ */
466
467 #define SPEC_FLAGS              "*'I"
468
469 enum {
470         FLAG_SURPRESS   =   0x10,       /* MUST BE 1ST!!  See DO_FLAGS. */
471         FLAG_THOUSANDS  =       0x20,
472         FLAG_I18N               =       0x40,   /* only works for d, i, u */
473         FLAG_MALLOC     =   0x80,       /* only works for s, S, and [ (and l[)*/
474 };        
475
476
477 #define SPEC_RANGES             { CONV_n, CONV_p, CONV_i, CONV_A, \
478                                                   CONV_C, CONV_LEFTBRACKET, \
479                                                   CONV_c, CONV_leftbracket }
480
481 /* Note: We treat L and ll as synonymous... for ints and floats. */
482
483 #define SPEC_ALLOWED_FLAGS              { \
484         /* n */                 (0x0f|FLAG_SURPRESS), \
485         /* p */                 (   0|FLAG_SURPRESS), \
486         /* oxXudi */    (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
487         /* fFeEgGaA */  (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
488         /* C */                 (   0|FLAG_SURPRESS), \
489         /* S and l[ */  (   0|FLAG_SURPRESS|FLAG_MALLOC), \
490         /* c */                 (0x04|FLAG_SURPRESS), \
491         /* s and [ */   (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
492 }
493
494
495 /**********************************************************************/
496 /*
497  * In order to ease translation to what arginfo and _print_info._flags expect,
498  * we map:  0:int  1:char  2:longlong 4:long  8:short
499  * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
500  */
501
502 /* TODO -- Fix the table below to take into account stdint.h. */
503 /*  #ifndef LLONG_MAX */
504 /*  #error fix QUAL_CHARS for no long long!  Affects 'L', 'j', 'q', 'll'. */
505 /*  #else */
506 /*  #if LLONG_MAX != INTMAX_MAX */
507 /*  #error fix QUAL_CHARS intmax_t entry 'j'! */
508 /*  #endif */
509 /*  #endif */
510
511 #ifdef PDS
512 #error PDS already defined!
513 #endif
514 #ifdef SS
515 #error SS already defined!
516 #endif
517 #ifdef IMS
518 #error IMS already defined!
519 #endif
520
521 #if PTRDIFF_MAX == INT_MAX
522 #define PDS             0
523 #elif PTRDIFF_MAX == LONG_MAX
524 #define PDS             4
525 #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
526 #define PDS             8
527 #else
528 #error fix QUAL_CHARS ptrdiff_t entry 't'!
529 #endif
530
531 #if SIZE_MAX == UINT_MAX
532 #define SS              0
533 #elif SIZE_MAX == ULONG_MAX
534 #define SS              4
535 #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
536 #define SS              8
537 #else
538 #error fix QUAL_CHARS size_t entries 'z', 'Z'!
539 #endif
540
541 #if INTMAX_MAX == INT_MAX
542 #define IMS             0
543 #elif INTMAX_MAX == LONG_MAX
544 #define IMS             4
545 #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
546 #define IMS             8
547 #else
548 #error fix QUAL_CHARS ptrdiff_t entry 't'!
549 #endif
550
551 #define QUAL_CHARS              { \
552         /* j:(u)intmax_t z:(s)size_t  t:ptrdiff_t  \0:int  q:long_long */ \
553         'h',   'l',  'L',  'j',  'z',  't',  'q', 0, \
554          2,     4,    8,  IMS,   SS,  PDS,    8,  0, /* TODO -- fix!!! */\
555      1,     8   }
556
557
558 /**********************************************************************/
559
560 #ifdef L_vfwscanf
561 #if WINT_MIN > EOF
562 #error Unfortunately, we currently need wint_t to be able to store EOF.  Sorry.
563 #endif
564 #define W_EOF WEOF
565 #define Wint wint_t
566 #define Wchar wchar_t
567 #define Wuchar __uwchar_t
568 #define ISSPACE(C) iswspace((C))
569 #define VFSCANF vfwscanf
570 #define GETC(SC) (SC)->sc_getc((SC))
571 #else
572 typedef unsigned char __uchar_t;
573 #define W_EOF EOF
574 #define Wint int
575 #define Wchar char
576 #define Wuchar __uchar_t
577 #define ISSPACE(C) isspace((C))
578 #define VFSCANF vfscanf
579 #ifdef __UCLIBC_HAS_WCHAR__
580 #define GETC(SC) (SC)->sc_getc((SC))
581 #else  /* __UCLIBC_HAS_WCHAR__ */
582 #define GETC(SC) getc_unlocked((SC)->fp)
583 #endif /* __UCLIBC_HAS_WCHAR__ */
584 #endif
585
586 struct scan_cookie {
587         Wint cc;
588         Wint ungot_char;
589         FILE *fp;
590         int nread;
591         int width;
592
593 #ifdef __UCLIBC_HAS_WCHAR__
594         wchar_t app_ungot;                      /* Match FILE struct member type. */
595         unsigned char ungot_wchar_width;
596 #else  /* __UCLIBC_HAS_WCHAR__ */
597         unsigned char app_ungot;        /* Match FILE struct member type. */
598 #endif /* __UCLIBC_HAS_WCHAR__ */
599
600         char ungot_flag;
601
602 #ifdef __UCLIBC_HAS_WCHAR__
603         char ungot_wflag;                       /* vfwscanf */
604         char mb_fail;                           /* vfscanf */
605         mbstate_t mbstate;                      /* vfscanf */
606         wint_t wc;
607         wint_t ungot_wchar;                     /* to support __scan_getc */
608         int (*sc_getc)(struct scan_cookie *);
609 #endif /* __UCLIBC_HAS_WCHAR__ */
610
611 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
612         const char *grouping;
613         const unsigned char *thousands_sep;
614         int tslen;
615 #ifdef __UCLIBC_HAS_WCHAR__
616         wchar_t thousands_sep_wc;
617 #endif /* __UCLIBC_HAS_WCHAR__ */
618 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
619
620 #ifdef __UCLIBC_HAS_FLOATS__
621         const unsigned char *decpt;
622         int decpt_len;
623 #ifdef __UCLIBC_HAS_WCHAR__
624         wchar_t decpt_wc;
625 #endif /* __UCLIBC_HAS_WCHAR__ */
626         const unsigned char *fake_decpt;
627 #endif /* __UCLIBC_HAS_FLOATS__ */
628
629 };
630
631 typedef struct {
632 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
633 #if NL_ARGMAX > 10
634 #warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
635 #endif
636         void *pos_args[NL_ARGMAX];
637         int num_pos_args;               /* Must start at -1. */
638         int cur_pos_arg;
639 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
640         void *cur_ptr;
641         const unsigned char *fmt;
642         int cnt, dataargtype, conv_num, max_width;
643         unsigned char store, flags;
644 } psfs_t;                                               /* parse scanf format state */
645
646
647 /**********************************************************************/
648 /**********************************************************************/
649
650 extern void __init_scan_cookie(register struct scan_cookie *sc,
651                                                            register FILE *fp) attribute_hidden;
652 extern int __scan_getc(register struct scan_cookie *sc) attribute_hidden;
653 extern void __scan_ungetc(register struct scan_cookie *sc) attribute_hidden;
654
655 #ifdef __UCLIBC_HAS_FLOATS__
656 extern int __scan_strtold(long double *ld, struct scan_cookie *sc);
657 #endif /* __UCLIBC_HAS_FLOATS__ */
658
659 extern int __psfs_parse_spec(psfs_t *psfs) attribute_hidden;
660 extern int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc) attribute_hidden;
661
662 /**********************************************************************/
663 #ifdef L___scan_cookie
664
665 #ifdef __UCLIBC_MJN3_ONLY__
666 #warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
667 #endif
668 #ifndef __UCLIBC_HAS_LOCALE__
669 static const char decpt_str[] = ".";
670 #endif
671
672 void attribute_hidden __init_scan_cookie(register struct scan_cookie *sc,
673                                                 register FILE *fp)
674 {
675         sc->fp = fp;
676         sc->nread = 0;
677         sc->ungot_flag = 0;
678         sc->app_ungot = ((fp->__modeflags & __FLAG_UNGOT) ? fp->__ungot[1] : 0);
679 #ifdef __UCLIBC_HAS_WCHAR__
680         sc->ungot_wflag = 0;            /* vfwscanf */
681         sc->mb_fail = 0;
682 #endif /* __UCLIBC_HAS_WCHAR__ */
683
684 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
685         if (*(sc->grouping = __UCLIBC_CURLOCALE_DATA.grouping)) {
686                 sc->thousands_sep = __UCLIBC_CURLOCALE_DATA.thousands_sep;
687                 sc->tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
688 #ifdef __UCLIBC_HAS_WCHAR__
689                 sc->thousands_sep_wc = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
690 #endif /* __UCLIBC_HAS_WCHAR__ */
691         }
692 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
693
694 #ifdef __UCLIBC_HAS_FLOATS__
695 #ifdef __UCLIBC_HAS_LOCALE__
696         sc->decpt = __UCLIBC_CURLOCALE_DATA.decimal_point;
697         sc->decpt_len = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
698 #else  /* __UCLIBC_HAS_LOCALE__ */
699         sc->fake_decpt = sc->decpt = decpt_str;
700         sc->decpt_len = 1;
701 #endif /* __UCLIBC_HAS_LOCALE__ */
702 #ifdef __UCLIBC_HAS_WCHAR__
703 #ifdef __UCLIBC_HAS_LOCALE__
704         sc->decpt_wc = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
705 #else
706         sc->decpt_wc = '.';
707 #endif
708 #endif /* __UCLIBC_HAS_WCHAR__ */
709 #endif /* __UCLIBC_HAS_FLOATS__ */
710
711 }
712
713 int attribute_hidden __scan_getc(register struct scan_cookie *sc)
714 {
715         int c;
716
717 #ifdef __UCLIBC_HAS_WCHAR__
718         assert(!sc->mb_fail);
719 #endif /* __UCLIBC_HAS_WCHAR__ */
720
721         sc->cc = EOF;
722
723         if (--sc->width < 0) {
724                 sc->ungot_flag |= 2;
725                 return -1;
726         }
727
728         if (sc->ungot_flag == 0) {
729 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
730                 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
731                         c = GETC(sc);
732                 } else {
733                         __FILE_vsscanf *fv = (__FILE_vsscanf *)(sc->fp);
734                         if (fv->bufpos < fv->bufread) {
735                                 c = *fv->bufpos++;
736                         } else {
737                                 c = EOF;
738                                 sc->fp->__modeflags |= __FLAG_EOF;
739                         }
740                 }
741                 if (c == EOF) {
742                         sc->ungot_flag |= 2;
743                         return -1;
744                 }
745 #else
746                 if ((c = GETC(sc)) == EOF) {
747                         sc->ungot_flag |= 2;
748                         return -1;
749                 }
750 #endif
751                 sc->ungot_char = c;
752         } else {
753                 assert(sc->ungot_flag == 1);
754                 sc->ungot_flag = 0;
755         }
756
757         ++sc->nread;
758         return sc->cc = sc->ungot_char;
759 }
760
761 void attribute_hidden __scan_ungetc(register struct scan_cookie *sc)
762 {
763         ++sc->width;
764         if (sc->ungot_flag == 2) {      /* last was EOF */
765                 sc->ungot_flag = 0;
766                 sc->cc = sc->ungot_char;
767         } else if (sc->ungot_flag == 0) {
768                 sc->ungot_flag = 1;
769                 --sc->nread;
770         } else {
771                 assert(0);
772         }
773 }
774
775 #endif
776 /**********************************************************************/
777 #ifdef L___psfs_parse_spec
778
779 #ifdef SPEC_FLAGS
780 static const unsigned char spec_flags[] = SPEC_FLAGS;
781 #endif /* SPEC_FLAGS */
782 static const unsigned char spec_chars[] = SPEC_CHARS;
783 static const unsigned char qual_chars[] = QUAL_CHARS;
784 static const unsigned char spec_ranges[] = SPEC_RANGES;
785 static const unsigned short spec_allowed[] = SPEC_ALLOWED_FLAGS;
786
787 int attribute_hidden __psfs_parse_spec(register psfs_t *psfs)
788 {
789         const unsigned char *p;
790         const unsigned char *fmt0 = psfs->fmt;
791         int i;
792 #ifdef SPEC_FLAGS
793         int j;
794 #endif
795 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
796         unsigned char fail = 0;
797
798         i = 0;                                          /* Do this here to avoid a warning. */
799
800         if (!__isdigit_char(*psfs->fmt)) { /* Not a positional arg. */
801                 fail = 1;
802                 goto DO_FLAGS;
803         }
804
805         /* parse the positional arg (or width) value */
806         do {
807                 if (i <= ((INT_MAX - 9)/10)) {
808                         i = (i * 10) + (*psfs->fmt++ - '0');
809                 }
810         } while (__isdigit_char(*psfs->fmt));
811
812         if (*psfs->fmt != '$') { /* This is a max field width. */
813                 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
814                         goto ERROR_EINVAL;
815                 }
816                 psfs->max_width = i;
817                 psfs->num_pos_args = -2;
818                 goto DO_QUALIFIER;
819         }
820         ++psfs->fmt;                    /* Advance past '$'. */
821 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
822
823 #if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
824  DO_FLAGS:
825 #endif /* defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0)) */
826 #ifdef SPEC_FLAGS
827         p = spec_flags;
828         j = FLAG_SURPRESS;
829         do {
830                 if (*p == *psfs->fmt) {
831                         ++psfs->fmt;
832                         psfs->flags |= j;
833                         goto DO_FLAGS;
834                 }
835                 j += j;
836         } while (*++p);
837
838         if (psfs->flags & FLAG_SURPRESS) { /* Suppress assignment. */
839                 psfs->store = 0;
840                 goto DO_WIDTH;
841         }
842 #else  /* SPEC_FLAGS */
843         if (*psfs->fmt == '*') {        /* Suppress assignment. */
844                 ++psfs->fmt;
845                 psfs->store = 0;
846                 goto DO_WIDTH;
847         }
848 #endif /* SPEC_FLAGS */
849
850
851 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
852         if (fail) {
853                 /* Must be a non-positional arg */
854                 if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
855                         goto ERROR_EINVAL;
856                 }
857                 psfs->num_pos_args = -2;
858         } else {
859                 if ((psfs->num_pos_args == -2) || (((unsigned int)(--i)) >= NL_ARGMAX)) {
860                         /* Already saw a non-pos arg or (0-based) num too large. */
861                         goto ERROR_EINVAL;
862                 }
863                 psfs->cur_pos_arg = i;
864         }
865 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
866
867  DO_WIDTH:
868         for (i = 0 ; __isdigit_char(*psfs->fmt) ; ) {
869                 if (i <= ((INT_MAX - 9)/10)) {
870                         i = (i * 10) + (*psfs->fmt++ - '0');
871                         psfs->max_width = i;
872                 }
873         }
874
875 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
876  DO_QUALIFIER:
877 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
878         p = qual_chars;
879         do {
880                 if (*psfs->fmt == *p) {
881                         ++psfs->fmt;
882                         break;
883                 }
884         } while (*++p);
885         if ((p - qual_chars < 2) && (*psfs->fmt == *p)) {
886                 p += ((sizeof(qual_chars)-2) / 2);
887                 ++psfs->fmt;
888         }
889         psfs->dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
890
891 #ifdef __UCLIBC_MJN3_ONLY__
892 #warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()?  It would avoid whitespace consumption...
893 #warning CONSIDER: Should INT_MAX be a valid width (%c/%C)?  See __psfs_parse_spec().
894 #endif /* __UCLIBC_MJN3_ONLY__ */
895
896         p = spec_chars;
897         do {
898                 if (*psfs->fmt == *p) {
899                         int p_m_spec_chars = p - spec_chars;
900
901 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
902 #error implement gnu a flag
903                         if ((*p == 'a')
904                                 && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
905                                 ) {             /* Assumes ascii for 's' and 'S' test. */
906                                 psfs->flags |= FLAG_MALLOC;
907                                 ++psfs->fmt;
908                                 ++p;
909                                 continue; /* The related conversions follow 'a'. */
910                         }
911 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
912
913                         for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
914                         if (((psfs->dataargtype >> 8) | psfs->flags)
915                                 & ~spec_allowed[(int)(p - spec_ranges)]
916                                 ) {
917                                 goto ERROR_EINVAL;
918                         }
919
920                         if ((p_m_spec_chars >= CONV_c)
921                                 && (psfs->dataargtype & PA_FLAG_LONG)) {
922                                 p_m_spec_chars -= 3; /* lc -> C, ls -> S, l[ -> ?? */
923                         }
924
925                         psfs->conv_num = p_m_spec_chars;
926                         return psfs->fmt - fmt0;
927                 }
928                 if (!*++p) {
929                 ERROR_EINVAL:
930                         __set_errno(EINVAL);
931                         return -1;
932                 }
933         } while(1);
934
935         assert(0);
936 }
937
938 #endif
939 /**********************************************************************/
940 #if defined(L_vfscanf) || defined(L_vfwscanf)
941
942 #ifdef __UCLIBC_HAS_WCHAR__
943 #ifdef L_vfscanf
944 static int sc_getc(register struct scan_cookie *sc)
945 {
946         return (getc_unlocked)(sc->fp); /* Disable the macro. */
947 }
948
949 static int scan_getwc(register struct scan_cookie *sc)
950 {
951         size_t r;
952         int width;
953         wchar_t wc[1];
954         char b[1];
955
956         if (--sc->width < 0) {
957                 sc->ungot_flag |= 2;
958                 return -1;
959         }
960
961         width = sc->width;                      /* Preserve width. */
962         sc->width = INT_MAX;            /* MB_CUR_MAX can invoke a function. */
963
964         assert(!sc->mb_fail);
965
966         r = (size_t)(-3);
967         while (__scan_getc(sc) >= 0) {
968                 *b = sc->cc;
969
970                 r = mbrtowc(wc, b, 1, &sc->mbstate);
971                 if (((ssize_t) r) >= 0) { /* Successful completion of a wc. */
972                         sc->wc = *wc;
973                         goto SUCCESS;
974                 } else if (r == ((size_t) -2)) {
975                         /* Potentially valid but incomplete. */
976                         continue;
977                 }
978                 break;
979         }
980
981         if (r == ((size_t)(-3))) {      /* EOF or ERROR on first read */
982                 sc->wc = WEOF;
983                 r = (size_t)(-1);
984         } else {
985                 /* If we reach here, either r == ((size_t)-1) and
986                  * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
987                  * and stream is in an error state or at EOF with a
988                  * partially complete wchar. */
989                 __set_errno(EILSEQ);            /* In case of incomplete conversion. */
990                 sc->mb_fail = 1;
991         }
992         
993  SUCCESS:
994         sc->width = width;                      /* Restore width. */
995
996         return (int)((ssize_t) r);
997 }
998
999 #endif /* L_vfscanf */
1000
1001 #ifdef L_vfwscanf
1002
1003 /* This gets called by __scan_getc.  __scan_getc is called by vfwscanf
1004  * when the next wide char is expected to be valid ascii (digits).
1005  */
1006 static int sc_getc(register struct scan_cookie *sc)
1007 {
1008         wint_t wc;
1009
1010         if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1011                 if (sc->fp->__bufpos < sc->fp->__bufend) {
1012                         wc = *((wchar_t *)(sc->fp->__bufpos));
1013                         sc->fp->__bufpos += sizeof(wchar_t);
1014                 } else {
1015                         sc->fp->__modeflags |= __FLAG_EOF;
1016                         return EOF;
1017                 }
1018         } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1019                 return EOF;
1020         }
1021
1022         sc->ungot_wflag = 1;
1023         sc->ungot_wchar = wc;
1024         sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1025
1026 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1027         if (wc == sc->thousands_sep_wc) {
1028                 wc = ',';
1029         } else
1030 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1031 #ifdef __UCLIBC_HAS_FLOATS__
1032         if (wc == sc->decpt_wc) {
1033                 wc = '.';
1034         } else
1035 #endif /* __UCLIBC_HAS_FLOATS__ */
1036         if (!__isascii(wc)) {
1037                 wc = '?';
1038         }
1039         sc->wc = sc->ungot_char = wc;
1040
1041         return (int) wc;
1042 }
1043
1044 static int scan_getwc(register struct scan_cookie *sc)
1045 {
1046         wint_t wc;
1047
1048         sc->wc = WEOF;
1049
1050         if (--sc->width < 0) {
1051                 sc->ungot_flag |= 2;
1052                 return -1;
1053         }
1054
1055         if (sc->ungot_flag == 0) {
1056                 if (__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)) {
1057                         if (sc->fp->__bufpos < sc->fp->__bufend) {
1058                                 wc = *((wchar_t *)(sc->fp->__bufpos));
1059                                 sc->fp->__bufpos += sizeof(wchar_t);
1060                         } else {
1061                                 sc->ungot_flag |= 2;
1062                                 return -1;
1063                         }
1064                 } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
1065                         sc->ungot_flag |= 2;
1066                         return -1;
1067                 }
1068                 sc->ungot_wflag = 1;
1069                 sc->ungot_char = wc;
1070                 sc->ungot_wchar_width = sc->fp->__ungot_width[0];
1071         } else {
1072                 assert(sc->ungot_flag == 1);
1073                 sc->ungot_flag = 0;
1074         }
1075
1076         ++sc->nread;
1077         sc->wc = sc->ungot_char;
1078
1079         return 0;
1080 }
1081
1082
1083 #endif /* L_vfwscanf */
1084 #endif /* __UCLIBC_HAS_WCHAR__ */
1085
1086 static __inline void kill_scan_cookie(register struct scan_cookie *sc)
1087 {
1088 #ifdef L_vfscanf
1089
1090         if (sc->ungot_flag & 1) {
1091 #if !defined(__STDIO_BUFFERS) && !defined(__UCLIBC_HAS_WCHAR__)
1092                 if (!__STDIO_STREAM_IS_FAKE_VSSCANF_NB(sc->fp)) {
1093                         ungetc(sc->ungot_char, sc->fp);
1094                 }
1095 #else
1096                 ungetc(sc->ungot_char, sc->fp);
1097 #endif
1098                 /* Deal with distiction between user and scanf ungots. */
1099                 if (sc->nread == 0) {   /* Only one char was read... app ungot? */
1100                         sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1101                 } else {
1102                         sc->fp->__ungot[1] = 0;
1103                 }
1104         }
1105
1106 #else
1107
1108         if ((sc->ungot_flag & 1) && (sc->ungot_wflag & 1)
1109                 && !__STDIO_STREAM_IS_FAKE_VSWSCANF(sc->fp)
1110                 && (sc->fp->__state.__mask == 0)
1111                 ) {
1112                 ungetwc(sc->ungot_char, sc->fp);
1113                 /* Deal with distiction between user and scanf ungots. */
1114                 if (sc->nread == 0) {   /* Only one char was read... app ungot? */
1115                         sc->fp->__ungot[1] = sc->app_ungot; /* restore ungot state. */
1116                 } else {
1117                         sc->fp->__ungot[1] = 0;
1118                 }
1119                 sc->fp->__ungot_width[1] = sc->ungot_wchar_width;
1120         }
1121
1122 #endif
1123 }
1124
1125 #ifdef L_vfwscanf
1126 #ifdef __UCLIBC_HAS_FLOATS__
1127 static const char fake_decpt_str[] = ".";
1128 #endif
1129 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1130 static const char fake_thousands_sep_str[] = ",";
1131 #endif
1132 #endif /* L_vfwscanf */
1133
1134
1135 int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
1136 {
1137         const Wuchar *fmt;
1138         unsigned char *b;
1139
1140
1141 #ifdef L_vfwscanf
1142         wchar_t wbuf[1];
1143         wchar_t *wb;
1144 #endif /* L_vfwscanf */
1145
1146 #ifdef __UCLIBC_HAS_WCHAR__
1147         mbstate_t mbstate;
1148 #endif /* __UCLIBC_HAS_WCHAR__ */
1149
1150         struct scan_cookie sc;
1151         psfs_t psfs;
1152
1153         int i;
1154
1155 #ifdef __UCLIBC_MJN3_ONLY__
1156 #warning TODO: Fix MAX_DIGITS.  We do not do binary, so...!
1157 #endif
1158 #define MAX_DIGITS 65                   /* Allow one leading 0. */
1159         unsigned char buf[MAX_DIGITS+2];
1160 #ifdef L_vfscanf
1161         unsigned char scanset[UCHAR_MAX + 1];
1162         unsigned char invert;           /* Careful!  Meaning changes. */
1163 #endif /* L_vfscanf */
1164         unsigned char fail;
1165         unsigned char zero_conversions = 1;
1166         __STDIO_AUTO_THREADLOCK_VAR;
1167
1168 #ifdef __UCLIBC_MJN3_ONLY__
1169 #warning TODO: Make checking of the format string in C locale an option.
1170 #endif
1171         /* To support old programs, don't check mb validity if in C locale. */
1172 #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
1173         /* ANSI/ISO C99 requires format string to be a valid multibyte string
1174          * beginning and ending in its initial shift state. */
1175         if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
1176                 const char *p = format;
1177                 mbstate.__mask = 0;             /* Initialize the mbstate. */
1178                 if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
1179                         __set_errno(EINVAL); /* Format string is invalid. */
1180                         return 0;
1181                 }
1182         }
1183 #endif /* defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) */
1184
1185 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1186         psfs.num_pos_args = -1;         /* Must start at -1. */
1187         /* Initialize positional arg ptrs to NULL. */
1188         memset(psfs.pos_args, 0, sizeof(psfs.pos_args));
1189 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1190
1191         __STDIO_AUTO_THREADLOCK(fp);
1192
1193         __STDIO_STREAM_VALIDATE(fp);
1194
1195         __init_scan_cookie(&sc,fp);
1196 #ifdef __UCLIBC_HAS_WCHAR__
1197         sc.sc_getc = sc_getc;
1198         sc.ungot_wchar_width = sc.fp->__ungot_width[1];
1199
1200 #ifdef L_vfwscanf
1201
1202 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1203         if (*sc.grouping) {
1204                 sc.thousands_sep = fake_thousands_sep_str;
1205                 sc.tslen = 1;
1206         }
1207 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
1208
1209 #ifdef __UCLIBC_HAS_FLOATS__
1210         sc.fake_decpt = fake_decpt_str;
1211 #endif /* __UCLIBC_HAS_FLOATS__ */
1212
1213 #else  /* L_vfwscanf */
1214
1215 #ifdef __UCLIBC_HAS_FLOATS__
1216         sc.fake_decpt = sc.decpt;
1217 #endif /* __UCLIBC_HAS_FLOATS__ */
1218
1219 #endif /* L_vfwscanf */
1220
1221 #endif /* __UCLIBC_HAS_WCHAR__ */
1222         psfs.cnt = 0;
1223
1224         /* Note: If we ever wanted to support non-nice codesets, we
1225          * would really need to do a mb->wc conversion here in the
1226          * vfscanf case.  Related changes would have to be made in
1227          * the code that follows... basicly wherever fmt appears. */
1228         for (fmt = (const Wuchar *) format ; *fmt ; /* ++fmt */) {
1229
1230                 psfs.store = 1;
1231                 psfs.flags = 0;
1232 #ifndef NDEBUG
1233                 psfs.cur_ptr = NULL;    /* Debugging aid. */
1234 #endif /* NDEBUG */
1235
1236
1237                 sc.ungot_flag &= 1;             /* Clear (possible fake) EOF. */
1238                 sc.width = psfs.max_width = INT_MAX;
1239
1240                 /* Note: According to the standards, vfscanf does use isspace
1241                  * here. So, if we did a mb->wc conversion, we would have to do
1242                  * something like
1243                  *      ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
1244                  * because wc might not be in the allowed domain. */
1245                 if (ISSPACE(*fmt)) {
1246                         do {
1247                                 ++fmt;
1248                         } while (ISSPACE(*fmt));
1249                         --fmt;
1250                         psfs.conv_num = CONV_whitespace;
1251                         goto DO_WHITESPACE;
1252                 }
1253
1254                 if (*fmt == '%') {              /* Conversion specification. */
1255                         if (*++fmt == '%') { /* Remember, '%' eats whitespace too. */
1256                                 /* Note: The standard says no conversion occurs.
1257                                  * So do not reset zero_conversions flag. */
1258                                 psfs.conv_num = CONV_percent;
1259                                 goto DO_CONVERSION;
1260                         }
1261
1262
1263 #ifdef L_vfscanf
1264                         psfs.fmt = fmt;
1265 #else  /* L_vfscanf */
1266                         {
1267                                 const __uwchar_t *wf = fmt;
1268                                 psfs.fmt = b = buf;
1269
1270                                 while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
1271                                         *b++ = *wf++;
1272                                 }
1273 #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
1274 #error this is wrong... we need to ched in __psfs_parse_spec instead since this checks last char in buffer and conversion my have stopped before it.
1275                                 if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
1276                                         goto DONE;      /* Spec was excessively long. */
1277                                 }
1278 #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
1279                                 *b = 0;
1280                                 if (b == buf) { /* Bad conversion specifier! */
1281                                         goto DONE;
1282                                 }
1283                         }
1284 #endif /* L_vfscanf */
1285                         if ((i = __psfs_parse_spec(&psfs)) < 0) { /* Bad conversion specifier! */
1286                                 goto DONE;
1287                         }
1288                         fmt += i;
1289
1290                         if (psfs.store) {
1291 #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
1292                                 if (psfs.num_pos_args == -2) {
1293                                         psfs.cur_ptr = va_arg(arg, void *);
1294                                 } else {
1295                                         while (psfs.cur_pos_arg > psfs.num_pos_args) {
1296                                                 psfs.pos_args[++psfs.num_pos_args] = va_arg(arg, void *);
1297                                         }
1298                                         psfs.cur_ptr = psfs.pos_args[psfs.cur_pos_arg];
1299                                 }
1300 #else  /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1301                                 psfs.cur_ptr = va_arg(arg, void *);
1302 #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
1303                         }
1304
1305                 DO_CONVERSION:
1306                         /* First, consume white-space if not n, c, [, C, or l[. */
1307                         if ((((1L << CONV_n)|(1L << CONV_C)|(1L << CONV_c)
1308                                  |(1L << CONV_LEFTBRACKET)|(1L << CONV_leftbracket))
1309                                  & (1L << psfs.conv_num)) == 0
1310                                 ) {
1311                         DO_WHITESPACE:
1312                                 while ((__scan_getc(&sc) >= 0)
1313 #ifdef L_vfscanf
1314                                            && isspace(sc.cc)
1315 #else  /* L_vfscanf */
1316                                            && iswspace(sc.wc)
1317 #endif /* L_vfscanf */
1318                                            ) {}
1319                                 __scan_ungetc(&sc);
1320                                 if (psfs.conv_num == CONV_whitespace) {
1321                                         goto NEXT_FMT;
1322                                 }
1323                         }
1324
1325                         sc.width = psfs.max_width; /* Now limit the max width. */
1326
1327                         if (sc.width == 0) { /* 0 width is forbidden. */
1328                                 goto DONE;
1329                         }
1330
1331
1332                         if (psfs.conv_num == CONV_percent) {
1333                                 goto MATCH_CHAR;
1334                         }
1335
1336                         if (psfs.conv_num == CONV_n) {
1337 #ifdef __UCLIBC_MJN3_ONLY__
1338 #warning CONSIDER: Should %n count as a conversion as far as EOF return value?
1339 #endif
1340 /*                              zero_conversions = 0; */
1341                                 if (psfs.store) {
1342                                         _store_inttype(psfs.cur_ptr, psfs.dataargtype,
1343                                                                    (uintmax_t) sc.nread);
1344                                 }
1345                                 goto NEXT_FMT;
1346                         }
1347
1348                         if (psfs.conv_num <= CONV_A) { /* pointer, integer, or float spec */
1349                                 int r = __psfs_do_numeric(&psfs, &sc);
1350 #ifndef L_vfscanf
1351                                 if (sc.ungot_wflag == 1) {      /* fix up  '?', '.', and ',' hacks */
1352                                         sc.cc = sc.ungot_char = sc.ungot_wchar;
1353                                 }
1354 #endif
1355                                 if (r != -1) {  /* Either success or a matching failure. */
1356                                         zero_conversions = 0;
1357                                 }
1358                                 if (r < 0) {
1359                                         goto DONE;
1360                                 }
1361                                 goto NEXT_FMT;
1362                         }
1363
1364                         /* Do string conversions here since they are not common code. */
1365
1366
1367 #ifdef L_vfscanf
1368
1369                         if
1370 #ifdef __UCLIBC_HAS_WCHAR__
1371                                 (psfs.conv_num >= CONV_LEFTBRACKET)
1372 #else  /* __UCLIBC_HAS_WCHAR__ */
1373                                 (psfs.conv_num >= CONV_c)
1374 #endif /* __UCLIBC_HAS_WCHAR__ */
1375                         {
1376                                 b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
1377                                 fail = 1;
1378                         
1379
1380                                 if (psfs.conv_num == CONV_c) {
1381                                         if (sc.width == INT_MAX) {
1382                                                 sc.width = 1;
1383                                         }
1384
1385                                         while (__scan_getc(&sc) >= 0) {
1386                                                 zero_conversions = 0;
1387                                                 *b = sc.cc;
1388                                                 b += psfs.store;
1389                                         }
1390                                         __scan_ungetc(&sc);
1391                                         if (sc.width > 0) {     /* Failed to read all required. */
1392                                                 goto DONE;
1393                                         }
1394                                         psfs.cnt += psfs.store;
1395                                         goto NEXT_FMT;
1396                                 }
1397
1398                                 if (psfs.conv_num == CONV_s) {
1399                                         /* Yes, believe it or not, a %s conversion can store nuls. */
1400                                         while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
1401                                                 zero_conversions = 0;
1402                                                 *b = sc.cc;
1403                                                 b += psfs.store;
1404                                                 fail = 0;
1405                                         }
1406                                 } else {
1407 #ifdef __UCLIBC_HAS_WCHAR__
1408                                         assert((psfs.conv_num == CONV_LEFTBRACKET) || \
1409                                                    (psfs.conv_num == CONV_leftbracket));
1410 #else /* __UCLIBC_HAS_WCHAR__ */
1411                                         assert((psfs.conv_num == CONV_leftbracket));
1412 #endif /* __UCLIBC_HAS_WCHAR__ */
1413
1414                                         invert = 0;
1415
1416                                         if (*++fmt == '^') {
1417                                                 ++fmt;
1418                                                 invert = 1;
1419                                         }
1420                                         memset(scanset, invert, sizeof(scanset));
1421                                         invert = 1-invert;
1422
1423                                         if (*fmt == ']') {
1424                                                 scanset[(int)(']')] = invert;
1425                                                 ++fmt;
1426                                         }
1427
1428                                         while (*fmt != ']') {
1429                                                 if (!*fmt) { /* No closing ']'. */
1430                                                         goto DONE;
1431                                                 }
1432                                                 if ((*fmt == '-') && (fmt[1] != ']')
1433                                                         && (fmt[-1] < fmt[1]) /* sorted? */
1434                                                         ) {     /* range */
1435                                                         ++fmt;
1436                                                         i = fmt[-2];
1437                                                         /* Note: scanset[i] should already have been done
1438                                                          * in the previous iteration. */
1439                                                         do {
1440                                                                 scanset[++i] = invert;
1441                                                         } while (i < *fmt);
1442                                                         /* Safe to fall through, and a bit smaller. */
1443                                                 }
1444                                                 /* literal char */
1445                                                 scanset[(int) *fmt] = invert;
1446                                                 ++fmt;
1447                                         }
1448
1449 #ifdef __UCLIBC_HAS_WCHAR__
1450                                         if (psfs.conv_num == CONV_LEFTBRACKET) {
1451                                                 goto DO_LEFTBRACKET;
1452                                         }
1453 #endif /* __UCLIBC_HAS_WCHAR__ */
1454
1455
1456                                         while (__scan_getc(&sc) >= 0) {
1457                                                 zero_conversions = 0;
1458                                                 if (!scanset[sc.cc]) {
1459                                                         break;
1460                                                 }
1461                                                 *b = sc.cc;
1462                                                 b += psfs.store;
1463                                                 fail = 0;
1464                                         }
1465                                 }
1466                                 /* Common tail for processing of %s and %[. */
1467
1468                                 __scan_ungetc(&sc);
1469                                 if (fail) {     /* nothing stored! */
1470                                         goto DONE;
1471                                 }
1472                                 *b = 0;         /* Nul-terminate string. */
1473                                 psfs.cnt += psfs.store;
1474                                 goto NEXT_FMT;
1475                         }
1476
1477 #ifdef __UCLIBC_HAS_WCHAR__
1478                 DO_LEFTBRACKET:                 /* Need to do common wide init. */
1479                         if (psfs.conv_num >= CONV_C) {
1480                                 wchar_t wbuf[1];
1481                                 wchar_t *wb;
1482
1483                                 sc.mbstate.__mask = 0;
1484
1485                                 wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
1486                                 fail = 1;
1487                         
1488                                 if (psfs.conv_num == CONV_C) {
1489                                         if (sc.width == INT_MAX) {
1490                                                 sc.width = 1;
1491                                         }
1492
1493                                         while (scan_getwc(&sc) >= 0) {
1494                                                 zero_conversions = 0;
1495                                                 assert(sc.width >= 0);
1496                                                 *wb = sc.wc;
1497                                                 wb += psfs.store;
1498                                         }
1499
1500                                         __scan_ungetc(&sc);
1501                                         if (sc.width > 0) {     /* Failed to read all required. */
1502                                                 goto DONE;
1503                                         }
1504                                         psfs.cnt += psfs.store;
1505                                         goto NEXT_FMT;
1506                                 }
1507
1508
1509                                 if (psfs.conv_num == CONV_S) {
1510                                         /* Yes, believe it or not, a %s conversion can store nuls. */
1511                                         while (scan_getwc(&sc) >= 0) {
1512                                                 zero_conversions = 0;
1513                                                 if ((((__uwchar_t)(sc.wc)) <= UCHAR_MAX) && isspace(sc.wc)) {
1514                                                         break;
1515                                                 }
1516                                                 *wb = sc.wc;
1517                                                 wb += psfs.store;
1518                                                 fail = 0;
1519                                         }
1520                                 } else {
1521                                         assert(psfs.conv_num == CONV_LEFTBRACKET);
1522
1523                                         while (scan_getwc(&sc) >= 0) {
1524                                                 zero_conversions = 0;
1525                                                 if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
1526                                                         if (!scanset[sc.wc]) {
1527                                                                 break;
1528                                                         }
1529                                                 } else if (invert) {
1530                                                         break;
1531                                                 }
1532                                                 *wb = sc.wc;
1533                                                 wb += psfs.store;
1534                                                 fail = 0;
1535                                         }
1536                                 }
1537                                 /* Common tail for processing of %ls and %l[. */
1538
1539                                 __scan_ungetc(&sc);
1540                                 if (fail || sc.mb_fail) { /* Nothing stored or mb error. */
1541                                         goto DONE;
1542                                 }
1543                                 *wb = 0;                /* Nul-terminate string. */
1544                                 psfs.cnt += psfs.store;
1545                                 goto NEXT_FMT;
1546
1547                         }
1548
1549 #endif /* __UCLIBC_HAS_WCHAR__ */
1550 #else  /* L_vfscanf */
1551
1552                         if (psfs.conv_num >= CONV_C) {
1553                                 b = buf;
1554                                 wb = wbuf;
1555                                 if (psfs.conv_num >= CONV_c) {
1556                                         mbstate.__mask = 0;             /* Initialize the mbstate. */
1557                                         if (psfs.store) {
1558                                                 b = (unsigned char *) psfs.cur_ptr;
1559                                         }
1560                                 } else {
1561                                         if (psfs.store) {
1562                                                 wb = (wchar_t *) psfs.cur_ptr;
1563                                         }
1564                                 }
1565                                 fail = 1;
1566
1567
1568                                 if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
1569                                         if (sc.width == INT_MAX) {
1570                                                 sc.width = 1;
1571                                         }
1572
1573                                         while (scan_getwc(&sc) >= 0) {
1574                                                 zero_conversions = 0;
1575                                                 if (psfs.conv_num == CONV_C) {
1576                                                         *wb = sc.wc;
1577                                                         wb += psfs.store;
1578                                                 } else {
1579                                                         i = wcrtomb(b, sc.wc, &mbstate);
1580                                                         if (i < 0) { /* Conversion failure. */
1581                                                                 goto DONE_DO_UNGET;
1582                                                         }
1583                                                         if (psfs.store) {
1584                                                                 b += i;
1585                                                         }
1586                                                 }
1587                                         }
1588                                         __scan_ungetc(&sc);
1589                                         if (sc.width > 0) {     /* Failed to read all required. */
1590                                                 goto DONE;
1591                                         }
1592                                         psfs.cnt += psfs.store;
1593                                         goto NEXT_FMT;
1594                                 }
1595
1596                                 if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
1597                                         /* Yes, believe it or not, a %s conversion can store nuls. */
1598                                         while (scan_getwc(&sc) >= 0) {
1599                                                 zero_conversions = 0;
1600                                                 if  (iswspace(sc.wc)) {
1601                                                         break;
1602                                                 }
1603                                                 if (psfs.conv_num == CONV_S) {
1604                                                         *wb = sc.wc;
1605                                                         wb += psfs.store;
1606                                                 } else {
1607                                                         i = wcrtomb(b, sc.wc, &mbstate);
1608                                                         if (i < 0) { /* Conversion failure. */
1609                                                                 goto DONE_DO_UNGET;
1610                                                         }
1611                                                         if (psfs.store) {
1612                                                                 b += i;
1613                                                         }
1614                                                 }
1615                                                 fail = 0;
1616                                         }
1617                                 } else {
1618                                         const wchar_t *sss;
1619                                         const wchar_t *ssp;
1620                                         unsigned char invert = 0;
1621
1622                                         assert((psfs.conv_num == CONV_LEFTBRACKET)
1623                                                    || (psfs.conv_num == CONV_leftbracket));
1624
1625                                         if (*++fmt == '^') {
1626                                                 ++fmt;
1627                                                 invert = 1;
1628                                         }
1629                                         sss = (const wchar_t *) fmt;
1630                                         if (*fmt == ']') {
1631                                                 ++fmt;
1632                                         }
1633                                         while (*fmt != ']') {
1634                                                 if (!*fmt) { /* No closing ']'. */
1635                                                         goto DONE;
1636                                                 }
1637                                                 if ((*fmt == '-') && (fmt[1] != ']')
1638                                                         && (fmt[-1] < fmt[1]) /* sorted? */
1639                                                         ) {     /* range */
1640                                                         ++fmt;
1641                                                 }
1642                                                 ++fmt;
1643                                         }
1644                                         /* Ok... a valid scanset spec. */
1645
1646                                         while (scan_getwc(&sc) >= 0) {
1647                                                 zero_conversions = 0;
1648                                                 ssp = sss;
1649                                                 do {    /* We know sss < fmt. */
1650                                                         if (*ssp == '-') { /* possible range... */
1651                                                                 /* Note: We accept a-c-e (ordered) as
1652                                                                  * equivalent to a-e. */
1653                                                                 if (ssp > sss) {
1654                                                                         if ((++ssp < (const wchar_t *) fmt)
1655                                                                                 && (ssp[-2] < *ssp)     /* sorted? */
1656                                                                                 ) { /* yes */
1657                                                                                 if ((sc.wc >= ssp[-2])
1658                                                                                         && (sc.wc <= *ssp)) {
1659                                                                                         break;
1660                                                                                 }
1661                                                                                 continue; /* not in range */
1662                                                                         }
1663                                                                         --ssp; /* oops... '-' at end, so back up */
1664                                                                 }
1665                                                                 /* false alarm... a literal '-' */
1666                                                         }
1667                                                         if (sc.wc == *ssp) { /* Matched literal char. */
1668                                                                 break;
1669                                                         }
1670                                                 } while (++ssp < (const wchar_t *) fmt);
1671
1672                                                 if ((ssp == (const wchar_t *) fmt) ^ invert) {
1673                                                         /* no match and not inverting
1674                                                          * or match and inverting */
1675                                                         break;
1676                                                 }
1677                                                 if (psfs.conv_num == CONV_LEFTBRACKET) {
1678                                                         *wb = sc.wc;
1679                                                         wb += psfs.store;
1680                                                 } else {
1681                                                         i = wcrtomb(b, sc.wc, &mbstate);
1682                                                         if (i < 0) { /* Conversion failure. */
1683                                                                 goto DONE_DO_UNGET;
1684                                                         }
1685                                                         if (psfs.store) {
1686                                                                 b += i;
1687                                                         }
1688                                                 }
1689                                                 fail = 0;
1690                                         }
1691                                 }
1692                                 /* Common tail for processing of %s and %[. */
1693
1694                                 __scan_ungetc(&sc);
1695                                 if (fail) {     /* nothing stored! */
1696                                         goto DONE;
1697                                 }
1698                                 *wb = 0;                /* Nul-terminate string. */
1699                                 *b = 0;
1700                                 psfs.cnt += psfs.store;
1701                                 goto NEXT_FMT;
1702                         }
1703
1704 #endif /* L_vfscanf */
1705
1706                         assert(0);
1707                         goto DONE;
1708                 } /* conversion specification */
1709
1710         MATCH_CHAR:
1711                 if (__scan_getc(&sc) != *fmt) {
1712 #ifdef L_vfwscanf
1713                 DONE_DO_UNGET:
1714 #endif /* L_vfwscanf */
1715                         __scan_ungetc(&sc);
1716                         goto DONE;
1717                 }
1718
1719         NEXT_FMT:
1720                 ++fmt;
1721                 if (__FERROR_UNLOCKED(fp)) {
1722                         break;
1723                 }
1724         }
1725
1726  DONE:
1727         if (__FERROR_UNLOCKED(fp) || (*fmt && zero_conversions && __FEOF_UNLOCKED(fp))) {
1728                 psfs.cnt = EOF;                 /* Yes, vfwscanf also returns EOF. */
1729         }
1730
1731         kill_scan_cookie(&sc);
1732
1733         __STDIO_STREAM_VALIDATE(fp);
1734
1735         __STDIO_AUTO_THREADUNLOCK(fp);
1736
1737         return psfs.cnt;
1738 }
1739 #endif
1740 /**********************************************************************/
1741 #ifdef L___psfs_do_numeric
1742
1743 static const unsigned char spec_base[] = SPEC_BASE;
1744 static const unsigned char nil_string[] = "(nil)";
1745
1746 int attribute_hidden __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc)
1747 {
1748         unsigned char *b;
1749         const unsigned char *p;
1750
1751 #ifdef __UCLIBC_HAS_FLOATS__
1752         int exp_adjust = 0;
1753 #endif
1754 #ifdef __UCLIBC_MJN3_ONLY__
1755 #warning TODO: Fix MAX_DIGITS.  We do not do binary, so...!
1756 #warning TODO: Fix buf!
1757 #endif
1758 #define MAX_DIGITS 65                   /* Allow one leading 0. */
1759         unsigned char buf[MAX_DIGITS+2+ 100];
1760         unsigned char usflag, base;
1761         unsigned char nonzero = 0;
1762         unsigned char seendigit = 0;
1763         
1764
1765 #ifdef __UCLIBC_MJN3_ONLY__
1766 #warning CONSIDER: What should be returned for an invalid conversion specifier?
1767 #endif
1768 #ifndef __UCLIBC_HAS_FLOATS__
1769         if (psfs->conv_num > CONV_i) { /* floating point */
1770                 goto DONE;
1771         }
1772 #endif
1773
1774         base = spec_base[psfs->conv_num - CONV_p];
1775         usflag = (psfs->conv_num <= CONV_u); /* (1)0 if (un)signed */
1776         b = buf;
1777
1778
1779         if (psfs->conv_num == CONV_p) { /* Pointer */
1780                 p = nil_string;
1781                 do {
1782                         if ((__scan_getc(sc) < 0) || (*p != sc->cc)) {
1783                                 __scan_ungetc(sc);
1784                                 if (p > nil_string) {
1785                                         /* We matched at least the '(' so even if we
1786                                          * are at eof,  we can not match a pointer. */
1787                                         return -2;      /* Matching failure */
1788                                 }
1789                                 break;
1790                         }
1791                         if (!*++p) {   /* Matched (nil), so no unget necessary. */
1792                                 if (psfs->store) {
1793                                         ++psfs->cnt;
1794                                         _store_inttype(psfs->cur_ptr, psfs->dataargtype,
1795                                                                    (uintmax_t) NULL);
1796                                 }
1797                                 return 0;
1798                         }
1799                 } while (1);
1800
1801 #ifdef __UCLIBC_MJN3_ONLY__
1802 #warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
1803 #endif /*  __UCLIBC_MJN3_ONLY__ */
1804         }
1805
1806         __scan_getc(sc);
1807         if (sc->cc < 0) {
1808                 return -1;                              /* Input failure (nothing read yet). */
1809         }
1810
1811         if ((sc->cc == '+') || (sc->cc == '-')) { /* Handle leading sign.*/
1812                 *b++ = sc->cc;
1813                 __scan_getc(sc);
1814         }
1815
1816         if ((base & 0xef) == 0) { /* 0xef is ~16, so 16 or 0. */
1817                 if (sc->cc == '0') {    /* Possibly set base and handle prefix. */
1818                         __scan_getc(sc);
1819                         if ((sc->cc|0x20) == 'x') { /* Assumes ascii.. x or X. */
1820                                 if (__scan_getc(sc) < 0) {
1821                                         /* Either EOF or error (including wc outside char range).
1822                                          * If EOF or error, this is a matching failure (we read 0x).
1823                                          * If wc outside char range, this is also a matching failure.
1824                                          * Hence, we do an unget (although not really necessary here
1825                                          * and fail. */
1826                                         goto DONE_DO_UNGET;     /* matching failure */
1827                                 }
1828                                 base = 16; /* Base 16 for sure now. */
1829 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
1830                                 /* The prefix is required for hexadecimal floats. */
1831                                 *b++ = '0';
1832                                 *b++ = 'x';
1833 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
1834                         } else { /* oops... back up */
1835                                 __scan_ungetc(sc);
1836                                 sc->cc = '0';   /* NASTY HACK! */
1837
1838                                 base = (base >> 1) + 8; /* 0->8, 16->16.  no 'if' */
1839 #ifdef __UCLIBC_HAS_FLOATS__
1840                                 if (psfs->conv_num > CONV_i) { /* floating point */
1841                                         base = 10;
1842                                 }
1843 #endif
1844                         }
1845                 } else if (!base) {
1846                         base = 10;
1847                 }
1848         }
1849
1850         /***************** digit grouping **********************/
1851 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
1852
1853         if ((psfs->flags & FLAG_THOUSANDS) && (base == 10)
1854                 && *(p = sc->grouping)
1855                 ) {
1856
1857                 int nblk1, nblk2, nbmax, lastblock, pass, i;
1858
1859
1860 #ifdef __UCLIBC_MJN3_ONLY__
1861 #warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
1862 #endif /*  __UCLIBC_MJN3_ONLY__ */
1863                 nbmax = nblk2 = nblk1 = *p;
1864                 if (*++p) {
1865                         nblk2 = *p;
1866                         if (nbmax < nblk2) {
1867                                 nbmax = nblk2;
1868                         }
1869                         assert(!p[1]);
1870                 }
1871
1872                 /* Note: for printf, if 0 and \' flags appear then
1873                  * grouping is done before 0-padding.  Should we
1874                  * strip leading 0's first?  Or add a 0 flag? */
1875
1876                 /* For vfwscanf, sc_getc translates, so the value of sc->cc is
1877                  * either EOF or a char. */
1878
1879                 if (!__isdigit_char_or_EOF(sc->cc)) { /* No starting digit! */
1880 #ifdef __UCLIBC_HAS_FLOATS__
1881                         if (psfs->conv_num > CONV_i) { /* floating point */
1882                                 goto NO_STARTING_DIGIT;
1883                         }
1884 #endif
1885                         goto DONE_DO_UNGET;
1886                 }
1887
1888                 if (sc->cc == '0') {
1889                         seendigit = 1;
1890                         *b++ = '0';                     /* Store the first 0. */
1891 #ifdef __UCLIBC_MJN3_ONLY__
1892 #warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
1893 #endif /*  __UCLIBC_MJN3_ONLY__ */
1894 #if 0
1895                         do {                            /* But ignore all subsequent 0s. */
1896                                 __scan_getc(sc);
1897                         } while (sc->cc == '0');
1898 #endif
1899                 }
1900                 pass = 0;
1901                 lastblock = 0;
1902                 do {
1903                         i = 0;
1904                         while (__isdigit_char_or_EOF(sc->cc)) {
1905                                 seendigit = 1;
1906                                 if (i == nbmax) { /* too many digits for a block */
1907 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
1908                                         if (!pass) { /* treat as nongrouped */
1909                                                 if (nonzero) {
1910                                                         goto DO_NO_GROUP;
1911                                                 }
1912                                                 goto DO_TRIM_LEADING_ZEROS;
1913                                         }
1914 #endif
1915                                         if (nbmax > nblk1) {
1916                                                 goto DONE_DO_UNGET;     /* matching failure */
1917                                         }
1918                                         goto DONE_GROUPING_DO_UNGET; /* nbmax == nblk1 */
1919                                 }
1920                                 ++i;
1921
1922                                 if (nonzero || (sc->cc != '0')) {
1923                                         if (b < buf + MAX_DIGITS) {
1924                                                 *b++ = sc->cc;
1925                                                 nonzero = 1;
1926 #ifdef __UCLIBC_HAS_FLOATS__
1927                                         } else {
1928                                                 ++exp_adjust;
1929 #endif
1930                                         }
1931                                 }
1932
1933                                 __scan_getc(sc);
1934                         }
1935
1936                         if (i) {                        /* we saw digits digits */
1937                                 if ((i == nblk2) || ((i < nblk2) && !pass)) {
1938                                         /* (possible) outer grp */
1939                                         p = sc->thousands_sep;
1940                                         if (*p == sc->cc) {     /* first byte matches... */
1941                                                 /* so check if grouping mb char */
1942                                                 /* Since 1st matched, either match or fail now
1943                                                  * unless EOF (yuk) */
1944                                                 __scan_getc(sc);
1945                                         MBG_LOOP:
1946                                                 if (!*++p) { /* is a grouping mb char */
1947                                                         lastblock = i;
1948                                                         ++pass;
1949                                                         continue;
1950                                                 }
1951                                                 if (*p == sc->cc) {
1952                                                         __scan_getc(sc);
1953                                                         goto MBG_LOOP;
1954                                                 }
1955                                                 /* bad grouping mb char! */
1956                                                 __scan_ungetc(sc);
1957                                                 if ((sc->cc >= 0) || (p > sc->thousands_sep + 1)) {
1958 #ifdef __UCLIBC_HAS_FLOATS__
1959                                                         /* We failed to match a thousep mb char, and
1960                                                          * we've read too much to recover.  But if
1961                                                          * this is a floating point conversion and
1962                                                          * the initial portion of the decpt mb char
1963                                                          * matches, then we may still be able to
1964                                                          * recover. */
1965                                                         int k = p - sc->thousands_sep - 1;
1966
1967                                                         if ((psfs->conv_num > CONV_i) /* float conversion */
1968                                                                 && (!pass || (i == nblk1)) /* possible last */
1969                                                                 && !memcmp(sc->thousands_sep, sc->fake_decpt, k)
1970                                                                 /* and prefix matched, so could be decpt */
1971                                                                 ) {
1972                                                                 __scan_getc(sc);
1973                                                                 p = sc->fake_decpt + k;
1974                                                                 do {
1975                                                                         if (!*++p) {
1976                                                                                 strcpy(b, sc->decpt);
1977                                                                                 b += sc->decpt_len;
1978                                                                                 goto GOT_DECPT;
1979                                                                         }
1980                                                                         if (*p != sc->cc) {
1981                                                                                 __scan_ungetc(sc);
1982                                                                                 break; /* failed */
1983                                                                         }
1984                                                                         __scan_getc(sc);
1985                                                                 } while (1);
1986                                                         }
1987 #endif /* __UCLIBC_HAS_FLOATS__ */
1988                                                         goto DONE;
1989                                                 }
1990                                                 /* was EOF and 1st, so recoverable. */
1991                                         }
1992                                 }
1993                                 if ((i == nblk1) || ((i < nblk1) && !pass)) {
1994                                         /* got an inner group */
1995                                         goto DONE_GROUPING_DO_UNGET;
1996                                 }
1997                                 goto DONE_DO_UNGET;     /* Matching failure. */
1998                         } /* i != 0 */
1999
2000                         assert(pass);
2001
2002                         goto DONE_DO_UNGET;
2003                 } while (1);
2004
2005                 assert(0);                              /* Should never get here. */
2006         }
2007
2008 #endif /***************** digit grouping **********************/
2009
2010         /* Not grouping so first trim all but one leading 0. */
2011 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2012         DO_TRIM_LEADING_ZEROS:
2013 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2014         if (sc->cc == '0') {
2015                 seendigit = 1;
2016                 *b++ = '0';                             /* Store the first 0. */
2017                 do {                                    /* But ignore all subsequent 0s. */
2018                         __scan_getc(sc);
2019                 } while (sc->cc == '0');
2020         }
2021
2022 #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
2023  DO_NO_GROUP:
2024 #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
2025         /* At this point, we're ready to start reading digits. */
2026
2027 #define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
2028
2029         while (valid_digit(sc->cc,base)) { /* Now for significant digits.*/
2030                 if (b - buf < MAX_DIGITS) {
2031                         nonzero = seendigit = 1; /* Set nonzero too 0s trimmed above. */
2032                         *b++ = sc->cc;
2033 #ifdef __UCLIBC_HAS_FLOATS__
2034                 } else {
2035                         ++exp_adjust;
2036 #endif
2037                 }
2038                 __scan_getc(sc);
2039         }
2040
2041 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2042  DONE_GROUPING_DO_UNGET:
2043 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
2044         if (psfs->conv_num <= CONV_i) { /* integer conversion */
2045                 __scan_ungetc(sc);
2046                 *b = 0;                                         /* null-terminate */
2047                 if (!seendigit) {
2048                         goto DONE;                              /* No digits! */
2049                 }
2050                 if (psfs->store) {
2051                         if (*buf == '-') {
2052                                 usflag = 0;
2053                         }
2054                         ++psfs->cnt;
2055                         _store_inttype(psfs->cur_ptr, psfs->dataargtype,
2056                                                    (uintmax_t) STRTOUIM(buf, NULL, base, 1-usflag));
2057                 }
2058                 return 0;
2059         }
2060
2061 #ifdef __UCLIBC_HAS_FLOATS__
2062
2063         /* At this point, we have everything left of the decimal point or exponent. */
2064 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2065  NO_STARTING_DIGIT:
2066 #endif
2067         p = sc->fake_decpt;
2068         do {
2069                 if (!*p) {
2070                         strcpy(b, sc->decpt);
2071                         b += sc->decpt_len;
2072                         break;
2073                 }
2074                 if (*p != sc->cc) {
2075                         if (p > sc->fake_decpt) {
2076                                 goto DONE_DO_UNGET;     /* matching failure (read some of decpt) */
2077                         }
2078                         goto DO_DIGIT_CHECK;
2079                 }
2080                 ++p;
2081                 __scan_getc(sc);
2082         } while (1);
2083
2084 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
2085  GOT_DECPT:
2086 #endif
2087         if (!nonzero) {
2088                 if (sc->cc == '0') {
2089                         assert(exp_adjust == 0);
2090                         *b++ = '0';
2091                         ++exp_adjust;
2092                         seendigit = 1;
2093                         do {
2094                                 --exp_adjust;
2095                                 __scan_getc(sc);
2096                         } while (sc->cc == '0');
2097                 }
2098         }
2099
2100         while (valid_digit(sc->cc,base)) { /* Process fractional digits.*/
2101                 if (b - buf < MAX_DIGITS) {
2102                         seendigit = 1;
2103                         *b++ = sc->cc;
2104                 }
2105                 __scan_getc(sc);
2106         }
2107
2108  DO_DIGIT_CHECK:
2109         /* Hmm... no decimal point.   */
2110         if (!seendigit) {
2111                 static const unsigned char nan_inf_str[] = "an\0nfinity";
2112
2113                 if (base == 16) {               /* We had a prefix, but no digits! */
2114                         goto DONE_DO_UNGET;     /* matching failure */
2115                 }
2116
2117                 /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
2118 #undef TOLOWER
2119 #define TOLOWER(C)     ((C)|0x20)
2120
2121                 switch (TOLOWER(sc->cc)) {
2122                         case 'i':
2123                                 p = nan_inf_str + 3;
2124                                 break;
2125                         case 'n':
2126                                 p = nan_inf_str;
2127                                 break;
2128                         default:
2129                                 /* No digits and not inf or nan. */
2130                                 goto DONE_DO_UNGET;
2131                 }
2132
2133                 *b++ = sc->cc;
2134
2135                 do {
2136                         __scan_getc(sc);
2137                         if (TOLOWER(sc->cc) == *p) {
2138                                 *b++ = sc->cc;
2139                                 ++p;
2140                                 continue;
2141                         }
2142                         if (!*p || (p == nan_inf_str + 5)) { /* match nan/infinity or inf */
2143                                 goto GOT_FLOAT;
2144                         }
2145                         /* Unrecoverable.  Even if on 1st char, we had no digits. */
2146                         goto DONE_DO_UNGET;
2147                 } while (1);
2148         }
2149
2150         /* If we get here, we had some digits. */
2151
2152         if (
2153 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
2154                 ((base == 16) && (((sc->cc)|0x20) == 'p')) ||
2155 #endif
2156                 (((sc->cc)|0x20) == 'e')
2157                 ) {                                             /* Process an exponent. */
2158                 *b++ = sc->cc;
2159
2160                 __scan_getc(sc);
2161                 if (sc->cc < 0) {
2162                         goto DONE_DO_UNGET;     /* matching failure.. no exponent digits */
2163                 }
2164
2165                 if ((sc->cc == '+') || (sc->cc == '-')) { /* Signed exponent? */
2166                         *b++ = sc->cc;
2167                         __scan_getc(sc);
2168                 }
2169
2170 #ifdef __UCLIBC_MJN3_ONLY__
2171 #warning TODO: Fix MAX_EXP_DIGITS!
2172 #endif
2173 #define MAX_EXP_DIGITS 20
2174                 assert(seendigit);
2175                 seendigit = 0;
2176                 nonzero = 0;
2177                 
2178                 if (sc->cc == '0') {
2179                         seendigit = 1;
2180                         *b++ = '0';
2181                         do {
2182                                 __scan_getc(sc);
2183                         } while (sc->cc == '0');
2184                 }
2185
2186                 while (__isdigit_char_or_EOF(sc->cc)) { /* Exponent digits (base 10).*/
2187                         if (seendigit < MAX_EXP_DIGITS) {
2188                                 ++seendigit;
2189                                 *b++ = sc->cc;
2190                         }
2191                         __scan_getc(sc);
2192                 }
2193                         
2194                 if (!seendigit) {               /* No digits.  Unrecoverable. */
2195                         goto DONE_DO_UNGET;
2196                 }
2197         }
2198
2199
2200  GOT_FLOAT:
2201         *b = 0;
2202         {
2203                 __fpmax_t x;
2204                 char *e;
2205                 x = __strtofpmax(buf, &e, exp_adjust);
2206                 assert(!*e);
2207                 if (psfs->store) {
2208                         if (psfs->dataargtype & PA_FLAG_LONG_LONG) {
2209                                 *((long double *)psfs->cur_ptr) = (long double) x;
2210                         } else if (psfs->dataargtype & PA_FLAG_LONG) {
2211                                 *((double *)psfs->cur_ptr) = (double) x;
2212                         } else {
2213                                 *((float *)psfs->cur_ptr) = (float) x;
2214                         }
2215                         ++psfs->cnt;
2216                 }
2217                 __scan_ungetc(sc);
2218                 return 0;
2219         }
2220 #endif /* __UCLIBC_HAS_FLOATS__ */
2221
2222  DONE_DO_UNGET:
2223         __scan_ungetc(sc);
2224  DONE:
2225         return -2;                                      /* Matching failure. */
2226
2227 }
2228 #endif
2229 /**********************************************************************/