OSDN Git Service

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