scanf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Modified by Manuel Novoa III Mar 13, 2001
  3. *
  4. * The vfscanf routine was completely rewritten to add features and remove
  5. * bugs. The function __strtold, based on my strtod code in stdlib, was
  6. * added to provide floating point support for the scanf functions.
  7. *
  8. * So far they pass the test cases from glibc-2.1.3, except in two instances.
  9. * In one case, the test appears to be broken. The other case is something
  10. * I need to research further. This version of scanf assumes it can only
  11. * peek one character ahead. Apparently, glibc looks further. The difference
  12. * can be seen when parsing a floating point value in the character
  13. * sequence "100ergs". glibc is able to back up before the 'e' and return
  14. * a value of 100, whereas this scanf reports a bad match with the stream
  15. * pointer at 'r'. A similar situation can also happen when parsing hex
  16. * values prefixed by 0x or 0X; a failure would occur for "0xg". In order to
  17. * fix this, I need to rework the "ungetc" machinery in stdio.c again.
  18. * I do have one reference though, that seems to imply scanf has a single
  19. * character of lookahead.
  20. *
  21. * May 20, 2001
  22. *
  23. * Quote from ANSI/ISO C99 standard:
  24. *
  25. * fscanf pushes back at most one input character onto the input stream.
  26. * Therefore, some sequences that are acceptable to strtod, strtol, etc.,
  27. * are unacceptable to fscanf.
  28. *
  29. * So uClibc's *scanf functions conform to the standard, and glibc's
  30. * implementation doesn't for the "100ergs" case mentioned above.
  31. */
  32. #define _GNU_SOURCE
  33. #include <features.h>
  34. #if defined(__UCLIBC__) && !defined(__USE_ISOC99)
  35. #define __USE_ISOC99
  36. #endif
  37. #define _STDIO_UTILITY
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <ctype.h>
  42. #include <string.h>
  43. #include <stdarg.h>
  44. #ifdef __STDIO_THREADSAFE
  45. #include <pthread.h>
  46. #endif /* __STDIO_THREADSAFE */
  47. #ifdef L_scanf
  48. #ifdef __STDC__
  49. int scanf(const char *fmt, ...)
  50. #else
  51. int scanf(fmt, va_alist)
  52. __const char *fmt;
  53. va_dcl
  54. #endif
  55. {
  56. va_list ptr;
  57. int rv;
  58. va_start(ptr, fmt);
  59. rv = vfscanf(stdin, fmt, ptr);
  60. va_end(ptr);
  61. return rv;
  62. }
  63. #endif
  64. #ifdef L_sscanf
  65. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  66. #warning skipping sscanf since no buffering and no custom streams!
  67. #else
  68. int sscanf(const char *sp, const char *fmt, ...)
  69. {
  70. va_list ptr;
  71. int rv;
  72. va_start(ptr, fmt);
  73. rv = vsscanf(sp, fmt, ptr);
  74. va_end(ptr);
  75. return rv;
  76. }
  77. #endif
  78. #endif
  79. #ifdef L_fscanf
  80. #ifdef __STDC__
  81. int fscanf(FILE * fp, const char *fmt, ...)
  82. #else
  83. int fscanf(fp, fmt, va_alist)
  84. FILE *fp;
  85. __const char *fmt;
  86. va_dcl
  87. #endif
  88. {
  89. va_list ptr;
  90. int rv;
  91. va_start(ptr, fmt);
  92. rv = vfscanf(fp, fmt, ptr);
  93. va_end(ptr);
  94. return rv;
  95. }
  96. #endif
  97. #ifdef L_vscanf
  98. int vscanf(fmt, ap)
  99. __const char *fmt;
  100. va_list ap;
  101. {
  102. return vfscanf(stdin, fmt, ap);
  103. }
  104. #endif
  105. #ifdef L_vsscanf
  106. #ifdef __STDIO_BUFFERS
  107. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  108. {
  109. FILE string[1];
  110. string->filedes = -2; /* for debugging */
  111. string->modeflags = (__FLAG_NARROW|__FLAG_READONLY);
  112. string->bufstart = string->bufrpos = (unsigned char *) ((void *) sp);
  113. string->bufgetc = (char *) ((unsigned) -1);
  114. #ifdef __STDIO_THREADSAFE
  115. __stdio_init_mutex(&string->lock);
  116. #endif
  117. return vfscanf(string, fmt, ap);
  118. }
  119. #else /* __STDIO_BUFFERS */
  120. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  121. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  122. {
  123. FILE *f;
  124. int rv;
  125. if ((f = fmemopen((char *)sp, strlen(sp), "r")) == NULL) {
  126. return -1;
  127. }
  128. rv = vfscanf(f, fmt, ap);
  129. fclose(f);
  130. return rv;
  131. }
  132. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  133. #warning skipping vsscanf since no buffering and no custom streams!
  134. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  135. #endif /* __STDIO_BUFFERS */
  136. #endif
  137. #ifdef L_vfscanf
  138. #include <assert.h>
  139. #include <ctype.h>
  140. #include <limits.h>
  141. static int valid_digit(char c, char base)
  142. {
  143. if (base == 16) {
  144. return isxdigit(c);
  145. } else {
  146. return (isdigit(c) && (c < '0' + base));
  147. }
  148. }
  149. extern unsigned long
  150. _stdlib_strto_l(register const char * __restrict str,
  151. char ** __restrict endptr, int base, int sflag);
  152. extern unsigned long long
  153. _stdlib_strto_ll(register const char * __restrict str,
  154. char ** __restrict endptr, int base, int sflag);
  155. struct scan_cookie {
  156. FILE *fp;
  157. int nread;
  158. int width;
  159. int width_flag;
  160. int ungot_char;
  161. int ungot_flag;
  162. int app_ungot;
  163. };
  164. #ifdef __UCLIBC_HAS_LONG_LONG__
  165. static const char qual[] = "hl" /* "jtz" */ "Lq";
  166. /* char = -2, short = -1, int = 0, long = 1, long long = 2 */
  167. static const char qsz[] = { -1, 1, 2, 2 };
  168. #else
  169. static const char qual[] = "hl" /* "jtz" */;
  170. static const char qsz[] = { -1, 1, };
  171. #endif
  172. #ifdef __UCLIBC_HAS_FLOATS__
  173. static int __strtold(long double *ld, struct scan_cookie *sc);
  174. /*01234567890123456 */
  175. static const char spec[] = "%n[csoupxXidfeEgG";
  176. #else
  177. static const char spec[] = "%n[csoupxXid";
  178. #endif
  179. /* radix[i] <-> spec[i+5] o u p x X i d */
  180. static const char radix[] = { 8, 10, 16, 16, 16, 0, 10 };
  181. static void init_scan_cookie(struct scan_cookie *sc, FILE *fp)
  182. {
  183. sc->fp = fp;
  184. sc->nread = 0;
  185. sc->width_flag = 0;
  186. sc->ungot_flag = 0;
  187. sc->app_ungot = ((fp->modeflags & __MASK_UNGOT) ? fp->ungot[1] : 0);
  188. }
  189. /* TODO -- what about literal '\0' chars in a file??? */
  190. static int scan_getc_nw(struct scan_cookie *sc)
  191. {
  192. if (sc->ungot_flag == 0) {
  193. sc->ungot_char = getc(sc->fp);
  194. } else {
  195. sc->ungot_flag = 0;
  196. }
  197. if (sc->ungot_char > 0) {
  198. ++sc->nread;
  199. }
  200. sc->width_flag = 0;
  201. return sc->ungot_char;
  202. }
  203. static int scan_getc(struct scan_cookie *sc)
  204. {
  205. if (sc->ungot_flag == 0) {
  206. sc->ungot_char = getc(sc->fp);
  207. }
  208. sc->width_flag = 1;
  209. if (--sc->width < 0) {
  210. sc->ungot_flag = 1;
  211. return 0;
  212. }
  213. sc->ungot_flag = 0;
  214. if (sc->ungot_char > 0) {
  215. ++sc->nread;
  216. }
  217. return sc->ungot_char;
  218. }
  219. static void scan_ungetc(struct scan_cookie *sc)
  220. {
  221. if (sc->ungot_flag != 0) {
  222. assert(sc->width < 0);
  223. return;
  224. }
  225. if (sc->width_flag) {
  226. ++sc->width;
  227. }
  228. sc->ungot_flag = 1;
  229. if (sc->ungot_char > 0) { /* not EOF or EOS */
  230. --sc->nread;
  231. }
  232. }
  233. static void kill_scan_cookie(struct scan_cookie *sc)
  234. {
  235. if (sc->ungot_flag) {
  236. ungetc(sc->ungot_char,sc->fp);
  237. /* Deal with distiction between user and scanf ungots. */
  238. if (sc->nread == 0) { /* Only one char was read... app ungot? */
  239. sc->fp->ungot[1] = sc->app_ungot; /* restore ungot state. */
  240. }
  241. }
  242. }
  243. int vfscanf(fp, format, ap)
  244. FILE *fp;
  245. const char *format;
  246. va_list ap;
  247. {
  248. #ifdef __UCLIBC_HAS_LONG_LONG__
  249. #define STRTO_L_(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
  250. #define MAX_DIGITS 64
  251. #define UV_TYPE unsigned long long
  252. #define V_TYPE long long
  253. #else
  254. #define STRTO_L_(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
  255. #define MAX_DIGITS 32
  256. #define UV_TYPE unsigned long
  257. #define V_TYPE long
  258. #endif
  259. #ifdef __UCLIBC_HAS_FLOATS__
  260. long double ld;
  261. #endif
  262. UV_TYPE uv;
  263. struct scan_cookie sc;
  264. unsigned const char *fmt;
  265. const char *p;
  266. unsigned char *b;
  267. void *vp;
  268. int cc, i, cnt;
  269. signed char lval;
  270. unsigned char store, usflag, base, invert, r0, r1;
  271. unsigned char buf[MAX_DIGITS+2];
  272. unsigned char scanset[UCHAR_MAX + 1];
  273. __STDIO_THREADLOCK(fp);
  274. init_scan_cookie(&sc,fp);
  275. fmt = (unsigned const char *) format;
  276. cnt = 0;
  277. while (*fmt) {
  278. store = 1;
  279. lval = 0;
  280. sc.width = INT_MAX;
  281. if (*fmt == '%') { /* Conversion specification. */
  282. ++fmt;
  283. if (*fmt == '*') { /* Suppress assignment. */
  284. store = 0;
  285. ++fmt;
  286. }
  287. for (i = 0 ; isdigit(*fmt) ; sc.width = i) {
  288. i = (i * 10) + (*fmt++ - '0'); /* Get specified width. */
  289. }
  290. for (i = 0 ; i < sizeof(qual) ; i++) { /* Optional qualifier. */
  291. if (qual[i] == *fmt) {
  292. ++fmt;
  293. lval += qsz[i];
  294. if ((i < 2) && (qual[i] == *fmt)) { /* Double h or l. */
  295. ++fmt;
  296. lval += qsz[i];
  297. }
  298. break;
  299. }
  300. }
  301. for (p = spec ; *p ; p++) { /* Process format specifier. */
  302. if (*fmt != *p) continue;
  303. if (p-spec < 1) { /* % - match a '%'*/
  304. goto matchchar;
  305. }
  306. if (p-spec < 2) { /* n - store number of chars read */
  307. *(va_arg(ap, int *)) = sc.nread;
  308. scan_getc_nw(&sc);
  309. goto nextfmt;
  310. }
  311. if (p-spec > 3) { /* skip white space if not c or [ */
  312. while (isspace(scan_getc_nw(&sc)))
  313. {}
  314. scan_ungetc(&sc);
  315. }
  316. if (p-spec < 5) { /* [,c,s - string conversions */
  317. invert = 0;
  318. if (*p == 'c') {
  319. invert = 1;
  320. if (sc.width == INT_MAX) {
  321. sc.width = 1;
  322. }
  323. }
  324. for (i=0 ; i<= UCHAR_MAX ; i++) {
  325. scanset[i] = ((*p == 's') ? (isspace(i) == 0) : 0);
  326. }
  327. if (*p == '[') { /* need to build a scanset */
  328. if (*++fmt == '^') {
  329. invert = 1;
  330. ++fmt;
  331. }
  332. if (*fmt == ']') {
  333. scanset[(int)']'] = 1;
  334. ++fmt;
  335. }
  336. r0 = 0;
  337. while (*fmt && *fmt !=']') { /* build scanset */
  338. if ((*fmt == '-') && r0 && (fmt[1] != ']')) {
  339. /* range */
  340. ++fmt;
  341. if (*fmt < r0) {
  342. r1 = r0;
  343. r0 = *fmt;
  344. } else {
  345. r1 = *fmt;
  346. }
  347. for (i=r0 ; i<= r1 ; i++) {
  348. scanset[i] = 1;
  349. }
  350. r0 = 0;
  351. } else {
  352. r0 = *fmt;
  353. scanset[r0] = 1;
  354. }
  355. ++fmt;
  356. }
  357. if (!*fmt) { /* format string exhausted! */
  358. goto done;
  359. }
  360. }
  361. /* ok -- back to common work */
  362. if (sc.width <= 0) {
  363. goto done;
  364. }
  365. if (store) {
  366. b = va_arg(ap, unsigned char *);
  367. } else {
  368. b = buf;
  369. }
  370. cc = scan_getc(&sc);
  371. if (cc <= 0) {
  372. scan_ungetc(&sc);
  373. goto done; /* return EOF if cnt == 0 */
  374. }
  375. i = 0;
  376. while ((cc>0) && (scanset[cc] != invert)) {
  377. i = 1; /* yes, we stored something */
  378. *b = cc;
  379. b += store;
  380. cc = scan_getc(&sc);
  381. }
  382. if (i==0) {
  383. scan_ungetc(&sc);
  384. goto done; /* return cnt */
  385. }
  386. if (*p != 'c') { /* nul-terminate the stored string */
  387. *b = 0;
  388. }
  389. cnt += store;
  390. goto nextfmt;
  391. }
  392. if (p-spec < 12) { /* o,u,p,x,X,i,d - (un)signed integer */
  393. if (*p == 'p') {
  394. /* assume pointer same size as int or long. */
  395. lval = (sizeof(char *) == sizeof(long));
  396. }
  397. usflag = ((p-spec) < 10); /* (1)0 if (un)signed */
  398. base = radix[(int)(p-spec) - 5];
  399. b = buf;
  400. if (sc.width <= 0) {
  401. goto done;
  402. }
  403. cc = scan_getc(&sc);
  404. if ((cc == '+') || (cc == '-')) { /* Handle leading sign.*/
  405. *b++ = cc;
  406. cc = scan_getc(&sc);
  407. }
  408. if (cc == '0') { /* Possibly set base and handle prefix. */
  409. if ((base == 0) || (base == 16)) {
  410. cc = scan_getc(&sc);
  411. if ((cc == 'x') || (cc == 'X')) {
  412. /* We're committed to base 16 now. */
  413. base = 16;
  414. cc = scan_getc(&sc);
  415. } else { /* oops... back up */
  416. scan_ungetc(&sc);
  417. cc = '0';
  418. if (base == 0) {
  419. base = 8;
  420. }
  421. }
  422. }
  423. }
  424. if (base == 0) { /* Default to base 10 */
  425. base = 10;
  426. }
  427. /* At this point, we're ready to start reading digits. */
  428. if (cc == '0') {
  429. *b++ = cc; /* Store first leading 0 */
  430. do { /* but ignore others. */
  431. cc = scan_getc(&sc);
  432. } while (cc == '0');
  433. }
  434. while (valid_digit(cc,base)) { /* Now for nonzero digits.*/
  435. if (b - buf < MAX_DIGITS) {
  436. *b++ = cc;
  437. }
  438. cc = scan_getc(&sc);
  439. }
  440. *b = 0; /* null-terminate */
  441. if ((b == buf) || (*--b == '+') || (*b == '-')) {
  442. scan_ungetc(&sc);
  443. goto done; /* No digits! */
  444. }
  445. if (store) {
  446. if (*buf == '-') {
  447. usflag = 0;
  448. }
  449. uv = STRTO_L_(buf, NULL, base, 1-usflag);
  450. vp = va_arg(ap, void *);
  451. switch (lval) {
  452. case 2: /* If no long long, treat as long . */
  453. #ifdef __UCLIBC_HAS_LONG_LONG__
  454. *((unsigned long long *)vp) = uv;
  455. break;
  456. #endif
  457. case 1:
  458. #if ULONG_MAX == UINT_MAX
  459. case 0: /* int and long int are the same */
  460. #endif
  461. #ifdef __UCLIBC_HAS_LONG_LONG__
  462. if (usflag) {
  463. if (uv > ULONG_MAX) {
  464. uv = ULONG_MAX;
  465. }
  466. } else if (((V_TYPE)uv) > LONG_MAX) {
  467. uv = LONG_MAX;
  468. } else if (((V_TYPE)uv) < LONG_MIN) {
  469. uv = (UV_TYPE) LONG_MIN;
  470. }
  471. #endif
  472. *((unsigned long *)vp) = (unsigned long)uv;
  473. break;
  474. #if ULONG_MAX != UINT_MAX
  475. case 0: /* int and long int are different */
  476. if (usflag) {
  477. if (uv > UINT_MAX) {
  478. uv = UINT_MAX;
  479. }
  480. } else if (((V_TYPE)uv) > INT_MAX) {
  481. uv = INT_MAX;
  482. } else if (((V_TYPE)uv) < INT_MIN) {
  483. uv = (UV_TYPE) INT_MIN;
  484. }
  485. *((unsigned int *)vp) = (unsigned int)uv;
  486. break;
  487. #endif
  488. case -1:
  489. if (usflag) {
  490. if (uv > USHRT_MAX) {
  491. uv = USHRT_MAX;
  492. }
  493. } else if (((V_TYPE)uv) > SHRT_MAX) {
  494. uv = SHRT_MAX;
  495. } else if (((V_TYPE)uv) < SHRT_MIN) {
  496. uv = (UV_TYPE) SHRT_MIN;
  497. }
  498. *((unsigned short *)vp) = (unsigned short)uv;
  499. break;
  500. case -2:
  501. if (usflag) {
  502. if (uv > UCHAR_MAX) {
  503. uv = UCHAR_MAX;
  504. }
  505. } else if (((V_TYPE)uv) > CHAR_MAX) {
  506. uv = CHAR_MAX;
  507. } else if (((V_TYPE)uv) < CHAR_MIN) {
  508. uv = (UV_TYPE) CHAR_MIN;
  509. }
  510. *((unsigned char *)vp) = (unsigned char) uv;
  511. break;
  512. default:
  513. assert(0);
  514. }
  515. ++cnt;
  516. }
  517. goto nextfmt;
  518. }
  519. #ifdef __UCLIBC_HAS_FLOATS__
  520. else { /* floating point */
  521. if (sc.width <= 0) {
  522. goto done;
  523. }
  524. if (__strtold(&ld, &sc)) { /* Success! */
  525. if (store) {
  526. vp = va_arg(ap, void *);
  527. switch (lval) {
  528. case 2:
  529. *((long double *)vp) = ld;
  530. break;
  531. case 1:
  532. *((double *)vp) = (double) ld;
  533. break;
  534. case 0:
  535. *((float *)vp) = (float) ld;
  536. break;
  537. default: /* Illegal qualifier! */
  538. assert(0);
  539. goto done;
  540. }
  541. ++cnt;
  542. }
  543. goto nextfmt;
  544. }
  545. }
  546. #else
  547. assert(0);
  548. #endif
  549. goto done;
  550. }
  551. /* Unrecognized specifier! */
  552. goto RETURN_cnt;
  553. } if (isspace(*fmt)) { /* Consume all whitespace. */
  554. while (isspace(scan_getc_nw(&sc)))
  555. {}
  556. } else { /* Match the current fmt char. */
  557. matchchar:
  558. if (scan_getc_nw(&sc) != *fmt) {
  559. goto done;
  560. }
  561. scan_getc_nw(&sc);
  562. }
  563. nextfmt:
  564. scan_ungetc(&sc);
  565. ++fmt;
  566. }
  567. done: /* end of scan */
  568. kill_scan_cookie(&sc);
  569. if ((sc.ungot_char <= 0) && (cnt == 0) && (*fmt)) {
  570. cnt = EOF;
  571. }
  572. RETURN_cnt:
  573. __STDIO_THREADUNLOCK(fp);
  574. return (cnt);
  575. }
  576. /*****************************************************************************/
  577. #ifdef __UCLIBC_HAS_FLOATS__
  578. #include <float.h>
  579. #define MAX_SIG_DIGITS 20
  580. #define MAX_IGNORED_DIGITS 2000
  581. #define MAX_ALLOWED_EXP (MAX_SIG_DIGITS + MAX_IGNORED_DIGITS + LDBL_MAX_10_EXP)
  582. #if LDBL_DIG > MAX_SIG_DIGITS
  583. #error need to adjust MAX_SIG_DIGITS
  584. #endif
  585. #include <limits.h>
  586. #if MAX_ALLOWED_EXP > INT_MAX
  587. #error size assumption violated for MAX_ALLOWED_EXP
  588. #endif
  589. int __strtold(long double *ld, struct scan_cookie *sc)
  590. {
  591. long double number;
  592. long double p10;
  593. int exponent_power;
  594. int exponent_temp;
  595. int negative;
  596. int num_digits;
  597. int since_decimal;
  598. int c;
  599. c = scan_getc(sc); /* Decrements width. */
  600. negative = 0;
  601. switch(c) { /* Handle optional sign. */
  602. case '-': negative = 1; /* Fall through to get next char. */
  603. case '+': c = scan_getc(sc);
  604. }
  605. number = 0.;
  606. num_digits = -1;
  607. exponent_power = 0;
  608. since_decimal = INT_MIN;
  609. LOOP:
  610. while (isdigit(c)) { /* Process string of digits. */
  611. ++since_decimal;
  612. if (num_digits < 0) { /* First time through? */
  613. ++num_digits; /* We've now seen a digit. */
  614. }
  615. if (num_digits || (c != '0')) { /* had/have nonzero */
  616. ++num_digits;
  617. if (num_digits <= MAX_SIG_DIGITS) { /* Is digit significant? */
  618. number = number * 10. + (c - '0');
  619. }
  620. }
  621. c = scan_getc(sc);
  622. }
  623. if ((c == '.') && (since_decimal < 0)) { /* If no previous decimal pt, */
  624. since_decimal = 0; /* save position of decimal point */
  625. c = scan_getc(sc); /* and process rest of digits */
  626. goto LOOP;
  627. }
  628. if (num_digits<0) { /* Must have at least one digit. */
  629. goto FAIL;
  630. }
  631. if (num_digits > MAX_SIG_DIGITS) { /* Adjust exp for skipped digits. */
  632. exponent_power += num_digits - MAX_SIG_DIGITS;
  633. }
  634. if (since_decimal >= 0) { /* Adjust exponent for decimal point. */
  635. exponent_power -= since_decimal;
  636. }
  637. if (negative) { /* Correct for sign. */
  638. number = -number;
  639. negative = 0; /* Reset for exponent processing below. */
  640. }
  641. /* Process an exponent string. */
  642. if (c == 'e' || c == 'E') {
  643. c = scan_getc(sc);
  644. switch(c) { /* Handle optional sign. */
  645. case '-': negative = 1; /* Fall through to get next char. */
  646. case '+': c = scan_getc(sc);
  647. }
  648. num_digits = 0;
  649. exponent_temp = 0;
  650. while (isdigit(c)) { /* Process string of digits. */
  651. if (exponent_temp < MAX_ALLOWED_EXP) { /* overflow check */
  652. exponent_temp = exponent_temp * 10 + (c - '0');
  653. }
  654. c = scan_getc(sc);
  655. ++num_digits;
  656. }
  657. if (num_digits == 0) { /* Were there no exp digits? */
  658. goto FAIL;
  659. } /* else */
  660. if (negative) {
  661. exponent_power -= exponent_temp;
  662. } else {
  663. exponent_power += exponent_temp;
  664. }
  665. }
  666. if (number != 0.) {
  667. /* Now scale the result. */
  668. exponent_temp = exponent_power;
  669. p10 = 10.;
  670. if (exponent_temp < 0) {
  671. exponent_temp = -exponent_temp;
  672. }
  673. while (exponent_temp) {
  674. if (exponent_temp & 1) {
  675. if (exponent_power < 0) {
  676. number /= p10;
  677. } else {
  678. number *= p10;
  679. }
  680. }
  681. exponent_temp >>= 1;
  682. p10 *= p10;
  683. }
  684. }
  685. *ld = number;
  686. return 1;
  687. FAIL:
  688. scan_ungetc(sc);
  689. return 0;
  690. }
  691. #endif /* __UCLIBC_HAS_FLOATS__ */
  692. #endif