scanf.c 17 KB

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