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
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/*
* _doprnt: common code for printf, fprintf, sprintf
*/
#include "lint.h"
#include "mtlib.h"
#include "print.h" /* parameters & macros for doprnt */
#include <wchar.h>
#include "libc.h"
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <stdarg.h>
#include <values.h>
#include <memory.h>
#include <string.h>
#include <locale.h>
#include <widec.h>
#include "../i18n/_locale.h"
#include <errno.h>
#include <sys/types.h>
#include <libw.h>
#include "mse.h"
#include "xpg6.h"
static const char nullstr[] = "(null)";
static const wchar_t widenullstr[] = L"(null)";
#if defined(__i386) || defined(__amd64) || defined(__sparcv9)
#define GETQVAL(arg) (va_arg(arg, long double))
#else /* !defined(__i386) && !defined(__sparcv9) */
#define GETQVAL(arg) *(va_arg(arg, long double *))
#endif /* !defined(__i386) && !defined(__sparcv9) */
#ifdef _WIDE
#define STRCHR wcschr
#define STRSPN wcsspn
#define ATOI(x) _watoi((wchar_t *)x)
#define _P_HYPHEN L"-"
#define _P_PLUS L"+"
#define _P_BLANK L" "
#define _P_ZEROx L"0x"
#define _P_ZEROX L"0X"
#define _M_ISDIGIT(c) (((c) >= 0) && ((c) < 256) && isdigit((c)))
#define _M_ISUPPER(c) (((c) >= 0) && ((c) < 256) && isupper((c)))
#else /* _WIDE */
#define STRCHR strchr
#define STRSPN strspn
#define ATOI(x) atoi(x)
#define _P_HYPHEN "-"
#define _P_PLUS "+"
#define _P_BLANK " "
#define _P_ZEROx "0x"
#define _P_ZEROX "0X"
#define _M_ISDIGIT(c) isdigit((c))
#define _M_ISUPPER(c) isupper((c))
#endif /* _WIDE */
#ifdef _WIDE
#define PUT(p, n) \
{ \
int retp; \
retp = put_wide(iop, &bufptr, bufferend, p, n, sflag); \
if (retp == EOF) { \
return ((ssize_t)EOF); \
} \
}
#define PAD(s, n) \
{ \
int retp; \
retp = pad_wide(iop, &bufptr, bufferend, s, n, sflag); \
if (retp == EOF) { \
return ((ssize_t)EOF); \
} \
}
#define FPCONV(func, val, prec, decpt, sign, cvtbuf) \
{ \
char cb[DECIMAL_STRING_LENGTH]; \
wchar_t *wp; \
char *cp; \
(void) func(val, prec, decpt, sign, cb); \
wp = cvtbuf; \
cp = cb; \
while (*cp) { \
*wp++ = (wchar_t)*cp++; \
} \
*wp = L'\0'; \
}
#else /* _WIDE */
#define PUT(p, n) \
{\
/*\
* When _doprnt() is called by [v]snprintf, we need to \
* always call _dowrite(). We also need to call _dowrite() \
* if the bufptr lies beyond the end of the buffer. This \
* is possible due to known off-by-one errors in __flsbuf() \
* and _fwrite_unlocked(). See 1235867 and 1231720 for the \
* sordid details. \
*/\
if (snflag || bufptr > bufferend ||\
(unsigned long)(bufferend - bufptr) < (n)) {\
if (!_dowrite(p, n, iop, &bufptr)) {\
return (EOF);\
}\
} else {\
unsigned char *fbp = bufptr;\
switch (n) {\
case 4:\
*fbp = *p;\
*(fbp + 1) = *(p + 1);\
*(fbp + 2) = *(p + 2);\
*(fbp + 3) = *(p + 3);\
bufptr += 4;\
break;\
case 3:\
*fbp = *p;\
*(fbp + 1) = *(p + 1);\
*(fbp + 2) = *(p + 2);\
bufptr += 3;\
break;\
case 2:\
*fbp = *p;\
*(fbp + 1) = *(p + 1);\
bufptr += 2;\
break;\
case 1:\
*bufptr++ = *p;\
break;\
default:\
bufptr = (unsigned char *)memcpy(fbp, p, n)\
+ (n);\
}\
}\
}
#define PAD(s, n) { ssize_t nn; \
for (nn = n; nn > PAD_LEN; nn -= PAD_LEN) \
if (!_dowrite(s, PAD_LEN, iop, &bufptr)) \
return (EOF); \
PUT(s, nn); \
}
#define FPCONV(func, val, prec, decpt, sign, cvtbuf) \
(void) func(val, prec, decpt, sign, cvtbuf);
#endif /* _WIDE */
/* bit positions for flags used in doprnt */
#define LENGTH 0x1 /* l */
#define FPLUS 0x2 /* + */
#define FMINUS 0x4 /* - */
#define FBLANK 0x8 /* blank */
#define FSHARP 0x10 /* # */
#define PADZERO 0x20 /* padding zeroes requested via '0' */
#define DOTSEEN 0x40 /* dot appeared in format specification */
#define SUFFIX 0x80 /* a suffix is to appear in the output */
#define RZERO 0x100 /* there will be trailing zeros in output */
#define LZERO 0x200 /* there will be leading zeroes in output */
#define SHORT 0x400 /* h */
#define QUAD 0x800 /* Q for long double */
#define XLONG 0x1000 /* ll for long long */
#define CHAR 0x2000 /* hh for char */
#ifdef _WIDE
static wchar_t *insert_decimal_point(wchar_t *ep);
static wchar_t *insert_thousands_sep(wchar_t *bp, wchar_t *ep);
#else /* _WIDE */
static char *insert_decimal_point(char *ep);
static char *insert_thousands_sep(char *bp, char *ep);
#endif /* _WIDE */
static int _rec_scrswidth(wchar_t *, ssize_t);
/*
* Positional Parameter information
*/
#define MAXARGS 30 /* max. number of args for fast positional paramters */
static ssize_t
_dowrite(const char *p, ssize_t n, FILE *iop, unsigned char **ptrptr);
/*
* stva_list is used to subvert C's restriction that a variable with an
* array type can not appear on the left hand side of an assignment operator.
* By putting the array inside a structure, the functionality of assigning to
* the whole array through a simple assignment is achieved..
*/
typedef struct stva_list {
va_list ap;
} stva_list;
#ifdef _WIDE
static void _wmkarglst(wchar_t *, stva_list, stva_list [], int);
static void _wgetarg(wchar_t *, stva_list *, long, int);
#else /* _WIDE */
static void _mkarglst(char *, stva_list, stva_list [], int);
void _getarg(char *, stva_list *, long, int);
#endif /* _WIDE */
static int
_lowdigit(ssize_t *valptr)
{
/* This function computes the decimal low-order digit of the number */
/* pointed to by valptr, and returns this digit after dividing */
/* *valptr by ten. This function is called ONLY to compute the */
/* low-order digit of a long whose high-order bit is set. */
ssize_t lowbit = *valptr & 1;
long value = (*valptr >> 1) & ~HIBITL;
*valptr = value / 5;
value = value % 5 * 2 + lowbit + '0';
return ((int)value);
}
static int
_lowlldigit(long long *valptr)
{
ssize_t lowbit = *valptr & 1;
long long value = (*valptr >> 1) & ~HIBITLL;
*valptr = value / 5;
value = value % 5 * 2 + lowbit + '0';
return ((int)value);
}
/* The function _dowrite carries out buffer pointer bookkeeping surrounding */
/* a call to fwrite. It is called only when the end of the file output */
/* buffer is approached or in other unusual situations. */
static ssize_t
_dowrite(const char *p, ssize_t n, FILE *iop, unsigned char **ptrptr)
{
if (!(iop->_flag & _IOREAD)) {
iop->_cnt -= (*ptrptr - iop->_ptr);
iop->_ptr = *ptrptr;
_bufsync(iop, _bufend(iop));
if (_FWRITE(p, 1, n, iop) != n) {
return (0);
}
*ptrptr = iop->_ptr;
} else {
if (n > iop->_cnt)
n = iop->_cnt;
iop->_cnt -= n;
*ptrptr = (unsigned char *)memcpy((char *)*ptrptr, p, n) + n;
iop->_ptr = *ptrptr;
}
return (1);
}
#define PAD_LEN 20
static const char _blanks[] = " ";
static const char _zeroes[] = "00000000000000000000";
#ifdef _WIDE
static const wchar_t uc_digs[] = L"0123456789ABCDEF";
static const wchar_t lc_digs[] = L"0123456789abcdef";
#else /* _WIDE */
static const char uc_digs[] = "0123456789ABCDEF";
static const char lc_digs[] = "0123456789abcdef";
#endif /* _WIDE */
#ifdef _WIDE
static int
put_wide(FILE *iop, unsigned char **bufptr,
unsigned char *bufferend, wchar_t *p, size_t n,
int sflag)
{
unsigned char *newbufptr;
wchar_t *q;
int r;
size_t len, i;
if (sflag) {
len = (wchar_t *)bufferend - (wchar_t *)*bufptr;
if (n > len) {
(void) wmemcpy((wchar_t *)*bufptr, p, len);
iop->_ptr = bufferend;
return (EOF);
} else {
(void) wmemcpy((wchar_t *)*bufptr, p, n);
*bufptr = (unsigned char *)((wchar_t *)*bufptr + n);
return (0);
}
} else {
char *tmpp, *tmpq;
size_t tsize;
tsize = (n + 1) * MB_LEN_MAX;
tmpp = lmalloc(tsize);
if (tmpp == NULL) {
errno = ENOMEM;
return (EOF);
}
q = p;
tmpq = tmpp;
for (len = 0, i = 0; i < n; i++) {
r = wctomb(tmpq, *q++);
if (r == -1) {
lfree(tmpp, tsize);
errno = EILSEQ;
return (EOF);
}
len += r;
tmpq += r;
}
tmpq = tmpp;
newbufptr = *bufptr + len;
if (newbufptr > bufferend) {
if (!_dowrite(tmpp, len, iop, bufptr)) {
lfree(tmpp, tsize);
return (EOF);
}
} else {
(void) memcpy(*bufptr, tmpp, len);
*bufptr = newbufptr;
}
lfree(tmpp, tsize);
return (0);
}
}
static int
pad_wide(FILE *iop, unsigned char **bufptr,
unsigned char *bufferend, const char *s, size_t n,
int sflag)
{
unsigned char *newbufptr;
ssize_t nn;
size_t len;
wchar_t ps;
if (sflag) {
/* for swprintf */
ps = (wchar_t)s[0];
len = (wchar_t *)bufferend - (wchar_t *)*bufptr;
if (n > len) {
(void) wmemset((wchar_t *)*bufptr, ps, len);
iop->_ptr = bufferend;
return (EOF);
} else {
(void) wmemset((wchar_t *)*bufptr, ps, n);
*bufptr = (unsigned char *)((wchar_t *)*bufptr + n);
return (0);
}
} else {
for (nn = n; nn > PAD_LEN; nn -= PAD_LEN) {
if (!_dowrite(s, PAD_LEN, iop, bufptr))
return (EOF);
}
newbufptr = *bufptr + nn;
if (newbufptr > bufferend) {
if (!_dowrite(s, nn, iop, bufptr))
return (EOF);
} else {
(void) memcpy(*bufptr, s, nn);
*bufptr = newbufptr;
}
return (0);
}
}
#endif /* _WIDE */
#ifdef _WIDE
ssize_t
_wdoprnt(const wchar_t *format, va_list in_args, FILE *iop)
{
return (_wndoprnt(format, in_args, iop, 0));
}
#else /* _WIDE */
ssize_t
_doprnt(const char *format, va_list in_args, FILE *iop)
{
return (_ndoprnt(format, in_args, iop, 0));
}
#endif /* _WIDE */
#ifdef _WIDE
ssize_t
_wndoprnt(const wchar_t *format, va_list in_args, FILE *iop, int prflag)
#else /* _WIDE */
ssize_t
_ndoprnt(const char *format, va_list in_args, FILE *iop, int prflag)
#endif /* _WIDE */
{
#ifdef _WIDE
int sflag = 0;
size_t maxcount;
#else
int snflag = 0;
#endif /* _WIDE */
/* bufptr is used inside of doprnt instead of iop->_ptr; */
/* bufferend is a copy of _bufend(iop), if it exists. For */
/* dummy file descriptors (iop->_flag & _IOREAD), bufferend */
/* may be meaningless. Dummy file descriptors are used so that */
/* sprintf and vsprintf may share the _doprnt routine with the */
/* rest of the printf family. */
unsigned char *bufptr;
unsigned char *bufferend;
#ifdef _WIDE
/* This variable counts output characters. */
size_t count = 0;
#else /* _WIDE */
/* This variable counts output characters. */
int count = 0;
#endif /* _WIDE */
#ifdef _WIDE
wchar_t *bp;
size_t bpsize;
wchar_t *p;
char *cbp;
#else /* _WIDE */
/* Starting and ending points for value to be printed */
char *bp;
char *p;
/* Field width and precision */
ssize_t preco;
char tmpbuf[10];
int retcode;
#endif /* _WIDE */
/* Field width and precision */
int prec = 0;
ssize_t width;
ssize_t num;
wchar_t *wp;
ssize_t wcount = 0;
char wflag;
char lflag;
int quote; /* ' */
#ifdef _WIDE
/* Format code */
wchar_t fcode;
#else /* _WIDE */
/* Format code */
char fcode;
ssize_t sec_display;
#endif /* _WIDE */
/* Number of padding zeroes required on the left and right */
ssize_t lzero, rzero, rz, leadzeroes;
/* Flags - bit positions defined by LENGTH, FPLUS, FMINUS, FBLANK, */
/* and FSHARP are set if corresponding character is in format */
/* Bit position defined by PADZERO means extra space in the field */
/* should be padded with leading zeroes rather than with blanks */
ssize_t flagword;
#ifdef _WIDE
/* Values are developed in this buffer */
wchar_t buf[max(MAXLLDIGS, 1034)];
wchar_t cvtbuf[512 + DECIMAL_STRING_LENGTH];
/* Pointer to sign, "0x", "0X", or empty */
wchar_t *prefix;
wchar_t prefixbuf[4];
/* Exponent or empty */
wchar_t *suffix;
/* Buffer to create exponent */
wchar_t expbuf[MAXESIZ + 1];
#else /* _WIDE */
/* Values are developed in this buffer */
char buf[max(MAXLLDIGS, 1034)];
char cvtbuf[512 + DECIMAL_STRING_LENGTH];
/* Pointer to sign, "0x", "0X", or empty */
char *prefix;
char prefixbuf[4];
/* Exponent or empty */
char *suffix;
/* Buffer to create exponent */
char expbuf[MAXESIZ + 1];
#endif /* _WIDE */
/* Length of prefix and of suffix */
ssize_t prefixlength, suffixlength;
/* Combined length of leading zeroes, trailing zeroes, and suffix */
ssize_t otherlength;
/* The value being converted, if integer */
ssize_t val;
/* The value being converted, if long long */
long long ll = 0LL;
/* Output value from aconvert */
int exp;
/* Output values from fcvt and ecvt */
int decpt, sign;
#ifdef _WIDE
/* Pointer to a translate table for digits of whatever radix */
const wchar_t *tab;
#else /* _WIDE */
/* Pointer to a translate table for digits of whatever radix */
const char *tab;
#endif /* _WIDE */
/* Work variables */
ssize_t k, lradix, mradix;
int inf_nan = 0;
int inf_nan_mixed_case = 0;
#ifdef _WIDE
/* variables for positional parameters */
/* save the beginning of the format */
wchar_t *sformat = (wchar_t *)format;
#else /* _WIDE */
/* variables for positional parameters */
char *sformat = (char *)format; /* save the beginning of the format */
#endif
int fpos = 1; /* 1 if first positional parameter */
stva_list args, /* used to step through the argument list */
sargs; /* used to save the start of the arg list */
stva_list bargs; /* used to restore args if positional width */
/* or precision */
stva_list arglst[MAXARGS]; /* array giving appropriate values */
/* for va_arg() to retrieve the */
/* corresponding argument: */
/* arglst[0] is the first arg */
/* arglst[1] is the second arg, etc */
int starflg = 0; /* set to 1 if * format specifier seen */
/*
* Initialize args and sargs to the start of the argument list.
* We don't know any portable way to copy an arbitrary C object
* so we use a system-specific routine (probably a macro) from
* stdarg.h. (Remember that if va_list is an array, in_args will
* be a pointer and &in_args won't be what we would want for
* memcpy.)
*/
va_copy(args.ap, in_args);
sargs = args;
#ifdef _WIDE
if (iop->_flag == _IOREAD)
sflag = 1;
if (!sflag) {
#endif /* _WIDE */
/* if first I/O to the stream get a buffer */
/* Note that iop->_base should not equal 0 for sprintf and vsprintf */
if (iop->_base == 0) {
if (_findbuf(iop) == 0)
return (EOF);
/*
* _findbuf leaves _cnt set to 0 which is the wrong thing
* to do for fully buffered files
*/
if (!(iop->_flag & (_IOLBF|_IONBF)))
iop->_cnt = _bufend(iop) - iop->_base;
}
#ifdef _WIDE
}
#endif /* _WIDE */
#ifdef _WIDE
bufptr = iop->_ptr;
if (sflag) {
maxcount = (size_t)iop->_cnt;
bufferend = (unsigned char *)(((wchar_t *)iop->_ptr) +
maxcount);
} else {
bufferend = _bufend(iop);
}
#else /* _WIDE */
/* initialize buffer pointer and buffer end pointer */
bufptr = iop->_ptr;
if (iop->_flag & _IOREAD) {
/*
* [v]sprintf or [v]snprintf
*/
if (iop->_cnt == MAXINT) {
/*
* [v]sprintf (no boundschecking)
*/
bufferend =
(unsigned char *)((long)bufptr | (-1L & ~HIBITL));
} else {
/*
* [v]snprintf (with boundschecking) or
* iop with _IORW has been read.
*/
bufferend = _bufend(iop);
if (bufferend == NULL) {
/*
* [v]snprintf
*
* [v]snprint() needs to be always handled by
* _dowrite().
*/
snflag = 1;
}
}
} else {
/*
* [v]printf or [v]fprintf
*/
bufferend = _bufend(iop);
}
#endif /* _WIDE */
/*
* The main loop -- this loop goes through one iteration
* for each string of ordinary characters or format specification.
*/
for (; ; ) {
ssize_t n;
if ((fcode = *format) != '\0' && fcode != '%') {
#ifdef _WIDE
bp = (wchar_t *)format;
#else /* _WIDE */
bp = (char *)format;
#endif /* _WIDE */
do {
format++;
} while ((fcode = *format) != '\0' && fcode != '%');
count += (n = format - bp); /* n = no. of non-% chars */
PUT(bp, n);
}
if (fcode == '\0') { /* end of format; return */
ssize_t nn = bufptr - iop->_ptr;
#ifdef _WIDE
if (sflag) {
iop->_ptr = bufptr;
return ((ssize_t)count);
}
#endif /* _WIDE */
iop->_cnt -= nn;
iop->_ptr = bufptr;
/* in case of interrupt during last several lines */
if ((bufptr + iop->_cnt) > bufferend && !(iop->_flag \
& _IOREAD))
_bufsync(iop, bufferend);
if (iop->_flag & (_IONBF | _IOLBF) && \
(iop->_flag & _IONBF || \
memchr((char *)(bufptr+iop->_cnt), \
'\n', -iop->_cnt) != NULL))
(void) _xflsbuf(iop);
#ifdef _WIDE
return (FERROR(iop) ? EOF : (ssize_t)count);
#else /* _WIDE */
return (FERROR(iop) ? EOF : (int)count);
#endif /* _WIDE */
}
/*
* % has been found.
* The following switch is used to parse the format
* specification and to perform the operation specified
* by the format letter. The program repeatedly goes
* back to this switch until the format letter is
* encountered.
*/
width = prefixlength = otherlength = 0;
flagword = suffixlength = 0;
format++;
wflag = 0;
lflag = 0;
quote = 0;
#ifndef _WIDE
sec_display = 0;
#endif
charswitch:
switch (fcode = *format++) {
case '+':
flagword |= FPLUS;
goto charswitch;
case '-':
flagword |= FMINUS;
flagword &= ~PADZERO; /* ignore 0 flag */
goto charswitch;
case ' ':
flagword |= FBLANK;
goto charswitch;
case '\'': /* XSH4 */
quote++;
goto charswitch;
case '#':
flagword |= FSHARP;
goto charswitch;
/* Scan the field width and precision */
case '.':
flagword |= DOTSEEN;
prec = 0;
goto charswitch;
case '*':
if (_M_ISDIGIT(*format)) {
starflg = 1;
bargs = args;
goto charswitch;
}
if (!(flagword & DOTSEEN)) {
width = va_arg(args.ap, int);
if (width < 0) {
width = -width;
flagword |= FMINUS;
}
} else {
prec = va_arg(args.ap, int);
if (prec < 0) {
prec = 0;
flagword ^= DOTSEEN; /* ANSI sez so */
}
}
goto charswitch;
case '$':
{
ssize_t position;
stva_list targs;
if (fpos) {
#ifdef _WIDE
_wmkarglst(sformat, sargs, arglst, prflag);
#else /* _WIDE */
_mkarglst(sformat, sargs, arglst, prflag);
#endif /* _WIDE */
fpos = 0;
}
if (flagword & DOTSEEN) {
position = prec;
prec = 0;
} else {
position = width;
width = 0;
}
if (position <= 0) {
/* illegal position */
format--;
continue;
}
if (position <= MAXARGS) {
targs = arglst[position - 1];
} else {
targs = arglst[MAXARGS - 1];
#ifdef _WIDE
_wgetarg(sformat, &targs, position, prflag);
#else /* _WIDE */
_getarg(sformat, &targs, position, prflag);
#endif /* _WIDE */
}
if (!starflg)
args = targs;
else {
starflg = 0;
args = bargs;
if (flagword & DOTSEEN) {
prec = va_arg(targs.ap, int);
if (prec < 0) {
prec = 0;
flagword ^= DOTSEEN; /* XSH */
}
} else {
width = va_arg(targs.ap, int);
if (width < 0) {
width = -width;
flagword |= FMINUS;
}
}
}
goto charswitch;
}
case '0': /* obsolescent spec: leading zero in width */
/* means pad with leading zeros */
if (!(flagword & (DOTSEEN | FMINUS)))
flagword |= PADZERO;
/* FALLTHROUGH */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num = fcode - '0';
while (_M_ISDIGIT(fcode = *format)) {
num = num * 10 + fcode - '0';
format++;
}
if (flagword & DOTSEEN)
prec = num;
else
width = num;
goto charswitch;
/* Scan the length modifier */
case 'l':
if (!(flagword & XLONG)) {
if (lflag) {
/* long long */
flagword &= ~LENGTH;
flagword |= XLONG;
} else {
/* long */
flagword |= LENGTH;
}
}
lflag++;
goto charswitch;
case 'L': /* long double */
flagword |= QUAD;
goto charswitch;
case 'h':
if (!(flagword & CHAR)) {
if (flagword & SHORT) {
/* char - hh */
flagword &= ~SHORT;
flagword |= CHAR;
} else {
/* short */
flagword |= SHORT;
}
}
goto charswitch;
case 'j':
#ifndef _LP64
/*
* *printf_c89() in 32-bit libc uses
* 32-bit intmax_t; otherwise intmax_t
* is 64-bits.
*/
if (!(prflag & _F_INTMAX32)) {
#endif
flagword |= XLONG; /* [u]intmax_t (64) */
#ifndef _LP64
}
#endif
goto charswitch;
case 't':
/*
* LENGTH is shared by l, t, z specifiers; protect
* against (destructive) undefined behavior (eg:
* avoid %llt setting XLONG and LENGTH) with invalid
* combinations of specifiers
*/
if (!(flagword & XLONG)) {
flagword |= LENGTH; /* ptrdiff_t */
}
goto charswitch;
case 'z':
if (!(flagword & XLONG)) {
flagword |= LENGTH; /* [s]size_t */
}
goto charswitch;
/*
* The character addressed by format must be
* the format letter -- there is nothing
* left for it to be.
*
* The status of the +, -, #, and blank
* flags are reflected in the variable
* "flagword". "width" and "prec" contain
* numbers corresponding to the digit
* strings before and after the decimal
* point, respectively. If there was no
* decimal point, then flagword & DOTSEEN
* is false and the value of prec is meaningless.
*
* The following switch cases set things up
* for printing. What ultimately gets
* printed will be padding blanks, a
* prefix, left padding zeroes, a value,
* right padding zeroes, a suffix, and
* more padding blanks. Padding blanks
* will not appear simultaneously on both
* the left and the right. Each case in
* this switch will compute the value, and
* leave in several variables the informa-
* tion necessary to construct what is to
* be printed.
*
* The prefix is a sign, a blank, "0x",
* "0X", a sign or a blank followed by "0x"
* or "0X", or nothing, and is addressed by
* "prefix".
*
* The suffix is either null or an
* exponent, and is addressed by "suffix".
* If there is a suffix, the flagword bit
* SUFFIX will be set.
*
* The value to be printed starts at "bp"
* and continues up to and not including
* "p".
*
* "lzero" and "rzero" will contain the
* number of padding zeroes required on
* the left and right, respectively.
* The flagword bits LZERO and RZERO tell
* whether padding zeros are required.
*
* The number of padding blanks, and
* whether they go on the left or the
* right, will be computed on exit from
* the switch.
*/
/*
* decimal fixed point representations
*
* HIBITL is 100...000
* binary, and is equal to the maximum
* negative number.
* We assume a 2's complement machine
*/
case 'i':
case 'd':
if ((flagword & PADZERO) && (flagword & DOTSEEN))
flagword &= ~PADZERO; /* ignore 0 flag */
/* Set buffer pointer to last digit */
p = bp = buf + MAXLLDIGS;
/* Fetch the argument to be printed */
if (flagword & XLONG) { /* long long */
ll = va_arg(args.ap, long long);
/* If signed conversion, make sign */
if (ll < 0) {
prefix = _P_HYPHEN;
prefixlength = 1;
/*
* Negate, checking in advance for
* possible overflow.
*/
if (ll != HIBITLL)
ll = -ll;
else
/* number is -HIBITLL; convert last */
/* digit now and get positive number */
*--bp = _lowlldigit(&ll);
} else if (flagword & FPLUS) {
prefix = _P_PLUS;
prefixlength = 1;
} else if (flagword & FBLANK) {
prefix = _P_BLANK;
prefixlength = 1;
}
} else { /* not long long */
if (flagword & LENGTH)
val = va_arg(args.ap, long);
else
val = va_arg(args.ap, int);
if (flagword & SHORT)
val = (short)val;
else if (flagword & CHAR)
val = (char)val;
/* If signed conversion, make sign */
if (val < 0) {
prefix = _P_HYPHEN;
prefixlength = 1;
/*
* Negate, checking in advance
* for possible overflow.
*/
if (val != HIBITL)
val = -val;
/*
* number is -HIBITL; convert
* last digit now and get
* positive number
*/
else
*--bp = _lowdigit(&val);
} else if (flagword & FPLUS) {
prefix = _P_PLUS;
prefixlength = 1;
} else if (flagword & FBLANK) {
prefix = _P_BLANK;
prefixlength = 1;
}
}
decimal:
{
long qval = val;
long long lll = ll;
long long tll;
if (flagword & XLONG) {
if (lll < 10LL) {
#ifdef _WIDE
if (lll != 0LL || !(flagword & DOTSEEN))
*--bp = (wchar_t)lll + L'0';
#else /* _WIDE */
if (lll != 0LL || !(flagword & DOTSEEN))
*--bp = (char)lll + '0';
#endif /* _WIDE */
} else {
do {
tll = lll;
lll /= 10;
#ifdef _WIDE
*--bp = (wchar_t)
(tll - lll * 10 + '0');
#else /* _WIDE */
*--bp = (char) \
(tll - lll * 10 + '0');
#endif /* _WIDE */
} while (lll >= 10);
#ifdef _WIDE
*--bp = (wchar_t)lll + '0';
#else /* _WIDE */
*--bp = (char)lll + '0';
#endif /* _WIDE */
}
} else {
if (qval <= 9) {
#ifdef _WIDE
if (qval != 0 || !(flagword & DOTSEEN))
*--bp = (wchar_t)qval + '0';
#else /* _WIDE */
if (qval != 0 || !(flagword & DOTSEEN))
*--bp = (char)qval + '0';
#endif /* _WIDE */
} else {
do {
n = qval;
qval /= 10;
#ifdef _WIDE
*--bp = (wchar_t) \
(n - qval * 10 + '0');
#else /* _WIDE */
*--bp = (char) \
(n - qval * 10 + '0');
#endif /* _WIDE */
} while (qval > 9);
#ifdef _WIDE
*--bp = (wchar_t)qval + '0';
#else /* _WIDE */
*--bp = (char)qval + '0';
#endif /* _WIDE */
}
}
}
/* Handle the ' flag */
if (quote) {
p = insert_thousands_sep(bp, p);
}
/* Calculate minimum padding zero requirement */
if (flagword & DOTSEEN) {
leadzeroes = prec - (p - bp);
if (leadzeroes > 0) {
otherlength = lzero = leadzeroes;
flagword |= LZERO;
}
}
break;
case 'u':
if ((flagword & PADZERO) && (flagword & DOTSEEN))
flagword &= ~PADZERO; /* ignore 0 flag */
p = bp = buf + MAXLLDIGS;
/* Fetch the argument to be printed */
if (flagword & XLONG) {
ll = va_arg(args.ap, long long);
if (ll & HIBITLL)
*--bp = _lowlldigit(&ll);
} else {
if (flagword & LENGTH)
val = va_arg(args.ap, long);
else
val = va_arg(args.ap, unsigned);
if (flagword & SHORT)
val = (unsigned short)val;
else if (flagword & CHAR)
val = (unsigned char)val;
if (val & HIBITL)
*--bp = _lowdigit(&val);
}
goto decimal;
/*
* non-decimal fixed point representations
* for radix equal to a power of two
*
* "mradix" is one less than the radix for the conversion.
* "lradix" is one less than the base 2 log
* of the radix for the conversion. Conversion is unsigned.
* HIBITL is 100...000
* binary, and is equal to the maximum
* negative number.
* We assume a 2's complement machine
*/
case 'o':
mradix = 7;
lradix = 2;
/*
* DR151 and clarification in C90
* presence of '#' increases precision to first
* digit of the result to be zero
*/
if ((flagword & DOTSEEN) && (flagword & FSHARP) &&
prec == 0)
prec = 1;
goto fixed;
case 'p':
flagword &= ~(XLONG | SHORT);
flagword |= LENGTH;
/* FALLTHRU */
case 'X':
case 'x':
mradix = 15;
lradix = 3;
fixed:
if ((flagword & PADZERO) && (flagword & DOTSEEN))
flagword &= ~PADZERO; /* ignore 0 flag */
#ifdef _WIDE
/* Set translate table for digits */
tab = (wchar_t *)((fcode == 'X') ? uc_digs : lc_digs);
#else /* _WIDE */
/* Set translate table for digits */
tab = (fcode == 'X') ? uc_digs : lc_digs;
#endif /* _WIDE */
/* Fetch the argument to be printed */
if (flagword & XLONG) {
ll = va_arg(args.ap, long long);
} else {
if (flagword & LENGTH)
val = va_arg(args.ap, long);
else
val = va_arg(args.ap, unsigned);
if (flagword & SHORT)
val = (unsigned short) val;
else if (flagword & CHAR)
val = (unsigned char) val;
}
p = bp = buf + MAXLLDIGS;
/* Develop the digits of the value */
if (flagword & XLONG) {
long long lll = ll;
if (lll == 0LL) {
if (!(flagword & DOTSEEN)) {
otherlength = lzero = 1;
flagword |= LZERO;
}
} else do {
*--bp = tab[(ssize_t)(lll & mradix)];
lll = ((lll >> 1) & ~HIBITLL) \
>> lradix;
} while (lll != 0LL);
} else {
long qval = val;
if (qval == 0) {
if (!(flagword & DOTSEEN)) {
otherlength = lzero = 1;
flagword |= LZERO;
}
} else do {
*--bp = tab[qval & mradix];
qval = ((qval >> 1) & ~HIBITL) \
>> lradix;
} while (qval != 0);
}
/* Calculate minimum padding zero requirement */
if (flagword & DOTSEEN) {
leadzeroes = prec - (p - bp);
if (leadzeroes > 0) {
otherlength = lzero = leadzeroes;
flagword |= LZERO;
}
}
/* Handle the # flag, (val != 0) for int and long */
/* (ll!= 0) handles long long case */
if ((flagword & FSHARP) &&
(((flagword & XLONG) == 0 && val != 0) ||
((flagword & XLONG) == XLONG && ll != 0)))
switch (fcode) {
case 'o':
if (!(flagword & LZERO)) {
otherlength = lzero = 1;
flagword |= LZERO;
}
break;
case 'x':
prefix = _P_ZEROx;
prefixlength = 2;
break;
case 'X':
prefix = _P_ZEROX;
prefixlength = 2;
break;
}
break;
case 'A':
case 'a':
/* A format */
if (flagword & QUAD) {
long double qval = GETQVAL(args.ap);
/* establish default precision */
if (!(flagword & DOTSEEN))
#if defined(__sparc)
prec = HEXFP_QUAD_DIG - 1;
#elif defined(__i386) || defined(__amd64)
prec = HEXFP_EXTENDED_DIG - 1;
#else
#error Unknown architecture
#endif
FPCONV(__qaconvert, &qval,
min(prec + 1, MAXECVT), &exp, &sign,
cvtbuf);
} else {
double dval = va_arg(args.ap, double);
/* establish default precision */
if (!(flagword & DOTSEEN))
prec = HEXFP_DOUBLE_DIG - 1;
FPCONV(__aconvert, dval,
min(prec + 1, MAXECVT), &exp, &sign,
cvtbuf);
}
bp = cvtbuf;
/*
* The following is wide-character safe because
* __aconvert and __qaconvert only produce ASCII
* characters.
*/
if (!isxdigit((unsigned char)*bp)) {
inf_nan = 1;
break;
}
/*
* Create the prefix. We ought to use the strings
* defined above (_P_HYPHEN, etc.), but that would
* be awkward: we'd either have to define six more
* of them or we'd have to use strcpy/strcat to
* assemble the ones already defined. So instead,
* we just build the prefix character by character.
*/
p = prefix = prefixbuf;
if (sign) {
*p++ = '-';
prefixlength = 1;
} else if (flagword & FPLUS) {
*p++ = '+';
prefixlength = 1;
} else if (flagword & FBLANK) {
*p++ = ' ';
prefixlength = 1;
}
*p++ = '0';
*p++ = (fcode == 'A') ? 'X' : 'x';
*p = '\0';
prefixlength += 2;
/* put the first digit in the buffer */
p = &buf[0];
*p++ = (*bp != '\0') ? *bp++ : '0';
/* Put in a decimal point if needed */
if (prec != 0 || (flagword & FSHARP))
p = insert_decimal_point(p);
/* Create the rest of the mantissa */
rz = prec;
if (fcode == 'A') {
for (; rz > 0 && *bp != '\0'; --rz) {
*p++ = ('a' <= *bp && *bp <= 'f')?
*bp - 32 : *bp;
bp++;
}
} else {
for (; rz > 0 && *bp != '\0'; --rz)
*p++ = *bp++;
}
if (rz > 0) {
otherlength = rzero = rz;
flagword |= RZERO;
}
bp = &buf[0];
/*
* Create the exponent in right-to-left order.
* buf[0] == '0' if and only if the value being
* converted is exactly zero, in which case the
* exponent should be +0 regardless of exp.
*/
suffix = &expbuf[MAXESIZ];
*suffix = '\0';
if (buf[0] != '0') {
int nn;
nn = exp;
if (nn < 0)
nn = -nn;
for (; nn > 9; nn /= 10)
*--suffix = todigit(nn % 10);
*--suffix = todigit(nn);
*--suffix = (exp >= 0) ? '+' : '-';
} else {
*--suffix = '0';
*--suffix = '+';
}
/* put in the p */
*--suffix = (fcode == 'A') ? 'P' : 'p';
/* compute size of suffix */
suffixlength = &expbuf[MAXESIZ] - suffix;
otherlength += suffixlength;
flagword |= SUFFIX;
break;
case 'E':
case 'e':
/*
* E-format. The general strategy
* here is fairly easy: we take what
* econvert gives us and re-format it.
* (qeconvert for long double)
*/
/* Establish default precision */
if (!(flagword & DOTSEEN))
prec = 6;
if (flagword & QUAD) { /* long double */
long double qval = GETQVAL(args.ap);
FPCONV(qeconvert, &qval,
min(prec + 1, MAXECVT), &decpt, &sign,
cvtbuf);
} else { /* double */
double dval = va_arg(args.ap, double);
FPCONV(econvert, dval,
min(prec + 1, MAXECVT), &decpt, &sign,
cvtbuf);
}
bp = cvtbuf;
if (*bp > '9') {
inf_nan = 1;
inf_nan_mixed_case = (__xpg6 &
_C99SUSv3_mixed_case_Inf_and_NaN);
break;
}
/* Determine the prefix */
e_merge:
if (sign) {
prefix = _P_HYPHEN;
prefixlength = 1;
} else if (flagword & FPLUS) {
prefix = _P_PLUS;
prefixlength = 1;
} else if (flagword & FBLANK) {
prefix = _P_BLANK;
prefixlength = 1;
}
/* Place the first digit in the buffer */
p = &buf[0];
*p++ = (*bp != '\0') ? *bp++ : '0';
/* Put in a decimal point if needed */
if (prec != 0 || (flagword & FSHARP))
p = insert_decimal_point(p);
/* Create the rest of the mantissa */
rz = prec;
for (; rz > 0 && *bp != '\0'; --rz)
*p++ = *bp++;
if (rz > 0) {
otherlength = rzero = rz;
flagword |= RZERO;
}
bp = &buf[0];
/*
* Create the exponent. buf[0] == '0' if and
* only if the value being converted is exactly
* zero, in which case the exponent should be
* +0 regardless of decpt.
*/
*(suffix = &expbuf[MAXESIZ]) = '\0';
if (buf[0] != '0') {
int nn = decpt - 1;
if (nn < 0)
nn = -nn;
for (; nn > 9; nn /= 10)
*--suffix = todigit(nn % 10);
*--suffix = todigit(nn);
}
/* Prepend leading zeroes to the exponent */
while (suffix > &expbuf[MAXESIZ - 2])
*--suffix = '0';
/* Put in the exponent sign */
*--suffix = (decpt > 0 || buf[0] == '0') ? '+' : '-';
/* Put in the e */
*--suffix = _M_ISUPPER(fcode) ? 'E' : 'e';
/* compute size of suffix */
otherlength += (suffixlength = &expbuf[MAXESIZ] \
- suffix);
flagword |= SUFFIX;
break;
case 'F':
case 'f':
/*
* F-format floating point. This is a
* good deal less simple than E-format.
* The overall strategy will be to call
* fconvert, reformat its result into buf,
* and calculate how many trailing
* zeroes will be required. There will
* never be any leading zeroes needed.
* (qfconvert for long double)
*/
/* Establish default precision */
if (!(flagword & DOTSEEN))
prec = 6;
if (flagword & QUAD) { /* long double */
long double qval = GETQVAL(args.ap);
FPCONV(qfconvert, &qval, min(prec, MAXFCVT),
&decpt, &sign, cvtbuf);
bp = cvtbuf;
if (*bp == 0) {
/*
* qfconvert would have required
* too many characters; use qeconvert
* instead
*/
FPCONV(qeconvert, &qval,
min(prec + 1, MAXECVT), &decpt,
&sign, cvtbuf);
goto e_merge;
}
} else { /* double */
double dval = va_arg(args.ap, double);
FPCONV(fconvert, dval, min(prec, MAXFCVT),
&decpt, &sign, cvtbuf);
}
bp = cvtbuf;
if (*bp > '9') {
inf_nan = 1;
if (fcode == 'f')
inf_nan_mixed_case = (__xpg6 &
_C99SUSv3_mixed_case_Inf_and_NaN);
break;
}
/* Determine the prefix */
f_merge:
if (sign) {
prefix = _P_HYPHEN;
prefixlength = 1;
} else if (flagword & FPLUS) {
prefix = _P_PLUS;
prefixlength = 1;
} else if (flagword & FBLANK) {
prefix = _P_BLANK;
prefixlength = 1;
}
/* Initialize buffer pointer */
p = &buf[0];
{
ssize_t nn = decpt;
/* Emit the digits before the decimal point */
k = 0;
do {
*p++ = (nn <= 0 || *bp == '\0' || \
k >= MAXFSIG) ? '0' : (k++, *bp++);
} while (--nn > 0);
if (quote)
p = insert_thousands_sep(buf, p);
/* Put in a decimal point if needed */
if (prec > 0 || (flagword & FSHARP))
p = insert_decimal_point(p);
/* Digits (if any) after the decimal point */
nn = min(prec, MAXFCVT);
if (prec > nn) {
flagword |= RZERO;
otherlength = rzero = prec - nn;
}
while (--nn >= 0)
*p++ = (++decpt <= 0 || *bp == '\0' || \
k >= MAXFSIG) ? '0' : (k++, *bp++);
}
bp = &buf[0];
break;
case 'G':
case 'g':
/*
* g-format. We play around a bit
* and then jump into e or f, as needed.
*/
/* Establish default precision */
if (!(flagword & DOTSEEN))
prec = 6;
else if (prec == 0)
prec = 1;
if (flagword & QUAD) { /* long double */
long double qval = GETQVAL(args.ap);
FPCONV(qeconvert, &qval, min(prec, MAXECVT),
&decpt, &sign, cvtbuf);
} else { /* double */
double dval = va_arg(args.ap, double);
FPCONV(econvert, dval, min(prec, MAXECVT),
&decpt, &sign, cvtbuf);
}
bp = cvtbuf;
if (*bp > '9') {
inf_nan = 1;
inf_nan_mixed_case = (__xpg6 &
_C99SUSv3_mixed_case_Inf_and_NaN);
break;
}
if (*bp == '0') /* the value converted is zero */
decpt = 1;
{
int kk = prec;
if (!(flagword & FSHARP)) {
#ifdef _WIDE
n = wcslen(bp);
#else /* _WIDE */
n = strlen(bp);
#endif /* _WIDE */
if (n < kk)
kk = (int)n;
while (kk >= 1 && bp[kk-1] == '0')
--kk;
}
if (decpt < -3 || decpt > prec) {
prec = kk - 1;
goto e_merge;
}
prec = kk - decpt;
goto f_merge;
}
case '%':
buf[0] = fcode;
goto c_merge;
#ifndef _WIDE
case 'w':
wflag = 1;
goto charswitch;
#endif /* _WIDE */
case 'C': /* XPG XSH4 extention */
wide_C:
{
wchar_t temp;
temp = va_arg(args.ap, wchar_t);
#ifdef _WIDE
if (temp) {
buf[0] = temp;
p = (bp = buf) + 1;
} else {
buf[0] = 0;
p = (bp = buf) + 1;
}
wcount = 1;
wflag = 1;
#else /* _WIDE */
if (temp) {
retcode = wctomb(buf, temp);
if (retcode == -1) {
errno = EILSEQ;
return (EOF);
} else {
p = (bp = buf) + retcode;
}
} else { /* NULL character */
buf[0] = 0;
p = (bp = buf) + 1;
}
wcount = p - bp;
#endif /* _WIDE */
}
break;
case 'c':
if (lflag) {
goto wide_C;
}
#ifndef _WIDE
if (wflag) {
wchar_t temp;
temp = va_arg(args.ap, wchar_t);
if (temp) {
retcode = wctomb(buf, temp);
if (retcode == -1) {
p = (bp = buf) + 1;
} else {
p = (bp = buf) + retcode;
}
} else { /* NULL character */
buf[0] = 0;
p = (bp = buf) + 1;
}
wcount = p - bp;
} else {
#endif /* _WIDE */
if (flagword & XLONG) {
long long temp;
temp = va_arg(args.ap, long long);
#ifdef _WIDE
buf[0] = (wchar_t)temp;
#else /* _WIDE */
buf[0] = (char)temp;
#endif /* _WIDE */
} else
buf[0] = va_arg(args.ap, int);
c_merge:
p = (bp = &buf[0]) + 1;
#ifdef _WIDE
wcount = 1;
wflag = 1;
#endif /* _WIDE */
#ifndef _WIDE
}
#endif /* _WIDE */
break;
case 'S': /* XPG XSH4 extention */
wide_S:
#ifdef _WIDE
if (!lflag) {
lflag++;
}
bp = va_arg(args.ap, wchar_t *);
if (bp == NULL)
bp = (wchar_t *)widenullstr;
if (!(flagword & DOTSEEN)) {
/* wide character handling */
prec = MAXINT;
}
wp = bp;
wcount = 0;
while (*wp) {
if ((prec - wcount - 1) >= 0) {
wcount++;
wp++;
} else {
break;
}
}
p = wp;
wflag = 1;
break;
#else /* _WIDE */
if (!wflag)
wflag++;
bp = va_arg(args.ap, char *);
if (bp == NULL)
bp = (char *)widenullstr;
if (!(flagword & DOTSEEN)) {
/* wide character handling */
prec = MAXINT;
}
wp = (wchar_t *)(uintptr_t)bp;
wcount = 0;
while (*wp) {
int nbytes;
nbytes = wctomb(tmpbuf, *wp);
if (nbytes < 0) {
errno = EILSEQ;
return (EOF);
}
if ((prec - (wcount + nbytes)) >= 0) {
wcount += nbytes;
wp++;
} else {
break;
}
}
sec_display = wcount;
p = (char *)wp;
break;
#endif /* _WIDE */
case 's':
if (lflag) {
goto wide_S;
}
#ifdef _WIDE
cbp = va_arg(args.ap, char *);
if (cbp == NULL)
cbp = (char *)nullstr;
if (!(flagword & DOTSEEN)) {
size_t nwc;
wchar_t *wstr;
nwc = mbstowcs(NULL, cbp, 0);
if (nwc == (size_t)-1) {
errno = EILSEQ;
return (EOF);
}
bpsize = sizeof (wchar_t) * (nwc + 1);
wstr = (wchar_t *)lmalloc(bpsize);
if (wstr == NULL) {
errno = EILSEQ;
return (EOF);
}
nwc = mbstowcs(wstr, cbp, MAXINT);
wcount = nwc;
bp = wstr;
p = wstr + nwc;
} else {
size_t nwc;
wchar_t *wstr;
nwc = mbstowcs(NULL, cbp, 0);
if (nwc == (size_t)-1) {
errno = EILSEQ;
return (EOF);
}
if (prec > nwc) {
bpsize = sizeof (wchar_t) * nwc;
wstr = (wchar_t *)lmalloc(bpsize);
if (wstr == NULL) {
errno = ENOMEM;
return (EOF);
}
nwc = mbstowcs(wstr, cbp, nwc);
wcount = nwc;
bp = wstr;
p = wstr + nwc;
} else {
wchar_t *wstr;
bpsize = sizeof (wchar_t) * prec;
wstr = (wchar_t *)lmalloc(bpsize);
if (wstr == NULL) {
errno = ENOMEM;
return (EOF);
}
nwc = mbstowcs(wstr, cbp, prec);
wcount = prec;
bp = wstr;
p = wstr + nwc;
}
}
wflag = 1;
#else /* _WIDE */
bp = va_arg(args.ap, char *);
if (bp == NULL)
bp = (char *)nullstr;
if (!(flagword & DOTSEEN)) {
if (wflag) {
/* wide character handling */
prec = MAXINT;
goto wide_hand;
}
p = bp + strlen(bp);
/*
* sec_display only needed if width
* is specified (ie, "%<width>s")
* Solaris behavior counts <width> in
* screen column width. (If XPG4 behavior,
* <width> is counted in bytes.)
*/
if (width > 0 && __xpg4 == 0 &&
MB_CUR_MAX > 1) {
#define NW 256
wchar_t wbuff[NW];
wchar_t *wp, *wptr;
size_t wpsize;
size_t nwc;
wp = NULL;
if ((nwc = mbstowcs(wbuff, bp,
NW)) == (size_t)-1) {
/* Estimate width */
sec_display = strlen(bp);
goto mbs_err;
}
if (nwc < NW) {
wptr = wbuff;
} else {
/*
* If widechar does not fit into
* wbuff, allocate larger buffer
*/
if ((nwc =
mbstowcs(NULL, bp, 0)) ==
(size_t)-1) {
sec_display =
strlen(bp);
goto mbs_err;
}
wpsize = (nwc + 1) *
sizeof (wchar_t);
if ((wp = lmalloc(wpsize))
== NULL) {
errno = ENOMEM;
return (EOF);
}
if ((nwc = mbstowcs(wp,
bp, nwc)) == (size_t)-1) {
sec_display = \
strlen(bp);
goto mbs_err;
}
wptr = wp;
}
sec_display = wcswidth(wptr, nwc);
if (sec_display == -1) {
sec_display =
_rec_scrswidth(wptr, nwc);
}
mbs_err:
if (wp)
lfree(wp, wpsize);
}
} else { /* a strnlen function would be useful here! */
/*
* If we've seen a dot, and count has been set
* to 0, then we don't output in any cases
* below. prec should be always >= 0. So we only
* check to see if it's zero.
*/
if (prec == 0) {
p = bp;
break;
}
if (wflag) {
/* wide character handling */
wide_hand:
wp = (wchar_t *)(uintptr_t)bp;
preco = prec;
wcount = 0;
while (*wp &&
(prec -= _scrwidth(*wp)) >= 0) {
if ((retcode =
wctomb(tmpbuf, *wp)) < 0)
wcount++;
else
wcount += retcode;
wp++;
}
if (*wp)
prec += _scrwidth(*wp);
p = (char *)wp;
sec_display = preco - prec;
} else if (__xpg4 == 0 && MB_CUR_MAX > 1) {
/*
* Solaris behavior - count
* precision as screen column width
*/
char *qp = bp;
int ncol, nbytes;
wchar_t wc;
ncol = 0;
preco = prec;
while (*qp) {
if (isascii(*qp)) {
qp++;
if (--prec == 0)
break;
continue;
}
if ((nbytes = mbtowc(&wc, qp,
MB_LEN_MAX)) == -1) {
/* print illegal char */
nbytes = 1;
ncol = 1;
} else {
ncol = _scrwidth(wc);
if (ncol == 0) {
ncol = 1;
}
}
if ((prec -= ncol) >= 0) {
qp += nbytes;
if (prec == 0)
break;
} else {
break;
}
}
if (prec < 0)
prec += ncol;
p = qp;
sec_display = preco - prec;
} else {
/*
* XPG4 behavior - count
* precision as bytes.
* We don't use strlen() because
* the given char string may not
* be null-terminated.
*/
char *qp;
qp = memchr(bp, '\0', prec);
if (qp == NULL) {
p = bp + prec;
} else {
p = qp;
}
}
}
#endif /* _WIDE */
break;
case 'n':
{
if (flagword & XLONG) {
long long *svcount;
svcount = va_arg(args.ap, long long *);
*svcount = (long long)count;
} else if (flagword & LENGTH) {
long *svcount;
svcount = va_arg(args.ap, long *);
*svcount = (long)count;
} else if (flagword & SHORT) {
short *svcount;
svcount = va_arg(args.ap, short *);
*svcount = (short)count;
} else if (flagword & CHAR) {
char *svcount;
svcount = va_arg(args.ap, char *);
*svcount = (char)count;
} else {
int *svcount;
svcount = va_arg(args.ap, int *);
*svcount = count;
}
continue;
}
default: /* this is technically an error; what we do is to */
/* back up the format pointer to the offending char */
/* and continue with the format scan */
format--;
continue;
}
if (inf_nan) {
if (inf_nan_mixed_case) {
/* advance p */
for (p = bp + 1; *p != '\0'; p++)
;
} else {
int upper;
/* advance p and make output all one case */
upper = _M_ISUPPER(fcode);
for (p = bp; *p != '\0'; p++)
*p = upper? toupper(*p) : tolower(*p);
}
if (sign) {
prefix = _P_HYPHEN;
prefixlength = 1;
} else if (flagword & FPLUS) {
prefix = _P_PLUS;
prefixlength = 1;
} else if (flagword & FBLANK) {
prefix = _P_BLANK;
prefixlength = 1;
}
inf_nan = 0;
inf_nan_mixed_case = 0;
flagword &= ~PADZERO; /* ignore 0 flag */
}
/* Calculate number of padding blanks */
n = p - bp; /* n == size of the converted value (in bytes) */
#ifdef _WIDE
k = n;
#else /* _WIDE */
if (sec_display) /* when format is %s or %ws or %S */
k = sec_display;
else
k = n;
#endif /* _WIDE */
/*
* k is the (screen) width or # of bytes of the converted value
*/
k += prefixlength + otherlength;
#ifdef _WIDE
if (wflag) {
count += wcount;
} else {
count += n;
}
#else /* _WIDE */
/*
* update count which is the overall size of the output data
* and passed to memchr()
*/
if (wflag)
/*
* when wflag != 0 (i.e. %ws or %wc), the size of the
* converted value is wcount bytes
*/
count += wcount;
else
/*
* when wflag == 0, the size of the converted
* value is n (= p-bp) bytes
*/
count += n;
#endif /* _WIDE */
count += prefixlength + otherlength;
if (width > k) {
count += (width - k);
/*
* Set up for padding zeroes if requested
* Otherwise emit padding blanks unless output is
* to be left-justified.
*/
if (flagword & PADZERO) {
if (!(flagword & LZERO)) {
flagword |= LZERO;
lzero = width - k;
} else
lzero += width - k;
k = width; /* cancel padding blanks */
} else
/* Blanks on left if required */
if (!(flagword & FMINUS))
PAD(_blanks, width - k);
}
/* Prefix, if any */
if (prefixlength != 0)
PUT(prefix, prefixlength);
/* Zeroes on the left */
if ((flagword & LZERO)) /* && */
/* (!(flagword & SHORT) || !(flagword & FMINUS)) */
PAD(_zeroes, lzero);
#ifdef _WIDE
if (n > 0)
PUT(bp, n);
if ((fcode == 's') && !lflag) {
if (bp)
lfree(bp, bpsize);
}
#else /* _WIDE */
/* The value itself */
if ((fcode == 's' || fcode == 'S') && wflag) {
/* wide character handling */
wchar_t *wp = (wchar_t *)(uintptr_t)bp;
int cnt;
char *bufp;
long printn;
printn = (wchar_t *)(uintptr_t)p -
(wchar_t *)(uintptr_t)bp;
bufp = buf;
while (printn > 0) {
if ((cnt = wctomb(buf, *wp)) < 0)
cnt = 1;
PUT(bufp, cnt);
wp++;
printn--;
}
} else { /* non wide character value */
if (n > 0)
PUT(bp, n);
}
#endif /* _WIDE */
if (flagword & (RZERO | SUFFIX | FMINUS)) {
/* Zeroes on the right */
if (flagword & RZERO)
PAD(_zeroes, rzero);
/* The suffix */
if (flagword & SUFFIX)
PUT(suffix, suffixlength);
/* Blanks on the right if required */
if (flagword & FMINUS && width > k)
PAD(_blanks, width - k);
}
}
}
#ifdef _WIDE
static int
_watoi(wchar_t *fmt)
{
int n = 0;
wchar_t ch;
ch = *fmt;
if (_M_ISDIGIT(ch)) {
n = ch - '0';
ch = *++fmt;
while (_M_ISDIGIT(ch)) {
n *= 10;
n += ch - '0';
ch = *++fmt;
}
}
return (n);
}
#endif /* _WIDE */
/*
* This function initializes arglst, to contain the appropriate va_list values
* for the first MAXARGS arguments.
*/
/*
* Type modifier flags:
* 0x01 for long
* 0x02 for int
* 0x04 for long long
* 0x08 for long double
*/
#define FLAG_LONG 0x01
#define FLAG_INT 0x02
#define FLAG_LONG_LONG 0x04
#define FLAG_LONG_DBL 0x08
#ifdef _WIDE
static void
_wmkarglst(wchar_t *fmt, stva_list args, stva_list arglst[],
int prflag __unused)
#else /* _WIDE */
static void
_mkarglst(char *fmt, stva_list args, stva_list arglst[], int prflag __unused)
#endif /* _WIDE */
{
#ifdef _WIDE
static const wchar_t digits[] = L"01234567890";
static const wchar_t skips[] = L"# +-.'0123456789h$";
#else /* _WIDE */
static const char digits[] = "01234567890";
static const char skips[] = "# +-.'0123456789h$";
#endif /* _WIDE */
enum types {INT = 1, LONG, CHAR_PTR, DOUBLE, LONG_DOUBLE, VOID_PTR,
LONG_PTR, INT_PTR, LONG_LONG, LONG_LONG_PTR};
enum types typelst[MAXARGS], curtype;
ssize_t n;
int maxnum, curargno, flags;
/*
* Algorithm 1. set all argument types to zero.
* 2. walk through fmt putting arg types in typelst[].
* 3. walk through args using va_arg(args.ap, typelst[n])
* and set arglst[] to the appropriate values.
* Assumptions: Cannot use %*$... to specify variable position.
*/
(void) memset((void *) typelst, 0, sizeof (typelst));
maxnum = -1;
curargno = 0;
while ((fmt = STRCHR(fmt, '%')) != 0) {
fmt++; /* skip % */
if (fmt[n = STRSPN(fmt, digits)] == '$') {
/* convert to zero base */
curargno = ATOI(fmt) - 1;
if (curargno < 0)
continue;
fmt += n + 1;
}
flags = 0;
again:;
fmt += STRSPN(fmt, skips);
switch (*fmt++) {
case '%': /* there is no argument! */
continue;
case 'l':
if (flags & (FLAG_LONG | FLAG_LONG_LONG)) {
flags |= FLAG_LONG_LONG;
flags &= ~FLAG_LONG;
} else {
flags |= FLAG_LONG;
}
goto again;
case 'j':
#ifndef _LP64
/*
* *printf_c89() in 32-bit libc uses
* 32-bit intmax_t; otherwise intmax_t
* is 64-bits.
*/
if (!(prflag & _F_INTMAX32)) {
#endif
flags |= FLAG_LONG_LONG; /* 64-bit */
#ifndef _LP64
}
#endif
goto again;
case 't':
flags |= FLAG_LONG;
goto again;
case 'z':
flags |= FLAG_LONG;
goto again;
case 'L':
flags |= FLAG_LONG_DBL;
goto again;
case '*': /* int argument used for value */
/* check if there is a positional parameter */
#ifdef _WIDE
if ((*fmt >= 0) && (*fmt < 256) && isdigit(*fmt))
#else /* _WIDE */
if (isdigit(*fmt))
#endif /* _WIDE */
{
int targno;
targno = ATOI(fmt) - 1;
fmt += STRSPN(fmt, digits);
if (*fmt == '$')
fmt++; /* skip '$' */
if (targno >= 0 && targno < MAXARGS) {
typelst[targno] = INT;
if (maxnum < targno)
maxnum = targno;
}
goto again;
}
flags |= FLAG_INT;
curtype = INT;
break;
case 'a':
case 'A':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
if (flags & FLAG_LONG_DBL)
curtype = LONG_DOUBLE;
else
curtype = DOUBLE;
break;
case 's':
curtype = CHAR_PTR;
break;
case 'p':
curtype = VOID_PTR;
break;
case 'n':
if (flags & FLAG_LONG_LONG)
curtype = LONG_LONG_PTR;
else if (flags & FLAG_LONG)
curtype = LONG_PTR;
else
curtype = INT_PTR;
break;
default:
if (flags & FLAG_LONG_LONG)
curtype = LONG_LONG;
else if (flags & FLAG_LONG)
curtype = LONG;
else
curtype = INT;
break;
}
if (curargno >= 0 && curargno < MAXARGS) {
typelst[curargno] = curtype;
if (maxnum < curargno)
maxnum = curargno;
}
curargno++; /* default to next in list */
if (flags & FLAG_INT) /* took care of *, keep going */
{
flags ^= FLAG_INT;
goto again;
}
}
for (n = 0; n <= maxnum; n++) {
arglst[n] = args;
if (typelst[n] == 0)
typelst[n] = INT;
switch (typelst[n]) {
case INT:
(void) va_arg(args.ap, int);
break;
case LONG:
(void) va_arg(args.ap, long);
break;
case CHAR_PTR:
(void) va_arg(args.ap, char *);
break;
case DOUBLE:
(void) va_arg(args.ap, double);
break;
case LONG_DOUBLE:
(void) GETQVAL(args.ap);
break;
case VOID_PTR:
(void) va_arg(args.ap, void *);
break;
case LONG_PTR:
(void) va_arg(args.ap, long *);
break;
case INT_PTR:
(void) va_arg(args.ap, int *);
break;
case LONG_LONG:
(void) va_arg(args.ap, long long);
break;
case LONG_LONG_PTR:
(void) va_arg(args.ap, long long *);
break;
}
}
}
/*
* This function is used to find the va_list value for arguments whose
* position is greater than MAXARGS. This function is slow, so hopefully
* MAXARGS will be big enough so that this function need only be called in
* unusual circumstances.
* pargs is assumed to contain the value of arglst[MAXARGS - 1].
*/
#ifdef _WIDE
static void
_wgetarg(wchar_t *fmt, stva_list *pargs, long argno, int prflag __unused)
#else /* _WIDE */
void
_getarg(char *fmt, stva_list *pargs, long argno, int prflag __unused)
#endif /* _WIDE */
{
#ifdef _WIDE
static const wchar_t digits[] = L"01234567890";
static const wchar_t skips[] = L"# +-.'0123456789h$";
wchar_t *sfmt = fmt;
#else /* _WIDE */
static const char digits[] = "01234567890";
static const char skips[] = "# +-.'0123456789h$";
char *sfmt = fmt;
#endif /* _WIDE */
ssize_t n;
int i, curargno, flags;
int found = 1;
i = MAXARGS;
curargno = 1;
while (found) {
fmt = sfmt;
found = 0;
while ((i != argno) && (fmt = STRCHR(fmt, '%')) != 0) {
fmt++; /* skip % */
if (fmt[n = STRSPN(fmt, digits)] == '$') {
curargno = ATOI(fmt);
if (curargno <= 0)
continue;
fmt += n + 1;
}
/* find conversion specifier for next argument */
if (i != curargno) {
curargno++;
continue;
} else
found = 1;
flags = 0;
again:;
fmt += STRSPN(fmt, skips);
switch (*fmt++) {
case '%': /* there is no argument! */
continue;
case 'l':
if (flags & (FLAG_LONG | FLAG_LONG_LONG)) {
flags |= FLAG_LONG_LONG;
flags &= ~FLAG_LONG;
} else {
flags |= FLAG_LONG;
}
goto again;
case 'j':
#ifndef _LP64
/*
* *printf_c89() in 32-bit libc uses
* 32-bit intmax_t; otherwise intmax_t
* is 64-bits.
*/
if (!(prflag & _F_INTMAX32)) {
#endif
flags |= FLAG_LONG_LONG; /* 64-bit */
#ifndef _LP64
}
#endif
goto again;
case 't':
flags |= FLAG_LONG;
goto again;
case 'z':
flags |= FLAG_LONG;
goto again;
case 'L':
flags |= FLAG_LONG_DBL;
goto again;
case '*': /* int argument used for value */
/*
* check if there is a positional parameter;
* if so, just skip it; its size will be
* correctly determined by default
*/
if (_M_ISDIGIT(*fmt)) {
fmt += STRSPN(fmt, digits);
if (*fmt == '$')
fmt++; /* skip '$' */
goto again;
}
flags |= FLAG_INT;
(void) va_arg((*pargs).ap, int);
break;
case 'a':
case 'A':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
if (flags & FLAG_LONG_DBL)
(void) GETQVAL((*pargs).ap);
else
(void) va_arg((*pargs).ap, double);
break;
case 's':
(void) va_arg((*pargs).ap, char *);
break;
case 'p':
(void) va_arg((*pargs).ap, void *);
break;
case 'n':
if (flags & FLAG_LONG_LONG)
(void) va_arg((*pargs).ap, long long *);
else if (flags & FLAG_LONG)
(void) va_arg((*pargs).ap, long *);
else
(void) va_arg((*pargs).ap, int *);
break;
default:
if (flags & FLAG_LONG_LONG)
(void) va_arg((*pargs).ap, long long);
else if (flags & FLAG_LONG)
(void) va_arg((*pargs).ap, long int);
else
(void) va_arg((*pargs).ap, int);
break;
}
i++;
curargno++; /* default to next in list */
if (flags & FLAG_INT) /* took care of *, keep going */
{
flags ^= FLAG_INT;
goto again;
}
}
/* missing specifier for parameter, assume param is an int */
if (!found && i != argno) {
(void) va_arg((*pargs).ap, int);
i++;
curargno = i;
found = 1;
}
}
}
#ifdef _WIDE
static wchar_t *
insert_thousands_sep(wchar_t *bp, wchar_t *ep)
#else /* _WIDE */
static char *
insert_thousands_sep(char *bp, char *ep)
#endif /* _WIDE */
{
char thousep;
struct lconv *locptr;
ssize_t buf_index;
int i;
#ifdef _WIDE
wchar_t *obp = bp;
wchar_t buf[371];
wchar_t *bufptr = buf;
#else /* _WIDE */
char *obp = bp;
char buf[371];
char *bufptr = buf;
#endif /* _WIDE */
char *grp_ptr;
/* get the thousands sep. from the current locale */
locptr = localeconv();
thousep = *locptr->thousands_sep;
grp_ptr = locptr->grouping;
/* thousands sep. not use in this locale or no grouping required */
if (!thousep || (*grp_ptr == '\0'))
return (ep);
buf_index = ep - bp;
for (;;) {
if (*grp_ptr == CHAR_MAX) {
for (i = 0; i < buf_index--; i++)
*bufptr++ = *(bp + buf_index);
break;
}
for (i = 0; i < *grp_ptr && buf_index-- > 0; i++)
*bufptr++ = *(bp + buf_index);
if (buf_index > 0) {
#ifdef _WIDE
*bufptr++ = (wchar_t)thousep;
#else /* _WIDE */
*bufptr++ = thousep;
#endif /* _WIDE */
ep++;
}
else
break;
if (*(grp_ptr + 1) != '\0')
++grp_ptr;
}
/* put the string in the caller's buffer in reverse order */
--bufptr;
while (buf <= bufptr)
*obp++ = *bufptr--;
return (ep);
}
#ifdef _WIDE
static wchar_t *
insert_decimal_point(wchar_t *ep)
#else
static char *
insert_decimal_point(char *ep)
#endif
{
struct lconv *locptr = localeconv();
char *dp = locptr->decimal_point;
#ifdef _WIDE
wchar_t wdp;
(void) mbtowc(&wdp, dp, MB_CUR_MAX);
*ep = wdp;
return (ep + 1);
#else
(void) memcpy(ep, dp, strlen(dp));
return (ep + strlen(dp));
#endif
}
/*
* Recovery scrswidth function -
* this variant of wcswidth() accepts non-printable or illegal
* widechar characters.
*/
static int
_rec_scrswidth(wchar_t *wp, ssize_t n)
{
int col;
int i;
col = 0;
while (*wp && (n-- > 0)) {
if ((i = _scrwidth(*wp++)) == 0)
i = 1;
col += i;
}
return (col);
}
|