1483
1814
/* Ensures that allocated >= needed. Aborts through a jump to
1484
1815
out_of_memory if needed is SIZE_MAX or otherwise too big. */
1485
1816
#define ENSURE_ALLOCATION(needed) \
1486
if ((needed) > allocated) \
1488
size_t memory_size; \
1491
allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1492
if ((needed) > allocated) \
1493
allocated = (needed); \
1494
memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1495
if (size_overflow_p (memory_size)) \
1496
goto out_of_memory; \
1497
if (result == resultbuf || result == NULL) \
1498
memory = (DCHAR_T *) malloc (memory_size); \
1500
memory = (DCHAR_T *) realloc (result, memory_size); \
1501
if (memory == NULL) \
1502
goto out_of_memory; \
1503
if (result == resultbuf && length > 0) \
1504
DCHAR_CPY (memory, result, length); \
1817
if ((needed) > allocated) \
1819
size_t memory_size; \
1822
allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1823
if ((needed) > allocated) \
1824
allocated = (needed); \
1825
memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1826
if (size_overflow_p (memory_size)) \
1827
goto out_of_memory; \
1828
if (result == resultbuf || result == NULL) \
1829
memory = (DCHAR_T *) malloc (memory_size); \
1831
memory = (DCHAR_T *) realloc (result, memory_size); \
1832
if (memory == NULL) \
1833
goto out_of_memory; \
1834
if (result == resultbuf && length > 0) \
1835
DCHAR_CPY (memory, result, length); \
1508
1839
for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
1510
if (cp != dp->dir_start)
1512
size_t n = dp->dir_start - cp;
1513
size_t augmented_length = xsum (length, n);
1515
ENSURE_ALLOCATION (augmented_length);
1516
/* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1517
need that the format string contains only ASCII characters
1518
if FCHAR_T and DCHAR_T are not the same type. */
1519
if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1521
DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1522
length = augmented_length;
1527
result[length++] = (unsigned char) *cp++;
1534
/* Execute a single directive. */
1535
if (dp->conversion == '%')
1537
size_t augmented_length;
1539
if (!(dp->arg_index == ARG_NONE))
1541
augmented_length = xsum (length, 1);
1542
ENSURE_ALLOCATION (augmented_length);
1543
result[length] = '%';
1544
length = augmented_length;
1548
if (!(dp->arg_index != ARG_NONE))
1551
if (dp->conversion == 'n')
1553
switch (a.arg[dp->arg_index].type)
1555
case TYPE_COUNT_SCHAR_POINTER:
1556
*a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1558
case TYPE_COUNT_SHORT_POINTER:
1559
*a.arg[dp->arg_index].a.a_count_short_pointer = length;
1561
case TYPE_COUNT_INT_POINTER:
1562
*a.arg[dp->arg_index].a.a_count_int_pointer = length;
1564
case TYPE_COUNT_LONGINT_POINTER:
1565
*a.arg[dp->arg_index].a.a_count_longint_pointer = length;
1841
if (cp != dp->dir_start)
1843
size_t n = dp->dir_start - cp;
1844
size_t augmented_length = xsum (length, n);
1846
ENSURE_ALLOCATION (augmented_length);
1847
/* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1848
need that the format string contains only ASCII characters
1849
if FCHAR_T and DCHAR_T are not the same type. */
1850
if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1852
DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1853
length = augmented_length;
1858
result[length++] = (unsigned char) *cp++;
1865
/* Execute a single directive. */
1866
if (dp->conversion == '%')
1868
size_t augmented_length;
1870
if (!(dp->arg_index == ARG_NONE))
1872
augmented_length = xsum (length, 1);
1873
ENSURE_ALLOCATION (augmented_length);
1874
result[length] = '%';
1875
length = augmented_length;
1879
if (!(dp->arg_index != ARG_NONE))
1882
if (dp->conversion == 'n')
1884
switch (a.arg[dp->arg_index].type)
1886
case TYPE_COUNT_SCHAR_POINTER:
1887
*a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1889
case TYPE_COUNT_SHORT_POINTER:
1890
*a.arg[dp->arg_index].a.a_count_short_pointer = length;
1892
case TYPE_COUNT_INT_POINTER:
1893
*a.arg[dp->arg_index].a.a_count_int_pointer = length;
1895
case TYPE_COUNT_LONGINT_POINTER:
1896
*a.arg[dp->arg_index].a.a_count_longint_pointer = length;
1567
1898
#if HAVE_LONG_LONG_INT
1568
case TYPE_COUNT_LONGLONGINT_POINTER:
1569
*a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
1899
case TYPE_COUNT_LONGLONGINT_POINTER:
1900
*a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
1576
1907
#if ENABLE_UNISTDIO
1577
/* The unistdio extensions. */
1578
else if (dp->conversion == 'U')
1580
arg_type type = a.arg[dp->arg_index].type;
1581
int flags = dp->flags;
1589
if (dp->width_start != dp->width_end)
1591
if (dp->width_arg_index != ARG_NONE)
1595
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1597
arg = a.arg[dp->width_arg_index].a.a_int;
1600
/* "A negative field width is taken as a '-' flag
1601
followed by a positive field width." */
1603
width = (unsigned int) (-arg);
1610
const FCHAR_T *digitp = dp->width_start;
1613
width = xsum (xtimes (width, 10), *digitp++ - '0');
1614
while (digitp != dp->width_end);
1621
if (dp->precision_start != dp->precision_end)
1623
if (dp->precision_arg_index != ARG_NONE)
1627
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1629
arg = a.arg[dp->precision_arg_index].a.a_int;
1630
/* "A negative precision is taken as if the precision
1640
const FCHAR_T *digitp = dp->precision_start + 1;
1643
while (digitp != dp->precision_end)
1644
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1651
case TYPE_U8_STRING:
1653
const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1654
const uint8_t *arg_end;
1659
/* Use only PRECISION characters, from the left. */
1662
for (; precision > 0; precision--)
1664
int count = u8_strmblen (arg_end);
1669
if (!(result == resultbuf || result == NULL))
1671
if (buf_malloced != NULL)
1672
free (buf_malloced);
1683
/* Use the entire string, and count the number of
1689
int count = u8_strmblen (arg_end);
1694
if (!(result == resultbuf || result == NULL))
1696
if (buf_malloced != NULL)
1697
free (buf_malloced);
1708
/* Use the entire string. */
1709
arg_end = arg + u8_strlen (arg);
1710
/* The number of characters doesn't matter. */
1714
if (has_width && width > characters
1715
&& !(dp->flags & FLAG_LEFT))
1717
size_t n = width - characters;
1718
ENSURE_ALLOCATION (xsum (length, n));
1719
DCHAR_SET (result + length, ' ', n);
1908
/* The unistdio extensions. */
1909
else if (dp->conversion == 'U')
1911
arg_type type = a.arg[dp->arg_index].type;
1912
int flags = dp->flags;
1920
if (dp->width_start != dp->width_end)
1922
if (dp->width_arg_index != ARG_NONE)
1926
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1928
arg = a.arg[dp->width_arg_index].a.a_int;
1931
/* "A negative field width is taken as a '-' flag
1932
followed by a positive field width." */
1934
width = (unsigned int) (-arg);
1941
const FCHAR_T *digitp = dp->width_start;
1944
width = xsum (xtimes (width, 10), *digitp++ - '0');
1945
while (digitp != dp->width_end);
1952
if (dp->precision_start != dp->precision_end)
1954
if (dp->precision_arg_index != ARG_NONE)
1958
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1960
arg = a.arg[dp->precision_arg_index].a.a_int;
1961
/* "A negative precision is taken as if the precision
1971
const FCHAR_T *digitp = dp->precision_start + 1;
1974
while (digitp != dp->precision_end)
1975
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1982
case TYPE_U8_STRING:
1984
const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1985
const uint8_t *arg_end;
1990
/* Use only PRECISION characters, from the left. */
1993
for (; precision > 0; precision--)
1995
int count = u8_strmblen (arg_end);
2000
if (!(result == resultbuf || result == NULL))
2002
if (buf_malloced != NULL)
2003
free (buf_malloced);
2014
/* Use the entire string, and count the number of
2020
int count = u8_strmblen (arg_end);
2025
if (!(result == resultbuf || result == NULL))
2027
if (buf_malloced != NULL)
2028
free (buf_malloced);
2039
/* Use the entire string. */
2040
arg_end = arg + u8_strlen (arg);
2041
/* The number of characters doesn't matter. */
2045
if (has_width && width > characters
2046
&& !(dp->flags & FLAG_LEFT))
2048
size_t n = width - characters;
2049
ENSURE_ALLOCATION (xsum (length, n));
2050
DCHAR_SET (result + length, ' ', n);
1723
2054
# if DCHAR_IS_UINT8_T
1725
size_t n = arg_end - arg;
1726
ENSURE_ALLOCATION (xsum (length, n));
1727
DCHAR_CPY (result + length, arg, n);
2056
size_t n = arg_end - arg;
2057
ENSURE_ALLOCATION (xsum (length, n));
2058
DCHAR_CPY (result + length, arg, n);
1732
DCHAR_T *converted = result + length;
1733
size_t converted_len = allocated - length;
2063
DCHAR_T *converted = result + length;
2064
size_t converted_len = allocated - length;
1734
2065
# if DCHAR_IS_TCHAR
1735
/* Convert from UTF-8 to locale encoding. */
1736
if (u8_conv_to_encoding (locale_charset (),
1737
iconveh_question_mark,
1738
arg, arg_end - arg, NULL,
1739
&converted, &converted_len)
2066
/* Convert from UTF-8 to locale encoding. */
2068
u8_conv_to_encoding (locale_charset (),
2069
iconveh_question_mark,
2070
arg, arg_end - arg, NULL,
2071
converted, &converted_len);
1742
/* Convert from UTF-8 to UTF-16/UTF-32. */
1744
U8_TO_DCHAR (arg, arg_end - arg,
1745
converted, &converted_len);
1746
if (converted == NULL)
2073
/* Convert from UTF-8 to UTF-16/UTF-32. */
2075
U8_TO_DCHAR (arg, arg_end - arg,
2076
converted, &converted_len);
1749
int saved_errno = errno;
1750
if (!(result == resultbuf || result == NULL))
1752
if (buf_malloced != NULL)
1753
free (buf_malloced);
1755
errno = saved_errno;
1758
if (converted != result + length)
1760
ENSURE_ALLOCATION (xsum (length, converted_len));
1761
DCHAR_CPY (result + length, converted, converted_len);
1764
length += converted_len;
2078
if (converted == NULL)
2080
int saved_errno = errno;
2081
if (!(result == resultbuf || result == NULL))
2083
if (buf_malloced != NULL)
2084
free (buf_malloced);
2086
errno = saved_errno;
2089
if (converted != result + length)
2091
ENSURE_ALLOCATION (xsum (length, converted_len));
2092
DCHAR_CPY (result + length, converted, converted_len);
2095
length += converted_len;
1768
if (has_width && width > characters
1769
&& (dp->flags & FLAG_LEFT))
1771
size_t n = width - characters;
1772
ENSURE_ALLOCATION (xsum (length, n));
1773
DCHAR_SET (result + length, ' ', n);
1779
case TYPE_U16_STRING:
1781
const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
1782
const uint16_t *arg_end;
1787
/* Use only PRECISION characters, from the left. */
1790
for (; precision > 0; precision--)
1792
int count = u16_strmblen (arg_end);
1797
if (!(result == resultbuf || result == NULL))
1799
if (buf_malloced != NULL)
1800
free (buf_malloced);
1811
/* Use the entire string, and count the number of
1817
int count = u16_strmblen (arg_end);
1822
if (!(result == resultbuf || result == NULL))
1824
if (buf_malloced != NULL)
1825
free (buf_malloced);
1836
/* Use the entire string. */
1837
arg_end = arg + u16_strlen (arg);
1838
/* The number of characters doesn't matter. */
1842
if (has_width && width > characters
1843
&& !(dp->flags & FLAG_LEFT))
1845
size_t n = width - characters;
1846
ENSURE_ALLOCATION (xsum (length, n));
1847
DCHAR_SET (result + length, ' ', n);
2099
if (has_width && width > characters
2100
&& (dp->flags & FLAG_LEFT))
2102
size_t n = width - characters;
2103
ENSURE_ALLOCATION (xsum (length, n));
2104
DCHAR_SET (result + length, ' ', n);
2110
case TYPE_U16_STRING:
2112
const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
2113
const uint16_t *arg_end;
2118
/* Use only PRECISION characters, from the left. */
2121
for (; precision > 0; precision--)
2123
int count = u16_strmblen (arg_end);
2128
if (!(result == resultbuf || result == NULL))
2130
if (buf_malloced != NULL)
2131
free (buf_malloced);
2142
/* Use the entire string, and count the number of
2148
int count = u16_strmblen (arg_end);
2153
if (!(result == resultbuf || result == NULL))
2155
if (buf_malloced != NULL)
2156
free (buf_malloced);
2167
/* Use the entire string. */
2168
arg_end = arg + u16_strlen (arg);
2169
/* The number of characters doesn't matter. */
2173
if (has_width && width > characters
2174
&& !(dp->flags & FLAG_LEFT))
2176
size_t n = width - characters;
2177
ENSURE_ALLOCATION (xsum (length, n));
2178
DCHAR_SET (result + length, ' ', n);
1851
2182
# if DCHAR_IS_UINT16_T
1853
size_t n = arg_end - arg;
1854
ENSURE_ALLOCATION (xsum (length, n));
1855
DCHAR_CPY (result + length, arg, n);
2184
size_t n = arg_end - arg;
2185
ENSURE_ALLOCATION (xsum (length, n));
2186
DCHAR_CPY (result + length, arg, n);
1860
DCHAR_T *converted = result + length;
1861
size_t converted_len = allocated - length;
2191
DCHAR_T *converted = result + length;
2192
size_t converted_len = allocated - length;
1862
2193
# if DCHAR_IS_TCHAR
1863
/* Convert from UTF-16 to locale encoding. */
1864
if (u16_conv_to_encoding (locale_charset (),
1865
iconveh_question_mark,
1866
arg, arg_end - arg, NULL,
1867
&converted, &converted_len)
2194
/* Convert from UTF-16 to locale encoding. */
2196
u16_conv_to_encoding (locale_charset (),
2197
iconveh_question_mark,
2198
arg, arg_end - arg, NULL,
2199
converted, &converted_len);
1870
/* Convert from UTF-16 to UTF-8/UTF-32. */
1872
U16_TO_DCHAR (arg, arg_end - arg,
1873
converted, &converted_len);
1874
if (converted == NULL)
2201
/* Convert from UTF-16 to UTF-8/UTF-32. */
2203
U16_TO_DCHAR (arg, arg_end - arg,
2204
converted, &converted_len);
1877
int saved_errno = errno;
1878
if (!(result == resultbuf || result == NULL))
1880
if (buf_malloced != NULL)
1881
free (buf_malloced);
1883
errno = saved_errno;
1886
if (converted != result + length)
1888
ENSURE_ALLOCATION (xsum (length, converted_len));
1889
DCHAR_CPY (result + length, converted, converted_len);
1892
length += converted_len;
2206
if (converted == NULL)
2208
int saved_errno = errno;
2209
if (!(result == resultbuf || result == NULL))
2211
if (buf_malloced != NULL)
2212
free (buf_malloced);
2214
errno = saved_errno;
2217
if (converted != result + length)
2219
ENSURE_ALLOCATION (xsum (length, converted_len));
2220
DCHAR_CPY (result + length, converted, converted_len);
2223
length += converted_len;
1896
if (has_width && width > characters
1897
&& (dp->flags & FLAG_LEFT))
1899
size_t n = width - characters;
1900
ENSURE_ALLOCATION (xsum (length, n));
1901
DCHAR_SET (result + length, ' ', n);
1907
case TYPE_U32_STRING:
1909
const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
1910
const uint32_t *arg_end;
1915
/* Use only PRECISION characters, from the left. */
1918
for (; precision > 0; precision--)
1920
int count = u32_strmblen (arg_end);
1925
if (!(result == resultbuf || result == NULL))
1927
if (buf_malloced != NULL)
1928
free (buf_malloced);
1939
/* Use the entire string, and count the number of
1945
int count = u32_strmblen (arg_end);
1950
if (!(result == resultbuf || result == NULL))
1952
if (buf_malloced != NULL)
1953
free (buf_malloced);
1964
/* Use the entire string. */
1965
arg_end = arg + u32_strlen (arg);
1966
/* The number of characters doesn't matter. */
1970
if (has_width && width > characters
1971
&& !(dp->flags & FLAG_LEFT))
1973
size_t n = width - characters;
1974
ENSURE_ALLOCATION (xsum (length, n));
1975
DCHAR_SET (result + length, ' ', n);
2227
if (has_width && width > characters
2228
&& (dp->flags & FLAG_LEFT))
2230
size_t n = width - characters;
2231
ENSURE_ALLOCATION (xsum (length, n));
2232
DCHAR_SET (result + length, ' ', n);
2238
case TYPE_U32_STRING:
2240
const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
2241
const uint32_t *arg_end;
2246
/* Use only PRECISION characters, from the left. */
2249
for (; precision > 0; precision--)
2251
int count = u32_strmblen (arg_end);
2256
if (!(result == resultbuf || result == NULL))
2258
if (buf_malloced != NULL)
2259
free (buf_malloced);
2270
/* Use the entire string, and count the number of
2276
int count = u32_strmblen (arg_end);
2281
if (!(result == resultbuf || result == NULL))
2283
if (buf_malloced != NULL)
2284
free (buf_malloced);
2295
/* Use the entire string. */
2296
arg_end = arg + u32_strlen (arg);
2297
/* The number of characters doesn't matter. */
2301
if (has_width && width > characters
2302
&& !(dp->flags & FLAG_LEFT))
2304
size_t n = width - characters;
2305
ENSURE_ALLOCATION (xsum (length, n));
2306
DCHAR_SET (result + length, ' ', n);
1979
2310
# if DCHAR_IS_UINT32_T
1981
size_t n = arg_end - arg;
1982
ENSURE_ALLOCATION (xsum (length, n));
1983
DCHAR_CPY (result + length, arg, n);
1988
DCHAR_T *converted = result + length;
1989
size_t converted_len = allocated - length;
1991
/* Convert from UTF-32 to locale encoding. */
1992
if (u32_conv_to_encoding (locale_charset (),
1993
iconveh_question_mark,
1994
arg, arg_end - arg, NULL,
1995
&converted, &converted_len)
1998
/* Convert from UTF-32 to UTF-8/UTF-16. */
2000
U32_TO_DCHAR (arg, arg_end - arg,
2001
converted, &converted_len);
2002
if (converted == NULL)
2005
int saved_errno = errno;
2006
if (!(result == resultbuf || result == NULL))
2008
if (buf_malloced != NULL)
2009
free (buf_malloced);
2011
errno = saved_errno;
2014
if (converted != result + length)
2016
ENSURE_ALLOCATION (xsum (length, converted_len));
2017
DCHAR_CPY (result + length, converted, converted_len);
2020
length += converted_len;
2024
if (has_width && width > characters
2025
&& (dp->flags & FLAG_LEFT))
2027
size_t n = width - characters;
2028
ENSURE_ALLOCATION (xsum (length, n));
2029
DCHAR_SET (result + length, ' ', n);
2312
size_t n = arg_end - arg;
2313
ENSURE_ALLOCATION (xsum (length, n));
2314
DCHAR_CPY (result + length, arg, n);
2319
DCHAR_T *converted = result + length;
2320
size_t converted_len = allocated - length;
2322
/* Convert from UTF-32 to locale encoding. */
2324
u32_conv_to_encoding (locale_charset (),
2325
iconveh_question_mark,
2326
arg, arg_end - arg, NULL,
2327
converted, &converted_len);
2329
/* Convert from UTF-32 to UTF-8/UTF-16. */
2331
U32_TO_DCHAR (arg, arg_end - arg,
2332
converted, &converted_len);
2334
if (converted == NULL)
2336
int saved_errno = errno;
2337
if (!(result == resultbuf || result == NULL))
2339
if (buf_malloced != NULL)
2340
free (buf_malloced);
2342
errno = saved_errno;
2345
if (converted != result + length)
2347
ENSURE_ALLOCATION (xsum (length, converted_len));
2348
DCHAR_CPY (result + length, converted, converted_len);
2351
length += converted_len;
2355
if (has_width && width > characters
2356
&& (dp->flags & FLAG_LEFT))
2358
size_t n = width - characters;
2359
ENSURE_ALLOCATION (xsum (length, n));
2360
DCHAR_SET (result + length, ' ', n);
2371
#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2372
else if (dp->conversion == 's'
2373
# if WIDE_CHAR_VERSION
2374
&& a.arg[dp->arg_index].type != TYPE_WIDE_STRING
2376
&& a.arg[dp->arg_index].type == TYPE_WIDE_STRING
2380
/* The normal handling of the 's' directive below requires
2381
allocating a temporary buffer. The determination of its
2382
length (tmp_length), in the case when a precision is
2383
specified, below requires a conversion between a char[]
2384
string and a wchar_t[] wide string. It could be done, but
2385
we have no guarantee that the implementation of sprintf will
2386
use the exactly same algorithm. Without this guarantee, it
2387
is possible to have buffer overrun bugs. In order to avoid
2388
such bugs, we implement the entire processing of the 's'
2389
directive ourselves. */
2390
int flags = dp->flags;
2398
if (dp->width_start != dp->width_end)
2400
if (dp->width_arg_index != ARG_NONE)
2404
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2406
arg = a.arg[dp->width_arg_index].a.a_int;
2409
/* "A negative field width is taken as a '-' flag
2410
followed by a positive field width." */
2412
width = (unsigned int) (-arg);
2419
const FCHAR_T *digitp = dp->width_start;
2422
width = xsum (xtimes (width, 10), *digitp++ - '0');
2423
while (digitp != dp->width_end);
2430
if (dp->precision_start != dp->precision_end)
2432
if (dp->precision_arg_index != ARG_NONE)
2436
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2438
arg = a.arg[dp->precision_arg_index].a.a_int;
2439
/* "A negative precision is taken as if the precision
2449
const FCHAR_T *digitp = dp->precision_start + 1;
2452
while (digitp != dp->precision_end)
2453
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2458
# if WIDE_CHAR_VERSION
2459
/* %s in vasnwprintf. See the specification of fwprintf. */
2461
const char *arg = a.arg[dp->arg_index].a.a_string;
2462
const char *arg_end;
2467
/* Use only as many bytes as needed to produce PRECISION
2468
wide characters, from the left. */
2471
memset (&state, '\0', sizeof (mbstate_t));
2475
for (; precision > 0; precision--)
2479
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2481
count = mblen (arg_end, MB_CUR_MAX);
2484
/* Found the terminating NUL. */
2488
/* Invalid or incomplete multibyte character. */
2489
if (!(result == resultbuf || result == NULL))
2491
if (buf_malloced != NULL)
2492
free (buf_malloced);
2503
/* Use the entire string, and count the number of wide
2507
memset (&state, '\0', sizeof (mbstate_t));
2515
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2517
count = mblen (arg_end, MB_CUR_MAX);
2520
/* Found the terminating NUL. */
2524
/* Invalid or incomplete multibyte character. */
2525
if (!(result == resultbuf || result == NULL))
2527
if (buf_malloced != NULL)
2528
free (buf_malloced);
2539
/* Use the entire string. */
2540
arg_end = arg + strlen (arg);
2541
/* The number of characters doesn't matter. */
2545
if (has_width && width > characters
2546
&& !(dp->flags & FLAG_LEFT))
2548
size_t n = width - characters;
2549
ENSURE_ALLOCATION (xsum (length, n));
2550
DCHAR_SET (result + length, ' ', n);
2554
if (has_precision || has_width)
2556
/* We know the number of wide characters in advance. */
2560
memset (&state, '\0', sizeof (mbstate_t));
2562
ENSURE_ALLOCATION (xsum (length, characters));
2563
for (remaining = characters; remaining > 0; remaining--)
2568
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2570
count = mbtowc (&wc, arg, arg_end - arg);
2573
/* mbrtowc not consistent with mbrlen, or mbtowc
2574
not consistent with mblen. */
2576
result[length++] = wc;
2579
if (!(arg == arg_end))
2586
memset (&state, '\0', sizeof (mbstate_t));
2588
while (arg < arg_end)
2593
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2595
count = mbtowc (&wc, arg, arg_end - arg);
2598
/* mbrtowc not consistent with mbrlen, or mbtowc
2599
not consistent with mblen. */
2601
ENSURE_ALLOCATION (xsum (length, 1));
2602
result[length++] = wc;
2607
if (has_width && width > characters
2608
&& (dp->flags & FLAG_LEFT))
2610
size_t n = width - characters;
2611
ENSURE_ALLOCATION (xsum (length, n));
2612
DCHAR_SET (result + length, ' ', n);
2617
/* %ls in vasnprintf. See the specification of fprintf. */
2619
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
2620
const wchar_t *arg_end;
2622
# if !DCHAR_IS_TCHAR
2623
/* This code assumes that TCHAR_T is 'char'. */
2624
typedef int TCHAR_T_verify[2 * (sizeof (TCHAR_T) == 1) - 1];
2633
/* Use only as many wide characters as needed to produce
2634
at most PRECISION bytes, from the left. */
2635
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2637
memset (&state, '\0', sizeof (mbstate_t));
2641
while (precision > 0)
2643
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2647
/* Found the terminating null wide character. */
2649
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2650
count = wcrtomb (cbuf, *arg_end, &state);
2652
count = wctomb (cbuf, *arg_end);
2656
/* Cannot convert. */
2657
if (!(result == resultbuf || result == NULL))
2659
if (buf_malloced != NULL)
2660
free (buf_malloced);
2665
if (precision < count)
2668
characters += count;
2678
/* Use the entire string, and count the number of
2680
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2682
memset (&state, '\0', sizeof (mbstate_t));
2688
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2692
/* Found the terminating null wide character. */
2694
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2695
count = wcrtomb (cbuf, *arg_end, &state);
2697
count = wctomb (cbuf, *arg_end);
2701
/* Cannot convert. */
2702
if (!(result == resultbuf || result == NULL))
2704
if (buf_malloced != NULL)
2705
free (buf_malloced);
2711
characters += count;
2717
/* Use the entire string. */
2718
arg_end = arg + local_wcslen (arg);
2719
/* The number of bytes doesn't matter. */
2724
# if !DCHAR_IS_TCHAR
2725
/* Convert the string into a piece of temporary memory. */
2726
tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
2730
TCHAR_T *tmpptr = tmpsrc;
2732
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2734
memset (&state, '\0', sizeof (mbstate_t));
2736
for (remaining = characters; remaining > 0; )
2738
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2743
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2744
count = wcrtomb (cbuf, *arg, &state);
2746
count = wctomb (cbuf, *arg);
2749
/* Inconsistency. */
2751
memcpy (tmpptr, cbuf, count);
2756
if (!(arg == arg_end))
2760
/* Convert from TCHAR_T[] to DCHAR_T[]. */
2762
DCHAR_CONV_FROM_ENCODING (locale_charset (),
2763
iconveh_question_mark,
2769
int saved_errno = errno;
2771
if (!(result == resultbuf || result == NULL))
2773
if (buf_malloced != NULL)
2774
free (buf_malloced);
2776
errno = saved_errno;
2784
# if ENABLE_UNISTDIO
2785
/* Outside POSIX, it's preferrable to compare the width
2786
against the number of _characters_ of the converted
2788
w = DCHAR_MBSNLEN (result + length, characters);
2790
/* The width is compared against the number of _bytes_
2791
of the converted value, says POSIX. */
2796
/* w doesn't matter. */
2799
if (has_width && width > w
2800
&& !(dp->flags & FLAG_LEFT))
2802
size_t n = width - w;
2803
ENSURE_ALLOCATION (xsum (length, n));
2804
DCHAR_SET (result + length, ' ', n);
2809
if (has_precision || has_width)
2811
/* We know the number of bytes in advance. */
2813
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2815
memset (&state, '\0', sizeof (mbstate_t));
2817
ENSURE_ALLOCATION (xsum (length, characters));
2818
for (remaining = characters; remaining > 0; )
2820
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2825
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2826
count = wcrtomb (cbuf, *arg, &state);
2828
count = wctomb (cbuf, *arg);
2831
/* Inconsistency. */
2833
memcpy (result + length, cbuf, count);
2838
if (!(arg == arg_end))
2843
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2845
memset (&state, '\0', sizeof (mbstate_t));
2847
while (arg < arg_end)
2849
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2854
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2855
count = wcrtomb (cbuf, *arg, &state);
2857
count = wctomb (cbuf, *arg);
2861
/* Cannot convert. */
2862
if (!(result == resultbuf || result == NULL))
2864
if (buf_malloced != NULL)
2865
free (buf_malloced);
2870
ENSURE_ALLOCATION (xsum (length, count));
2871
memcpy (result + length, cbuf, count);
2877
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
2878
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
2880
length += tmpdst_len;
2883
if (has_width && width > w
2884
&& (dp->flags & FLAG_LEFT))
2886
size_t n = width - w;
2887
ENSURE_ALLOCATION (xsum (length, n));
2888
DCHAR_SET (result + length, ' ', n);
2040
2895
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2041
else if ((dp->conversion == 'a' || dp->conversion == 'A')
2896
else if ((dp->conversion == 'a' || dp->conversion == 'A')
2042
2897
# if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2044
2899
# if NEED_PRINTF_DOUBLE
2045
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2900
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2047
2902
# if NEED_PRINTF_LONG_DOUBLE
2048
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2903
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2054
arg_type type = a.arg[dp->arg_index].type;
2055
int flags = dp->flags;
2061
DCHAR_T tmpbuf[700];
2068
if (dp->width_start != dp->width_end)
2070
if (dp->width_arg_index != ARG_NONE)
2074
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2076
arg = a.arg[dp->width_arg_index].a.a_int;
2079
/* "A negative field width is taken as a '-' flag
2080
followed by a positive field width." */
2082
width = (unsigned int) (-arg);
2089
const FCHAR_T *digitp = dp->width_start;
2092
width = xsum (xtimes (width, 10), *digitp++ - '0');
2093
while (digitp != dp->width_end);
2100
if (dp->precision_start != dp->precision_end)
2102
if (dp->precision_arg_index != ARG_NONE)
2106
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2108
arg = a.arg[dp->precision_arg_index].a.a_int;
2109
/* "A negative precision is taken as if the precision
2119
const FCHAR_T *digitp = dp->precision_start + 1;
2122
while (digitp != dp->precision_end)
2123
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2128
/* Allocate a temporary buffer of sufficient size. */
2129
if (type == TYPE_LONGDOUBLE)
2131
(unsigned int) ((LDBL_DIG + 1)
2132
* 0.831 /* decimal -> hexadecimal */
2134
+ 1; /* turn floor into ceil */
2137
(unsigned int) ((DBL_DIG + 1)
2138
* 0.831 /* decimal -> hexadecimal */
2140
+ 1; /* turn floor into ceil */
2141
if (tmp_length < precision)
2142
tmp_length = precision;
2143
/* Account for sign, decimal point etc. */
2144
tmp_length = xsum (tmp_length, 12);
2146
if (tmp_length < width)
2149
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2151
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2155
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2157
if (size_overflow_p (tmp_memsize))
2158
/* Overflow, would lead to out of memory. */
2160
tmp = (DCHAR_T *) malloc (tmp_memsize);
2162
/* Out of memory. */
2168
if (type == TYPE_LONGDOUBLE)
2909
arg_type type = a.arg[dp->arg_index].type;
2910
int flags = dp->flags;
2916
DCHAR_T tmpbuf[700];
2923
if (dp->width_start != dp->width_end)
2925
if (dp->width_arg_index != ARG_NONE)
2929
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2931
arg = a.arg[dp->width_arg_index].a.a_int;
2934
/* "A negative field width is taken as a '-' flag
2935
followed by a positive field width." */
2937
width = (unsigned int) (-arg);
2944
const FCHAR_T *digitp = dp->width_start;
2947
width = xsum (xtimes (width, 10), *digitp++ - '0');
2948
while (digitp != dp->width_end);
2955
if (dp->precision_start != dp->precision_end)
2957
if (dp->precision_arg_index != ARG_NONE)
2961
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2963
arg = a.arg[dp->precision_arg_index].a.a_int;
2964
/* "A negative precision is taken as if the precision
2974
const FCHAR_T *digitp = dp->precision_start + 1;
2977
while (digitp != dp->precision_end)
2978
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2983
/* Allocate a temporary buffer of sufficient size. */
2984
if (type == TYPE_LONGDOUBLE)
2986
(unsigned int) ((LDBL_DIG + 1)
2987
* 0.831 /* decimal -> hexadecimal */
2989
+ 1; /* turn floor into ceil */
2992
(unsigned int) ((DBL_DIG + 1)
2993
* 0.831 /* decimal -> hexadecimal */
2995
+ 1; /* turn floor into ceil */
2996
if (tmp_length < precision)
2997
tmp_length = precision;
2998
/* Account for sign, decimal point etc. */
2999
tmp_length = xsum (tmp_length, 12);
3001
if (tmp_length < width)
3004
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3006
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3010
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3012
if (size_overflow_p (tmp_memsize))
3013
/* Overflow, would lead to out of memory. */
3015
tmp = (DCHAR_T *) malloc (tmp_memsize);
3017
/* Out of memory. */
3023
if (type == TYPE_LONGDOUBLE)
2170
3025
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
2171
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2175
if (dp->conversion == 'A')
2177
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2181
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2187
DECL_LONG_DOUBLE_ROUNDING
2189
BEGIN_LONG_DOUBLE_ROUNDING ();
2191
if (signbit (arg)) /* arg < 0.0L or negative zero */
2199
else if (flags & FLAG_SHOWSIGN)
2201
else if (flags & FLAG_SPACE)
2204
if (arg > 0.0L && arg + arg == arg)
2206
if (dp->conversion == 'A')
2208
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2212
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2218
long double mantissa;
2221
mantissa = printf_frexpl (arg, &exponent);
2229
&& precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
2231
/* Round the mantissa. */
2232
long double tail = mantissa;
2235
for (q = precision; ; q--)
2237
int digit = (int) tail;
2241
if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
2250
for (q = precision; q > 0; q--)
2256
*p++ = dp->conversion - 'A' + 'X';
2261
digit = (int) mantissa;
2264
if ((flags & FLAG_ALT)
2265
|| mantissa > 0.0L || precision > 0)
2267
*p++ = decimal_point_char ();
2268
/* This loop terminates because we assume
2269
that FLT_RADIX is a power of 2. */
2270
while (mantissa > 0.0L)
2273
digit = (int) mantissa;
2278
: dp->conversion - 10);
2282
while (precision > 0)
2289
*p++ = dp->conversion - 'A' + 'P';
3026
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3030
if (dp->conversion == 'A')
3032
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3036
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3042
DECL_LONG_DOUBLE_ROUNDING
3044
BEGIN_LONG_DOUBLE_ROUNDING ();
3046
if (signbit (arg)) /* arg < 0.0L or negative zero */
3054
else if (flags & FLAG_SHOWSIGN)
3056
else if (flags & FLAG_SPACE)
3059
if (arg > 0.0L && arg + arg == arg)
3061
if (dp->conversion == 'A')
3063
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3067
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3073
long double mantissa;
3076
mantissa = printf_frexpl (arg, &exponent);
3084
&& precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
3086
/* Round the mantissa. */
3087
long double tail = mantissa;
3090
for (q = precision; ; q--)
3092
int digit = (int) tail;
3096
if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
3105
for (q = precision; q > 0; q--)
3111
*p++ = dp->conversion - 'A' + 'X';
3116
digit = (int) mantissa;
3119
if ((flags & FLAG_ALT)
3120
|| mantissa > 0.0L || precision > 0)
3122
*p++ = decimal_point_char ();
3123
/* This loop terminates because we assume
3124
that FLT_RADIX is a power of 2. */
3125
while (mantissa > 0.0L)
3128
digit = (int) mantissa;
3133
: dp->conversion - 10);
3137
while (precision > 0)
3144
*p++ = dp->conversion - 'A' + 'P';
2290
3145
# if WIDE_CHAR_VERSION
2292
static const wchar_t decimal_format[] =
2293
{ '%', '+', 'd', '\0' };
2294
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3147
static const wchar_t decimal_format[] =
3148
{ '%', '+', 'd', '\0' };
3149
SNPRINTF (p, 6 + 1, decimal_format, exponent);
2299
if (sizeof (DCHAR_T) == 1)
2301
sprintf ((char *) p, "%+d", exponent);
2309
sprintf (expbuf, "%+d", exponent);
2310
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3154
if (sizeof (DCHAR_T) == 1)
3156
sprintf ((char *) p, "%+d", exponent);
3164
sprintf (expbuf, "%+d", exponent);
3165
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2316
END_LONG_DOUBLE_ROUNDING ();
3171
END_LONG_DOUBLE_ROUNDING ();
2324
3179
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
2325
double arg = a.arg[dp->arg_index].a.a_double;
2329
if (dp->conversion == 'A')
2331
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2335
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2342
if (signbit (arg)) /* arg < 0.0 or negative zero */
2350
else if (flags & FLAG_SHOWSIGN)
2352
else if (flags & FLAG_SPACE)
2355
if (arg > 0.0 && arg + arg == arg)
2357
if (dp->conversion == 'A')
2359
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2363
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2372
mantissa = printf_frexp (arg, &exponent);
2380
&& precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
2382
/* Round the mantissa. */
2383
double tail = mantissa;
2386
for (q = precision; ; q--)
2388
int digit = (int) tail;
2392
if (digit & 1 ? tail >= 0.5 : tail > 0.5)
2401
for (q = precision; q > 0; q--)
2407
*p++ = dp->conversion - 'A' + 'X';
2412
digit = (int) mantissa;
2415
if ((flags & FLAG_ALT)
2416
|| mantissa > 0.0 || precision > 0)
2418
*p++ = decimal_point_char ();
2419
/* This loop terminates because we assume
2420
that FLT_RADIX is a power of 2. */
2421
while (mantissa > 0.0)
2424
digit = (int) mantissa;
2429
: dp->conversion - 10);
2433
while (precision > 0)
2440
*p++ = dp->conversion - 'A' + 'P';
3180
double arg = a.arg[dp->arg_index].a.a_double;
3184
if (dp->conversion == 'A')
3186
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3190
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3197
if (signbit (arg)) /* arg < 0.0 or negative zero */
3205
else if (flags & FLAG_SHOWSIGN)
3207
else if (flags & FLAG_SPACE)
3210
if (arg > 0.0 && arg + arg == arg)
3212
if (dp->conversion == 'A')
3214
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3218
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3227
mantissa = printf_frexp (arg, &exponent);
3235
&& precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
3237
/* Round the mantissa. */
3238
double tail = mantissa;
3241
for (q = precision; ; q--)
3243
int digit = (int) tail;
3247
if (digit & 1 ? tail >= 0.5 : tail > 0.5)
3256
for (q = precision; q > 0; q--)
3262
*p++ = dp->conversion - 'A' + 'X';
3267
digit = (int) mantissa;
3270
if ((flags & FLAG_ALT)
3271
|| mantissa > 0.0 || precision > 0)
3273
*p++ = decimal_point_char ();
3274
/* This loop terminates because we assume
3275
that FLT_RADIX is a power of 2. */
3276
while (mantissa > 0.0)
3279
digit = (int) mantissa;
3284
: dp->conversion - 10);
3288
while (precision > 0)
3295
*p++ = dp->conversion - 'A' + 'P';
2441
3296
# if WIDE_CHAR_VERSION
2443
static const wchar_t decimal_format[] =
2444
{ '%', '+', 'd', '\0' };
2445
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3298
static const wchar_t decimal_format[] =
3299
{ '%', '+', 'd', '\0' };
3300
SNPRINTF (p, 6 + 1, decimal_format, exponent);
2450
if (sizeof (DCHAR_T) == 1)
2452
sprintf ((char *) p, "%+d", exponent);
2460
sprintf (expbuf, "%+d", exponent);
2461
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3305
if (sizeof (DCHAR_T) == 1)
3307
sprintf ((char *) p, "%+d", exponent);
3315
sprintf (expbuf, "%+d", exponent);
3316
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2471
/* The generated string now extends from tmp to p, with the
2472
zero padding insertion point being at pad_ptr. */
2473
if (has_width && p - tmp < width)
2475
size_t pad = width - (p - tmp);
2476
DCHAR_T *end = p + pad;
2478
if (flags & FLAG_LEFT)
2480
/* Pad with spaces on the right. */
2481
for (; pad > 0; pad--)
2484
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
2486
/* Pad with zeroes. */
2491
for (; pad > 0; pad--)
2496
/* Pad with spaces on the left. */
2501
for (; pad > 0; pad--)
2509
size_t count = p - tmp;
2511
if (count >= tmp_length)
2512
/* tmp_length was incorrectly calculated - fix the
2516
/* Make room for the result. */
2517
if (count >= allocated - length)
2519
size_t n = xsum (length, count);
2521
ENSURE_ALLOCATION (n);
2524
/* Append the result. */
2525
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3326
/* The generated string now extends from tmp to p, with the
3327
zero padding insertion point being at pad_ptr. */
3328
if (has_width && p - tmp < width)
3330
size_t pad = width - (p - tmp);
3331
DCHAR_T *end = p + pad;
3333
if (flags & FLAG_LEFT)
3335
/* Pad with spaces on the right. */
3336
for (; pad > 0; pad--)
3339
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3341
/* Pad with zeroes. */
3346
for (; pad > 0; pad--)
3351
/* Pad with spaces on the left. */
3356
for (; pad > 0; pad--)
3364
size_t count = p - tmp;
3366
if (count >= tmp_length)
3367
/* tmp_length was incorrectly calculated - fix the
3371
/* Make room for the result. */
3372
if (count >= allocated - length)
3374
size_t n = xsum (length, count);
3376
ENSURE_ALLOCATION (n);
3379
/* Append the result. */
3380
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
2532
3387
#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
2533
else if ((dp->conversion == 'f' || dp->conversion == 'F'
2534
|| dp->conversion == 'e' || dp->conversion == 'E'
2535
|| dp->conversion == 'g' || dp->conversion == 'G'
2536
|| dp->conversion == 'a' || dp->conversion == 'A')
3388
else if ((dp->conversion == 'f' || dp->conversion == 'F'
3389
|| dp->conversion == 'e' || dp->conversion == 'E'
3390
|| dp->conversion == 'g' || dp->conversion == 'G'
3391
|| dp->conversion == 'a' || dp->conversion == 'A')
2538
3393
# if NEED_PRINTF_DOUBLE
2539
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
3394
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2540
3395
# elif NEED_PRINTF_INFINITE_DOUBLE
2541
|| (a.arg[dp->arg_index].type == TYPE_DOUBLE
2542
/* The systems (mingw) which produce wrong output
2543
for Inf, -Inf, and NaN also do so for -0.0.
2544
Therefore we treat this case here as well. */
2545
&& is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
3396
|| (a.arg[dp->arg_index].type == TYPE_DOUBLE
3397
/* The systems (mingw) which produce wrong output
3398
for Inf, -Inf, and NaN also do so for -0.0.
3399
Therefore we treat this case here as well. */
3400
&& is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
2547
3402
# if NEED_PRINTF_LONG_DOUBLE
2548
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3403
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2549
3404
# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
2550
|| (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2551
/* Some systems produce wrong output for Inf,
2553
&& is_infinitel (a.arg[dp->arg_index].a.a_longdouble))
3405
|| (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3406
/* Some systems produce wrong output for Inf,
3407
-Inf, and NaN. Some systems in this category
3408
(IRIX 5.3) also do so for -0.0. Therefore we
3409
treat this case here as well. */
3410
&& is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
2557
3414
# if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
2558
arg_type type = a.arg[dp->arg_index].type;
3415
arg_type type = a.arg[dp->arg_index].type;
2560
int flags = dp->flags;
2566
DCHAR_T tmpbuf[700];
2573
if (dp->width_start != dp->width_end)
2575
if (dp->width_arg_index != ARG_NONE)
2579
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2581
arg = a.arg[dp->width_arg_index].a.a_int;
2584
/* "A negative field width is taken as a '-' flag
2585
followed by a positive field width." */
2587
width = (unsigned int) (-arg);
2594
const FCHAR_T *digitp = dp->width_start;
2597
width = xsum (xtimes (width, 10), *digitp++ - '0');
2598
while (digitp != dp->width_end);
2605
if (dp->precision_start != dp->precision_end)
2607
if (dp->precision_arg_index != ARG_NONE)
2611
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2613
arg = a.arg[dp->precision_arg_index].a.a_int;
2614
/* "A negative precision is taken as if the precision
2624
const FCHAR_T *digitp = dp->precision_start + 1;
2627
while (digitp != dp->precision_end)
2628
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2633
/* POSIX specifies the default precision to be 6 for %f, %F,
2634
%e, %E, but not for %g, %G. Implementations appear to use
2635
the same default precision also for %g, %G. */
2639
/* Allocate a temporary buffer of sufficient size. */
3417
int flags = dp->flags;
3423
DCHAR_T tmpbuf[700];
3430
if (dp->width_start != dp->width_end)
3432
if (dp->width_arg_index != ARG_NONE)
3436
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3438
arg = a.arg[dp->width_arg_index].a.a_int;
3441
/* "A negative field width is taken as a '-' flag
3442
followed by a positive field width." */
3444
width = (unsigned int) (-arg);
3451
const FCHAR_T *digitp = dp->width_start;
3454
width = xsum (xtimes (width, 10), *digitp++ - '0');
3455
while (digitp != dp->width_end);
3462
if (dp->precision_start != dp->precision_end)
3464
if (dp->precision_arg_index != ARG_NONE)
3468
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3470
arg = a.arg[dp->precision_arg_index].a.a_int;
3471
/* "A negative precision is taken as if the precision
3481
const FCHAR_T *digitp = dp->precision_start + 1;
3484
while (digitp != dp->precision_end)
3485
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3490
/* POSIX specifies the default precision to be 6 for %f, %F,
3491
%e, %E, but not for %g, %G. Implementations appear to use
3492
the same default precision also for %g, %G. But for %a, %A,
3493
the default precision is 0. */
3495
if (!(dp->conversion == 'a' || dp->conversion == 'A'))
3498
/* Allocate a temporary buffer of sufficient size. */
2640
3499
# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2641
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
3500
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
2642
3501
# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2643
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
3502
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
2644
3503
# elif NEED_PRINTF_LONG_DOUBLE
2645
tmp_length = LDBL_DIG + 1;
3504
tmp_length = LDBL_DIG + 1;
2646
3505
# elif NEED_PRINTF_DOUBLE
2647
tmp_length = DBL_DIG + 1;
3506
tmp_length = DBL_DIG + 1;
2651
if (tmp_length < precision)
2652
tmp_length = precision;
3510
if (tmp_length < precision)
3511
tmp_length = precision;
2653
3512
# if NEED_PRINTF_LONG_DOUBLE
2654
3513
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2655
if (type == TYPE_LONGDOUBLE)
3514
if (type == TYPE_LONGDOUBLE)
2657
if (dp->conversion == 'f' || dp->conversion == 'F')
2659
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2660
if (!(isnanl (arg) || arg + arg == arg))
2662
/* arg is finite and nonzero. */
2663
int exponent = floorlog10l (arg < 0 ? -arg : arg);
2664
if (exponent >= 0 && tmp_length < exponent + precision)
2665
tmp_length = exponent + precision;
3516
if (dp->conversion == 'f' || dp->conversion == 'F')
3518
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3519
if (!(isnanl (arg) || arg + arg == arg))
3521
/* arg is finite and nonzero. */
3522
int exponent = floorlog10l (arg < 0 ? -arg : arg);
3523
if (exponent >= 0 && tmp_length < exponent + precision)
3524
tmp_length = exponent + precision;
2669
3528
# if NEED_PRINTF_DOUBLE
2670
3529
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2671
if (type == TYPE_DOUBLE)
3530
if (type == TYPE_DOUBLE)
2673
if (dp->conversion == 'f' || dp->conversion == 'F')
2675
double arg = a.arg[dp->arg_index].a.a_double;
2676
if (!(isnan (arg) || arg + arg == arg))
2678
/* arg is finite and nonzero. */
2679
int exponent = floorlog10 (arg < 0 ? -arg : arg);
2680
if (exponent >= 0 && tmp_length < exponent + precision)
2681
tmp_length = exponent + precision;
3532
if (dp->conversion == 'f' || dp->conversion == 'F')
3534
double arg = a.arg[dp->arg_index].a.a_double;
3535
if (!(isnand (arg) || arg + arg == arg))
3537
/* arg is finite and nonzero. */
3538
int exponent = floorlog10 (arg < 0 ? -arg : arg);
3539
if (exponent >= 0 && tmp_length < exponent + precision)
3540
tmp_length = exponent + precision;
2685
/* Account for sign, decimal point etc. */
2686
tmp_length = xsum (tmp_length, 12);
2688
if (tmp_length < width)
2691
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2693
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2697
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2699
if (size_overflow_p (tmp_memsize))
2700
/* Overflow, would lead to out of memory. */
2702
tmp = (DCHAR_T *) malloc (tmp_memsize);
2704
/* Out of memory. */
3544
/* Account for sign, decimal point etc. */
3545
tmp_length = xsum (tmp_length, 12);
3547
if (tmp_length < width)
3550
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3552
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3556
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3558
if (size_overflow_p (tmp_memsize))
3559
/* Overflow, would lead to out of memory. */
3561
tmp = (DCHAR_T *) malloc (tmp_memsize);
3563
/* Out of memory. */
2711
3570
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2712
3571
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2713
if (type == TYPE_LONGDOUBLE)
3572
if (type == TYPE_LONGDOUBLE)
2716
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2720
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
2722
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2726
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2732
DECL_LONG_DOUBLE_ROUNDING
2734
BEGIN_LONG_DOUBLE_ROUNDING ();
2736
if (signbit (arg)) /* arg < 0.0L or negative zero */
2744
else if (flags & FLAG_SHOWSIGN)
2746
else if (flags & FLAG_SPACE)
2749
if (arg > 0.0L && arg + arg == arg)
2751
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
2753
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2757
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3575
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3579
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3581
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3585
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3591
DECL_LONG_DOUBLE_ROUNDING
3593
BEGIN_LONG_DOUBLE_ROUNDING ();
3595
if (signbit (arg)) /* arg < 0.0L or negative zero */
3603
else if (flags & FLAG_SHOWSIGN)
3605
else if (flags & FLAG_SPACE)
3608
if (arg > 0.0L && arg + arg == arg)
3610
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3612
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3616
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2762
3621
# if NEED_PRINTF_LONG_DOUBLE
2765
if (dp->conversion == 'f' || dp->conversion == 'F')
2771
scale10_round_decimal_long_double (arg, precision);
2774
END_LONG_DOUBLE_ROUNDING ();
2777
ndigits = strlen (digits);
2779
if (ndigits > precision)
2783
*p++ = digits[ndigits];
2785
while (ndigits > precision);
2788
/* Here ndigits <= precision. */
2789
if ((flags & FLAG_ALT) || precision > 0)
2791
*p++ = decimal_point_char ();
2792
for (; precision > ndigits; precision--)
2797
*p++ = digits[ndigits];
2803
else if (dp->conversion == 'e' || dp->conversion == 'E')
2811
if ((flags & FLAG_ALT) || precision > 0)
2813
*p++ = decimal_point_char ();
2814
for (; precision > 0; precision--)
2825
exponent = floorlog10l (arg);
2830
scale10_round_decimal_long_double (arg,
2831
(int)precision - exponent);
2834
END_LONG_DOUBLE_ROUNDING ();
2837
ndigits = strlen (digits);
2839
if (ndigits == precision + 1)
2841
if (ndigits < precision
2842
|| ndigits > precision + 2)
2843
/* The exponent was not guessed
2844
precisely enough. */
2847
/* None of two values of exponent is
2848
the right one. Prevent an endless
2852
if (ndigits == precision)
2859
/* Here ndigits = precision+1. */
2860
*p++ = digits[--ndigits];
2861
if ((flags & FLAG_ALT) || precision > 0)
2863
*p++ = decimal_point_char ();
2867
*p++ = digits[ndigits];
2874
*p++ = dp->conversion; /* 'e' or 'E' */
2875
# if WIDE_CHAR_VERSION
2877
static const wchar_t decimal_format[] =
2878
{ '%', '+', '.', '2', 'd', '\0' };
2879
SNPRINTF (p, 6 + 1, decimal_format, exponent);
2884
if (sizeof (DCHAR_T) == 1)
2886
sprintf ((char *) p, "%+.2d", exponent);
2894
sprintf (expbuf, "%+.2d", exponent);
2895
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2900
else if (dp->conversion == 'g' || dp->conversion == 'G')
2904
/* precision >= 1. */
2907
/* The exponent is 0, >= -4, < precision.
2908
Use fixed-point notation. */
2910
size_t ndigits = precision;
2911
/* Number of trailing zeroes that have to be
2914
(flags & FLAG_ALT ? 0 : precision - 1);
2918
if ((flags & FLAG_ALT) || ndigits > nzeroes)
2920
*p++ = decimal_point_char ();
2921
while (ndigits > nzeroes)
2937
exponent = floorlog10l (arg);
2942
scale10_round_decimal_long_double (arg,
2943
(int)(precision - 1) - exponent);
2946
END_LONG_DOUBLE_ROUNDING ();
2949
ndigits = strlen (digits);
2951
if (ndigits == precision)
2953
if (ndigits < precision - 1
2954
|| ndigits > precision + 1)
2955
/* The exponent was not guessed
2956
precisely enough. */
2959
/* None of two values of exponent is
2960
the right one. Prevent an endless
2964
if (ndigits < precision)
2970
/* Here ndigits = precision. */
2972
/* Determine the number of trailing zeroes
2973
that have to be dropped. */
2975
if ((flags & FLAG_ALT) == 0)
2976
while (nzeroes < ndigits
2977
&& digits[nzeroes] == '0')
2980
/* The exponent is now determined. */
2982
&& exponent < (long)precision)
2984
/* Fixed-point notation:
2985
max(exponent,0)+1 digits, then the
2986
decimal point, then the remaining
2987
digits without trailing zeroes. */
2990
size_t count = exponent + 1;
2991
/* Note: count <= precision = ndigits. */
2992
for (; count > 0; count--)
2993
*p++ = digits[--ndigits];
2994
if ((flags & FLAG_ALT) || ndigits > nzeroes)
2996
*p++ = decimal_point_char ();
2997
while (ndigits > nzeroes)
3000
*p++ = digits[ndigits];
3006
size_t count = -exponent - 1;
3008
*p++ = decimal_point_char ();
3009
for (; count > 0; count--)
3011
while (ndigits > nzeroes)
3014
*p++ = digits[ndigits];
3020
/* Exponential notation. */
3021
*p++ = digits[--ndigits];
3022
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3024
*p++ = decimal_point_char ();
3025
while (ndigits > nzeroes)
3028
*p++ = digits[ndigits];
3031
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3032
# if WIDE_CHAR_VERSION
3034
static const wchar_t decimal_format[] =
3035
{ '%', '+', '.', '2', 'd', '\0' };
3036
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3041
if (sizeof (DCHAR_T) == 1)
3043
sprintf ((char *) p, "%+.2d", exponent);
3051
sprintf (expbuf, "%+.2d", exponent);
3052
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3624
if (dp->conversion == 'f' || dp->conversion == 'F')
3630
scale10_round_decimal_long_double (arg, precision);
3633
END_LONG_DOUBLE_ROUNDING ();
3636
ndigits = strlen (digits);
3638
if (ndigits > precision)
3642
*p++ = digits[ndigits];
3644
while (ndigits > precision);
3647
/* Here ndigits <= precision. */
3648
if ((flags & FLAG_ALT) || precision > 0)
3650
*p++ = decimal_point_char ();
3651
for (; precision > ndigits; precision--)
3656
*p++ = digits[ndigits];
3662
else if (dp->conversion == 'e' || dp->conversion == 'E')
3670
if ((flags & FLAG_ALT) || precision > 0)
3672
*p++ = decimal_point_char ();
3673
for (; precision > 0; precision--)
3684
exponent = floorlog10l (arg);
3689
scale10_round_decimal_long_double (arg,
3690
(int)precision - exponent);
3693
END_LONG_DOUBLE_ROUNDING ();
3696
ndigits = strlen (digits);
3698
if (ndigits == precision + 1)
3700
if (ndigits < precision
3701
|| ndigits > precision + 2)
3702
/* The exponent was not guessed
3703
precisely enough. */
3706
/* None of two values of exponent is
3707
the right one. Prevent an endless
3711
if (ndigits == precision)
3717
/* Here ndigits = precision+1. */
3718
if (is_borderline (digits, precision))
3720
/* Maybe the exponent guess was too high
3721
and a smaller exponent can be reached
3722
by turning a 10...0 into 9...9x. */
3724
scale10_round_decimal_long_double (arg,
3725
(int)precision - exponent + 1);
3726
if (digits2 == NULL)
3729
END_LONG_DOUBLE_ROUNDING ();
3732
if (strlen (digits2) == precision + 1)
3741
/* Here ndigits = precision+1. */
3743
*p++ = digits[--ndigits];
3744
if ((flags & FLAG_ALT) || precision > 0)
3746
*p++ = decimal_point_char ();
3750
*p++ = digits[ndigits];
3757
*p++ = dp->conversion; /* 'e' or 'E' */
3758
# if WIDE_CHAR_VERSION
3760
static const wchar_t decimal_format[] =
3761
{ '%', '+', '.', '2', 'd', '\0' };
3762
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3767
if (sizeof (DCHAR_T) == 1)
3769
sprintf ((char *) p, "%+.2d", exponent);
3777
sprintf (expbuf, "%+.2d", exponent);
3778
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3783
else if (dp->conversion == 'g' || dp->conversion == 'G')
3787
/* precision >= 1. */
3790
/* The exponent is 0, >= -4, < precision.
3791
Use fixed-point notation. */
3793
size_t ndigits = precision;
3794
/* Number of trailing zeroes that have to be
3797
(flags & FLAG_ALT ? 0 : precision - 1);
3801
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3803
*p++ = decimal_point_char ();
3804
while (ndigits > nzeroes)
3820
exponent = floorlog10l (arg);
3825
scale10_round_decimal_long_double (arg,
3826
(int)(precision - 1) - exponent);
3829
END_LONG_DOUBLE_ROUNDING ();
3832
ndigits = strlen (digits);
3834
if (ndigits == precision)
3836
if (ndigits < precision - 1
3837
|| ndigits > precision + 1)
3838
/* The exponent was not guessed
3839
precisely enough. */
3842
/* None of two values of exponent is
3843
the right one. Prevent an endless
3847
if (ndigits < precision)
3853
/* Here ndigits = precision. */
3854
if (is_borderline (digits, precision - 1))
3856
/* Maybe the exponent guess was too high
3857
and a smaller exponent can be reached
3858
by turning a 10...0 into 9...9x. */
3860
scale10_round_decimal_long_double (arg,
3861
(int)(precision - 1) - exponent + 1);
3862
if (digits2 == NULL)
3865
END_LONG_DOUBLE_ROUNDING ();
3868
if (strlen (digits2) == precision)
3877
/* Here ndigits = precision. */
3879
/* Determine the number of trailing zeroes
3880
that have to be dropped. */
3882
if ((flags & FLAG_ALT) == 0)
3883
while (nzeroes < ndigits
3884
&& digits[nzeroes] == '0')
3887
/* The exponent is now determined. */
3889
&& exponent < (long)precision)
3891
/* Fixed-point notation:
3892
max(exponent,0)+1 digits, then the
3893
decimal point, then the remaining
3894
digits without trailing zeroes. */
3897
size_t count = exponent + 1;
3898
/* Note: count <= precision = ndigits. */
3899
for (; count > 0; count--)
3900
*p++ = digits[--ndigits];
3901
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3903
*p++ = decimal_point_char ();
3904
while (ndigits > nzeroes)
3907
*p++ = digits[ndigits];
3913
size_t count = -exponent - 1;
3915
*p++ = decimal_point_char ();
3916
for (; count > 0; count--)
3918
while (ndigits > nzeroes)
3921
*p++ = digits[ndigits];
3927
/* Exponential notation. */
3928
*p++ = digits[--ndigits];
3929
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3931
*p++ = decimal_point_char ();
3932
while (ndigits > nzeroes)
3935
*p++ = digits[ndigits];
3938
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3939
# if WIDE_CHAR_VERSION
3941
static const wchar_t decimal_format[] =
3942
{ '%', '+', '.', '2', 'd', '\0' };
3943
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3948
if (sizeof (DCHAR_T) == 1)
3950
sprintf ((char *) p, "%+.2d", exponent);
3958
sprintf (expbuf, "%+.2d", exponent);
3959
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3064
/* arg is finite. */
3971
/* arg is finite. */
3977
if (dp->conversion == 'f' || dp->conversion == 'F')
3980
if ((flags & FLAG_ALT) || precision > 0)
3982
*p++ = decimal_point_char ();
3983
for (; precision > 0; precision--)
3987
else if (dp->conversion == 'e' || dp->conversion == 'E')
3990
if ((flags & FLAG_ALT) || precision > 0)
3992
*p++ = decimal_point_char ();
3993
for (; precision > 0; precision--)
3996
*p++ = dp->conversion; /* 'e' or 'E' */
4001
else if (dp->conversion == 'g' || dp->conversion == 'G')
4004
if (flags & FLAG_ALT)
4007
(precision > 0 ? precision - 1 : 0);
4008
*p++ = decimal_point_char ();
4009
for (; ndigits > 0; --ndigits)
4013
else if (dp->conversion == 'a' || dp->conversion == 'A')
4016
*p++ = dp->conversion - 'A' + 'X';
4019
if ((flags & FLAG_ALT) || precision > 0)
4021
*p++ = decimal_point_char ();
4022
for (; precision > 0; precision--)
4025
*p++ = dp->conversion - 'A' + 'P';
3069
END_LONG_DOUBLE_ROUNDING ();
4034
END_LONG_DOUBLE_ROUNDING ();
3072
4037
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3076
4041
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3078
double arg = a.arg[dp->arg_index].a.a_double;
3082
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3084
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3088
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3095
if (signbit (arg)) /* arg < 0.0 or negative zero */
3103
else if (flags & FLAG_SHOWSIGN)
3105
else if (flags & FLAG_SPACE)
3108
if (arg > 0.0 && arg + arg == arg)
3110
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3112
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3116
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
4043
double arg = a.arg[dp->arg_index].a.a_double;
4047
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
4049
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
4053
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
4060
if (signbit (arg)) /* arg < 0.0 or negative zero */
4068
else if (flags & FLAG_SHOWSIGN)
4070
else if (flags & FLAG_SPACE)
4073
if (arg > 0.0 && arg + arg == arg)
4075
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
4077
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
4081
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3121
4086
# if NEED_PRINTF_DOUBLE
3124
if (dp->conversion == 'f' || dp->conversion == 'F')
3130
scale10_round_decimal_double (arg, precision);
3133
ndigits = strlen (digits);
3135
if (ndigits > precision)
3139
*p++ = digits[ndigits];
3141
while (ndigits > precision);
3144
/* Here ndigits <= precision. */
3145
if ((flags & FLAG_ALT) || precision > 0)
3147
*p++ = decimal_point_char ();
3148
for (; precision > ndigits; precision--)
3153
*p++ = digits[ndigits];
3159
else if (dp->conversion == 'e' || dp->conversion == 'E')
3167
if ((flags & FLAG_ALT) || precision > 0)
3169
*p++ = decimal_point_char ();
3170
for (; precision > 0; precision--)
3181
exponent = floorlog10 (arg);
3186
scale10_round_decimal_double (arg,
3187
(int)precision - exponent);
3190
ndigits = strlen (digits);
3192
if (ndigits == precision + 1)
3194
if (ndigits < precision
3195
|| ndigits > precision + 2)
3196
/* The exponent was not guessed
3197
precisely enough. */
3200
/* None of two values of exponent is
3201
the right one. Prevent an endless
3205
if (ndigits == precision)
3212
/* Here ndigits = precision+1. */
3213
*p++ = digits[--ndigits];
3214
if ((flags & FLAG_ALT) || precision > 0)
3216
*p++ = decimal_point_char ();
3220
*p++ = digits[ndigits];
3227
*p++ = dp->conversion; /* 'e' or 'E' */
3228
# if WIDE_CHAR_VERSION
3230
static const wchar_t decimal_format[] =
3231
/* Produce the same number of exponent digits
3232
as the native printf implementation. */
3233
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3234
{ '%', '+', '.', '3', 'd', '\0' };
3236
{ '%', '+', '.', '2', 'd', '\0' };
3238
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3244
static const char decimal_format[] =
3245
/* Produce the same number of exponent digits
3246
as the native printf implementation. */
3247
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3252
if (sizeof (DCHAR_T) == 1)
3254
sprintf ((char *) p, decimal_format, exponent);
3262
sprintf (expbuf, decimal_format, exponent);
3263
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3269
else if (dp->conversion == 'g' || dp->conversion == 'G')
3273
/* precision >= 1. */
3276
/* The exponent is 0, >= -4, < precision.
3277
Use fixed-point notation. */
3279
size_t ndigits = precision;
3280
/* Number of trailing zeroes that have to be
3283
(flags & FLAG_ALT ? 0 : precision - 1);
3287
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3289
*p++ = decimal_point_char ();
3290
while (ndigits > nzeroes)
3306
exponent = floorlog10 (arg);
3311
scale10_round_decimal_double (arg,
3312
(int)(precision - 1) - exponent);
3315
ndigits = strlen (digits);
3317
if (ndigits == precision)
3319
if (ndigits < precision - 1
3320
|| ndigits > precision + 1)
3321
/* The exponent was not guessed
3322
precisely enough. */
3325
/* None of two values of exponent is
3326
the right one. Prevent an endless
3330
if (ndigits < precision)
3336
/* Here ndigits = precision. */
3338
/* Determine the number of trailing zeroes
3339
that have to be dropped. */
3341
if ((flags & FLAG_ALT) == 0)
3342
while (nzeroes < ndigits
3343
&& digits[nzeroes] == '0')
3346
/* The exponent is now determined. */
3348
&& exponent < (long)precision)
3350
/* Fixed-point notation:
3351
max(exponent,0)+1 digits, then the
3352
decimal point, then the remaining
3353
digits without trailing zeroes. */
3356
size_t count = exponent + 1;
3357
/* Note: count <= precision = ndigits. */
3358
for (; count > 0; count--)
3359
*p++ = digits[--ndigits];
3360
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3362
*p++ = decimal_point_char ();
3363
while (ndigits > nzeroes)
3366
*p++ = digits[ndigits];
3372
size_t count = -exponent - 1;
3374
*p++ = decimal_point_char ();
3375
for (; count > 0; count--)
3377
while (ndigits > nzeroes)
3380
*p++ = digits[ndigits];
3386
/* Exponential notation. */
3387
*p++ = digits[--ndigits];
3388
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3390
*p++ = decimal_point_char ();
3391
while (ndigits > nzeroes)
3394
*p++ = digits[ndigits];
3397
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3398
# if WIDE_CHAR_VERSION
3400
static const wchar_t decimal_format[] =
3401
/* Produce the same number of exponent digits
3402
as the native printf implementation. */
3403
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3404
{ '%', '+', '.', '3', 'd', '\0' };
3406
{ '%', '+', '.', '2', 'd', '\0' };
3408
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3414
static const char decimal_format[] =
3415
/* Produce the same number of exponent digits
3416
as the native printf implementation. */
3417
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3422
if (sizeof (DCHAR_T) == 1)
3424
sprintf ((char *) p, decimal_format, exponent);
3432
sprintf (expbuf, decimal_format, exponent);
3433
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
4089
if (dp->conversion == 'f' || dp->conversion == 'F')
4095
scale10_round_decimal_double (arg, precision);
4098
ndigits = strlen (digits);
4100
if (ndigits > precision)
4104
*p++ = digits[ndigits];
4106
while (ndigits > precision);
4109
/* Here ndigits <= precision. */
4110
if ((flags & FLAG_ALT) || precision > 0)
4112
*p++ = decimal_point_char ();
4113
for (; precision > ndigits; precision--)
4118
*p++ = digits[ndigits];
4124
else if (dp->conversion == 'e' || dp->conversion == 'E')
4132
if ((flags & FLAG_ALT) || precision > 0)
4134
*p++ = decimal_point_char ();
4135
for (; precision > 0; precision--)
4146
exponent = floorlog10 (arg);
4151
scale10_round_decimal_double (arg,
4152
(int)precision - exponent);
4155
ndigits = strlen (digits);
4157
if (ndigits == precision + 1)
4159
if (ndigits < precision
4160
|| ndigits > precision + 2)
4161
/* The exponent was not guessed
4162
precisely enough. */
4165
/* None of two values of exponent is
4166
the right one. Prevent an endless
4170
if (ndigits == precision)
4176
/* Here ndigits = precision+1. */
4177
if (is_borderline (digits, precision))
4179
/* Maybe the exponent guess was too high
4180
and a smaller exponent can be reached
4181
by turning a 10...0 into 9...9x. */
4183
scale10_round_decimal_double (arg,
4184
(int)precision - exponent + 1);
4185
if (digits2 == NULL)
4190
if (strlen (digits2) == precision + 1)
4199
/* Here ndigits = precision+1. */
4201
*p++ = digits[--ndigits];
4202
if ((flags & FLAG_ALT) || precision > 0)
4204
*p++ = decimal_point_char ();
4208
*p++ = digits[ndigits];
4215
*p++ = dp->conversion; /* 'e' or 'E' */
4216
# if WIDE_CHAR_VERSION
4218
static const wchar_t decimal_format[] =
4219
/* Produce the same number of exponent digits
4220
as the native printf implementation. */
4221
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4222
{ '%', '+', '.', '3', 'd', '\0' };
4224
{ '%', '+', '.', '2', 'd', '\0' };
4226
SNPRINTF (p, 6 + 1, decimal_format, exponent);
4232
static const char decimal_format[] =
4233
/* Produce the same number of exponent digits
4234
as the native printf implementation. */
4235
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4240
if (sizeof (DCHAR_T) == 1)
4242
sprintf ((char *) p, decimal_format, exponent);
4250
sprintf (expbuf, decimal_format, exponent);
4251
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
4257
else if (dp->conversion == 'g' || dp->conversion == 'G')
4261
/* precision >= 1. */
4264
/* The exponent is 0, >= -4, < precision.
4265
Use fixed-point notation. */
4267
size_t ndigits = precision;
4268
/* Number of trailing zeroes that have to be
4271
(flags & FLAG_ALT ? 0 : precision - 1);
4275
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4277
*p++ = decimal_point_char ();
4278
while (ndigits > nzeroes)
4294
exponent = floorlog10 (arg);
4299
scale10_round_decimal_double (arg,
4300
(int)(precision - 1) - exponent);
4303
ndigits = strlen (digits);
4305
if (ndigits == precision)
4307
if (ndigits < precision - 1
4308
|| ndigits > precision + 1)
4309
/* The exponent was not guessed
4310
precisely enough. */
4313
/* None of two values of exponent is
4314
the right one. Prevent an endless
4318
if (ndigits < precision)
4324
/* Here ndigits = precision. */
4325
if (is_borderline (digits, precision - 1))
4327
/* Maybe the exponent guess was too high
4328
and a smaller exponent can be reached
4329
by turning a 10...0 into 9...9x. */
4331
scale10_round_decimal_double (arg,
4332
(int)(precision - 1) - exponent + 1);
4333
if (digits2 == NULL)
4338
if (strlen (digits2) == precision)
4347
/* Here ndigits = precision. */
4349
/* Determine the number of trailing zeroes
4350
that have to be dropped. */
4352
if ((flags & FLAG_ALT) == 0)
4353
while (nzeroes < ndigits
4354
&& digits[nzeroes] == '0')
4357
/* The exponent is now determined. */
4359
&& exponent < (long)precision)
4361
/* Fixed-point notation:
4362
max(exponent,0)+1 digits, then the
4363
decimal point, then the remaining
4364
digits without trailing zeroes. */
4367
size_t count = exponent + 1;
4368
/* Note: count <= precision = ndigits. */
4369
for (; count > 0; count--)
4370
*p++ = digits[--ndigits];
4371
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4373
*p++ = decimal_point_char ();
4374
while (ndigits > nzeroes)
4377
*p++ = digits[ndigits];
4383
size_t count = -exponent - 1;
4385
*p++ = decimal_point_char ();
4386
for (; count > 0; count--)
4388
while (ndigits > nzeroes)
4391
*p++ = digits[ndigits];
4397
/* Exponential notation. */
4398
*p++ = digits[--ndigits];
4399
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4401
*p++ = decimal_point_char ();
4402
while (ndigits > nzeroes)
4405
*p++ = digits[ndigits];
4408
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
4409
# if WIDE_CHAR_VERSION
4411
static const wchar_t decimal_format[] =
4412
/* Produce the same number of exponent digits
4413
as the native printf implementation. */
4414
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4415
{ '%', '+', '.', '3', 'd', '\0' };
4417
{ '%', '+', '.', '2', 'd', '\0' };
4419
SNPRINTF (p, 6 + 1, decimal_format, exponent);
4425
static const char decimal_format[] =
4426
/* Produce the same number of exponent digits
4427
as the native printf implementation. */
4428
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4433
if (sizeof (DCHAR_T) == 1)
4435
sprintf ((char *) p, decimal_format, exponent);
4443
sprintf (expbuf, decimal_format, exponent);
4444
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3446
/* arg is finite. */
3452
if (dp->conversion == 'f' || dp->conversion == 'F')
3455
if ((flags & FLAG_ALT) || precision > 0)
3457
*p++ = decimal_point_char ();
3458
for (; precision > 0; precision--)
3462
else if (dp->conversion == 'e' || dp->conversion == 'E')
3465
if ((flags & FLAG_ALT) || precision > 0)
3467
*p++ = decimal_point_char ();
3468
for (; precision > 0; precision--)
3471
*p++ = dp->conversion; /* 'e' or 'E' */
3473
/* Produce the same number of exponent digits as
3474
the native printf implementation. */
4457
/* arg is finite. */
4463
if (dp->conversion == 'f' || dp->conversion == 'F')
4466
if ((flags & FLAG_ALT) || precision > 0)
4468
*p++ = decimal_point_char ();
4469
for (; precision > 0; precision--)
4473
else if (dp->conversion == 'e' || dp->conversion == 'E')
4476
if ((flags & FLAG_ALT) || precision > 0)
4478
*p++ = decimal_point_char ();
4479
for (; precision > 0; precision--)
4482
*p++ = dp->conversion; /* 'e' or 'E' */
4484
/* Produce the same number of exponent digits as
4485
the native printf implementation. */
3475
4486
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3481
else if (dp->conversion == 'g' || dp->conversion == 'G')
3484
if (flags & FLAG_ALT)
3487
(precision > 0 ? precision - 1 : 0);
3488
*p++ = decimal_point_char ();
3489
for (; ndigits > 0; --ndigits)
3501
/* The generated string now extends from tmp to p, with the
3502
zero padding insertion point being at pad_ptr. */
3503
if (has_width && p - tmp < width)
3505
size_t pad = width - (p - tmp);
3506
DCHAR_T *end = p + pad;
3508
if (flags & FLAG_LEFT)
3510
/* Pad with spaces on the right. */
3511
for (; pad > 0; pad--)
3514
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3516
/* Pad with zeroes. */
3521
for (; pad > 0; pad--)
3526
/* Pad with spaces on the left. */
3531
for (; pad > 0; pad--)
3539
size_t count = p - tmp;
3541
if (count >= tmp_length)
3542
/* tmp_length was incorrectly calculated - fix the
3546
/* Make room for the result. */
3547
if (count >= allocated - length)
3549
size_t n = xsum (length, count);
3551
ENSURE_ALLOCATION (n);
3554
/* Append the result. */
3555
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3564
arg_type type = a.arg[dp->arg_index].type;
3565
int flags = dp->flags;
3566
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3570
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3574
#if NEED_PRINTF_UNBOUNDED_PRECISION
3577
# define prec_ourselves 0
3579
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3582
# define pad_ourselves 0
3585
unsigned int prefix_count;
3589
TCHAR_T tmpbuf[700];
3593
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3596
if (dp->width_start != dp->width_end)
3598
if (dp->width_arg_index != ARG_NONE)
3602
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3604
arg = a.arg[dp->width_arg_index].a.a_int;
3607
/* "A negative field width is taken as a '-' flag
3608
followed by a positive field width." */
3610
width = (unsigned int) (-arg);
3617
const FCHAR_T *digitp = dp->width_start;
3620
width = xsum (xtimes (width, 10), *digitp++ - '0');
3621
while (digitp != dp->width_end);
3627
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3630
if (dp->precision_start != dp->precision_end)
3632
if (dp->precision_arg_index != ARG_NONE)
3636
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3638
arg = a.arg[dp->precision_arg_index].a.a_int;
3639
/* "A negative precision is taken as if the precision
3649
const FCHAR_T *digitp = dp->precision_start + 1;
3652
while (digitp != dp->precision_end)
3653
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3660
/* Allocate a temporary buffer of sufficient size for calling
3663
switch (dp->conversion)
3666
case 'd': case 'i': case 'u':
3667
# if HAVE_LONG_LONG_INT
3668
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3670
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3671
* 0.30103 /* binary -> decimal */
3673
+ 1; /* turn floor into ceil */
3676
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3678
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
3679
* 0.30103 /* binary -> decimal */
3681
+ 1; /* turn floor into ceil */
3684
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
3685
* 0.30103 /* binary -> decimal */
3687
+ 1; /* turn floor into ceil */
3688
if (tmp_length < precision)
3689
tmp_length = precision;
3690
/* Multiply by 2, as an estimate for FLAG_GROUP. */
3691
tmp_length = xsum (tmp_length, tmp_length);
3692
/* Add 1, to account for a leading sign. */
3693
tmp_length = xsum (tmp_length, 1);
3697
# if HAVE_LONG_LONG_INT
3698
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3700
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3701
* 0.333334 /* binary -> octal */
3703
+ 1; /* turn floor into ceil */
3706
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3708
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
3709
* 0.333334 /* binary -> octal */
3711
+ 1; /* turn floor into ceil */
3714
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
3715
* 0.333334 /* binary -> octal */
3717
+ 1; /* turn floor into ceil */
3718
if (tmp_length < precision)
3719
tmp_length = precision;
3720
/* Add 1, to account for a leading sign. */
3721
tmp_length = xsum (tmp_length, 1);
3725
# if HAVE_LONG_LONG_INT
3726
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
3728
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
3729
* 0.25 /* binary -> hexadecimal */
3731
+ 1; /* turn floor into ceil */
3734
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3736
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
3737
* 0.25 /* binary -> hexadecimal */
3739
+ 1; /* turn floor into ceil */
3742
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
3743
* 0.25 /* binary -> hexadecimal */
3745
+ 1; /* turn floor into ceil */
3746
if (tmp_length < precision)
3747
tmp_length = precision;
3748
/* Add 2, to account for a leading sign or alternate form. */
3749
tmp_length = xsum (tmp_length, 2);
3753
if (type == TYPE_LONGDOUBLE)
3755
(unsigned int) (LDBL_MAX_EXP
3756
* 0.30103 /* binary -> decimal */
3757
* 2 /* estimate for FLAG_GROUP */
3759
+ 1 /* turn floor into ceil */
3760
+ 10; /* sign, decimal point etc. */
3763
(unsigned int) (DBL_MAX_EXP
3764
* 0.30103 /* binary -> decimal */
3765
* 2 /* estimate for FLAG_GROUP */
3767
+ 1 /* turn floor into ceil */
3768
+ 10; /* sign, decimal point etc. */
3769
tmp_length = xsum (tmp_length, precision);
3772
case 'e': case 'E': case 'g': case 'G':
3774
12; /* sign, decimal point, exponent etc. */
3775
tmp_length = xsum (tmp_length, precision);
3779
if (type == TYPE_LONGDOUBLE)
3781
(unsigned int) (LDBL_DIG
3782
* 0.831 /* decimal -> hexadecimal */
3784
+ 1; /* turn floor into ceil */
3787
(unsigned int) (DBL_DIG
3788
* 0.831 /* decimal -> hexadecimal */
3790
+ 1; /* turn floor into ceil */
3791
if (tmp_length < precision)
3792
tmp_length = precision;
3793
/* Account for sign, decimal point etc. */
3794
tmp_length = xsum (tmp_length, 12);
3798
# if HAVE_WINT_T && !WIDE_CHAR_VERSION
3799
if (type == TYPE_WIDE_CHAR)
3800
tmp_length = MB_CUR_MAX;
3808
if (type == TYPE_WIDE_STRING)
3811
local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
3813
# if !WIDE_CHAR_VERSION
3814
tmp_length = xtimes (tmp_length, MB_CUR_MAX);
3819
tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
3824
(unsigned int) (sizeof (void *) * CHAR_BIT
3825
* 0.25 /* binary -> hexadecimal */
3827
+ 1 /* turn floor into ceil */
3828
+ 2; /* account for leading 0x */
3835
# if ENABLE_UNISTDIO
3836
/* Padding considers the number of characters, therefore the
3837
number of elements after padding may be
3838
> max (tmp_length, width)
3840
<= tmp_length + width. */
3841
tmp_length = xsum (tmp_length, width);
3843
/* Padding considers the number of elements, says POSIX. */
3844
if (tmp_length < width)
3848
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3851
if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
3855
size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
3857
if (size_overflow_p (tmp_memsize))
3858
/* Overflow, would lead to out of memory. */
3860
tmp = (TCHAR_T *) malloc (tmp_memsize);
3862
/* Out of memory. */
3867
/* Decide whether to handle the precision ourselves. */
3868
#if NEED_PRINTF_UNBOUNDED_PRECISION
3869
switch (dp->conversion)
3871
case 'd': case 'i': case 'u':
3873
case 'x': case 'X': case 'p':
3874
prec_ourselves = has_precision && (precision > 0);
3882
/* Decide whether to perform the padding ourselves. */
3883
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3884
switch (dp->conversion)
4492
else if (dp->conversion == 'g' || dp->conversion == 'G')
4495
if (flags & FLAG_ALT)
4498
(precision > 0 ? precision - 1 : 0);
4499
*p++ = decimal_point_char ();
4500
for (; ndigits > 0; --ndigits)
4512
/* The generated string now extends from tmp to p, with the
4513
zero padding insertion point being at pad_ptr. */
4514
if (has_width && p - tmp < width)
4516
size_t pad = width - (p - tmp);
4517
DCHAR_T *end = p + pad;
4519
if (flags & FLAG_LEFT)
4521
/* Pad with spaces on the right. */
4522
for (; pad > 0; pad--)
4525
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4527
/* Pad with zeroes. */
4532
for (; pad > 0; pad--)
4537
/* Pad with spaces on the left. */
4542
for (; pad > 0; pad--)
4550
size_t count = p - tmp;
4552
if (count >= tmp_length)
4553
/* tmp_length was incorrectly calculated - fix the
4557
/* Make room for the result. */
4558
if (count >= allocated - length)
4560
size_t n = xsum (length, count);
4562
ENSURE_ALLOCATION (n);
4565
/* Append the result. */
4566
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4575
arg_type type = a.arg[dp->arg_index].type;
4576
int flags = dp->flags;
4577
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4581
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4585
#if NEED_PRINTF_UNBOUNDED_PRECISION
4588
# define prec_ourselves 0
4590
#if NEED_PRINTF_FLAG_LEFTADJUST
4591
# define pad_ourselves 1
4592
#elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4595
# define pad_ourselves 0
4598
unsigned int prefix_count;
4599
int prefixes[2] IF_LINT (= { 0 });
4602
TCHAR_T tmpbuf[700];
4606
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4609
if (dp->width_start != dp->width_end)
4611
if (dp->width_arg_index != ARG_NONE)
4615
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4617
arg = a.arg[dp->width_arg_index].a.a_int;
4620
/* "A negative field width is taken as a '-' flag
4621
followed by a positive field width." */
4623
width = (unsigned int) (-arg);
4630
const FCHAR_T *digitp = dp->width_start;
4633
width = xsum (xtimes (width, 10), *digitp++ - '0');
4634
while (digitp != dp->width_end);
4640
#if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || NEED_PRINTF_UNBOUNDED_PRECISION
4643
if (dp->precision_start != dp->precision_end)
4645
if (dp->precision_arg_index != ARG_NONE)
4649
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4651
arg = a.arg[dp->precision_arg_index].a.a_int;
4652
/* "A negative precision is taken as if the precision
4662
const FCHAR_T *digitp = dp->precision_start + 1;
4665
while (digitp != dp->precision_end)
4666
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
4672
/* Decide whether to handle the precision ourselves. */
4673
#if NEED_PRINTF_UNBOUNDED_PRECISION
4674
switch (dp->conversion)
4676
case 'd': case 'i': case 'u':
4678
case 'x': case 'X': case 'p':
4679
prec_ourselves = has_precision && (precision > 0);
4687
/* Decide whether to perform the padding ourselves. */
4688
#if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4689
switch (dp->conversion)
3886
4691
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
3887
/* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
3888
to perform the padding after this conversion. Functions
3889
with unistdio extensions perform the padding based on
3890
character count rather than element count. */
4692
/* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4693
to perform the padding after this conversion. Functions
4694
with unistdio extensions perform the padding based on
4695
character count rather than element count. */
3893
4698
# if NEED_PRINTF_FLAG_ZERO
3894
case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
4699
case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
3900
pad_ourselves = prec_ourselves;
3905
/* Construct the format string for calling snprintf or
4705
pad_ourselves = prec_ourselves;
4711
/* Allocate a temporary buffer of sufficient size for calling
4714
MAX_ROOM_NEEDED (&a, dp->arg_index, dp->conversion, type,
4715
flags, width, has_precision, precision,
4718
if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
4722
size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
4724
if (size_overflow_p (tmp_memsize))
4725
/* Overflow, would lead to out of memory. */
4727
tmp = (TCHAR_T *) malloc (tmp_memsize);
4729
/* Out of memory. */
4734
/* Construct the format string for calling snprintf or
3909
4738
#if NEED_PRINTF_FLAG_GROUPING
3910
/* The underlying implementation doesn't support the ' flag.
3911
Produce no grouping characters in this case; this is
3912
acceptable because the grouping is locale dependent. */
4739
/* The underlying implementation doesn't support the ' flag.
4740
Produce no grouping characters in this case; this is
4741
acceptable because the grouping is locale dependent. */
3914
if (flags & FLAG_GROUP)
4743
if (flags & FLAG_GROUP)
3917
if (flags & FLAG_LEFT)
3919
if (flags & FLAG_SHOWSIGN)
3921
if (flags & FLAG_SPACE)
3923
if (flags & FLAG_ALT)
3927
if (flags & FLAG_ZERO)
3929
if (dp->width_start != dp->width_end)
3931
size_t n = dp->width_end - dp->width_start;
3932
/* The width specification is known to consist only
3933
of standard ASCII characters. */
3934
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
3936
memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
3941
const FCHAR_T *mp = dp->width_start;
3943
*fbp++ = (unsigned char) *mp++;
3948
if (!prec_ourselves)
3950
if (dp->precision_start != dp->precision_end)
3952
size_t n = dp->precision_end - dp->precision_start;
3953
/* The precision specification is known to consist only
3954
of standard ASCII characters. */
3955
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
3957
memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
3962
const FCHAR_T *mp = dp->precision_start;
3964
*fbp++ = (unsigned char) *mp++;
4746
if (flags & FLAG_LEFT)
4748
if (flags & FLAG_SHOWSIGN)
4750
if (flags & FLAG_SPACE)
4752
if (flags & FLAG_ALT)
4756
if (flags & FLAG_ZERO)
4758
if (dp->width_start != dp->width_end)
4760
size_t n = dp->width_end - dp->width_start;
4761
/* The width specification is known to consist only
4762
of standard ASCII characters. */
4763
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4765
memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
4770
const FCHAR_T *mp = dp->width_start;
4772
*fbp++ = (unsigned char) *mp++;
4777
if (!prec_ourselves)
4779
if (dp->precision_start != dp->precision_end)
4781
size_t n = dp->precision_end - dp->precision_start;
4782
/* The precision specification is known to consist only
4783
of standard ASCII characters. */
4784
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4786
memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
4791
const FCHAR_T *mp = dp->precision_start;
4793
*fbp++ = (unsigned char) *mp++;
3972
4801
#if HAVE_LONG_LONG_INT
3973
case TYPE_LONGLONGINT:
3974
case TYPE_ULONGLONGINT:
4802
case TYPE_LONGLONGINT:
4803
case TYPE_ULONGLONGINT:
3975
4804
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3987
4816
#if HAVE_WINT_T
3988
case TYPE_WIDE_CHAR:
4817
case TYPE_WIDE_CHAR:
3990
4819
#if HAVE_WCHAR_T
3991
case TYPE_WIDE_STRING:
4820
case TYPE_WIDE_STRING:
3995
case TYPE_LONGDOUBLE:
4824
case TYPE_LONGDOUBLE:
4001
4830
#if NEED_PRINTF_DIRECTIVE_F
4002
if (dp->conversion == 'F')
4831
if (dp->conversion == 'F')
4006
*fbp = dp->conversion;
4835
*fbp = dp->conversion;
4007
4836
#if USE_SNPRINTF
4008
# if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
4837
# if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4013
/* On glibc2 systems from glibc >= 2.3 - probably also older
4014
ones - we know that snprintf's returns value conforms to
4015
ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4016
Therefore we can avoid using %n in this situation.
4017
On glibc2 systems from 2004-10-18 or newer, the use of %n
4018
in format strings in writable memory may crash the program
4019
(if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4020
in this situation. */
4842
/* On glibc2 systems from glibc >= 2.3 - probably also older
4843
ones - we know that snprintf's returns value conforms to
4844
ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4845
Therefore we can avoid using %n in this situation.
4846
On glibc2 systems from 2004-10-18 or newer, the use of %n
4847
in format strings in writable memory may crash the program
4848
(if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4849
in this situation. */
4850
/* On native Win32 systems (such as mingw), we can avoid using
4852
- Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4853
snprintf does not write more than the specified number
4854
of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4855
'4', '5', '6' into buf, not '4', '5', '\0'.)
4856
- Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4857
allows us to recognize the case of an insufficient
4858
buffer size: it returns -1 in this case.
4859
On native Win32 systems (such as mingw) where the OS is
4860
Windows Vista, the use of %n in format strings by default
4861
crashes the program. See
4862
<http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4863
<http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4864
So we should avoid %n in this situation. */
4027
/* Construct the arguments for calling snprintf or sprintf. */
4029
if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4031
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4033
prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
4035
if (dp->precision_arg_index != ARG_NONE)
4037
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4039
prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
4871
/* Construct the arguments for calling snprintf or sprintf. */
4873
if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4875
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4877
prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
4879
if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
4881
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4883
prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
4042
4886
#if USE_SNPRINTF
4043
/* The SNPRINTF result is appended after result[0..length].
4044
The latter is an array of DCHAR_T; SNPRINTF appends an
4045
array of TCHAR_T to it. This is possible because
4046
sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4047
alignof (TCHAR_T) <= alignof (DCHAR_T). */
4887
/* The SNPRINTF result is appended after result[0..length].
4888
The latter is an array of DCHAR_T; SNPRINTF appends an
4889
array of TCHAR_T to it. This is possible because
4890
sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4891
alignof (TCHAR_T) <= alignof (DCHAR_T). */
4048
4892
# define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4049
/* Prepare checking whether snprintf returns the count
4051
ENSURE_ALLOCATION (xsum (length, 1));
4052
*(TCHAR_T *) (result + length) = '\0';
4893
/* Ensure that maxlen below will be >= 2. Needed on BeOS,
4894
where an snprintf() with maxlen==1 acts like sprintf(). */
4895
ENSURE_ALLOCATION (xsum (length,
4896
(2 + TCHARS_PER_DCHAR - 1)
4897
/ TCHARS_PER_DCHAR));
4898
/* Prepare checking whether snprintf returns the count
4900
*(TCHAR_T *) (result + length) = '\0';
4059
4907
#if USE_SNPRINTF
4061
size_t maxlen = allocated - length;
4062
/* SNPRINTF can fail if its second argument is
4064
if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4065
maxlen = INT_MAX / TCHARS_PER_DCHAR;
4066
maxlen = maxlen * TCHARS_PER_DCHAR;
4909
size_t maxlen = allocated - length;
4910
/* SNPRINTF can fail if its second argument is
4912
if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4913
maxlen = INT_MAX / TCHARS_PER_DCHAR;
4914
maxlen = maxlen * TCHARS_PER_DCHAR;
4067
4915
# define SNPRINTF_BUF(arg) \
4068
switch (prefix_count) \
4071
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4076
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4078
prefixes[0], arg, &count); \
4081
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4083
prefixes[0], prefixes[1], arg, \
4916
switch (prefix_count) \
4919
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4924
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4926
prefixes[0], arg, &count); \
4929
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4931
prefixes[0], prefixes[1], arg, \
4090
4938
# define SNPRINTF_BUF(arg) \
4091
switch (prefix_count) \
4094
count = sprintf (tmp, buf, arg); \
4097
count = sprintf (tmp, buf, prefixes[0], arg); \
4100
count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4939
switch (prefix_count) \
4942
count = sprintf (tmp, buf, arg); \
4945
count = sprintf (tmp, buf, prefixes[0], arg); \
4948
count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4112
int arg = a.arg[dp->arg_index].a.a_schar;
4118
unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4124
int arg = a.arg[dp->arg_index].a.a_short;
4130
unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4136
int arg = a.arg[dp->arg_index].a.a_int;
4142
unsigned int arg = a.arg[dp->arg_index].a.a_uint;
4148
long int arg = a.arg[dp->arg_index].a.a_longint;
4154
unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
4961
int arg = a.arg[dp->arg_index].a.a_schar;
4967
unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4973
int arg = a.arg[dp->arg_index].a.a_short;
4979
unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4985
int arg = a.arg[dp->arg_index].a.a_int;
4991
unsigned int arg = a.arg[dp->arg_index].a.a_uint;
4997
long int arg = a.arg[dp->arg_index].a.a_longint;
5003
unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
4158
5007
#if HAVE_LONG_LONG_INT
4159
case TYPE_LONGLONGINT:
4161
long long int arg = a.arg[dp->arg_index].a.a_longlongint;
4165
case TYPE_ULONGLONGINT:
4167
unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
5008
case TYPE_LONGLONGINT:
5010
long long int arg = a.arg[dp->arg_index].a.a_longlongint;
5014
case TYPE_ULONGLONGINT:
5016
unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
4174
double arg = a.arg[dp->arg_index].a.a_double;
4178
case TYPE_LONGDOUBLE:
4180
long double arg = a.arg[dp->arg_index].a.a_longdouble;
4186
int arg = a.arg[dp->arg_index].a.a_char;
5023
double arg = a.arg[dp->arg_index].a.a_double;
5027
case TYPE_LONGDOUBLE:
5029
long double arg = a.arg[dp->arg_index].a.a_longdouble;
5035
int arg = a.arg[dp->arg_index].a.a_char;
4190
5039
#if HAVE_WINT_T
4191
case TYPE_WIDE_CHAR:
4193
wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
5040
case TYPE_WIDE_CHAR:
5042
wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
4200
const char *arg = a.arg[dp->arg_index].a.a_string;
5049
const char *arg = a.arg[dp->arg_index].a.a_string;
4204
5053
#if HAVE_WCHAR_T
4205
case TYPE_WIDE_STRING:
4207
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
4214
void *arg = a.arg[dp->arg_index].a.a_pointer;
4223
/* Portability: Not all implementations of snprintf()
4224
are ISO C 99 compliant. Determine the number of
4225
bytes that snprintf() has produced or would have
4229
/* Verify that snprintf() has NUL-terminated its
4232
&& ((TCHAR_T *) (result + length)) [count] != '\0')
4234
/* Portability hack. */
4235
if (retcount > count)
4240
/* snprintf() doesn't understand the '%n'
4244
/* Don't use the '%n' directive; instead, look
4245
at the snprintf() return value. */
4251
/* Look at the snprintf() return value. */
4254
/* HP-UX 10.20 snprintf() is doubly deficient:
4255
It doesn't understand the '%n' directive,
4256
*and* it returns -1 (rather than the length
4257
that would have been required) when the
4258
buffer is too small. */
4259
size_t bigger_need =
4260
xsum (xtimes (allocated, 2), 12);
4261
ENSURE_ALLOCATION (bigger_need);
4270
/* Attempt to handle failure. */
4273
if (!(result == resultbuf || result == NULL))
4275
if (buf_malloced != NULL)
4276
free (buf_malloced);
4283
/* Handle overflow of the allocated buffer.
4284
If such an overflow occurs, a C99 compliant snprintf()
4285
returns a count >= maxlen. However, a non-compliant
4286
snprintf() function returns only count = maxlen - 1. To
4287
cover both cases, test whether count >= maxlen - 1. */
4288
if ((unsigned int) count + 1 >= maxlen)
4290
/* If maxlen already has attained its allowed maximum,
4291
allocating more memory will not increase maxlen.
4292
Instead of looping, bail out. */
4293
if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
4297
/* Need at least count * sizeof (TCHAR_T) bytes.
4298
But allocate proportionally, to avoid looping
4299
eternally if snprintf() reports a too small
4303
(count + TCHARS_PER_DCHAR - 1)
4304
/ TCHARS_PER_DCHAR),
4305
xtimes (allocated, 2));
4307
ENSURE_ALLOCATION (n);
5054
case TYPE_WIDE_STRING:
5056
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
5063
void *arg = a.arg[dp->arg_index].a.a_pointer;
5072
/* Portability: Not all implementations of snprintf()
5073
are ISO C 99 compliant. Determine the number of
5074
bytes that snprintf() has produced or would have
5078
/* Verify that snprintf() has NUL-terminated its
5081
&& ((TCHAR_T *) (result + length)) [count] != '\0')
5083
/* Portability hack. */
5084
if (retcount > count)
5089
/* snprintf() doesn't understand the '%n'
5093
/* Don't use the '%n' directive; instead, look
5094
at the snprintf() return value. */
5100
/* Look at the snprintf() return value. */
5103
# if !HAVE_SNPRINTF_RETVAL_C99
5104
/* HP-UX 10.20 snprintf() is doubly deficient:
5105
It doesn't understand the '%n' directive,
5106
*and* it returns -1 (rather than the length
5107
that would have been required) when the
5108
buffer is too small.
5109
But a failure at this point can also come
5110
from other reasons than a too small buffer,
5111
such as an invalid wide string argument to
5112
the %ls directive, or possibly an invalid
5113
floating-point argument. */
5115
MAX_ROOM_NEEDED (&a, dp->arg_index,
5116
dp->conversion, type, flags,
5117
width, has_precision,
5118
precision, pad_ourselves);
5120
if (maxlen < tmp_length)
5122
/* Make more room. But try to do through
5123
this reallocation only once. */
5124
size_t bigger_need =
5127
TCHARS_PER_DCHAR - 1)
5128
/ TCHARS_PER_DCHAR);
5129
/* And always grow proportionally.
5130
(There may be several arguments, each
5131
needing a little more room than the
5133
size_t bigger_need2 =
5134
xsum (xtimes (allocated, 2), 12);
5135
if (bigger_need < bigger_need2)
5136
bigger_need = bigger_need2;
5137
ENSURE_ALLOCATION (bigger_need);
5148
/* Attempt to handle failure. */
5151
/* SNPRINTF or sprintf failed. Save and use the errno
5152
that it has set, if any. */
5153
int saved_errno = errno;
5155
if (!(result == resultbuf || result == NULL))
5157
if (buf_malloced != NULL)
5158
free (buf_malloced);
5163
: (dp->conversion == 'c' || dp->conversion == 's'
5170
/* Handle overflow of the allocated buffer.
5171
If such an overflow occurs, a C99 compliant snprintf()
5172
returns a count >= maxlen. However, a non-compliant
5173
snprintf() function returns only count = maxlen - 1. To
5174
cover both cases, test whether count >= maxlen - 1. */
5175
if ((unsigned int) count + 1 >= maxlen)
5177
/* If maxlen already has attained its allowed maximum,
5178
allocating more memory will not increase maxlen.
5179
Instead of looping, bail out. */
5180
if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
5184
/* Need at least (count + 1) * sizeof (TCHAR_T)
5185
bytes. (The +1 is for the trailing NUL.)
5186
But ask for (count + 2) * sizeof (TCHAR_T)
5187
bytes, so that in the next round, we likely get
5188
maxlen > (unsigned int) count + 1
5189
and so we don't get here again.
5190
And allocate proportionally, to avoid looping
5191
eternally if snprintf() reports a too small
5195
((unsigned int) count + 2
5196
+ TCHARS_PER_DCHAR - 1)
5197
/ TCHARS_PER_DCHAR),
5198
xtimes (allocated, 2));
5200
ENSURE_ALLOCATION (n);
4313
5206
#if NEED_PRINTF_UNBOUNDED_PRECISION
4316
/* Handle the precision. */
5209
/* Handle the precision. */
4318
5211
# if USE_SNPRINTF
4319
(TCHAR_T *) (result + length);
5212
(TCHAR_T *) (result + length);
4323
size_t prefix_count;
4327
/* Put the additional zeroes after the sign. */
4329
&& (*prec_ptr == '-' || *prec_ptr == '+'
4330
|| *prec_ptr == ' '))
4332
/* Put the additional zeroes after the 0x prefix if
4333
(flags & FLAG_ALT) || (dp->conversion == 'p'). */
4335
&& prec_ptr[0] == '0'
4336
&& (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
4339
move = count - prefix_count;
4340
if (precision > move)
4342
/* Insert zeroes. */
4343
size_t insert = precision - move;
5216
size_t prefix_count;
5220
/* Put the additional zeroes after the sign. */
5222
&& (*prec_ptr == '-' || *prec_ptr == '+'
5223
|| *prec_ptr == ' '))
5225
/* Put the additional zeroes after the 0x prefix if
5226
(flags & FLAG_ALT) || (dp->conversion == 'p'). */
5228
&& prec_ptr[0] == '0'
5229
&& (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
5232
move = count - prefix_count;
5233
if (precision > move)
5235
/* Insert zeroes. */
5236
size_t insert = precision - move;
4346
5239
# if USE_SNPRINTF
4349
(count + insert + TCHARS_PER_DCHAR - 1)
4350
/ TCHARS_PER_DCHAR);
4351
length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
4352
ENSURE_ALLOCATION (n);
4353
length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
4354
prec_ptr = (TCHAR_T *) (result + length);
5242
(count + insert + TCHARS_PER_DCHAR - 1)
5243
/ TCHARS_PER_DCHAR);
5244
length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5245
ENSURE_ALLOCATION (n);
5246
length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5247
prec_ptr = (TCHAR_T *) (result + length);
4357
prec_end = prec_ptr + count;
4358
prec_ptr += prefix_count;
4360
while (prec_end > prec_ptr)
4363
prec_end[insert] = prec_end[0];
4369
while (prec_end > prec_ptr);
5250
prec_end = prec_ptr + count;
5251
prec_ptr += prefix_count;
5253
while (prec_end > prec_ptr)
5256
prec_end[insert] = prec_end[0];
5262
while (prec_end > prec_ptr);
5270
if (count >= tmp_length)
5271
/* tmp_length was incorrectly calculated - fix the
4376
5276
#if !DCHAR_IS_TCHAR
4378
if (count >= tmp_length)
4379
/* tmp_length was incorrectly calculated - fix the
4384
/* Convert from TCHAR_T[] to DCHAR_T[]. */
4385
if (dp->conversion == 'c' || dp->conversion == 's')
4387
/* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
4389
The result string is not certainly ASCII. */
4390
const TCHAR_T *tmpsrc;
4393
/* This code assumes that TCHAR_T is 'char'. */
4394
typedef int TCHAR_T_verify
4395
[2 * (sizeof (TCHAR_T) == 1) - 1];
4397
tmpsrc = (TCHAR_T *) (result + length);
4403
if (DCHAR_CONV_FROM_ENCODING (locale_charset (),
4404
iconveh_question_mark,
4407
&tmpdst, &tmpdst_len)
4410
int saved_errno = errno;
4411
if (!(result == resultbuf || result == NULL))
4413
if (buf_malloced != NULL)
4414
free (buf_malloced);
4416
errno = saved_errno;
4419
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
4420
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
4426
/* The result string is ASCII.
4427
Simple 1:1 conversion. */
4429
/* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
4430
no-op conversion, in-place on the array starting
4431
at (result + length). */
4432
if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
4435
const TCHAR_T *tmpsrc;
4440
if (result == resultbuf)
4442
tmpsrc = (TCHAR_T *) (result + length);
4443
/* ENSURE_ALLOCATION will not move tmpsrc
4444
(because it's part of resultbuf). */
4445
ENSURE_ALLOCATION (xsum (length, count));
4449
/* ENSURE_ALLOCATION will move the array
4450
(because it uses realloc(). */
4451
ENSURE_ALLOCATION (xsum (length, count));
4452
tmpsrc = (TCHAR_T *) (result + length);
4456
ENSURE_ALLOCATION (xsum (length, count));
4458
tmpdst = result + length;
4459
/* Copy backwards, because of overlapping. */
4462
for (n = count; n > 0; n--)
4463
*--tmpdst = (unsigned char) *--tmpsrc;
5277
/* Convert from TCHAR_T[] to DCHAR_T[]. */
5278
if (dp->conversion == 'c' || dp->conversion == 's')
5280
/* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5282
The result string is not certainly ASCII. */
5283
const TCHAR_T *tmpsrc;
5286
/* This code assumes that TCHAR_T is 'char'. */
5287
typedef int TCHAR_T_verify
5288
[2 * (sizeof (TCHAR_T) == 1) - 1];
5290
tmpsrc = (TCHAR_T *) (result + length);
5295
DCHAR_CONV_FROM_ENCODING (locale_charset (),
5296
iconveh_question_mark,
5302
int saved_errno = errno;
5303
if (!(result == resultbuf || result == NULL))
5305
if (buf_malloced != NULL)
5306
free (buf_malloced);
5308
errno = saved_errno;
5311
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
5312
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
5318
/* The result string is ASCII.
5319
Simple 1:1 conversion. */
5321
/* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5322
no-op conversion, in-place on the array starting
5323
at (result + length). */
5324
if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
5327
const TCHAR_T *tmpsrc;
5332
if (result == resultbuf)
5334
tmpsrc = (TCHAR_T *) (result + length);
5335
/* ENSURE_ALLOCATION will not move tmpsrc
5336
(because it's part of resultbuf). */
5337
ENSURE_ALLOCATION (xsum (length, count));
5341
/* ENSURE_ALLOCATION will move the array
5342
(because it uses realloc(). */
5343
ENSURE_ALLOCATION (xsum (length, count));
5344
tmpsrc = (TCHAR_T *) (result + length);
5348
ENSURE_ALLOCATION (xsum (length, count));
5350
tmpdst = result + length;
5351
/* Copy backwards, because of overlapping. */
5354
for (n = count; n > 0; n--)
5355
*--tmpdst = (unsigned char) *--tmpsrc;
4468
5360
#if DCHAR_IS_TCHAR && !USE_SNPRINTF
4469
/* Make room for the result. */
4470
if (count > allocated - length)
4472
/* Need at least count elements. But allocate
4475
xmax (xsum (length, count), xtimes (allocated, 2));
5361
/* Make room for the result. */
5362
if (count > allocated - length)
5364
/* Need at least count elements. But allocate
5367
xmax (xsum (length, count), xtimes (allocated, 2));
4477
ENSURE_ALLOCATION (n);
5369
ENSURE_ALLOCATION (n);
4481
/* Here count <= allocated - length. */
5373
/* Here count <= allocated - length. */
4483
/* Perform padding. */
4484
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4485
if (pad_ourselves && has_width)
5375
/* Perform padding. */
5376
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5377
if (pad_ourselves && has_width)
4488
5380
# if ENABLE_UNISTDIO
4489
/* Outside POSIX, it's preferrable to compare the width
4490
against the number of _characters_ of the converted
4492
w = DCHAR_MBSNLEN (result + length, count);
5381
/* Outside POSIX, it's preferrable to compare the width
5382
against the number of _characters_ of the converted
5384
w = DCHAR_MBSNLEN (result + length, count);
4494
/* The width is compared against the number of _bytes_
4495
of the converted value, says POSIX. */
5386
/* The width is compared against the number of _bytes_
5387
of the converted value, says POSIX. */
4500
size_t pad = width - w;
5392
size_t pad = width - w;
5394
/* Make room for the result. */
5395
if (xsum (count, pad) > allocated - length)
5397
/* Need at least count + pad elements. But
5398
allocate proportionally. */
5400
xmax (xsum3 (length, count, pad),
5401
xtimes (allocated, 2));
4501
5403
# if USE_SNPRINTF
4502
/* Make room for the result. */
4503
if (xsum (count, pad) > allocated - length)
4505
/* Need at least count + pad elements. But
4506
allocate proportionally. */
4508
xmax (xsum3 (length, count, pad),
4509
xtimes (allocated, 2));
5405
ENSURE_ALLOCATION (n);
5408
ENSURE_ALLOCATION (n);
5411
/* Here count + pad <= allocated - length. */
4512
ENSURE_ALLOCATION (n);
4515
/* Here count + pad <= allocated - length. */
4518
5414
# if !DCHAR_IS_TCHAR || USE_SNPRINTF
4519
DCHAR_T * const rp = result + length;
5415
DCHAR_T * const rp = result + length;
4521
DCHAR_T * const rp = tmp;
4523
DCHAR_T *p = rp + count;
4524
DCHAR_T *end = p + pad;
4525
# if NEED_PRINTF_FLAG_ZERO
4527
# if !DCHAR_IS_TCHAR
4528
if (dp->conversion == 'c'
4529
|| dp->conversion == 's')
4530
/* No zero-padding for string directives. */
4535
pad_ptr = (*rp == '-' ? rp + 1 : rp);
4536
/* No zero-padding of "inf" and "nan". */
4537
if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
4538
|| (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
4542
/* The generated string now extends from rp to p,
4543
with the zero padding insertion point being at
4546
count = count + pad; /* = end - rp */
4548
if (flags & FLAG_LEFT)
4550
/* Pad with spaces on the right. */
4551
for (; pad > 0; pad--)
4554
# if NEED_PRINTF_FLAG_ZERO
4555
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4557
/* Pad with zeroes. */
4562
for (; pad > 0; pad--)
4568
/* Pad with spaces on the left. */
4573
for (; pad > 0; pad--)
4581
#if DCHAR_IS_TCHAR && !USE_SNPRINTF
4582
if (count >= tmp_length)
4583
/* tmp_length was incorrectly calculated - fix the
4588
/* Here still count <= allocated - length. */
5417
DCHAR_T * const rp = tmp;
5419
DCHAR_T *p = rp + count;
5420
DCHAR_T *end = p + pad;
5422
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5423
if (dp->conversion == 'c'
5424
|| dp->conversion == 's')
5425
/* No zero-padding for string directives. */
5430
pad_ptr = (*rp == '-' ? rp + 1 : rp);
5431
/* No zero-padding of "inf" and "nan". */
5432
if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
5433
|| (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
5436
/* The generated string now extends from rp to p,
5437
with the zero padding insertion point being at
5440
count = count + pad; /* = end - rp */
5442
if (flags & FLAG_LEFT)
5444
/* Pad with spaces on the right. */
5445
for (; pad > 0; pad--)
5448
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
5450
/* Pad with zeroes. */
5455
for (; pad > 0; pad--)
5460
/* Pad with spaces on the left. */
5465
for (; pad > 0; pad--)
5473
/* Here still count <= allocated - length. */
4590
5475
#if !DCHAR_IS_TCHAR || USE_SNPRINTF
4591
/* The snprintf() result did fit. */
5476
/* The snprintf() result did fit. */
4593
/* Append the sprintf() result. */
4594
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
5478
/* Append the sprintf() result. */
5479
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4596
5481
#if !USE_SNPRINTF
4601
5486
#if NEED_PRINTF_DIRECTIVE_F
4602
if (dp->conversion == 'F')
4604
/* Convert the %f result to upper case for %F. */
4605
DCHAR_T *rp = result + length;
4607
for (rc = count; rc > 0; rc--, rp++)
4608
if (*rp >= 'a' && *rp <= 'z')
4609
*rp = *rp - 'a' + 'A';
5487
if (dp->conversion == 'F')
5489
/* Convert the %f result to upper case for %F. */
5490
DCHAR_T *rp = result + length;
5492
for (rc = count; rc > 0; rc--, rp++)
5493
if (*rp >= 'a' && *rp <= 'z')
5494
*rp = *rp - 'a' + 'A';
5501
#undef pad_ourselves
5502
#undef prec_ourselves
4620
5507
/* Add the final NUL. */