scanf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. #ifdef __UCLIBC_HAS_LONG_LONG__
  169. static const char qual[] = "hl" /* "jtz" */ "Lq";
  170. /* char = -2, short = -1, int = 0, long = 1, long long = 2 */
  171. static const char qsz[] = { -1, 1, 2, 2 };
  172. #else
  173. static const char qual[] = "hl" /* "jtz" */;
  174. static const char qsz[] = { -1, 1, };
  175. #endif
  176. #ifdef __UCLIBC_HAS_FLOATS__
  177. static int __strtold(long double *ld, struct scan_cookie *sc);
  178. /*01234567890123456 */
  179. static const char spec[] = "%n[csoupxXidfeEgG";
  180. #else
  181. static const char spec[] = "%n[csoupxXid";
  182. #endif
  183. /* radix[i] <-> spec[i+5] o u p x X i d */
  184. static const char radix[] = { 8, 10, 16, 16, 16, 0, 10 };
  185. static void init_scan_cookie(register struct scan_cookie *sc,
  186. register FILE *fp)
  187. {
  188. sc->fp = fp;
  189. sc->nread = 0;
  190. sc->width_flag = 0;
  191. sc->ungot_flag = 0;
  192. sc->app_ungot = ((fp->modeflags & __MASK_UNGOT) ? fp->ungot[1] : 0);
  193. }
  194. /* TODO -- what about literal '\0' chars in a file??? */
  195. static int scan_getc_nw(register struct scan_cookie *sc)
  196. {
  197. if (sc->ungot_flag == 0) {
  198. sc->ungot_char = getc(sc->fp);
  199. } else {
  200. sc->ungot_flag = 0;
  201. }
  202. if (sc->ungot_char > 0) {
  203. ++sc->nread;
  204. }
  205. sc->width_flag = 0;
  206. return sc->ungot_char;
  207. }
  208. static int scan_getc(register struct scan_cookie *sc)
  209. {
  210. if (sc->ungot_flag == 0) {
  211. sc->ungot_char = getc(sc->fp);
  212. }
  213. sc->width_flag = 1;
  214. if (--sc->width < 0) {
  215. sc->ungot_flag = 1;
  216. return 0;
  217. }
  218. sc->ungot_flag = 0;
  219. if (sc->ungot_char > 0) {
  220. ++sc->nread;
  221. }
  222. return sc->ungot_char;
  223. }
  224. static void scan_ungetc(register struct scan_cookie *sc)
  225. {
  226. if (sc->ungot_flag != 0) {
  227. assert(sc->width < 0);
  228. return;
  229. }
  230. if (sc->width_flag) {
  231. ++sc->width;
  232. }
  233. sc->ungot_flag = 1;
  234. if (sc->ungot_char > 0) { /* not EOF or EOS */
  235. --sc->nread;
  236. }
  237. }
  238. static void kill_scan_cookie(register struct scan_cookie *sc)
  239. {
  240. if (sc->ungot_flag) {
  241. ungetc(sc->ungot_char,sc->fp);
  242. /* Deal with distiction between user and scanf ungots. */
  243. if (sc->nread == 0) { /* Only one char was read... app ungot? */
  244. sc->fp->ungot[1] = sc->app_ungot; /* restore ungot state. */
  245. }
  246. }
  247. }
  248. int vfscanf(FILE *fp, const char *format, va_list ap)
  249. {
  250. #ifdef __UCLIBC_HAS_LONG_LONG__
  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. #else
  256. #define STRTO_L_(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
  257. #define MAX_DIGITS 32
  258. #define UV_TYPE unsigned long
  259. #define V_TYPE long
  260. #endif
  261. #ifdef __UCLIBC_HAS_FLOATS__
  262. long double ld;
  263. #endif
  264. UV_TYPE uv;
  265. struct scan_cookie sc;
  266. register unsigned const char *fmt;
  267. const char *p;
  268. register unsigned char *b;
  269. void *vp;
  270. int cc, i, cnt;
  271. signed char lval;
  272. unsigned char store, usflag, base, invert, r0, r1;
  273. unsigned char buf[MAX_DIGITS+2];
  274. unsigned char scanset[UCHAR_MAX + 1];
  275. __STDIO_THREADLOCK(fp);
  276. init_scan_cookie(&sc,fp);
  277. fmt = (unsigned const char *) format;
  278. cnt = 0;
  279. while (*fmt) {
  280. store = 1;
  281. lval = 0;
  282. sc.width = INT_MAX;
  283. if (*fmt == '%') { /* Conversion specification. */
  284. ++fmt;
  285. if (*fmt == '*') { /* Suppress assignment. */
  286. store = 0;
  287. ++fmt;
  288. }
  289. for (i = 0 ; __isdigit(*fmt) ; sc.width = i) {
  290. i = (i * 10) + (*fmt++ - '0'); /* Get specified width. */
  291. }
  292. for (i = 0 ; i < sizeof(qual) ; i++) { /* Optional qualifier. */
  293. if (qual[i] == *fmt) {
  294. ++fmt;
  295. lval += qsz[i];
  296. if ((i < 2) && (qual[i] == *fmt)) { /* Double h or l. */
  297. ++fmt;
  298. lval += qsz[i];
  299. }
  300. break;
  301. }
  302. }
  303. for (p = spec ; *p ; p++) { /* Process format specifier. */
  304. if (*fmt != *p) continue;
  305. if (p-spec < 1) { /* % - match a '%'*/
  306. goto matchchar;
  307. }
  308. if (p-spec < 2) { /* n - store number of chars read */
  309. *(va_arg(ap, int *)) = sc.nread;
  310. scan_getc_nw(&sc);
  311. goto nextfmt;
  312. }
  313. if (p-spec > 3) { /* skip white space if not c or [ */
  314. do {
  315. i = scan_getc_nw(&sc);
  316. } while (__isspace(i));
  317. scan_ungetc(&sc);
  318. }
  319. if (p-spec < 5) { /* [,c,s - string conversions */
  320. invert = 0;
  321. if (*p == 'c') {
  322. invert = 1;
  323. if (sc.width == INT_MAX) {
  324. sc.width = 1;
  325. }
  326. }
  327. for (i=0 ; i<= UCHAR_MAX ; i++) {
  328. scanset[i] = ((*p == 's') ? (__isspace(i) == 0) : 0);
  329. }
  330. if (*p == '[') { /* need to build a scanset */
  331. if (*++fmt == '^') {
  332. invert = 1;
  333. ++fmt;
  334. }
  335. if (*fmt == ']') {
  336. scanset[(int)']'] = 1;
  337. ++fmt;
  338. }
  339. r0 = 0;
  340. while (*fmt && *fmt !=']') { /* build scanset */
  341. if ((*fmt == '-') && r0 && (fmt[1] != ']')) {
  342. /* range */
  343. ++fmt;
  344. if (*fmt < r0) {
  345. r1 = r0;
  346. r0 = *fmt;
  347. } else {
  348. r1 = *fmt;
  349. }
  350. for (i=r0 ; i<= r1 ; i++) {
  351. scanset[i] = 1;
  352. }
  353. r0 = 0;
  354. } else {
  355. r0 = *fmt;
  356. scanset[r0] = 1;
  357. }
  358. ++fmt;
  359. }
  360. if (!*fmt) { /* format string exhausted! */
  361. goto done;
  362. }
  363. }
  364. /* ok -- back to common work */
  365. if (sc.width <= 0) {
  366. goto done;
  367. }
  368. if (store) {
  369. b = va_arg(ap, unsigned char *);
  370. } else {
  371. b = buf;
  372. }
  373. cc = scan_getc(&sc);
  374. if (cc <= 0) {
  375. scan_ungetc(&sc);
  376. goto done; /* return EOF if cnt == 0 */
  377. }
  378. i = 0;
  379. while ((cc>0) && (scanset[cc] != invert)) {
  380. i = 1; /* yes, we stored something */
  381. *b = cc;
  382. b += store;
  383. cc = scan_getc(&sc);
  384. }
  385. if (i==0) {
  386. scan_ungetc(&sc);
  387. goto done; /* return cnt */
  388. }
  389. if (*p != 'c') { /* nul-terminate the stored string */
  390. *b = 0;
  391. }
  392. cnt += store;
  393. goto nextfmt;
  394. }
  395. if (p-spec < 12) { /* o,u,p,x,X,i,d - (un)signed integer */
  396. if (*p == 'p') {
  397. /* assume pointer same size as int or long. */
  398. lval = (sizeof(char *) == sizeof(long));
  399. }
  400. usflag = ((p-spec) < 10); /* (1)0 if (un)signed */
  401. base = radix[(int)(p-spec) - 5];
  402. b = buf;
  403. if (sc.width <= 0) {
  404. goto done;
  405. }
  406. cc = scan_getc(&sc);
  407. if ((cc == '+') || (cc == '-')) { /* Handle leading sign.*/
  408. *b++ = cc;
  409. cc = scan_getc(&sc);
  410. }
  411. if (cc == '0') { /* Possibly set base and handle prefix. */
  412. if ((base == 0) || (base == 16)) {
  413. cc = scan_getc(&sc);
  414. if ((cc == 'x') || (cc == 'X')) {
  415. /* We're committed to base 16 now. */
  416. base = 16;
  417. cc = scan_getc(&sc);
  418. } else { /* oops... back up */
  419. scan_ungetc(&sc);
  420. cc = '0';
  421. if (base == 0) {
  422. base = 8;
  423. }
  424. }
  425. }
  426. }
  427. if (base == 0) { /* Default to base 10 */
  428. base = 10;
  429. }
  430. /* At this point, we're ready to start reading digits. */
  431. if (cc == '0') {
  432. *b++ = cc; /* Store first leading 0 */
  433. do { /* but ignore others. */
  434. cc = scan_getc(&sc);
  435. } while (cc == '0');
  436. }
  437. while (valid_digit(cc,base)) { /* Now for nonzero digits.*/
  438. if (b - buf < MAX_DIGITS) {
  439. *b++ = cc;
  440. }
  441. cc = scan_getc(&sc);
  442. }
  443. *b = 0; /* null-terminate */
  444. if ((b == buf) || (*--b == '+') || (*b == '-')) {
  445. scan_ungetc(&sc);
  446. goto done; /* No digits! */
  447. }
  448. if (store) {
  449. if (*buf == '-') {
  450. usflag = 0;
  451. }
  452. uv = STRTO_L_(buf, NULL, base, 1-usflag);
  453. vp = va_arg(ap, void *);
  454. switch (lval) {
  455. case 2: /* If no long long, treat as long . */
  456. #ifdef __UCLIBC_HAS_LONG_LONG__
  457. *((unsigned long long *)vp) = uv;
  458. break;
  459. #endif
  460. case 1:
  461. #if ULONG_MAX == UINT_MAX
  462. case 0: /* int and long int are the same */
  463. #endif
  464. #ifdef __UCLIBC_HAS_LONG_LONG__
  465. if (usflag) {
  466. if (uv > ULONG_MAX) {
  467. uv = ULONG_MAX;
  468. }
  469. } else if (((V_TYPE)uv) > LONG_MAX) {
  470. uv = LONG_MAX;
  471. } else if (((V_TYPE)uv) < LONG_MIN) {
  472. uv = (UV_TYPE) LONG_MIN;
  473. }
  474. #endif
  475. *((unsigned long *)vp) = (unsigned long)uv;
  476. break;
  477. #if ULONG_MAX != UINT_MAX
  478. case 0: /* int and long int are different */
  479. if (usflag) {
  480. if (uv > UINT_MAX) {
  481. uv = UINT_MAX;
  482. }
  483. } else if (((V_TYPE)uv) > INT_MAX) {
  484. uv = INT_MAX;
  485. } else if (((V_TYPE)uv) < INT_MIN) {
  486. uv = (UV_TYPE) INT_MIN;
  487. }
  488. *((unsigned int *)vp) = (unsigned int)uv;
  489. break;
  490. #endif
  491. case (signed char)(-1):
  492. if (usflag) {
  493. if (uv > USHRT_MAX) {
  494. uv = USHRT_MAX;
  495. }
  496. } else if (((V_TYPE)uv) > SHRT_MAX) {
  497. uv = SHRT_MAX;
  498. } else if (((V_TYPE)uv) < SHRT_MIN) {
  499. uv = (UV_TYPE) SHRT_MIN;
  500. }
  501. *((unsigned short *)vp) = (unsigned short)uv;
  502. break;
  503. case (signed char)(-2):
  504. if (usflag) {
  505. if (uv > UCHAR_MAX) {
  506. uv = UCHAR_MAX;
  507. }
  508. } else if (((V_TYPE)uv) > CHAR_MAX) {
  509. uv = CHAR_MAX;
  510. } else if (((V_TYPE)uv) < CHAR_MIN) {
  511. uv = (UV_TYPE) CHAR_MIN;
  512. }
  513. *((unsigned char *)vp) = (unsigned char) uv;
  514. break;
  515. default:
  516. assert(0);
  517. }
  518. ++cnt;
  519. }
  520. goto nextfmt;
  521. }
  522. #ifdef __UCLIBC_HAS_FLOATS__
  523. else { /* floating point */
  524. if (sc.width <= 0) {
  525. goto done;
  526. }
  527. if (__strtold(&ld, &sc)) { /* Success! */
  528. if (store) {
  529. vp = va_arg(ap, void *);
  530. switch (lval) {
  531. case 2:
  532. *((long double *)vp) = ld;
  533. break;
  534. case 1:
  535. *((double *)vp) = (double) ld;
  536. break;
  537. case 0:
  538. *((float *)vp) = (float) ld;
  539. break;
  540. default: /* Illegal qualifier! */
  541. assert(0);
  542. goto done;
  543. }
  544. ++cnt;
  545. }
  546. goto nextfmt;
  547. }
  548. }
  549. #else
  550. assert(0);
  551. #endif
  552. goto done;
  553. }
  554. /* Unrecognized specifier! */
  555. goto RETURN_cnt;
  556. } if (__isspace(*fmt)) { /* Consume all whitespace. */
  557. do {
  558. i = scan_getc_nw(&sc);
  559. } while (__isspace(i));
  560. } else { /* Match the current fmt char. */
  561. matchchar:
  562. if (scan_getc_nw(&sc) != *fmt) {
  563. goto done;
  564. }
  565. scan_getc_nw(&sc);
  566. }
  567. nextfmt:
  568. scan_ungetc(&sc);
  569. ++fmt;
  570. }
  571. done: /* end of scan */
  572. kill_scan_cookie(&sc);
  573. if ((sc.ungot_char <= 0) && (cnt == 0) && (*fmt)) {
  574. cnt = EOF;
  575. }
  576. RETURN_cnt:
  577. __STDIO_THREADUNLOCK(fp);
  578. return (cnt);
  579. }
  580. /*****************************************************************************/
  581. #ifdef __UCLIBC_HAS_FLOATS__
  582. #include <float.h>
  583. #define MAX_SIG_DIGITS 20
  584. #define MAX_IGNORED_DIGITS 2000
  585. #define MAX_ALLOWED_EXP (MAX_SIG_DIGITS + MAX_IGNORED_DIGITS + LDBL_MAX_10_EXP)
  586. #if LDBL_DIG > MAX_SIG_DIGITS
  587. #error need to adjust MAX_SIG_DIGITS
  588. #endif
  589. #include <limits.h>
  590. #if MAX_ALLOWED_EXP > INT_MAX
  591. #error size assumption violated for MAX_ALLOWED_EXP
  592. #endif
  593. int __strtold(long double *ld, struct scan_cookie *sc)
  594. {
  595. long double number;
  596. long double p10;
  597. int exponent_power;
  598. int exponent_temp;
  599. int negative;
  600. int num_digits;
  601. int since_decimal;
  602. int c;
  603. c = scan_getc(sc); /* Decrements width. */
  604. negative = 0;
  605. switch(c) { /* Handle optional sign. */
  606. case '-': negative = 1; /* Fall through to get next char. */
  607. case '+': c = scan_getc(sc);
  608. }
  609. number = 0.;
  610. num_digits = -1;
  611. exponent_power = 0;
  612. since_decimal = INT_MIN;
  613. LOOP:
  614. while (__isdigit(c)) { /* Process string of digits. */
  615. ++since_decimal;
  616. if (num_digits < 0) { /* First time through? */
  617. ++num_digits; /* We've now seen a digit. */
  618. }
  619. if (num_digits || (c != '0')) { /* had/have nonzero */
  620. ++num_digits;
  621. if (num_digits <= MAX_SIG_DIGITS) { /* Is digit significant? */
  622. number = number * 10. + (c - '0');
  623. }
  624. }
  625. c = scan_getc(sc);
  626. }
  627. if ((c == '.') && (since_decimal < 0)) { /* If no previous decimal pt, */
  628. since_decimal = 0; /* save position of decimal point */
  629. c = scan_getc(sc); /* and process rest of digits */
  630. goto LOOP;
  631. }
  632. if (num_digits<0) { /* Must have at least one digit. */
  633. goto FAIL;
  634. }
  635. if (num_digits > MAX_SIG_DIGITS) { /* Adjust exp for skipped digits. */
  636. exponent_power += num_digits - MAX_SIG_DIGITS;
  637. }
  638. if (since_decimal >= 0) { /* Adjust exponent for decimal point. */
  639. exponent_power -= since_decimal;
  640. }
  641. if (negative) { /* Correct for sign. */
  642. number = -number;
  643. negative = 0; /* Reset for exponent processing below. */
  644. }
  645. /* Process an exponent string. */
  646. if (c == 'e' || c == 'E') {
  647. c = scan_getc(sc);
  648. switch(c) { /* Handle optional sign. */
  649. case '-': negative = 1; /* Fall through to get next char. */
  650. case '+': c = scan_getc(sc);
  651. }
  652. num_digits = 0;
  653. exponent_temp = 0;
  654. while (__isdigit(c)) { /* Process string of digits. */
  655. if (exponent_temp < MAX_ALLOWED_EXP) { /* overflow check */
  656. exponent_temp = exponent_temp * 10 + (c - '0');
  657. }
  658. c = scan_getc(sc);
  659. ++num_digits;
  660. }
  661. if (num_digits == 0) { /* Were there no exp digits? */
  662. goto FAIL;
  663. } /* else */
  664. if (negative) {
  665. exponent_power -= exponent_temp;
  666. } else {
  667. exponent_power += exponent_temp;
  668. }
  669. }
  670. if (number != 0.) {
  671. /* Now scale the result. */
  672. exponent_temp = exponent_power;
  673. p10 = 10.;
  674. if (exponent_temp < 0) {
  675. exponent_temp = -exponent_temp;
  676. }
  677. while (exponent_temp) {
  678. if (exponent_temp & 1) {
  679. if (exponent_power < 0) {
  680. number /= p10;
  681. } else {
  682. number *= p10;
  683. }
  684. }
  685. exponent_temp >>= 1;
  686. p10 *= p10;
  687. }
  688. }
  689. *ld = number;
  690. return 1;
  691. FAIL:
  692. scan_ungetc(sc);
  693. return 0;
  694. }
  695. #endif /* __UCLIBC_HAS_FLOATS__ */
  696. #endif