File: vfscanf.c

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (875 lines) | stat: -rw-r--r-- 23,551 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/* Copyright (c) 2002,2004,2005 Joerg Wunsch
   Copyright (c) 2008  Dmitry Xmelkov
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.

   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

/* $Id: vfscanf.c,v 1.13.4.1 2008/02/23 08:58:35 dmix Exp $ */

#include <avr/pgmspace.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "stdio_private.h"

#if	!defined (SCANF_LEVEL)
# ifndef SCANF_WWIDTH		/* use word for width variable	*/
#  define SCANF_WWIDTH 0
# endif
# ifndef SCANF_BRACKET		/* use '%[' conversion	*/
#  define SCANF_BRACKET	0
# endif
# ifndef SCANF_FLOAT		/* use float point conversion	*/
#  define SCANF_FLOAT	0
# endif
#elif	SCANF_LEVEL == SCANF_MIN
# define SCANF_WWIDTH	0
# define SCANF_BRACKET	0
# define SCANF_FLOAT	0
#elif	SCANF_LEVEL == SCANF_STD
# define SCANF_WWIDTH	0
# define SCANF_BRACKET	1
# define SCANF_FLOAT	0
#elif	SCANF_LEVEL == SCANF_FLT
# define SCANF_WWIDTH	1
# define SCANF_BRACKET	1
# define SCANF_FLOAT	1
#else
# error	 "Not a known scanf level."
#endif

#if	SCANF_WWIDTH
typedef unsigned int width_t;
#else
typedef unsigned char width_t;
#endif

#ifndef	DISABLE_ASM
# if	 defined(__AVR__) && __AVR__
#  define DISABLE_ASM	0
# else
#  define DISABLE_ASM	1
# endif
#endif

#if  SHRT_MAX != INT_MAX
# error  "SHRT_MAX != INT_MAX for target: not supported"
#endif

/* ATTENTION: check FL_CHAR first, not FL_LONG. The last is set
   simultaneously.	*/
#define FL_STAR	    0x01	/* '*': skip assignment		*/
#define FL_WIDTH    0x02	/* width is present		*/
#define FL_LONG	    0x04	/* 'long' type modifier		*/
#define FL_CHAR	    0x08	/* 'char' type modifier		*/
#define FL_OCT	    0x10	/* octal number			*/
#define FL_DEC	    0x20	/* decimal number		*/
#define FL_HEX	    0x40	/* hexidecimal number		*/
#define FL_MINUS    0x80	/* minus flag (field or value)	*/

#ifndef	__AVR_HAVE_LPMX__
# if  defined(__AVR_ENHANCED__) && __AVR_ENHANCED__
#  define __AVR_HAVE_LPMX__	1
# endif
#endif

#ifndef	__AVR_HAVE_MOVW__
# if  defined(__AVR_ENHANCED__) && __AVR_ENHANCED__
#  define __AVR_HAVE_MOVW__	1
# endif
#endif

#if  DISABLE_ASM
# define GETBYTE(flag, mask, pnt)	({	\
    unsigned char __c;				\
    __c = ((flag) & (mask))			\
	  ? pgm_read_byte(pnt) : *pnt;		\
    pnt++;					\
    __c;					\
})
#elif  defined(__AVR_HAVE_LPMX__) && __AVR_HAVE_LPMX__
# define GETBYTE(flag, mask, pnt)	({	\
    unsigned char __c;				\
    asm (					\
	"sbrc	%2,%3	\n\t"			\
	"lpm	%0,Z+	\n\t"			\
	"sbrs	%2,%3	\n\t"			\
	"ld	%0,Z+	"			\
	: "=r" (__c),				\
	  "+z" (pnt)				\
	: "r" (flag),				\
	  "I" (_FFS(mask) - 1)			\
    );						\
    __c;					\
})
#else
# define GETBYTE(flag, mask, pnt)	({	\
    unsigned char __c;				\
    asm (					\
	"sbrc	%2,%3	\n\t"			\
	"lpm		\n\t"			\
	"sbrs	%2,%3	\n\t"			\
	"ld	r0,Z	\n\t"			\
	"adiw	r30,1	\n\t"			\
	"mov	%0,r0	"			\
	: "=r" (__c),				\
	  "+z" (pnt)				\
	: "r" (flag),				\
	  "I" (_FFS(mask) - 1)			\
	: "r0"					\
    );						\
    __c;					\
})
#endif

/* Add noinline attribute to avoid GCC 4.2 optimization.	*/

__attribute__((noinline))
static void putval (void *addr, long val, unsigned char flags)
{
    if (!(flags & FL_STAR)) {
#if  DISABLE_ASM
	if (flags & FL_CHAR)
	    *(char *)addr = val;
	else if (flags & FL_LONG)
	    *(long *)addr = val;
	else
	    *(int *)addr = val;
#else
	asm volatile (
	    "sbrc    %[flags], %[bit_char]	\n\t"
	    "rjmp    1f				\n\t"
	    "sbrs    %[flags], %[bit_long]	\n\t"
	    "rjmp    2f				\n\t"
	    "std     Z+3, %D1			\n\t"
	    "std     Z+2, %C1			\n"
    "2:      std     Z+1, %B1			\n"
    "1:      std     Z+0, %A1"
	    :: "z"(addr), "r"(val), [flags]"r"(flags),
	       [bit_char] "M"(_FFS(FL_CHAR) - 1),
	       [bit_long] "M"(_FFS(FL_LONG) - 1)
	);
#endif
    }
}

__attribute__((noinline))
static unsigned long
mulacc (unsigned long val, unsigned char flags, unsigned char c)
{
    unsigned char cnt;

    if (flags & FL_OCT) {
	cnt = 3;
    } else if (flags & FL_HEX) {
	cnt = 4;
    } else {
#if  DISABLE_ASM
	val += (val << 2);
#else
	asm (
# if  defined(__AVR_HAVE_MOVW__) && __AVR_HAVE_MOVW__
	    "movw    r26, %A0		\n\t"
	    "movw    r30, %C0		\n"
# else
	    "mov     r26, %A0		\n\t"
	    "mov     r27, %B0		\n\t"
	    "mov     r30, %C0		\n\t"
	    "mov     r31, %D0		\n"
# endif
    "1:      lsl     r26		\n\t"
	    "rol     r27		\n\t"
	    "rol     r30		\n\t"
	    "rol     r31		\n\t"
	    "com     __zero_reg__	\n\t"
	    "brne    1b			\n\t"
	    "add     %A0, r26		\n\t"
	    "adc     %B0, r27		\n\t"
	    "adc     %C0, r30		\n\t"
	    "adc     %D0, r31"
	    : "=r"(val)
	    : "0"(val)
	    : "r26","r27","r30","r31"
	);
#endif
	cnt = 1;
    }

    do { val <<= 1; } while (--cnt);
    return val + c;
}

__attribute__((noinline))
static unsigned char
conv_int (FILE *stream, width_t width, void *addr, unsigned char flags)
{
    unsigned long val;
    int i;

    i = getc (stream);			/* after ungetc()	*/

    switch ((unsigned char)i) {
      case '-':
        flags |= FL_MINUS;
	/* FALLTHROUGH */
      case '+':
	if (!--width || (i = getc(stream)) < 0)
	    goto err;
    }

    val = 0;
    flags &= ~FL_WIDTH;

    if (!(flags & (FL_DEC | FL_OCT)) && (unsigned char)i == '0') {
	if (!--width || (i = getc (stream)) < 0)
	    goto putval;
	flags |= FL_WIDTH;
	if ((unsigned char)(i) == 'x' || (unsigned char)(i) == 'X') {
	    flags |= FL_HEX;
	    if (!--width || (i = getc(stream)) < 0)
		goto putval;
	} else {
	    if (!(flags & FL_HEX))
		flags |= FL_OCT;
	}
    }

/* This fact is used below to parse hexidecimal digit.	*/
#if	('A' - '0') != (('a' - '0') & ~('A' ^ 'a'))
# error
#endif
    do {
	unsigned char c = i;
	c -= '0';
	if (c > 7) {
	    if (flags & FL_OCT) goto unget;
	    if (c > 9) {
		if (!(flags & FL_HEX)) goto unget;
		c &= ~('A' ^ 'a');
		c += '0' - 'A';
		if (c > 5) {
		  unget:
		    ungetc (i, stream);
		    break;
		}
		c += 10;
	    }
	}
	val = mulacc (val, flags, c);
	flags |= FL_WIDTH;
	if (!--width) goto putval;
    } while ((i = getc(stream)) >= 0);
    if (!(flags & FL_WIDTH))
	goto err;

  putval:
    if (flags & FL_MINUS) val = -val;
    putval (addr, val, flags);
    return 1;

  err:
    return 0;
}

#if  SCANF_BRACKET
__attribute__((noinline))
static const char *
conv_brk (FILE *stream, width_t width, char *addr, const char *fmt)
{
    unsigned char msk[32];
    unsigned char fnegate;
    unsigned char frange;
    unsigned char cabove;
    int i;
    
    memset (msk, 0, sizeof(msk));
    fnegate = 0;
    frange = 0;
    cabove = 0;			/* init to avoid compiler warning	*/
    
    for (i = 0; ; i++) {
	unsigned char c = GETBYTE(stream->flags, __SPGM, fmt);

	if (c == 0) {
	    return 0;
	} else if (c == '^' && !i) {
	    fnegate = 1;
	    continue;
	} else if (i > fnegate) {
	    if (c == ']') break;
	    if (c == '-' && !frange) {
		frange = 1;
		continue;
	    }
	}
	
	if (!frange) cabove = c;
	
	for (;;) {
	    msk[c >> 3] |= 1 << (c & 7);
	    if (c == cabove) break;
	    if (c < cabove)
		c++;
	    else
		c--;
	}

	frange = 0;
    }
    if (frange)
	msk['-'/8] |= 1 << ('-' & 7);

    if (fnegate) {
	unsigned char *p = msk;
	do {
	    unsigned char c = *p;
	    *p++ = ~c;
	} while (p != msk + sizeof(msk));
    }

    /* And now it is a flag of fault.	*/
    fnegate = 1;

    /* NUL ('\0') is consided as normal character. This is match to Glibc.
       Note, there is no method to include NUL into symbol list.	*/
    do {
	i = getc (stream);
	if (i < 0) break;
	if (!((msk[(unsigned char)i >> 3] >> (i & 7)) & 1)) {
	    ungetc (i, stream);
	    break;
	}
	if (addr) *addr++ = i;
	fnegate = 0;
    } while (--width);
    
    if (fnegate) {
	return 0;
    } else {
	if (addr) *addr = 0;
        return fmt;
    }
}
#endif	/* SCANF_BRACKET */

#if  SCANF_FLOAT

/* GCC before 4.2 does not use a library function to convert an unsigned
   long to float.  Instead it uses a signed long to float conversion
   function along with a large inline code to correct the result.
   Seems, GCC 4.3 does not use it also.	*/
extern double __floatunsisf (unsigned long);

PROGMEM static const float pwr_p10 [6] = {
    1e+1, 1e+2, 1e+4, 1e+8, 1e+16, 1e+32
};
PROGMEM static const float pwr_m10 [6] = {
    1e-1, 1e-2, 1e-4, 1e-8, 1e-16, 1e-32
};

PROGMEM static const char pstr_nfinity[] = "nfinity";
PROGMEM static const char pstr_an[] = "an";

__attribute__((noinline))
static unsigned char conv_flt (FILE *stream, width_t width, float *addr)
{
    union {
	unsigned long u32;
	float flt;
    } x;
    int i;
    const char *p;
    int exp;

    unsigned char flag;
#define FL_MINUS    0x80	/* number is negative	*/
#define FL_ANY	    0x02	/* any digit was readed	*/
#define FL_OVFL	    0x04	/* overflow was		*/
#define FL_DOT	    0x08	/* decimal '.' was	*/
#define FL_MEXP	    0x10	/* exponent 'e' is neg.	*/

    i = getc (stream);		/* after ungetc()	*/

    flag = 0;
    switch ((unsigned char)i) {
      case '-':
        flag = FL_MINUS;
	/* FALLTHROUGH */
      case '+':
	if (!--width || (i = getc (stream)) < 0)
	    goto err;
    }

    switch (tolower (i)) {

      case 'n':
	p = pstr_an;
	goto operate_pstr;

      case 'i':
	p = pstr_nfinity;
      operate_pstr:
        {
	    unsigned char c;
	    
	    while ((c = pgm_read_byte (p++)) != 0) {
		if (!--width
		    || (i = getc (stream)) < 0
		    || ((unsigned char)tolower(i) != c
			&& (ungetc (i, stream), 1)))
		{	
		    if (p == pstr_nfinity + 3)
			break;
		    goto err;
		}
	    }
        }
	x.flt = (p == pstr_an + 3) ? NAN : INFINITY;
	break;

      default:
        exp = 0;
	x.u32 = 0;
	do {

	    unsigned char c = i - '0';
    
	    if (c <= 9) {
		flag |= FL_ANY;
		if (flag & FL_OVFL) {
		    if (!(flag & FL_DOT))
			exp += 1;
		} else {
		    if (flag & FL_DOT)
			exp -= 1;
		    x.u32 = mulacc (x.u32, FL_DEC, c);
		    if (x.u32 >= (ULONG_MAX - 9) / 10)
			flag |= FL_OVFL;
	        }

	    } else if (c == (('.'-'0') & 0xff) && !(flag & FL_DOT)) {
		flag |= FL_DOT;
	    } else {
		break;
	    }
	} while (--width && (i = getc (stream)) >= 0);
    
	if (!(flag & FL_ANY))
	    goto err;
    
	if ((unsigned char)i == 'e' || (unsigned char)i == 'E')
	{
	    int expacc;

	    if (!--width || (i = getc (stream)) < 0) goto err;
	    switch ((unsigned char)i) {
	      case '-':
		flag |= FL_MEXP;
		/* FALLTHROUGH */
	      case '+':
		if (!--width) goto err;
		i = getc (stream);		/* test EOF will below	*/
	    }

	    if (!isdigit (i)) goto err;

	    expacc = 0;
	    do {
		expacc = mulacc (expacc, FL_DEC, i - '0');
	    } while (--width && isdigit (i = getc(stream)));
	    if (flag & FL_MEXP)
		expacc = -expacc;
	    exp += expacc;
	}

	if (width && i >= 0) ungetc (i, stream);
    
	x.flt = __floatunsisf (x.u32);

	if (exp < 0) {
	    p = (void *)(pwr_m10 + 5);
	    exp = -exp;
	} else {
	    p = (void *)(pwr_p10 + 5);
	}
	for (width = 32; width; width >>= 1) {
	    for (; (unsigned)exp >= width; exp -= width) {
		union {
		    long lo;
		    float fl;
		} y;
		y.lo = pgm_read_dword (p);
		x.flt *= y.fl;
	    }
	    p = (void *)p - sizeof(float);
	}
    } /* switch */

    if (flag & FL_MINUS)
	x.flt = -x.flt;
    if (addr) *addr = x.flt;
    return 1;

  err:
    return 0;
}
#endif	/* SCANF_FLOAT	*/

__attribute__((noinline))
static int skip_spaces (FILE *stream)
{
    int i;
    do {
	if ((i = getc (stream)) < 0)
	    return i;
    } while (isspace (i));
    ungetc (i, stream);
    return i;
}

/**
   Formatted input.  This function is the heart of the \b scanf family of
   functions.

   Characters are read from \a stream and processed in a way described by
   \a fmt.  Conversion results will be assigned to the parameters passed
   via \a ap.

   The format string \a fmt is scanned for conversion specifications.
   Anything that doesn't comprise a conversion specification is taken as
   text that is matched literally against the input.  White space in the
   format string will match any white space in the data (including none),
   all other characters match only itself. Processing is aborted as soon
   as the data and format string no longer match, or there is an error or
   end-of-file condition on \a stream.

   Most conversions skip leading white space before starting the actual
   conversion.

   Conversions are introduced with the character \b %.  Possible options
   can follow the \b %:

   - a \c * indicating that the conversion should be performed but
     the conversion result is to be discarded; no parameters will
     be processed from \c ap,
   - the character \c h indicating that the argument is a pointer
     to <tt>short int</tt> (rather than <tt>int</tt>),
   - the 2 characters \c hh indicating that the argument is a pointer
     to <tt>char</tt> (rather than <tt>int</tt>).
   - the character \c l indicating that the argument is a pointer
     to <tt>long int</tt> (rather than <tt>int</tt>, for integer
     type conversions), or a pointer to \c double (for floating
     point conversions),

   In addition, a maximal field width may be specified as a nonzero
   positive decimal integer, which will restrict the conversion to at
   most this many characters from the input stream.  This field width is
   limited to at most 255 characters which is also the default value
   (except for the <tt>%c</tt> conversion that defaults to 1).

   The following conversion flags are supported:

   - \c % Matches a literal \c % character.  This is not a conversion.
   - \c d Matches an optionally signed decimal integer; the next
     pointer must be a pointer to \c int.
   - \c i Matches an optionally signed integer; the next pointer must
     be a pointer to \c int.  The integer is read in base 16 if it
     begins with \b 0x or \b 0X, in base 8 if it begins with \b 0, and
     in base 10 otherwise.  Only characters that correspond to the
     base are used.
   - \c o Matches an octal integer; the next pointer must be a pointer to
     <tt>unsigned int</tt>.
   - \c u Matches an optionally signed decimal integer; the next
     pointer must be a pointer to <tt>unsigned int</tt>.
   - \c x Matches an optionally signed hexadecimal integer; the next
     pointer must be a pointer to <tt>unsigned int</tt>.
   - \c f Matches an optionally signed floating-point number; the next
     pointer must be a pointer to \c float.
   - <tt>e, g, F, E, G</tt> Equivalent to \c f.
   - \c s
     Matches a sequence of non-white-space characters; the next pointer
     must be a pointer to \c char, and the array must be large enough to
     accept all the sequence and the terminating \c NUL character.  The
     input string stops at white space or at the maximum field width,
     whichever occurs first.
   - \c c
     Matches a sequence of width count characters (default 1); the next
     pointer must be a pointer to \c char, and there must be enough room
     for all the characters (no terminating \c NUL is added).  The usual
     skip of leading white space is suppressed.  To skip white space
     first, use an explicit space in the format.
   - \c [
     Matches a nonempty sequence of characters from the specified set
     of accepted characters; the next pointer must be a pointer to \c
     char, and there must be enough room for all the characters in the
     string, plus a terminating \c NUL character.  The usual skip of
     leading white space is suppressed.  The string is to be made up
     of characters in (or not in) a particular set; the set is defined
     by the characters between the open bracket \c [ character and a
     close bracket \c ] character.  The set excludes those characters
     if the first character after the open bracket is a circumflex
     \c ^.  To include a close bracket in the set, make it the first
     character after the open bracket or the circumflex; any other
     position will end the set.  The hyphen character \c - is also
     special; when placed between two other characters, it adds all
     intervening characters to the set.  To include a hyphen, make it
     the last character before the final close bracket.  For instance,
     <tt>[^]0-9-]</tt> means the set of <em>everything except close
     bracket, zero through nine, and hyphen</em>.  The string ends
     with the appearance of a character not in the (or, with a
     circumflex, in) set or when the field width runs out.  Note that
     usage of this conversion enlarges the stack expense.
   - \c p
     Matches a pointer value (as printed by <tt>%p</tt> in printf()); the
     next pointer must be a pointer to \c void.
   - \c n
     Nothing is expected; instead, the number of characters consumed
     thus far from the input is stored through the next pointer, which
     must be a pointer to \c int.  This is not a conversion, although it
     can be suppressed with the \c * flag.

     These functions return the number of input items assigned, which can
     be fewer than provided for, or even zero, in the event of a matching
     failure.  Zero indicates that, while there was input available, no
     conversions were assigned; typically this is due to an invalid input
     character, such as an alphabetic character for a <tt>%d</tt>
     conversion.  The value \c EOF is returned if an input failure occurs
     before any conversion such as an end-of-file occurs.  If an error or
     end-of-file occurs after conversion has begun, the number of
     conversions which were successfully completed is returned.

     By default, all the conversions described above are available except
     the floating-point conversions and the width is limited to 255
     characters.  The float-point conversion will be available in the
     extended version provided by the library \c libscanf_flt.a.  Also in
     this case the width is not limited (exactly, it is limited to 65535
     characters).  To link a program against the extended version, use the
     following compiler flags in the link stage:

     \code
     -Wl,-u,vfscanf -lscanf_flt -lm
     \endcode

     A third version is available for environments that are tight on
     space.  In addition to the restrictions of the standard one, this
     version implements no <tt>%[</tt> specification.  This version is
     provided in the library \c libscanf_min.a, and can be requested using
     the following options in the link stage:

     \code
     -Wl,-u,vfscanf -lscanf_min -lm
     \endcode
*/
int vfscanf (FILE * stream, const char *fmt, va_list ap)
{
    unsigned char nconvs;
    unsigned char stream_flags;
    unsigned char c;
    width_t width;
    void *addr;
    unsigned char flags;
    int i;

    nconvs = 0;
    stream->len = 0;

    /* Initialization of stream_flags at each pass simplifies the register
       allocation with GCC 3.3 - 4.2.  Only the GCC 4.3 is good to move it
       to the begin.	*/
    while ((c = GETBYTE (stream_flags = stream->flags, __SPGM, fmt)) != 0) {

	if (isspace (c)) {
	    skip_spaces (stream);

	} else if (c != '%'
		   || (c = GETBYTE (stream_flags, __SPGM, fmt)) == '%')
	{
	    /* Ordinary character.	*/
	    if ((i = getc (stream)) < 0)
		goto eof;
	    if ((unsigned char)i != c) {
		ungetc (i, stream);
		break;
	    }
	
	} else {
	    flags = 0;

	    if (c == '*') {
		flags = FL_STAR;
		c = GETBYTE (stream_flags, __SPGM, fmt);
	    }

	    width = 0;
	    while ((c -= '0') < 10) {
		flags |= FL_WIDTH;
		width = mulacc (width, FL_DEC, c);
		c = GETBYTE (stream_flags, __SPGM, fmt);
	    }
	    c += '0';
	    if (flags & FL_WIDTH) {
		/* C99 says that width must be greater than zero.
		   To simplify program do treat 0 as error in format.	*/
		if (!width) break;
	    } else {
		width = ~0;
	    }

	    /* ATTENTION: with FL_CHAR the FL_LONG is set also.	*/
	    switch (c) {
	      case 'h':
	        if ((c = GETBYTE (stream_flags, __SPGM, fmt)) != 'h')
		    break;
		flags |= FL_CHAR;
		/* FALLTHROUGH */
	      case 'l':
		flags |= FL_LONG;
		c = GETBYTE (stream_flags, __SPGM, fmt);
	    }

#define CNV_BASE	"cdinopsuxX"
#if	SCANF_BRACKET
# define CNV_BRACKET	"["
#else
# define CNV_BRACKET	""
#endif
#if	SCANF_FLOAT
# define CNV_FLOAT	"efgEFG"
#else
# define CNV_FLOAT	""
#endif
#define CNV_LIST	CNV_BASE CNV_BRACKET CNV_FLOAT
	    if (!c || !strchr_P (PSTR (CNV_LIST), c))
		break;

	    addr = (flags & FL_STAR) ? 0 : va_arg (ap, void *);

	    if (c == 'n') {
		putval (addr, (unsigned)(stream->len), flags);
		continue;
	    }

	    if (c == 'c') {
		if (!(flags & FL_WIDTH)) width = 1;
		do {
		    if ((i = getc (stream)) < 0)
			goto eof;
		    if (addr) *(char *)addr++ = i;
		} while (--width);
		c = 1;			/* no matter with smart GCC	*/

#if  SCANF_BRACKET
	    } else if (c == '[') {
		fmt = conv_brk (stream, width, addr, fmt);
		c = (fmt != 0);
#endif

	    } else {

		if (skip_spaces (stream) < 0)
		    goto eof;
		
		switch (c) {

		  case 's':
		    /* Now we have 1 nospace symbol.	*/
		    do {
			if ((i = getc (stream)) < 0)
			    break;
			if (isspace (i)) {
			    ungetc (i, stream);
			    break;
			}
			if (addr) *(char *)addr++ = i;
		    } while (--width);
		    if (addr) *(char *)addr = 0;
		    c = 1;		/* no matter with smart GCC	*/
		    break;

#if  SCANF_FLOAT
	          case 'p':
		  case 'x':
	          case 'X':
		    flags |= FL_HEX;
		    goto conv_int;

	          case 'd':
		  case 'u':
		    flags |= FL_DEC;
		    goto conv_int;

	          case 'o':
		    flags |= FL_OCT;
		    /* FALLTHROUGH */
		  case 'i':
		  conv_int:
		    c = conv_int (stream, width, addr, flags);
		    break;

	          default:		/* e,E,f,F,g,G	*/
		    c = conv_flt (stream, width, addr);
#else
	          case 'd':
		  case 'u':
		    flags |= FL_DEC;
		    goto conv_int;

	          case 'o':
		    flags |= FL_OCT;
		    /* FALLTHROUGH */
		  case 'i':
		    goto conv_int;

		  default:			/* p,x,X	*/
		    flags |= FL_HEX;
		  conv_int:
		    c = conv_int (stream, width, addr, flags);
#endif
		}
	    } /* else */

	    if (!c) {
		if (stream->flags & (__SERR | __SEOF))
		    goto eof;
		break;
	    }
	    if (!(flags & FL_STAR)) nconvs += 1;
	} /* else */
    } /* while */
    return nconvs;

  eof:
    return nconvs ? nconvs : EOF;
}