2 * pANS stdio -- vfscanf
6 static int icvt_f(FILE *f, va_list *args, int store, int width, int type);
7 static int icvt_x(FILE *f, va_list *args, int store, int width, int type);
8 static int icvt_sq(FILE *f, va_list *args, int store, int width, int type);
9 static int icvt_c(FILE *f, va_list *args, int store, int width, int type);
10 static int icvt_d(FILE *f, va_list *args, int store, int width, int type);
11 static int icvt_i(FILE *f, va_list *args, int store, int width, int type);
12 static int icvt_n(FILE *f, va_list *args, int store, int width, int type);
13 static int icvt_o(FILE *f, va_list *args, int store, int width, int type);
14 static int icvt_p(FILE *f, va_list *args, int store, int width, int type);
15 static int icvt_s(FILE *f, va_list *args, int store, int width, int type);
16 static int icvt_u(FILE *f, va_list *args, int store, int width, int type);
17 static int (*icvt[])(FILE *, va_list *, int, int, int)={
18 0, 0, 0, 0, 0, 0, 0, 0, /* ^@ ^A ^B ^C ^D ^E ^F ^G */
19 0, 0, 0, 0, 0, 0, 0, 0, /* ^H ^I ^J ^K ^L ^M ^N ^O */
20 0, 0, 0, 0, 0, 0, 0, 0, /* ^P ^Q ^R ^S ^T ^U ^V ^W */
21 0, 0, 0, 0, 0, 0, 0, 0, /* ^X ^Y ^Z ^[ ^\ ^] ^^ ^_ */
22 0, 0, 0, 0, 0, 0, 0, 0, /* sp ! " # $ % & ' */
23 0, 0, 0, 0, 0, 0, 0, 0, /* ( ) * + , - . / */
24 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */
25 0, 0, 0, 0, 0, 0, 0, 0, /* 8 9 : ; < = > ? */
26 0, 0, 0, 0, 0, icvt_f, 0, icvt_f, /* @ A B C D E F G */
27 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */
28 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */
29 icvt_x, 0, 0, icvt_sq,0, 0, 0, 0, /* X Y Z [ \ ] ^ _ */
30 0, 0, 0, icvt_c, icvt_d, icvt_f, icvt_f, icvt_f, /* ` a b c d e f g */
31 0, icvt_i, 0, 0, 0, 0, icvt_n, icvt_o, /* h i j k l m n o */
32 icvt_p, 0, 0, icvt_s, 0, icvt_u, 0, 0, /* p q r s t u v w */
33 icvt_x, 0, 0, 0, 0, 0, 0, 0, /* x y z { | } ~ ^? */
35 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, 0, 0, 0, 0,
39 0, 0, 0, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0,
53 #define ngetc(f) (nread++, getc(f))
54 #define nungetc(c, f) (--nread, ungetc((c), f))
55 #define wgetc(c, f, out) if(width--==0) goto out; (c)=ngetc(f)
56 #define wungetc(c, f) (++width, nungetc(c, f))
57 static int nread, ncvt;
58 static const char *fmtp;
60 int vfscanf(FILE *f, const char *s, va_list args){
61 int c, width, type, store;
65 for(;*fmtp;fmtp++) switch(*fmtp){
71 if(c==EOF) return ncvt?ncvt:EOF;
77 if(c==EOF) return ncvt?ncvt:EOF;
85 if(*fmtp!='*') store=1;
90 if('0'<=*fmtp && *fmtp<='9'){
92 while('0'<=*fmtp && *fmtp<='9') width=width*10 + *fmtp++ - '0';
96 type=*fmtp=='h' || *fmtp=='l' || *fmtp=='L'?*fmtp++:'n';
97 if(!icvt[*fmtp]) goto NonSpecial;
98 if(!(*icvt[*fmtp])(f, &args, store, width, type))
100 if(*fmtp=='\0') break;
105 static int icvt_n(FILE *f, va_list *args, int store, int width, int type){
109 --ncvt; /* this assignment doesn't count! */
111 case 'h': *va_arg(*args, short *)=nread; break;
112 case 'n': *va_arg(*args, int *)=nread; break;
114 case 'L': *va_arg(*args, long *)=nread; break;
123 * Generic fixed-point conversion
124 * f is the input FILE *;
125 * args is the va_list * into which to store the number;
126 * store is a flag to enable storing;
127 * width is the maximum field width;
128 * type is 'h' 'l' or 'L', the scanf type modifier;
129 * unsgned is SIGNED, UNSIGNED or POINTER, giving part of the type to store in;
130 * base is the number base -- if 0, C number syntax is used.
132 static int icvt_fixed(FILE *f, va_list *args,
133 int store, int width, int type, int unsgned, int base){
134 unsigned long int num=0;
135 int sign=1, ndig=0, dig;
155 if(c=='x' || c=='X'){
170 if(c=='x' || c=='X'){
177 while('0'<=c && c<='9' || 'a'<=c && c<='f' || 'A'<=c && c<='F'){
178 dig='0'<=c && c<='9'?c-'0':'a'<=c && c<='f'?c-'a'+10:c-'A'+10;
186 if(ndig==0) return 0;
191 case 'h': *va_arg(*args, short *)=num*sign; break;
192 case 'n': *va_arg(*args, int *)=num*sign; break;
194 case 'L': *va_arg(*args, long *)=num*sign; break;
199 case 'h': *va_arg(*args, unsigned short *)=num*sign; break;
200 case 'n': *va_arg(*args, unsigned int *)=num*sign; break;
202 case 'L': *va_arg(*args, unsigned long *)=num*sign; break;
206 *va_arg(*args, void **)=(void *)(num*sign); break;
211 static int icvt_d(FILE *f, va_list *args, int store, int width, int type){
212 return icvt_fixed(f, args, store, width, type, SIGNED, 10);
214 static int icvt_x(FILE *f, va_list *args, int store, int width, int type){
215 return icvt_fixed(f, args, store, width, type, UNSIGNED, 16);
217 static int icvt_o(FILE *f, va_list *args, int store, int width, int type){
218 return icvt_fixed(f, args, store, width, type, UNSIGNED, 8);
220 static int icvt_i(FILE *f, va_list *args, int store, int width, int type){
221 return icvt_fixed(f, args, store, width, type, SIGNED, 0);
223 static int icvt_u(FILE *f, va_list *args, int store, int width, int type){
224 return icvt_fixed(f, args, store, width, type, UNSIGNED, 10);
226 static int icvt_p(FILE *f, va_list *args, int store, int width, int type){
227 return icvt_fixed(f, args, store, width, type, POINTER, 16);
230 static int icvt_f(FILE *f, va_list *args, int store, int width, int type){
233 int c, ndig=0, ndpt=0, nexp=1;
234 if(width<0 || NBUF<width) width=NBUF; /* bug -- no limit specified in ansi */
242 if(c=='+' || c=='-'){
246 while('0'<=c && c<='9' || ndpt==0 && c=='.'){
252 if(c=='e' || c=='E'){
256 if(c=='+' || c=='-'){
260 while('0'<=c && c<='9'){
268 if(ndig==0 || nexp==0) return 0;
270 if(store) switch(type){
272 case 'n': *va_arg(*args, float *)=atof(buf); break;
273 case 'L': /* bug -- should store in a long double */
274 case 'l': *va_arg(*args, double *)=atof(buf); break;
278 static int icvt_s(FILE *f, va_list *args, int store, int width, int type){
282 if(store) s=va_arg(*args, char *);
306 static int icvt_c(FILE *f, va_list *args, int store, int width, int type){
310 if(store) s=va_arg(*args, char *);
320 static int match(int c, const char *pat){
327 if(pat+2<fmtp && pat[1]=='-'){
328 if(pat[0]<=c && c<=pat[2]
329 || pat[2]<=c && c<=pat[0])
333 else if(c==*pat) return ok;
338 static int icvt_sq(FILE *f, va_list *args, int store, int width, int type){
342 register const char *pat;
344 if(*fmtp=='^') fmtp++;
345 if(*fmtp!='\0') fmtp++;
346 while(*fmtp!='\0' && *fmtp!=']') fmtp++;
347 if(store) s=va_arg(*args, char *);
356 if(!match(c, pat)) break;