scanf.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  1. /* Copyright (C) 2002, 2003 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* Aug 1, 2003
  18. * New *scanf implementation with lots of bug fixes and *wscanf support.
  19. * Also now optionally supports hexadecimal float notation, positional
  20. * args, and glibc locale-specific digit grouping. Should now be
  21. * standards compliant.
  22. *
  23. * Aug 18, 2003
  24. * Bug fix: scanf %lc,%ls,%l[ would always set mb_fail on eof or error,
  25. * even when just starting a new mb char.
  26. * Bug fix: wscanf would incorrectly unget in certain situations.
  27. */
  28. #define _ISOC99_SOURCE /* for LLONG_MAX primarily... */
  29. #define _GNU_SOURCE
  30. #define _STDIO_UTILITY
  31. #include <features.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. #include <ctype.h>
  36. #include <string.h>
  37. #include <stdarg.h>
  38. #include <stdint.h>
  39. #include <errno.h>
  40. #include <printf.h>
  41. #ifdef __UCLIBC_HAS_WCHAR__
  42. #include <bits/uClibc_uwchar.h>
  43. #include <wchar.h>
  44. #include <wctype.h>
  45. #endif /* __UCLIBC_HAS_WCHAR__ */
  46. #include <langinfo.h>
  47. #include <locale.h>
  48. #include <assert.h>
  49. #include <limits.h>
  50. #ifdef __STDIO_THREADSAFE
  51. #include <stdio_ext.h>
  52. #include <pthread.h>
  53. #endif /* __STDIO_THREADSAFE */
  54. #ifdef __UCLIBC_HAS_FLOATS__
  55. #include <float.h>
  56. #include <bits/uClibc_fpmax.h>
  57. #endif /* __UCLIBC_HAS_FLOATS__ */
  58. #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  59. #ifdef L_vfscanf
  60. /* only emit this once */
  61. #warning Forcing undef of __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ until implemented!
  62. #endif
  63. #undef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  64. #endif
  65. extern void _store_inttype(void *dest, int desttype, uintmax_t val);
  66. #ifdef LLONG_MAX
  67. extern unsigned long long
  68. _stdlib_strto_ll(register const char * __restrict str,
  69. char ** __restrict endptr, int base, int sflag);
  70. #if (ULLONG_MAX == UINTMAX_MAX)
  71. #define STRTOUIM(s,e,b,sf) _stdlib_strto_ll(s,e,b,sf)
  72. #endif
  73. #else /* LLONG_MAX */
  74. extern unsigned long
  75. _stdlib_strto_l(register const char * __restrict str,
  76. char ** __restrict endptr, int base, int sflag);
  77. #if (ULONG_MAX == UINTMAX_MAX)
  78. #define STRTOUIM(s,e,b,sf) _stdlib_strto_l(s,e,b,sf)
  79. #endif
  80. #endif /* LLONG_MAX */
  81. #ifndef STRTOUIM
  82. #error STRTOUIM conversion function is undefined!
  83. #endif
  84. /**********************************************************************/
  85. /* The standards require EOF < 0. */
  86. #if EOF >= CHAR_MIN
  87. #define __isdigit_char_or_EOF(C) __isdigit_char((C))
  88. #else
  89. #define __isdigit_char_or_EOF(C) __isdigit_int((C))
  90. #endif
  91. /**********************************************************************/
  92. #ifdef L_fscanf
  93. int fscanf(FILE * __restrict stream, const char * __restrict format, ...)
  94. {
  95. va_list arg;
  96. int rv;
  97. va_start(arg, format);
  98. rv = vfscanf(stream, format, arg);
  99. va_end(arg);
  100. return rv;
  101. }
  102. #endif
  103. /**********************************************************************/
  104. #ifdef L_scanf
  105. int scanf(const char * __restrict format, ...)
  106. {
  107. va_list arg;
  108. int rv;
  109. va_start(arg, format);
  110. rv = vfscanf(stdin, format, arg);
  111. va_end(arg);
  112. return rv;
  113. }
  114. #endif
  115. /**********************************************************************/
  116. #ifdef L_sscanf
  117. #if defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS)
  118. int sscanf(const char * __restrict str, const char * __restrict format, ...)
  119. {
  120. va_list arg;
  121. int rv;
  122. va_start(arg, format);
  123. rv = vsscanf(str, format, arg);
  124. va_end(arg);
  125. return rv;
  126. }
  127. #else /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  128. #warning Skipping sscanf since no buffering and no custom streams!
  129. #endif /* defined(__STDIO_BUFFERS) || defined(__STDIO_GLIBC_CUSTOM_STREAMS) */
  130. #endif
  131. /**********************************************************************/
  132. #ifdef L_vscanf
  133. int vscanf(const char * __restrict format, va_list arg)
  134. {
  135. return vfscanf(stdin, format, arg);
  136. }
  137. #endif
  138. /**********************************************************************/
  139. #ifdef L_vsscanf
  140. #ifdef __UCLIBC_MJN3_ONLY__
  141. #warning WISHLIST: Implement vsscanf for non-buffered and no custom stream case.
  142. #endif /* __UCLIBC_MJN3_ONLY__ */
  143. #ifdef __STDIO_BUFFERS
  144. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  145. {
  146. FILE string[1];
  147. string->filedes = -2;
  148. string->modeflags = (__FLAG_NARROW|__FLAG_READONLY);
  149. string->bufstart = string->bufpos = (unsigned char *) ((void *) sp);
  150. #ifdef __STDIO_GETC_MACRO
  151. string->bufgetc =
  152. #endif /* __STDIO_GETC_MACRO */
  153. string->bufread = string->bufstart + strlen(sp);
  154. #ifdef __STDIO_MBSTATE
  155. __INIT_MBSTATE(&(string->state));
  156. #endif /* __STDIO_MBSTATE */
  157. #ifdef __STDIO_THREADSAFE
  158. string->user_locking = 0;
  159. __stdio_init_mutex(&string->lock);
  160. #endif
  161. return vfscanf(string, fmt, ap);
  162. }
  163. #else /* __STDIO_BUFFERS */
  164. #ifdef __STDIO_GLIBC_CUSTOM_STREAMS
  165. int vsscanf(__const char *sp, __const char *fmt, va_list ap)
  166. {
  167. FILE *f;
  168. int rv;
  169. if ((f = fmemopen((char *)sp, strlen(sp), "r")) == NULL) {
  170. return -1;
  171. }
  172. rv = vfscanf(f, fmt, ap);
  173. fclose(f);
  174. return rv;
  175. }
  176. #else /* __STDIO_GLIBC_CUSTOM_STREAMS */
  177. #warning Skipping vsscanf since no buffering and no custom streams!
  178. #endif /* __STDIO_GLIBC_CUSTOM_STREAMS */
  179. #endif /* __STDIO_BUFFERS */
  180. #endif
  181. /**********************************************************************/
  182. #ifdef L_fwscanf
  183. int fwscanf(FILE * __restrict stream, const wchar_t * __restrict format, ...)
  184. {
  185. va_list arg;
  186. int rv;
  187. va_start(arg, format);
  188. rv = vfwscanf(stream, format, arg);
  189. va_end(arg);
  190. return rv;
  191. }
  192. #endif
  193. /**********************************************************************/
  194. #ifdef L_wscanf
  195. int wscanf(const wchar_t * __restrict format, ...)
  196. {
  197. va_list arg;
  198. int rv;
  199. va_start(arg, format);
  200. rv = vfwscanf(stdin, format, arg);
  201. va_end(arg);
  202. return rv;
  203. }
  204. #endif
  205. /**********************************************************************/
  206. #ifdef L_swscanf
  207. #ifdef __STDIO_BUFFERS
  208. int swscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
  209. ...)
  210. {
  211. va_list arg;
  212. int rv;
  213. va_start(arg, format);
  214. rv = vswscanf(str, format, arg);
  215. va_end(arg);
  216. return rv;
  217. }
  218. #else /* __STDIO_BUFFERS */
  219. #warning Skipping swscanf since no buffering!
  220. #endif /* __STDIO_BUFFERS */
  221. #endif
  222. /**********************************************************************/
  223. #ifdef L_vwscanf
  224. int vwscanf(const wchar_t * __restrict format, va_list arg)
  225. {
  226. return vfwscanf(stdin, format, arg);
  227. }
  228. #endif
  229. /**********************************************************************/
  230. #ifdef L_vswscanf
  231. #ifdef __STDIO_BUFFERS
  232. int vswscanf(const wchar_t * __restrict str, const wchar_t * __restrict format,
  233. va_list arg)
  234. {
  235. FILE f;
  236. f.filedes = -3; /* FAKE STREAM TO SUPPORT *wscanf! */
  237. f.modeflags = (__FLAG_WIDE|__FLAG_READONLY|__FLAG_READING);
  238. f.bufpos = (char *) str;
  239. f.bufend = (char *)(str + wcslen(str));
  240. f.ungot_width[0] = 0;
  241. #ifdef __STDIO_THREADSAFE
  242. f.user_locking = 0;
  243. __stdio_init_mutex(&f.lock);
  244. #endif
  245. return vfwscanf(&f, format, arg);
  246. }
  247. #else /* __STDIO_BUFFERS */
  248. #warning Skipping vswscanf since no buffering!
  249. #endif /* __STDIO_BUFFERS */
  250. #endif
  251. /**********************************************************************/
  252. /**********************************************************************/
  253. /* float layout 0123456789012345678901 repeat n for "l[" */
  254. #define SPEC_CHARS "npxXoudifFeEgGaACSncs["
  255. /* npxXoudif eEgG CS cs[ */
  256. /* NOTE: Ordering is important! In particular, CONV_LEFTBRACKET
  257. * must immediately precede CONV_c. */
  258. enum {
  259. CONV_n = 0,
  260. CONV_p,
  261. CONV_x, CONV_X, CONV_o, CONV_u, CONV_d, CONV_i,
  262. CONV_f, CONV_F, CONV_e, CONV_E, CONV_g, CONV_G, CONV_a, CONV_A,
  263. CONV_C, CONV_S, CONV_LEFTBRACKET, CONV_c, CONV_s, CONV_leftbracket,
  264. CONV_percent, CONV_whitespace /* not in SPEC_* and no flags */
  265. };
  266. #ifdef __UCLIBC_HAS_FLOATS__
  267. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  268. /* p x X o u d i f F e E g G a A */
  269. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  270. #else
  271. /* p x X o u d i f F e E g G a A */
  272. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10 }
  273. #endif
  274. #else /* __UCLIBC_HAS_FLOATS__ */
  275. /* p x X o u d i f F e E g G a A */
  276. #define SPEC_BASE { 16, 16, 16, 8, 10, 10, 0 }
  277. #endif /* __UCLIBC_HAS_FLOATS__ */
  278. #ifdef __UCLIBC_MJN3_ONLY__
  279. #ifdef L_vfscanf
  280. /* emit once */
  281. #warning CONSIDER: Add a '0' flag to eat 0 padding when grouping?
  282. #endif
  283. #endif /* __UCLIBC_MJN3_ONLY__ */
  284. #define SPEC_FLAGS "*'I"
  285. enum {
  286. FLAG_SURPRESS = 0x10, /* MUST BE 1ST!! See DO_FLAGS. */
  287. FLAG_THOUSANDS = 0x20,
  288. FLAG_I18N = 0x40, /* only works for d, i, u */
  289. FLAG_MALLOC = 0x80, /* only works for s, S, and [ (and l[)*/
  290. };
  291. #define SPEC_RANGES { CONV_n, CONV_p, CONV_i, CONV_A, \
  292. CONV_C, CONV_LEFTBRACKET, \
  293. CONV_c, CONV_leftbracket }
  294. /* Note: We treat L and ll as synonymous... for ints and floats. */
  295. #define SPEC_ALLOWED_FLAGS { \
  296. /* n */ (0x0f|FLAG_SURPRESS), \
  297. /* p */ ( 0|FLAG_SURPRESS), \
  298. /* oxXudi */ (0x0f|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
  299. /* fFeEgGaA */ (0x0c|FLAG_SURPRESS|FLAG_THOUSANDS|FLAG_I18N), \
  300. /* C */ ( 0|FLAG_SURPRESS), \
  301. /* S and l[ */ ( 0|FLAG_SURPRESS|FLAG_MALLOC), \
  302. /* c */ (0x04|FLAG_SURPRESS), \
  303. /* s and [ */ (0x04|FLAG_SURPRESS|FLAG_MALLOC), \
  304. }
  305. /**********************************************************************/
  306. /*
  307. * In order to ease translation to what arginfo and _print_info._flags expect,
  308. * we map: 0:int 1:char 2:longlong 4:long 8:short
  309. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  310. */
  311. /* TODO -- Fix the table below to take into account stdint.h. */
  312. /* #ifndef LLONG_MAX */
  313. /* #error fix QUAL_CHARS for no long long! Affects 'L', 'j', 'q', 'll'. */
  314. /* #else */
  315. /* #if LLONG_MAX != INTMAX_MAX */
  316. /* #error fix QUAL_CHARS intmax_t entry 'j'! */
  317. /* #endif */
  318. /* #endif */
  319. #ifdef PDS
  320. #error PDS already defined!
  321. #endif
  322. #ifdef SS
  323. #error SS already defined!
  324. #endif
  325. #ifdef IMS
  326. #error IMS already defined!
  327. #endif
  328. #if PTRDIFF_MAX == INT_MAX
  329. #define PDS 0
  330. #elif PTRDIFF_MAX == LONG_MAX
  331. #define PDS 4
  332. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  333. #define PDS 8
  334. #else
  335. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  336. #endif
  337. #if SIZE_MAX == UINT_MAX
  338. #define SS 0
  339. #elif SIZE_MAX == ULONG_MAX
  340. #define SS 4
  341. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  342. #define SS 8
  343. #else
  344. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  345. #endif
  346. #if INTMAX_MAX == INT_MAX
  347. #define IMS 0
  348. #elif INTMAX_MAX == LONG_MAX
  349. #define IMS 4
  350. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  351. #define IMS 8
  352. #else
  353. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  354. #endif
  355. #define QUAL_CHARS { \
  356. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int q:long_long */ \
  357. 'h', 'l', 'L', 'j', 'z', 't', 'q', 0, \
  358. 2, 4, 8, IMS, SS, PDS, 8, 0, /* TODO -- fix!!! */\
  359. 1, 8 }
  360. /**********************************************************************/
  361. #ifdef L_vfwscanf
  362. #if WINT_MIN > EOF
  363. #error Unfortunately, we currently need wint_t to be able to store EOF. Sorry.
  364. #endif
  365. #define W_EOF WEOF
  366. #define Wint wint_t
  367. #define Wchar wchar_t
  368. #define Wuchar __uwchar_t
  369. #define ISSPACE(C) iswspace((C))
  370. #define VFSCANF vfwscanf
  371. #define GETC(SC) (SC)->sc_getc((SC))
  372. #else
  373. typedef unsigned char __uchar_t;
  374. #define W_EOF EOF
  375. #define Wint int
  376. #define Wchar char
  377. #define Wuchar __uchar_t
  378. #define ISSPACE(C) isspace((C))
  379. #define VFSCANF vfscanf
  380. #ifdef __UCLIBC_HAS_WCHAR__
  381. #define GETC(SC) (SC)->sc_getc((SC))
  382. #else /* __UCLIBC_HAS_WCHAR__ */
  383. #define GETC(SC) getc_unlocked((SC)->fp)
  384. #endif /* __UCLIBC_HAS_WCHAR__ */
  385. #endif
  386. struct scan_cookie {
  387. Wint cc;
  388. Wint ungot_char;
  389. FILE *fp;
  390. int nread;
  391. int width;
  392. #ifdef __UCLIBC_HAS_WCHAR__
  393. wchar_t app_ungot; /* Match FILE struct member type. */
  394. unsigned char ungot_wchar_width;
  395. #else /* __UCLIBC_HAS_WCHAR__ */
  396. unsigned char app_ungot; /* Match FILE struct member type. */
  397. #endif /* __UCLIBC_HAS_WCHAR__ */
  398. char ungot_flag;
  399. #ifdef __UCLIBC_HAS_WCHAR__
  400. char ungot_wflag; /* vfwscanf */
  401. char mb_fail; /* vfscanf */
  402. mbstate_t mbstate; /* vfscanf */
  403. wint_t wc;
  404. wint_t ungot_wchar; /* to support __scan_getc */
  405. int (*sc_getc)(struct scan_cookie *);
  406. #endif /* __UCLIBC_HAS_WCHAR__ */
  407. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  408. const char *grouping;
  409. const unsigned char *thousands_sep;
  410. int tslen;
  411. #ifdef __UCLIBC_HAS_WCHAR__
  412. wchar_t thousands_sep_wc;
  413. #endif /* __UCLIBC_HAS_WCHAR__ */
  414. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  415. #ifdef __UCLIBC_HAS_FLOATS__
  416. const unsigned char *decpt;
  417. int decpt_len;
  418. #ifdef __UCLIBC_HAS_WCHAR__
  419. wchar_t decpt_wc;
  420. #endif /* __UCLIBC_HAS_WCHAR__ */
  421. const unsigned char *fake_decpt;
  422. #endif /* __UCLIBC_HAS_FLOATS__ */
  423. };
  424. typedef struct {
  425. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  426. #if NL_ARGMAX > 10
  427. #warning NL_ARGMAX > 10, and space is allocated on the stack for positional args.
  428. #endif
  429. void *pos_args[NL_ARGMAX];
  430. int num_pos_args; /* Must start at -1. */
  431. int cur_pos_arg;
  432. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  433. void *cur_ptr;
  434. const unsigned char *fmt;
  435. int cnt, dataargtype, conv_num, max_width;
  436. unsigned char store, flags;
  437. } psfs_t; /* parse scanf format state */
  438. /**********************************************************************/
  439. /**********************************************************************/
  440. extern void __init_scan_cookie(register struct scan_cookie *sc,
  441. register FILE *fp);
  442. extern int __scan_getc(register struct scan_cookie *sc);
  443. extern void __scan_ungetc(register struct scan_cookie *sc);
  444. #ifdef __UCLIBC_HAS_FLOATS__
  445. extern int __scan_strtold(long double *ld, struct scan_cookie *sc);
  446. #endif /* __UCLIBC_HAS_FLOATS__ */
  447. extern int __psfs_parse_spec(psfs_t *psfs);
  448. extern int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc);
  449. /**********************************************************************/
  450. #ifdef L___scan_cookie
  451. #ifdef __UCLIBC_MJN3_ONLY__
  452. #warning TODO: Remove dependence on decpt_str and fake_decpt in stub locale mode.
  453. #endif
  454. #ifndef __UCLIBC_HAS_LOCALE__
  455. static const char decpt_str[] = ".";
  456. #endif
  457. void __init_scan_cookie(register struct scan_cookie *sc,
  458. register FILE *fp)
  459. {
  460. sc->fp = fp;
  461. sc->nread = 0;
  462. sc->ungot_flag = 0;
  463. sc->app_ungot = ((fp->modeflags & __MASK_UNGOT) ? fp->ungot[1] : 0);
  464. #ifdef __UCLIBC_HAS_WCHAR__
  465. sc->ungot_wflag = 0; /* vfwscanf */
  466. sc->mb_fail = 0;
  467. #endif /* __UCLIBC_HAS_WCHAR__ */
  468. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  469. if (*(sc->grouping = __UCLIBC_CURLOCALE_DATA.grouping)) {
  470. sc->thousands_sep = __UCLIBC_CURLOCALE_DATA.thousands_sep;
  471. sc->tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
  472. #ifdef __UCLIBC_HAS_WCHAR__
  473. sc->thousands_sep_wc = __UCLIBC_CURLOCALE_DATA.thousands_sep_wc;
  474. #endif /* __UCLIBC_HAS_WCHAR__ */
  475. }
  476. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  477. #ifdef __UCLIBC_HAS_FLOATS__
  478. #ifdef __UCLIBC_HAS_LOCALE__
  479. sc->decpt = __UCLIBC_CURLOCALE_DATA.decimal_point;
  480. sc->decpt_len = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
  481. #else /* __UCLIBC_HAS_LOCALE__ */
  482. sc->fake_decpt = sc->decpt = decpt_str;
  483. sc->decpt_len = 1;
  484. #endif /* __UCLIBC_HAS_LOCALE__ */
  485. #ifdef __UCLIBC_HAS_WCHAR__
  486. #ifdef __UCLIBC_HAS_LOCALE__
  487. sc->decpt_wc = __UCLIBC_CURLOCALE_DATA.decimal_point_wc;
  488. #else
  489. sc->decpt_wc = '.';
  490. #endif
  491. #endif /* __UCLIBC_HAS_WCHAR__ */
  492. #endif /* __UCLIBC_HAS_FLOATS__ */
  493. }
  494. int __scan_getc(register struct scan_cookie *sc)
  495. {
  496. int c;
  497. #ifdef __UCLIBC_HAS_WCHAR__
  498. assert(!sc->mb_fail);
  499. #endif /* __UCLIBC_HAS_WCHAR__ */
  500. sc->cc = EOF;
  501. if (--sc->width < 0) {
  502. sc->ungot_flag |= 2;
  503. return -1;
  504. }
  505. if (sc->ungot_flag == 0) {
  506. if ((c = GETC(sc)) == EOF) {
  507. sc->ungot_flag |= 2;
  508. return -1;
  509. }
  510. sc->ungot_char = c;
  511. } else {
  512. assert(sc->ungot_flag == 1);
  513. sc->ungot_flag = 0;
  514. }
  515. ++sc->nread;
  516. return sc->cc = sc->ungot_char;
  517. }
  518. void __scan_ungetc(register struct scan_cookie *sc)
  519. {
  520. ++sc->width;
  521. if (sc->ungot_flag == 2) { /* last was EOF */
  522. sc->ungot_flag = 0;
  523. sc->cc = sc->ungot_char;
  524. } else if (sc->ungot_flag == 0) {
  525. sc->ungot_flag = 1;
  526. --sc->nread;
  527. } else {
  528. assert(0);
  529. }
  530. }
  531. #endif
  532. /**********************************************************************/
  533. #ifdef L___psfs_parse_spec
  534. #ifdef SPEC_FLAGS
  535. static const unsigned char spec_flags[] = SPEC_FLAGS;
  536. #endif /* SPEC_FLAGS */
  537. static const unsigned char spec_chars[] = SPEC_CHARS;
  538. static const unsigned char qual_chars[] = QUAL_CHARS;
  539. static const unsigned char spec_ranges[] = SPEC_RANGES;
  540. static const unsigned short spec_allowed[] = SPEC_ALLOWED_FLAGS;
  541. int __psfs_parse_spec(register psfs_t *psfs)
  542. {
  543. const unsigned char *p;
  544. const unsigned char *fmt0 = psfs->fmt;
  545. int i;
  546. #ifdef SPEC_FLAGS
  547. int j;
  548. #endif
  549. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  550. unsigned char fail = 0;
  551. i = 0; /* Do this here to avoid a warning. */
  552. if (!__isdigit_char(*psfs->fmt)) { /* Not a positional arg. */
  553. fail = 1;
  554. goto DO_FLAGS;
  555. }
  556. /* parse the positional arg (or width) value */
  557. do {
  558. if (i <= ((INT_MAX - 9)/10)) {
  559. i = (i * 10) + (*psfs->fmt++ - '0');
  560. }
  561. } while (__isdigit_char(*psfs->fmt));
  562. if (*psfs->fmt != '$') { /* This is a max field width. */
  563. if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
  564. goto ERROR_EINVAL;
  565. }
  566. psfs->max_width = i;
  567. psfs->num_pos_args = -2;
  568. goto DO_QUALIFIER;
  569. }
  570. ++psfs->fmt; /* Advance past '$'. */
  571. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  572. #if defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0))
  573. DO_FLAGS:
  574. #endif /* defined(SPEC_FLAGS) || (defined(NL_ARGMAX) && (NL_ARGMAX > 0)) */
  575. #ifdef SPEC_FLAGS
  576. p = spec_flags;
  577. j = FLAG_SURPRESS;
  578. do {
  579. if (*p == *psfs->fmt) {
  580. ++psfs->fmt;
  581. psfs->flags |= j;
  582. goto DO_FLAGS;
  583. }
  584. j += j;
  585. } while (*++p);
  586. if (psfs->flags & FLAG_SURPRESS) { /* Suppress assignment. */
  587. psfs->store = 0;
  588. goto DO_WIDTH;
  589. }
  590. #else /* SPEC_FLAGS */
  591. if (*psfs->fmt == '*') { /* Suppress assignment. */
  592. ++psfs->fmt;
  593. psfs->store = 0;
  594. goto DO_WIDTH;
  595. }
  596. #endif /* SPEC_FLAGS */
  597. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  598. if (fail) {
  599. /* Must be a non-positional arg */
  600. if (psfs->num_pos_args >= 0) { /* Already saw a pos arg! */
  601. goto ERROR_EINVAL;
  602. }
  603. psfs->num_pos_args = -2;
  604. } else {
  605. if ((psfs->num_pos_args == -2) || (((unsigned int)(--i)) >= NL_ARGMAX)) {
  606. /* Already saw a non-pos arg or (0-based) num too large. */
  607. goto ERROR_EINVAL;
  608. }
  609. psfs->cur_pos_arg = i;
  610. }
  611. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  612. DO_WIDTH:
  613. for (i = 0 ; __isdigit_char(*psfs->fmt) ; ) {
  614. if (i <= ((INT_MAX - 9)/10)) {
  615. i = (i * 10) + (*psfs->fmt++ - '0');
  616. psfs->max_width = i;
  617. }
  618. }
  619. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  620. DO_QUALIFIER:
  621. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  622. p = qual_chars;
  623. do {
  624. if (*psfs->fmt == *p) {
  625. ++psfs->fmt;
  626. break;
  627. }
  628. } while (*++p);
  629. if ((p - qual_chars < 2) && (*psfs->fmt == *p)) {
  630. p += ((sizeof(qual_chars)-2) / 2);
  631. ++psfs->fmt;
  632. }
  633. psfs->dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  634. #ifdef __UCLIBC_MJN3_ONLY__
  635. #warning CONSIDER: Should we validate that psfs->max_width > 0 in __psfs_parse_spec()? It would avoid whitespace consumption...
  636. #warning CONSIDER: Should INT_MAX be a valid width (%c/%C)? See __psfs_parse_spec().
  637. #endif /* __UCLIBC_MJN3_ONLY__ */
  638. p = spec_chars;
  639. do {
  640. if (*psfs->fmt == *p) {
  641. int p_m_spec_chars = p - spec_chars;
  642. #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  643. #error implement gnu a flag
  644. if ((*p == 'a')
  645. && ((psfs->fmt[1] == '[') || ((psfs->fmt[1]|0x20) == 's'))
  646. ) { /* Assumes ascii for 's' and 'S' test. */
  647. psfs->flags |= FLAG_MALLOC;
  648. ++psfs->fmt;
  649. ++p;
  650. continue; /* The related conversions follow 'a'. */
  651. }
  652. #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  653. for (p = spec_ranges; p_m_spec_chars > *p ; ++p) {}
  654. if (((psfs->dataargtype >> 8) | psfs->flags)
  655. & ~spec_allowed[(int)(p - spec_ranges)]
  656. ) {
  657. goto ERROR_EINVAL;
  658. }
  659. if ((p_m_spec_chars >= CONV_c)
  660. && (psfs->dataargtype & PA_FLAG_LONG)) {
  661. p_m_spec_chars -= 3; /* lc -> C, ls -> S, l[ -> ?? */
  662. }
  663. psfs->conv_num = p_m_spec_chars;
  664. return psfs->fmt - fmt0;
  665. }
  666. if (!*++p) {
  667. ERROR_EINVAL:
  668. __set_errno(EINVAL);
  669. return -1;
  670. }
  671. } while(1);
  672. assert(0);
  673. }
  674. #endif
  675. /**********************************************************************/
  676. #if defined(L_vfscanf) || defined(L_vfwscanf)
  677. #ifdef __UCLIBC_HAS_WCHAR__
  678. #ifdef L_vfscanf
  679. static int sc_getc(register struct scan_cookie *sc)
  680. {
  681. return getc(sc->fp);
  682. }
  683. static int scan_getwc(register struct scan_cookie *sc)
  684. {
  685. size_t r;
  686. int width;
  687. wchar_t wc[1];
  688. char b[1];
  689. if (--sc->width < 0) {
  690. sc->ungot_flag |= 2;
  691. return -1;
  692. }
  693. width = sc->width; /* Preserve width. */
  694. sc->width = INT_MAX; /* MB_CUR_MAX can invoke a function. */
  695. assert(!sc->mb_fail);
  696. r = (size_t)(-3);
  697. while (__scan_getc(sc) >= 0) {
  698. *b = sc->cc;
  699. r = mbrtowc(wc, b, 1, &sc->mbstate);
  700. if (((ssize_t) r) >= 0) { /* Successful completion of a wc. */
  701. sc->wc = *wc;
  702. goto SUCCESS;
  703. } else if (r == ((size_t) -2)) {
  704. /* Potentially valid but incomplete. */
  705. continue;
  706. }
  707. break;
  708. }
  709. if (r == ((size_t)(-3))) { /* EOF or ERROR on first read */
  710. sc->wc = WEOF;
  711. r = (size_t)(-1);
  712. } else {
  713. /* If we reach here, either r == ((size_t)-1) and
  714. * mbrtowc set errno to EILSEQ, or r == ((size_t)-2)
  715. * and stream is in an error state or at EOF with a
  716. * partially complete wchar. */
  717. __set_errno(EILSEQ); /* In case of incomplete conversion. */
  718. sc->mb_fail = 1;
  719. }
  720. SUCCESS:
  721. sc->width = width; /* Restore width. */
  722. return (int)((ssize_t) r);
  723. }
  724. #endif /* L_vfscanf */
  725. #ifdef L_vfwscanf
  726. /* This gets called by __scan_getc. __scan_getc is called by vfwscanf
  727. * when the next wide char is expected to be valid ascii (digits).
  728. */
  729. static int sc_getc(register struct scan_cookie *sc)
  730. {
  731. wint_t wc;
  732. if (sc->fp->filedes == -3) {
  733. if (sc->fp->bufpos < sc->fp->bufend) {
  734. wc = *((wchar_t *)(sc->fp->bufpos));
  735. sc->fp->bufpos += sizeof(wchar_t);
  736. } else {
  737. sc->fp->modeflags |= __FLAG_EOF;
  738. return EOF;
  739. }
  740. } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
  741. return EOF;
  742. }
  743. sc->ungot_wflag = 1;
  744. sc->ungot_wchar = wc;
  745. sc->ungot_wchar_width = sc->fp->ungot_width[0];
  746. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  747. if (wc == sc->thousands_sep_wc) {
  748. wc = ',';
  749. } else
  750. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  751. #ifdef __UCLIBC_HAS_FLOATS__
  752. if (wc == sc->decpt_wc) {
  753. wc = '.';
  754. } else
  755. #endif /* __UCLIBC_HAS_FLOATS__ */
  756. if (!__isascii(wc)) {
  757. wc = '?';
  758. }
  759. sc->wc = sc->ungot_char = wc;
  760. return (int) wc;
  761. }
  762. static int scan_getwc(register struct scan_cookie *sc)
  763. {
  764. wint_t wc;
  765. sc->wc = WEOF;
  766. if (--sc->width < 0) {
  767. sc->ungot_flag |= 2;
  768. return -1;
  769. }
  770. if (sc->ungot_flag == 0) {
  771. if (sc->fp->filedes == -3) {
  772. if (sc->fp->bufpos < sc->fp->bufend) {
  773. wc = *((wchar_t *)(sc->fp->bufpos));
  774. sc->fp->bufpos += sizeof(wchar_t);
  775. } else {
  776. sc->ungot_flag |= 2;
  777. return -1;
  778. }
  779. } else if ((wc = fgetwc_unlocked(sc->fp)) == WEOF) {
  780. sc->ungot_flag |= 2;
  781. return -1;
  782. }
  783. sc->ungot_wflag = 1;
  784. sc->ungot_char = wc;
  785. sc->ungot_wchar_width = sc->fp->ungot_width[0];
  786. } else {
  787. assert(sc->ungot_flag == 1);
  788. sc->ungot_flag = 0;
  789. }
  790. ++sc->nread;
  791. sc->wc = sc->ungot_char;
  792. return 0;
  793. }
  794. #endif /* L_vfwscanf */
  795. #endif /* __UCLIBC_HAS_WCHAR__ */
  796. static __inline void kill_scan_cookie(register struct scan_cookie *sc)
  797. {
  798. #ifdef L_vfscanf
  799. if (sc->ungot_flag & 1) {
  800. ungetc(sc->ungot_char, sc->fp);
  801. /* Deal with distiction between user and scanf ungots. */
  802. if (sc->nread == 0) { /* Only one char was read... app ungot? */
  803. sc->fp->ungot[1] = sc->app_ungot; /* restore ungot state. */
  804. } else {
  805. sc->fp->ungot[1] = 0;
  806. }
  807. }
  808. #else
  809. if ((sc->ungot_flag & 1) && (sc->ungot_wflag & 1)
  810. && (sc->fp->filedes != -3) && (sc->fp->state.mask == 0)
  811. ) {
  812. ungetwc(sc->ungot_char, sc->fp);
  813. /* Deal with distiction between user and scanf ungots. */
  814. if (sc->nread == 0) { /* Only one char was read... app ungot? */
  815. sc->fp->ungot[1] = sc->app_ungot; /* restore ungot state. */
  816. } else {
  817. sc->fp->ungot[1] = 0;
  818. }
  819. sc->fp->ungot_width[1] = sc->ungot_wchar_width;
  820. }
  821. #endif
  822. }
  823. #ifdef L_vfwscanf
  824. #ifdef __UCLIBC_HAS_FLOATS__
  825. static const char fake_decpt_str[] = ".";
  826. #endif
  827. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  828. static const char fake_thousands_sep_str[] = ",";
  829. #endif
  830. #endif /* L_vfwscanf */
  831. int VFSCANF (FILE *__restrict fp, const Wchar *__restrict format, va_list arg)
  832. {
  833. const Wuchar *fmt;
  834. unsigned char *b;
  835. #ifdef L_vfwscanf
  836. wchar_t wbuf[1];
  837. wchar_t *wb;
  838. #endif /* L_vfwscanf */
  839. #ifdef __UCLIBC_HAS_WCHAR__
  840. mbstate_t mbstate;
  841. #endif /* __UCLIBC_HAS_WCHAR__ */
  842. struct scan_cookie sc;
  843. psfs_t psfs;
  844. int i;
  845. #warning fix MAX_DIGITS. we do not do binary, so...!
  846. #define MAX_DIGITS 65 /* Allow one leading 0. */
  847. unsigned char buf[MAX_DIGITS+2];
  848. #ifdef L_vfscanf
  849. unsigned char scanset[UCHAR_MAX + 1];
  850. unsigned char invert; /* Careful! Meaning changes. */
  851. #endif /* L_vfscanf */
  852. unsigned char fail;
  853. #ifdef __UCLIBC_MJN3_ONLY__
  854. #warning TODO: Make checking of the format string in C locale an option.
  855. #endif
  856. /* To support old programs, don't check mb validity if in C locale. */
  857. #if defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf)
  858. /* ANSI/ISO C99 requires format string to be a valid multibyte string
  859. * beginning and ending in its initial shift state. */
  860. if (((__UCLIBC_CURLOCALE_DATA).encoding) != __ctype_encoding_7_bit) {
  861. mbstate.mask = 0; /* Initialize the mbstate. */
  862. const char *p = format;
  863. if (mbsrtowcs(NULL, &p, SIZE_MAX, &mbstate) == ((size_t)(-1))) {
  864. __set_errno(EINVAL); /* Format string is invalid. */
  865. return 0;
  866. }
  867. }
  868. #endif /* defined(__UCLIBC_HAS_LOCALE__) && !defined(L_vfwscanf) */
  869. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  870. psfs.num_pos_args = -1; /* Must start at -1. */
  871. /* Initialize positional arg ptrs to NULL. */
  872. memset(psfs.pos_args, 0, sizeof(psfs.pos_args));
  873. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  874. __STDIO_THREADLOCK(fp);
  875. __init_scan_cookie(&sc,fp);
  876. #ifdef __UCLIBC_HAS_WCHAR__
  877. sc.sc_getc = sc_getc;
  878. sc.ungot_wchar_width = sc.fp->ungot_width[1];
  879. #ifdef L_vfwscanf
  880. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  881. if (*sc.grouping) {
  882. sc.thousands_sep = fake_thousands_sep_str;
  883. sc.tslen = 1;
  884. }
  885. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  886. #ifdef __UCLIBC_HAS_FLOATS__
  887. sc.fake_decpt = fake_decpt_str;
  888. #endif /* __UCLIBC_HAS_FLOATS__ */
  889. #else /* L_vfwscanf */
  890. #ifdef __UCLIBC_HAS_FLOATS__
  891. sc.fake_decpt = sc.decpt;
  892. #endif /* __UCLIBC_HAS_FLOATS__ */
  893. #endif /* L_vfwscanf */
  894. #endif /* __UCLIBC_HAS_WCHAR__ */
  895. psfs.cnt = 0;
  896. /* Note: If we ever wanted to support non-nice codesets, we
  897. * would really need to do a mb->wc conversion here in the
  898. * vfscanf case. Related changes would have to be made in
  899. * the code that follows... basicly wherever fmt appears. */
  900. for (fmt = (const Wuchar *) format ; *fmt ; /* ++fmt */) {
  901. psfs.store = 1;
  902. psfs.flags = 0;
  903. #ifndef NDEBUG
  904. psfs.cur_ptr = NULL; /* Debugging aid. */
  905. #endif /* NDEBUG */
  906. sc.ungot_flag &= 1; /* Clear (possible fake) EOF. */
  907. sc.width = psfs.max_width = INT_MAX;
  908. /* Note: According to the standards, vfscanf does use isspace
  909. * here. So, if we did a mb->wc conversion, we would have to do
  910. * something like
  911. * ((((__uwchar_t)wc) < UCHAR_MAX) && isspace(wc))
  912. * because wc might not be in the allowed domain. */
  913. if (ISSPACE(*fmt)) {
  914. do {
  915. ++fmt;
  916. } while (ISSPACE(*fmt));
  917. --fmt;
  918. psfs.conv_num = CONV_whitespace;
  919. goto DO_WHITESPACE;
  920. }
  921. if (*fmt == '%') { /* Conversion specification. */
  922. if (*++fmt == '%') { /* Remember, '%' eats whitespace too. */
  923. psfs.conv_num = CONV_percent;
  924. goto DO_CONVERSION;
  925. }
  926. #ifdef L_vfscanf
  927. psfs.fmt = fmt;
  928. #else /* L_vfscanf */
  929. {
  930. const __uwchar_t *wf = fmt;
  931. psfs.fmt = b = buf;
  932. while (*wf && __isascii(*wf) && (b < buf + sizeof(buf) - 1)) {
  933. *b++ = *wf++;
  934. }
  935. #ifdef __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__
  936. #error this is wrong... we need to ched in __psfs_parse_spec instead since this checks last char in buffer and conversion my have stopped before it.
  937. if ((*b == 'a') && ((*wf == '[') || ((*wf|0x20) == 's'))) {
  938. goto DONE; /* Spec was excessively long. */
  939. }
  940. #endif /* __UCLIBC_HAS_SCANF_GLIBC_A_FLAG__ */
  941. *b = 0;
  942. if (b == buf) { /* Bad conversion specifier! */
  943. goto DONE;
  944. }
  945. }
  946. #endif /* L_vfscanf */
  947. if ((i = __psfs_parse_spec(&psfs)) < 0) { /* Bad conversion specifier! */
  948. goto DONE;
  949. }
  950. fmt += i;
  951. #if defined(NL_ARGMAX) && (NL_ARGMAX > 0)
  952. if (psfs.store) {
  953. if (psfs.num_pos_args == -2) {
  954. psfs.cur_ptr = va_arg(arg, void *);
  955. } else {
  956. while (psfs.cur_pos_arg > psfs.num_pos_args) {
  957. psfs.pos_args[++psfs.num_pos_args] = va_arg(arg, void *);
  958. }
  959. psfs.cur_ptr = psfs.pos_args[psfs.cur_pos_arg];
  960. }
  961. }
  962. #else /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  963. psfs.cur_ptr = va_arg(arg, void *);
  964. #endif /* defined(NL_ARGMAX) && (NL_ARGMAX > 0) */
  965. DO_CONVERSION:
  966. /* First, consume white-space if not n, c, [, C, or l[. */
  967. if ((((1L << CONV_n)|(1L << CONV_C)|(1L << CONV_c)
  968. |(1L << CONV_LEFTBRACKET)|(1L << CONV_leftbracket))
  969. & (1L << psfs.conv_num)) == 0
  970. ) {
  971. DO_WHITESPACE:
  972. while ((__scan_getc(&sc) >= 0)
  973. #ifdef L_vfscanf
  974. && isspace(sc.cc)
  975. #else /* L_vfscanf */
  976. && iswspace(sc.wc)
  977. #endif /* L_vfscanf */
  978. ) {}
  979. __scan_ungetc(&sc);
  980. if (psfs.conv_num == CONV_whitespace) {
  981. goto NEXT_FMT;
  982. }
  983. }
  984. sc.width = psfs.max_width; /* Now limit the max width. */
  985. if (sc.width == 0) { /* 0 width is forbidden. */
  986. goto DONE;
  987. }
  988. if (psfs.conv_num == CONV_percent) {
  989. goto MATCH_CHAR;
  990. }
  991. if (psfs.conv_num == CONV_n) {
  992. if (psfs.store) {
  993. _store_inttype(psfs.cur_ptr, psfs.dataargtype,
  994. (uintmax_t) sc.nread);
  995. }
  996. goto NEXT_FMT;
  997. }
  998. if (psfs.conv_num <= CONV_A) { /* pointer, integer, or float spec */
  999. #ifdef L_vfscanf
  1000. if (__psfs_do_numeric(&psfs, &sc) < 0) { /* Num conv failed! */
  1001. goto DONE;
  1002. }
  1003. goto NEXT_FMT;
  1004. #else
  1005. int r = __psfs_do_numeric(&psfs, &sc);
  1006. if (sc.ungot_wflag == 1) { /* fix up '?', '.', and ',' hacks */
  1007. sc.cc = sc.ungot_char = sc.ungot_wchar;
  1008. }
  1009. if (r < 0) {
  1010. goto DONE;
  1011. }
  1012. goto NEXT_FMT;
  1013. #endif
  1014. }
  1015. /* Do string conversions here since they are not common code. */
  1016. #ifdef L_vfscanf
  1017. if
  1018. #ifdef __UCLIBC_HAS_WCHAR__
  1019. (psfs.conv_num >= CONV_LEFTBRACKET)
  1020. #else /* __UCLIBC_HAS_WCHAR__ */
  1021. (psfs.conv_num >= CONV_c)
  1022. #endif /* __UCLIBC_HAS_WCHAR__ */
  1023. {
  1024. b = (psfs.store ? ((unsigned char *) psfs.cur_ptr) : buf);
  1025. fail = 1;
  1026. if (psfs.conv_num == CONV_c) {
  1027. if (sc.width == INT_MAX) {
  1028. sc.width = 1;
  1029. }
  1030. while (__scan_getc(&sc) >= 0) {
  1031. *b = sc.cc;
  1032. b += psfs.store;
  1033. }
  1034. __scan_ungetc(&sc);
  1035. if (sc.width > 0) { /* Failed to read all required. */
  1036. goto DONE;
  1037. }
  1038. psfs.cnt += psfs.store;
  1039. goto NEXT_FMT;
  1040. }
  1041. if (psfs.conv_num == CONV_s) {
  1042. /* Yes, believe it or not, a %s conversion can store nuls. */
  1043. while ((__scan_getc(&sc) >= 0) && !isspace(sc.cc)) {
  1044. *b = sc.cc;
  1045. b += psfs.store;
  1046. fail = 0;
  1047. }
  1048. } else {
  1049. #ifdef __UCLIBC_HAS_WCHAR__
  1050. assert((psfs.conv_num == CONV_LEFTBRACKET) || \
  1051. (psfs.conv_num == CONV_leftbracket));
  1052. #else /* __UCLIBC_HAS_WCHAR__ */
  1053. assert((psfs.conv_num == CONV_leftbracket));
  1054. #endif /* __UCLIBC_HAS_WCHAR__ */
  1055. invert = 0;
  1056. if (*++fmt == '^') {
  1057. ++fmt;
  1058. invert = 1;
  1059. }
  1060. memset(scanset, invert, sizeof(scanset));
  1061. invert = 1-invert;
  1062. if (*fmt == ']') {
  1063. scanset[(int)(']')] = invert;
  1064. ++fmt;
  1065. }
  1066. while (*fmt != ']') {
  1067. if (!*fmt) { /* No closing ']'. */
  1068. goto DONE;
  1069. }
  1070. if ((*fmt == '-') && (fmt[1] != ']')
  1071. && (fmt[-1] < fmt[1]) /* sorted? */
  1072. ) { /* range */
  1073. ++fmt;
  1074. i = fmt[-2];
  1075. /* Note: scanset[i] should already have been done
  1076. * in the previous iteration. */
  1077. do {
  1078. scanset[++i] = invert;
  1079. } while (i < *fmt);
  1080. /* Safe to fall through, and a bit smaller. */
  1081. }
  1082. /* literal char */
  1083. scanset[(int) *fmt] = invert;
  1084. ++fmt;
  1085. }
  1086. #ifdef __UCLIBC_HAS_WCHAR__
  1087. if (psfs.conv_num == CONV_LEFTBRACKET) {
  1088. goto DO_LEFTBRACKET;
  1089. }
  1090. #endif /* __UCLIBC_HAS_WCHAR__ */
  1091. while (__scan_getc(&sc) >= 0) {
  1092. if (!scanset[sc.cc]) {
  1093. break;
  1094. }
  1095. *b = sc.cc;
  1096. b += psfs.store;
  1097. fail = 0;
  1098. }
  1099. }
  1100. /* Common tail for processing of %s and %[. */
  1101. __scan_ungetc(&sc);
  1102. if (fail) { /* nothing stored! */
  1103. goto DONE;
  1104. }
  1105. *b = 0; /* Nul-terminate string. */
  1106. psfs.cnt += psfs.store;
  1107. goto NEXT_FMT;
  1108. }
  1109. #ifdef __UCLIBC_HAS_WCHAR__
  1110. DO_LEFTBRACKET: /* Need to do common wide init. */
  1111. if (psfs.conv_num >= CONV_C) {
  1112. wchar_t wbuf[1];
  1113. wchar_t *wb;
  1114. sc.mbstate.mask = 0;
  1115. wb = (psfs.store ? ((wchar_t *) psfs.cur_ptr) : wbuf);
  1116. fail = 1;
  1117. if (psfs.conv_num == CONV_C) {
  1118. if (sc.width == INT_MAX) {
  1119. sc.width = 1;
  1120. }
  1121. while (scan_getwc(&sc) >= 0) {
  1122. assert(sc.width >= 0);
  1123. *wb = sc.wc;
  1124. wb += psfs.store;
  1125. }
  1126. __scan_ungetc(&sc);
  1127. if (sc.width > 0) { /* Failed to read all required. */
  1128. goto DONE;
  1129. }
  1130. psfs.cnt += psfs.store;
  1131. goto NEXT_FMT;
  1132. }
  1133. if (psfs.conv_num == CONV_S) {
  1134. /* Yes, believe it or not, a %s conversion can store nuls. */
  1135. while ((scan_getwc(&sc) >= 0)
  1136. && ((((__uwchar_t)(sc.wc)) > UCHAR_MAX)
  1137. || !isspace(sc.wc))
  1138. ) {
  1139. *wb = sc.wc;
  1140. wb += psfs.store;
  1141. fail = 0;
  1142. }
  1143. } else {
  1144. assert(psfs.conv_num == CONV_LEFTBRACKET);
  1145. while (scan_getwc(&sc) >= 0) {
  1146. if (((__uwchar_t) sc.wc) <= UCHAR_MAX) {
  1147. if (!scanset[sc.wc]) {
  1148. break;
  1149. }
  1150. } else if (invert) {
  1151. break;
  1152. }
  1153. *wb = sc.wc;
  1154. wb += psfs.store;
  1155. fail = 0;
  1156. }
  1157. }
  1158. /* Common tail for processing of %ls and %l[. */
  1159. __scan_ungetc(&sc);
  1160. if (fail || sc.mb_fail) { /* Nothing stored or mb error. */
  1161. goto DONE;
  1162. }
  1163. *wb = 0; /* Nul-terminate string. */
  1164. psfs.cnt += psfs.store;
  1165. goto NEXT_FMT;
  1166. }
  1167. #endif /* __UCLIBC_HAS_WCHAR__ */
  1168. #else /* L_vfscanf */
  1169. if (psfs.conv_num >= CONV_C) {
  1170. b = buf;
  1171. wb = wbuf;
  1172. if (psfs.conv_num >= CONV_c) {
  1173. mbstate.mask = 0; /* Initialize the mbstate. */
  1174. if (psfs.store) {
  1175. b = (unsigned char *) psfs.cur_ptr;
  1176. }
  1177. } else {
  1178. if (psfs.store) {
  1179. wb = (wchar_t *) psfs.cur_ptr;
  1180. }
  1181. }
  1182. fail = 1;
  1183. if ((psfs.conv_num == CONV_C) || (psfs.conv_num == CONV_c)) {
  1184. if (sc.width == INT_MAX) {
  1185. sc.width = 1;
  1186. }
  1187. while (scan_getwc(&sc) >= 0) {
  1188. if (psfs.conv_num == CONV_C) {
  1189. *wb = sc.wc;
  1190. wb += psfs.store;
  1191. } else {
  1192. i = wcrtomb(b, sc.wc, &mbstate);
  1193. if (i < 0) { /* Conversion failure. */
  1194. goto DONE_DO_UNGET;
  1195. }
  1196. if (psfs.store) {
  1197. b += i;
  1198. }
  1199. }
  1200. }
  1201. __scan_ungetc(&sc);
  1202. if (sc.width > 0) { /* Failed to read all required. */
  1203. goto DONE;
  1204. }
  1205. psfs.cnt += psfs.store;
  1206. goto NEXT_FMT;
  1207. }
  1208. if ((psfs.conv_num == CONV_S) || (psfs.conv_num == CONV_s)) {
  1209. /* Yes, believe it or not, a %s conversion can store nuls. */
  1210. while ((scan_getwc(&sc) >= 0) && !iswspace(sc.wc)) {
  1211. if (psfs.conv_num == CONV_S) {
  1212. *wb = sc.wc;
  1213. wb += psfs.store;
  1214. } else {
  1215. i = wcrtomb(b, sc.wc, &mbstate);
  1216. if (i < 0) { /* Conversion failure. */
  1217. goto DONE_DO_UNGET;
  1218. }
  1219. if (psfs.store) {
  1220. b += i;
  1221. }
  1222. }
  1223. fail = 0;
  1224. }
  1225. } else {
  1226. const wchar_t *sss;
  1227. const wchar_t *ssp;
  1228. unsigned char invert = 0;
  1229. assert((psfs.conv_num == CONV_LEFTBRACKET)
  1230. || (psfs.conv_num == CONV_leftbracket));
  1231. if (*++fmt == '^') {
  1232. ++fmt;
  1233. invert = 1;
  1234. }
  1235. sss = (const wchar_t *) fmt;
  1236. if (*fmt == ']') {
  1237. ++fmt;
  1238. }
  1239. while (*fmt != ']') {
  1240. if (!*fmt) { /* No closing ']'. */
  1241. goto DONE;
  1242. }
  1243. if ((*fmt == '-') && (fmt[1] != ']')
  1244. && (fmt[-1] < fmt[1]) /* sorted? */
  1245. ) { /* range */
  1246. ++fmt;
  1247. }
  1248. ++fmt;
  1249. }
  1250. /* Ok... a valid scanset spec. */
  1251. while (scan_getwc(&sc) >= 0) {
  1252. ssp = sss;
  1253. do { /* We know sss < fmt. */
  1254. if (*ssp == '-') { /* possible range... */
  1255. /* Note: We accept a-c-e (ordered) as
  1256. * equivalent to a-e. */
  1257. if (ssp > sss) {
  1258. if ((++ssp < (const wchar_t *) fmt)
  1259. && (ssp[-2] < *ssp) /* sorted? */
  1260. ) { /* yes */
  1261. if ((sc.wc >= ssp[-2])
  1262. && (sc.wc <= *ssp)) {
  1263. break;
  1264. }
  1265. continue; /* not in range */
  1266. }
  1267. --ssp; /* oops... '-' at end, so back up */
  1268. }
  1269. /* false alarm... a literal '-' */
  1270. }
  1271. if (sc.wc == *ssp) { /* Matched literal char. */
  1272. break;
  1273. }
  1274. } while (++ssp < (const wchar_t *) fmt);
  1275. if ((ssp == (const wchar_t *) fmt) ^ invert) {
  1276. /* no match and not inverting
  1277. * or match and inverting */
  1278. break;
  1279. }
  1280. if (psfs.conv_num == CONV_LEFTBRACKET) {
  1281. *wb = sc.wc;
  1282. wb += psfs.store;
  1283. } else {
  1284. i = wcrtomb(b, sc.wc, &mbstate);
  1285. if (i < 0) { /* Conversion failure. */
  1286. goto DONE_DO_UNGET;
  1287. }
  1288. if (psfs.store) {
  1289. b += i;
  1290. }
  1291. }
  1292. fail = 0;
  1293. }
  1294. }
  1295. /* Common tail for processing of %s and %[. */
  1296. __scan_ungetc(&sc);
  1297. if (fail) { /* nothing stored! */
  1298. goto DONE;
  1299. }
  1300. *wb = 0; /* Nul-terminate string. */
  1301. *b = 0;
  1302. psfs.cnt += psfs.store;
  1303. goto NEXT_FMT;
  1304. }
  1305. #endif /* L_vfscanf */
  1306. assert(0);
  1307. goto DONE;
  1308. } /* conversion specification */
  1309. MATCH_CHAR:
  1310. if (__scan_getc(&sc) != *fmt) {
  1311. #ifdef L_vfwscanf
  1312. DONE_DO_UNGET:
  1313. #endif /* L_vfwscanf */
  1314. __scan_ungetc(&sc);
  1315. goto DONE;
  1316. }
  1317. NEXT_FMT:
  1318. ++fmt;
  1319. }
  1320. DONE:
  1321. if ((psfs.cnt == 0) && (*fmt) && __FEOF_OR_FERROR(fp)) {
  1322. psfs.cnt = EOF; /* Yes, vfwscanf also returns EOF. */
  1323. }
  1324. kill_scan_cookie(&sc);
  1325. /* RETURN_cnt: */
  1326. __STDIO_THREADUNLOCK(fp);
  1327. return psfs.cnt;
  1328. }
  1329. #endif
  1330. /**********************************************************************/
  1331. #ifdef L___psfs_do_numeric
  1332. static const unsigned char spec_base[] = SPEC_BASE;
  1333. static const unsigned char nil_string[] = "(nil)";
  1334. int __psfs_do_numeric(psfs_t *psfs, struct scan_cookie *sc)
  1335. {
  1336. unsigned char *b;
  1337. const unsigned char *p;
  1338. #ifdef __UCLIBC_HAS_FLOATS__
  1339. int exp_adjust = 0;
  1340. #endif
  1341. #warning fix MAX_DIGITS. we do not do binary, so...!
  1342. #define MAX_DIGITS 65 /* Allow one leading 0. */
  1343. #warning fix buf!
  1344. unsigned char buf[MAX_DIGITS+2+ 100];
  1345. unsigned char usflag, base;
  1346. unsigned char nonzero = 0;
  1347. unsigned char seendigit = 0;
  1348. #ifndef __UCLIBC_HAS_FLOATS__
  1349. if (psfs->conv_num > CONV_i) { /* floating point */
  1350. goto DONE;
  1351. }
  1352. #endif
  1353. base = spec_base[psfs->conv_num - CONV_p];
  1354. usflag = (psfs->conv_num <= CONV_u); /* (1)0 if (un)signed */
  1355. b = buf;
  1356. if (psfs->conv_num == CONV_p) { /* Pointer */
  1357. p = nil_string;
  1358. do {
  1359. if ((__scan_getc(sc) < 0) || (*p != sc->cc)) {
  1360. __scan_ungetc(sc);
  1361. if (p > nil_string) { /* failed */
  1362. /* We matched at least the '(' so even if we
  1363. * are at eof, we can not match a pointer. */
  1364. goto DONE;
  1365. }
  1366. break;
  1367. }
  1368. if (!*++p) { /* Matched (nil), so no unget necessary. */
  1369. if (psfs->store) {
  1370. ++psfs->cnt;
  1371. _store_inttype(psfs->cur_ptr, psfs->dataargtype,
  1372. (uintmax_t) NULL);
  1373. }
  1374. return 0;
  1375. }
  1376. } while (1);
  1377. #ifdef __UCLIBC_MJN3_ONLY__
  1378. #warning CONSIDER: Should we require a 0x prefix and disallow +/- for pointer %p?
  1379. #endif /* __UCLIBC_MJN3_ONLY__ */
  1380. }
  1381. __scan_getc(sc);
  1382. if ((sc->cc == '+') || (sc->cc == '-')) { /* Handle leading sign.*/
  1383. *b++ = sc->cc;
  1384. __scan_getc(sc);
  1385. }
  1386. if ((base & 0xef) == 0) { /* 0xef is ~16, so 16 or 0. */
  1387. if (sc->cc == '0') { /* Possibly set base and handle prefix. */
  1388. __scan_getc(sc);
  1389. if ((sc->cc|0x20) == 'x') { /* Assumes ascii.. x or X. */
  1390. if ((__scan_getc(sc) < 0)
  1391. #ifdef __UCLIBC_HAS_WCHAR__
  1392. && !sc->ungot_wflag /* wc outside char range */
  1393. #endif /* __UCLIBC_HAS_WCHAR__ */
  1394. ) {
  1395. /* Note! 'x' at end of file|field is special.
  1396. * While this looks like I'm 'unget'ing twice,
  1397. * EOF and end of field are handled specially
  1398. * by the scan_* funcs. */
  1399. __scan_ungetc(sc);
  1400. goto DO_NO_0X;
  1401. }
  1402. base = 16; /* Base 16 for sure now. */
  1403. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1404. /* The prefix is required for hexadecimal floats. */
  1405. *b++ = '0';
  1406. *b++ = 'x';
  1407. #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
  1408. } else { /* oops... back up */
  1409. DO_NO_0X:
  1410. __scan_ungetc(sc);
  1411. sc->cc = '0'; /* NASTY HACK! */
  1412. base = (base >> 1) + 8; /* 0->8, 16->16. no 'if' */
  1413. #ifdef __UCLIBC_HAS_FLOATS__
  1414. if (psfs->conv_num > CONV_i) { /* floating point */
  1415. base = 10;
  1416. }
  1417. #endif
  1418. }
  1419. } else if (!base) {
  1420. base = 10;
  1421. }
  1422. }
  1423. /***************** digit grouping **********************/
  1424. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1425. if ((psfs->flags & FLAG_THOUSANDS) && (base == 10)
  1426. && *(p = sc->grouping)
  1427. ) {
  1428. int nblk1, nblk2, nbmax, lastblock, pass, i;
  1429. #ifdef __UCLIBC_MJN3_ONLY__
  1430. #warning CONSIDER: Should we initalize the grouping blocks in __init_scan_cookie()?
  1431. #endif /* __UCLIBC_MJN3_ONLY__ */
  1432. nbmax = nblk2 = nblk1 = *p;
  1433. if (*++p) {
  1434. nblk2 = *p;
  1435. if (nbmax < nblk2) {
  1436. nbmax = nblk2;
  1437. }
  1438. assert(!*++p);
  1439. }
  1440. /* Note: for printf, if 0 and \' flags appear then
  1441. * grouping is done before 0-padding. Should we
  1442. * strip leading 0's first? Or add a 0 flag? */
  1443. /* For vfwscanf, sc_getc translates, so the value of sc->cc is
  1444. * either EOF or a char. */
  1445. if (!__isdigit_char_or_EOF(sc->cc)) { /* No starting digit! */
  1446. #ifdef __UCLIBC_HAS_FLOATS__
  1447. if (psfs->conv_num > CONV_i) { /* floating point */
  1448. goto NO_STARTING_DIGIT;
  1449. }
  1450. #endif
  1451. goto DONE_DO_UNGET;
  1452. }
  1453. if (sc->cc == '0') {
  1454. seendigit = 1;
  1455. *b++ = '0'; /* Store the first 0. */
  1456. #ifdef __UCLIBC_MJN3_ONLY__
  1457. #warning CONSIDER: Should leading 0s be skipped before digit grouping? (printf 0 pad)
  1458. #endif /* __UCLIBC_MJN3_ONLY__ */
  1459. #if 0
  1460. do { /* But ignore all subsequent 0s. */
  1461. __scan_getc(sc);
  1462. } while (sc->cc == '0');
  1463. #endif
  1464. }
  1465. pass = 0;
  1466. lastblock = 0;
  1467. do {
  1468. i = 0;
  1469. while (__isdigit_char_or_EOF(sc->cc)) {
  1470. seendigit = 1;
  1471. if (i == nbmax) { /* too many digits for a block */
  1472. #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
  1473. if (!pass) { /* treat as nongrouped */
  1474. if (nonzero) {
  1475. goto DO_NO_GROUP;
  1476. }
  1477. goto DO_TRIM_LEADING_ZEROS;
  1478. }
  1479. #endif
  1480. if (nbmax > nblk1) {
  1481. goto DONE_DO_UNGET;
  1482. }
  1483. goto DONE_GROUPING_DO_UNGET; /* nbmax == nblk1 */
  1484. }
  1485. ++i;
  1486. if (nonzero || (sc->cc != '0')) {
  1487. if (b < buf + MAX_DIGITS) {
  1488. *b++ = sc->cc;
  1489. nonzero = 1;
  1490. #ifdef __UCLIBC_HAS_FLOATS__
  1491. } else {
  1492. ++exp_adjust;
  1493. #endif
  1494. }
  1495. }
  1496. __scan_getc(sc);
  1497. }
  1498. if (i) { /* we saw digits digits */
  1499. if ((i == nblk2) || ((i < nblk2) && !pass)) {
  1500. /* (possible) outer grp */
  1501. p = sc->thousands_sep;
  1502. if (*p == sc->cc) { /* first byte matches... */
  1503. /* so check if grouping mb char */
  1504. /* Since 1st matched, either match or fail now
  1505. * unless EOF (yuk) */
  1506. __scan_getc(sc);
  1507. MBG_LOOP:
  1508. if (!*++p) { /* is a grouping mb char */
  1509. lastblock = i;
  1510. ++pass;
  1511. continue;
  1512. }
  1513. if (*p == sc->cc) {
  1514. __scan_getc(sc);
  1515. goto MBG_LOOP;
  1516. }
  1517. /* bad grouping mb char! */
  1518. __scan_ungetc(sc);
  1519. if ((sc->cc >= 0) || (p > sc->thousands_sep + 1)) {
  1520. #ifdef __UCLIBC_HAS_FLOATS__
  1521. /* We failed to match a thousep mb char, and
  1522. * we've read too much to recover. But if
  1523. * this is a floating point conversion and
  1524. * the initial portion of the decpt mb char
  1525. * matches, then we may still be able to
  1526. * recover. */
  1527. int k = p - sc->thousands_sep - 1;
  1528. if ((psfs->conv_num > CONV_i) /* float conversion */
  1529. && (!pass || (i == nblk1)) /* possible last */
  1530. && !memcmp(sc->thousands_sep, sc->fake_decpt, k)
  1531. /* and prefix matched, so could be decpt */
  1532. ) {
  1533. __scan_getc(sc);
  1534. p = sc->fake_decpt + k;
  1535. do {
  1536. if (!*++p) {
  1537. strcpy(b, sc->decpt);
  1538. b += sc->decpt_len;
  1539. goto GOT_DECPT;
  1540. }
  1541. if (*p != sc->cc) {
  1542. __scan_ungetc(sc);
  1543. break; /* failed */
  1544. }
  1545. __scan_getc(sc);
  1546. } while (1);
  1547. }
  1548. #endif /* __UCLIBC_HAS_FLOATS__ */
  1549. goto DONE;
  1550. }
  1551. /* was EOF and 1st, so recoverable. */
  1552. }
  1553. }
  1554. if ((i == nblk1) || ((i < nblk1) && !pass)) {
  1555. /* got an inner group */
  1556. goto DONE_GROUPING_DO_UNGET;
  1557. }
  1558. if (i > nblk1) {
  1559. /* An inner group if we can back up a bit. */
  1560. if ((i - nblk1) <= (sc->ungot_flag ^ 1)) {
  1561. assert(sc->cc < 0);
  1562. --b;
  1563. goto DO_RECOVER_GROUP;
  1564. }
  1565. }
  1566. /* (0 < i < nblk1) && (pass > 0) so prev group char
  1567. * So we have an unrecoverable situation. */
  1568. goto DONE_DO_UNGET;
  1569. } /* i != 0 */
  1570. assert(pass);
  1571. /* No next group. Can we back up past grouping mb char? */
  1572. if ((pass == 1) || (nblk1 == nblk2)) {
  1573. if (!i && (sc->tslen == 1) && (sc->cc < 0)) {
  1574. /* No digits, grouping mb char is len 1, and EOF*/
  1575. DO_RECOVER_GROUP:
  1576. if (sc->ungot_flag & 2) {
  1577. __scan_ungetc(sc);
  1578. }
  1579. goto DONE_GROUPING_DO_UNGET;
  1580. }
  1581. }
  1582. goto DONE_DO_UNGET;
  1583. } while (1);
  1584. assert(0); /* Should never get here. */
  1585. }
  1586. #endif /***************** digit grouping **********************/
  1587. /* Not grouping so first trim all but one leading 0. */
  1588. #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
  1589. DO_TRIM_LEADING_ZEROS:
  1590. #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
  1591. if (sc->cc == '0') {
  1592. seendigit = 1;
  1593. *b++ = '0'; /* Store the first 0. */
  1594. do { /* But ignore all subsequent 0s. */
  1595. __scan_getc(sc);
  1596. } while (sc->cc == '0');
  1597. }
  1598. #ifdef __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__
  1599. DO_NO_GROUP:
  1600. #endif /* __UCLIBC_HAS_SCANF_LENIENT_DIGIT_GROUPING__ */
  1601. /* At this point, we're ready to start reading digits. */
  1602. #define valid_digit(cc,base) (isxdigit(cc) && ((base == 16) || (cc - '0' < base)))
  1603. while (valid_digit(sc->cc,base)) { /* Now for significant digits.*/
  1604. if (b - buf < MAX_DIGITS) {
  1605. nonzero = seendigit = 1; /* Set nonzero too 0s trimmed above. */
  1606. *b++ = sc->cc;
  1607. #ifdef __UCLIBC_HAS_FLOATS__
  1608. } else {
  1609. ++exp_adjust;
  1610. #endif
  1611. }
  1612. __scan_getc(sc);
  1613. }
  1614. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1615. DONE_GROUPING_DO_UNGET:
  1616. #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
  1617. if (psfs->conv_num <= CONV_i) { /* integer conversion */
  1618. __scan_ungetc(sc);
  1619. *b = 0; /* null-terminate */
  1620. if (!seendigit) {
  1621. goto DONE; /* No digits! */
  1622. }
  1623. if (psfs->store) {
  1624. if (*buf == '-') {
  1625. usflag = 0;
  1626. }
  1627. ++psfs->cnt;
  1628. _store_inttype(psfs->cur_ptr, psfs->dataargtype,
  1629. (uintmax_t) STRTOUIM(buf, NULL, base, 1-usflag));
  1630. }
  1631. return 0;
  1632. }
  1633. #ifdef __UCLIBC_HAS_FLOATS__
  1634. /* At this point, we have everything left of the decimal point or exponent. */
  1635. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1636. NO_STARTING_DIGIT:
  1637. #endif
  1638. p = sc->fake_decpt;
  1639. do {
  1640. if (!*p) {
  1641. strcpy(b, sc->decpt);
  1642. b += sc->decpt_len;
  1643. break;
  1644. }
  1645. if (*p != sc->cc) {
  1646. if (p > sc->fake_decpt) {
  1647. if ((sc->cc >= 0) && (p > sc->fake_decpt + 1)) {
  1648. goto DONE_DO_UNGET; /* failed */
  1649. }
  1650. __scan_ungetc(sc);
  1651. }
  1652. goto DO_DIGIT_CHECK;
  1653. }
  1654. ++p;
  1655. __scan_getc(sc);
  1656. } while (1);
  1657. #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
  1658. GOT_DECPT:
  1659. #endif
  1660. if (!nonzero) {
  1661. if (sc->cc == '0') {
  1662. assert(exp_adjust == 0);
  1663. *b++ = '0';
  1664. ++exp_adjust;
  1665. seendigit = 1;
  1666. do {
  1667. --exp_adjust;
  1668. __scan_getc(sc);
  1669. } while (sc->cc == '0');
  1670. }
  1671. }
  1672. while (valid_digit(sc->cc,base)) { /* Process fractional digits.*/
  1673. if (b - buf < MAX_DIGITS) {
  1674. seendigit = 1;
  1675. *b++ = sc->cc;
  1676. }
  1677. __scan_getc(sc);
  1678. }
  1679. DO_DIGIT_CHECK:
  1680. /* Hmm... no decimal point. */
  1681. if (!seendigit) {
  1682. static const unsigned char nan_inf_str[] = "an\0nfinity";
  1683. if (base == 16) { /* We had a prefix, but no digits! */
  1684. goto DONE_DO_UNGET;
  1685. }
  1686. /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/
  1687. #undef TOLOWER
  1688. #define TOLOWER(C) ((C)|0x20)
  1689. switch (TOLOWER(sc->cc)) {
  1690. case 'i':
  1691. p = nan_inf_str + 3;
  1692. break;
  1693. case 'n':
  1694. p = nan_inf_str;
  1695. break;
  1696. default:
  1697. /* No digits and not inf or nan. */
  1698. goto DONE_DO_UNGET;
  1699. }
  1700. *b++ = sc->cc;
  1701. do {
  1702. __scan_getc(sc);
  1703. if (TOLOWER(sc->cc) == *p) {
  1704. *b++ = sc->cc;
  1705. ++p;
  1706. continue;
  1707. }
  1708. if (!*p || (p == nan_inf_str + 5)) { /* match nan/infinity or inf */
  1709. goto GOT_FLOAT;
  1710. }
  1711. /* Unrecoverable. Even if on 1st char, we had no digits. */
  1712. goto DONE_DO_UNGET;
  1713. } while (1);
  1714. }
  1715. /* If we get here, we had some digits. */
  1716. if (
  1717. #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
  1718. ((base == 16) && (((sc->cc)|0x20) == 'p')) ||
  1719. #endif
  1720. (((sc->cc)|0x20) == 'e')
  1721. ) { /* Process an exponent. */
  1722. *b++ = sc->cc;
  1723. __scan_getc(sc);
  1724. if (sc->cc < 0) { /* EOF... recoverable */
  1725. --b;
  1726. goto GOT_FLOAT;
  1727. }
  1728. if ((sc->cc == '+') || (sc->cc == '-')) { /* Signed exponent? */
  1729. *b++ = sc->cc;
  1730. __scan_getc(sc);
  1731. }
  1732. #warning fix MAX_EXP_DIGITS!
  1733. #define MAX_EXP_DIGITS 20
  1734. assert(seendigit);
  1735. seendigit = 0;
  1736. nonzero = 0;
  1737. if (sc->cc == '0') {
  1738. seendigit = 1;
  1739. *b++ = '0';
  1740. do {
  1741. __scan_getc(sc);
  1742. } while (sc->cc == '0');
  1743. }
  1744. while (__isdigit_char_or_EOF(sc->cc)) { /* Exponent digits (base 10).*/
  1745. if (seendigit < MAX_EXP_DIGITS) {
  1746. ++seendigit;
  1747. *b++ = sc->cc;
  1748. }
  1749. __scan_getc(sc);
  1750. }
  1751. if (!seendigit) { /* No digits. Unrecoverable. */
  1752. goto DONE_DO_UNGET;
  1753. }
  1754. }
  1755. GOT_FLOAT:
  1756. *b = 0;
  1757. {
  1758. __fpmax_t x;
  1759. char *e;
  1760. x = __strtofpmax(buf, &e, exp_adjust);
  1761. assert(!*e);
  1762. if (psfs->store) {
  1763. if (psfs->dataargtype & PA_FLAG_LONG_LONG) {
  1764. *((long double *)psfs->cur_ptr) = (long double) x;
  1765. } else if (psfs->dataargtype & PA_FLAG_LONG) {
  1766. *((double *)psfs->cur_ptr) = (double) x;
  1767. } else {
  1768. *((float *)psfs->cur_ptr) = (float) x;
  1769. }
  1770. ++psfs->cnt;
  1771. }
  1772. __scan_ungetc(sc);
  1773. return 0;
  1774. }
  1775. #endif /* __UCLIBC_HAS_FLOATS__ */
  1776. DONE_DO_UNGET:
  1777. __scan_ungetc(sc);
  1778. DONE:
  1779. return -1;
  1780. }
  1781. #endif
  1782. /**********************************************************************/