summaryrefslogtreecommitdiff
path: root/lib/sh/snprintf.c
blob: d681b1655671869aab0ec6934192b5fe248abf28 (plain)
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
/* snprintf - formatted output to strings, with bounds checking and allocation */

/*
 build a test version with
   gcc -g -DDRIVER -I../.. -I../../include -o test-snprintf snprintf.c fmtu*long.o
*/
 
/*
   Unix snprintf implementation.
   derived from inetutils/libinetutils/snprintf.c Version 1.1

   Copyright (C) 2001,2006 Free Software Foundation, Inc.

   This file is part of GNU Bash, the Bourne Again SHell.

   Bash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Bash 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
   
   Revision History:

   1.1:
      *  added changes from Miles Bader
      *  corrected a bug with %f
      *  added support for %#g
      *  added more comments :-)
   1.0:
      *  supporting must ANSI syntaxic_sugars
   0.0:
      *  support %s %c %d

 THANKS(for the patches and ideas):
     Miles Bader
     Cyrille Rustom
     Jacek Slabocewiz
     Mike Parker(mouse)

*/

/*
 * Currently doesn't handle (and bash/readline doesn't use):
 *	* *M$ width, precision specifications
 *	* %N$ numbered argument conversions
 *	* inf, nan floating values imperfect (if isinf(), isnan() not in libc)
 *	* support for `F' is imperfect with ldfallback(), since underlying
 *	  printf may not handle it -- should ideally have another autoconf test
 */

#define FLOATING_POINT

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* GCC 4.2 on Snow Leopard doesn't like the snprintf prototype */
#if defined(DEBUG) && !defined (MACOSX)
#  undef HAVE_SNPRINTF
#  undef HAVE_ASPRINTF

#  define HAVE_SNPRINTF 0
#  define HAVE_ASPRINTF 0
#endif

#if defined(DRIVER) && !defined(HAVE_CONFIG_H)
#define HAVE_LONG_LONG
#define HAVE_LONG_DOUBLE
#ifdef __linux__
#define HAVE_PRINTF_A_FORMAT
#endif
#define HAVE_ISINF_IN_LIBC
#define HAVE_ISNAN_IN_LIBC
#define PREFER_STDARG
#define HAVE_STRINGIZE
#define HAVE_LIMITS_H
#define HAVE_STDDEF_H
#define HAVE_LOCALE_H
#define intmax_t long
#endif

#if !HAVE_SNPRINTF || !HAVE_ASPRINTF

#include <bashtypes.h>

#if defined(PREFER_STDARG)
#  include <stdarg.h>
#else
#  include <varargs.h>
#endif

#ifdef HAVE_LIMITS_H
#  include <limits.h>
#endif
#include <bashansi.h>
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
#include <chartypes.h>

#ifdef HAVE_STDINT_H
#  include <stdint.h>
#endif

#ifdef FLOATING_POINT
#  include <float.h>	/* for manifest constants */
#  include <stdio.h>	/* for sprintf */
#endif

#include <typemax.h>

#ifdef HAVE_LOCALE_H
#  include <locale.h>
#endif

#include "stdc.h"
#include <shmbutil.h>

#ifndef DRIVER
#  include "shell.h"
#else
#  define FL_PREFIX     0x01    /* add 0x, 0X, or 0 prefix as appropriate */
#  define FL_ADDBASE    0x02    /* add base# prefix to converted value */
#  define FL_HEXUPPER   0x04    /* use uppercase when converting to hex */
#  define FL_UNSIGNED   0x08    /* don't add any sign */
extern char *fmtulong __P((unsigned long int, int, char *, size_t, int));
extern char *fmtullong __P((unsigned long long int, int, char *, size_t, int));
#endif

#ifndef FREE
#  define FREE(x)	if (x) free (x)
#endif

/* Bound on length of the string representing an integer value of type T.
   Subtract one for the sign bit if T is signed;
   302 / 1000 is log10 (2) rounded up;
   add one for integer division truncation;
   add one more for a minus sign if t is signed.  */
#define INT_STRLEN_BOUND(t) \
  ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 \
     + 1 + TYPE_SIGNED (t))

/* conversion flags */
#define PF_ALTFORM	0x00001		/* # */
#define PF_HEXPREFIX	0x00002		/* 0[Xx] */
#define PF_LADJUST	0x00004		/* - */
#define PF_ZEROPAD	0x00008		/* 0 */
#define PF_PLUS		0x00010		/* + */
#define PF_SPACE	0x00020		/* ' ' */
#define PF_THOUSANDS	0x00040		/* ' */

#define PF_DOT		0x00080		/* `.precision' */
#define PF_STAR_P	0x00100		/* `*' after precision */
#define PF_STAR_W	0x00200		/* `*' before or without precision */

/* length modifiers */
#define PF_SIGNEDCHAR	0x00400		/* hh */
#define PF_SHORTINT	0x00800		/* h */
#define PF_LONGINT	0x01000		/* l */
#define PF_LONGLONG	0x02000		/* ll */
#define PF_LONGDBL	0x04000		/* L */
#define PF_INTMAX_T	0x08000		/* j */
#define PF_SIZE_T	0x10000		/* z */
#define PF_PTRDIFF_T	0x20000		/* t */

#define PF_ALLOCBUF	0x40000		/* for asprintf, vasprintf */

#define PFM_SN		0x01		/* snprintf, vsnprintf */
#define PFM_AS		0x02		/* asprintf, vasprintf */

#define ASBUFSIZE	128

#define x_digs	"0123456789abcdef"
#define X_digs	"0123456789ABCDEF"

static char intbuf[INT_STRLEN_BOUND(unsigned long) + 1];

static int decpoint;
static int thoussep;
static char *grouping;

/* 
 * For the FLOATING POINT FORMAT :
 *  the challenge was finding a way to
 *  manipulate the Real numbers without having
 *  to resort to mathematical function(it
 *  would require to link with -lm) and not
 *  going down to the bit pattern(not portable)
 *
 *  so a number, a real is:

      real = integral + fraction

      integral = ... + a(2)*10^2 + a(1)*10^1 + a(0)*10^0
      fraction = b(1)*10^-1 + b(2)*10^-2 + ...

      where:
       0 <= a(i) => 9 
       0 <= b(i) => 9 
 
    from then it was simple math
 */

/*
 * size of the buffer for the integral part
 * and the fraction part 
 */
#define MAX_INT  99 + 1 /* 1 for the null */
#define MAX_FRACT 307 + 1

/* 
 * These functions use static buffers to store the results,
 * and so are not reentrant
 */
#define itoa(n) fmtulong(n, 10, intbuf, sizeof(intbuf), 0);
#define dtoa(n, p, f) numtoa(n, 10, p, f)

#define SWAP_INT(a,b) {int t; t = (a); (a) = (b); (b) = t;}

#define GETARG(type)	(va_arg(args, type))

/* Macros that do proper sign extension and handle length modifiers.  Used
   for the integer conversion specifiers. */
#define GETSIGNED(p) \
  (((p)->flags & PF_LONGINT) \
	? GETARG (long) \
  	: (((p)->flags & PF_SHORTINT) ? (long)(short)GETARG (int) \
				      : (long)GETARG (int)))

#define GETUNSIGNED(p) \
  (((p)->flags & PF_LONGINT) \
	? GETARG (unsigned long) \
	: (((p)->flags & PF_SHORTINT) ? (unsigned long)(unsigned short)GETARG (int) \
				      : (unsigned long)GETARG (unsigned int)))


#ifdef HAVE_LONG_DOUBLE
#define GETLDOUBLE(p) GETARG (long double)
#endif
#define GETDOUBLE(p) GETARG (double)

#define SET_SIZE_FLAGS(p, type) \
  if (sizeof (type) > sizeof (int)) \
    (p)->flags |= PF_LONGINT; \
  if (sizeof (type) > sizeof (long)) \
    (p)->flags |= PF_LONGLONG;

/* this struct holds everything we need */
struct DATA
{
  int length;
  char *base;		/* needed for [v]asprintf */
  char *holder;
  int counter;
  const char *pf;

/* FLAGS */
  int flags;
  int justify;
  int width, precision;
  char pad;
};

/* the floating point stuff */
#ifdef FLOATING_POINT
static double pow_10 __P((int));
static int log_10 __P((double));
static double integral __P((double, double *));
static char *numtoa __P((double, int, int, char **));
#endif

static void init_data __P((struct DATA *, char *, size_t, const char *, int));
static void init_conv_flag __P((struct DATA *));

/* for the format */
#ifdef FLOATING_POINT
static void floating __P((struct DATA *, double));
static void exponent __P((struct DATA *, double));
#endif
static void number __P((struct DATA *, unsigned long, int));
#ifdef HAVE_LONG_LONG
static void lnumber __P((struct DATA *, unsigned long long, int));
#endif
static void pointer __P((struct DATA *, unsigned long));
static void strings __P((struct DATA *, char *));

#ifdef FLOATING_POINT
#  define FALLBACK_FMTSIZE	32
#  define FALLBACK_BASE		4096
#  define LFALLBACK_BASE	5120
#  ifdef HAVE_LONG_DOUBLE
static void ldfallback __P((struct DATA *, const char *, const char *, long double));
#  endif
static void dfallback __P((struct DATA *, const char *, const char *, double));
#endif

static char *groupnum __P((char *));

#ifndef HAVE_ISINF_IN_LIBC
static int isinf __P((double));
#endif
#ifndef HAVE_ISNAN_IN_LIBC
static int isnan __P((double));
#endif

#ifdef DRIVER
static void memory_error_and_abort ();
static void *xmalloc __P((size_t));
static void *xrealloc __P((void *, size_t));
static void xfree __P((void *));
#else
#  include <xmalloc.h>
#endif

/* those are defines specific to snprintf to hopefully
 * make the code clearer :-)
 */
#define RIGHT 1
#define LEFT  0
#define NOT_FOUND -1
#define FOUND 1
#define MAX_FIELD 15

/* round off to the precision */
#define ROUND(d, p) \
	    (d < 0.) ? \
	     d - pow_10(-(p)->precision) * 0.5 : \
	     d + pow_10(-(p)->precision) * 0.5

/* set default precision */
#define DEF_PREC(p) \
	    if ((p)->precision == NOT_FOUND) \
	      (p)->precision = 6

/* put a char.  increment the number of chars written even if we've exceeded
   the vsnprintf/snprintf buffer size (for the return value) */
#define PUT_CHAR(c, p) \
	do \
	  { \
	    if (((p)->flags & PF_ALLOCBUF) && ((p)->counter >= (p)->length - 1)) \
	      { \
		(p)->length += ASBUFSIZE; \
		(p)->base = (char *)xrealloc((p)->base, (p)->length); \
		(p)->holder = (p)->base + (p)->counter; /* in case reallocated */ \
	      } \
	    if ((p)->counter < (p)->length) \
	      *(p)->holder++ = (c); \
	    (p)->counter++; \
	  } \
	while (0)

/* Output a string.  P->WIDTH has already been adjusted for padding. */
#define PUT_STRING(string, len, p) \
	do \
	  { \
	    PAD_RIGHT (p); \
	    while ((len)-- > 0) \
	      { \
		PUT_CHAR (*(string), (p)); \
		(string)++; \
	      } \
	    PAD_LEFT (p); \
	  } \
	while (0)

#define PUT_PLUS(d, p, zero) \
	    if ((d) > zero && (p)->justify == RIGHT) \
	      PUT_CHAR('+', p)

#define PUT_SPACE(d, p, zero) \
	    if (((p)->flags & PF_SPACE) && (d) > zero) \
	      PUT_CHAR(' ', p)

/* pad right */ 
#define PAD_RIGHT(p) \
	    if ((p)->width > 0 && (p)->justify != LEFT) \
	      for (; (p)->width > 0; (p)->width--) \
		 PUT_CHAR((p)->pad, p)

/* pad left */
#define PAD_LEFT(p) \
	    if ((p)->width > 0 && (p)->justify == LEFT) \
	      for (; (p)->width > 0; (p)->width--) \
		 PUT_CHAR((p)->pad, p)

/* pad with zeros from decimal precision */
#define PAD_ZERO(p) \
	if ((p)->precision > 0) \
	  for (; (p)->precision > 0; (p)->precision--) \
	    PUT_CHAR('0', p)

/* if width and prec. in the args */
#define STAR_ARGS(p) \
	do { \
	    if ((p)->flags & PF_STAR_W) \
	      { \
		(p)->width = GETARG (int); \
		if ((p)->width < 0) \
		  { \
		    (p)->flags |= PF_LADJUST; \
		    (p)->justify = LEFT; \
		    (p)->width = -(p)->width; \
		  } \
	      } \
	    if ((p)->flags & PF_STAR_P) \
	      { \
		(p)->precision = GETARG (int); \
		if ((p)->precision < 0) \
		  { \
		    (p)->flags &= ~PF_STAR_P; \
		    (p)->precision = NOT_FOUND; \
		  } \
	      } \
	} while (0)

#if defined (HAVE_LOCALE_H) && defined (HAVE_LOCALECONV)
#  define GETLOCALEDATA(d, t, g) \
      do \
	{ \
	  struct lconv *lv; \
	  if ((d) == 0) { \
	  (d) = '.'; (t) = -1; (g) = 0; /* defaults */ \
	  lv = localeconv(); \
	  if (lv) \
	    { \
	      if (lv->decimal_point && lv->decimal_point[0]) \
	        (d) = lv->decimal_point[0]; \
	      if (lv->thousands_sep && lv->thousands_sep[0]) \
	        (t) = lv->thousands_sep[0]; \
	      (g) = lv->grouping ? lv->grouping : ""; \
	      if (*(g) == '\0' || *(g) == CHAR_MAX || (t) == -1) (g) = 0; \
	    } \
	  } \
	} \
      while (0);
#else
#  define GETLOCALEDATA(d, t, g) \
      ( (d) = '.', (t) = ',', g = "\003" )
#endif

#ifdef FLOATING_POINT
/*
 * Find the nth power of 10
 */
static double
pow_10(n)
     int n;
{ 
  double P;

  /* handle common cases with fast switch statement. */
  switch (n)
    {
    case -3:	return .001;
    case -2:	return .01;
    case -1:	return .1;
    case 0:	return 1.;
    case 1:	return 10.;
    case 2:	return 100.;
    case 3:	return 1000.;
    }

  if (n < 0)
    {
      P = .0001;
      for (n += 4; n < 0; n++)
	P /= 10.;
    }
  else
    {
      P = 10000.;
      for (n -= 4; n > 0; n--)
	P *= 10.;
    }

  return P;
}

/*
 * Find the integral part of the log in base 10 
 * Note: this not a real log10()
	 I just need and approximation(integerpart) of x in:
	  10^x ~= r
 * log_10(200) = 2;
 * log_10(250) = 2;
 *
 * NOTE: do not call this with r == 0 -- an infinite loop results.
 */
static int
log_10(r)
     double r;
{ 
  int i = 0;
  double result = 1.;

  if (r < 0.)
    r = -r;

  if (r < 1.)
    {
      while (result >= r)
	{
	  result /= 10.;
	  i++;
	}
      return (-i);
    }
  else
    {
      while (result <= r)
	{
	  result *= 10.;
	  i++;
	}
      return (i - 1);
    }
}

/*
 * This function return the fraction part of a double
 * and set in ip the integral part.
 * In many ways it resemble the modf() found on most Un*x
 */
static double
integral(real, ip)
     double real;
     double *ip;
{ 
  int j;
  double i, s, p;
  double real_integral = 0.;

  /* take care of the obvious */
  /* equal to zero ? */
  if (real == 0.)
    {
      *ip = 0.;
      return (0.);
    }

  /* negative number ? */
  if (real < 0.)
    real = -real;

  /* a fraction ? */
  if ( real < 1.)
    {
      *ip = 0.;
      return real;
    }

  /* the real work :-) */
  for (j = log_10(real); j >= 0; j--)
    {
      p = pow_10(j);
      s = (real - real_integral)/p;
      i = 0.;
      while (i + 1. <= s)
	i++;
      real_integral += i*p;
    }
  *ip = real_integral;
  return (real - real_integral);
}

#define PRECISION 1.e-6
/* 
 * return an ascii representation of the integral part of the number
 * and set fract to be an ascii representation of the fraction part
 * the container for the fraction and the integral part or staticly
 * declare with fix size 
 */
static char *
numtoa(number, base, precision, fract)
     double number;
     int base, precision;
     char **fract;
{
  register int i, j;
  double ip, fp; /* integer and fraction part */
  double fraction;
  int digits = MAX_INT - 1;
  static char integral_part[MAX_INT];
  static char fraction_part[MAX_FRACT];
  double sign;
  int ch;

  /* taking care of the obvious case: 0.0 */
  if (number == 0.)
    { 
      integral_part[0] = '0';
      integral_part[1] = '\0';
      /* The fractional part has to take the precision into account */
      for (ch = 0; ch < precision-1; ch++)
 	fraction_part[ch] = '0';
      fraction_part[ch] = '0';
      fraction_part[ch+1] = '\0';
      if (fract)
	*fract = fraction_part;
      return integral_part;
    }

  /* for negative numbers */
  if ((sign = number) < 0.)
    {
      number = -number;
      digits--; /* sign consume one digit */
    }

  fraction = integral(number, &ip);
  number = ip;

  /* do the integral part */
  if (ip == 0.)
    {
      integral_part[0] = '0';
      i = 1;
    }
  else
    {
      for ( i = 0; i < digits && number != 0.; ++i)
	{
	  number /= base;
	  fp = integral(number, &ip);
	  ch = (int)((fp + PRECISION)*base); /* force to round */
	  integral_part[i] = (ch <= 9) ? ch + '0' : ch + 'a' - 10;
	  if (! ISXDIGIT((unsigned char)integral_part[i]))
	    break;	/* bail out overflow !! */
	  number = ip;
	 }
    }
     
  /* Oh No !! out of bound, ho well fill it up ! */
  if (number != 0.)
    for (i = 0; i < digits; ++i)
      integral_part[i] = '9';

  /* put the sign ? */
  if (sign < 0.)
    integral_part[i++] = '-';

  integral_part[i] = '\0';

  /* reverse every thing */
  for ( i--, j = 0; j < i; j++, i--)
    SWAP_INT(integral_part[i], integral_part[j]);  

  /* the fractional part */
  for (i=0, fp=fraction; precision > 0 && i < MAX_FRACT ; i++, precision--)
    {
      fraction_part[i] = (int)((fp + PRECISION)*10. + '0');
      if (! DIGIT(fraction_part[i])) /* underflow ? */
	break;
      fp = (fp*10.0) - (double)(long)((fp + PRECISION)*10.);
    }
  fraction_part[i] = '\0';

  if (fract != (char **)0)
    *fract = fraction_part;

  return integral_part;
}
#endif

/* for %d and friends, it puts in holder
 * the representation with the right padding
 */
static void
number(p, d, base)
     struct DATA *p;
     unsigned long d;
     int base;
{
  char *tmp, *t;
  long sd;
  int flags;

  /* An explicit precision turns off the zero-padding flag. */
  if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT))
    p->flags &= ~PF_ZEROPAD;

  sd = d;	/* signed for ' ' padding in base 10 */
  flags = 0;
  flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0;
  if (*p->pf == 'X')
    flags |= FL_HEXUPPER;

  tmp = fmtulong (d, base, intbuf, sizeof(intbuf), flags);
  t = 0;
  if ((p->flags & PF_THOUSANDS))
    {
      GETLOCALEDATA(decpoint, thoussep, grouping);
      if (grouping && (t = groupnum (tmp)))
        tmp = t;
    }

  p->width -= strlen(tmp);
  PAD_RIGHT(p);

  if ((p->flags & PF_DOT) && p->precision > 0)
    {
      p->precision -= strlen(tmp);
      PAD_ZERO(p);
    }

  switch (base)
    {
    case 10:
      PUT_PLUS(sd, p, 0);
      PUT_SPACE(sd, p, 0);
      break;
    case 8:
      if (p->flags & PF_ALTFORM)
	PUT_CHAR('0', p);
      break;
    case 16:
      if (p->flags & PF_ALTFORM)
	{
	  PUT_CHAR('0', p);
	  PUT_CHAR(*p->pf, p);
	}
      break;
    }

  while (*tmp)
    {
      PUT_CHAR(*tmp, p);
      tmp++;
    }

  PAD_LEFT(p);
  FREE (t);
}

#ifdef HAVE_LONG_LONG
/*
 * identical to number() but works for `long long'
 */
static void
lnumber(p, d, base)
     struct DATA *p;
     unsigned long long d;
     int base;
{
  char *tmp, *t;
  long long sd;
  int flags;

  /* An explicit precision turns off the zero-padding flag. */
  if ((p->flags & PF_ZEROPAD) && p->precision >= 0 && (p->flags & PF_DOT))
    p->flags &= ~PF_ZEROPAD;

  sd = d;	/* signed for ' ' padding in base 10 */
  flags = (*p->pf == 'x' || *p->pf == 'X' || *p->pf == 'o' || *p->pf == 'u' || *p->pf == 'U') ? FL_UNSIGNED : 0;
  if (*p->pf == 'X')
    flags |= FL_HEXUPPER;

  tmp = fmtullong (d, base, intbuf, sizeof(intbuf), flags);
  t = 0;
  if ((p->flags & PF_THOUSANDS))
    {
      GETLOCALEDATA(decpoint, thoussep, grouping);
      if (grouping && (t = groupnum (tmp)))
        tmp = t;
    }

  p->width -= strlen(tmp);
  PAD_RIGHT(p);

  if ((p->flags & PF_DOT) && p->precision > 0)
    {
      p->precision -= strlen(tmp);
      PAD_ZERO(p);
    }

  switch (base)
    {
    case 10:
      PUT_PLUS(sd, p, 0);
      PUT_SPACE(sd, p, 0);
      break;
    case 8:
      if (p->flags & PF_ALTFORM)
	PUT_CHAR('0', p);
      break;
    case 16:
      if (p->flags & PF_ALTFORM)
	{
	  PUT_CHAR('0', p);
	  PUT_CHAR(*p->pf, p);
	}
      break;
    }

  while (*tmp)
    {
      PUT_CHAR(*tmp, p);
      tmp++;
    }

  PAD_LEFT(p);
  FREE (t);
}
#endif

static void
pointer(p, d)
     struct DATA *p;
     unsigned long d;
{
  char *tmp;

  tmp = fmtulong(d, 16, intbuf, sizeof(intbuf), 0);
  p->width -= strlen(tmp);
  PAD_RIGHT(p);

  /* prefix '0x' for pointers */
  PUT_CHAR('0', p);
  PUT_CHAR('x', p);

  while (*tmp)
    {
      PUT_CHAR(*tmp, p);
      tmp++;
    }

  PAD_LEFT(p);
}

/* %s strings */
static void
strings(p, tmp)
     struct DATA *p;
     char *tmp;
{
  size_t len;

  len = strlen(tmp);
  if (p->precision != NOT_FOUND) /* the smallest number */
    len = (len < p->precision ? len : p->precision);
  p->width -= len;

  PUT_STRING (tmp, len, p);
}

#if HANDLE_MULTIBYTE
/* %ls wide-character strings */
static void
wstrings(p, tmp)
     struct DATA *p;
     wchar_t *tmp;
{
  size_t len;
  mbstate_t mbs;
  char *os;
  const wchar_t *ws;

  memset (&mbs, '\0', sizeof (mbstate_t));
  ws = (const wchar_t *)tmp;

  os = (char *)NULL;
  if (p->precision != NOT_FOUND)
    {
      os = (char *)xmalloc (p->precision + 1);
      len = wcsrtombs (os, &ws, p->precision, &mbs);
    }
  else
    {
      len = wcsrtombs (NULL, &ws, 0, &mbs);
      if (len != (size_t)-1)
        {
	  memset (&mbs, '\0', sizeof (mbstate_t));
	  os = (char *)xmalloc (len + 1);
	  (void)wcsrtombs (os, &ws, len + 1, &mbs);
        }
    }
  if (len == (size_t)-1)
    {
      /* invalid multibyte sequence; bail now. */
      FREE (os);      
      return;
    }

  p->width -= len;
  PUT_STRING (os, len, p);
  free (os);
}

static void
wchars (p, wc)
     struct DATA *p;
     wint_t wc;
{
  char *lbuf, *l;
  mbstate_t mbs;
  size_t len;

  lbuf = (char *)malloc (MB_CUR_MAX+1);
  if (lbuf == 0)
    return;
  memset (&mbs, '\0', sizeof (mbstate_t));
  len = wcrtomb (lbuf, wc, &mbs);
  if (len == (size_t)-1)
    /* conversion failed; bail now. */
    return;
  p->width -= len;
  l = lbuf;
  PUT_STRING (l, len, p);
  free (lbuf);
}
#endif /* HANDLE_MULTIBYTE */

#ifdef FLOATING_POINT

#ifndef HAVE_ISINF_IN_LIBC
/* Half-assed versions, since we don't want to link with libm. */
static int
isinf(d)
     double d;
{
#ifdef DBL_MAX
  if (d < DBL_MIN)
    return -1;
  else if (d > DBL_MAX)
    return 1;
  else
#endif
    return 0;
}
#endif

#ifndef HAVE_ISNAN_IN_LIBC
static int
isnan(d)
     double d;
{
  return 0;
}
#endif

/* Check for [+-]infinity and NaN.  If MODE == 1, we check for Infinity, else
   (mode == 2) we check for NaN.  This does the necessary printing.  Returns
   1 if Inf or Nan, 0 if not. */
static int
chkinfnan(p, d, mode)
     struct DATA *p;
     double d;
     int mode;		/* == 1 for inf, == 2 for nan */
{
  int i;
  char *tmp;
  char *big, *small;

  i = (mode == 1) ? isinf(d) : isnan(d);
  if (i == 0)
    return 0;
  big = (mode == 1) ? "INF" : "NAN";
  small = (mode == 1) ? "inf" : "nan";

  tmp = (*p->pf == 'F' || *p->pf == 'G' || *p->pf == 'E') ? big : small;

  if (i < 0)
    PUT_CHAR('-', p);

  while (*tmp)
    {
      PUT_CHAR (*tmp, p);
      tmp++;
    }

  return 1;
}

/* %f %F %g %G floating point representation */
static void
floating(p, d)
     struct DATA *p;
     double d;
{
  char *tmp, *tmp2, *t;
  int i;

  if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2)))
    return;	/* already printed nan or inf */

  GETLOCALEDATA(decpoint, thoussep, grouping);
  DEF_PREC(p);
  d = ROUND(d, p);
  tmp = dtoa(d, p->precision, &tmp2);
  t = 0;
  if ((p->flags & PF_THOUSANDS) && grouping && (t = groupnum (tmp)))
    tmp = t;

  if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0)
    {
      /* smash the trailing zeros unless altform */
      for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--)
        tmp2[i] = '\0'; 
      if (tmp2[0] == '\0')
	p->precision = 0;
    }

  /* calculate the padding. 1 for the dot */
  p->width = p->width -
	    ((d > 0. && p->justify == RIGHT) ? 1:0) -
	    ((p->flags & PF_SPACE) ? 1:0) -
	    strlen(tmp) - p->precision -
	    ((p->precision != 0 || (p->flags & PF_ALTFORM)) ? 1 : 0);	/* radix char */
  PAD_RIGHT(p);  
  PUT_PLUS(d, p, 0.);
  PUT_SPACE(d, p, 0.);

  while (*tmp)
    {
      PUT_CHAR(*tmp, p);	/* the integral */
      tmp++;
    }
  FREE (t);

  if (p->precision != 0 || (p->flags & PF_ALTFORM))
    PUT_CHAR(decpoint, p);  /* put the '.' */

  for (; *tmp2; tmp2++)
    PUT_CHAR(*tmp2, p); /* the fraction */
  
  PAD_LEFT(p);
} 

/* %e %E %g %G exponent representation */
static void
exponent(p, d)
     struct DATA *p;
     double d;
{
  char *tmp, *tmp2;
  int j, i;

  if (d != 0 && (chkinfnan(p, d, 1) || chkinfnan(p, d, 2)))
    return;	/* already printed nan or inf */

  GETLOCALEDATA(decpoint, thoussep, grouping);
  DEF_PREC(p);
  if (d == 0.)
    j = 0;
  else
    {
      j = log_10(d);
      d = d / pow_10(j);  /* get the Mantissa */
      d = ROUND(d, p);		  
    }
  tmp = dtoa(d, p->precision, &tmp2);

  /* 1 for unit, 1 for the '.', 1 for 'e|E',
   * 1 for '+|-', 2 for 'exp' */
  /* calculate how much padding need */
  p->width = p->width - 
	     ((d > 0. && p->justify == RIGHT) ? 1:0) -
	     ((p->flags & PF_SPACE) ? 1:0) - p->precision - 6;

  PAD_RIGHT(p);
  PUT_PLUS(d, p, 0.);
  PUT_SPACE(d, p, 0.);

  while (*tmp)
    {
      PUT_CHAR(*tmp, p);
      tmp++;
    }

  if (p->precision != 0 || (p->flags & PF_ALTFORM))
      PUT_CHAR(decpoint, p);  /* the '.' */

  if ((*p->pf == 'g' || *p->pf == 'G') && (p->flags & PF_ALTFORM) == 0)
    /* smash the trailing zeros unless altform */
    for (i = strlen(tmp2) - 1; i >= 0 && tmp2[i] == '0'; i--)
      tmp2[i] = '\0'; 

  for (; *tmp2; tmp2++)
    PUT_CHAR(*tmp2, p); /* the fraction */

  /* the exponent put the 'e|E' */
  if (*p->pf == 'g' || *p->pf == 'e')
    PUT_CHAR('e', p);
  else
    PUT_CHAR('E', p);

  /* the sign of the exp */
  if (j >= 0)
    PUT_CHAR('+', p);
  else
    {
      PUT_CHAR('-', p);
      j = -j;
    }

   tmp = itoa(j);
   /* pad out to at least two spaces.  pad with `0' if the exponent is a
      single digit. */
   if (j <= 9)
     PUT_CHAR('0', p);

   /* the exponent */
   while (*tmp)
     {
       PUT_CHAR(*tmp, p);
       tmp++;
     }

   PAD_LEFT(p);
}
#endif

/* Return a new string with the digits in S grouped according to the locale's
   grouping info and thousands separator.  If no grouping should be performed,
   this returns NULL; the caller needs to check for it. */
static char *
groupnum (s)
     char *s;
{
  char *se, *ret, *re, *g;
  int len, slen;

  if (grouping == 0 || *grouping <= 0 || *grouping == CHAR_MAX)
    return ((char *)NULL);

  /* find min grouping to size returned string */
  for (len = *grouping, g = grouping; *g; g++)
      if (*g > 0 && *g < len)
	len = *g;

  slen = strlen (s);
  len = slen / len + 1;
  ret = (char *)xmalloc (slen + len + 1);
  re = ret + slen + len;
  *re = '\0';

  g = grouping;
  se = s + slen;
  len = *g;

  while (se > s)
    {
      *--re = *--se;

      /* handle `-' inserted by numtoa() and the fmtu* family here. */
      if (se > s && se[-1] == '-')
	continue;

      /* begin new group. */
      if (--len == 0 && se > s)
	{
	  *--re = thoussep;
	  len = *++g;		/* was g++, but that uses first char twice (glibc bug, too) */
	  if (*g == '\0')
	    len = *--g;		/* use previous grouping */
	  else if (*g == CHAR_MAX)
	    {
	      do
	        *--re = *--se;
	      while (se > s);
	      break;
	    }
	}
    }

  if (re > ret)
#ifdef HAVE_MEMMOVE
    memmove (ret, re, strlen (re) + 1);
#else
    strcpy (ret, re);
#endif
   
  return ret;
}

/* initialize the conversion specifiers */
static void
init_conv_flag (p)
     struct DATA *p;
{
  p->flags &= PF_ALLOCBUF;		/* preserve PF_ALLOCBUF flag */
  p->precision = p->width = NOT_FOUND;
  p->justify = NOT_FOUND;
  p->pad = ' ';
}

static void
init_data (p, string, length, format, mode)
     struct DATA *p;
     char *string;
     size_t length;
     const char *format;
     int mode;
{
  p->length = length - 1; /* leave room for '\0' */
  p->holder = p->base = string;
  p->pf = format;
  p->counter = 0;
  p->flags = (mode == PFM_AS) ? PF_ALLOCBUF : 0;
}

static int
#if defined (__STDC__)
vsnprintf_internal(struct DATA *data, char *string, size_t length, const char *format, va_list args)
#else
vsnprintf_internal(data, string, length, format, args)
     struct DATA *data;
     char *string;
     size_t length;
     const char *format;
     va_list args;
#endif
{
  double d; /* temporary holder */
#ifdef HAVE_LONG_DOUBLE
  long double ld;	/* for later */
#endif
  unsigned long ul;
#ifdef HAVE_LONG_LONG
  unsigned long long ull;
#endif
  int state, i, c, n;
  char *s;
#if HANDLE_MULTIBYTE
  wchar_t *ws;
  wint_t wc;
#endif
  const char *convstart;
  int negprec;

  /* Sanity check, the string length must be >= 0.  C99 actually says that
     LENGTH can be zero here, in the case of snprintf/vsnprintf (it's never
     0 in the case of asprintf/vasprintf), and the return value is the number
     of characters that would have been written. */
  if (length < 0)
    return -1;

  if (format == 0)
    return 0;

  /* Reset these for each call because the locale might have changed. */
  decpoint = thoussep = 0;
  grouping = 0;

  negprec = 0;
  for (; c = *(data->pf); data->pf++)
    {
      if (c != '%')
	{
	  PUT_CHAR (c, data);
	  continue;
	}

      convstart = data->pf;
      init_conv_flag (data); /* initialise format flags */

      state = 1;
      for (state = 1; state && *data->pf; )
	{
	  c = *(++data->pf);
	      /* fmtend = data->pf */
#if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE)
	  if (data->flags & PF_LONGDBL)
	    {
	      switch (c)
		{
		case 'f': case 'F':
		case 'e': case 'E':
		case 'g': case 'G':
#  ifdef HAVE_PRINTF_A_FORMAT
		case 'a': case 'A':
#  endif
		  STAR_ARGS (data);
		  ld = GETLDOUBLE (data);
		  ldfallback (data, convstart, data->pf, ld);
		  goto conv_break;
		}
	    }
#endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */

	  switch (c)
	    {
	      /* Parse format flags */
	      case '\0': /* a NULL here ? ? bail out */
		*data->holder = '\0';
		return data->counter;
		break;
	      case '#':
		data->flags |= PF_ALTFORM;
		continue;
	      case '0':
		data->flags |= PF_ZEROPAD;
		data->pad = '0';
		continue;
	      case '*':
		if (data->flags & PF_DOT)
		  data->flags |= PF_STAR_P;
		else
		  data->flags |= PF_STAR_W;
		continue;
	      case '-':
		if ((data->flags & PF_DOT) == 0)
		  {
		    data->flags |= PF_LADJUST;
		    data->justify = LEFT;
		  }
		else
		  negprec = 1;
		continue;
	      case ' ':
		if ((data->flags & PF_PLUS) == 0)
		  data->flags |= PF_SPACE;
		continue;
	      case '+':
		if ((data->flags & PF_DOT) == 0)
		  {
		    data->flags |= PF_PLUS;
		    data->justify = RIGHT;
		  }
		continue;
	      case '\'':
		data->flags |= PF_THOUSANDS;
		continue;

	      case '1': case '2': case '3':
	      case '4': case '5': case '6':
	      case '7': case '8': case '9':
		n = 0;
		do
		  {
		    n = n * 10 + TODIGIT(c);
		    c = *(++data->pf);
		  }
		while (DIGIT(c));
		data->pf--;		/* went too far */
		if (n < 0)
		  n = 0;
		if (data->flags & PF_DOT)
		  data->precision = negprec ? NOT_FOUND : n;
		else
		  data->width = n;
		continue;

	      /* optional precision */
	      case '.':
		data->flags |= PF_DOT;
		data->precision = 0;
		continue;

	      /* length modifiers */
	      case 'h':
		data->flags |= (data->flags & PF_SHORTINT) ? PF_SIGNEDCHAR : PF_SHORTINT;
		continue;
	      case 'l':
		data->flags |= (data->flags & PF_LONGINT) ? PF_LONGLONG : PF_LONGINT;
		continue;
	      case 'L':
		data->flags |= PF_LONGDBL;
		continue;
	      case 'q':
		data->flags |= PF_LONGLONG;
		continue;
	      case 'j':
		data->flags |= PF_INTMAX_T;
		SET_SIZE_FLAGS(data, intmax_t);
		continue;
	      case 'z':
		data->flags |= PF_SIZE_T;
		SET_SIZE_FLAGS(data, size_t);
		continue;
	      case 't':
		data->flags |= PF_PTRDIFF_T;
		SET_SIZE_FLAGS(data, ptrdiff_t);
		continue;
		
	      /* Conversion specifiers */
#ifdef FLOATING_POINT
	      case 'f':  /* float, double */
	      case 'F':
		STAR_ARGS(data);
		d = GETDOUBLE(data);
		floating(data, d);
conv_break:		
		state = 0;
		break;
	      case 'g': 
	      case 'G':
		STAR_ARGS(data);
		DEF_PREC(data);
		d = GETDOUBLE(data);
		i = (d != 0.) ? log_10(d) : -1;
		/*
		 * for '%g|%G' ANSI: use f if exponent
		 * is in the range or [-4,p] exclusively
		 * else use %e|%E
		 */
		if (-4 < i && i < data->precision)
		  {
		    /* reset precision */
		    data->precision -= i + 1;
		    floating(data, d);
		  }
		else
		  {
		    /* reduce precision by 1 because of leading digit before
		       decimal point in e format. */
		    data->precision--;
		    exponent(data, d);
		  }
		state = 0;
		break;
	      case 'e':
	      case 'E':  /* Exponent double */
		STAR_ARGS(data);
		d = GETDOUBLE(data);
		exponent(data, d);
		state = 0;
		break;
#  ifdef HAVE_PRINTF_A_FORMAT
	      case 'a':
	      case 'A':
		STAR_ARGS(data);
		d = GETDOUBLE(data);
		dfallback(data, convstart, data->pf, d);
		state = 0;
		break;
#  endif /* HAVE_PRINTF_A_FORMAT */
#endif /* FLOATING_POINT */
	      case 'U':
		data->flags |= PF_LONGINT;
		/* FALLTHROUGH */
	      case 'u':
		STAR_ARGS(data);
#ifdef HAVE_LONG_LONG
		if (data->flags & PF_LONGLONG)
		  {
		    ull = GETARG (unsigned long long);
		    lnumber(data, ull, 10);
		  }
		else
#endif
		  {
		    ul = GETUNSIGNED(data);
		    number(data, ul, 10);
		  }
		state = 0;
		break;
	      case 'D':
		data->flags |= PF_LONGINT;
		/* FALLTHROUGH */
	      case 'd':  /* decimal */
	      case 'i':
		STAR_ARGS(data);
#ifdef HAVE_LONG_LONG
		if (data->flags & PF_LONGLONG)
		  {
		    ull = GETARG (long long);
		    lnumber(data, ull, 10);
		  }
		else
#endif
		  {
		    ul = GETSIGNED(data);
		    number(data, ul, 10);
		  }
		state = 0;
		break;
	      case 'o':  /* octal */
		STAR_ARGS(data);
#ifdef HAVE_LONG_LONG
		if (data->flags & PF_LONGLONG)
		  {
		    ull = GETARG (unsigned long long);
		    lnumber(data, ull, 8);
		  }
		else
#endif
		  {
		    ul = GETUNSIGNED(data);
		    number(data, ul, 8);
		  }
		state = 0;
		break;
	      case 'x': 
	      case 'X':  /* hexadecimal */
		STAR_ARGS(data);
#ifdef HAVE_LONG_LONG
		if (data->flags & PF_LONGLONG)
		  {
		    ull = GETARG (unsigned long long);
		    lnumber(data, ull, 16);
		  }
		else
#endif
		  {
		    ul = GETUNSIGNED(data);
		    number(data, ul, 16);
		  }
		state = 0;
		break;
	      case 'p':
		STAR_ARGS(data);
		ul = (unsigned long)GETARG (void *);
		pointer(data, ul);
		state = 0;
		break;
#if HANDLE_MULTIBYTE
	      case 'C':
		data->flags |= PF_LONGINT;
		/* FALLTHROUGH */
#endif
	      case 'c': /* character */
		STAR_ARGS(data);
#if HANDLE_MULTIBYTE
		if (data->flags & PF_LONGINT)
		  {
		    wc = GETARG (wint_t);
		    wchars (data, wc);
		  }
		else
#endif
		  {		
		    ul = GETARG (int);
		    PUT_CHAR(ul, data);
		  }
		state = 0;
		break;
#if HANDLE_MULTIBYTE
	      case 'S':
		data->flags |= PF_LONGINT;
		/* FALLTHROUGH */
#endif
	      case 's':  /* string */
		STAR_ARGS(data);
#if HANDLE_MULTIBYTE
		if (data->flags & PF_LONGINT)
		  {
		    ws = GETARG (wchar_t *);
		    wstrings (data, ws);
		  }
		else
#endif
		  {
		    s = GETARG (char *);
		    strings(data, s);
		  }
		state = 0;
		break;
	      case 'n':
#ifdef HAVE_LONG_LONG
		if (data->flags & PF_LONGLONG)
		  *(GETARG (long long *)) = data->counter;
		else
#endif
		if (data->flags & PF_LONGINT)
		  *(GETARG (long *)) = data->counter;
		else if (data->flags & PF_SHORTINT)
		  *(GETARG (short *)) = data->counter;
		else
		  *(GETARG (int *)) = data->counter;
		state = 0;
		break;
	      case '%':  /* nothing just % */
		PUT_CHAR('%', data);
		state = 0;
		break;
  	      default:
		/* is this an error ? maybe bail out */
		state = 0;
		break;
	} /* end switch */
      } /* end of `%' for loop */
    } /* end of format string for loop */

  if (data->length >= 0)
    *data->holder = '\0'; /* the end ye ! */

  return data->counter;
}

#if defined (FLOATING_POINT) && defined (HAVE_LONG_DOUBLE)
/*
 * Printing floating point numbers accurately is an art.  I'm not good
 * at it.  Fall back to sprintf for long double formats.
 */
static void
ldfallback (data, fs, fe, ld)
     struct DATA *data;
     const char *fs, *fe;
     long double ld;
{
  register char *x;
  char fmtbuf[FALLBACK_FMTSIZE], *obuf;
  int fl;

  fl = LFALLBACK_BASE + (data->precision < 6 ? 6 : data->precision) + 2;
  obuf = (char *)xmalloc (fl);
  fl = fe - fs + 1;
  strncpy (fmtbuf, fs, fl);
  fmtbuf[fl] = '\0';

  if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P))
    sprintf (obuf, fmtbuf, data->width, data->precision, ld);
  else if (data->flags & PF_STAR_W)
    sprintf (obuf, fmtbuf, data->width, ld);
  else if (data->flags & PF_STAR_P)
    sprintf (obuf, fmtbuf, data->precision, ld);
  else
    sprintf (obuf, fmtbuf, ld);

  for (x = obuf; *x; x++)
    PUT_CHAR (*x, data);    
  xfree (obuf);
}
#endif /* FLOATING_POINT && HAVE_LONG_DOUBLE */

#ifdef FLOATING_POINT
/* Used for %a, %A if the libc printf supports them. */
static void
dfallback (data, fs, fe, d)
     struct DATA *data;
     const char *fs, *fe;
     double d;
{
  register char *x;
  char fmtbuf[FALLBACK_FMTSIZE], obuf[FALLBACK_BASE];
  int fl;

  fl = fe - fs + 1;
  strncpy (fmtbuf, fs, fl);
  fmtbuf[fl] = '\0';

  if ((data->flags & PF_STAR_W) && (data->flags & PF_STAR_P))
    sprintf (obuf, fmtbuf, data->width, data->precision, d);
  else if (data->flags & PF_STAR_W)
    sprintf (obuf, fmtbuf, data->width, d);
  else if (data->flags & PF_STAR_P)
    sprintf (obuf, fmtbuf, data->precision, d);
  else
    sprintf (obuf, fmtbuf, d);

  for (x = obuf; *x; x++)
    PUT_CHAR (*x, data);    
}
#endif /* FLOATING_POINT */

#if !HAVE_SNPRINTF

int
#if defined (__STDC__)
vsnprintf(char *string, size_t length, const char *format, va_list args)
#else
vsnprintf(string, length, format, args)
     char *string;
     size_t length;
     const char *format;
     va_list args;
#endif
{
  struct DATA data;

  if (string == 0 && length != 0)
    return 0;
  init_data (&data, string, length, format, PFM_SN);
  return (vsnprintf_internal(&data, string, length, format, args));
}

int
#if defined(PREFER_STDARG)
snprintf(char *string, size_t length, const char * format, ...)
#else
snprintf(string, length, format, va_alist)
     char *string;
     size_t length;
     const char *format;
     va_dcl
#endif
{
  struct DATA data;
  int rval;
  va_list args;

  SH_VA_START(args, format);

  if (string == 0 && length != 0)
    return 0;
  init_data (&data, string, length, format, PFM_SN);
  rval = vsnprintf_internal (&data, string, length, format, args);

  va_end(args);

  return rval;
}

#endif /* HAVE_SNPRINTF */

#if !HAVE_ASPRINTF

int
#if defined (__STDC__)
vasprintf(char **stringp, const char *format, va_list args)
#else
vasprintf(stringp, format, args)
     char **stringp;
     const char *format;
     va_list args;
#endif
{
  struct DATA data;
  char *string;
  int r;

  string = (char *)xmalloc(ASBUFSIZE);
  init_data (&data, string, ASBUFSIZE, format, PFM_AS);
  r = vsnprintf_internal(&data, string, ASBUFSIZE, format, args);
  *stringp = data.base;		/* not string in case reallocated */
  return r;
}

int
#if defined(PREFER_STDARG)
asprintf(char **stringp, const char * format, ...)
#else
asprintf(stringp, format, va_alist)
     char **stringp;
     const char *format;
     va_dcl
#endif
{
  int rval;
  va_list args;

  SH_VA_START(args, format);

  rval = vasprintf (stringp, format, args);

  va_end(args);

  return rval;
}

#endif /* !HAVE_ASPRINTF */

#endif /* !HAVE_SNPRINTF || !HAVE_ASPRINTF */

#ifdef DRIVER

static void
memory_error_and_abort ()
{
  write (2, "out of virtual memory\n", 22);
  abort ();
}

static void *
xmalloc(bytes)
     size_t bytes;
{
  void *ret;

  ret = malloc(bytes);
  if (ret == 0)
    memory_error_and_abort ();
  return ret;
}

static void *
xrealloc (pointer, bytes)
     void *pointer;
     size_t bytes;
{
  void *ret;

  ret = pointer ? realloc(pointer, bytes) : malloc(bytes);
  if (ret == 0)
    memory_error_and_abort ();
  return ret;
}

static void
xfree(x)
     void *x;
{
  if (x)
    free (x);
}

/* set of small tests for snprintf() */
main()
{
  char holder[100];
  char *h;
  int i, si, ai;

#ifdef HAVE_LOCALE_H
  setlocale(LC_ALL, "");
#endif

#if 1
  si = snprintf((char *)NULL, 0, "abcde\n");
  printf("snprintf returns %d with NULL first argument and size of 0\n", si);
  si = snprintf(holder, 0, "abcde\n");
  printf("snprintf returns %d with non-NULL first argument and size of 0\n", si);
  si = snprintf((char *)NULL, 16, "abcde\n");
  printf("snprintf returns %d with NULL first argument and non-zero size\n", si);
  
/*
  printf("Suite of test for snprintf:\n");
  printf("a_format\n");
  printf("printf() format\n");
  printf("snprintf() format\n\n");
*/
/* Checking the field widths */

  printf("/%%ld %%ld/, 336, 336\n");
  snprintf(holder, sizeof holder, "/%ld %ld/\n", 336, 336);
  asprintf(&h, "/%ld %ld/\n", 336, 336);
  printf("/%ld %ld/\n", 336, 336);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%d/, 336\n");
  snprintf(holder, sizeof holder, "/%d/\n", 336);
  asprintf(&h, "/%d/\n", 336);
  printf("/%d/\n", 336);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%2d/, 336\n");
  snprintf(holder, sizeof holder, "/%2d/\n", 336);
  asprintf(&h, "/%2d/\n", 336);
  printf("/%2d/\n", 336);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%10d/, 336\n");
  snprintf(holder, sizeof holder, "/%10d/\n", 336);
  asprintf(&h, "/%10d/\n", 336);
  printf("/%10d/\n", 336);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%-10d/, 336\n");
  snprintf(holder, sizeof holder, "/%-10d/\n", 336);
  asprintf(&h, "/%-10d/\n", 336);
  printf("/%-10d/\n", 336);
  printf("%s", holder);
  printf("%s\n", h);


/* floating points */

  printf("/%%f/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%f/\n", 1234.56);
  asprintf(&h, "/%f/\n", 1234.56);
  printf("/%f/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%e/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%e/\n", 1234.56);
  asprintf(&h, "/%e/\n", 1234.56);
  printf("/%e/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%4.2f/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%4.2f/\n", 1234.56);
  asprintf(&h, "/%4.2f/\n", 1234.56);
  printf("/%4.2f/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%3.1f/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%3.1f/\n", 1234.56);
  asprintf(&h, "/%3.1f/\n", 1234.56);
  printf("/%3.1f/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%10.3f/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%10.3f/\n", 1234.56);
  asprintf(&h, "/%10.3f/\n", 1234.56);
  printf("/%10.3f/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%10.3e/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%10.3e/\n", 1234.56);
  asprintf(&h, "/%10.3e/\n", 1234.56);
  printf("/%10.3e/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%+4.2f/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%+4.2f/\n", 1234.56);
  asprintf(&h, "/%+4.2f/\n", 1234.56);
  printf("/%+4.2f/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%010.2f/, 1234.56\n");
  snprintf(holder, sizeof holder, "/%010.2f/\n", 1234.56);
  asprintf(&h, "/%010.2f/\n", 1234.56);
  printf("/%010.2f/\n", 1234.56);
  printf("%s", holder);
  printf("%s\n", h);

#define BLURB "Outstanding acting !"
/* strings precisions */

  printf("/%%2s/, \"%s\"\n", BLURB);
  snprintf(holder, sizeof holder, "/%2s/\n", BLURB);
  asprintf(&h, "/%2s/\n", BLURB);
  printf("/%2s/\n", BLURB);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%22s/ %s\n", BLURB);
  snprintf(holder, sizeof holder, "/%22s/\n", BLURB);
  asprintf(&h, "/%22s/\n", BLURB);
  printf("/%22s/\n", BLURB);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%22.5s/ %s\n", BLURB);
  snprintf(holder, sizeof holder, "/%22.5s/\n", BLURB);
  asprintf(&h, "/%22.5s/\n", BLURB);
  printf("/%22.5s/\n", BLURB);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%-22.5s/ %s\n", BLURB);
  snprintf(holder, sizeof holder, "/%-22.5s/\n", BLURB);
  asprintf(&h, "/%-22.5s/\n", BLURB);
  printf("/%-22.5s/\n", BLURB);
  printf("%s", holder);
  printf("%s\n", h);

/* see some flags */

  printf("%%x %%X %%#x, 31, 31, 31\n");
  snprintf(holder, sizeof holder, "%x %X %#x\n", 31, 31, 31);
  asprintf(&h, "%x %X %#x\n", 31, 31, 31);
  printf("%x %X %#x\n", 31, 31, 31);
  printf("%s", holder);
  printf("%s\n", h);

  printf("**%%d**%% d**%% d**, 42, 42, -42\n");
  snprintf(holder, sizeof holder, "**%d**% d**% d**\n", 42, 42, -42);
  asprintf(&h, "**%d**% d**% d**\n", 42, 42, -42);
  printf("**%d**% d**% d**\n", 42, 42, -42);
  printf("%s", holder);
  printf("%s\n", h);

/* other flags */

  printf("/%%g/, 31.4\n");
  snprintf(holder, sizeof holder, "/%g/\n", 31.4);
  asprintf(&h, "/%g/\n", 31.4);
  printf("/%g/\n", 31.4);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%.6g/, 31.4\n");
  snprintf(holder, sizeof holder, "/%.6g/\n", 31.4);
  asprintf(&h, "/%.6g/\n", 31.4);
  printf("/%.6g/\n", 31.4);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%.1G/, 31.4\n");
  snprintf(holder, sizeof holder, "/%.1G/\n", 31.4);
  asprintf(&h, "/%.1G/\n", 31.4);
  printf("/%.1G/\n", 31.4);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%.1G/, 3100000000.4\n");
  snprintf(holder, sizeof holder, "/%.1G/\n", 3100000000.4);  
  asprintf(&h, "/%.1G/\n", 3100000000.4);  
  printf("/%.1G/\n", 3100000000.4); 
  printf("%s", holder);
  printf("%s\n", h);

  printf("abc%%n\n");
  printf("abc%n", &i); printf("%d\n", i);
  snprintf(holder, sizeof holder, "abc%n", &i);
  printf("%s", holder); printf("%d\n\n", i);
  asprintf(&h, "abc%n", &i);
  printf("%s", h); printf("%d\n\n", i);
  
  printf("%%*.*s --> 10.10\n");
  snprintf(holder, sizeof holder, "%*.*s\n", 10, 10, BLURB);
  asprintf(&h, "%*.*s\n", 10, 10, BLURB);
  printf("%*.*s\n", 10, 10, BLURB);
  printf("%s", holder);
  printf("%s\n", h);

  printf("%%%%%%%%\n");
  snprintf(holder, sizeof holder, "%%%%\n");
  asprintf(&h, "%%%%\n");
  printf("%%%%\n");
  printf("%s", holder);
  printf("%s\n", h);

#define BIG "Hello this is a too big string for the buffer"
/*  printf("A buffer to small of 10, trying to put this:\n");*/
  printf("<%%>, %s\n", BIG); 
  i = snprintf(holder, 10, "%s\n", BIG);
  i = asprintf(&h, "%s", BIG);
  printf("<%s>\n", BIG);
  printf("<%s>\n", holder);
  printf("<%s>\n\n", h);

  printf ("<%%p> vsnprintf\n");
  i = snprintf(holder, 100, "%p", vsnprintf);
  i = asprintf(&h, "%p", vsnprintf);
  printf("<%p>\n", vsnprintf);
  printf("<%s>\n", holder);  
  printf("<%s>\n\n", h);

  printf ("<%%lu> LONG_MAX+1\n");
  i = snprintf(holder, 100, "%lu", (unsigned long)(LONG_MAX)+1);
  i = asprintf(&h, "%lu", (unsigned long)(LONG_MAX)+1);
  printf("<%lu>\n", (unsigned long)(LONG_MAX)+1);
  printf("<%s>\n", holder);
  printf("<%s>\n\n", h);

#ifdef HAVE_LONG_LONG
  printf ("<%%llu> LLONG_MAX+1\n");
  i = snprintf(holder, 100, "%llu", (unsigned long long)(LLONG_MAX)+1);
  i = asprintf(&h, "%llu", (unsigned long long)(LLONG_MAX)+1);
  printf("<%llu>\n", (unsigned long long)(LLONG_MAX)+1);
  printf("<%s>\n", holder);
  printf("<%s>\n\n", h);
#endif

#ifdef HAVE_LONG_DOUBLE
  printf ("<%%6.2LE> 42.42\n");
  i = snprintf(holder, 100, "%6.2LE", (long double)42.42);
  i = asprintf(&h, "%6.2LE", (long double)42.42);
  printf ("<%6.2LE>\n", (long double)42.42);
  printf ("<%s>\n", holder);
  printf ("<%s>\n\n", h);
#endif

#ifdef HAVE_PRINTF_A_FORMAT
  printf ("<%%6.2A> 42.42\n");
  i = snprintf(holder, 100, "%6.2A", 42.42);
  i = asprintf(&h, "%6.2A", 42.42);
  printf ("<%6.2A>\n", 42.42);
  printf ("<%s>\n", holder);
  printf ("<%s>\n\n", h);

  printf ("<%%6.2LA> 42.42\n");
  i = snprintf(holder, 100, "%6.2LA", (long double)42.42);
  i = asprintf(&h, "%6.2LA", (long double)42.42);
  printf ("<%6.2LA>\n", (long double)42.42);
  printf ("<%s>\n", holder);
  printf ("<%s>\n\n", h);
#endif

  printf ("<%%.10240f> DBL_MAX\n");
  si = snprintf(holder, 100, "%.10240f", DBL_MAX);
  ai = asprintf(&h, "%.10240f", DBL_MAX);
  printf ("<%.10240f>\n", DBL_MAX);
  printf ("<%d> <%s>\n", si, holder);
  printf ("<%d> <%s>\n\n", ai, h);

  printf ("<%%.10240Lf> LDBL_MAX\n");
  si = snprintf(holder, 100, "%.10240Lf", (long double)LDBL_MAX);
  ai = asprintf(&h, "%.10240Lf", (long double)LDBL_MAX);
  printf ("<%.10240Lf>\n", (long double)LDBL_MAX);
  printf ("<%d> <%s>\n", si, holder);
  printf ("<%d> <%s>\n\n", ai, h);

  /* huh? */
  printf("/%%g/, 421.2345\n");
  snprintf(holder, sizeof holder, "/%g/\n", 421.2345);
  asprintf(&h, "/%g/\n", 421.2345);
  printf("/%g/\n", 421.2345);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%g/, 4214.2345\n");
  snprintf(holder, sizeof holder, "/%g/\n", 4214.2345);
  asprintf(&h, "/%g/\n", 4214.2345);
  printf("/%g/\n", 4214.2345);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%.5g/, 4214.2345\n");
  snprintf(holder, sizeof holder, "/%.5g/\n", 4214.2345);
  asprintf(&h, "/%.5g/\n", 4214.2345);
  printf("/%.5g/\n", 4214.2345);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%.4g/, 4214.2345\n");
  snprintf(holder, sizeof holder, "/%.4g/\n", 4214.2345);
  asprintf(&h, "/%.4g/\n", 4214.2345);
  printf("/%.4g/\n", 4214.2345);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%'ld %%'ld/, 12345, 1234567\n");
  snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 12345, 1234567);
  asprintf(&h, "/%'ld %'ld/\n", 12345, 1234567);
  printf("/%'ld %'ld/\n", 12345, 1234567);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%'ld %%'ld/, 336, 3336\n");
  snprintf(holder, sizeof holder, "/%'ld %'ld/\n", 336, 3336);
  asprintf(&h, "/%'ld %'ld/\n", 336, 3336);
  printf("/%'ld %'ld/\n", 336, 3336);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%'ld %%'ld/, -42786, -142786\n");
  snprintf(holder, sizeof holder, "/%'ld %'ld/\n", -42786, -142786);
  asprintf(&h, "/%'ld %'ld/\n", -42786, -142786);
  printf("/%'ld %'ld/\n", -42786, -142786);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%'f %%'f/, 421.2345, 421234.56789\n");
  snprintf(holder, sizeof holder, "/%'f %'f/\n", 421.2345, 421234.56789);
  asprintf(&h, "/%'f %'f/\n", 421.2345, 421234.56789);
  printf("/%'f %'f/\n", 421.2345, 421234.56789);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%'f %%'f/, -421.2345, -421234.56789\n");
  snprintf(holder, sizeof holder, "/%'f %'f/\n", -421.2345, -421234.56789);
  asprintf(&h, "/%'f %'f/\n", -421.2345, -421234.56789);
  printf("/%'f %'f/\n", -421.2345, -421234.56789);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%'g %%'g/, 421.2345, 421234.56789\n");
  snprintf(holder, sizeof holder, "/%'g %'g/\n", 421.2345, 421234.56789);
  asprintf(&h, "/%'g %'g/\n", 421.2345, 421234.56789);
  printf("/%'g %'g/\n", 421.2345, 421234.56789);
  printf("%s", holder);
  printf("%s\n", h);

  printf("/%%'g %%'g/, -421.2345, -421234.56789\n");
  snprintf(holder, sizeof holder, "/%'g %'g/\n", -421.2345, -421234.56789);
  asprintf(&h, "/%'g %'g/\n", -421.2345, -421234.56789);
  printf("/%'g %'g/\n", -421.2345, -421234.56789);
  printf("%s", holder);
  printf("%s\n", h);
#endif

  printf("/%%'g/, 4213455.8392\n");
  snprintf(holder, sizeof holder, "/%'g/\n", 4213455.8392);
  asprintf(&h, "/%'g/\n", 4213455.8392);
  printf("/%'g/\n", 4213455.8392);
  printf("%s", holder);
  printf("%s\n", h);

  exit (0);
}
#endif