接上一篇C語言中可變參數函數實現原理,從理論上詳細介紹了C語言中可變參數函數的實現,這一篇從minix內核源碼中的scanf函數入手,學習C語言經典可變參數函數的實現過程html
在scanf.c文件中,能夠看到scanf函數,代碼以下:c++
#include <stdio.h> #include <stdarg.h> #include "loc_incl.h" int scanf(const char *format, ...) { va_list ap; int retval; va_start(ap, format); retval = _doscan(stdin, format, ap); va_end(ap); return retval; }
對於va_list、va_start、va_end等在stdarg.h頭文件中定義的宏,都已經在(stdarg.h頭文件源代碼分析)一文中介紹過。git
在上述代碼中咱們能夠看到有一個_doscan函數,而這一函數在頭文件loc_incl.h中定義,函數聲明以下:ide
int _doscan(FILE * stream, const char *format, va_list ap);
_doscan函數的實現源代碼以下:函數
1 int 2 _doscan(register FILE *stream, const char *format, va_list ap) 3 { 4 int done = 0; /* number of items done */ 5 int nrchars = 0; /* number of characters read */ 6 int conv = 0; /* # of conversions */ 7 int base; /* conversion base */ 8 unsigned long val; /* an integer value */ 9 register char *str; /* temporary pointer */ 10 char *tmp_string; /* ditto */ 11 unsigned width = 0; /* width of field */ 12 int flags; /* some flags */ 13 int reverse; /* reverse the checking in [...] */ 14 int kind; 15 register int ic = EOF; /* the input character */ 16 #ifndef NOFLOAT 17 long double ld_val; 18 #endif 19 20 if (!*format) return 0; 21 22 while (1) { 23 if (isspace(*format)) { 24 while (isspace(*format)) 25 format++; /* skip whitespace */ 26 ic = getc(stream); 27 nrchars++; 28 while (isspace (ic)) { 29 ic = getc(stream); 30 nrchars++; 31 } 32 if (ic != EOF) ungetc(ic,stream); 33 nrchars--; 34 } 35 if (!*format) break; /* end of format */ 36 37 if (*format != '%') { 38 ic = getc(stream); 39 nrchars++; 40 if (ic != *format++) break; /* error */ 41 continue; 42 } 43 format++; 44 if (*format == '%') { 45 ic = getc(stream); 46 nrchars++; 47 if (ic == '%') { 48 format++; 49 continue; 50 } 51 else break; 52 } 53 flags = 0; 54 if (*format == '*') { 55 format++; 56 flags |= FL_NOASSIGN; 57 } 58 if (isdigit (*format)) { 59 flags |= FL_WIDTHSPEC; 60 for (width = 0; isdigit (*format);) 61 width = width * 10 + *format++ - '0'; 62 } 63 64 switch (*format) { 65 case 'h': flags |= FL_SHORT; format++; break; 66 case 'l': flags |= FL_LONG; format++; break; 67 case 'L': flags |= FL_LONGDOUBLE; format++; break; 68 } 69 kind = *format; 70 if ((kind != 'c') && (kind != '[') && (kind != 'n')) { 71 do { 72 ic = getc(stream); 73 nrchars++; 74 } while (isspace(ic)); 75 if (ic == EOF) break; /* outer while */ 76 } else if (kind != 'n') { /* %c or %[ */ 77 ic = getc(stream); 78 if (ic == EOF) break; /* outer while */ 79 nrchars++; 80 } 81 switch (kind) { 82 default: 83 /* not recognized, like %q */ 84 return conv || (ic != EOF) ? done : EOF; 85 break; 86 case 'n': 87 if (!(flags & FL_NOASSIGN)) { /* silly, though */ 88 if (flags & FL_SHORT) 89 *va_arg(ap, short *) = (short) nrchars; 90 else if (flags & FL_LONG) 91 *va_arg(ap, long *) = (long) nrchars; 92 else 93 *va_arg(ap, int *) = (int) nrchars; 94 } 95 break; 96 case 'p': /* pointer */ 97 set_pointer(flags); 98 /* fallthrough */ 99 case 'b': /* binary */ 100 case 'd': /* decimal */ 101 case 'i': /* general integer */ 102 case 'o': /* octal */ 103 case 'u': /* unsigned */ 104 case 'x': /* hexadecimal */ 105 case 'X': /* ditto */ 106 if (!(flags & FL_WIDTHSPEC) || width > NUMLEN) 107 width = NUMLEN; 108 if (!width) return done; 109 110 str = o_collect(ic, stream, kind, width, &base); 111 if (str < inp_buf 112 || (str == inp_buf 113 && (*str == '-' 114 || *str == '+'))) return done; 115 116 /* 117 * Although the length of the number is str-inp_buf+1 118 * we don't add the 1 since we counted it already 119 */ 120 nrchars += str - inp_buf; 121 122 if (!(flags & FL_NOASSIGN)) { 123 if (kind == 'd' || kind == 'i') 124 val = strtol(inp_buf, &tmp_string, base); 125 else 126 val = strtoul(inp_buf, &tmp_string, base); 127 if (flags & FL_LONG) 128 *va_arg(ap, unsigned long *) = (unsigned long) val; 129 else if (flags & FL_SHORT) 130 *va_arg(ap, unsigned short *) = (unsigned short) val; 131 else 132 *va_arg(ap, unsigned *) = (unsigned) val; 133 } 134 break; 135 case 'c': 136 if (!(flags & FL_WIDTHSPEC)) 137 width = 1; 138 if (!(flags & FL_NOASSIGN)) 139 str = va_arg(ap, char *); 140 if (!width) return done; 141 142 while (width && ic != EOF) { 143 if (!(flags & FL_NOASSIGN)) 144 *str++ = (char) ic; 145 if (--width) { 146 ic = getc(stream); 147 nrchars++; 148 } 149 } 150 151 if (width) { 152 if (ic != EOF) ungetc(ic,stream); 153 nrchars--; 154 } 155 break; 156 case 's': 157 if (!(flags & FL_WIDTHSPEC)) 158 width = 0xffff; 159 if (!(flags & FL_NOASSIGN)) 160 str = va_arg(ap, char *); 161 if (!width) return done; 162 163 while (width && ic != EOF && !isspace(ic)) { 164 if (!(flags & FL_NOASSIGN)) 165 *str++ = (char) ic; 166 if (--width) { 167 ic = getc(stream); 168 nrchars++; 169 } 170 } 171 /* terminate the string */ 172 if (!(flags & FL_NOASSIGN)) 173 *str = '\0'; 174 if (width) { 175 if (ic != EOF) ungetc(ic,stream); 176 nrchars--; 177 } 178 break; 179 case '[': 180 if (!(flags & FL_WIDTHSPEC)) 181 width = 0xffff; 182 if (!width) return done; 183 184 if ( *++format == '^' ) { 185 reverse = 1; 186 format++; 187 } else 188 reverse = 0; 189 190 for (str = Xtable; str < &Xtable[NR_CHARS] 191 ; str++) 192 *str = 0; 193 194 if (*format == ']') Xtable[*format++] = 1; 195 196 while (*format && *format != ']') { 197 Xtable[*format++] = 1; 198 if (*format == '-') { 199 format++; 200 if (*format 201 && *format != ']' 202 && *(format) >= *(format -2)) { 203 int c; 204 205 for( c = *(format -2) + 1 206 ; c <= *format ; c++) 207 Xtable[c] = 1; 208 format++; 209 } 210 else Xtable['-'] = 1; 211 } 212 } 213 if (!*format) return done; 214 215 if (!(Xtable[ic] ^ reverse)) { 216 /* MAT 8/9/96 no match must return character */ 217 ungetc(ic, stream); 218 return done; 219 } 220 221 if (!(flags & FL_NOASSIGN)) 222 str = va_arg(ap, char *); 223 224 do { 225 if (!(flags & FL_NOASSIGN)) 226 *str++ = (char) ic; 227 if (--width) { 228 ic = getc(stream); 229 nrchars++; 230 } 231 } while (width && ic != EOF && (Xtable[ic] ^ reverse)); 232 233 if (width) { 234 if (ic != EOF) ungetc(ic, stream); 235 nrchars--; 236 } 237 if (!(flags & FL_NOASSIGN)) { /* terminate string */ 238 *str = '\0'; 239 } 240 break; 241 #ifndef NOFLOAT 242 case 'e': 243 case 'E': 244 case 'f': 245 case 'g': 246 case 'G': 247 if (!(flags & FL_WIDTHSPEC) || width > NUMLEN) 248 width = NUMLEN; 249 250 if (!width) return done; 251 str = f_collect(ic, stream, width); 252 253 if (str < inp_buf 254 || (str == inp_buf 255 && (*str == '-' 256 || *str == '+'))) return done; 257 258 /* 259 * Although the length of the number is str-inp_buf+1 260 * we don't add the 1 since we counted it already 261 */ 262 nrchars += str - inp_buf; 263 264 if (!(flags & FL_NOASSIGN)) { 265 ld_val = strtod(inp_buf, &tmp_string); 266 if (flags & FL_LONGDOUBLE) 267 *va_arg(ap, long double *) = (long double) ld_val; 268 else 269 if (flags & FL_LONG) 270 *va_arg(ap, double *) = (double) ld_val; 271 else 272 *va_arg(ap, float *) = (float) ld_val; 273 } 274 break; 275 #endif 276 } /* end switch */ 277 conv++; 278 if (!(flags & FL_NOASSIGN) && kind != 'n') done++; 279 format++; 280 } 281 return conv || (ic != EOF) ? done : EOF; 282 }
在上面的源代碼中,值得注意的是第26行的getc宏,定義代碼以下:源碼分析
#define getc(p) (--(p)->_count >= 0 ? (int) (*(p)->_ptr++) : \ __fillbuf(p))
getc的調用形式:ch=getc(fp); 功能是從文件指針指向的文件讀入一個字符,並把它做爲函數值返回給int型變量ch。學習
第4行~第17行,定義一些後面須要用到的變量spa
第23行~第34行,跳過format格式串中的空格,而且跳過輸入流中的空格指針
第37行~第42行,輸入流stream與format格式串中的空白符(空白符能夠是空格(space)、製表符(tab)和新行符(newline))保持一致code
第44行~第52行,在format中的字符爲'%'的前提下,stream中的字符也爲'%',則繼續
第54行~第57行,format當前字符爲'*',表示讀指定類型的數據但不保存
第58行~第62行,指定說明最大域寬。 在百分號(%)與格式碼之間的整數用於限制從對應域讀入的最大字符數於寬度
第64行~第282行,switch語句,用於格式修飾符,這些修飾符包括: h、l、L、c、p、b、d、i、o、u……,還有基於掃描集的'['修飾符
對scanf函數的源碼分析,須要在scanf函數的語法格式詳細的理解基礎上進行,因爲scanf函數實現十分複雜,須要仔細的品味,這裏只是比較初步的分析,具體還有待後期不斷的完善