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
56 # include "printf-frexp.h"
58 # include "isnanl-nolibm.h"
59 # include "printf-frexpl.h"
63 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
65 # define EOVERFLOW E2BIG
70 # define local_wcslen wcslen
72 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
73 a dependency towards this library, here is a local substitute.
74 Define this substitute only once, even if this file is included
75 twice in the same compilation unit. */
76 # ifndef local_wcslen_defined
77 # define local_wcslen_defined 1
79 local_wcslen (const wchar_t *s)
83 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
92 # define VASNPRINTF vasnwprintf
93 # define CHAR_T wchar_t
94 # define DIRECTIVE wchar_t_directive
95 # define DIRECTIVES wchar_t_directives
96 # define PRINTF_PARSE wprintf_parse
97 # define USE_SNPRINTF 1
98 # if HAVE_DECL__SNWPRINTF
99 /* On Windows, the function swprintf() has a different signature than
100 on Unix; we use the _snwprintf() function instead. */
101 # define SNPRINTF _snwprintf
104 # define SNPRINTF swprintf
107 # define VASNPRINTF vasnprintf
109 # define DIRECTIVE char_directive
110 # define DIRECTIVES char_directives
111 # define PRINTF_PARSE printf_parse
112 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
113 # if HAVE_DECL__SNPRINTF
115 # define SNPRINTF _snprintf
118 # define SNPRINTF snprintf
119 /* Here we need to call the native snprintf, not rpl_snprintf. */
123 /* Here we need to call the native sprintf, not rpl_sprintf. */
127 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
132 if (PRINTF_PARSE (format, &d, &a) < 0)
143 if (printf_fetchargs (args, &a) < 0)
151 size_t buf_neededlength;
153 CHAR_T *buf_malloced;
157 /* Output string accumulator. */
162 /* Allocate a small buffer that will hold a directive passed to
163 sprintf or snprintf. */
165 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
167 if (buf_neededlength < 4000 / sizeof (CHAR_T))
169 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
175 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
176 if (size_overflow_p (buf_memsize))
177 goto out_of_memory_1;
178 buf = (CHAR_T *) malloc (buf_memsize);
180 goto out_of_memory_1;
184 if (resultbuf != NULL)
187 allocated = *lengthp;
196 result is either == resultbuf or == NULL or malloc-allocated.
197 If length > 0, then result != NULL. */
199 /* Ensures that allocated >= needed. Aborts through a jump to
200 out_of_memory if needed is SIZE_MAX or otherwise too big. */
201 #define ENSURE_ALLOCATION(needed) \
202 if ((needed) > allocated) \
204 size_t memory_size; \
207 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
208 if ((needed) > allocated) \
209 allocated = (needed); \
210 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
211 if (size_overflow_p (memory_size)) \
212 goto out_of_memory; \
213 if (result == resultbuf || result == NULL) \
214 memory = (CHAR_T *) malloc (memory_size); \
216 memory = (CHAR_T *) realloc (result, memory_size); \
217 if (memory == NULL) \
218 goto out_of_memory; \
219 if (result == resultbuf && length > 0) \
220 memcpy (memory, result, length * sizeof (CHAR_T)); \
224 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
226 if (cp != dp->dir_start)
228 size_t n = dp->dir_start - cp;
229 size_t augmented_length = xsum (length, n);
231 ENSURE_ALLOCATION (augmented_length);
232 memcpy (result + length, cp, n * sizeof (CHAR_T));
233 length = augmented_length;
238 /* Execute a single directive. */
239 if (dp->conversion == '%')
241 size_t augmented_length;
243 if (!(dp->arg_index == ARG_NONE))
245 augmented_length = xsum (length, 1);
246 ENSURE_ALLOCATION (augmented_length);
247 result[length] = '%';
248 length = augmented_length;
252 if (!(dp->arg_index != ARG_NONE))
255 if (dp->conversion == 'n')
257 switch (a.arg[dp->arg_index].type)
259 case TYPE_COUNT_SCHAR_POINTER:
260 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
262 case TYPE_COUNT_SHORT_POINTER:
263 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
265 case TYPE_COUNT_INT_POINTER:
266 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
268 case TYPE_COUNT_LONGINT_POINTER:
269 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
271 #if HAVE_LONG_LONG_INT
272 case TYPE_COUNT_LONGLONGINT_POINTER:
273 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
280 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
281 else if (dp->conversion == 'a' || dp->conversion == 'A')
283 arg_type type = a.arg[dp->arg_index].type;
284 int flags = dp->flags;
297 if (dp->width_start != dp->width_end)
299 if (dp->width_arg_index != ARG_NONE)
303 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
305 arg = a.arg[dp->width_arg_index].a.a_int;
308 /* "A negative field width is taken as a '-' flag
309 followed by a positive field width." */
311 width = (unsigned int) (-arg);
318 const CHAR_T *digitp = dp->width_start;
321 width = xsum (xtimes (width, 10), *digitp++ - '0');
322 while (digitp != dp->width_end);
329 if (dp->precision_start != dp->precision_end)
331 if (dp->precision_arg_index != ARG_NONE)
335 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
337 arg = a.arg[dp->precision_arg_index].a.a_int;
338 /* "A negative precision is taken as if the precision
348 const CHAR_T *digitp = dp->precision_start + 1;
351 while (digitp != dp->precision_end)
352 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
357 /* Allocate a temporary buffer of sufficient size. */
358 # if HAVE_LONG_DOUBLE
359 if (type == TYPE_LONGDOUBLE)
361 (unsigned int) ((LDBL_DIG + 1)
362 * 0.831 /* decimal -> hexadecimal */
364 + 1; /* turn floor into ceil */
368 (unsigned int) ((DBL_DIG + 1)
369 * 0.831 /* decimal -> hexadecimal */
371 + 1; /* turn floor into ceil */
372 if (tmp_length < precision)
373 tmp_length = precision;
374 /* Account for sign, decimal point etc. */
375 tmp_length = xsum (tmp_length, 12);
377 if (tmp_length < width)
380 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
382 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
386 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
388 if (size_overflow_p (tmp_memsize))
389 /* Overflow, would lead to out of memory. */
391 tmp = (CHAR_T *) malloc (tmp_memsize);
399 # if HAVE_LONG_DOUBLE
400 if (type == TYPE_LONGDOUBLE)
402 long double arg = a.arg[dp->arg_index].a.a_longdouble;
406 if (dp->conversion == 'A')
408 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
412 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
424 else if (arg == 0.0L)
426 /* Distinguish 0.0L and -0.0L. */
427 static long double plus_zero = 0.0L;
428 long double arg_mem = arg;
429 if (memcmp (&plus_zero, &arg_mem, SIZEOF_LDBL) != 0)
438 else if (flags & FLAG_SHOWSIGN)
440 else if (flags & FLAG_SPACE)
443 if (arg > 0.0L && arg + arg == arg)
445 if (dp->conversion == 'A')
447 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
451 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
457 long double mantissa;
460 mantissa = printf_frexpl (arg, &exponent);
468 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
470 /* Round the mantissa. */
471 long double tail = mantissa;
474 for (q = precision; ; q--)
476 int digit = (int) tail;
480 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
489 for (q = precision; q > 0; q--)
495 *p++ = dp->conversion - 'A' + 'X';
500 digit = (int) mantissa;
503 if ((flags & FLAG_ALT)
504 || mantissa > 0.0L || precision > 0)
507 localeconv () -> decimal_point;
508 /* The decimal point is always a single byte:
509 either '.' or ','. */
510 *p++ = (point[0] != '\0' ? point[0] : '.');
511 /* This loop terminates because we assume
512 that FLT_RADIX is a power of 2. */
513 while (mantissa > 0.0L)
516 digit = (int) mantissa;
521 : dp->conversion - 10);
525 while (precision > 0)
532 *p++ = dp->conversion - 'A' + 'P';
533 # if WIDE_CHAR_VERSION
535 static const wchar_t decimal_format[] =
536 { '%', '+', 'd', '\0' };
537 SNPRINTF (p, 6 + 1, decimal_format, exponent);
540 sprintf (p, "%+d", exponent);
550 double arg = a.arg[dp->arg_index].a.a_double;
554 if (dp->conversion == 'A')
556 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
560 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
574 /* Distinguish 0.0 and -0.0. */
575 static double plus_zero = 0.0;
576 double arg_mem = arg;
577 if (memcmp (&plus_zero, &arg_mem, SIZEOF_DBL) != 0)
586 else if (flags & FLAG_SHOWSIGN)
588 else if (flags & FLAG_SPACE)
591 if (arg > 0.0 && arg + arg == arg)
593 if (dp->conversion == 'A')
595 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
599 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
608 mantissa = printf_frexp (arg, &exponent);
616 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
618 /* Round the mantissa. */
619 double tail = mantissa;
622 for (q = precision; ; q--)
624 int digit = (int) tail;
628 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
637 for (q = precision; q > 0; q--)
643 *p++ = dp->conversion - 'A' + 'X';
648 digit = (int) mantissa;
651 if ((flags & FLAG_ALT)
652 || mantissa > 0.0 || precision > 0)
655 localeconv () -> decimal_point;
656 /* The decimal point is always a single byte:
657 either '.' or ','. */
658 *p++ = (point[0] != '\0' ? point[0] : '.');
659 /* This loop terminates because we assume
660 that FLT_RADIX is a power of 2. */
661 while (mantissa > 0.0)
664 digit = (int) mantissa;
669 : dp->conversion - 10);
673 while (precision > 0)
680 *p++ = dp->conversion - 'A' + 'P';
681 # if WIDE_CHAR_VERSION
683 static const wchar_t decimal_format[] =
684 { '%', '+', 'd', '\0' };
685 SNPRINTF (p, 6 + 1, decimal_format, exponent);
688 sprintf (p, "%+d", exponent);
695 /* The generated string now extends from tmp to p, with the
696 zero padding insertion point being at pad_ptr. */
697 if (has_width && p - tmp < width)
699 size_t pad = width - (p - tmp);
700 CHAR_T *end = p + pad;
702 if (flags & FLAG_LEFT)
704 /* Pad with spaces on the right. */
705 for (; pad > 0; pad--)
708 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
710 /* Pad with zeroes. */
715 for (; pad > 0; pad--)
720 /* Pad with spaces on the left. */
725 for (; pad > 0; pad--)
733 size_t count = p - tmp;
735 if (count >= tmp_length)
736 /* tmp_length was incorrectly calculated - fix the
740 /* Make room for the result. */
741 if (count >= allocated - length)
743 size_t n = xsum (length, count);
745 ENSURE_ALLOCATION (n);
748 /* Append the result. */
749 memcpy (result + length, tmp, count * sizeof (CHAR_T));
758 arg_type type = a.arg[dp->arg_index].type;
760 unsigned int prefix_count;
767 /* Allocate a temporary buffer of sufficient size for calling
774 if (dp->width_start != dp->width_end)
776 if (dp->width_arg_index != ARG_NONE)
780 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
782 arg = a.arg[dp->width_arg_index].a.a_int;
783 width = (arg < 0 ? (unsigned int) (-arg) : arg);
787 const CHAR_T *digitp = dp->width_start;
790 width = xsum (xtimes (width, 10), *digitp++ - '0');
791 while (digitp != dp->width_end);
796 if (dp->precision_start != dp->precision_end)
798 if (dp->precision_arg_index != ARG_NONE)
802 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
804 arg = a.arg[dp->precision_arg_index].a.a_int;
805 precision = (arg < 0 ? 0 : arg);
809 const CHAR_T *digitp = dp->precision_start + 1;
812 while (digitp != dp->precision_end)
813 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
817 switch (dp->conversion)
820 case 'd': case 'i': case 'u':
821 # if HAVE_LONG_LONG_INT
822 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
824 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
825 * 0.30103 /* binary -> decimal */
827 + 1; /* turn floor into ceil */
830 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
832 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
833 * 0.30103 /* binary -> decimal */
835 + 1; /* turn floor into ceil */
838 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
839 * 0.30103 /* binary -> decimal */
841 + 1; /* turn floor into ceil */
842 if (tmp_length < precision)
843 tmp_length = precision;
844 /* Multiply by 2, as an estimate for FLAG_GROUP. */
845 tmp_length = xsum (tmp_length, tmp_length);
846 /* Add 1, to account for a leading sign. */
847 tmp_length = xsum (tmp_length, 1);
851 # if HAVE_LONG_LONG_INT
852 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
854 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
855 * 0.333334 /* binary -> octal */
857 + 1; /* turn floor into ceil */
860 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
862 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
863 * 0.333334 /* binary -> octal */
865 + 1; /* turn floor into ceil */
868 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
869 * 0.333334 /* binary -> octal */
871 + 1; /* turn floor into ceil */
872 if (tmp_length < precision)
873 tmp_length = precision;
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.25 /* binary -> hexadecimal */
885 + 1; /* turn floor into ceil */
888 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
890 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
891 * 0.25 /* binary -> hexadecimal */
893 + 1; /* turn floor into ceil */
896 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
897 * 0.25 /* binary -> hexadecimal */
899 + 1; /* turn floor into ceil */
900 if (tmp_length < precision)
901 tmp_length = precision;
902 /* Add 2, to account for a leading sign or alternate form. */
903 tmp_length = xsum (tmp_length, 2);
907 # if HAVE_LONG_DOUBLE
908 if (type == TYPE_LONGDOUBLE)
910 (unsigned int) (LDBL_MAX_EXP
911 * 0.30103 /* binary -> decimal */
912 * 2 /* estimate for FLAG_GROUP */
914 + 1 /* turn floor into ceil */
915 + 10; /* sign, decimal point etc. */
919 (unsigned int) (DBL_MAX_EXP
920 * 0.30103 /* binary -> decimal */
921 * 2 /* estimate for FLAG_GROUP */
923 + 1 /* turn floor into ceil */
924 + 10; /* sign, decimal point etc. */
925 tmp_length = xsum (tmp_length, precision);
928 case 'e': case 'E': case 'g': case 'G':
930 12; /* sign, decimal point, exponent etc. */
931 tmp_length = xsum (tmp_length, precision);
935 # if HAVE_LONG_DOUBLE
936 if (type == TYPE_LONGDOUBLE)
938 (unsigned int) (LDBL_DIG
939 * 0.831 /* decimal -> hexadecimal */
941 + 1; /* turn floor into ceil */
945 (unsigned int) (DBL_DIG
946 * 0.831 /* decimal -> hexadecimal */
948 + 1; /* turn floor into ceil */
949 if (tmp_length < precision)
950 tmp_length = precision;
951 /* Account for sign, decimal point etc. */
952 tmp_length = xsum (tmp_length, 12);
956 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
957 if (type == TYPE_WIDE_CHAR)
958 tmp_length = MB_CUR_MAX;
966 if (type == TYPE_WIDE_STRING)
969 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
971 # if !WIDE_CHAR_VERSION
972 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
977 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
982 (unsigned int) (sizeof (void *) * CHAR_BIT
983 * 0.25 /* binary -> hexadecimal */
985 + 1 /* turn floor into ceil */
986 + 2; /* account for leading 0x */
993 if (tmp_length < width)
996 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
999 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
1003 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1005 if (size_overflow_p (tmp_memsize))
1006 /* Overflow, would lead to out of memory. */
1008 tmp = (CHAR_T *) malloc (tmp_memsize);
1010 /* Out of memory. */
1015 /* Construct the format string for calling snprintf or
1019 if (dp->flags & FLAG_GROUP)
1021 if (dp->flags & FLAG_LEFT)
1023 if (dp->flags & FLAG_SHOWSIGN)
1025 if (dp->flags & FLAG_SPACE)
1027 if (dp->flags & FLAG_ALT)
1029 if (dp->flags & FLAG_ZERO)
1031 if (dp->width_start != dp->width_end)
1033 size_t n = dp->width_end - dp->width_start;
1034 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1037 if (dp->precision_start != dp->precision_end)
1039 size_t n = dp->precision_end - dp->precision_start;
1040 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1046 #if HAVE_LONG_LONG_INT
1047 case TYPE_LONGLONGINT:
1048 case TYPE_ULONGLONGINT:
1055 case TYPE_WIDE_CHAR:
1058 case TYPE_WIDE_STRING:
1062 #if HAVE_LONG_DOUBLE
1063 case TYPE_LONGDOUBLE:
1070 *p = dp->conversion;
1079 /* Construct the arguments for calling snprintf or sprintf. */
1081 if (dp->width_arg_index != ARG_NONE)
1083 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1085 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1087 if (dp->precision_arg_index != ARG_NONE)
1089 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1091 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1095 /* Prepare checking whether snprintf returns the count
1097 ENSURE_ALLOCATION (xsum (length, 1));
1098 result[length] = '\0';
1107 maxlen = allocated - length;
1112 /* SNPRINTF can fail if maxlen > INT_MAX. */
1113 if (maxlen > INT_MAX)
1115 # define SNPRINTF_BUF(arg) \
1116 switch (prefix_count) \
1119 retcount = SNPRINTF (result + length, maxlen, buf, \
1123 retcount = SNPRINTF (result + length, maxlen, buf, \
1124 prefixes[0], arg, &count); \
1127 retcount = SNPRINTF (result + length, maxlen, buf, \
1128 prefixes[0], prefixes[1], arg, \
1135 # define SNPRINTF_BUF(arg) \
1136 switch (prefix_count) \
1139 count = sprintf (tmp, buf, arg); \
1142 count = sprintf (tmp, buf, prefixes[0], arg); \
1145 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1157 int arg = a.arg[dp->arg_index].a.a_schar;
1163 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1169 int arg = a.arg[dp->arg_index].a.a_short;
1175 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1181 int arg = a.arg[dp->arg_index].a.a_int;
1187 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1193 long int arg = a.arg[dp->arg_index].a.a_longint;
1199 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1203 #if HAVE_LONG_LONG_INT
1204 case TYPE_LONGLONGINT:
1206 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1210 case TYPE_ULONGLONGINT:
1212 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1219 double arg = a.arg[dp->arg_index].a.a_double;
1223 #if HAVE_LONG_DOUBLE
1224 case TYPE_LONGDOUBLE:
1226 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1233 int arg = a.arg[dp->arg_index].a.a_char;
1238 case TYPE_WIDE_CHAR:
1240 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1247 const char *arg = a.arg[dp->arg_index].a.a_string;
1252 case TYPE_WIDE_STRING:
1254 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1261 void *arg = a.arg[dp->arg_index].a.a_pointer;
1270 /* Portability: Not all implementations of snprintf()
1271 are ISO C 99 compliant. Determine the number of
1272 bytes that snprintf() has produced or would have
1276 /* Verify that snprintf() has NUL-terminated its
1278 if (count < maxlen && result[length + count] != '\0')
1280 /* Portability hack. */
1281 if (retcount > count)
1286 /* snprintf() doesn't understand the '%n'
1290 /* Don't use the '%n' directive; instead, look
1291 at the snprintf() return value. */
1297 /* Look at the snprintf() return value. */
1300 /* HP-UX 10.20 snprintf() is doubly deficient:
1301 It doesn't understand the '%n' directive,
1302 *and* it returns -1 (rather than the length
1303 that would have been required) when the
1304 buffer is too small. */
1305 size_t bigger_need =
1306 xsum (xtimes (allocated, 2), 12);
1307 ENSURE_ALLOCATION (bigger_need);
1316 /* Attempt to handle failure. */
1319 if (!(result == resultbuf || result == NULL))
1321 if (buf_malloced != NULL)
1322 free (buf_malloced);
1329 if (count >= tmp_length)
1330 /* tmp_length was incorrectly calculated - fix the
1335 /* Make room for the result. */
1336 if (count >= maxlen)
1338 /* Need at least count bytes. But allocate
1339 proportionally, to avoid looping eternally if
1340 snprintf() reports a too small count. */
1342 xmax (xsum (length, count), xtimes (allocated, 2));
1344 ENSURE_ALLOCATION (n);
1351 /* The snprintf() result did fit. */
1353 /* Append the sprintf() result. */
1354 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1366 /* Add the final NUL. */
1367 ENSURE_ALLOCATION (xsum (length, 1));
1368 result[length] = '\0';
1370 if (result != resultbuf && length + 1 < allocated)
1372 /* Shrink the allocated memory if possible. */
1375 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1380 if (buf_malloced != NULL)
1381 free (buf_malloced);
1384 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1385 says that snprintf() fails with errno = EOVERFLOW in this case, but
1386 that's only because snprintf() returns an 'int'. This function does
1387 not have this limitation. */
1391 if (!(result == resultbuf || result == NULL))
1393 if (buf_malloced != NULL)
1394 free (buf_malloced);
1400 if (!(result == resultbuf || result == NULL))
1402 if (buf_malloced != NULL)
1403 free (buf_malloced);