scanf.c 17 KB

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