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){
76 if(c==EOF) return ncvt?ncvt:EOF;
84 if(*fmtp!='*') store=1;
89 if('0'<=*fmtp && *fmtp<='9'){
91 while('0'<=*fmtp && *fmtp<='9') width=width*10 + *fmtp++ - '0';
95 type=*fmtp=='h' || *fmtp=='l' || *fmtp=='L'?*fmtp++:'n';
96 if(!icvt[*fmtp]) goto NonSpecial;
97 if(!(*icvt[*fmtp])(f, &args, store, width, type))
99 if(*fmtp=='\0') break;
104 static int icvt_n(FILE *f, va_list *args, int store, int width, int type){
106 --ncvt; /* this assignment doesn't count! */
108 case 'h': *va_arg(*args, short *)=nread; break;
109 case 'n': *va_arg(*args, int *)=nread; break;
111 case 'L': *va_arg(*args, long *)=nread; break;
120 * Generic fixed-point conversion
121 * f is the input FILE *;
122 * args is the va_list * into which to store the number;
123 * store is a flag to enable storing;
124 * width is the maximum field width;
125 * type is 'h' 'l' or 'L', the scanf type modifier;
126 * unsgned is SIGNED, UNSIGNED or POINTER, giving part of the type to store in;
127 * base is the number base -- if 0, C number syntax is used.
129 static int icvt_fixed(FILE *f, va_list *args,
130 int store, int width, int type, int unsgned, int base){
131 unsigned long int num=0;
132 int sign=1, ndig=0, dig;
152 if(c=='x' || c=='X'){
167 if(c=='x' || c=='X'){
174 while('0'<=c && c<='9' || 'a'<=c && c<='f' || 'A'<=c && c<='F'){
175 dig='0'<=c && c<='9'?c-'0':'a'<=c && c<='f'?c-'a'+10:c-'A'+10;
183 if(ndig==0) return 0;
188 case 'h': *va_arg(*args, short *)=num*sign; break;
189 case 'n': *va_arg(*args, int *)=num*sign; break;
191 case 'L': *va_arg(*args, long *)=num*sign; break;
196 case 'h': *va_arg(*args, unsigned short *)=num*sign; break;
197 case 'n': *va_arg(*args, unsigned int *)=num*sign; break;
199 case 'L': *va_arg(*args, unsigned long *)=num*sign; break;
203 *va_arg(*args, void **)=(void *)(num*sign); break;
208 static int icvt_d(FILE *f, va_list *args, int store, int width, int type){
209 return icvt_fixed(f, args, store, width, type, SIGNED, 10);
211 static int icvt_x(FILE *f, va_list *args, int store, int width, int type){
212 return icvt_fixed(f, args, store, width, type, UNSIGNED, 16);
214 static int icvt_o(FILE *f, va_list *args, int store, int width, int type){
215 return icvt_fixed(f, args, store, width, type, UNSIGNED, 8);
217 static int icvt_i(FILE *f, va_list *args, int store, int width, int type){
218 return icvt_fixed(f, args, store, width, type, SIGNED, 0);
220 static int icvt_u(FILE *f, va_list *args, int store, int width, int type){
221 return icvt_fixed(f, args, store, width, type, UNSIGNED, 10);
223 static int icvt_p(FILE *f, va_list *args, int store, int width, int type){
224 return icvt_fixed(f, args, store, width, type, POINTER, 16);
227 static int icvt_f(FILE *f, va_list *args, int store, int width, int type){
230 int c, ndig=0, ndpt=0, nexp=1;
231 if(width<0 || NBUF<width) width=NBUF; /* bug -- no limit specified in ansi */
239 if(c=='+' || c=='-'){
243 while('0'<=c && c<='9' || ndpt==0 && c=='.'){
249 if(c=='e' || c=='E'){
253 if(c=='+' || c=='-'){
257 while('0'<=c && c<='9'){
265 if(ndig==0 || nexp==0) return 0;
267 if(store) switch(type){
269 case 'n': *va_arg(*args, float *)=atof(buf); break;
270 case 'L': /* bug -- should store in a long double */
271 case 'l': *va_arg(*args, double *)=atof(buf); break;
275 static int icvt_s(FILE *f, va_list *args, int store, int width, int type){
278 if(store) s=va_arg(*args, char *);
302 static int icvt_c(FILE *f, va_list *args, int store, int width, int type){
305 if(store) s=va_arg(*args, char *);
315 static int match(int c, const char *pat){
322 if(pat+2<fmtp && pat[1]=='-'){
323 if(pat[0]<=c && c<=pat[2]
324 || pat[2]<=c && c<=pat[0])
328 else if(c==*pat) return ok;
333 static int icvt_sq(FILE *f, va_list *args, int store, int width, int type){
336 register const char *pat;
339 if(*fmtp=='^') fmtp++;
340 if(*fmtp!='\0') fmtp++;
341 while(*fmtp!='\0' && *fmtp!=']') fmtp++;
342 if(store) s=va_arg(*args, char *);