1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
19 This must come before <config.h> because <config.h> may include
20 <features.h>, and once <features.h> has been included, it's too late. */
22 # define _GNU_SOURCE 1
32 # include "vasnwprintf.h"
34 # include "vasnprintf.h"
37 #include <locale.h> /* localeconv() */
38 #include <stdio.h> /* snprintf(), sprintf() */
39 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
40 #include <string.h> /* memcpy(), strlen() */
41 #include <errno.h> /* errno */
42 #include <limits.h> /* CHAR_BIT */
43 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
45 # include <langinfo.h>
48 # include "wprintf-parse.h"
50 # include "printf-parse.h"
53 /* Checked size_t computations. */
56 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
59 # include "printf-frexp.h"
61 # include "isnanl-nolibm.h"
62 # include "printf-frexpl.h"
67 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
69 # define EOVERFLOW E2BIG
74 # define local_wcslen wcslen
76 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
77 a dependency towards this library, here is a local substitute.
78 Define this substitute only once, even if this file is included
79 twice in the same compilation unit. */
80 # ifndef local_wcslen_defined
81 # define local_wcslen_defined 1
83 local_wcslen (const wchar_t *s)
87 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
96 # define VASNPRINTF vasnwprintf
97 # define CHAR_T wchar_t
98 # define DIRECTIVE wchar_t_directive
99 # define DIRECTIVES wchar_t_directives
100 # define PRINTF_PARSE wprintf_parse
101 # define USE_SNPRINTF 1
102 # if HAVE_DECL__SNWPRINTF
103 /* On Windows, the function swprintf() has a different signature than
104 on Unix; we use the _snwprintf() function instead. */
105 # define SNPRINTF _snwprintf
108 # define SNPRINTF swprintf
111 # define VASNPRINTF vasnprintf
113 # define DIRECTIVE char_directive
114 # define DIRECTIVES char_directives
115 # define PRINTF_PARSE printf_parse
116 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
117 # if HAVE_DECL__SNPRINTF
119 # define SNPRINTF _snprintf
122 # define SNPRINTF snprintf
123 /* Here we need to call the native snprintf, not rpl_snprintf. */
127 /* Here we need to call the native sprintf, not rpl_sprintf. */
130 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
131 /* Determine the decimal-point character according to the current locale. */
132 # ifndef decimal_point_char_defined
133 # define decimal_point_char_defined 1
135 decimal_point_char ()
138 /* Determine it in a multithread-safe way. We know nl_langinfo is
139 multithread-safe on glibc systems, but is not required to be multithread-
140 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
141 is rarely multithread-safe. */
142 # if HAVE_NL_LANGINFO && __GLIBC__
143 point = nl_langinfo (RADIXCHAR);
146 sprintf (pointbuf, "%#.0f", 1.0);
147 point = &pointbuf[1];
149 point = localeconv () -> decimal_point;
151 /* The decimal point is always a single byte: either '.' or ','. */
152 return (point[0] != '\0' ? point[0] : '.');
158 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
163 if (PRINTF_PARSE (format, &d, &a) < 0)
174 if (printf_fetchargs (args, &a) < 0)
182 size_t buf_neededlength;
184 CHAR_T *buf_malloced;
188 /* Output string accumulator. */
193 /* Allocate a small buffer that will hold a directive passed to
194 sprintf or snprintf. */
196 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
198 if (buf_neededlength < 4000 / sizeof (CHAR_T))
200 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
206 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
207 if (size_overflow_p (buf_memsize))
208 goto out_of_memory_1;
209 buf = (CHAR_T *) malloc (buf_memsize);
211 goto out_of_memory_1;
215 if (resultbuf != NULL)
218 allocated = *lengthp;
227 result is either == resultbuf or == NULL or malloc-allocated.
228 If length > 0, then result != NULL. */
230 /* Ensures that allocated >= needed. Aborts through a jump to
231 out_of_memory if needed is SIZE_MAX or otherwise too big. */
232 #define ENSURE_ALLOCATION(needed) \
233 if ((needed) > allocated) \
235 size_t memory_size; \
238 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
239 if ((needed) > allocated) \
240 allocated = (needed); \
241 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
242 if (size_overflow_p (memory_size)) \
243 goto out_of_memory; \
244 if (result == resultbuf || result == NULL) \
245 memory = (CHAR_T *) malloc (memory_size); \
247 memory = (CHAR_T *) realloc (result, memory_size); \
248 if (memory == NULL) \
249 goto out_of_memory; \
250 if (result == resultbuf && length > 0) \
251 memcpy (memory, result, length * sizeof (CHAR_T)); \
255 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
257 if (cp != dp->dir_start)
259 size_t n = dp->dir_start - cp;
260 size_t augmented_length = xsum (length, n);
262 ENSURE_ALLOCATION (augmented_length);
263 memcpy (result + length, cp, n * sizeof (CHAR_T));
264 length = augmented_length;
269 /* Execute a single directive. */
270 if (dp->conversion == '%')
272 size_t augmented_length;
274 if (!(dp->arg_index == ARG_NONE))
276 augmented_length = xsum (length, 1);
277 ENSURE_ALLOCATION (augmented_length);
278 result[length] = '%';
279 length = augmented_length;
283 if (!(dp->arg_index != ARG_NONE))
286 if (dp->conversion == 'n')
288 switch (a.arg[dp->arg_index].type)
290 case TYPE_COUNT_SCHAR_POINTER:
291 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
293 case TYPE_COUNT_SHORT_POINTER:
294 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
296 case TYPE_COUNT_INT_POINTER:
297 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
299 case TYPE_COUNT_LONGINT_POINTER:
300 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
302 #if HAVE_LONG_LONG_INT
303 case TYPE_COUNT_LONGLONGINT_POINTER:
304 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
311 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
312 else if (dp->conversion == 'a' || dp->conversion == 'A')
314 arg_type type = a.arg[dp->arg_index].type;
315 int flags = dp->flags;
328 if (dp->width_start != dp->width_end)
330 if (dp->width_arg_index != ARG_NONE)
334 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
336 arg = a.arg[dp->width_arg_index].a.a_int;
339 /* "A negative field width is taken as a '-' flag
340 followed by a positive field width." */
342 width = (unsigned int) (-arg);
349 const CHAR_T *digitp = dp->width_start;
352 width = xsum (xtimes (width, 10), *digitp++ - '0');
353 while (digitp != dp->width_end);
360 if (dp->precision_start != dp->precision_end)
362 if (dp->precision_arg_index != ARG_NONE)
366 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
368 arg = a.arg[dp->precision_arg_index].a.a_int;
369 /* "A negative precision is taken as if the precision
379 const CHAR_T *digitp = dp->precision_start + 1;
382 while (digitp != dp->precision_end)
383 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
388 /* Allocate a temporary buffer of sufficient size. */
389 # if HAVE_LONG_DOUBLE
390 if (type == TYPE_LONGDOUBLE)
392 (unsigned int) ((LDBL_DIG + 1)
393 * 0.831 /* decimal -> hexadecimal */
395 + 1; /* turn floor into ceil */
399 (unsigned int) ((DBL_DIG + 1)
400 * 0.831 /* decimal -> hexadecimal */
402 + 1; /* turn floor into ceil */
403 if (tmp_length < precision)
404 tmp_length = precision;
405 /* Account for sign, decimal point etc. */
406 tmp_length = xsum (tmp_length, 12);
408 if (tmp_length < width)
411 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
413 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
417 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
419 if (size_overflow_p (tmp_memsize))
420 /* Overflow, would lead to out of memory. */
422 tmp = (CHAR_T *) malloc (tmp_memsize);
430 # if HAVE_LONG_DOUBLE
431 if (type == TYPE_LONGDOUBLE)
433 long double arg = a.arg[dp->arg_index].a.a_longdouble;
437 if (dp->conversion == 'A')
439 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
443 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
449 DECL_LONG_DOUBLE_ROUNDING
451 BEGIN_LONG_DOUBLE_ROUNDING ();
458 else if (arg == 0.0L)
460 /* Distinguish 0.0L and -0.0L. */
461 static long double plus_zero = 0.0L;
462 long double arg_mem = arg;
463 if (memcmp (&plus_zero, &arg_mem, SIZEOF_LDBL) != 0)
472 else if (flags & FLAG_SHOWSIGN)
474 else if (flags & FLAG_SPACE)
477 if (arg > 0.0L && arg + arg == arg)
479 if (dp->conversion == 'A')
481 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
485 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
491 long double mantissa;
494 mantissa = printf_frexpl (arg, &exponent);
502 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
504 /* Round the mantissa. */
505 long double tail = mantissa;
508 for (q = precision; ; q--)
510 int digit = (int) tail;
514 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
523 for (q = precision; q > 0; q--)
529 *p++ = dp->conversion - 'A' + 'X';
534 digit = (int) mantissa;
537 if ((flags & FLAG_ALT)
538 || mantissa > 0.0L || precision > 0)
540 *p++ = decimal_point_char ();
541 /* This loop terminates because we assume
542 that FLT_RADIX is a power of 2. */
543 while (mantissa > 0.0L)
546 digit = (int) mantissa;
551 : dp->conversion - 10);
555 while (precision > 0)
562 *p++ = dp->conversion - 'A' + 'P';
563 # if WIDE_CHAR_VERSION
565 static const wchar_t decimal_format[] =
566 { '%', '+', 'd', '\0' };
567 SNPRINTF (p, 6 + 1, decimal_format, exponent);
570 sprintf (p, "%+d", exponent);
576 END_LONG_DOUBLE_ROUNDING ();
582 double arg = a.arg[dp->arg_index].a.a_double;
586 if (dp->conversion == 'A')
588 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
592 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
606 /* Distinguish 0.0 and -0.0. */
607 static double plus_zero = 0.0;
608 double arg_mem = arg;
609 if (memcmp (&plus_zero, &arg_mem, SIZEOF_DBL) != 0)
618 else if (flags & FLAG_SHOWSIGN)
620 else if (flags & FLAG_SPACE)
623 if (arg > 0.0 && arg + arg == arg)
625 if (dp->conversion == 'A')
627 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
631 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
640 mantissa = printf_frexp (arg, &exponent);
648 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
650 /* Round the mantissa. */
651 double tail = mantissa;
654 for (q = precision; ; q--)
656 int digit = (int) tail;
660 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
669 for (q = precision; q > 0; q--)
675 *p++ = dp->conversion - 'A' + 'X';
680 digit = (int) mantissa;
683 if ((flags & FLAG_ALT)
684 || mantissa > 0.0 || precision > 0)
686 *p++ = decimal_point_char ();
687 /* This loop terminates because we assume
688 that FLT_RADIX is a power of 2. */
689 while (mantissa > 0.0)
692 digit = (int) mantissa;
697 : dp->conversion - 10);
701 while (precision > 0)
708 *p++ = dp->conversion - 'A' + 'P';
709 # if WIDE_CHAR_VERSION
711 static const wchar_t decimal_format[] =
712 { '%', '+', 'd', '\0' };
713 SNPRINTF (p, 6 + 1, decimal_format, exponent);
716 sprintf (p, "%+d", exponent);
723 /* The generated string now extends from tmp to p, with the
724 zero padding insertion point being at pad_ptr. */
725 if (has_width && p - tmp < width)
727 size_t pad = width - (p - tmp);
728 CHAR_T *end = p + pad;
730 if (flags & FLAG_LEFT)
732 /* Pad with spaces on the right. */
733 for (; pad > 0; pad--)
736 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
738 /* Pad with zeroes. */
743 for (; pad > 0; pad--)
748 /* Pad with spaces on the left. */
753 for (; pad > 0; pad--)
761 size_t count = p - tmp;
763 if (count >= tmp_length)
764 /* tmp_length was incorrectly calculated - fix the
768 /* Make room for the result. */
769 if (count >= allocated - length)
771 size_t n = xsum (length, count);
773 ENSURE_ALLOCATION (n);
776 /* Append the result. */
777 memcpy (result + length, tmp, count * sizeof (CHAR_T));
786 arg_type type = a.arg[dp->arg_index].type;
788 unsigned int prefix_count;
795 /* Allocate a temporary buffer of sufficient size for calling
802 if (dp->width_start != dp->width_end)
804 if (dp->width_arg_index != ARG_NONE)
808 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
810 arg = a.arg[dp->width_arg_index].a.a_int;
811 width = (arg < 0 ? (unsigned int) (-arg) : arg);
815 const CHAR_T *digitp = dp->width_start;
818 width = xsum (xtimes (width, 10), *digitp++ - '0');
819 while (digitp != dp->width_end);
824 if (dp->precision_start != dp->precision_end)
826 if (dp->precision_arg_index != ARG_NONE)
830 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
832 arg = a.arg[dp->precision_arg_index].a.a_int;
833 precision = (arg < 0 ? 0 : arg);
837 const CHAR_T *digitp = dp->precision_start + 1;
840 while (digitp != dp->precision_end)
841 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
845 switch (dp->conversion)
848 case 'd': case 'i': case 'u':
849 # if HAVE_LONG_LONG_INT
850 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
852 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
853 * 0.30103 /* binary -> decimal */
855 + 1; /* turn floor into ceil */
858 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
860 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
861 * 0.30103 /* binary -> decimal */
863 + 1; /* turn floor into ceil */
866 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
867 * 0.30103 /* binary -> decimal */
869 + 1; /* turn floor into ceil */
870 if (tmp_length < precision)
871 tmp_length = precision;
872 /* Multiply by 2, as an estimate for FLAG_GROUP. */
873 tmp_length = xsum (tmp_length, tmp_length);
874 /* Add 1, to account for a leading sign. */
875 tmp_length = xsum (tmp_length, 1);
879 # if HAVE_LONG_LONG_INT
880 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
882 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
883 * 0.333334 /* binary -> octal */
885 + 1; /* turn floor into ceil */
888 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
890 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
891 * 0.333334 /* binary -> octal */
893 + 1; /* turn floor into ceil */
896 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
897 * 0.333334 /* binary -> octal */
899 + 1; /* turn floor into ceil */
900 if (tmp_length < precision)
901 tmp_length = precision;
902 /* Add 1, to account for a leading sign. */
903 tmp_length = xsum (tmp_length, 1);
907 # if HAVE_LONG_LONG_INT
908 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
910 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
911 * 0.25 /* binary -> hexadecimal */
913 + 1; /* turn floor into ceil */
916 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
918 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
919 * 0.25 /* binary -> hexadecimal */
921 + 1; /* turn floor into ceil */
924 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
925 * 0.25 /* binary -> hexadecimal */
927 + 1; /* turn floor into ceil */
928 if (tmp_length < precision)
929 tmp_length = precision;
930 /* Add 2, to account for a leading sign or alternate form. */
931 tmp_length = xsum (tmp_length, 2);
935 # if HAVE_LONG_DOUBLE
936 if (type == TYPE_LONGDOUBLE)
938 (unsigned int) (LDBL_MAX_EXP
939 * 0.30103 /* binary -> decimal */
940 * 2 /* estimate for FLAG_GROUP */
942 + 1 /* turn floor into ceil */
943 + 10; /* sign, decimal point etc. */
947 (unsigned int) (DBL_MAX_EXP
948 * 0.30103 /* binary -> decimal */
949 * 2 /* estimate for FLAG_GROUP */
951 + 1 /* turn floor into ceil */
952 + 10; /* sign, decimal point etc. */
953 tmp_length = xsum (tmp_length, precision);
956 case 'e': case 'E': case 'g': case 'G':
958 12; /* sign, decimal point, exponent etc. */
959 tmp_length = xsum (tmp_length, precision);
963 # if HAVE_LONG_DOUBLE
964 if (type == TYPE_LONGDOUBLE)
966 (unsigned int) (LDBL_DIG
967 * 0.831 /* decimal -> hexadecimal */
969 + 1; /* turn floor into ceil */
973 (unsigned int) (DBL_DIG
974 * 0.831 /* decimal -> hexadecimal */
976 + 1; /* turn floor into ceil */
977 if (tmp_length < precision)
978 tmp_length = precision;
979 /* Account for sign, decimal point etc. */
980 tmp_length = xsum (tmp_length, 12);
984 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
985 if (type == TYPE_WIDE_CHAR)
986 tmp_length = MB_CUR_MAX;
994 if (type == TYPE_WIDE_STRING)
997 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
999 # if !WIDE_CHAR_VERSION
1000 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
1005 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
1010 (unsigned int) (sizeof (void *) * CHAR_BIT
1011 * 0.25 /* binary -> hexadecimal */
1013 + 1 /* turn floor into ceil */
1014 + 2; /* account for leading 0x */
1021 if (tmp_length < width)
1024 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1027 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
1031 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1033 if (size_overflow_p (tmp_memsize))
1034 /* Overflow, would lead to out of memory. */
1036 tmp = (CHAR_T *) malloc (tmp_memsize);
1038 /* Out of memory. */
1043 /* Construct the format string for calling snprintf or
1047 if (dp->flags & FLAG_GROUP)
1049 if (dp->flags & FLAG_LEFT)
1051 if (dp->flags & FLAG_SHOWSIGN)
1053 if (dp->flags & FLAG_SPACE)
1055 if (dp->flags & FLAG_ALT)
1057 if (dp->flags & FLAG_ZERO)
1059 if (dp->width_start != dp->width_end)
1061 size_t n = dp->width_end - dp->width_start;
1062 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1065 if (dp->precision_start != dp->precision_end)
1067 size_t n = dp->precision_end - dp->precision_start;
1068 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1074 #if HAVE_LONG_LONG_INT
1075 case TYPE_LONGLONGINT:
1076 case TYPE_ULONGLONGINT:
1083 case TYPE_WIDE_CHAR:
1086 case TYPE_WIDE_STRING:
1090 #if HAVE_LONG_DOUBLE
1091 case TYPE_LONGDOUBLE:
1098 *p = dp->conversion;
1107 /* Construct the arguments for calling snprintf or sprintf. */
1109 if (dp->width_arg_index != ARG_NONE)
1111 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1113 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1115 if (dp->precision_arg_index != ARG_NONE)
1117 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1119 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1123 /* Prepare checking whether snprintf returns the count
1125 ENSURE_ALLOCATION (xsum (length, 1));
1126 result[length] = '\0';
1135 maxlen = allocated - length;
1140 /* SNPRINTF can fail if maxlen > INT_MAX. */
1141 if (maxlen > INT_MAX)
1143 # define SNPRINTF_BUF(arg) \
1144 switch (prefix_count) \
1147 retcount = SNPRINTF (result + length, maxlen, buf, \
1151 retcount = SNPRINTF (result + length, maxlen, buf, \
1152 prefixes[0], arg, &count); \
1155 retcount = SNPRINTF (result + length, maxlen, buf, \
1156 prefixes[0], prefixes[1], arg, \
1163 # define SNPRINTF_BUF(arg) \
1164 switch (prefix_count) \
1167 count = sprintf (tmp, buf, arg); \
1170 count = sprintf (tmp, buf, prefixes[0], arg); \
1173 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1185 int arg = a.arg[dp->arg_index].a.a_schar;
1191 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1197 int arg = a.arg[dp->arg_index].a.a_short;
1203 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1209 int arg = a.arg[dp->arg_index].a.a_int;
1215 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1221 long int arg = a.arg[dp->arg_index].a.a_longint;
1227 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1231 #if HAVE_LONG_LONG_INT
1232 case TYPE_LONGLONGINT:
1234 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1238 case TYPE_ULONGLONGINT:
1240 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1247 double arg = a.arg[dp->arg_index].a.a_double;
1251 #if HAVE_LONG_DOUBLE
1252 case TYPE_LONGDOUBLE:
1254 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1261 int arg = a.arg[dp->arg_index].a.a_char;
1266 case TYPE_WIDE_CHAR:
1268 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1275 const char *arg = a.arg[dp->arg_index].a.a_string;
1280 case TYPE_WIDE_STRING:
1282 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1289 void *arg = a.arg[dp->arg_index].a.a_pointer;
1298 /* Portability: Not all implementations of snprintf()
1299 are ISO C 99 compliant. Determine the number of
1300 bytes that snprintf() has produced or would have
1304 /* Verify that snprintf() has NUL-terminated its
1306 if (count < maxlen && result[length + count] != '\0')
1308 /* Portability hack. */
1309 if (retcount > count)
1314 /* snprintf() doesn't understand the '%n'
1318 /* Don't use the '%n' directive; instead, look
1319 at the snprintf() return value. */
1325 /* Look at the snprintf() return value. */
1328 /* HP-UX 10.20 snprintf() is doubly deficient:
1329 It doesn't understand the '%n' directive,
1330 *and* it returns -1 (rather than the length
1331 that would have been required) when the
1332 buffer is too small. */
1333 size_t bigger_need =
1334 xsum (xtimes (allocated, 2), 12);
1335 ENSURE_ALLOCATION (bigger_need);
1344 /* Attempt to handle failure. */
1347 if (!(result == resultbuf || result == NULL))
1349 if (buf_malloced != NULL)
1350 free (buf_malloced);
1357 if (count >= tmp_length)
1358 /* tmp_length was incorrectly calculated - fix the
1363 /* Make room for the result. */
1364 if (count >= maxlen)
1366 /* Need at least count bytes. But allocate
1367 proportionally, to avoid looping eternally if
1368 snprintf() reports a too small count. */
1370 xmax (xsum (length, count), xtimes (allocated, 2));
1372 ENSURE_ALLOCATION (n);
1379 /* The snprintf() result did fit. */
1381 /* Append the sprintf() result. */
1382 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1394 /* Add the final NUL. */
1395 ENSURE_ALLOCATION (xsum (length, 1));
1396 result[length] = '\0';
1398 if (result != resultbuf && length + 1 < allocated)
1400 /* Shrink the allocated memory if possible. */
1403 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1408 if (buf_malloced != NULL)
1409 free (buf_malloced);
1412 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1413 says that snprintf() fails with errno = EOVERFLOW in this case, but
1414 that's only because snprintf() returns an 'int'. This function does
1415 not have this limitation. */
1419 if (!(result == resultbuf || result == NULL))
1421 if (buf_malloced != NULL)
1422 free (buf_malloced);
1428 if (!(result == resultbuf || result == NULL))
1430 if (buf_malloced != NULL)
1431 free (buf_malloced);