scanf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. #ifdef __STDIO_THREADSAFE
  115. __stdio_init_mutex(&string->lock);
  116. #endif
  117. return vfscanf(string, fmt, ap);
  118. }
  119. #else /* __STDIO_BUFFERS */
  120. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  121. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  122. {
  123. FILE *f;
  124. int rv;
  125. if ((f = fmemopen((char *)sp, strlen(sp), "r")) == NULL) {
  126. return -1;
  127. }
  128. rv = vfscanf(f, fmt, ap);
  129. fclose(f);
  130. return rv;
  131. }
  132. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  133. #warning skipping vsscanf since no buffering and no custom streams!
  134. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  135. #endif /* __STDIO_BUFFERS */
  136. #endif
  137. #ifdef L_vfscanf
  138. #include <assert.h>
  139. #include <ctype.h>
  140. #include <limits.h>
  141. static int valid_digit(char c, char base)
  142. {
  143. if (base == 16) {
  144. return isxdigit(c);
  145. } else {
  146. return (__isdigit(c) && (c < '0' + base));
  147. }
  148. }
  149. extern unsigned long
  150. _stdlib_strto_l(register const char * __restrict str,
  151. char ** __restrict endptr, int base, int sflag);
  152. extern unsigned long long
  153. _stdlib_strto_ll(register const char * __restrict str,
  154. char ** __restrict endptr, int base, int sflag);
  155. struct scan_cookie {
  156. FILE *fp;
  157. int nread;
  158. int width;
  159. int width_flag;
  160. int ungot_char;
  161. int ungot_flag;
  162. int app_ungot;
  163. };
  164. #ifdef __UCLIBC_HAS_LONG_LONG__
  165. static const char qual[] = "hl" /* "jtz" */ "Lq";
  166. /* char = -2, short = -1, int = 0, long = 1, long long = 2 */
  167. static const char qsz[] = { -1, 1, 2, 2 };
  168. #else
  169. static const char qual[] = "hl" /* "jtz" */;
  170. static const char qsz[] = { -1, 1, };
  171. #endif
  172. #ifdef __UCLIBC_HAS_FLOATS__
  173. static int __strtold(long double *ld, struct scan_cookie *sc);
  174. /*01234567890123456 */
  175. static const char spec[] = "%n[csoupxXidfeEgG";
  176. #else
  177. static const char spec[] = "%n[csoupxXid";
  178. #endif
  179. /* radix[i] <-> spec[i+5] o u p x X i d */
  180. static const char radix[] = { 8, 10, 16, 16, 16, 0, 10 };
  181. static void init_scan_cookie(struct scan_cookie *sc, 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(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(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(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(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(fp, format, ap)
  244. FILE *fp;
  245. const char *format;
  246. va_list ap;
  247. {
  248. #ifdef __UCLIBC_HAS_LONG_LONG__
  249. #define STRTO_L_(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
  250. #define MAX_DIGITS 64
  251. #define UV_TYPE unsigned long long
  252. #define V_TYPE long long
  253. #else
  254. #define STRTO_L_(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
  255. #define MAX_DIGITS 32
  256. #define UV_TYPE unsigned long
  257. #define V_TYPE long
  258. #endif
  259. #ifdef __UCLIBC_HAS_FLOATS__
  260. long double ld;
  261. #endif
  262. UV_TYPE uv;
  263. struct scan_cookie sc;
  264. unsigned const char *fmt;
  265. const char *p;
  266. unsigned char *b;
  267. void *vp;
  268. int cc, i, cnt;
  269. signed char lval;
  270. unsigned char store, usflag, base, invert, r0, r1;
  271. unsigned char buf[MAX_DIGITS+2];
  272. unsigned char scanset[UCHAR_MAX + 1];
  273. __STDIO_THREADLOCK(fp);
  274. init_scan_cookie(&sc,fp);
  275. fmt = (unsigned const char *) format;
  276. cnt = 0;
  277. while (*fmt) {
  278. store = 1;
  279. lval = 0;
  280. sc.width = INT_MAX;
  281. if (*fmt == '%') { /* Conversion specification. */
  282. ++fmt;
  283. if (*fmt == '*') { /* Suppress assignment. */
  284. store = 0;
  285. ++fmt;
  286. }
  287. for (i = 0 ; __isdigit(*fmt) ; sc.width = i) {
  288. i = (i * 10) + (*fmt++ - '0'); /* Get specified width. */
  289. }
  290. for (i = 0 ; i < sizeof(qual) ; i++) { /* Optional qualifier. */
  291. if (qual[i] == *fmt) {
  292. ++fmt;
  293. lval += qsz[i];
  294. if ((i < 2) && (qual[i] == *fmt)) { /* Double h or l. */
  295. ++fmt;
  296. lval += qsz[i];
  297. }
  298. break;
  299. }
  300. }
  301. for (p = spec ; *p ; p++) { /* Process format specifier. */
  302. if (*fmt != *p) continue;
  303. if (p-spec < 1) { /* % - match a '%'*/
  304. goto matchchar;
  305. }
  306. if (p-spec < 2) { /* n - store number of chars read */
  307. *(va_arg(ap, int *)) = sc.nread;
  308. scan_getc_nw(&sc);
  309. goto nextfmt;
  310. }
  311. if (p-spec > 3) { /* skip white space if not c or [ */
  312. do {
  313. i = scan_getc_nw(&sc);
  314. } while (__isspace(i));
  315. scan_ungetc(&sc);
  316. }
  317. if (p-spec < 5) { /* [,c,s - string conversions */
  318. invert = 0;
  319. if (*p == 'c') {
  320. invert = 1;
  321. if (sc.width == INT_MAX) {
  322. sc.width = 1;
  323. }
  324. }
  325. for (i=0 ; i<= UCHAR_MAX ; i++) {
  326. scanset[i] = ((*p == 's') ? (__isspace(i) == 0) : 0);
  327. }
  328. if (*p == '[') { /* need to build a scanset */
  329. if (*++fmt == '^') {
  330. invert = 1;
  331. ++fmt;
  332. }
  333. if (*fmt == ']') {
  334. scanset[(int)']'] = 1;
  335. ++fmt;
  336. }
  337. r0 = 0;
  338. while (*fmt && *fmt !=']') { /* build scanset */
  339. if ((*fmt == '-') && r0 && (fmt[1] != ']')) {
  340. /* range */
  341. ++fmt;
  342. if (*fmt < r0) {
  343. r1 = r0;
  344. r0 = *fmt;
  345. } else {
  346. r1 = *fmt;
  347. }
  348. for (i=r0 ; i<= r1 ; i++) {
  349. scanset[i] = 1;
  350. }
  351. r0 = 0;
  352. } else {
  353. r0 = *fmt;
  354. scanset[r0] = 1;
  355. }
  356. ++fmt;
  357. }
  358. if (!*fmt) { /* format string exhausted! */
  359. goto done;
  360. }
  361. }
  362. /* ok -- back to common work */
  363. if (sc.width <= 0) {
  364. goto done;
  365. }
  366. if (store) {
  367. b = va_arg(ap, unsigned char *);
  368. } else {
  369. b = buf;
  370. }
  371. cc = scan_getc(&sc);
  372. if (cc <= 0) {
  373. scan_ungetc(&sc);
  374. goto done; /* return EOF if cnt == 0 */
  375. }
  376. i = 0;
  377. while ((cc>0) && (scanset[cc] != invert)) {
  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. #ifdef __UCLIBC_HAS_LONG_LONG__
  455. *((unsigned long long *)vp) = uv;
  456. break;
  457. #endif
  458. case 1:
  459. #if ULONG_MAX == UINT_MAX
  460. case 0: /* int and long int are the same */
  461. #endif
  462. #ifdef __UCLIBC_HAS_LONG_LONG__
  463. if (usflag) {
  464. if (uv > ULONG_MAX) {
  465. uv = ULONG_MAX;
  466. }
  467. } else if (((V_TYPE)uv) > LONG_MAX) {
  468. uv = LONG_MAX;
  469. } else if (((V_TYPE)uv) < LONG_MIN) {
  470. uv = (UV_TYPE) LONG_MIN;
  471. }
  472. #endif
  473. *((unsigned long *)vp) = (unsigned long)uv;
  474. break;
  475. #if ULONG_MAX != UINT_MAX
  476. case 0: /* int and long int are different */
  477. if (usflag) {
  478. if (uv > UINT_MAX) {
  479. uv = UINT_MAX;
  480. }
  481. } else if (((V_TYPE)uv) > INT_MAX) {
  482. uv = INT_MAX;
  483. } else if (((V_TYPE)uv) < INT_MIN) {
  484. uv = (UV_TYPE) INT_MIN;
  485. }
  486. *((unsigned int *)vp) = (unsigned int)uv;
  487. break;
  488. #endif
  489. case -1:
  490. if (usflag) {
  491. if (uv > USHRT_MAX) {
  492. uv = USHRT_MAX;
  493. }
  494. } else if (((V_TYPE)uv) > SHRT_MAX) {
  495. uv = SHRT_MAX;
  496. } else if (((V_TYPE)uv) < SHRT_MIN) {
  497. uv = (UV_TYPE) SHRT_MIN;
  498. }
  499. *((unsigned short *)vp) = (unsigned short)uv;
  500. break;
  501. case -2:
  502. if (usflag) {
  503. if (uv > UCHAR_MAX) {
  504. uv = UCHAR_MAX;
  505. }
  506. } else if (((V_TYPE)uv) > CHAR_MAX) {
  507. uv = CHAR_MAX;
  508. } else if (((V_TYPE)uv) < CHAR_MIN) {
  509. uv = (UV_TYPE) CHAR_MIN;
  510. }
  511. *((unsigned char *)vp) = (unsigned char) uv;
  512. break;
  513. default:
  514. assert(0);
  515. }
  516. ++cnt;
  517. }
  518. goto nextfmt;
  519. }
  520. #ifdef __UCLIBC_HAS_FLOATS__
  521. else { /* floating point */
  522. if (sc.width <= 0) {
  523. goto done;
  524. }
  525. if (__strtold(&ld, &sc)) { /* Success! */
  526. if (store) {
  527. vp = va_arg(ap, void *);
  528. switch (lval) {
  529. case 2:
  530. *((long double *)vp) = ld;
  531. break;
  532. case 1:
  533. *((double *)vp) = (double) ld;
  534. break;
  535. case 0:
  536. *((float *)vp) = (float) ld;
  537. break;
  538. default: /* Illegal qualifier! */
  539. assert(0);
  540. goto done;
  541. }
  542. ++cnt;
  543. }
  544. goto nextfmt;
  545. }
  546. }
  547. #else
  548. assert(0);
  549. #endif
  550. goto done;
  551. }
  552. /* Unrecognized specifier! */
  553. goto RETURN_cnt;
  554. } if (__isspace(*fmt)) { /* Consume all whitespace. */
  555. do {
  556. i = scan_getc_nw(&sc);
  557. } while (__isspace(i));
  558. } else { /* Match the current fmt char. */
  559. matchchar:
  560. if (scan_getc_nw(&sc) != *fmt) {
  561. goto done;
  562. }
  563. scan_getc_nw(&sc);
  564. }
  565. nextfmt:
  566. scan_ungetc(&sc);
  567. ++fmt;
  568. }
  569. done: /* end of scan */
  570. kill_scan_cookie(&sc);
  571. if ((sc.ungot_char <= 0) && (cnt == 0) && (*fmt)) {
  572. cnt = EOF;
  573. }
  574. RETURN_cnt:
  575. __STDIO_THREADUNLOCK(fp);
  576. return (cnt);
  577. }
  578. /*****************************************************************************/
  579. #ifdef __UCLIBC_HAS_FLOATS__
  580. #include <float.h>
  581. #define MAX_SIG_DIGITS 20
  582. #define MAX_IGNORED_DIGITS 2000
  583. #define MAX_ALLOWED_EXP (MAX_SIG_DIGITS + MAX_IGNORED_DIGITS + LDBL_MAX_10_EXP)
  584. #if LDBL_DIG > MAX_SIG_DIGITS
  585. #error need to adjust MAX_SIG_DIGITS
  586. #endif
  587. #include <limits.h>
  588. #if MAX_ALLOWED_EXP > INT_MAX
  589. #error size assumption violated for MAX_ALLOWED_EXP
  590. #endif
  591. int __strtold(long double *ld, struct scan_cookie *sc)
  592. {
  593. long double number;
  594. long double p10;
  595. int exponent_power;
  596. int exponent_temp;
  597. int negative;
  598. int num_digits;
  599. int since_decimal;
  600. int c;
  601. c = scan_getc(sc); /* Decrements width. */
  602. negative = 0;
  603. switch(c) { /* Handle optional sign. */
  604. case '-': negative = 1; /* Fall through to get next char. */
  605. case '+': c = scan_getc(sc);
  606. }
  607. number = 0.;
  608. num_digits = -1;
  609. exponent_power = 0;
  610. since_decimal = INT_MIN;
  611. LOOP:
  612. while (__isdigit(c)) { /* Process string of digits. */
  613. ++since_decimal;
  614. if (num_digits < 0) { /* First time through? */
  615. ++num_digits; /* We've now seen a digit. */
  616. }
  617. if (num_digits || (c != '0')) { /* had/have nonzero */
  618. ++num_digits;
  619. if (num_digits <= MAX_SIG_DIGITS) { /* Is digit significant? */
  620. number = number * 10. + (c - '0');
  621. }
  622. }
  623. c = scan_getc(sc);
  624. }
  625. if ((c == '.') && (since_decimal < 0)) { /* If no previous decimal pt, */
  626. since_decimal = 0; /* save position of decimal point */
  627. c = scan_getc(sc); /* and process rest of digits */
  628. goto LOOP;
  629. }
  630. if (num_digits<0) { /* Must have at least one digit. */
  631. goto FAIL;
  632. }
  633. if (num_digits > MAX_SIG_DIGITS) { /* Adjust exp for skipped digits. */
  634. exponent_power += num_digits - MAX_SIG_DIGITS;
  635. }
  636. if (since_decimal >= 0) { /* Adjust exponent for decimal point. */
  637. exponent_power -= since_decimal;
  638. }
  639. if (negative) { /* Correct for sign. */
  640. number = -number;
  641. negative = 0; /* Reset for exponent processing below. */
  642. }
  643. /* Process an exponent string. */
  644. if (c == 'e' || c == 'E') {
  645. c = scan_getc(sc);
  646. switch(c) { /* Handle optional sign. */
  647. case '-': negative = 1; /* Fall through to get next char. */
  648. case '+': c = scan_getc(sc);
  649. }
  650. num_digits = 0;
  651. exponent_temp = 0;
  652. while (__isdigit(c)) { /* Process string of digits. */
  653. if (exponent_temp < MAX_ALLOWED_EXP) { /* overflow check */
  654. exponent_temp = exponent_temp * 10 + (c - '0');
  655. }
  656. c = scan_getc(sc);
  657. ++num_digits;
  658. }
  659. if (num_digits == 0) { /* Were there no exp digits? */
  660. goto FAIL;
  661. } /* else */
  662. if (negative) {
  663. exponent_power -= exponent_temp;
  664. } else {
  665. exponent_power += exponent_temp;
  666. }
  667. }
  668. if (number != 0.) {
  669. /* Now scale the result. */
  670. exponent_temp = exponent_power;
  671. p10 = 10.;
  672. if (exponent_temp < 0) {
  673. exponent_temp = -exponent_temp;
  674. }
  675. while (exponent_temp) {
  676. if (exponent_temp & 1) {
  677. if (exponent_power < 0) {
  678. number /= p10;
  679. } else {
  680. number *= p10;
  681. }
  682. }
  683. exponent_temp >>= 1;
  684. p10 *= p10;
  685. }
  686. }
  687. *ld = number;
  688. return 1;
  689. FAIL:
  690. scan_ungetc(sc);
  691. return 0;
  692. }
  693. #endif /* __UCLIBC_HAS_FLOATS__ */
  694. #endif