scanf.c 17 KB

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