scanf.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 == '*') {
  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. ++fmt;
  185. }
  186. --fmt;
  187. fmtnxt:
  188. ++fmt;
  189. switch (tolower(*fmt)) { /* tolower() is a MACRO! */
  190. case '*':
  191. endnull = store = 0;
  192. goto fmtnxt;
  193. case 'l': /* long data */
  194. lval = 1;
  195. goto fmtnxt;
  196. case 'h': /* short data */
  197. lval = 0;
  198. goto fmtnxt;
  199. case 'i': /* any-base numeric */
  200. base = 0;
  201. goto numfmt;
  202. case 'b': /* unsigned binary */
  203. base = 2;
  204. goto numfmt;
  205. case 'o': /* unsigned octal */
  206. base = 8;
  207. goto numfmt;
  208. case 'x': /* unsigned hexadecimal */
  209. base = 16;
  210. goto numfmt;
  211. case 'd': /* SIGNED decimal */
  212. neg = 0;
  213. /* FALL-THRU */
  214. case 'u': /* unsigned decimal */
  215. numfmt:skip();
  216. if (isupper(*fmt))
  217. lval = 1;
  218. if (!base) {
  219. base = 10;
  220. neg = 0;
  221. if (c == '%') {
  222. base = 2;
  223. goto skip1;
  224. } else if (c == '0') {
  225. c = getc(fp);
  226. if (c < 1)
  227. goto savnum;
  228. if ((c != 'x')
  229. && (c != 'X')) {
  230. base = 8;
  231. digits[8] = '\0';
  232. goto zeroin;
  233. }
  234. base = 16;
  235. goto skip1;
  236. }
  237. }
  238. if ((neg == 0) && (base == 10)
  239. && ((neg = (c == '-')) || (c == '+'))) {
  240. skip1:
  241. c = getc(fp);
  242. if (c < 1)
  243. goto done;
  244. }
  245. digits[base] = '\0';
  246. p = ((unsigned char *)
  247. strchr(digits, toupper(c)));
  248. if ((!c || !p) && width)
  249. goto done;
  250. while (p && width-- && c) {
  251. n = (n * base) + (p - digits);
  252. c = getc(fp);
  253. zeroin:
  254. p = ((unsigned char *)
  255. strchr(digits, toupper(c)));
  256. }
  257. savnum:
  258. if (store) {
  259. if (neg == 1)
  260. n = -n;
  261. if (lval)
  262. *va_arg(ap, long *) = n;
  263. else
  264. *va_arg(ap, short *) = n;
  265. ++cnt;
  266. }
  267. break;
  268. #if FLOATS
  269. case 'e': /* float */
  270. case 'f':
  271. case 'g':
  272. skip();
  273. fprintf(stderr, "LIBM:SCANF");
  274. if (isupper(*fmt))
  275. lval = 1;
  276. fstate = FS_INIT;
  277. neg = 0;
  278. eneg = 0;
  279. n = 0;
  280. frac = 0;
  281. expo = 0;
  282. fraclen = 0;
  283. while (c && width--) {
  284. if (c >= '0' && c <= '9')
  285. trans = FC_DIG;
  286. else if (c == '.')
  287. trans = FC_DOT;
  288. else if (c == '+' || c == '-')
  289. trans = FC_SIGN;
  290. else if (tolower(c) == 'e')
  291. trans = FC_E;
  292. else
  293. goto fdone;
  294. switch (fp_do[trans][fstate]) {
  295. case F_SIGN:
  296. neg = (c == '-');
  297. break;
  298. case F_ESIGN:
  299. eneg = (c == '-');
  300. break;
  301. case F_INT:
  302. n = 10 * n + (c - '0');
  303. break;
  304. case F_FRAC:
  305. frac = 10 * frac + (c - '0');
  306. fraclen++;
  307. break;
  308. case F_EXP:
  309. expo = 10 * expo + (c - '0');
  310. break;
  311. case F_QUIT:
  312. goto fdone;
  313. }
  314. fstate = fp_ns[trans][fstate];
  315. c = getc(fp);
  316. }
  317. fdone:
  318. if (!fp_sval[fstate])
  319. goto done;
  320. if (store) {
  321. fx = fp_scan(neg, eneg, n, frac, expo, fraclen);
  322. if (lval)
  323. *va_arg(ap, double *) = fx;
  324. else
  325. *va_arg(ap, float *) = fx;
  326. ++cnt;
  327. }
  328. break;
  329. #else
  330. case 'e': /* float */
  331. case 'f':
  332. case 'g':
  333. fprintf(stderr, "LIBC:SCANF");
  334. exit(-1);
  335. #endif
  336. case 'c': /* character data */
  337. width = wide1;
  338. lval = endnull = 0;
  339. delim[0] = '\0';
  340. goto strproc;
  341. case '[': /* string w/ delimiter set */
  342. /* get delimiters */
  343. p = delim;
  344. if (*++fmt == '^') {
  345. fmt++;
  346. lval = 0;
  347. } else
  348. lval = 1;
  349. rngflag = 2;
  350. if ((*fmt == ']') || (*fmt == '-')) {
  351. *p++ = *fmt++;
  352. rngflag = 0;
  353. }
  354. while (*fmt != ']') {
  355. if (*fmt == '\0')
  356. goto done;
  357. switch (rngflag) {
  358. case 1:
  359. c2 = *(p - 2);
  360. if (c2 <= *fmt) {
  361. p -= 2;
  362. while (c2 < *fmt)
  363. *p++ = c2++;
  364. rngflag = 2;
  365. break;
  366. }
  367. /* fall thru intentional */
  368. case 0:
  369. rngflag = (*fmt == '-');
  370. break;
  371. case 2:
  372. rngflag = 0;
  373. }
  374. *p++ = *fmt++;
  375. }
  376. *p = '\0';
  377. goto strproc;
  378. case 's': /* string data */
  379. lval = 0;
  380. skip();
  381. strproc:
  382. /* process string */
  383. p = va_arg(ap, unsigned char *);
  384. /* if the 1st char fails, match fails */
  385. if (width) {
  386. q = ((unsigned char *)
  387. strchr(delim, c));
  388. if ((c < 1) || lval == (q == 0)) {
  389. if (endnull)
  390. *p = '\0';
  391. goto done;
  392. }
  393. }
  394. for (;;) { /* FOREVER */
  395. if (store)
  396. *p++ = c;
  397. if (((c = getc(fp)) < 1) || (--width == 0))
  398. break;
  399. q = ((unsigned char *)
  400. strchr(delim, c));
  401. if (lval == (q == 0))
  402. break;
  403. }
  404. if (store) {
  405. if (endnull)
  406. *p = '\0';
  407. ++cnt;
  408. }
  409. break;
  410. case '\0': /* early EOS */
  411. --fmt;
  412. /* FALL THRU */
  413. default:
  414. goto cmatch;
  415. }
  416. } else if (isspace(*fmt)) { /* skip whitespace */
  417. skip();
  418. } else { /* normal match char */
  419. cmatch:
  420. if (c != *fmt)
  421. break;
  422. c = getc(fp);
  423. }
  424. if (!*++fmt)
  425. break;
  426. }
  427. done: /* end of scan */
  428. if ((c == EOF) && (cnt == 0))
  429. return (EOF);
  430. if (c != EOF)
  431. ungetc(c, fp);
  432. return (cnt);
  433. }
  434. #endif