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
113 /* Here we need to call the native snprintf, not rpl_snprintf. */
117 /* Here we need to call the native sprintf, not rpl_sprintf. */
121 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
126 if (PRINTF_PARSE (format, &d, &a) < 0)
137 if (printf_fetchargs (args, &a) < 0)
145 size_t buf_neededlength;
147 CHAR_T *buf_malloced;
151 /* Output string accumulator. */
156 /* Allocate a small buffer that will hold a directive passed to
157 sprintf or snprintf. */
159 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
161 if (buf_neededlength < 4000 / sizeof (CHAR_T))
163 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
169 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
170 if (size_overflow_p (buf_memsize))
171 goto out_of_memory_1;
172 buf = (CHAR_T *) malloc (buf_memsize);
174 goto out_of_memory_1;
178 if (resultbuf != NULL)
181 allocated = *lengthp;
190 result is either == resultbuf or == NULL or malloc-allocated.
191 If length > 0, then result != NULL. */
193 /* Ensures that allocated >= needed. Aborts through a jump to
194 out_of_memory if needed is SIZE_MAX or otherwise too big. */
195 #define ENSURE_ALLOCATION(needed) \
196 if ((needed) > allocated) \
198 size_t memory_size; \
201 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
202 if ((needed) > allocated) \
203 allocated = (needed); \
204 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
205 if (size_overflow_p (memory_size)) \
206 goto out_of_memory; \
207 if (result == resultbuf || result == NULL) \
208 memory = (CHAR_T *) malloc (memory_size); \
210 memory = (CHAR_T *) realloc (result, memory_size); \
211 if (memory == NULL) \
212 goto out_of_memory; \
213 if (result == resultbuf && length > 0) \
214 memcpy (memory, result, length * sizeof (CHAR_T)); \
218 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
220 if (cp != dp->dir_start)
222 size_t n = dp->dir_start - cp;
223 size_t augmented_length = xsum (length, n);
225 ENSURE_ALLOCATION (augmented_length);
226 memcpy (result + length, cp, n * sizeof (CHAR_T));
227 length = augmented_length;
232 /* Execute a single directive. */
233 if (dp->conversion == '%')
235 size_t augmented_length;
237 if (!(dp->arg_index == ARG_NONE))
239 augmented_length = xsum (length, 1);
240 ENSURE_ALLOCATION (augmented_length);
241 result[length] = '%';
242 length = augmented_length;
246 if (!(dp->arg_index != ARG_NONE))
249 if (dp->conversion == 'n')
251 switch (a.arg[dp->arg_index].type)
253 case TYPE_COUNT_SCHAR_POINTER:
254 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
256 case TYPE_COUNT_SHORT_POINTER:
257 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
259 case TYPE_COUNT_INT_POINTER:
260 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
262 case TYPE_COUNT_LONGINT_POINTER:
263 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
265 #if HAVE_LONG_LONG_INT
266 case TYPE_COUNT_LONGLONGINT_POINTER:
267 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
274 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
275 else if (dp->conversion == 'a' || dp->conversion == 'A')
277 arg_type type = a.arg[dp->arg_index].type;
278 int flags = dp->flags;
291 if (dp->width_start != dp->width_end)
293 if (dp->width_arg_index != ARG_NONE)
297 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
299 arg = a.arg[dp->width_arg_index].a.a_int;
302 /* "A negative field width is taken as a '-' flag
303 followed by a positive field width." */
305 width = (unsigned int) (-arg);
312 const CHAR_T *digitp = dp->width_start;
315 width = xsum (xtimes (width, 10), *digitp++ - '0');
316 while (digitp != dp->width_end);
323 if (dp->precision_start != dp->precision_end)
325 if (dp->precision_arg_index != ARG_NONE)
329 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
331 arg = a.arg[dp->precision_arg_index].a.a_int;
332 /* "A negative precision is taken as if the precision
342 const CHAR_T *digitp = dp->precision_start + 1;
345 while (digitp != dp->precision_end)
346 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
351 /* Allocate a temporary buffer of sufficient size. */
352 # if HAVE_LONG_DOUBLE
353 if (type == TYPE_LONGDOUBLE)
355 (unsigned int) ((LDBL_DIG + 1)
356 * 0.831 /* decimal -> hexadecimal */
358 + 1; /* turn floor into ceil */
362 (unsigned int) ((DBL_DIG + 1)
363 * 0.831 /* decimal -> hexadecimal */
365 + 1; /* turn floor into ceil */
366 if (tmp_length < precision)
367 tmp_length = precision;
368 /* Account for sign, decimal point etc. */
369 tmp_length = xsum (tmp_length, 12);
371 if (tmp_length < width)
374 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
376 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
380 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
382 if (size_overflow_p (tmp_memsize))
383 /* Overflow, would lead to out of memory. */
385 tmp = (CHAR_T *) malloc (tmp_memsize);
393 # if HAVE_LONG_DOUBLE
394 if (type == TYPE_LONGDOUBLE)
396 long double arg = a.arg[dp->arg_index].a.a_longdouble;
400 if (dp->conversion == 'A')
402 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
406 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
418 else if (arg == 0.0L)
420 /* Distinguish 0.0L and -0.0L. */
421 static long double plus_zero = 0.0L;
422 long double arg_mem = arg;
423 if (memcmp (&plus_zero, &arg_mem, sizeof (long double)) != 0)
432 else if (flags & FLAG_SHOWSIGN)
434 else if (flags & FLAG_SPACE)
437 if (arg > 0.0L && arg + arg == arg)
439 if (dp->conversion == 'A')
441 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
445 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
451 long double mantissa;
454 mantissa = printf_frexpl (arg, &exponent);
462 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
464 /* Round the mantissa. */
465 long double tail = mantissa;
468 for (q = precision; ; q--)
470 int digit = (int) tail;
474 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
483 for (q = precision; q > 0; q--)
489 *p++ = dp->conversion - 'A' + 'X';
494 digit = (int) mantissa;
497 if ((flags & FLAG_ALT)
498 || mantissa > 0.0L || precision > 0)
501 localeconv () -> decimal_point;
502 /* The decimal point is always a single byte:
503 either '.' or ','. */
504 *p++ = (point[0] != '\0' ? point[0] : '.');
505 /* This loop terminates because we assume
506 that FLT_RADIX is a power of 2. */
507 while (mantissa > 0.0L)
510 digit = (int) mantissa;
515 : dp->conversion - 10);
519 while (precision > 0)
526 *p++ = dp->conversion - 'A' + 'P';
527 # if WIDE_CHAR_VERSION
529 static const wchar_t decimal_format[] =
530 { '%', '+', 'd', '\0' };
531 SNPRINTF (p, 6 + 1, decimal_format, exponent);
534 sprintf (p, "%+d", exponent);
544 double arg = a.arg[dp->arg_index].a.a_double;
548 if (dp->conversion == 'A')
550 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
554 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
568 /* Distinguish 0.0 and -0.0. */
569 static double plus_zero = 0.0;
570 double arg_mem = arg;
571 if (memcmp (&plus_zero, &arg_mem, sizeof (double)) != 0)
580 else if (flags & FLAG_SHOWSIGN)
582 else if (flags & FLAG_SPACE)
585 if (arg > 0.0 && arg + arg == arg)
587 if (dp->conversion == 'A')
589 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
593 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
602 mantissa = printf_frexp (arg, &exponent);
610 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
612 /* Round the mantissa. */
613 double tail = mantissa;
616 for (q = precision; ; q--)
618 int digit = (int) tail;
622 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
631 for (q = precision; q > 0; q--)
637 *p++ = dp->conversion - 'A' + 'X';
642 digit = (int) mantissa;
645 if ((flags & FLAG_ALT)
646 || mantissa > 0.0 || precision > 0)
649 localeconv () -> decimal_point;
650 /* The decimal point is always a single byte:
651 either '.' or ','. */
652 *p++ = (point[0] != '\0' ? point[0] : '.');
653 /* This loop terminates because we assume
654 that FLT_RADIX is a power of 2. */
655 while (mantissa > 0.0)
658 digit = (int) mantissa;
663 : dp->conversion - 10);
667 while (precision > 0)
674 *p++ = dp->conversion - 'A' + 'P';
675 # if WIDE_CHAR_VERSION
677 static const wchar_t decimal_format[] =
678 { '%', '+', 'd', '\0' };
679 SNPRINTF (p, 6 + 1, decimal_format, exponent);
682 sprintf (p, "%+d", exponent);
689 /* The generated string now extends from tmp to p, with the
690 zero padding insertion point being at pad_ptr. */
691 if (has_width && p - tmp < width)
693 size_t pad = width - (p - tmp);
694 CHAR_T *end = p + pad;
696 if (flags & FLAG_LEFT)
698 /* Pad with spaces on the right. */
699 for (; pad > 0; pad--)
702 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
704 /* Pad with zeroes. */
709 for (; pad > 0; pad--)
714 /* Pad with spaces on the left. */
719 for (; pad > 0; pad--)
727 size_t count = p - tmp;
729 if (count >= tmp_length)
730 /* tmp_length was incorrectly calculated - fix the
734 /* Make room for the result. */
735 if (count >= allocated - length)
737 size_t n = xsum (length, count);
739 ENSURE_ALLOCATION (n);
742 /* Append the result. */
743 memcpy (result + length, tmp, count * sizeof (CHAR_T));
752 arg_type type = a.arg[dp->arg_index].type;
754 unsigned int prefix_count;
761 /* Allocate a temporary buffer of sufficient size for calling
768 if (dp->width_start != dp->width_end)
770 if (dp->width_arg_index != ARG_NONE)
774 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
776 arg = a.arg[dp->width_arg_index].a.a_int;
777 width = (arg < 0 ? (unsigned int) (-arg) : arg);
781 const CHAR_T *digitp = dp->width_start;
784 width = xsum (xtimes (width, 10), *digitp++ - '0');
785 while (digitp != dp->width_end);
790 if (dp->precision_start != dp->precision_end)
792 if (dp->precision_arg_index != ARG_NONE)
796 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
798 arg = a.arg[dp->precision_arg_index].a.a_int;
799 precision = (arg < 0 ? 0 : arg);
803 const CHAR_T *digitp = dp->precision_start + 1;
806 while (digitp != dp->precision_end)
807 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
811 switch (dp->conversion)
814 case 'd': case 'i': case 'u':
815 # if HAVE_LONG_LONG_INT
816 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
818 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
819 * 0.30103 /* binary -> decimal */
821 + 1; /* turn floor into ceil */
824 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
826 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
827 * 0.30103 /* binary -> decimal */
829 + 1; /* turn floor into ceil */
832 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
833 * 0.30103 /* binary -> decimal */
835 + 1; /* turn floor into ceil */
836 if (tmp_length < precision)
837 tmp_length = precision;
838 /* Multiply by 2, as an estimate for FLAG_GROUP. */
839 tmp_length = xsum (tmp_length, tmp_length);
840 /* Add 1, to account for a leading sign. */
841 tmp_length = xsum (tmp_length, 1);
845 # if HAVE_LONG_LONG_INT
846 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
848 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
849 * 0.333334 /* binary -> octal */
851 + 1; /* turn floor into ceil */
854 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
856 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
857 * 0.333334 /* binary -> octal */
859 + 1; /* turn floor into ceil */
862 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
863 * 0.333334 /* binary -> octal */
865 + 1; /* turn floor into ceil */
866 if (tmp_length < precision)
867 tmp_length = precision;
868 /* Add 1, to account for a leading sign. */
869 tmp_length = xsum (tmp_length, 1);
873 # if HAVE_LONG_LONG_INT
874 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
876 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
877 * 0.25 /* binary -> hexadecimal */
879 + 1; /* turn floor into ceil */
882 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
884 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
885 * 0.25 /* binary -> hexadecimal */
887 + 1; /* turn floor into ceil */
890 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
891 * 0.25 /* binary -> hexadecimal */
893 + 1; /* turn floor into ceil */
894 if (tmp_length < precision)
895 tmp_length = precision;
896 /* Add 2, to account for a leading sign or alternate form. */
897 tmp_length = xsum (tmp_length, 2);
901 # if HAVE_LONG_DOUBLE
902 if (type == TYPE_LONGDOUBLE)
904 (unsigned int) (LDBL_MAX_EXP
905 * 0.30103 /* binary -> decimal */
906 * 2 /* estimate for FLAG_GROUP */
908 + 1 /* turn floor into ceil */
909 + 10; /* sign, decimal point etc. */
913 (unsigned int) (DBL_MAX_EXP
914 * 0.30103 /* binary -> decimal */
915 * 2 /* estimate for FLAG_GROUP */
917 + 1 /* turn floor into ceil */
918 + 10; /* sign, decimal point etc. */
919 tmp_length = xsum (tmp_length, precision);
922 case 'e': case 'E': case 'g': case 'G':
924 12; /* sign, decimal point, exponent etc. */
925 tmp_length = xsum (tmp_length, precision);
929 # if HAVE_LONG_DOUBLE
930 if (type == TYPE_LONGDOUBLE)
932 (unsigned int) (LDBL_DIG
933 * 0.831 /* decimal -> hexadecimal */
935 + 1; /* turn floor into ceil */
939 (unsigned int) (DBL_DIG
940 * 0.831 /* decimal -> hexadecimal */
942 + 1; /* turn floor into ceil */
943 if (tmp_length < precision)
944 tmp_length = precision;
945 /* Account for sign, decimal point etc. */
946 tmp_length = xsum (tmp_length, 12);
950 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
951 if (type == TYPE_WIDE_CHAR)
952 tmp_length = MB_CUR_MAX;
960 if (type == TYPE_WIDE_STRING)
963 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
965 # if !WIDE_CHAR_VERSION
966 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
971 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
976 (unsigned int) (sizeof (void *) * CHAR_BIT
977 * 0.25 /* binary -> hexadecimal */
979 + 1 /* turn floor into ceil */
980 + 2; /* account for leading 0x */
987 if (tmp_length < width)
990 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
993 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
997 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
999 if (size_overflow_p (tmp_memsize))
1000 /* Overflow, would lead to out of memory. */
1002 tmp = (CHAR_T *) malloc (tmp_memsize);
1004 /* Out of memory. */
1009 /* Construct the format string for calling snprintf or
1013 if (dp->flags & FLAG_GROUP)
1015 if (dp->flags & FLAG_LEFT)
1017 if (dp->flags & FLAG_SHOWSIGN)
1019 if (dp->flags & FLAG_SPACE)
1021 if (dp->flags & FLAG_ALT)
1023 if (dp->flags & FLAG_ZERO)
1025 if (dp->width_start != dp->width_end)
1027 size_t n = dp->width_end - dp->width_start;
1028 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1031 if (dp->precision_start != dp->precision_end)
1033 size_t n = dp->precision_end - dp->precision_start;
1034 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1040 #if HAVE_LONG_LONG_INT
1041 case TYPE_LONGLONGINT:
1042 case TYPE_ULONGLONGINT:
1049 case TYPE_WIDE_CHAR:
1052 case TYPE_WIDE_STRING:
1056 #if HAVE_LONG_DOUBLE
1057 case TYPE_LONGDOUBLE:
1064 *p = dp->conversion;
1073 /* Construct the arguments for calling snprintf or sprintf. */
1075 if (dp->width_arg_index != ARG_NONE)
1077 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1079 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1081 if (dp->precision_arg_index != ARG_NONE)
1083 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1085 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1089 /* Prepare checking whether snprintf returns the count
1091 ENSURE_ALLOCATION (xsum (length, 1));
1092 result[length] = '\0';
1101 maxlen = allocated - length;
1106 # define SNPRINTF_BUF(arg) \
1107 switch (prefix_count) \
1110 retcount = SNPRINTF (result + length, maxlen, buf, \
1114 retcount = SNPRINTF (result + length, maxlen, buf, \
1115 prefixes[0], arg, &count); \
1118 retcount = SNPRINTF (result + length, maxlen, buf, \
1119 prefixes[0], prefixes[1], arg, \
1126 # define SNPRINTF_BUF(arg) \
1127 switch (prefix_count) \
1130 count = sprintf (tmp, buf, arg); \
1133 count = sprintf (tmp, buf, prefixes[0], arg); \
1136 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1148 int arg = a.arg[dp->arg_index].a.a_schar;
1154 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1160 int arg = a.arg[dp->arg_index].a.a_short;
1166 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1172 int arg = a.arg[dp->arg_index].a.a_int;
1178 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1184 long int arg = a.arg[dp->arg_index].a.a_longint;
1190 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1194 #if HAVE_LONG_LONG_INT
1195 case TYPE_LONGLONGINT:
1197 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1201 case TYPE_ULONGLONGINT:
1203 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1210 double arg = a.arg[dp->arg_index].a.a_double;
1214 #if HAVE_LONG_DOUBLE
1215 case TYPE_LONGDOUBLE:
1217 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1224 int arg = a.arg[dp->arg_index].a.a_char;
1229 case TYPE_WIDE_CHAR:
1231 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1238 const char *arg = a.arg[dp->arg_index].a.a_string;
1243 case TYPE_WIDE_STRING:
1245 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1252 void *arg = a.arg[dp->arg_index].a.a_pointer;
1261 /* Portability: Not all implementations of snprintf()
1262 are ISO C 99 compliant. Determine the number of
1263 bytes that snprintf() has produced or would have
1267 /* Verify that snprintf() has NUL-terminated its
1269 if (count < maxlen && result[length + count] != '\0')
1271 /* Portability hack. */
1272 if (retcount > count)
1277 /* snprintf() doesn't understand the '%n'
1281 /* Don't use the '%n' directive; instead, look
1282 at the snprintf() return value. */
1288 /* Look at the snprintf() return value. */
1291 /* HP-UX 10.20 snprintf() is doubly deficient:
1292 It doesn't understand the '%n' directive,
1293 *and* it returns -1 (rather than the length
1294 that would have been required) when the
1295 buffer is too small. */
1296 size_t bigger_need =
1297 xsum (xtimes (allocated, 2), 12);
1298 ENSURE_ALLOCATION (bigger_need);
1307 /* Attempt to handle failure. */
1310 if (!(result == resultbuf || result == NULL))
1312 if (buf_malloced != NULL)
1313 free (buf_malloced);
1320 if (count >= tmp_length)
1321 /* tmp_length was incorrectly calculated - fix the
1326 /* Make room for the result. */
1327 if (count >= maxlen)
1329 /* Need at least count bytes. But allocate
1330 proportionally, to avoid looping eternally if
1331 snprintf() reports a too small count. */
1333 xmax (xsum (length, count), xtimes (allocated, 2));
1335 ENSURE_ALLOCATION (n);
1342 /* The snprintf() result did fit. */
1344 /* Append the sprintf() result. */
1345 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1357 /* Add the final NUL. */
1358 ENSURE_ALLOCATION (xsum (length, 1));
1359 result[length] = '\0';
1361 if (result != resultbuf && length + 1 < allocated)
1363 /* Shrink the allocated memory if possible. */
1366 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1371 if (buf_malloced != NULL)
1372 free (buf_malloced);
1375 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1376 says that snprintf() fails with errno = EOVERFLOW in this case, but
1377 that's only because snprintf() returns an 'int'. This function does
1378 not have this limitation. */
1382 if (!(result == resultbuf || result == NULL))
1384 if (buf_malloced != NULL)
1385 free (buf_malloced);