scanf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*-
  2. * Copyright (c) 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Chris Torek.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 4. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp
  33. * From: static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
  34. * From: static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
  35. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <stdarg.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. #pragma warning(disable : 4244) // conversion from '*' to '*', possible loss of data
  45. #pragma warning(disable : 4018) // signed/unsigned mismatch
  46. #pragma warning(disable : 4267) // '=' : conversion from 'size_t' to 'int', possible loss of data
  47. #define strtoq _strtoi64
  48. #define strtouq _strtoui64
  49. #define bcopy(b1,b2,len) (memmove((b2), (b1), (len)), (void) 0)
  50. typedef int long long quad_t;
  51. typedef unsigned long long u_quad_t;
  52. typedef unsigned char u_char;
  53. #define BUF 32 /* Maximum length of numeric string. */
  54. /*
  55. * Flags used during conversion.
  56. */
  57. #define LONG 0x01 /* l: long or double */
  58. #define SHORT 0x04 /* h: short */
  59. #define SUPPRESS 0x08 /* suppress assignment */
  60. #define POINTER 0x10 /* weird %p pointer (`fake hex') */
  61. #define NOSKIP 0x20 /* do not skip blanks */
  62. #define QUAD 0x400
  63. /*
  64. * The following are used in numeric conversions only:
  65. * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
  66. * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
  67. */
  68. #define SIGNOK 0x40 /* +/- is (still) legal */
  69. #define NDIGITS 0x80 /* no digits detected */
  70. #define DPTOK 0x100 /* (float) decimal point is still legal */
  71. #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
  72. #define PFXOK 0x100 /* 0x prefix is (still) legal */
  73. #define NZDIGITS 0x200 /* no zero digits detected */
  74. /*
  75. * Conversion types.
  76. */
  77. #define CT_CHAR 0 /* %c conversion */
  78. #define CT_CCL 1 /* %[...] conversion */
  79. #define CT_STRING 2 /* %s conversion */
  80. #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
  81. typedef u_quad_t (*ccfntype)(const char *, char **, int);
  82. static const u_char *__sccl(char *, const u_char *);
  83. int
  84. vsscanf(const char *inp, char const *fmt0, va_list ap)
  85. {
  86. int inr;
  87. const u_char *fmt = (const u_char *)fmt0;
  88. int c; /* character from format, or conversion */
  89. size_t width; /* field width, or 0 */
  90. char *p; /* points into all kinds of strings */
  91. int n; /* handy integer */
  92. int flags; /* flags as defined above */
  93. char *p0; /* saves original value of p when necessary */
  94. int nassigned; /* number of fields assigned */
  95. int nconversions; /* number of conversions */
  96. int nread; /* number of characters consumed from fp */
  97. int base; /* base argument to strtoq/strtouq */
  98. ccfntype ccfn; /* conversion function (strtoq/strtouq) */
  99. char ccltab[256]; /* character class table for %[...] */
  100. char buf[BUF]; /* buffer for numeric conversions */
  101. /* `basefix' is used to avoid `if' tests in the integer scanner */
  102. static short basefix[17] =
  103. { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  104. inr = strlen(inp);
  105. nassigned = 0;
  106. nconversions = 0;
  107. nread = 0;
  108. base = 0; /* XXX just to keep gcc happy */
  109. ccfn = NULL; /* XXX just to keep gcc happy */
  110. for (;;) {
  111. c = *fmt++;
  112. if (c == 0)
  113. return (nassigned);
  114. if (isspace(c)) {
  115. while (inr > 0 && isspace(*inp))
  116. nread++, inr--, inp++;
  117. continue;
  118. }
  119. if (c != '%')
  120. goto literal;
  121. width = 0;
  122. flags = 0;
  123. /*
  124. * switch on the format. continue if done;
  125. * break once format type is derived.
  126. */
  127. again: c = *fmt++;
  128. switch (c) {
  129. case '%':
  130. literal:
  131. if (inr <= 0)
  132. goto input_failure;
  133. if (*inp != c)
  134. goto match_failure;
  135. inr--, inp++;
  136. nread++;
  137. continue;
  138. case '*':
  139. flags |= SUPPRESS;
  140. goto again;
  141. case 'l':
  142. flags |= LONG;
  143. goto again;
  144. case 'q':
  145. flags |= QUAD;
  146. goto again;
  147. case 'h':
  148. flags |= SHORT;
  149. goto again;
  150. case '0': case '1': case '2': case '3': case '4':
  151. case '5': case '6': case '7': case '8': case '9':
  152. width = width * 10 + c - '0';
  153. goto again;
  154. /*
  155. * Conversions.
  156. *
  157. */
  158. case 'd':
  159. c = CT_INT;
  160. ccfn = (ccfntype)strtoq;
  161. base = 10;
  162. break;
  163. case 'i':
  164. c = CT_INT;
  165. ccfn = (ccfntype)strtoq;
  166. base = 0;
  167. break;
  168. case 'o':
  169. c = CT_INT;
  170. ccfn = strtouq;
  171. base = 8;
  172. break;
  173. case 'u':
  174. c = CT_INT;
  175. ccfn = strtouq;
  176. base = 10;
  177. break;
  178. case 'x':
  179. flags |= PFXOK; /* enable 0x prefixing */
  180. c = CT_INT;
  181. ccfn = strtouq;
  182. base = 16;
  183. break;
  184. case 's':
  185. c = CT_STRING;
  186. break;
  187. case '[':
  188. fmt = __sccl(ccltab, fmt);
  189. flags |= NOSKIP;
  190. c = CT_CCL;
  191. break;
  192. case 'c':
  193. flags |= NOSKIP;
  194. c = CT_CHAR;
  195. break;
  196. case 'p': /* pointer format is like hex */
  197. flags |= POINTER | PFXOK;
  198. c = CT_INT;
  199. ccfn = strtouq;
  200. base = 16;
  201. break;
  202. case 'n':
  203. nconversions++;
  204. if (flags & SUPPRESS) /* ??? */
  205. continue;
  206. if (flags & SHORT)
  207. *va_arg(ap, short *) = nread;
  208. else if (flags & LONG)
  209. *va_arg(ap, long *) = nread;
  210. else if (flags & QUAD)
  211. *va_arg(ap, quad_t *) = nread;
  212. else
  213. *va_arg(ap, int *) = nread;
  214. continue;
  215. }
  216. /*
  217. * We have a conversion that requires input.
  218. */
  219. if (inr <= 0)
  220. goto input_failure;
  221. /*
  222. * Consume leading white space, except for formats
  223. * that suppress this.
  224. */
  225. if ((flags & NOSKIP) == 0) {
  226. while (isspace(*inp)) {
  227. nread++;
  228. if (--inr > 0)
  229. inp++;
  230. else
  231. goto input_failure;
  232. }
  233. /*
  234. * Note that there is at least one character in
  235. * the buffer, so conversions that do not set NOSKIP
  236. * can no longer result in an input failure.
  237. */
  238. }
  239. /*
  240. * Do the conversion.
  241. */
  242. switch (c) {
  243. case CT_CHAR:
  244. /* scan arbitrary characters (sets NOSKIP) */
  245. if (width == 0)
  246. width = 1;
  247. if (flags & SUPPRESS) {
  248. size_t sum = 0;
  249. for (;;) {
  250. if ((n = inr) < width) {
  251. sum += n;
  252. width -= n;
  253. inp += n;
  254. if (sum == 0)
  255. goto input_failure;
  256. break;
  257. } else {
  258. sum += width;
  259. inr -= width;
  260. inp += width;
  261. break;
  262. }
  263. }
  264. nread += sum;
  265. } else {
  266. bcopy(inp, va_arg(ap, char *), width);
  267. inr -= width;
  268. inp += width;
  269. nread += width;
  270. nassigned++;
  271. }
  272. nconversions++;
  273. break;
  274. case CT_CCL:
  275. /* scan a (nonempty) character class (sets NOSKIP) */
  276. if (width == 0)
  277. width = (size_t)~0; /* `infinity' */
  278. /* take only those things in the class */
  279. if (flags & SUPPRESS) {
  280. n = 0;
  281. while (ccltab[(unsigned char)*inp]) {
  282. n++, inr--, inp++;
  283. if (--width == 0)
  284. break;
  285. if (inr <= 0) {
  286. if (n == 0)
  287. goto input_failure;
  288. break;
  289. }
  290. }
  291. if (n == 0)
  292. goto match_failure;
  293. } else {
  294. p0 = p = va_arg(ap, char *);
  295. while (ccltab[(unsigned char)*inp]) {
  296. inr--;
  297. *p++ = *inp++;
  298. if (--width == 0)
  299. break;
  300. if (inr <= 0) {
  301. if (p == p0)
  302. goto input_failure;
  303. break;
  304. }
  305. }
  306. n = p - p0;
  307. if (n == 0)
  308. goto match_failure;
  309. *p = 0;
  310. nassigned++;
  311. }
  312. nread += n;
  313. nconversions++;
  314. break;
  315. case CT_STRING:
  316. /* like CCL, but zero-length string OK, & no NOSKIP */
  317. if (width == 0)
  318. width = (size_t)~0;
  319. if (flags & SUPPRESS) {
  320. n = 0;
  321. while (!isspace(*inp)) {
  322. n++, inr--, inp++;
  323. if (--width == 0)
  324. break;
  325. if (inr <= 0)
  326. break;
  327. }
  328. nread += n;
  329. } else {
  330. p0 = p = va_arg(ap, char *);
  331. while (!isspace(*inp)) {
  332. inr--;
  333. *p++ = *inp++;
  334. if (--width == 0)
  335. break;
  336. if (inr <= 0)
  337. break;
  338. }
  339. *p = 0;
  340. nread += p - p0;
  341. nassigned++;
  342. }
  343. nconversions++;
  344. continue;
  345. case CT_INT:
  346. /* scan an integer as if by strtoq/strtouq */
  347. #ifdef hardway
  348. if (width == 0 || width > sizeof(buf) - 1)
  349. width = sizeof(buf) - 1;
  350. #else
  351. /* size_t is unsigned, hence this optimisation */
  352. if (--width > sizeof(buf) - 2)
  353. width = sizeof(buf) - 2;
  354. width++;
  355. #endif
  356. flags |= SIGNOK | NDIGITS | NZDIGITS;
  357. for (p = buf; width; width--) {
  358. c = *inp;
  359. /*
  360. * Switch on the character; `goto ok'
  361. * if we accept it as a part of number.
  362. */
  363. switch (c) {
  364. /*
  365. * The digit 0 is always legal, but is
  366. * special. For %i conversions, if no
  367. * digits (zero or nonzero) have been
  368. * scanned (only signs), we will have
  369. * base==0. In that case, we should set
  370. * it to 8 and enable 0x prefixing.
  371. * Also, if we have not scanned zero digits
  372. * before this, do not turn off prefixing
  373. * (someone else will turn it off if we
  374. * have scanned any nonzero digits).
  375. */
  376. case '0':
  377. if (base == 0) {
  378. base = 8;
  379. flags |= PFXOK;
  380. }
  381. if (flags & NZDIGITS)
  382. flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
  383. else
  384. flags &= ~(SIGNOK|PFXOK|NDIGITS);
  385. goto ok;
  386. /* 1 through 7 always legal */
  387. case '1': case '2': case '3':
  388. case '4': case '5': case '6': case '7':
  389. base = basefix[base];
  390. flags &= ~(SIGNOK | PFXOK | NDIGITS);
  391. goto ok;
  392. /* digits 8 and 9 ok iff decimal or hex */
  393. case '8': case '9':
  394. base = basefix[base];
  395. if (base <= 8)
  396. break; /* not legal here */
  397. flags &= ~(SIGNOK | PFXOK | NDIGITS);
  398. goto ok;
  399. /* letters ok iff hex */
  400. case 'A': case 'B': case 'C':
  401. case 'D': case 'E': case 'F':
  402. case 'a': case 'b': case 'c':
  403. case 'd': case 'e': case 'f':
  404. /* no need to fix base here */
  405. if (base <= 10)
  406. break; /* not legal here */
  407. flags &= ~(SIGNOK | PFXOK | NDIGITS);
  408. goto ok;
  409. /* sign ok only as first character */
  410. case '+': case '-':
  411. if (flags & SIGNOK) {
  412. flags &= ~SIGNOK;
  413. goto ok;
  414. }
  415. break;
  416. /* x ok iff flag still set & 2nd char */
  417. case 'x': case 'X':
  418. if (flags & PFXOK && p == buf + 1) {
  419. base = 16; /* if %i */
  420. flags &= ~PFXOK;
  421. goto ok;
  422. }
  423. break;
  424. }
  425. /*
  426. * If we got here, c is not a legal character
  427. * for a number. Stop accumulating digits.
  428. */
  429. break;
  430. ok:
  431. /*
  432. * c is legal: store it and look at the next.
  433. */
  434. *p++ = c;
  435. if (--inr > 0)
  436. inp++;
  437. else
  438. break; /* end of input */
  439. }
  440. /*
  441. * If we had only a sign, it is no good; push
  442. * back the sign. If the number ends in `x',
  443. * it was [sign] '0' 'x', so push back the x
  444. * and treat it as [sign] '0'.
  445. */
  446. if (flags & NDIGITS) {
  447. if (p > buf) {
  448. inp--;
  449. inr++;
  450. }
  451. goto match_failure;
  452. }
  453. c = ((u_char *)p)[-1];
  454. if (c == 'x' || c == 'X') {
  455. --p;
  456. inp--;
  457. inr++;
  458. }
  459. if ((flags & SUPPRESS) == 0) {
  460. u_quad_t res;
  461. *p = 0;
  462. res = (*ccfn)(buf, (char **)NULL, base);
  463. if (flags & POINTER)
  464. *va_arg(ap, void **) =
  465. (void *)(uintptr_t)res;
  466. else if (flags & SHORT)
  467. *va_arg(ap, short *) = res;
  468. else if (flags & LONG)
  469. *va_arg(ap, long *) = res;
  470. else if (flags & QUAD)
  471. *va_arg(ap, quad_t *) = res;
  472. else
  473. *va_arg(ap, int *) = res;
  474. nassigned++;
  475. }
  476. nread += p - buf;
  477. nconversions++;
  478. break;
  479. }
  480. }
  481. input_failure:
  482. return (nconversions != 0 ? nassigned : -1);
  483. match_failure:
  484. return (nassigned);
  485. }
  486. /*
  487. * Fill in the given table from the scanset at the given format
  488. * (just after `['). Return a pointer to the character past the
  489. * closing `]'. The table has a 1 wherever characters should be
  490. * considered part of the scanset.
  491. */
  492. static const u_char *
  493. __sccl(char *tab, const u_char *fmt)
  494. {
  495. int c, n, v;
  496. /* first `clear' the whole table */
  497. c = *fmt++; /* first char hat => negated scanset */
  498. if (c == '^') {
  499. v = 1; /* default => accept */
  500. c = *fmt++; /* get new first char */
  501. } else
  502. v = 0; /* default => reject */
  503. /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
  504. for (n = 0; n < 256; n++)
  505. tab[n] = v; /* memset(tab, v, 256) */
  506. if (c == 0)
  507. return (fmt - 1);/* format ended before closing ] */
  508. /*
  509. * Now set the entries corresponding to the actual scanset
  510. * to the opposite of the above.
  511. *
  512. * The first character may be ']' (or '-') without being special;
  513. * the last character may be '-'.
  514. */
  515. v = 1 - v;
  516. for (;;) {
  517. tab[c] = v; /* take character c */
  518. doswitch:
  519. n = *fmt++; /* and examine the next */
  520. switch (n) {
  521. case 0: /* format ended too soon */
  522. return (fmt - 1);
  523. case '-':
  524. /*
  525. * A scanset of the form
  526. * [01+-]
  527. * is defined as `the digit 0, the digit 1,
  528. * the character +, the character -', but
  529. * the effect of a scanset such as
  530. * [a-zA-Z0-9]
  531. * is implementation defined. The V7 Unix
  532. * scanf treats `a-z' as `the letters a through
  533. * z', but treats `a-a' as `the letter a, the
  534. * character -, and the letter a'.
  535. *
  536. * For compatibility, the `-' is not considerd
  537. * to define a range if the character following
  538. * it is either a close bracket (required by ANSI)
  539. * or is not numerically greater than the character
  540. * we just stored in the table (c).
  541. */
  542. n = *fmt;
  543. if (n == ']' || n < c) {
  544. c = '-';
  545. break; /* resume the for(;;) */
  546. }
  547. fmt++;
  548. /* fill in the range */
  549. do {
  550. tab[++c] = v;
  551. } while (c < n);
  552. c = n;
  553. /*
  554. * Alas, the V7 Unix scanf also treats formats
  555. * such as [a-c-e] as `the letters a through e'.
  556. * This too is permitted by the standard....
  557. */
  558. goto doswitch;
  559. break;
  560. case ']': /* end of scanset */
  561. return (fmt);
  562. default: /* just another character */
  563. c = n;
  564. break;
  565. }
  566. }
  567. /* NOTREACHED */
  568. }
  569. /*
  570. int
  571. sscanf(const char *ibuf, const char *fmt, ...)
  572. {
  573. va_list ap;
  574. int ret;
  575. va_start(ap, fmt);
  576. ret = vsscanf(ibuf, fmt, ap);
  577. va_end(ap);
  578. return(ret);
  579. }
  580. */
  581. #ifdef __cplusplus
  582. }
  583. #endif