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