_scanf.c 52 KB

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