scanf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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 _ISOC99_SOURCE /* for LLONG_MAX primarily... */
  33. #define _GNU_SOURCE
  34. #define _STDIO_UTILITY
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <ctype.h>
  39. #include <string.h>
  40. #include <stdarg.h>
  41. #ifdef __STDIO_THREADSAFE
  42. #include <stdio_ext.h>
  43. #include <pthread.h>
  44. #endif /* __STDIO_THREADSAFE */
  45. #ifdef L_scanf
  46. #ifdef __STDC__
  47. int scanf(const char *fmt, ...)
  48. #else
  49. int scanf(fmt, va_alist)
  50. __const char *fmt;
  51. va_dcl
  52. #endif
  53. {
  54. va_list ptr;
  55. int rv;
  56. va_start(ptr, fmt);
  57. rv = vfscanf(stdin, fmt, ptr);
  58. va_end(ptr);
  59. return rv;
  60. }
  61. #endif
  62. #ifdef L_sscanf
  63. #if !defined(__STDIO_BUFFERS) && !defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  64. #warning skipping sscanf since no buffering and no custom streams!
  65. #else
  66. int sscanf(const char *sp, const char *fmt, ...)
  67. {
  68. va_list ptr;
  69. int rv;
  70. va_start(ptr, fmt);
  71. rv = vsscanf(sp, fmt, ptr);
  72. va_end(ptr);
  73. return rv;
  74. }
  75. #endif
  76. #endif
  77. #ifdef L_fscanf
  78. #ifdef __STDC__
  79. int fscanf(FILE * fp, const char *fmt, ...)
  80. #else
  81. int fscanf(fp, fmt, va_alist)
  82. FILE *fp;
  83. __const char *fmt;
  84. va_dcl
  85. #endif
  86. {
  87. va_list ptr;
  88. int rv;
  89. va_start(ptr, fmt);
  90. rv = vfscanf(fp, fmt, ptr);
  91. va_end(ptr);
  92. return rv;
  93. }
  94. #endif
  95. #ifdef L_vscanf
  96. int vscanf(fmt, ap)
  97. __const char *fmt;
  98. va_list ap;
  99. {
  100. return vfscanf(stdin, fmt, ap);
  101. }
  102. #endif
  103. #ifdef L_vsscanf
  104. #ifdef __STDIO_BUFFERS
  105. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  106. {
  107. FILE string[1];
  108. string->filedes = -2; /* for debugging */
  109. string->modeflags = (__FLAG_NARROW|__FLAG_READONLY);
  110. string->bufstart = string->bufpos = (unsigned char *) ((void *) sp);
  111. string->bufgetc = (char *) ((unsigned) -1);
  112. #ifdef __STDIO_MBSTATE
  113. __INIT_MBSTATE(&(string->state));
  114. #endif /* __STDIO_MBSTATE */
  115. #ifdef __STDIO_THREADSAFE
  116. string->user_locking = 0;
  117. __stdio_init_mutex(&string->lock);
  118. #endif
  119. return vfscanf(string, fmt, ap);
  120. }
  121. #else /* __STDIO_BUFFERS */
  122. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  123. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  124. {
  125. FILE *f;
  126. int rv;
  127. if ((f = fmemopen((char *)sp, strlen(sp), "r")) == NULL) {
  128. return -1;
  129. }
  130. rv = vfscanf(f, fmt, ap);
  131. fclose(f);
  132. return rv;
  133. }
  134. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  135. #warning skipping vsscanf since no buffering and no custom streams!
  136. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  137. #endif /* __STDIO_BUFFERS */
  138. #endif
  139. #ifdef L_vfscanf
  140. #include <assert.h>
  141. #include <ctype.h>
  142. #include <limits.h>
  143. static int valid_digit(char c, char base)
  144. {
  145. if (base == 16) {
  146. return isxdigit(c);
  147. } else {
  148. return (__isdigit(c) && (c < '0' + base));
  149. }
  150. }
  151. extern unsigned long
  152. _stdlib_strto_l(register const char * __restrict str,
  153. char ** __restrict endptr, int base, int sflag);
  154. #ifdef LLONG_MAX
  155. extern unsigned long long
  156. _stdlib_strto_ll(register const char * __restrict str,
  157. char ** __restrict endptr, int base, int sflag);
  158. #endif
  159. struct scan_cookie {
  160. FILE *fp;
  161. int nread;
  162. int width;
  163. int width_flag;
  164. int ungot_char;
  165. int ungot_flag;
  166. int app_ungot;
  167. };
  168. static const char qual[] = "hl" /* "jtz" */ "Lq";
  169. /* char = -2, short = -1, int = 0, long = 1, long long = 2 */
  170. static const char qsz[] = { -1, 1, 2, 2 };
  171. #ifdef __UCLIBC_HAS_FLOATS__
  172. static int __strtold(long double *ld, struct scan_cookie *sc);
  173. /*01234567890123456 */
  174. static const char spec[] = "%n[csoupxXidfeEgG";
  175. #else
  176. static const char spec[] = "%n[csoupxXid";
  177. #endif
  178. /* radix[i] <-> spec[i+5] o u p x X i d */
  179. static const char radix[] = { 8, 10, 16, 16, 16, 0, 10 };
  180. static void init_scan_cookie(register struct scan_cookie *sc,
  181. register 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(register 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(register 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(register 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(register 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(FILE *fp, const char *format, va_list ap)
  244. {
  245. #define STRTO_L_(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
  246. #define MAX_DIGITS 64
  247. #define UV_TYPE unsigned long long
  248. #define V_TYPE long long
  249. #ifdef __UCLIBC_HAS_FLOATS__
  250. long double ld;
  251. #endif
  252. UV_TYPE uv;
  253. struct scan_cookie sc;
  254. register unsigned const char *fmt;
  255. const char *p;
  256. register unsigned char *b;
  257. void *vp;
  258. int cc, i, cnt;
  259. signed char lval;
  260. unsigned char store, usflag, base, invert, r0, r1;
  261. unsigned char buf[MAX_DIGITS+2];
  262. unsigned char scanset[UCHAR_MAX + 1];
  263. __STDIO_THREADLOCK(fp);
  264. init_scan_cookie(&sc,fp);
  265. fmt = (unsigned const char *) format;
  266. cnt = 0;
  267. while (*fmt) {
  268. store = 1;
  269. lval = 0;
  270. sc.width = INT_MAX;
  271. if (*fmt == '%') { /* Conversion specification. */
  272. ++fmt;
  273. if (*fmt == '*') { /* Suppress assignment. */
  274. store = 0;
  275. ++fmt;
  276. }
  277. for (i = 0 ; __isdigit(*fmt) ; sc.width = i) {
  278. i = (i * 10) + (*fmt++ - '0'); /* Get specified width. */
  279. }
  280. for (i = 0 ; i < sizeof(qual) ; i++) { /* Optional qualifier. */
  281. if (qual[i] == *fmt) {
  282. ++fmt;
  283. lval += qsz[i];
  284. if ((i < 2) && (qual[i] == *fmt)) { /* Double h or l. */
  285. ++fmt;
  286. lval += qsz[i];
  287. }
  288. break;
  289. }
  290. }
  291. for (p = spec ; *p ; p++) { /* Process format specifier. */
  292. if (*fmt != *p) continue;
  293. if (p-spec < 1) { /* % - match a '%'*/
  294. goto matchchar;
  295. }
  296. if (p-spec < 2) { /* n - store number of chars read */
  297. *(va_arg(ap, int *)) = sc.nread;
  298. scan_getc_nw(&sc);
  299. goto nextfmt;
  300. }
  301. if (p-spec > 3) { /* skip white space if not c or [ */
  302. do {
  303. i = scan_getc_nw(&sc);
  304. } while (__isspace(i));
  305. scan_ungetc(&sc);
  306. }
  307. if (p-spec < 5) { /* [,c,s - string conversions */
  308. invert = 0;
  309. if (*p == 'c') {
  310. invert = 1;
  311. if (sc.width == INT_MAX) {
  312. sc.width = 1;
  313. }
  314. }
  315. for (i=0 ; i<= UCHAR_MAX ; i++) {
  316. scanset[i] = ((*p == 's') ? (__isspace(i) == 0) : 0);
  317. }
  318. if (*p == '[') { /* need to build a scanset */
  319. if (*++fmt == '^') {
  320. invert = 1;
  321. ++fmt;
  322. }
  323. if (*fmt == ']') {
  324. scanset[(int)']'] = 1;
  325. ++fmt;
  326. }
  327. r0 = 0;
  328. while (*fmt && *fmt !=']') { /* build scanset */
  329. if ((*fmt == '-') && r0 && (fmt[1] != ']')) {
  330. /* range */
  331. ++fmt;
  332. if (*fmt < r0) {
  333. r1 = r0;
  334. r0 = *fmt;
  335. } else {
  336. r1 = *fmt;
  337. }
  338. for (i=r0 ; i<= r1 ; i++) {
  339. scanset[i] = 1;
  340. }
  341. r0 = 0;
  342. } else {
  343. r0 = *fmt;
  344. scanset[r0] = 1;
  345. }
  346. ++fmt;
  347. }
  348. if (!*fmt) { /* format string exhausted! */
  349. goto done;
  350. }
  351. }
  352. /* ok -- back to common work */
  353. if (sc.width <= 0) {
  354. goto done;
  355. }
  356. if (store) {
  357. b = va_arg(ap, unsigned char *);
  358. } else {
  359. b = buf;
  360. }
  361. cc = scan_getc(&sc);
  362. if (cc <= 0) {
  363. scan_ungetc(&sc);
  364. goto done; /* return EOF if cnt == 0 */
  365. }
  366. i = 0;
  367. while ((cc>0) && (scanset[cc] != invert)) {
  368. i = 1; /* yes, we stored something */
  369. *b = cc;
  370. b += store;
  371. cc = scan_getc(&sc);
  372. }
  373. if (i==0) {
  374. scan_ungetc(&sc);
  375. goto done; /* return cnt */
  376. }
  377. if (*p != 'c') { /* nul-terminate the stored string */
  378. *b = 0;
  379. }
  380. cnt += store;
  381. goto nextfmt;
  382. }
  383. if (p-spec < 12) { /* o,u,p,x,X,i,d - (un)signed integer */
  384. if (*p == 'p') {
  385. /* assume pointer same size as int or long. */
  386. lval = (sizeof(char *) == sizeof(long));
  387. }
  388. usflag = ((p-spec) < 10); /* (1)0 if (un)signed */
  389. base = radix[(int)(p-spec) - 5];
  390. b = buf;
  391. if (sc.width <= 0) {
  392. goto done;
  393. }
  394. cc = scan_getc(&sc);
  395. if ((cc == '+') || (cc == '-')) { /* Handle leading sign.*/
  396. *b++ = cc;
  397. cc = scan_getc(&sc);
  398. }
  399. if (cc == '0') { /* Possibly set base and handle prefix. */
  400. if ((base == 0) || (base == 16)) {
  401. cc = scan_getc(&sc);
  402. if ((cc == 'x') || (cc == 'X')) {
  403. /* We're committed to base 16 now. */
  404. base = 16;
  405. cc = scan_getc(&sc);
  406. } else { /* oops... back up */
  407. scan_ungetc(&sc);
  408. cc = '0';
  409. if (base == 0) {
  410. base = 8;
  411. }
  412. }
  413. }
  414. }
  415. if (base == 0) { /* Default to base 10 */
  416. base = 10;
  417. }
  418. /* At this point, we're ready to start reading digits. */
  419. if (cc == '0') {
  420. *b++ = cc; /* Store first leading 0 */
  421. do { /* but ignore others. */
  422. cc = scan_getc(&sc);
  423. } while (cc == '0');
  424. }
  425. while (valid_digit(cc,base)) { /* Now for nonzero digits.*/
  426. if (b - buf < MAX_DIGITS) {
  427. *b++ = cc;
  428. }
  429. cc = scan_getc(&sc);
  430. }
  431. *b = 0; /* null-terminate */
  432. if ((b == buf) || (*--b == '+') || (*b == '-')) {
  433. scan_ungetc(&sc);
  434. goto done; /* No digits! */
  435. }
  436. if (store) {
  437. if (*buf == '-') {
  438. usflag = 0;
  439. }
  440. uv = STRTO_L_(buf, NULL, base, 1-usflag);
  441. vp = va_arg(ap, void *);
  442. switch (lval) {
  443. case 2: /* If no long long, treat as long . */
  444. *((unsigned long long *)vp) = uv;
  445. break;
  446. case 1:
  447. #if ULONG_MAX == UINT_MAX
  448. case 0: /* int and long int are the same */
  449. #endif
  450. if (usflag) {
  451. if (uv > ULONG_MAX) {
  452. uv = ULONG_MAX;
  453. }
  454. } else if (((V_TYPE)uv) > LONG_MAX) {
  455. uv = LONG_MAX;
  456. } else if (((V_TYPE)uv) < LONG_MIN) {
  457. uv = (UV_TYPE) LONG_MIN;
  458. }
  459. *((unsigned long *)vp) = (unsigned long)uv;
  460. break;
  461. #if ULONG_MAX != UINT_MAX
  462. case 0: /* int and long int are different */
  463. if (usflag) {
  464. if (uv > UINT_MAX) {
  465. uv = UINT_MAX;
  466. }
  467. } else if (((V_TYPE)uv) > INT_MAX) {
  468. uv = INT_MAX;
  469. } else if (((V_TYPE)uv) < INT_MIN) {
  470. uv = (UV_TYPE) INT_MIN;
  471. }
  472. *((unsigned int *)vp) = (unsigned int)uv;
  473. break;
  474. #endif
  475. case (signed char)(-1):
  476. if (usflag) {
  477. if (uv > USHRT_MAX) {
  478. uv = USHRT_MAX;
  479. }
  480. } else if (((V_TYPE)uv) > SHRT_MAX) {
  481. uv = SHRT_MAX;
  482. } else if (((V_TYPE)uv) < SHRT_MIN) {
  483. uv = (UV_TYPE) SHRT_MIN;
  484. }
  485. *((unsigned short *)vp) = (unsigned short)uv;
  486. break;
  487. case (signed char)(-2):
  488. if (usflag) {
  489. if (uv > UCHAR_MAX) {
  490. uv = UCHAR_MAX;
  491. }
  492. } else if (((V_TYPE)uv) > CHAR_MAX) {
  493. uv = CHAR_MAX;
  494. } else if (((V_TYPE)uv) < CHAR_MIN) {
  495. uv = (UV_TYPE) CHAR_MIN;
  496. }
  497. *((unsigned char *)vp) = (unsigned char) uv;
  498. break;
  499. default:
  500. assert(0);
  501. }
  502. ++cnt;
  503. }
  504. goto nextfmt;
  505. }
  506. #ifdef __UCLIBC_HAS_FLOATS__
  507. else { /* floating point */
  508. if (sc.width <= 0) {
  509. goto done;
  510. }
  511. if (__strtold(&ld, &sc)) { /* Success! */
  512. if (store) {
  513. vp = va_arg(ap, void *);
  514. switch (lval) {
  515. case 2:
  516. *((long double *)vp) = ld;
  517. break;
  518. case 1:
  519. *((double *)vp) = (double) ld;
  520. break;
  521. case 0:
  522. *((float *)vp) = (float) ld;
  523. break;
  524. default: /* Illegal qualifier! */
  525. assert(0);
  526. goto done;
  527. }
  528. ++cnt;
  529. }
  530. goto nextfmt;
  531. }
  532. }
  533. #else
  534. assert(0);
  535. #endif
  536. goto done;
  537. }
  538. /* Unrecognized specifier! */
  539. goto RETURN_cnt;
  540. } if (__isspace(*fmt)) { /* Consume all whitespace. */
  541. do {
  542. i = scan_getc_nw(&sc);
  543. } while (__isspace(i));
  544. } else { /* Match the current fmt char. */
  545. matchchar:
  546. if (scan_getc_nw(&sc) != *fmt) {
  547. goto done;
  548. }
  549. scan_getc_nw(&sc);
  550. }
  551. nextfmt:
  552. scan_ungetc(&sc);
  553. ++fmt;
  554. }
  555. done: /* end of scan */
  556. kill_scan_cookie(&sc);
  557. if ((sc.ungot_char <= 0) && (cnt == 0) && (*fmt)) {
  558. cnt = EOF;
  559. }
  560. RETURN_cnt:
  561. __STDIO_THREADUNLOCK(fp);
  562. return (cnt);
  563. }
  564. /*****************************************************************************/
  565. #ifdef __UCLIBC_HAS_FLOATS__
  566. #include <float.h>
  567. #define MAX_SIG_DIGITS 20
  568. #define MAX_IGNORED_DIGITS 2000
  569. #define MAX_ALLOWED_EXP (MAX_SIG_DIGITS + MAX_IGNORED_DIGITS + LDBL_MAX_10_EXP)
  570. #if LDBL_DIG > MAX_SIG_DIGITS
  571. #error need to adjust MAX_SIG_DIGITS
  572. #endif
  573. #include <limits.h>
  574. #if MAX_ALLOWED_EXP > INT_MAX
  575. #error size assumption violated for MAX_ALLOWED_EXP
  576. #endif
  577. int __strtold(long double *ld, struct scan_cookie *sc)
  578. {
  579. long double number;
  580. long double p10;
  581. int exponent_power;
  582. int exponent_temp;
  583. int negative;
  584. int num_digits;
  585. int since_decimal;
  586. int c;
  587. c = scan_getc(sc); /* Decrements width. */
  588. negative = 0;
  589. switch(c) { /* Handle optional sign. */
  590. case '-': negative = 1; /* Fall through to get next char. */
  591. case '+': c = scan_getc(sc);
  592. }
  593. number = 0.;
  594. num_digits = -1;
  595. exponent_power = 0;
  596. since_decimal = INT_MIN;
  597. LOOP:
  598. while (__isdigit(c)) { /* Process string of digits. */
  599. ++since_decimal;
  600. if (num_digits < 0) { /* First time through? */
  601. ++num_digits; /* We've now seen a digit. */
  602. }
  603. if (num_digits || (c != '0')) { /* had/have nonzero */
  604. ++num_digits;
  605. if (num_digits <= MAX_SIG_DIGITS) { /* Is digit significant? */
  606. number = number * 10. + (c - '0');
  607. }
  608. }
  609. c = scan_getc(sc);
  610. }
  611. if ((c == '.') && (since_decimal < 0)) { /* If no previous decimal pt, */
  612. since_decimal = 0; /* save position of decimal point */
  613. c = scan_getc(sc); /* and process rest of digits */
  614. goto LOOP;
  615. }
  616. if (num_digits<0) { /* Must have at least one digit. */
  617. goto FAIL;
  618. }
  619. if (num_digits > MAX_SIG_DIGITS) { /* Adjust exp for skipped digits. */
  620. exponent_power += num_digits - MAX_SIG_DIGITS;
  621. }
  622. if (since_decimal >= 0) { /* Adjust exponent for decimal point. */
  623. exponent_power -= since_decimal;
  624. }
  625. if (negative) { /* Correct for sign. */
  626. number = -number;
  627. negative = 0; /* Reset for exponent processing below. */
  628. }
  629. /* Process an exponent string. */
  630. if (c == 'e' || c == 'E') {
  631. c = scan_getc(sc);
  632. switch(c) { /* Handle optional sign. */
  633. case '-': negative = 1; /* Fall through to get next char. */
  634. case '+': c = scan_getc(sc);
  635. }
  636. num_digits = 0;
  637. exponent_temp = 0;
  638. while (__isdigit(c)) { /* Process string of digits. */
  639. if (exponent_temp < MAX_ALLOWED_EXP) { /* overflow check */
  640. exponent_temp = exponent_temp * 10 + (c - '0');
  641. }
  642. c = scan_getc(sc);
  643. ++num_digits;
  644. }
  645. if (num_digits == 0) { /* Were there no exp digits? */
  646. goto FAIL;
  647. } /* else */
  648. if (negative) {
  649. exponent_power -= exponent_temp;
  650. } else {
  651. exponent_power += exponent_temp;
  652. }
  653. }
  654. if (number != 0.) {
  655. /* Now scale the result. */
  656. exponent_temp = exponent_power;
  657. p10 = 10.;
  658. if (exponent_temp < 0) {
  659. exponent_temp = -exponent_temp;
  660. }
  661. while (exponent_temp) {
  662. if (exponent_temp & 1) {
  663. if (exponent_power < 0) {
  664. number /= p10;
  665. } else {
  666. number *= p10;
  667. }
  668. }
  669. exponent_temp >>= 1;
  670. p10 *= p10;
  671. }
  672. }
  673. *ld = number;
  674. return 1;
  675. FAIL:
  676. scan_ungetc(sc);
  677. return 0;
  678. }
  679. #endif /* __UCLIBC_HAS_FLOATS__ */
  680. #endif