1549
1814
/* Ensures that allocated >= needed. Aborts through a jump to
1550
1815
out_of_memory if needed is SIZE_MAX or otherwise too big. */
1551
1816
#define ENSURE_ALLOCATION(needed) \
1552
if ((needed) > allocated) \
1554
size_t memory_size; \
1557
allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1558
if ((needed) > allocated) \
1559
allocated = (needed); \
1560
memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1561
if (size_overflow_p (memory_size)) \
1562
goto out_of_memory; \
1563
if (result == resultbuf || result == NULL) \
1564
memory = (DCHAR_T *) malloc (memory_size); \
1566
memory = (DCHAR_T *) realloc (result, memory_size); \
1567
if (memory == NULL) \
1568
goto out_of_memory; \
1569
if (result == resultbuf && length > 0) \
1570
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); \
1574
1839
for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
1576
if (cp != dp->dir_start)
1578
size_t n = dp->dir_start - cp;
1579
size_t augmented_length = xsum (length, n);
1581
ENSURE_ALLOCATION (augmented_length);
1582
/* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1583
need that the format string contains only ASCII characters
1584
if FCHAR_T and DCHAR_T are not the same type. */
1585
if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1587
DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1588
length = augmented_length;
1593
result[length++] = (unsigned char) *cp++;
1600
/* Execute a single directive. */
1601
if (dp->conversion == '%')
1603
size_t augmented_length;
1605
if (!(dp->arg_index == ARG_NONE))
1607
augmented_length = xsum (length, 1);
1608
ENSURE_ALLOCATION (augmented_length);
1609
result[length] = '%';
1610
length = augmented_length;
1614
if (!(dp->arg_index != ARG_NONE))
1617
if (dp->conversion == 'n')
1619
switch (a.arg[dp->arg_index].type)
1621
case TYPE_COUNT_SCHAR_POINTER:
1622
*a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1624
case TYPE_COUNT_SHORT_POINTER:
1625
*a.arg[dp->arg_index].a.a_count_short_pointer = length;
1627
case TYPE_COUNT_INT_POINTER:
1628
*a.arg[dp->arg_index].a.a_count_int_pointer = length;
1630
case TYPE_COUNT_LONGINT_POINTER:
1631
*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;
1633
1898
#if HAVE_LONG_LONG_INT
1634
case TYPE_COUNT_LONGLONGINT_POINTER:
1635
*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;
1642
1907
#if ENABLE_UNISTDIO
1643
/* The unistdio extensions. */
1644
else if (dp->conversion == 'U')
1646
arg_type type = a.arg[dp->arg_index].type;
1647
int flags = dp->flags;
1655
if (dp->width_start != dp->width_end)
1657
if (dp->width_arg_index != ARG_NONE)
1661
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1663
arg = a.arg[dp->width_arg_index].a.a_int;
1666
/* "A negative field width is taken as a '-' flag
1667
followed by a positive field width." */
1669
width = (unsigned int) (-arg);
1676
const FCHAR_T *digitp = dp->width_start;
1679
width = xsum (xtimes (width, 10), *digitp++ - '0');
1680
while (digitp != dp->width_end);
1687
if (dp->precision_start != dp->precision_end)
1689
if (dp->precision_arg_index != ARG_NONE)
1693
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1695
arg = a.arg[dp->precision_arg_index].a.a_int;
1696
/* "A negative precision is taken as if the precision
1706
const FCHAR_T *digitp = dp->precision_start + 1;
1709
while (digitp != dp->precision_end)
1710
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1717
case TYPE_U8_STRING:
1719
const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1720
const uint8_t *arg_end;
1725
/* Use only PRECISION characters, from the left. */
1728
for (; precision > 0; precision--)
1730
int count = u8_strmblen (arg_end);
1735
if (!(result == resultbuf || result == NULL))
1737
if (buf_malloced != NULL)
1738
free (buf_malloced);
1749
/* Use the entire string, and count the number of
1755
int count = u8_strmblen (arg_end);
1760
if (!(result == resultbuf || result == NULL))
1762
if (buf_malloced != NULL)
1763
free (buf_malloced);
1774
/* Use the entire string. */
1775
arg_end = arg + u8_strlen (arg);
1776
/* The number of characters doesn't matter. */
1780
if (has_width && width > characters
1781
&& !(dp->flags & FLAG_LEFT))
1783
size_t n = width - characters;
1784
ENSURE_ALLOCATION (xsum (length, n));
1785
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);
1789
2054
# if DCHAR_IS_UINT8_T
1791
size_t n = arg_end - arg;
1792
ENSURE_ALLOCATION (xsum (length, n));
1793
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);
1798
DCHAR_T *converted = result + length;
1799
size_t converted_len = allocated - length;
2063
DCHAR_T *converted = result + length;
2064
size_t converted_len = allocated - length;
1800
2065
# if DCHAR_IS_TCHAR
1801
/* Convert from UTF-8 to locale encoding. */
1803
u8_conv_to_encoding (locale_charset (),
1804
iconveh_question_mark,
1805
arg, arg_end - arg, NULL,
1806
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);
1808
/* Convert from UTF-8 to UTF-16/UTF-32. */
1810
U8_TO_DCHAR (arg, arg_end - arg,
1811
converted, &converted_len);
2073
/* Convert from UTF-8 to UTF-16/UTF-32. */
2075
U8_TO_DCHAR (arg, arg_end - arg,
2076
converted, &converted_len);
1813
if (converted == NULL)
1815
int saved_errno = errno;
1816
if (!(result == resultbuf || result == NULL))
1818
if (buf_malloced != NULL)
1819
free (buf_malloced);
1821
errno = saved_errno;
1824
if (converted != result + length)
1826
ENSURE_ALLOCATION (xsum (length, converted_len));
1827
DCHAR_CPY (result + length, converted, converted_len);
1830
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;
1834
if (has_width && width > characters
1835
&& (dp->flags & FLAG_LEFT))
1837
size_t n = width - characters;
1838
ENSURE_ALLOCATION (xsum (length, n));
1839
DCHAR_SET (result + length, ' ', n);
1845
case TYPE_U16_STRING:
1847
const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
1848
const uint16_t *arg_end;
1853
/* Use only PRECISION characters, from the left. */
1856
for (; precision > 0; precision--)
1858
int count = u16_strmblen (arg_end);
1863
if (!(result == resultbuf || result == NULL))
1865
if (buf_malloced != NULL)
1866
free (buf_malloced);
1877
/* Use the entire string, and count the number of
1883
int count = u16_strmblen (arg_end);
1888
if (!(result == resultbuf || result == NULL))
1890
if (buf_malloced != NULL)
1891
free (buf_malloced);
1902
/* Use the entire string. */
1903
arg_end = arg + u16_strlen (arg);
1904
/* The number of characters doesn't matter. */
1908
if (has_width && width > characters
1909
&& !(dp->flags & FLAG_LEFT))
1911
size_t n = width - characters;
1912
ENSURE_ALLOCATION (xsum (length, n));
1913
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);
1917
2182
# if DCHAR_IS_UINT16_T
1919
size_t n = arg_end - arg;
1920
ENSURE_ALLOCATION (xsum (length, n));
1921
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);
1926
DCHAR_T *converted = result + length;
1927
size_t converted_len = allocated - length;
2191
DCHAR_T *converted = result + length;
2192
size_t converted_len = allocated - length;
1928
2193
# if DCHAR_IS_TCHAR
1929
/* Convert from UTF-16 to locale encoding. */
1931
u16_conv_to_encoding (locale_charset (),
1932
iconveh_question_mark,
1933
arg, arg_end - arg, NULL,
1934
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);
1936
/* Convert from UTF-16 to UTF-8/UTF-32. */
1938
U16_TO_DCHAR (arg, arg_end - arg,
1939
converted, &converted_len);
2201
/* Convert from UTF-16 to UTF-8/UTF-32. */
2203
U16_TO_DCHAR (arg, arg_end - arg,
2204
converted, &converted_len);
1941
if (converted == NULL)
1943
int saved_errno = errno;
1944
if (!(result == resultbuf || result == NULL))
1946
if (buf_malloced != NULL)
1947
free (buf_malloced);
1949
errno = saved_errno;
1952
if (converted != result + length)
1954
ENSURE_ALLOCATION (xsum (length, converted_len));
1955
DCHAR_CPY (result + length, converted, converted_len);
1958
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;
1962
if (has_width && width > characters
1963
&& (dp->flags & FLAG_LEFT))
1965
size_t n = width - characters;
1966
ENSURE_ALLOCATION (xsum (length, n));
1967
DCHAR_SET (result + length, ' ', n);
1973
case TYPE_U32_STRING:
1975
const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
1976
const uint32_t *arg_end;
1981
/* Use only PRECISION characters, from the left. */
1984
for (; precision > 0; precision--)
1986
int count = u32_strmblen (arg_end);
1991
if (!(result == resultbuf || result == NULL))
1993
if (buf_malloced != NULL)
1994
free (buf_malloced);
2005
/* Use the entire string, and count the number of
2011
int count = u32_strmblen (arg_end);
2016
if (!(result == resultbuf || result == NULL))
2018
if (buf_malloced != NULL)
2019
free (buf_malloced);
2030
/* Use the entire string. */
2031
arg_end = arg + u32_strlen (arg);
2032
/* The number of characters doesn't matter. */
2036
if (has_width && width > characters
2037
&& !(dp->flags & FLAG_LEFT))
2039
size_t n = width - characters;
2040
ENSURE_ALLOCATION (xsum (length, n));
2041
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);
2045
2310
# if DCHAR_IS_UINT32_T
2047
size_t n = arg_end - arg;
2048
ENSURE_ALLOCATION (xsum (length, n));
2049
DCHAR_CPY (result + length, arg, n);
2312
size_t n = arg_end - arg;
2313
ENSURE_ALLOCATION (xsum (length, n));
2314
DCHAR_CPY (result + length, arg, n);
2054
DCHAR_T *converted = result + length;
2055
size_t converted_len = allocated - length;
2319
DCHAR_T *converted = result + length;
2320
size_t converted_len = allocated - length;
2056
2321
# if DCHAR_IS_TCHAR
2057
/* Convert from UTF-32 to locale encoding. */
2059
u32_conv_to_encoding (locale_charset (),
2060
iconveh_question_mark,
2061
arg, arg_end - arg, NULL,
2062
converted, &converted_len);
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);
2064
/* Convert from UTF-32 to UTF-8/UTF-16. */
2066
U32_TO_DCHAR (arg, arg_end - arg,
2067
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);
2069
if (converted == NULL)
2071
int saved_errno = errno;
2072
if (!(result == resultbuf || result == NULL))
2074
if (buf_malloced != NULL)
2075
free (buf_malloced);
2077
errno = saved_errno;
2080
if (converted != result + length)
2082
ENSURE_ALLOCATION (xsum (length, converted_len));
2083
DCHAR_CPY (result + length, converted, converted_len);
2086
length += 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;
2090
if (has_width && width > characters
2091
&& (dp->flags & FLAG_LEFT))
2093
size_t n = width - characters;
2094
ENSURE_ALLOCATION (xsum (length, n));
2095
DCHAR_SET (result + length, ' ', n);
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);
2106
#if (!USE_SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2107
else if (dp->conversion == 's'
2371
#if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && HAVE_WCHAR_T
2372
else if (dp->conversion == 's'
2108
2373
# if WIDE_CHAR_VERSION
2109
&& a.arg[dp->arg_index].type != TYPE_WIDE_STRING
2374
&& a.arg[dp->arg_index].type != TYPE_WIDE_STRING
2111
&& a.arg[dp->arg_index].type == TYPE_WIDE_STRING
2376
&& a.arg[dp->arg_index].type == TYPE_WIDE_STRING
2115
/* The normal handling of the 's' directive below requires
2116
allocating a temporary buffer. The determination of its
2117
length (tmp_length), in the case when a precision is
2118
specified, below requires a conversion between a char[]
2119
string and a wchar_t[] wide string. It could be done, but
2120
we have no guarantee that the implementation of sprintf will
2121
use the exactly same algorithm. Without this guarantee, it
2122
is possible to have buffer overrun bugs. In order to avoid
2123
such bugs, we implement the entire processing of the 's'
2124
directive ourselves. */
2125
int flags = dp->flags;
2133
if (dp->width_start != dp->width_end)
2135
if (dp->width_arg_index != ARG_NONE)
2139
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2141
arg = a.arg[dp->width_arg_index].a.a_int;
2144
/* "A negative field width is taken as a '-' flag
2145
followed by a positive field width." */
2147
width = (unsigned int) (-arg);
2154
const FCHAR_T *digitp = dp->width_start;
2157
width = xsum (xtimes (width, 10), *digitp++ - '0');
2158
while (digitp != dp->width_end);
2165
if (dp->precision_start != dp->precision_end)
2167
if (dp->precision_arg_index != ARG_NONE)
2171
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2173
arg = a.arg[dp->precision_arg_index].a.a_int;
2174
/* "A negative precision is taken as if the precision
2184
const FCHAR_T *digitp = dp->precision_start + 1;
2187
while (digitp != dp->precision_end)
2188
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
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');
2193
2458
# if WIDE_CHAR_VERSION
2194
/* %s in vasnwprintf. See the specification of fwprintf. */
2196
const char *arg = a.arg[dp->arg_index].a.a_string;
2197
const char *arg_end;
2202
/* Use only as many bytes as needed to produce PRECISION
2203
wide characters, from the left. */
2206
memset (&state, '\0', sizeof (mbstate_t));
2210
for (; precision > 0; precision--)
2214
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2216
count = mblen (arg_end, MB_CUR_MAX);
2219
/* Found the terminating NUL. */
2223
/* Invalid or incomplete multibyte character. */
2224
if (!(result == resultbuf || result == NULL))
2226
if (buf_malloced != NULL)
2227
free (buf_malloced);
2238
/* Use the entire string, and count the number of wide
2242
memset (&state, '\0', sizeof (mbstate_t));
2250
count = mbrlen (arg_end, MB_CUR_MAX, &state);
2252
count = mblen (arg_end, MB_CUR_MAX);
2255
/* Found the terminating NUL. */
2259
/* Invalid or incomplete multibyte character. */
2260
if (!(result == resultbuf || result == NULL))
2262
if (buf_malloced != NULL)
2263
free (buf_malloced);
2274
/* Use the entire string. */
2275
arg_end = arg + strlen (arg);
2276
/* The number of characters doesn't matter. */
2280
if (has_width && width > characters
2281
&& !(dp->flags & FLAG_LEFT))
2283
size_t n = width - characters;
2284
ENSURE_ALLOCATION (xsum (length, n));
2285
DCHAR_SET (result + length, ' ', n);
2289
if (has_precision || has_width)
2291
/* We know the number of wide characters in advance. */
2295
memset (&state, '\0', sizeof (mbstate_t));
2297
ENSURE_ALLOCATION (xsum (length, characters));
2298
for (remaining = characters; remaining > 0; remaining--)
2303
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2305
count = mbtowc (&wc, arg, arg_end - arg);
2308
/* mbrtowc not consistent with mbrlen, or mbtowc
2309
not consistent with mblen. */
2311
result[length++] = wc;
2314
if (!(arg == arg_end))
2321
memset (&state, '\0', sizeof (mbstate_t));
2323
while (arg < arg_end)
2328
count = mbrtowc (&wc, arg, arg_end - arg, &state);
2330
count = mbtowc (&wc, arg, arg_end - arg);
2333
/* mbrtowc not consistent with mbrlen, or mbtowc
2334
not consistent with mblen. */
2336
ENSURE_ALLOCATION (xsum (length, 1));
2337
result[length++] = wc;
2342
if (has_width && width > characters
2343
&& (dp->flags & FLAG_LEFT))
2345
size_t n = width - characters;
2346
ENSURE_ALLOCATION (xsum (length, n));
2347
DCHAR_SET (result + length, ' ', n);
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);
2352
/* %ls in vasnprintf. See the specification of fprintf. */
2354
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
2355
const wchar_t *arg_end;
2357
# if !DCHAR_IS_TCHAR
2358
/* This code assumes that TCHAR_T is 'char'. */
2359
typedef int TCHAR_T_verify[2 * (sizeof (TCHAR_T) == 1) - 1];
2368
/* Use only as many wide characters as needed to produce
2369
at most PRECISION bytes, from the left. */
2372
memset (&state, '\0', sizeof (mbstate_t));
2376
while (precision > 0)
2378
char buf[64]; /* Assume MB_CUR_MAX <= 64. */
2382
/* Found the terminating null wide character. */
2385
count = wcrtomb (buf, *arg_end, &state);
2387
count = wctomb (buf, *arg_end);
2391
/* Cannot convert. */
2392
if (!(result == resultbuf || result == NULL))
2394
if (buf_malloced != NULL)
2395
free (buf_malloced);
2400
if (precision < count)
2403
characters += count;
2413
/* Use the entire string, and count the number of
2417
memset (&state, '\0', sizeof (mbstate_t));
2423
char buf[64]; /* Assume MB_CUR_MAX <= 64. */
2427
/* Found the terminating null wide character. */
2430
count = wcrtomb (buf, *arg_end, &state);
2432
count = wctomb (buf, *arg_end);
2436
/* Cannot convert. */
2437
if (!(result == resultbuf || result == NULL))
2439
if (buf_malloced != NULL)
2440
free (buf_malloced);
2446
characters += count;
2452
/* Use the entire string. */
2453
arg_end = arg + local_wcslen (arg);
2454
/* The number of bytes doesn't matter. */
2459
# if !DCHAR_IS_TCHAR
2460
/* Convert the string into a piece of temporary memory. */
2461
tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
2465
TCHAR_T *tmpptr = tmpsrc;
2469
memset (&state, '\0', sizeof (mbstate_t));
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));
2471
for (remaining = characters; remaining > 0; )
2473
char buf[64]; /* Assume MB_CUR_MAX <= 64. */
2736
for (remaining = characters; remaining > 0; )
2738
char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
2479
count = wcrtomb (buf, *arg, &state);
2743
# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
2744
count = wcrtomb (cbuf, *arg, &state);
2481
count = wctomb (buf, *arg);
2746
count = wctomb (cbuf, *arg);
2484
/* Inconsistency. */
2486
memcpy (tmpptr, buf, count);
2491
if (!(arg == arg_end))
2749
/* Inconsistency. */
2751
memcpy (tmpptr, cbuf, count);
2756
if (!(arg == arg_end))
2495
/* Convert from TCHAR_T[] to DCHAR_T[]. */
2497
DCHAR_CONV_FROM_ENCODING (locale_charset (),
2498
iconveh_question_mark,
2504
int saved_errno = errno;
2506
if (!(result == resultbuf || result == NULL))
2508
if (buf_malloced != NULL)
2509
free (buf_malloced);
2511
errno = saved_errno;
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;
2519
2784
# if ENABLE_UNISTDIO
2520
/* Outside POSIX, it's preferrable to compare the width
2521
against the number of _characters_ of the converted
2523
w = DCHAR_MBSNLEN (result + length, characters);
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);
2525
/* The width is compared against the number of _bytes_
2526
of the converted value, says POSIX. */
2790
/* The width is compared against the number of _bytes_
2791
of the converted value, says POSIX. */
2531
/* w doesn't matter. */
2796
/* w doesn't matter. */
2534
if (has_width && width > w
2535
&& !(dp->flags & FLAG_LEFT))
2537
size_t n = width - w;
2538
ENSURE_ALLOCATION (xsum (length, n));
2539
DCHAR_SET (result + length, ' ', n);
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);
2543
2808
# if DCHAR_IS_TCHAR
2544
if (has_precision || has_width)
2546
/* We know the number of bytes in advance. */
2550
memset (&state, '\0', sizeof (mbstate_t));
2552
ENSURE_ALLOCATION (xsum (length, characters));
2553
for (remaining = characters; remaining > 0; )
2555
char buf[64]; /* Assume MB_CUR_MAX <= 64. */
2561
count = wcrtomb (buf, *arg, &state);
2563
count = wctomb (buf, *arg);
2566
/* Inconsistency. */
2568
memcpy (result + length, buf, count);
2573
if (!(arg == arg_end))
2580
memset (&state, '\0', sizeof (mbstate_t));
2582
while (arg < arg_end)
2584
char buf[64]; /* Assume MB_CUR_MAX <= 64. */
2590
count = wcrtomb (buf, *arg, &state);
2592
count = wctomb (buf, *arg);
2595
/* Inconsistency. */
2597
ENSURE_ALLOCATION (xsum (length, count));
2598
memcpy (result + length, buf, count);
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);
2604
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
2605
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
2607
length += tmpdst_len;
2877
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
2878
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
2880
length += tmpdst_len;
2610
if (has_width && width > w
2611
&& (dp->flags & FLAG_LEFT))
2613
size_t n = width - w;
2614
ENSURE_ALLOCATION (xsum (length, n));
2615
DCHAR_SET (result + length, ' ', n);
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);
2622
2895
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2623
else if ((dp->conversion == 'a' || dp->conversion == 'A')
2896
else if ((dp->conversion == 'a' || dp->conversion == 'A')
2624
2897
# if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2626
2899
# if NEED_PRINTF_DOUBLE
2627
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2900
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2629
2902
# if NEED_PRINTF_LONG_DOUBLE
2630
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2903
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2636
arg_type type = a.arg[dp->arg_index].type;
2637
int flags = dp->flags;
2643
DCHAR_T tmpbuf[700];
2650
if (dp->width_start != dp->width_end)
2652
if (dp->width_arg_index != ARG_NONE)
2656
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2658
arg = a.arg[dp->width_arg_index].a.a_int;
2661
/* "A negative field width is taken as a '-' flag
2662
followed by a positive field width." */
2664
width = (unsigned int) (-arg);
2671
const FCHAR_T *digitp = dp->width_start;
2674
width = xsum (xtimes (width, 10), *digitp++ - '0');
2675
while (digitp != dp->width_end);
2682
if (dp->precision_start != dp->precision_end)
2684
if (dp->precision_arg_index != ARG_NONE)
2688
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2690
arg = a.arg[dp->precision_arg_index].a.a_int;
2691
/* "A negative precision is taken as if the precision
2701
const FCHAR_T *digitp = dp->precision_start + 1;
2704
while (digitp != dp->precision_end)
2705
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2710
/* Allocate a temporary buffer of sufficient size. */
2711
if (type == TYPE_LONGDOUBLE)
2713
(unsigned int) ((LDBL_DIG + 1)
2714
* 0.831 /* decimal -> hexadecimal */
2716
+ 1; /* turn floor into ceil */
2719
(unsigned int) ((DBL_DIG + 1)
2720
* 0.831 /* decimal -> hexadecimal */
2722
+ 1; /* turn floor into ceil */
2723
if (tmp_length < precision)
2724
tmp_length = precision;
2725
/* Account for sign, decimal point etc. */
2726
tmp_length = xsum (tmp_length, 12);
2728
if (tmp_length < width)
2731
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2733
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2737
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2739
if (size_overflow_p (tmp_memsize))
2740
/* Overflow, would lead to out of memory. */
2742
tmp = (DCHAR_T *) malloc (tmp_memsize);
2744
/* Out of memory. */
2750
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)
2752
3025
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
2753
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2757
if (dp->conversion == 'A')
2759
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2763
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2769
DECL_LONG_DOUBLE_ROUNDING
2771
BEGIN_LONG_DOUBLE_ROUNDING ();
2773
if (signbit (arg)) /* arg < 0.0L or negative zero */
2781
else if (flags & FLAG_SHOWSIGN)
2783
else if (flags & FLAG_SPACE)
2786
if (arg > 0.0L && arg + arg == arg)
2788
if (dp->conversion == 'A')
2790
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2794
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2800
long double mantissa;
2803
mantissa = printf_frexpl (arg, &exponent);
2811
&& precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
2813
/* Round the mantissa. */
2814
long double tail = mantissa;
2817
for (q = precision; ; q--)
2819
int digit = (int) tail;
2823
if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
2832
for (q = precision; q > 0; q--)
2838
*p++ = dp->conversion - 'A' + 'X';
2843
digit = (int) mantissa;
2846
if ((flags & FLAG_ALT)
2847
|| mantissa > 0.0L || precision > 0)
2849
*p++ = decimal_point_char ();
2850
/* This loop terminates because we assume
2851
that FLT_RADIX is a power of 2. */
2852
while (mantissa > 0.0L)
2855
digit = (int) mantissa;
2860
: dp->conversion - 10);
2864
while (precision > 0)
2871
*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';
2872
3145
# if WIDE_CHAR_VERSION
2874
static const wchar_t decimal_format[] =
2875
{ '%', '+', 'd', '\0' };
2876
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);
2881
if (sizeof (DCHAR_T) == 1)
2883
sprintf ((char *) p, "%+d", exponent);
2891
sprintf (expbuf, "%+d", exponent);
2892
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++)
2898
END_LONG_DOUBLE_ROUNDING ();
3171
END_LONG_DOUBLE_ROUNDING ();
2906
3179
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
2907
double arg = a.arg[dp->arg_index].a.a_double;
2911
if (dp->conversion == 'A')
2913
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2917
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2924
if (signbit (arg)) /* arg < 0.0 or negative zero */
2932
else if (flags & FLAG_SHOWSIGN)
2934
else if (flags & FLAG_SPACE)
2937
if (arg > 0.0 && arg + arg == arg)
2939
if (dp->conversion == 'A')
2941
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2945
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2954
mantissa = printf_frexp (arg, &exponent);
2962
&& precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
2964
/* Round the mantissa. */
2965
double tail = mantissa;
2968
for (q = precision; ; q--)
2970
int digit = (int) tail;
2974
if (digit & 1 ? tail >= 0.5 : tail > 0.5)
2983
for (q = precision; q > 0; q--)
2989
*p++ = dp->conversion - 'A' + 'X';
2994
digit = (int) mantissa;
2997
if ((flags & FLAG_ALT)
2998
|| mantissa > 0.0 || precision > 0)
3000
*p++ = decimal_point_char ();
3001
/* This loop terminates because we assume
3002
that FLT_RADIX is a power of 2. */
3003
while (mantissa > 0.0)
3006
digit = (int) mantissa;
3011
: dp->conversion - 10);
3015
while (precision > 0)
3022
*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';
3023
3296
# if WIDE_CHAR_VERSION
3025
static const wchar_t decimal_format[] =
3026
{ '%', '+', 'd', '\0' };
3027
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);
3032
if (sizeof (DCHAR_T) == 1)
3034
sprintf ((char *) p, "%+d", exponent);
3042
sprintf (expbuf, "%+d", exponent);
3043
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++)
3053
/* The generated string now extends from tmp to p, with the
3054
zero padding insertion point being at pad_ptr. */
3055
if (has_width && p - tmp < width)
3057
size_t pad = width - (p - tmp);
3058
DCHAR_T *end = p + pad;
3060
if (flags & FLAG_LEFT)
3062
/* Pad with spaces on the right. */
3063
for (; pad > 0; pad--)
3066
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3068
/* Pad with zeroes. */
3073
for (; pad > 0; pad--)
3078
/* Pad with spaces on the left. */
3083
for (; pad > 0; pad--)
3091
size_t count = p - tmp;
3093
if (count >= tmp_length)
3094
/* tmp_length was incorrectly calculated - fix the
3098
/* Make room for the result. */
3099
if (count >= allocated - length)
3101
size_t n = xsum (length, count);
3103
ENSURE_ALLOCATION (n);
3106
/* Append the result. */
3107
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));
3114
3387
#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
3115
else if ((dp->conversion == 'f' || dp->conversion == 'F'
3116
|| dp->conversion == 'e' || dp->conversion == 'E'
3117
|| dp->conversion == 'g' || dp->conversion == 'G'
3118
|| 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')
3120
3393
# if NEED_PRINTF_DOUBLE
3121
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
3394
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
3122
3395
# elif NEED_PRINTF_INFINITE_DOUBLE
3123
|| (a.arg[dp->arg_index].type == TYPE_DOUBLE
3124
/* The systems (mingw) which produce wrong output
3125
for Inf, -Inf, and NaN also do so for -0.0.
3126
Therefore we treat this case here as well. */
3127
&& 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))
3129
3402
# if NEED_PRINTF_LONG_DOUBLE
3130
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3403
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3131
3404
# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
3132
|| (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
3133
/* Some systems produce wrong output for Inf,
3134
-Inf, and NaN. Some systems in this category
3135
(IRIX 5.3) also do so for -0.0. Therefore we
3136
treat this case here as well. */
3137
&& is_infinite_or_zerol (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))
3141
3414
# if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
3142
arg_type type = a.arg[dp->arg_index].type;
3415
arg_type type = a.arg[dp->arg_index].type;
3144
int flags = dp->flags;
3150
DCHAR_T tmpbuf[700];
3157
if (dp->width_start != dp->width_end)
3159
if (dp->width_arg_index != ARG_NONE)
3163
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3165
arg = a.arg[dp->width_arg_index].a.a_int;
3168
/* "A negative field width is taken as a '-' flag
3169
followed by a positive field width." */
3171
width = (unsigned int) (-arg);
3178
const FCHAR_T *digitp = dp->width_start;
3181
width = xsum (xtimes (width, 10), *digitp++ - '0');
3182
while (digitp != dp->width_end);
3189
if (dp->precision_start != dp->precision_end)
3191
if (dp->precision_arg_index != ARG_NONE)
3195
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3197
arg = a.arg[dp->precision_arg_index].a.a_int;
3198
/* "A negative precision is taken as if the precision
3208
const FCHAR_T *digitp = dp->precision_start + 1;
3211
while (digitp != dp->precision_end)
3212
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
3217
/* POSIX specifies the default precision to be 6 for %f, %F,
3218
%e, %E, but not for %g, %G. Implementations appear to use
3219
the same default precision also for %g, %G. But for %a, %A,
3220
the default precision is 0. */
3222
if (!(dp->conversion == 'a' || dp->conversion == 'A'))
3225
/* 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. */
3226
3499
# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3227
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
3500
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
3228
3501
# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
3229
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
3502
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
3230
3503
# elif NEED_PRINTF_LONG_DOUBLE
3231
tmp_length = LDBL_DIG + 1;
3504
tmp_length = LDBL_DIG + 1;
3232
3505
# elif NEED_PRINTF_DOUBLE
3233
tmp_length = DBL_DIG + 1;
3506
tmp_length = DBL_DIG + 1;
3237
if (tmp_length < precision)
3238
tmp_length = precision;
3510
if (tmp_length < precision)
3511
tmp_length = precision;
3239
3512
# if NEED_PRINTF_LONG_DOUBLE
3240
3513
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3241
if (type == TYPE_LONGDOUBLE)
3514
if (type == TYPE_LONGDOUBLE)
3243
if (dp->conversion == 'f' || dp->conversion == 'F')
3245
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3246
if (!(isnanl (arg) || arg + arg == arg))
3248
/* arg is finite and nonzero. */
3249
int exponent = floorlog10l (arg < 0 ? -arg : arg);
3250
if (exponent >= 0 && tmp_length < exponent + precision)
3251
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;
3255
3528
# if NEED_PRINTF_DOUBLE
3256
3529
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3257
if (type == TYPE_DOUBLE)
3530
if (type == TYPE_DOUBLE)
3259
if (dp->conversion == 'f' || dp->conversion == 'F')
3261
double arg = a.arg[dp->arg_index].a.a_double;
3262
if (!(isnand (arg) || arg + arg == arg))
3264
/* arg is finite and nonzero. */
3265
int exponent = floorlog10 (arg < 0 ? -arg : arg);
3266
if (exponent >= 0 && tmp_length < exponent + precision)
3267
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;
3271
/* Account for sign, decimal point etc. */
3272
tmp_length = xsum (tmp_length, 12);
3274
if (tmp_length < width)
3277
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3279
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
3283
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
3285
if (size_overflow_p (tmp_memsize))
3286
/* Overflow, would lead to out of memory. */
3288
tmp = (DCHAR_T *) malloc (tmp_memsize);
3290
/* 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. */
3297
3570
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
3298
3571
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3299
if (type == TYPE_LONGDOUBLE)
3572
if (type == TYPE_LONGDOUBLE)
3302
long double arg = a.arg[dp->arg_index].a.a_longdouble;
3306
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3308
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3312
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3318
DECL_LONG_DOUBLE_ROUNDING
3320
BEGIN_LONG_DOUBLE_ROUNDING ();
3322
if (signbit (arg)) /* arg < 0.0L or negative zero */
3330
else if (flags & FLAG_SHOWSIGN)
3332
else if (flags & FLAG_SPACE)
3335
if (arg > 0.0L && arg + arg == arg)
3337
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3339
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3343
*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';
3348
3621
# if NEED_PRINTF_LONG_DOUBLE
3351
if (dp->conversion == 'f' || dp->conversion == 'F')
3357
scale10_round_decimal_long_double (arg, precision);
3360
END_LONG_DOUBLE_ROUNDING ();
3363
ndigits = strlen (digits);
3365
if (ndigits > precision)
3369
*p++ = digits[ndigits];
3371
while (ndigits > precision);
3374
/* Here ndigits <= precision. */
3375
if ((flags & FLAG_ALT) || precision > 0)
3377
*p++ = decimal_point_char ();
3378
for (; precision > ndigits; precision--)
3383
*p++ = digits[ndigits];
3389
else if (dp->conversion == 'e' || dp->conversion == 'E')
3397
if ((flags & FLAG_ALT) || precision > 0)
3399
*p++ = decimal_point_char ();
3400
for (; precision > 0; precision--)
3411
exponent = floorlog10l (arg);
3416
scale10_round_decimal_long_double (arg,
3417
(int)precision - exponent);
3420
END_LONG_DOUBLE_ROUNDING ();
3423
ndigits = strlen (digits);
3425
if (ndigits == precision + 1)
3427
if (ndigits < precision
3428
|| ndigits > precision + 2)
3429
/* The exponent was not guessed
3430
precisely enough. */
3433
/* None of two values of exponent is
3434
the right one. Prevent an endless
3438
if (ndigits == precision)
3444
/* Here ndigits = precision+1. */
3445
if (is_borderline (digits, precision))
3447
/* Maybe the exponent guess was too high
3448
and a smaller exponent can be reached
3449
by turning a 10...0 into 9...9x. */
3451
scale10_round_decimal_long_double (arg,
3452
(int)precision - exponent + 1);
3453
if (digits2 == NULL)
3456
END_LONG_DOUBLE_ROUNDING ();
3459
if (strlen (digits2) == precision + 1)
3468
/* Here ndigits = precision+1. */
3470
*p++ = digits[--ndigits];
3471
if ((flags & FLAG_ALT) || precision > 0)
3473
*p++ = decimal_point_char ();
3477
*p++ = digits[ndigits];
3484
*p++ = dp->conversion; /* 'e' or 'E' */
3485
# if WIDE_CHAR_VERSION
3487
static const wchar_t decimal_format[] =
3488
{ '%', '+', '.', '2', 'd', '\0' };
3489
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3494
if (sizeof (DCHAR_T) == 1)
3496
sprintf ((char *) p, "%+.2d", exponent);
3504
sprintf (expbuf, "%+.2d", exponent);
3505
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3510
else if (dp->conversion == 'g' || dp->conversion == 'G')
3514
/* precision >= 1. */
3517
/* The exponent is 0, >= -4, < precision.
3518
Use fixed-point notation. */
3520
size_t ndigits = precision;
3521
/* Number of trailing zeroes that have to be
3524
(flags & FLAG_ALT ? 0 : precision - 1);
3528
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3530
*p++ = decimal_point_char ();
3531
while (ndigits > nzeroes)
3547
exponent = floorlog10l (arg);
3552
scale10_round_decimal_long_double (arg,
3553
(int)(precision - 1) - exponent);
3556
END_LONG_DOUBLE_ROUNDING ();
3559
ndigits = strlen (digits);
3561
if (ndigits == precision)
3563
if (ndigits < precision - 1
3564
|| ndigits > precision + 1)
3565
/* The exponent was not guessed
3566
precisely enough. */
3569
/* None of two values of exponent is
3570
the right one. Prevent an endless
3574
if (ndigits < precision)
3580
/* Here ndigits = precision. */
3581
if (is_borderline (digits, precision - 1))
3583
/* Maybe the exponent guess was too high
3584
and a smaller exponent can be reached
3585
by turning a 10...0 into 9...9x. */
3587
scale10_round_decimal_long_double (arg,
3588
(int)(precision - 1) - exponent + 1);
3589
if (digits2 == NULL)
3592
END_LONG_DOUBLE_ROUNDING ();
3595
if (strlen (digits2) == precision)
3604
/* Here ndigits = precision. */
3606
/* Determine the number of trailing zeroes
3607
that have to be dropped. */
3609
if ((flags & FLAG_ALT) == 0)
3610
while (nzeroes < ndigits
3611
&& digits[nzeroes] == '0')
3614
/* The exponent is now determined. */
3616
&& exponent < (long)precision)
3618
/* Fixed-point notation:
3619
max(exponent,0)+1 digits, then the
3620
decimal point, then the remaining
3621
digits without trailing zeroes. */
3624
size_t count = exponent + 1;
3625
/* Note: count <= precision = ndigits. */
3626
for (; count > 0; count--)
3627
*p++ = digits[--ndigits];
3628
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3630
*p++ = decimal_point_char ();
3631
while (ndigits > nzeroes)
3634
*p++ = digits[ndigits];
3640
size_t count = -exponent - 1;
3642
*p++ = decimal_point_char ();
3643
for (; count > 0; count--)
3645
while (ndigits > nzeroes)
3648
*p++ = digits[ndigits];
3654
/* Exponential notation. */
3655
*p++ = digits[--ndigits];
3656
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3658
*p++ = decimal_point_char ();
3659
while (ndigits > nzeroes)
3662
*p++ = digits[ndigits];
3665
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3666
# if WIDE_CHAR_VERSION
3668
static const wchar_t decimal_format[] =
3669
{ '%', '+', '.', '2', 'd', '\0' };
3670
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3675
if (sizeof (DCHAR_T) == 1)
3677
sprintf ((char *) p, "%+.2d", exponent);
3685
sprintf (expbuf, "%+.2d", exponent);
3686
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++)
3698
/* arg is finite. */
3704
if (dp->conversion == 'f' || dp->conversion == 'F')
3707
if ((flags & FLAG_ALT) || precision > 0)
3709
*p++ = decimal_point_char ();
3710
for (; precision > 0; precision--)
3714
else if (dp->conversion == 'e' || dp->conversion == 'E')
3717
if ((flags & FLAG_ALT) || precision > 0)
3719
*p++ = decimal_point_char ();
3720
for (; precision > 0; precision--)
3723
*p++ = dp->conversion; /* 'e' or 'E' */
3728
else if (dp->conversion == 'g' || dp->conversion == 'G')
3731
if (flags & FLAG_ALT)
3734
(precision > 0 ? precision - 1 : 0);
3735
*p++ = decimal_point_char ();
3736
for (; ndigits > 0; --ndigits)
3740
else if (dp->conversion == 'a' || dp->conversion == 'A')
3743
*p++ = dp->conversion - 'A' + 'X';
3746
if ((flags & FLAG_ALT) || precision > 0)
3748
*p++ = decimal_point_char ();
3749
for (; precision > 0; precision--)
3752
*p++ = dp->conversion - 'A' + 'P';
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';
3761
END_LONG_DOUBLE_ROUNDING ();
4034
END_LONG_DOUBLE_ROUNDING ();
3764
4037
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3768
4041
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3770
double arg = a.arg[dp->arg_index].a.a_double;
3774
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3776
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3780
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3787
if (signbit (arg)) /* arg < 0.0 or negative zero */
3795
else if (flags & FLAG_SHOWSIGN)
3797
else if (flags & FLAG_SPACE)
3800
if (arg > 0.0 && arg + arg == arg)
3802
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3804
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3808
*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';
3813
4086
# if NEED_PRINTF_DOUBLE
3816
if (dp->conversion == 'f' || dp->conversion == 'F')
3822
scale10_round_decimal_double (arg, precision);
3825
ndigits = strlen (digits);
3827
if (ndigits > precision)
3831
*p++ = digits[ndigits];
3833
while (ndigits > precision);
3836
/* Here ndigits <= precision. */
3837
if ((flags & FLAG_ALT) || precision > 0)
3839
*p++ = decimal_point_char ();
3840
for (; precision > ndigits; precision--)
3845
*p++ = digits[ndigits];
3851
else if (dp->conversion == 'e' || dp->conversion == 'E')
3859
if ((flags & FLAG_ALT) || precision > 0)
3861
*p++ = decimal_point_char ();
3862
for (; precision > 0; precision--)
3873
exponent = floorlog10 (arg);
3878
scale10_round_decimal_double (arg,
3879
(int)precision - exponent);
3882
ndigits = strlen (digits);
3884
if (ndigits == precision + 1)
3886
if (ndigits < precision
3887
|| ndigits > precision + 2)
3888
/* The exponent was not guessed
3889
precisely enough. */
3892
/* None of two values of exponent is
3893
the right one. Prevent an endless
3897
if (ndigits == precision)
3903
/* Here ndigits = precision+1. */
3904
if (is_borderline (digits, precision))
3906
/* Maybe the exponent guess was too high
3907
and a smaller exponent can be reached
3908
by turning a 10...0 into 9...9x. */
3910
scale10_round_decimal_double (arg,
3911
(int)precision - exponent + 1);
3912
if (digits2 == NULL)
3917
if (strlen (digits2) == precision + 1)
3926
/* Here ndigits = precision+1. */
3928
*p++ = digits[--ndigits];
3929
if ((flags & FLAG_ALT) || precision > 0)
3931
*p++ = decimal_point_char ();
3935
*p++ = digits[ndigits];
3942
*p++ = dp->conversion; /* 'e' or 'E' */
3943
# if WIDE_CHAR_VERSION
3945
static const wchar_t decimal_format[] =
3946
/* Produce the same number of exponent digits
3947
as the native printf implementation. */
3948
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3949
{ '%', '+', '.', '3', 'd', '\0' };
3951
{ '%', '+', '.', '2', 'd', '\0' };
3953
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3959
static const char decimal_format[] =
3960
/* Produce the same number of exponent digits
3961
as the native printf implementation. */
3962
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3967
if (sizeof (DCHAR_T) == 1)
3969
sprintf ((char *) p, decimal_format, exponent);
3977
sprintf (expbuf, decimal_format, exponent);
3978
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3984
else if (dp->conversion == 'g' || dp->conversion == 'G')
3988
/* precision >= 1. */
3991
/* The exponent is 0, >= -4, < precision.
3992
Use fixed-point notation. */
3994
size_t ndigits = precision;
3995
/* Number of trailing zeroes that have to be
3998
(flags & FLAG_ALT ? 0 : precision - 1);
4002
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4004
*p++ = decimal_point_char ();
4005
while (ndigits > nzeroes)
4021
exponent = floorlog10 (arg);
4026
scale10_round_decimal_double (arg,
4027
(int)(precision - 1) - exponent);
4030
ndigits = strlen (digits);
4032
if (ndigits == precision)
4034
if (ndigits < precision - 1
4035
|| ndigits > precision + 1)
4036
/* The exponent was not guessed
4037
precisely enough. */
4040
/* None of two values of exponent is
4041
the right one. Prevent an endless
4045
if (ndigits < precision)
4051
/* Here ndigits = precision. */
4052
if (is_borderline (digits, precision - 1))
4054
/* Maybe the exponent guess was too high
4055
and a smaller exponent can be reached
4056
by turning a 10...0 into 9...9x. */
4058
scale10_round_decimal_double (arg,
4059
(int)(precision - 1) - exponent + 1);
4060
if (digits2 == NULL)
4065
if (strlen (digits2) == precision)
4074
/* Here ndigits = precision. */
4076
/* Determine the number of trailing zeroes
4077
that have to be dropped. */
4079
if ((flags & FLAG_ALT) == 0)
4080
while (nzeroes < ndigits
4081
&& digits[nzeroes] == '0')
4084
/* The exponent is now determined. */
4086
&& exponent < (long)precision)
4088
/* Fixed-point notation:
4089
max(exponent,0)+1 digits, then the
4090
decimal point, then the remaining
4091
digits without trailing zeroes. */
4094
size_t count = exponent + 1;
4095
/* Note: count <= precision = ndigits. */
4096
for (; count > 0; count--)
4097
*p++ = digits[--ndigits];
4098
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4100
*p++ = decimal_point_char ();
4101
while (ndigits > nzeroes)
4104
*p++ = digits[ndigits];
4110
size_t count = -exponent - 1;
4112
*p++ = decimal_point_char ();
4113
for (; count > 0; count--)
4115
while (ndigits > nzeroes)
4118
*p++ = digits[ndigits];
4124
/* Exponential notation. */
4125
*p++ = digits[--ndigits];
4126
if ((flags & FLAG_ALT) || ndigits > nzeroes)
4128
*p++ = decimal_point_char ();
4129
while (ndigits > nzeroes)
4132
*p++ = digits[ndigits];
4135
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
4136
# if WIDE_CHAR_VERSION
4138
static const wchar_t decimal_format[] =
4139
/* Produce the same number of exponent digits
4140
as the native printf implementation. */
4141
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4142
{ '%', '+', '.', '3', 'd', '\0' };
4144
{ '%', '+', '.', '2', 'd', '\0' };
4146
SNPRINTF (p, 6 + 1, decimal_format, exponent);
4152
static const char decimal_format[] =
4153
/* Produce the same number of exponent digits
4154
as the native printf implementation. */
4155
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4160
if (sizeof (DCHAR_T) == 1)
4162
sprintf ((char *) p, decimal_format, exponent);
4170
sprintf (expbuf, decimal_format, exponent);
4171
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++)
4184
/* arg is finite. */
4190
if (dp->conversion == 'f' || dp->conversion == 'F')
4193
if ((flags & FLAG_ALT) || precision > 0)
4195
*p++ = decimal_point_char ();
4196
for (; precision > 0; precision--)
4200
else if (dp->conversion == 'e' || dp->conversion == 'E')
4203
if ((flags & FLAG_ALT) || precision > 0)
4205
*p++ = decimal_point_char ();
4206
for (; precision > 0; precision--)
4209
*p++ = dp->conversion; /* 'e' or 'E' */
4211
/* Produce the same number of exponent digits as
4212
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. */
4213
4486
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4219
else if (dp->conversion == 'g' || dp->conversion == 'G')
4222
if (flags & FLAG_ALT)
4225
(precision > 0 ? precision - 1 : 0);
4226
*p++ = decimal_point_char ();
4227
for (; ndigits > 0; --ndigits)
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)
4239
/* The generated string now extends from tmp to p, with the
4240
zero padding insertion point being at pad_ptr. */
4241
if (has_width && p - tmp < width)
4243
size_t pad = width - (p - tmp);
4244
DCHAR_T *end = p + pad;
4246
if (flags & FLAG_LEFT)
4248
/* Pad with spaces on the right. */
4249
for (; pad > 0; pad--)
4252
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4254
/* Pad with zeroes. */
4259
for (; pad > 0; pad--)
4264
/* Pad with spaces on the left. */
4269
for (; pad > 0; pad--)
4277
size_t count = p - tmp;
4279
if (count >= tmp_length)
4280
/* tmp_length was incorrectly calculated - fix the
4284
/* Make room for the result. */
4285
if (count >= allocated - length)
4287
size_t n = xsum (length, count);
4289
ENSURE_ALLOCATION (n);
4292
/* Append the result. */
4293
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4302
arg_type type = a.arg[dp->arg_index].type;
4303
int flags = dp->flags;
4304
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4308
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
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
4312
4585
#if NEED_PRINTF_UNBOUNDED_PRECISION
4315
# define prec_ourselves 0
4588
# define prec_ourselves 0
4317
4590
#if NEED_PRINTF_FLAG_LEFTADJUST
4318
# define pad_ourselves 1
4591
# define pad_ourselves 1
4319
4592
#elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4322
# define pad_ourselves 0
4595
# define pad_ourselves 0
4325
unsigned int prefix_count;
4326
int prefixes[2] IF_LINT (= { 0 });
4598
unsigned int prefix_count;
4599
int prefixes[2] IF_LINT (= { 0 });
4327
4600
#if !USE_SNPRINTF
4329
TCHAR_T tmpbuf[700];
4333
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4336
if (dp->width_start != dp->width_end)
4338
if (dp->width_arg_index != ARG_NONE)
4342
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4344
arg = a.arg[dp->width_arg_index].a.a_int;
4347
/* "A negative field width is taken as a '-' flag
4348
followed by a positive field width." */
4350
width = (unsigned int) (-arg);
4357
const FCHAR_T *digitp = dp->width_start;
4360
width = xsum (xtimes (width, 10), *digitp++ - '0');
4361
while (digitp != dp->width_end);
4367
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
4370
if (dp->precision_start != dp->precision_end)
4372
if (dp->precision_arg_index != ARG_NONE)
4376
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4378
arg = a.arg[dp->precision_arg_index].a.a_int;
4379
/* "A negative precision is taken as if the precision
4389
const FCHAR_T *digitp = dp->precision_start + 1;
4392
while (digitp != dp->precision_end)
4393
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
4399
/* Decide whether to handle the precision ourselves. */
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. */
4400
4673
#if NEED_PRINTF_UNBOUNDED_PRECISION
4401
switch (dp->conversion)
4403
case 'd': case 'i': case 'u':
4405
case 'x': case 'X': case 'p':
4406
prec_ourselves = has_precision && (precision > 0);
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);
4414
/* Decide whether to perform the padding ourselves. */
4687
/* Decide whether to perform the padding ourselves. */
4415
4688
#if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
4416
switch (dp->conversion)
4689
switch (dp->conversion)
4418
4691
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4419
/* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4420
to perform the padding after this conversion. Functions
4421
with unistdio extensions perform the padding based on
4422
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. */
4425
4698
# if NEED_PRINTF_FLAG_ZERO
4426
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':
4432
pad_ourselves = prec_ourselves;
4705
pad_ourselves = prec_ourselves;
4437
4710
#if !USE_SNPRINTF
4438
/* Allocate a temporary buffer of sufficient size for calling
4441
switch (dp->conversion)
4444
case 'd': case 'i': case 'u':
4445
# if HAVE_LONG_LONG_INT
4446
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
4448
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
4449
* 0.30103 /* binary -> decimal */
4451
+ 1; /* turn floor into ceil */
4454
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
4456
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
4457
* 0.30103 /* binary -> decimal */
4459
+ 1; /* turn floor into ceil */
4462
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
4463
* 0.30103 /* binary -> decimal */
4465
+ 1; /* turn floor into ceil */
4466
if (tmp_length < precision)
4467
tmp_length = precision;
4468
/* Multiply by 2, as an estimate for FLAG_GROUP. */
4469
tmp_length = xsum (tmp_length, tmp_length);
4470
/* Add 1, to account for a leading sign. */
4471
tmp_length = xsum (tmp_length, 1);
4475
# if HAVE_LONG_LONG_INT
4476
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
4478
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
4479
* 0.333334 /* binary -> octal */
4481
+ 1; /* turn floor into ceil */
4484
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
4486
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
4487
* 0.333334 /* binary -> octal */
4489
+ 1; /* turn floor into ceil */
4492
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
4493
* 0.333334 /* binary -> octal */
4495
+ 1; /* turn floor into ceil */
4496
if (tmp_length < precision)
4497
tmp_length = precision;
4498
/* Add 1, to account for a leading sign. */
4499
tmp_length = xsum (tmp_length, 1);
4503
# if HAVE_LONG_LONG_INT
4504
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
4506
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
4507
* 0.25 /* binary -> hexadecimal */
4509
+ 1; /* turn floor into ceil */
4512
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
4514
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
4515
* 0.25 /* binary -> hexadecimal */
4517
+ 1; /* turn floor into ceil */
4520
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
4521
* 0.25 /* binary -> hexadecimal */
4523
+ 1; /* turn floor into ceil */
4524
if (tmp_length < precision)
4525
tmp_length = precision;
4526
/* Add 2, to account for a leading sign or alternate form. */
4527
tmp_length = xsum (tmp_length, 2);
4531
if (type == TYPE_LONGDOUBLE)
4533
(unsigned int) (LDBL_MAX_EXP
4534
* 0.30103 /* binary -> decimal */
4535
* 2 /* estimate for FLAG_GROUP */
4537
+ 1 /* turn floor into ceil */
4538
+ 10; /* sign, decimal point etc. */
4541
(unsigned int) (DBL_MAX_EXP
4542
* 0.30103 /* binary -> decimal */
4543
* 2 /* estimate for FLAG_GROUP */
4545
+ 1 /* turn floor into ceil */
4546
+ 10; /* sign, decimal point etc. */
4547
tmp_length = xsum (tmp_length, precision);
4550
case 'e': case 'E': case 'g': case 'G':
4552
12; /* sign, decimal point, exponent etc. */
4553
tmp_length = xsum (tmp_length, precision);
4557
if (type == TYPE_LONGDOUBLE)
4559
(unsigned int) (LDBL_DIG
4560
* 0.831 /* decimal -> hexadecimal */
4562
+ 1; /* turn floor into ceil */
4565
(unsigned int) (DBL_DIG
4566
* 0.831 /* decimal -> hexadecimal */
4568
+ 1; /* turn floor into ceil */
4569
if (tmp_length < precision)
4570
tmp_length = precision;
4571
/* Account for sign, decimal point etc. */
4572
tmp_length = xsum (tmp_length, 12);
4576
# if HAVE_WINT_T && !WIDE_CHAR_VERSION
4577
if (type == TYPE_WIDE_CHAR)
4578
tmp_length = MB_CUR_MAX;
4586
if (type == TYPE_WIDE_STRING)
4588
# if WIDE_CHAR_VERSION
4589
/* ISO C says about %ls in fwprintf:
4590
"If the precision is not specified or is greater
4591
than the size of the array, the array shall
4592
contain a null wide character."
4593
So if there is a precision, we must not use
4595
const wchar_t *arg =
4596
a.arg[dp->arg_index].a.a_wide_string;
4599
tmp_length = local_wcsnlen (arg, precision);
4601
tmp_length = local_wcslen (arg);
4603
/* ISO C says about %ls in fprintf:
4604
"If a precision is specified, no more than that
4605
many bytes are written (including shift
4606
sequences, if any), and the array shall contain
4607
a null wide character if, to equal the
4608
multibyte character sequence length given by
4609
the precision, the function would need to
4610
access a wide character one past the end of the
4612
So if there is a precision, we must not use
4614
/* This case has already been handled above. */
4621
# if WIDE_CHAR_VERSION
4622
/* ISO C says about %s in fwprintf:
4623
"If the precision is not specified or is greater
4624
than the size of the converted array, the
4625
converted array shall contain a null wide
4627
So if there is a precision, we must not use
4629
/* This case has already been handled above. */
4632
/* ISO C says about %s in fprintf:
4633
"If the precision is not specified or greater
4634
than the size of the array, the array shall
4635
contain a null character."
4636
So if there is a precision, we must not use
4638
const char *arg = a.arg[dp->arg_index].a.a_string;
4641
tmp_length = local_strnlen (arg, precision);
4643
tmp_length = strlen (arg);
4650
(unsigned int) (sizeof (void *) * CHAR_BIT
4651
* 0.25 /* binary -> hexadecimal */
4653
+ 1 /* turn floor into ceil */
4654
+ 2; /* account for leading 0x */
4663
# if ENABLE_UNISTDIO
4664
/* Padding considers the number of characters, therefore
4665
the number of elements after padding may be
4666
> max (tmp_length, width)
4668
<= tmp_length + width. */
4669
tmp_length = xsum (tmp_length, width);
4671
/* Padding considers the number of elements,
4673
if (tmp_length < width)
4678
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
4681
if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
4685
size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
4687
if (size_overflow_p (tmp_memsize))
4688
/* Overflow, would lead to out of memory. */
4690
tmp = (TCHAR_T *) malloc (tmp_memsize);
4692
/* Out of memory. */
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. */
4697
/* Construct the format string for calling snprintf or
4734
/* Construct the format string for calling snprintf or
4701
4738
#if NEED_PRINTF_FLAG_GROUPING
4702
/* The underlying implementation doesn't support the ' flag.
4703
Produce no grouping characters in this case; this is
4704
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. */
4706
if (flags & FLAG_GROUP)
4743
if (flags & FLAG_GROUP)
4709
if (flags & FLAG_LEFT)
4711
if (flags & FLAG_SHOWSIGN)
4713
if (flags & FLAG_SPACE)
4715
if (flags & FLAG_ALT)
4719
if (flags & FLAG_ZERO)
4721
if (dp->width_start != dp->width_end)
4723
size_t n = dp->width_end - dp->width_start;
4724
/* The width specification is known to consist only
4725
of standard ASCII characters. */
4726
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4728
memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
4733
const FCHAR_T *mp = dp->width_start;
4735
*fbp++ = (unsigned char) *mp++;
4740
if (!prec_ourselves)
4742
if (dp->precision_start != dp->precision_end)
4744
size_t n = dp->precision_end - dp->precision_start;
4745
/* The precision specification is known to consist only
4746
of standard ASCII characters. */
4747
if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4749
memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
4754
const FCHAR_T *mp = dp->precision_start;
4756
*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++;
4764
4801
#if HAVE_LONG_LONG_INT
4765
case TYPE_LONGLONGINT:
4766
case TYPE_ULONGLONGINT:
4802
case TYPE_LONGLONGINT:
4803
case TYPE_ULONGLONGINT:
4767
4804
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4779
4816
#if HAVE_WINT_T
4780
case TYPE_WIDE_CHAR:
4817
case TYPE_WIDE_CHAR:
4782
4819
#if HAVE_WCHAR_T
4783
case TYPE_WIDE_STRING:
4820
case TYPE_WIDE_STRING:
4787
case TYPE_LONGDOUBLE:
4824
case TYPE_LONGDOUBLE:
4793
4830
#if NEED_PRINTF_DIRECTIVE_F
4794
if (dp->conversion == 'F')
4831
if (dp->conversion == 'F')
4798
*fbp = dp->conversion;
4835
*fbp = dp->conversion;
4799
4836
#if USE_SNPRINTF
4800
4837
# if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
4805
/* On glibc2 systems from glibc >= 2.3 - probably also older
4806
ones - we know that snprintf's returns value conforms to
4807
ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4808
Therefore we can avoid using %n in this situation.
4809
On glibc2 systems from 2004-10-18 or newer, the use of %n
4810
in format strings in writable memory may crash the program
4811
(if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4812
in this situation. */
4813
/* On native Win32 systems (such as mingw), we can avoid using
4815
- Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
4816
snprintf does not write more than the specified number
4817
of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
4818
'4', '5', '6' into buf, not '4', '5', '\0'.)
4819
- Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
4820
allows us to recognize the case of an insufficient
4821
buffer size: it returns -1 in this case.
4822
On native Win32 systems (such as mingw) where the OS is
4823
Windows Vista, the use of %n in format strings by default
4824
crashes the program. See
4825
<http://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
4826
<http://msdn2.microsoft.com/en-us/library/ms175782(VS.80).aspx>
4827
So we should avoid %n 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. */
4834
/* Construct the arguments for calling snprintf or sprintf. */
4836
if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4838
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4840
prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
4842
if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
4844
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4846
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;
4849
4886
#if USE_SNPRINTF
4850
/* The SNPRINTF result is appended after result[0..length].
4851
The latter is an array of DCHAR_T; SNPRINTF appends an
4852
array of TCHAR_T to it. This is possible because
4853
sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4854
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). */
4855
4892
# define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4856
/* Ensure that maxlen below will be >= 2. Needed on BeOS,
4857
where an snprintf() with maxlen==1 acts like sprintf(). */
4858
ENSURE_ALLOCATION (xsum (length,
4859
(2 + TCHARS_PER_DCHAR - 1)
4860
/ TCHARS_PER_DCHAR));
4861
/* Prepare checking whether snprintf returns the count
4863
*(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';
4870
4907
#if USE_SNPRINTF
4872
size_t maxlen = allocated - length;
4873
/* SNPRINTF can fail if its second argument is
4875
if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4876
maxlen = INT_MAX / TCHARS_PER_DCHAR;
4877
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;
4878
4915
# define SNPRINTF_BUF(arg) \
4879
switch (prefix_count) \
4882
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4887
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4889
prefixes[0], arg, &count); \
4892
retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4894
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, \
4901
4938
# define SNPRINTF_BUF(arg) \
4902
switch (prefix_count) \
4905
count = sprintf (tmp, buf, arg); \
4908
count = sprintf (tmp, buf, prefixes[0], arg); \
4911
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],\
4923
int arg = a.arg[dp->arg_index].a.a_schar;
4929
unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4935
int arg = a.arg[dp->arg_index].a.a_short;
4941
unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4947
int arg = a.arg[dp->arg_index].a.a_int;
4953
unsigned int arg = a.arg[dp->arg_index].a.a_uint;
4959
long int arg = a.arg[dp->arg_index].a.a_longint;
4965
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;
4969
5007
#if HAVE_LONG_LONG_INT
4970
case TYPE_LONGLONGINT:
4972
long long int arg = a.arg[dp->arg_index].a.a_longlongint;
4976
case TYPE_ULONGLONGINT:
4978
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;
4985
double arg = a.arg[dp->arg_index].a.a_double;
4989
case TYPE_LONGDOUBLE:
4991
long double arg = a.arg[dp->arg_index].a.a_longdouble;
4997
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;
5001
5039
#if HAVE_WINT_T
5002
case TYPE_WIDE_CHAR:
5004
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;
5011
const char *arg = a.arg[dp->arg_index].a.a_string;
5049
const char *arg = a.arg[dp->arg_index].a.a_string;
5015
5053
#if HAVE_WCHAR_T
5016
case TYPE_WIDE_STRING:
5018
const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
5025
void *arg = a.arg[dp->arg_index].a.a_pointer;
5034
/* Portability: Not all implementations of snprintf()
5035
are ISO C 99 compliant. Determine the number of
5036
bytes that snprintf() has produced or would have
5040
/* Verify that snprintf() has NUL-terminated its
5043
&& ((TCHAR_T *) (result + length)) [count] != '\0')
5045
/* Portability hack. */
5046
if (retcount > count)
5051
/* snprintf() doesn't understand the '%n'
5055
/* Don't use the '%n' directive; instead, look
5056
at the snprintf() return value. */
5062
/* Look at the snprintf() return value. */
5065
/* HP-UX 10.20 snprintf() is doubly deficient:
5066
It doesn't understand the '%n' directive,
5067
*and* it returns -1 (rather than the length
5068
that would have been required) when the
5069
buffer is too small. */
5070
size_t bigger_need =
5071
xsum (xtimes (allocated, 2), 12);
5072
ENSURE_ALLOCATION (bigger_need);
5081
/* Attempt to handle failure. */
5084
if (!(result == resultbuf || result == NULL))
5086
if (buf_malloced != NULL)
5087
free (buf_malloced);
5094
/* Handle overflow of the allocated buffer.
5095
If such an overflow occurs, a C99 compliant snprintf()
5096
returns a count >= maxlen. However, a non-compliant
5097
snprintf() function returns only count = maxlen - 1. To
5098
cover both cases, test whether count >= maxlen - 1. */
5099
if ((unsigned int) count + 1 >= maxlen)
5101
/* If maxlen already has attained its allowed maximum,
5102
allocating more memory will not increase maxlen.
5103
Instead of looping, bail out. */
5104
if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
5108
/* Need at least (count + 1) * sizeof (TCHAR_T)
5109
bytes. (The +1 is for the trailing NUL.)
5110
But ask for (count + 2) * sizeof (TCHAR_T)
5111
bytes, so that in the next round, we likely get
5112
maxlen > (unsigned int) count + 1
5113
and so we don't get here again.
5114
And allocate proportionally, to avoid looping
5115
eternally if snprintf() reports a too small
5119
((unsigned int) count + 2
5120
+ TCHARS_PER_DCHAR - 1)
5121
/ TCHARS_PER_DCHAR),
5122
xtimes (allocated, 2));
5124
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);
5130
5206
#if NEED_PRINTF_UNBOUNDED_PRECISION
5133
/* Handle the precision. */
5209
/* Handle the precision. */
5135
5211
# if USE_SNPRINTF
5136
(TCHAR_T *) (result + length);
5212
(TCHAR_T *) (result + length);
5140
size_t prefix_count;
5144
/* Put the additional zeroes after the sign. */
5146
&& (*prec_ptr == '-' || *prec_ptr == '+'
5147
|| *prec_ptr == ' '))
5149
/* Put the additional zeroes after the 0x prefix if
5150
(flags & FLAG_ALT) || (dp->conversion == 'p'). */
5152
&& prec_ptr[0] == '0'
5153
&& (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
5156
move = count - prefix_count;
5157
if (precision > move)
5159
/* Insert zeroes. */
5160
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;
5163
5239
# if USE_SNPRINTF
5166
(count + insert + TCHARS_PER_DCHAR - 1)
5167
/ TCHARS_PER_DCHAR);
5168
length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5169
ENSURE_ALLOCATION (n);
5170
length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
5171
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);
5174
prec_end = prec_ptr + count;
5175
prec_ptr += prefix_count;
5177
while (prec_end > prec_ptr)
5180
prec_end[insert] = prec_end[0];
5186
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);
5193
5269
#if !USE_SNPRINTF
5194
if (count >= tmp_length)
5195
/* tmp_length was incorrectly calculated - fix the
5270
if (count >= tmp_length)
5271
/* tmp_length was incorrectly calculated - fix the
5200
5276
#if !DCHAR_IS_TCHAR
5201
/* Convert from TCHAR_T[] to DCHAR_T[]. */
5202
if (dp->conversion == 'c' || dp->conversion == 's')
5204
/* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
5206
The result string is not certainly ASCII. */
5207
const TCHAR_T *tmpsrc;
5210
/* This code assumes that TCHAR_T is 'char'. */
5211
typedef int TCHAR_T_verify
5212
[2 * (sizeof (TCHAR_T) == 1) - 1];
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];
5213
5289
# if USE_SNPRINTF
5214
tmpsrc = (TCHAR_T *) (result + length);
5290
tmpsrc = (TCHAR_T *) (result + length);
5219
DCHAR_CONV_FROM_ENCODING (locale_charset (),
5220
iconveh_question_mark,
5226
int saved_errno = errno;
5227
if (!(result == resultbuf || result == NULL))
5229
if (buf_malloced != NULL)
5230
free (buf_malloced);
5232
errno = saved_errno;
5235
ENSURE_ALLOCATION (xsum (length, tmpdst_len));
5236
DCHAR_CPY (result + length, tmpdst, tmpdst_len);
5242
/* The result string is ASCII.
5243
Simple 1:1 conversion. */
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. */
5244
5320
# if USE_SNPRINTF
5245
/* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
5246
no-op conversion, in-place on the array starting
5247
at (result + length). */
5248
if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
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))
5251
const TCHAR_T *tmpsrc;
5327
const TCHAR_T *tmpsrc;
5255
5331
# if USE_SNPRINTF
5256
if (result == resultbuf)
5258
tmpsrc = (TCHAR_T *) (result + length);
5259
/* ENSURE_ALLOCATION will not move tmpsrc
5260
(because it's part of resultbuf). */
5261
ENSURE_ALLOCATION (xsum (length, count));
5265
/* ENSURE_ALLOCATION will move the array
5266
(because it uses realloc(). */
5267
ENSURE_ALLOCATION (xsum (length, count));
5268
tmpsrc = (TCHAR_T *) (result + length);
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);
5272
ENSURE_ALLOCATION (xsum (length, count));
5348
ENSURE_ALLOCATION (xsum (length, count));
5274
tmpdst = result + length;
5275
/* Copy backwards, because of overlapping. */
5278
for (n = count; n > 0; n--)
5279
*--tmpdst = (unsigned char) *--tmpsrc;
5350
tmpdst = result + length;
5351
/* Copy backwards, because of overlapping. */
5354
for (n = count; n > 0; n--)
5355
*--tmpdst = (unsigned char) *--tmpsrc;
5284
5360
#if DCHAR_IS_TCHAR && !USE_SNPRINTF
5285
/* Make room for the result. */
5286
if (count > allocated - length)
5288
/* Need at least count elements. But allocate
5291
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));
5293
ENSURE_ALLOCATION (n);
5369
ENSURE_ALLOCATION (n);
5297
/* Here count <= allocated - length. */
5373
/* Here count <= allocated - length. */
5299
/* Perform padding. */
5375
/* Perform padding. */
5300
5376
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
5301
if (pad_ourselves && has_width)
5377
if (pad_ourselves && has_width)
5304
5380
# if ENABLE_UNISTDIO
5305
/* Outside POSIX, it's preferrable to compare the width
5306
against the number of _characters_ of the converted
5308
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);
5310
/* The width is compared against the number of _bytes_
5311
of the converted value, says POSIX. */
5386
/* The width is compared against the number of _bytes_
5387
of the converted value, says POSIX. */
5316
size_t pad = width - w;
5392
size_t pad = width - w;
5318
/* Make room for the result. */
5319
if (xsum (count, pad) > allocated - length)
5321
/* Need at least count + pad elements. But
5322
allocate proportionally. */
5324
xmax (xsum3 (length, count, pad),
5325
xtimes (allocated, 2));
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));
5327
5403
# if USE_SNPRINTF
5329
ENSURE_ALLOCATION (n);
5405
ENSURE_ALLOCATION (n);
5332
ENSURE_ALLOCATION (n);
5408
ENSURE_ALLOCATION (n);
5335
/* Here count + pad <= allocated - length. */
5411
/* Here count + pad <= allocated - length. */
5338
5414
# if !DCHAR_IS_TCHAR || USE_SNPRINTF
5339
DCHAR_T * const rp = result + length;
5415
DCHAR_T * const rp = result + length;
5341
DCHAR_T * const rp = tmp;
5417
DCHAR_T * const rp = tmp;
5343
DCHAR_T *p = rp + count;
5344
DCHAR_T *end = p + pad;
5419
DCHAR_T *p = rp + count;
5420
DCHAR_T *end = p + pad;
5346
5422
# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
5347
if (dp->conversion == 'c'
5348
|| dp->conversion == 's')
5349
/* No zero-padding for string directives. */
5423
if (dp->conversion == 'c'
5424
|| dp->conversion == 's')
5425
/* No zero-padding for string directives. */
5354
pad_ptr = (*rp == '-' ? rp + 1 : rp);
5355
/* No zero-padding of "inf" and "nan". */
5356
if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
5357
|| (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
5360
/* The generated string now extends from rp to p,
5361
with the zero padding insertion point being at
5364
count = count + pad; /* = end - rp */
5366
if (flags & FLAG_LEFT)
5368
/* Pad with spaces on the right. */
5369
for (; pad > 0; pad--)
5372
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
5374
/* Pad with zeroes. */
5379
for (; pad > 0; pad--)
5384
/* Pad with spaces on the left. */
5389
for (; pad > 0; pad--)
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--)
5397
/* Here still count <= allocated - length. */
5473
/* Here still count <= allocated - length. */
5399
5475
#if !DCHAR_IS_TCHAR || USE_SNPRINTF
5400
/* The snprintf() result did fit. */
5476
/* The snprintf() result did fit. */
5402
/* Append the sprintf() result. */
5403
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
5478
/* Append the sprintf() result. */
5479
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
5405
5481
#if !USE_SNPRINTF
5410
5486
#if NEED_PRINTF_DIRECTIVE_F
5411
if (dp->conversion == 'F')
5413
/* Convert the %f result to upper case for %F. */
5414
DCHAR_T *rp = result + length;
5416
for (rc = count; rc > 0; rc--, rp++)
5417
if (*rp >= 'a' && *rp <= 'z')
5418
*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
5429
5507
/* Add the final NUL. */