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 "wprintf-parse.h"
47 # include "printf-parse.h"
50 /* Checked size_t computations. */
53 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
57 # include "printf-frexp.h"
58 # include "printf-frexpl.h"
64 # define local_wcslen wcslen
66 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
67 a dependency towards this library, here is a local substitute.
68 Define this substitute only once, even if this file is included
69 twice in the same compilation unit. */
70 # ifndef local_wcslen_defined
71 # define local_wcslen_defined 1
73 local_wcslen (const wchar_t *s)
77 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
86 # define VASNPRINTF vasnwprintf
87 # define CHAR_T wchar_t
88 # define DIRECTIVE wchar_t_directive
89 # define DIRECTIVES wchar_t_directives
90 # define PRINTF_PARSE wprintf_parse
91 # define USE_SNPRINTF 1
92 # if HAVE_DECL__SNWPRINTF
93 /* On Windows, the function swprintf() has a different signature than
94 on Unix; we use the _snwprintf() function instead. */
95 # define SNPRINTF _snwprintf
98 # define SNPRINTF swprintf
101 # define VASNPRINTF vasnprintf
103 # define DIRECTIVE char_directive
104 # define DIRECTIVES char_directives
105 # define PRINTF_PARSE printf_parse
106 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
107 # if HAVE_DECL__SNPRINTF
109 # define SNPRINTF _snprintf
112 # define SNPRINTF snprintf
117 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
122 if (PRINTF_PARSE (format, &d, &a) < 0)
133 if (printf_fetchargs (args, &a) < 0)
141 size_t buf_neededlength;
143 CHAR_T *buf_malloced;
147 /* Output string accumulator. */
152 /* Allocate a small buffer that will hold a directive passed to
153 sprintf or snprintf. */
155 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
157 if (buf_neededlength < 4000 / sizeof (CHAR_T))
159 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
165 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
166 if (size_overflow_p (buf_memsize))
167 goto out_of_memory_1;
168 buf = (CHAR_T *) malloc (buf_memsize);
170 goto out_of_memory_1;
174 if (resultbuf != NULL)
177 allocated = *lengthp;
186 result is either == resultbuf or == NULL or malloc-allocated.
187 If length > 0, then result != NULL. */
189 /* Ensures that allocated >= needed. Aborts through a jump to
190 out_of_memory if needed is SIZE_MAX or otherwise too big. */
191 #define ENSURE_ALLOCATION(needed) \
192 if ((needed) > allocated) \
194 size_t memory_size; \
197 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
198 if ((needed) > allocated) \
199 allocated = (needed); \
200 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
201 if (size_overflow_p (memory_size)) \
202 goto out_of_memory; \
203 if (result == resultbuf || result == NULL) \
204 memory = (CHAR_T *) malloc (memory_size); \
206 memory = (CHAR_T *) realloc (result, memory_size); \
207 if (memory == NULL) \
208 goto out_of_memory; \
209 if (result == resultbuf && length > 0) \
210 memcpy (memory, result, length * sizeof (CHAR_T)); \
214 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
216 if (cp != dp->dir_start)
218 size_t n = dp->dir_start - cp;
219 size_t augmented_length = xsum (length, n);
221 ENSURE_ALLOCATION (augmented_length);
222 memcpy (result + length, cp, n * sizeof (CHAR_T));
223 length = augmented_length;
228 /* Execute a single directive. */
229 if (dp->conversion == '%')
231 size_t augmented_length;
233 if (!(dp->arg_index == ARG_NONE))
235 augmented_length = xsum (length, 1);
236 ENSURE_ALLOCATION (augmented_length);
237 result[length] = '%';
238 length = augmented_length;
242 if (!(dp->arg_index != ARG_NONE))
245 if (dp->conversion == 'n')
247 switch (a.arg[dp->arg_index].type)
249 case TYPE_COUNT_SCHAR_POINTER:
250 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
252 case TYPE_COUNT_SHORT_POINTER:
253 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
255 case TYPE_COUNT_INT_POINTER:
256 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
258 case TYPE_COUNT_LONGINT_POINTER:
259 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
261 #if HAVE_LONG_LONG_INT
262 case TYPE_COUNT_LONGLONGINT_POINTER:
263 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
270 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
271 else if (dp->conversion == 'a' || dp->conversion == 'A')
273 arg_type type = a.arg[dp->arg_index].type;
274 int flags = dp->flags;
287 if (dp->width_start != dp->width_end)
289 if (dp->width_arg_index != ARG_NONE)
293 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
295 arg = a.arg[dp->width_arg_index].a.a_int;
298 /* "A negative field width is taken as a '-' flag
299 followed by a positive field width." */
301 width = (unsigned int) (-arg);
308 const CHAR_T *digitp = dp->width_start;
311 width = xsum (xtimes (width, 10), *digitp++ - '0');
312 while (digitp != dp->width_end);
319 if (dp->precision_start != dp->precision_end)
321 if (dp->precision_arg_index != ARG_NONE)
325 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
327 arg = a.arg[dp->precision_arg_index].a.a_int;
328 /* "A negative precision is taken as if the precision
338 const CHAR_T *digitp = dp->precision_start + 1;
341 while (digitp != dp->precision_end)
342 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
347 /* Allocate a temporary buffer of sufficient size. */
348 # if HAVE_LONG_DOUBLE
349 if (type == TYPE_LONGDOUBLE)
351 (unsigned int) ((LDBL_DIG + 1)
352 * 0.831 /* decimal -> hexadecimal */
354 + 1; /* turn floor into ceil */
358 (unsigned int) ((DBL_DIG + 1)
359 * 0.831 /* decimal -> hexadecimal */
361 + 1; /* turn floor into ceil */
362 if (tmp_length < precision)
363 tmp_length = precision;
364 /* Account for sign, decimal point etc. */
365 tmp_length = xsum (tmp_length, 12);
367 if (tmp_length < width)
370 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
372 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
376 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
378 if (size_overflow_p (tmp_memsize))
379 /* Overflow, would lead to out of memory. */
381 tmp = (CHAR_T *) malloc (tmp_memsize);
389 # if HAVE_LONG_DOUBLE
390 if (type == TYPE_LONGDOUBLE)
392 long double arg = a.arg[dp->arg_index].a.a_longdouble;
396 if (dp->conversion == 'A')
398 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
402 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
414 else if (arg == 0.0L)
416 /* Distinguish 0.0L and -0.0L. */
417 static long double plus_zero = 0.0L;
418 long double arg_mem = arg;
419 if (memcmp (&plus_zero, &arg_mem, sizeof (long double)) != 0)
428 else if (flags & FLAG_SHOWSIGN)
430 else if (flags & FLAG_SPACE)
433 if (arg > 0.0L && arg + arg == arg)
435 if (dp->conversion == 'A')
437 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
441 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
447 long double mantissa;
450 mantissa = printf_frexpl (arg, &exponent);
458 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
460 /* Round the mantissa. */
461 long double tail = mantissa;
464 for (q = precision; ; q--)
466 int digit = (int) tail;
470 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
479 for (q = precision; q > 0; q--)
485 *p++ = dp->conversion - 'A' + 'X';
490 digit = (int) mantissa;
493 if ((flags & FLAG_ALT)
494 || mantissa > 0.0L || precision > 0)
497 localeconv () -> decimal_point;
498 /* The decimal point is always a single byte:
499 either '.' or ','. */
500 *p++ = (point[0] != '\0' ? point[0] : '.');
501 /* This loop terminates because we assume
502 that FLT_RADIX is a power of 2. */
503 while (mantissa > 0.0L)
506 digit = (int) mantissa;
511 : dp->conversion - 10);
515 while (precision > 0)
522 *p++ = dp->conversion - 'A' + 'P';
523 # if WIDE_CHAR_VERSION
525 static const wchar_t decimal_format[] =
526 { '%', '+', 'd', '\0' };
527 SNPRINTF (p, 6 + 1, decimal_format, exponent);
530 sprintf (p, "%+d", exponent);
540 double arg = a.arg[dp->arg_index].a.a_double;
544 if (dp->conversion == 'A')
546 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
550 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
564 /* Distinguish 0.0 and -0.0. */
565 static double plus_zero = 0.0;
566 double arg_mem = arg;
567 if (memcmp (&plus_zero, &arg_mem, sizeof (double)) != 0)
576 else if (flags & FLAG_SHOWSIGN)
578 else if (flags & FLAG_SPACE)
581 if (arg > 0.0 && arg + arg == arg)
583 if (dp->conversion == 'A')
585 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
589 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
598 mantissa = printf_frexp (arg, &exponent);
606 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
608 /* Round the mantissa. */
609 double tail = mantissa;
612 for (q = precision; ; q--)
614 int digit = (int) tail;
618 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
627 for (q = precision; q > 0; q--)
633 *p++ = dp->conversion - 'A' + 'X';
638 digit = (int) mantissa;
641 if ((flags & FLAG_ALT)
642 || mantissa > 0.0 || precision > 0)
645 localeconv () -> decimal_point;
646 /* The decimal point is always a single byte:
647 either '.' or ','. */
648 *p++ = (point[0] != '\0' ? point[0] : '.');
649 /* This loop terminates because we assume
650 that FLT_RADIX is a power of 2. */
651 while (mantissa > 0.0)
654 digit = (int) mantissa;
659 : dp->conversion - 10);
663 while (precision > 0)
670 *p++ = dp->conversion - 'A' + 'P';
671 # if WIDE_CHAR_VERSION
673 static const wchar_t decimal_format[] =
674 { '%', '+', 'd', '\0' };
675 SNPRINTF (p, 6 + 1, decimal_format, exponent);
678 sprintf (p, "%+d", exponent);
685 /* The generated string now extends from tmp to p, with the
686 zero padding insertion point being at pad_ptr. */
687 if (has_width && p - tmp < width)
689 size_t pad = width - (p - tmp);
690 CHAR_T *end = p + pad;
692 if (flags & FLAG_LEFT)
694 /* Pad with spaces on the right. */
695 for (; pad > 0; pad--)
698 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
700 /* Pad with zeroes. */
705 for (; pad > 0; pad--)
710 /* Pad with spaces on the left. */
715 for (; pad > 0; pad--)
723 size_t count = p - tmp;
725 if (count >= tmp_length)
726 /* tmp_length was incorrectly calculated - fix the
730 /* Make room for the result. */
731 if (count >= allocated - length)
733 size_t n = xsum (length, count);
735 ENSURE_ALLOCATION (n);
738 /* Append the result. */
739 memcpy (result + length, tmp, count * sizeof (CHAR_T));
748 arg_type type = a.arg[dp->arg_index].type;
750 unsigned int prefix_count;
757 /* Allocate a temporary buffer of sufficient size for calling
764 if (dp->width_start != dp->width_end)
766 if (dp->width_arg_index != ARG_NONE)
770 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
772 arg = a.arg[dp->width_arg_index].a.a_int;
773 width = (arg < 0 ? (unsigned int) (-arg) : arg);
777 const CHAR_T *digitp = dp->width_start;
780 width = xsum (xtimes (width, 10), *digitp++ - '0');
781 while (digitp != dp->width_end);
786 if (dp->precision_start != dp->precision_end)
788 if (dp->precision_arg_index != ARG_NONE)
792 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
794 arg = a.arg[dp->precision_arg_index].a.a_int;
795 precision = (arg < 0 ? 0 : arg);
799 const CHAR_T *digitp = dp->precision_start + 1;
802 while (digitp != dp->precision_end)
803 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
807 switch (dp->conversion)
810 case 'd': case 'i': case 'u':
811 # if HAVE_LONG_LONG_INT
812 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
814 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
815 * 0.30103 /* binary -> decimal */
817 + 1; /* turn floor into ceil */
820 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
822 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
823 * 0.30103 /* binary -> decimal */
825 + 1; /* turn floor into ceil */
828 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
829 * 0.30103 /* binary -> decimal */
831 + 1; /* turn floor into ceil */
832 if (tmp_length < precision)
833 tmp_length = precision;
834 /* Multiply by 2, as an estimate for FLAG_GROUP. */
835 tmp_length = xsum (tmp_length, tmp_length);
836 /* Add 1, to account for a leading sign. */
837 tmp_length = xsum (tmp_length, 1);
841 # if HAVE_LONG_LONG_INT
842 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
844 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
845 * 0.333334 /* binary -> octal */
847 + 1; /* turn floor into ceil */
850 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
852 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
853 * 0.333334 /* binary -> octal */
855 + 1; /* turn floor into ceil */
858 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
859 * 0.333334 /* binary -> octal */
861 + 1; /* turn floor into ceil */
862 if (tmp_length < precision)
863 tmp_length = precision;
864 /* Add 1, to account for a leading sign. */
865 tmp_length = xsum (tmp_length, 1);
869 # if HAVE_LONG_LONG_INT
870 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
872 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
873 * 0.25 /* binary -> hexadecimal */
875 + 1; /* turn floor into ceil */
878 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
880 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
881 * 0.25 /* binary -> hexadecimal */
883 + 1; /* turn floor into ceil */
886 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
887 * 0.25 /* binary -> hexadecimal */
889 + 1; /* turn floor into ceil */
890 if (tmp_length < precision)
891 tmp_length = precision;
892 /* Add 2, to account for a leading sign or alternate form. */
893 tmp_length = xsum (tmp_length, 2);
897 # if HAVE_LONG_DOUBLE
898 if (type == TYPE_LONGDOUBLE)
900 (unsigned int) (LDBL_MAX_EXP
901 * 0.30103 /* binary -> decimal */
902 * 2 /* estimate for FLAG_GROUP */
904 + 1 /* turn floor into ceil */
905 + 10; /* sign, decimal point etc. */
909 (unsigned int) (DBL_MAX_EXP
910 * 0.30103 /* binary -> decimal */
911 * 2 /* estimate for FLAG_GROUP */
913 + 1 /* turn floor into ceil */
914 + 10; /* sign, decimal point etc. */
915 tmp_length = xsum (tmp_length, precision);
918 case 'e': case 'E': case 'g': case 'G':
920 12; /* sign, decimal point, exponent etc. */
921 tmp_length = xsum (tmp_length, precision);
925 # if HAVE_LONG_DOUBLE
926 if (type == TYPE_LONGDOUBLE)
928 (unsigned int) (LDBL_DIG
929 * 0.831 /* decimal -> hexadecimal */
931 + 1; /* turn floor into ceil */
935 (unsigned int) (DBL_DIG
936 * 0.831 /* decimal -> hexadecimal */
938 + 1; /* turn floor into ceil */
939 if (tmp_length < precision)
940 tmp_length = precision;
941 /* Account for sign, decimal point etc. */
942 tmp_length = xsum (tmp_length, 12);
946 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
947 if (type == TYPE_WIDE_CHAR)
948 tmp_length = MB_CUR_MAX;
956 if (type == TYPE_WIDE_STRING)
959 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
961 # if !WIDE_CHAR_VERSION
962 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
967 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
972 (unsigned int) (sizeof (void *) * CHAR_BIT
973 * 0.25 /* binary -> hexadecimal */
975 + 1 /* turn floor into ceil */
976 + 2; /* account for leading 0x */
983 if (tmp_length < width)
986 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
989 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
993 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
995 if (size_overflow_p (tmp_memsize))
996 /* Overflow, would lead to out of memory. */
998 tmp = (CHAR_T *) malloc (tmp_memsize);
1000 /* Out of memory. */
1005 /* Construct the format string for calling snprintf or
1009 if (dp->flags & FLAG_GROUP)
1011 if (dp->flags & FLAG_LEFT)
1013 if (dp->flags & FLAG_SHOWSIGN)
1015 if (dp->flags & FLAG_SPACE)
1017 if (dp->flags & FLAG_ALT)
1019 if (dp->flags & FLAG_ZERO)
1021 if (dp->width_start != dp->width_end)
1023 size_t n = dp->width_end - dp->width_start;
1024 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1027 if (dp->precision_start != dp->precision_end)
1029 size_t n = dp->precision_end - dp->precision_start;
1030 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1036 #if HAVE_LONG_LONG_INT
1037 case TYPE_LONGLONGINT:
1038 case TYPE_ULONGLONGINT:
1045 case TYPE_WIDE_CHAR:
1048 case TYPE_WIDE_STRING:
1052 #if HAVE_LONG_DOUBLE
1053 case TYPE_LONGDOUBLE:
1060 *p = dp->conversion;
1069 /* Construct the arguments for calling snprintf or sprintf. */
1071 if (dp->width_arg_index != ARG_NONE)
1073 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1075 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1077 if (dp->precision_arg_index != ARG_NONE)
1079 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1081 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1085 /* Prepare checking whether snprintf returns the count
1087 ENSURE_ALLOCATION (xsum (length, 1));
1088 result[length] = '\0';
1097 maxlen = allocated - length;
1102 # define SNPRINTF_BUF(arg) \
1103 switch (prefix_count) \
1106 retcount = SNPRINTF (result + length, maxlen, buf, \
1110 retcount = SNPRINTF (result + length, maxlen, buf, \
1111 prefixes[0], arg, &count); \
1114 retcount = SNPRINTF (result + length, maxlen, buf, \
1115 prefixes[0], prefixes[1], arg, \
1122 # define SNPRINTF_BUF(arg) \
1123 switch (prefix_count) \
1126 count = sprintf (tmp, buf, arg); \
1129 count = sprintf (tmp, buf, prefixes[0], arg); \
1132 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1144 int arg = a.arg[dp->arg_index].a.a_schar;
1150 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1156 int arg = a.arg[dp->arg_index].a.a_short;
1162 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1168 int arg = a.arg[dp->arg_index].a.a_int;
1174 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1180 long int arg = a.arg[dp->arg_index].a.a_longint;
1186 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1190 #if HAVE_LONG_LONG_INT
1191 case TYPE_LONGLONGINT:
1193 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1197 case TYPE_ULONGLONGINT:
1199 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1206 double arg = a.arg[dp->arg_index].a.a_double;
1210 #if HAVE_LONG_DOUBLE
1211 case TYPE_LONGDOUBLE:
1213 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1220 int arg = a.arg[dp->arg_index].a.a_char;
1225 case TYPE_WIDE_CHAR:
1227 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1234 const char *arg = a.arg[dp->arg_index].a.a_string;
1239 case TYPE_WIDE_STRING:
1241 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1248 void *arg = a.arg[dp->arg_index].a.a_pointer;
1257 /* Portability: Not all implementations of snprintf()
1258 are ISO C 99 compliant. Determine the number of
1259 bytes that snprintf() has produced or would have
1263 /* Verify that snprintf() has NUL-terminated its
1265 if (count < maxlen && result[length + count] != '\0')
1267 /* Portability hack. */
1268 if (retcount > count)
1273 /* snprintf() doesn't understand the '%n'
1277 /* Don't use the '%n' directive; instead, look
1278 at the snprintf() return value. */
1284 /* Look at the snprintf() return value. */
1287 /* HP-UX 10.20 snprintf() is doubly deficient:
1288 It doesn't understand the '%n' directive,
1289 *and* it returns -1 (rather than the length
1290 that would have been required) when the
1291 buffer is too small. */
1292 size_t bigger_need =
1293 xsum (xtimes (allocated, 2), 12);
1294 ENSURE_ALLOCATION (bigger_need);
1303 /* Attempt to handle failure. */
1306 if (!(result == resultbuf || result == NULL))
1308 if (buf_malloced != NULL)
1309 free (buf_malloced);
1316 if (count >= tmp_length)
1317 /* tmp_length was incorrectly calculated - fix the
1322 /* Make room for the result. */
1323 if (count >= maxlen)
1325 /* Need at least count bytes. But allocate
1326 proportionally, to avoid looping eternally if
1327 snprintf() reports a too small count. */
1329 xmax (xsum (length, count), xtimes (allocated, 2));
1331 ENSURE_ALLOCATION (n);
1338 /* The snprintf() result did fit. */
1340 /* Append the sprintf() result. */
1341 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1353 /* Add the final NUL. */
1354 ENSURE_ALLOCATION (xsum (length, 1));
1355 result[length] = '\0';
1357 if (result != resultbuf && length + 1 < allocated)
1359 /* Shrink the allocated memory if possible. */
1362 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1367 if (buf_malloced != NULL)
1368 free (buf_malloced);
1371 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1372 says that snprintf() fails with errno = EOVERFLOW in this case, but
1373 that's only because snprintf() returns an 'int'. This function does
1374 not have this limitation. */
1378 if (!(result == resultbuf || result == NULL))
1380 if (buf_malloced != NULL)
1381 free (buf_malloced);