scanf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. return vfscanf(string, fmt, ap);
  115. }
  116. #else /* __STDIO_BUFFERS */
  117. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  118. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  119. {
  120. FILE *f;
  121. int rv;
  122. if ((f = fmemopen((char *)sp, strlen(sp), "r")) == NULL) {
  123. return -1;
  124. }
  125. rv = vfscanf(f, fmt, ap);
  126. fclose(f);
  127. return rv;
  128. }
  129. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  130. #warning skipping vsscanf since no buffering and no custom streams!
  131. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  132. #endif /* __STDIO_BUFFERS */
  133. #endif
  134. #ifdef L_vfscanf
  135. #include <assert.h>
  136. #include <ctype.h>
  137. #include <limits.h>
  138. static int valid_digit(char c, char base)
  139. {
  140. if (base == 16) {
  141. return isxdigit(c);
  142. } else {
  143. return (isdigit(c) && (c < '0' + base));
  144. }
  145. }
  146. extern unsigned long
  147. _stdlib_strto_l(register const char * __restrict str,
  148. char ** __restrict endptr, int base, int sflag);
  149. extern unsigned long long
  150. _stdlib_strto_ll(register const char * __restrict str,
  151. char ** __restrict endptr, int base, int sflag);
  152. struct scan_cookie {
  153. FILE *fp;
  154. int nread;
  155. int width;
  156. int width_flag;
  157. int ungot_char;
  158. int ungot_flag;
  159. int app_ungot;
  160. };
  161. #ifdef __UCLIBC_HAS_LONG_LONG__
  162. static const char qual[] = "hl" /* "jtz" */ "Lq";
  163. /* char = -2, short = -1, int = 0, long = 1, long long = 2 */
  164. static const char qsz[] = { -1, 1, 2, 2 };
  165. #else
  166. static const char qual[] = "hl" /* "jtz" */;
  167. static const char qsz[] = { -1, 1, };
  168. #endif
  169. #ifdef __UCLIBC_HAS_FLOATS__
  170. static int __strtold(long double *ld, struct scan_cookie *sc);
  171. /*01234567890123456 */
  172. static const char spec[] = "%n[csoupxXidfeEgG";
  173. #else
  174. static const char spec[] = "%n[csoupxXid";
  175. #endif
  176. /* radix[i] <-> spec[i+5] o u p x X i d */
  177. static const char radix[] = { 8, 10, 16, 16, 16, 0, 10 };
  178. static void init_scan_cookie(struct scan_cookie *sc, FILE *fp)
  179. {
  180. sc->fp = fp;
  181. sc->nread = 0;
  182. sc->width_flag = 0;
  183. sc->ungot_flag = 0;
  184. sc->app_ungot = ((fp->modeflags & __MASK_UNGOT) ? fp->ungot[1] : 0);
  185. }
  186. /* TODO -- what about literal '\0' chars in a file??? */
  187. static int scan_getc_nw(struct scan_cookie *sc)
  188. {
  189. if (sc->ungot_flag == 0) {
  190. sc->ungot_char = getc(sc->fp);
  191. } else {
  192. sc->ungot_flag = 0;
  193. }
  194. if (sc->ungot_char > 0) {
  195. ++sc->nread;
  196. }
  197. sc->width_flag = 0;
  198. return sc->ungot_char;
  199. }
  200. static int scan_getc(struct scan_cookie *sc)
  201. {
  202. if (sc->ungot_flag == 0) {
  203. sc->ungot_char = getc(sc->fp);
  204. }
  205. sc->width_flag = 1;
  206. if (--sc->width < 0) {
  207. sc->ungot_flag = 1;
  208. return 0;
  209. }
  210. sc->ungot_flag = 0;
  211. if (sc->ungot_char > 0) {
  212. ++sc->nread;
  213. }
  214. return sc->ungot_char;
  215. }
  216. static void scan_ungetc(struct scan_cookie *sc)
  217. {
  218. if (sc->ungot_flag != 0) {
  219. assert(sc->width < 0);
  220. return;
  221. }
  222. if (sc->width_flag) {
  223. ++sc->width;
  224. }
  225. sc->ungot_flag = 1;
  226. if (sc->ungot_char > 0) { /* not EOF or EOS */
  227. --sc->nread;
  228. }
  229. }
  230. static void kill_scan_cookie(struct scan_cookie *sc)
  231. {
  232. if (sc->ungot_flag) {
  233. ungetc(sc->ungot_char,sc->fp);
  234. /* Deal with distiction between user and scanf ungots. */
  235. if (sc->nread == 0) { /* Only one char was read... app ungot? */
  236. sc->fp->ungot[1] = sc->app_ungot; /* restore ungot state. */
  237. }
  238. }
  239. }
  240. int vfscanf(fp, format, ap)
  241. FILE *fp;
  242. const char *format;
  243. va_list ap;
  244. {
  245. #ifdef __UCLIBC_HAS_LONG_LONG__
  246. #define STRTO_L_(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
  247. #define MAX_DIGITS 64
  248. #define UV_TYPE unsigned long long
  249. #define V_TYPE long long
  250. #else
  251. #define STRTO_L_(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
  252. #define MAX_DIGITS 32
  253. #define UV_TYPE unsigned long
  254. #define V_TYPE long
  255. #endif
  256. #ifdef __UCLIBC_HAS_FLOATS__
  257. long double ld;
  258. #endif
  259. UV_TYPE uv;
  260. struct scan_cookie sc;
  261. unsigned const char *fmt;
  262. const char *p;
  263. unsigned char *b;
  264. void *vp;
  265. int cc, i, cnt;
  266. signed char lval;
  267. unsigned char store, usflag, base, invert, r0, r1;
  268. unsigned char buf[MAX_DIGITS+2];
  269. unsigned char scanset[UCHAR_MAX + 1];
  270. __STDIO_THREADLOCK(fp);
  271. init_scan_cookie(&sc,fp);
  272. fmt = (unsigned const char *) format;
  273. cnt = 0;
  274. while (*fmt) {
  275. store = 1;
  276. lval = 0;
  277. sc.width = INT_MAX;
  278. if (*fmt == '%') { /* Conversion specification. */
  279. ++fmt;
  280. if (*fmt == '*') { /* Suppress assignment. */
  281. store = 0;
  282. ++fmt;
  283. }
  284. for (i = 0 ; isdigit(*fmt) ; sc.width = i) {
  285. i = (i * 10) + (*fmt++ - '0'); /* Get specified width. */
  286. }
  287. for (i = 0 ; i < sizeof(qual) ; i++) { /* Optional qualifier. */
  288. if (qual[i] == *fmt) {
  289. ++fmt;
  290. lval += qsz[i];
  291. if ((i < 2) && (qual[i] == *fmt)) { /* Double h or l. */
  292. ++fmt;
  293. lval += qsz[i];
  294. }
  295. break;
  296. }
  297. }
  298. for (p = spec ; *p ; p++) { /* Process format specifier. */
  299. if (*fmt != *p) continue;
  300. if (p-spec < 1) { /* % - match a '%'*/
  301. goto matchchar;
  302. }
  303. if (p-spec < 2) { /* n - store number of chars read */
  304. *(va_arg(ap, int *)) = sc.nread;
  305. scan_getc_nw(&sc);
  306. goto nextfmt;
  307. }
  308. if (p-spec > 3) { /* skip white space if not c or [ */
  309. while (isspace(scan_getc_nw(&sc)))
  310. {}
  311. scan_ungetc(&sc);
  312. }
  313. if (p-spec < 5) { /* [,c,s - string conversions */
  314. invert = 0;
  315. if (*p == 'c') {
  316. invert = 1;
  317. if (sc.width == INT_MAX) {
  318. sc.width = 1;
  319. }
  320. }
  321. for (i=0 ; i<= UCHAR_MAX ; i++) {
  322. scanset[i] = ((*p == 's') ? (isspace(i) == 0) : 0);
  323. }
  324. if (*p == '[') { /* need to build a scanset */
  325. if (*++fmt == '^') {
  326. invert = 1;
  327. ++fmt;
  328. }
  329. if (*fmt == ']') {
  330. scanset[(int)']'] = 1;
  331. ++fmt;
  332. }
  333. r0 = 0;
  334. while (*fmt && *fmt !=']') { /* build scanset */
  335. if ((*fmt == '-') && r0 && (fmt[1] != ']')) {
  336. /* range */
  337. ++fmt;
  338. if (*fmt < r0) {
  339. r1 = r0;
  340. r0 = *fmt;
  341. } else {
  342. r1 = *fmt;
  343. }
  344. for (i=r0 ; i<= r1 ; i++) {
  345. scanset[i] = 1;
  346. }
  347. r0 = 0;
  348. } else {
  349. r0 = *fmt;
  350. scanset[r0] = 1;
  351. }
  352. ++fmt;
  353. }
  354. if (!*fmt) { /* format string exhausted! */
  355. goto done;
  356. }
  357. }
  358. /* ok -- back to common work */
  359. if (sc.width <= 0) {
  360. goto done;
  361. }
  362. if (store) {
  363. b = va_arg(ap, unsigned char *);
  364. } else {
  365. b = buf;
  366. }
  367. cc = scan_getc(&sc);
  368. if (cc <= 0) {
  369. scan_ungetc(&sc);
  370. goto done; /* return EOF if cnt == 0 */
  371. }
  372. i = 0;
  373. while ((cc>0) && (scanset[cc] != invert)) {
  374. i = 1; /* yes, we stored something */
  375. *b = cc;
  376. b += store;
  377. cc = scan_getc(&sc);
  378. }
  379. if (i==0) {
  380. scan_ungetc(&sc);
  381. goto done; /* return cnt */
  382. }
  383. if (*p != 'c') { /* nul-terminate the stored string */
  384. *b = 0;
  385. }
  386. cnt += store;
  387. goto nextfmt;
  388. }
  389. if (p-spec < 12) { /* o,u,p,x,X,i,d - (un)signed integer */
  390. if (*p == 'p') {
  391. /* assume pointer same size as int or long. */
  392. lval = (sizeof(char *) == sizeof(long));
  393. }
  394. usflag = ((p-spec) < 10); /* (1)0 if (un)signed */
  395. base = radix[(int)(p-spec) - 5];
  396. b = buf;
  397. if (sc.width <= 0) {
  398. goto done;
  399. }
  400. cc = scan_getc(&sc);
  401. if ((cc == '+') || (cc == '-')) { /* Handle leading sign.*/
  402. *b++ = cc;
  403. cc = scan_getc(&sc);
  404. }
  405. if (cc == '0') { /* Possibly set base and handle prefix. */
  406. if ((base == 0) || (base == 16)) {
  407. cc = scan_getc(&sc);
  408. if ((cc == 'x') || (cc == 'X')) {
  409. /* We're committed to base 16 now. */
  410. base = 16;
  411. cc = scan_getc(&sc);
  412. } else { /* oops... back up */
  413. scan_ungetc(&sc);
  414. cc = '0';
  415. if (base == 0) {
  416. base = 8;
  417. }
  418. }
  419. }
  420. }
  421. if (base == 0) { /* Default to base 10 */
  422. base = 10;
  423. }
  424. /* At this point, we're ready to start reading digits. */
  425. if (cc == '0') {
  426. *b++ = cc; /* Store first leading 0 */
  427. do { /* but ignore others. */
  428. cc = scan_getc(&sc);
  429. } while (cc == '0');
  430. }
  431. while (valid_digit(cc,base)) { /* Now for nonzero digits.*/
  432. if (b - buf < MAX_DIGITS) {
  433. *b++ = cc;
  434. }
  435. cc = scan_getc(&sc);
  436. }
  437. *b = 0; /* null-terminate */
  438. if ((b == buf) || (*--b == '+') || (*b == '-')) {
  439. scan_ungetc(&sc);
  440. goto done; /* No digits! */
  441. }
  442. if (store) {
  443. if (*buf == '-') {
  444. usflag = 0;
  445. }
  446. uv = STRTO_L_(buf, NULL, base, 1-usflag);
  447. vp = va_arg(ap, void *);
  448. switch (lval) {
  449. case 2: /* If no long long, treat as long . */
  450. #ifdef __UCLIBC_HAS_LONG_LONG__
  451. *((unsigned long long *)vp) = uv;
  452. break;
  453. #endif
  454. case 1:
  455. #if ULONG_MAX == UINT_MAX
  456. case 0: /* int and long int are the same */
  457. #endif
  458. #ifdef __UCLIBC_HAS_LONG_LONG__
  459. if (usflag) {
  460. if (uv > ULONG_MAX) {
  461. uv = ULONG_MAX;
  462. }
  463. } else if (((V_TYPE)uv) > LONG_MAX) {
  464. uv = LONG_MAX;
  465. } else if (((V_TYPE)uv) < LONG_MIN) {
  466. uv = (UV_TYPE) LONG_MIN;
  467. }
  468. #endif
  469. *((unsigned long *)vp) = (unsigned long)uv;
  470. break;
  471. #if ULONG_MAX != UINT_MAX
  472. case 0: /* int and long int are different */
  473. if (usflag) {
  474. if (uv > UINT_MAX) {
  475. uv = UINT_MAX;
  476. }
  477. } else if (((V_TYPE)uv) > INT_MAX) {
  478. uv = INT_MAX;
  479. } else if (((V_TYPE)uv) < INT_MIN) {
  480. uv = (UV_TYPE) INT_MIN;
  481. }
  482. *((unsigned int *)vp) = (unsigned int)uv;
  483. break;
  484. #endif
  485. case -1:
  486. if (usflag) {
  487. if (uv > USHRT_MAX) {
  488. uv = USHRT_MAX;
  489. }
  490. } else if (((V_TYPE)uv) > SHRT_MAX) {
  491. uv = SHRT_MAX;
  492. } else if (((V_TYPE)uv) < SHRT_MIN) {
  493. uv = (UV_TYPE) SHRT_MIN;
  494. }
  495. *((unsigned short *)vp) = (unsigned short)uv;
  496. break;
  497. case -2:
  498. if (usflag) {
  499. if (uv > UCHAR_MAX) {
  500. uv = UCHAR_MAX;
  501. }
  502. } else if (((V_TYPE)uv) > CHAR_MAX) {
  503. uv = CHAR_MAX;
  504. } else if (((V_TYPE)uv) < CHAR_MIN) {
  505. uv = (UV_TYPE) CHAR_MIN;
  506. }
  507. *((unsigned char *)vp) = (unsigned char) uv;
  508. break;
  509. default:
  510. assert(0);
  511. }
  512. ++cnt;
  513. }
  514. goto nextfmt;
  515. }
  516. #ifdef __UCLIBC_HAS_FLOATS__
  517. else { /* floating point */
  518. if (sc.width <= 0) {
  519. goto done;
  520. }
  521. if (__strtold(&ld, &sc)) { /* Success! */
  522. if (store) {
  523. vp = va_arg(ap, void *);
  524. switch (lval) {
  525. case 2:
  526. *((long double *)vp) = ld;
  527. break;
  528. case 1:
  529. *((double *)vp) = (double) ld;
  530. break;
  531. case 0:
  532. *((float *)vp) = (float) ld;
  533. break;
  534. default: /* Illegal qualifier! */
  535. assert(0);
  536. goto done;
  537. }
  538. ++cnt;
  539. }
  540. goto nextfmt;
  541. }
  542. }
  543. #else
  544. assert(0);
  545. #endif
  546. goto done;
  547. }
  548. /* Unrecognized specifier! */
  549. goto RETURN_cnt;
  550. } if (isspace(*fmt)) { /* Consume all whitespace. */
  551. while (isspace(scan_getc_nw(&sc)))
  552. {}
  553. } else { /* Match the current fmt char. */
  554. matchchar:
  555. if (scan_getc_nw(&sc) != *fmt) {
  556. goto done;
  557. }
  558. scan_getc_nw(&sc);
  559. }
  560. nextfmt:
  561. scan_ungetc(&sc);
  562. ++fmt;
  563. }
  564. done: /* end of scan */
  565. kill_scan_cookie(&sc);
  566. if ((sc.ungot_char <= 0) && (cnt == 0) && (*fmt)) {
  567. cnt = EOF;
  568. }
  569. RETURN_cnt:
  570. __STDIO_THREADUNLOCK(fp);
  571. return (cnt);
  572. }
  573. /*****************************************************************************/
  574. #ifdef __UCLIBC_HAS_FLOATS__
  575. #include <float.h>
  576. #define MAX_SIG_DIGITS 20
  577. #define MAX_IGNORED_DIGITS 2000
  578. #define MAX_ALLOWED_EXP (MAX_SIG_DIGITS + MAX_IGNORED_DIGITS + LDBL_MAX_10_EXP)
  579. #if LDBL_DIG > MAX_SIG_DIGITS
  580. #error need to adjust MAX_SIG_DIGITS
  581. #endif
  582. #include <limits.h>
  583. #if MAX_ALLOWED_EXP > INT_MAX
  584. #error size assumption violated for MAX_ALLOWED_EXP
  585. #endif
  586. int __strtold(long double *ld, struct scan_cookie *sc)
  587. {
  588. long double number;
  589. long double p10;
  590. int exponent_power;
  591. int exponent_temp;
  592. int negative;
  593. int num_digits;
  594. int since_decimal;
  595. int c;
  596. c = scan_getc(sc); /* Decrements width. */
  597. negative = 0;
  598. switch(c) { /* Handle optional sign. */
  599. case '-': negative = 1; /* Fall through to get next char. */
  600. case '+': c = scan_getc(sc);
  601. }
  602. number = 0.;
  603. num_digits = -1;
  604. exponent_power = 0;
  605. since_decimal = INT_MIN;
  606. LOOP:
  607. while (isdigit(c)) { /* Process string of digits. */
  608. ++since_decimal;
  609. if (num_digits < 0) { /* First time through? */
  610. ++num_digits; /* We've now seen a digit. */
  611. }
  612. if (num_digits || (c != '0')) { /* had/have nonzero */
  613. ++num_digits;
  614. if (num_digits <= MAX_SIG_DIGITS) { /* Is digit significant? */
  615. number = number * 10. + (c - '0');
  616. }
  617. }
  618. c = scan_getc(sc);
  619. }
  620. if ((c == '.') && (since_decimal < 0)) { /* If no previous decimal pt, */
  621. since_decimal = 0; /* save position of decimal point */
  622. c = scan_getc(sc); /* and process rest of digits */
  623. goto LOOP;
  624. }
  625. if (num_digits<0) { /* Must have at least one digit. */
  626. goto FAIL;
  627. }
  628. if (num_digits > MAX_SIG_DIGITS) { /* Adjust exp for skipped digits. */
  629. exponent_power += num_digits - MAX_SIG_DIGITS;
  630. }
  631. if (since_decimal >= 0) { /* Adjust exponent for decimal point. */
  632. exponent_power -= since_decimal;
  633. }
  634. if (negative) { /* Correct for sign. */
  635. number = -number;
  636. negative = 0; /* Reset for exponent processing below. */
  637. }
  638. /* Process an exponent string. */
  639. if (c == 'e' || c == 'E') {
  640. c = scan_getc(sc);
  641. switch(c) { /* Handle optional sign. */
  642. case '-': negative = 1; /* Fall through to get next char. */
  643. case '+': c = scan_getc(sc);
  644. }
  645. num_digits = 0;
  646. exponent_temp = 0;
  647. while (isdigit(c)) { /* Process string of digits. */
  648. if (exponent_temp < MAX_ALLOWED_EXP) { /* overflow check */
  649. exponent_temp = exponent_temp * 10 + (c - '0');
  650. }
  651. c = scan_getc(sc);
  652. ++num_digits;
  653. }
  654. if (num_digits == 0) { /* Were there no exp digits? */
  655. goto FAIL;
  656. } /* else */
  657. if (negative) {
  658. exponent_power -= exponent_temp;
  659. } else {
  660. exponent_power += exponent_temp;
  661. }
  662. }
  663. if (number != 0.) {
  664. /* Now scale the result. */
  665. exponent_temp = exponent_power;
  666. p10 = 10.;
  667. if (exponent_temp < 0) {
  668. exponent_temp = -exponent_temp;
  669. }
  670. while (exponent_temp) {
  671. if (exponent_temp & 1) {
  672. if (exponent_power < 0) {
  673. number /= p10;
  674. } else {
  675. number *= p10;
  676. }
  677. }
  678. exponent_temp >>= 1;
  679. p10 *= p10;
  680. }
  681. }
  682. *ld = number;
  683. return 1;
  684. FAIL:
  685. scan_ungetc(sc);
  686. return 0;
  687. }
  688. #endif /* __UCLIBC_HAS_FLOATS__ */
  689. #endif