scanf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #ifdef L_scanf
  8. #ifdef __STDC__
  9. int scanf(const char *fmt, ...)
  10. #else
  11. int scanf(fmt, va_alist)
  12. __const char *fmt;
  13. va_dcl
  14. #endif
  15. {
  16. va_list ptr;
  17. int rv;
  18. va_start(ptr, fmt);
  19. rv = vfscanf(stdin, fmt, ptr);
  20. va_end(ptr);
  21. return rv;
  22. }
  23. #endif
  24. #ifdef L_sscanf
  25. #ifdef __STDC__
  26. int sscanf(const char *sp, const char *fmt, ...)
  27. #else
  28. int sscanf(sp, fmt, va_alist)
  29. __const char *sp;
  30. __const char *fmt;
  31. va_dcl
  32. #endif
  33. {
  34. FILE string[1] = {
  35. {0, (char *) (unsigned) -1, 0, 0, (char *) (unsigned) -1, -1,
  36. _IOFBF | __MODE_READ}
  37. };
  38. va_list ptr;
  39. int rv;
  40. va_start(ptr, fmt);
  41. string->bufpos = (unsigned char *) ((void *) sp);
  42. rv = vfscanf(string, fmt, ptr);
  43. va_end(ptr);
  44. return rv;
  45. }
  46. #endif
  47. #ifdef L_fscanf
  48. #ifdef __STDC__
  49. int fscanf(FILE * fp, const char *fmt, ...)
  50. #else
  51. int fscanf(fp, fmt, va_alist)
  52. FILE *fp;
  53. __const char *fmt;
  54. va_dcl
  55. #endif
  56. {
  57. va_list ptr;
  58. int rv;
  59. va_start(ptr, fmt);
  60. rv = vfscanf(fp, fmt, ptr);
  61. va_end(ptr);
  62. return rv;
  63. }
  64. #endif
  65. #ifdef L_vscanf
  66. int vscanf(fmt, ap)
  67. __const char *fmt;
  68. va_list ap;
  69. {
  70. return vfscanf(stdin, fmt, ap);
  71. }
  72. #endif
  73. #ifdef L_vsscanf
  74. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  75. {
  76. FILE string[1] = {
  77. {0, (char *) (unsigned) -1, 0, 0, (char *) (unsigned) -1, -1,
  78. _IOFBF | __MODE_READ}
  79. };
  80. string->bufpos = (unsigned char *) ((void *) sp);
  81. return vfscanf(string, fmt, ap);
  82. }
  83. #endif
  84. #ifdef L_vfscanf
  85. #if FLOATS
  86. int _vfscanf_fp_ref = 1;
  87. #else
  88. int _vfscanf_fp_ref = 0;
  89. #endif
  90. /* #define skip() do{c=getc(fp); if (c<1) goto done;}while(isspace(c))*/
  91. #define skip() while(isspace(c)) { if ((c=getc(fp))<1) goto done; }
  92. #if FLOATS
  93. /* fp scan actions */
  94. #define F_NADA 0 /* just change state */
  95. #define F_SIGN 1 /* set sign */
  96. #define F_ESIGN 2 /* set exponent's sign */
  97. #define F_INT 3 /* adjust integer part */
  98. #define F_FRAC 4 /* adjust fraction part */
  99. #define F_EXP 5 /* adjust exponent part */
  100. #define F_QUIT 6
  101. #define NSTATE 8
  102. #define FS_INIT 0 /* initial state */
  103. #define FS_SIGNED 1 /* saw sign */
  104. #define FS_DIGS 2 /* saw digits, no . */
  105. #define FS_DOT 3 /* saw ., no digits */
  106. #define FS_DD 4 /* saw digits and . */
  107. #define FS_E 5 /* saw 'e' */
  108. #define FS_ESIGN 6 /* saw exp's sign */
  109. #define FS_EDIGS 7 /* saw exp's digits */
  110. #define FC_DIG 0
  111. #define FC_DOT 1
  112. #define FC_E 2
  113. #define FC_SIGN 3
  114. /* given transition,state do what action? */
  115. int fp_do[][NSTATE] = {
  116. {F_INT, F_INT, F_INT,
  117. F_FRAC, F_FRAC,
  118. F_EXP, F_EXP, F_EXP}, /* see digit */
  119. {F_NADA, F_NADA, F_NADA,
  120. F_QUIT, F_QUIT, F_QUIT, F_QUIT, F_QUIT}, /* see '.' */
  121. {F_QUIT, F_QUIT,
  122. F_NADA, F_QUIT, F_NADA,
  123. F_QUIT, F_QUIT, F_QUIT}, /* see e/E */
  124. {F_SIGN, F_QUIT, F_QUIT, F_QUIT, F_QUIT,
  125. F_ESIGN, F_QUIT, F_QUIT}, /* see sign */
  126. };
  127. /* given transition,state what is new state? */
  128. int fp_ns[][NSTATE] = {
  129. {FS_DIGS, FS_DIGS, FS_DIGS,
  130. FS_DD, FS_DD,
  131. FS_EDIGS, FS_EDIGS, FS_EDIGS}, /* see digit */
  132. {FS_DOT, FS_DOT, FS_DD,
  133. }, /* see '.' */
  134. {0, 0,
  135. FS_E, 0, FS_E,
  136. }, /* see e/E */
  137. {FS_SIGNED, 0, 0, 0, 0,
  138. FS_ESIGN, 0, 0}, /* see sign */
  139. };
  140. /* which states are valid terminators? */
  141. int fp_sval[NSTATE] = {
  142. 0, 0, 1, 0, 1, 0, 0, 1
  143. };
  144. #endif
  145. int vfscanf(fp, fmt, ap)
  146. register FILE *fp;
  147. register const char *fmt;
  148. va_list ap;
  149. {
  150. register long n;
  151. register int c, width, lval, cnt = 0;
  152. int store, neg, base, wide1, endnull, rngflag, c2;
  153. register unsigned char *p;
  154. unsigned char delim[128], digits[17], *q;
  155. #if FLOATS
  156. long frac, expo;
  157. int eneg, fraclen, fstate, trans;
  158. double fx, fp_scan();
  159. #endif
  160. if (!*fmt)
  161. return (0);
  162. c = getc(fp);
  163. while (c > 0) {
  164. store = 0;
  165. if (*fmt == '%') {
  166. n = 0;
  167. width = -1;
  168. wide1 = 1;
  169. base = 10;
  170. lval = (sizeof(long) == sizeof(int));
  171. store = 1;
  172. endnull = 1;
  173. neg = -1;
  174. strcpy(delim, "\011\012\013\014\015 ");
  175. strcpy(digits, "0123456789ABCDEF");
  176. if (fmt[1] == '*') {
  177. endnull = store = 0;
  178. ++fmt;
  179. }
  180. while (isdigit(*++fmt)) { /* width digit(s) */
  181. if (width == -1)
  182. width = 0;
  183. wide1 = width = (width * 10) + (*fmt - '0');
  184. }
  185. --fmt;
  186. fmtnxt:
  187. ++fmt;
  188. switch (tolower(*fmt)) { /* tolower() is a MACRO! */
  189. case '*':
  190. endnull = store = 0;
  191. goto fmtnxt;
  192. case 'l': /* long data */
  193. lval = 1;
  194. goto fmtnxt;
  195. case 'h': /* short data */
  196. lval = 0;
  197. goto fmtnxt;
  198. case 'i': /* any-base numeric */
  199. base = 0;
  200. goto numfmt;
  201. case 'b': /* unsigned binary */
  202. base = 2;
  203. goto numfmt;
  204. case 'o': /* unsigned octal */
  205. base = 8;
  206. goto numfmt;
  207. case 'x': /* unsigned hexadecimal */
  208. base = 16;
  209. goto numfmt;
  210. case 'd': /* SIGNED decimal */
  211. neg = 0;
  212. /* FALL-THRU */
  213. case 'u': /* unsigned decimal */
  214. numfmt:skip();
  215. if (isupper(*fmt))
  216. lval = 1;
  217. if (!base) {
  218. base = 10;
  219. neg = 0;
  220. if (c == '%') {
  221. base = 2;
  222. goto skip1;
  223. } else if (c == '0') {
  224. c = getc(fp);
  225. if (c < 1)
  226. goto savnum;
  227. if ((c != 'x')
  228. && (c != 'X')) {
  229. base = 8;
  230. digits[8] = '\0';
  231. goto zeroin;
  232. }
  233. base = 16;
  234. goto skip1;
  235. }
  236. }
  237. if ((neg == 0) && (base == 10)
  238. && ((neg = (c == '-')) || (c == '+'))) {
  239. skip1:
  240. c = getc(fp);
  241. if (c < 1)
  242. goto done;
  243. }
  244. digits[base] = '\0';
  245. p = ((unsigned char *)
  246. strchr(digits, toupper(c)));
  247. if ((!c || !p) && width)
  248. goto done;
  249. while (p && width-- && c) {
  250. n = (n * base) + (p - digits);
  251. c = getc(fp);
  252. zeroin:
  253. p = ((unsigned char *)
  254. strchr(digits, toupper(c)));
  255. }
  256. savnum:
  257. if (store) {
  258. if (neg == 1)
  259. n = -n;
  260. if (lval)
  261. *va_arg(ap, long *) = n;
  262. else
  263. *va_arg(ap, short *) = n;
  264. ++cnt;
  265. }
  266. break;
  267. #if FLOATS
  268. case 'e': /* float */
  269. case 'f':
  270. case 'g':
  271. skip();
  272. fprintf(stderr, "LIBM:SCANF");
  273. if (isupper(*fmt))
  274. lval = 1;
  275. fstate = FS_INIT;
  276. neg = 0;
  277. eneg = 0;
  278. n = 0;
  279. frac = 0;
  280. expo = 0;
  281. fraclen = 0;
  282. while (c && width--) {
  283. if (c >= '0' && c <= '9')
  284. trans = FC_DIG;
  285. else if (c == '.')
  286. trans = FC_DOT;
  287. else if (c == '+' || c == '-')
  288. trans = FC_SIGN;
  289. else if (tolower(c) == 'e')
  290. trans = FC_E;
  291. else
  292. goto fdone;
  293. switch (fp_do[trans][fstate]) {
  294. case F_SIGN:
  295. neg = (c == '-');
  296. break;
  297. case F_ESIGN:
  298. eneg = (c == '-');
  299. break;
  300. case F_INT:
  301. n = 10 * n + (c - '0');
  302. break;
  303. case F_FRAC:
  304. frac = 10 * frac + (c - '0');
  305. fraclen++;
  306. break;
  307. case F_EXP:
  308. expo = 10 * expo + (c - '0');
  309. break;
  310. case F_QUIT:
  311. goto fdone;
  312. }
  313. fstate = fp_ns[trans][fstate];
  314. c = getc(fp);
  315. }
  316. fdone:
  317. if (!fp_sval[fstate])
  318. goto done;
  319. if (store) {
  320. fx = fp_scan(neg, eneg, n, frac, expo, fraclen);
  321. if (lval)
  322. *va_arg(ap, double *) = fx;
  323. else
  324. *va_arg(ap, float *) = fx;
  325. ++cnt;
  326. }
  327. break;
  328. #else
  329. case 'e': /* float */
  330. case 'f':
  331. case 'g':
  332. fprintf(stderr, "LIBC:SCANF");
  333. exit(-1);
  334. #endif
  335. case 'c': /* character data */
  336. width = wide1;
  337. lval = endnull = 0;
  338. delim[0] = '\0';
  339. goto strproc;
  340. case '[': /* string w/ delimiter set */
  341. /* get delimiters */
  342. p = delim;
  343. if (*++fmt == '^') {
  344. fmt++;
  345. lval = 0;
  346. } else
  347. lval = 1;
  348. rngflag = 2;
  349. if ((*fmt == ']') || (*fmt == '-')) {
  350. *p++ = *fmt++;
  351. rngflag = 0;
  352. }
  353. while (*fmt != ']') {
  354. if (*fmt == '\0')
  355. goto done;
  356. switch (rngflag) {
  357. case 1:
  358. c2 = *(p - 2);
  359. if (c2 <= *fmt) {
  360. p -= 2;
  361. while (c2 < *fmt)
  362. *p++ = c2++;
  363. rngflag = 2;
  364. break;
  365. }
  366. /* fall thru intentional */
  367. case 0:
  368. rngflag = (*fmt == '-');
  369. break;
  370. case 2:
  371. rngflag = 0;
  372. }
  373. *p++ = *fmt++;
  374. }
  375. *p = '\0';
  376. goto strproc;
  377. case 's': /* string data */
  378. lval = 0;
  379. skip();
  380. strproc:
  381. /* process string */
  382. p = va_arg(ap, unsigned char *);
  383. /* if the 1st char fails, match fails */
  384. if (width) {
  385. q = ((unsigned char *)
  386. strchr(delim, c));
  387. if ((c < 1) || lval == (q == 0)) {
  388. if (endnull)
  389. *p = '\0';
  390. goto done;
  391. }
  392. }
  393. for (;;) { /* FOREVER */
  394. if (store)
  395. *p++ = c;
  396. if (((c = getc(fp)) < 1) || (--width == 0))
  397. break;
  398. q = ((unsigned char *)
  399. strchr(delim, c));
  400. if (lval == (q == 0))
  401. break;
  402. }
  403. if (store) {
  404. if (endnull)
  405. *p = '\0';
  406. ++cnt;
  407. }
  408. break;
  409. case '\0': /* early EOS */
  410. --fmt;
  411. /* FALL THRU */
  412. default:
  413. goto cmatch;
  414. }
  415. } else if (isspace(*fmt)) { /* skip whitespace */
  416. skip();
  417. } else { /* normal match char */
  418. cmatch:
  419. if (c != *fmt)
  420. break;
  421. c = getc(fp);
  422. }
  423. if (!*++fmt)
  424. break;
  425. }
  426. done: /* end of scan */
  427. if ((c == EOF) && (cnt == 0))
  428. return (EOF);
  429. if (c != EOF)
  430. ungetc(c, fp);
  431. return (cnt);
  432. }
  433. #endif