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