scanf.c 18 KB

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