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"
62 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
64 # define EOVERFLOW E2BIG
69 # define local_wcslen wcslen
71 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
72 a dependency towards this library, here is a local substitute.
73 Define this substitute only once, even if this file is included
74 twice in the same compilation unit. */
75 # ifndef local_wcslen_defined
76 # define local_wcslen_defined 1
78 local_wcslen (const wchar_t *s)
82 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
91 # define VASNPRINTF vasnwprintf
92 # define CHAR_T wchar_t
93 # define DIRECTIVE wchar_t_directive
94 # define DIRECTIVES wchar_t_directives
95 # define PRINTF_PARSE wprintf_parse
96 # define USE_SNPRINTF 1
97 # if HAVE_DECL__SNWPRINTF
98 /* On Windows, the function swprintf() has a different signature than
99 on Unix; we use the _snwprintf() function instead. */
100 # define SNPRINTF _snwprintf
103 # define SNPRINTF swprintf
106 # define VASNPRINTF vasnprintf
108 # define DIRECTIVE char_directive
109 # define DIRECTIVES char_directives
110 # define PRINTF_PARSE printf_parse
111 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
112 # if HAVE_DECL__SNPRINTF
114 # define SNPRINTF _snprintf
117 # define SNPRINTF snprintf
118 /* Here we need to call the native snprintf, not rpl_snprintf. */
122 /* Here we need to call the native sprintf, not rpl_sprintf. */
126 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
131 if (PRINTF_PARSE (format, &d, &a) < 0)
142 if (printf_fetchargs (args, &a) < 0)
150 size_t buf_neededlength;
152 CHAR_T *buf_malloced;
156 /* Output string accumulator. */
161 /* Allocate a small buffer that will hold a directive passed to
162 sprintf or snprintf. */
164 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
166 if (buf_neededlength < 4000 / sizeof (CHAR_T))
168 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
174 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
175 if (size_overflow_p (buf_memsize))
176 goto out_of_memory_1;
177 buf = (CHAR_T *) malloc (buf_memsize);
179 goto out_of_memory_1;
183 if (resultbuf != NULL)
186 allocated = *lengthp;
187 /* POSIX says that snprintf() fails with EOVERFLOW when the specified
188 buffer size is larger than INT_MAX. Let's do the same here. */
189 if (allocated > INT_MAX)
199 result is either == resultbuf or == NULL or malloc-allocated.
200 If length > 0, then result != NULL. */
202 /* Ensures that allocated >= needed. Aborts through a jump to
203 out_of_memory if needed is SIZE_MAX or otherwise too big. */
204 #define ENSURE_ALLOCATION(needed) \
205 if ((needed) > allocated) \
207 size_t memory_size; \
210 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
211 if ((needed) > allocated) \
212 allocated = (needed); \
213 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
214 if (size_overflow_p (memory_size)) \
215 goto out_of_memory; \
216 if (result == resultbuf || result == NULL) \
217 memory = (CHAR_T *) malloc (memory_size); \
219 memory = (CHAR_T *) realloc (result, memory_size); \
220 if (memory == NULL) \
221 goto out_of_memory; \
222 if (result == resultbuf && length > 0) \
223 memcpy (memory, result, length * sizeof (CHAR_T)); \
227 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
229 if (cp != dp->dir_start)
231 size_t n = dp->dir_start - cp;
232 size_t augmented_length = xsum (length, n);
234 ENSURE_ALLOCATION (augmented_length);
235 memcpy (result + length, cp, n * sizeof (CHAR_T));
236 length = augmented_length;
241 /* Execute a single directive. */
242 if (dp->conversion == '%')
244 size_t augmented_length;
246 if (!(dp->arg_index == ARG_NONE))
248 augmented_length = xsum (length, 1);
249 ENSURE_ALLOCATION (augmented_length);
250 result[length] = '%';
251 length = augmented_length;
255 if (!(dp->arg_index != ARG_NONE))
258 if (dp->conversion == 'n')
260 switch (a.arg[dp->arg_index].type)
262 case TYPE_COUNT_SCHAR_POINTER:
263 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
265 case TYPE_COUNT_SHORT_POINTER:
266 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
268 case TYPE_COUNT_INT_POINTER:
269 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
271 case TYPE_COUNT_LONGINT_POINTER:
272 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
274 #if HAVE_LONG_LONG_INT
275 case TYPE_COUNT_LONGLONGINT_POINTER:
276 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
283 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
284 else if (dp->conversion == 'a' || dp->conversion == 'A')
286 arg_type type = a.arg[dp->arg_index].type;
287 int flags = dp->flags;
300 if (dp->width_start != dp->width_end)
302 if (dp->width_arg_index != ARG_NONE)
306 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
308 arg = a.arg[dp->width_arg_index].a.a_int;
311 /* "A negative field width is taken as a '-' flag
312 followed by a positive field width." */
314 width = (unsigned int) (-arg);
321 const CHAR_T *digitp = dp->width_start;
324 width = xsum (xtimes (width, 10), *digitp++ - '0');
325 while (digitp != dp->width_end);
332 if (dp->precision_start != dp->precision_end)
334 if (dp->precision_arg_index != ARG_NONE)
338 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
340 arg = a.arg[dp->precision_arg_index].a.a_int;
341 /* "A negative precision is taken as if the precision
351 const CHAR_T *digitp = dp->precision_start + 1;
354 while (digitp != dp->precision_end)
355 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
360 /* Allocate a temporary buffer of sufficient size. */
361 # if HAVE_LONG_DOUBLE
362 if (type == TYPE_LONGDOUBLE)
364 (unsigned int) ((LDBL_DIG + 1)
365 * 0.831 /* decimal -> hexadecimal */
367 + 1; /* turn floor into ceil */
371 (unsigned int) ((DBL_DIG + 1)
372 * 0.831 /* decimal -> hexadecimal */
374 + 1; /* turn floor into ceil */
375 if (tmp_length < precision)
376 tmp_length = precision;
377 /* Account for sign, decimal point etc. */
378 tmp_length = xsum (tmp_length, 12);
380 if (tmp_length < width)
383 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
385 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
389 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
391 if (size_overflow_p (tmp_memsize))
392 /* Overflow, would lead to out of memory. */
394 tmp = (CHAR_T *) malloc (tmp_memsize);
402 # if HAVE_LONG_DOUBLE
403 if (type == TYPE_LONGDOUBLE)
405 long double arg = a.arg[dp->arg_index].a.a_longdouble;
409 if (dp->conversion == 'A')
411 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
415 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
427 else if (arg == 0.0L)
429 /* Distinguish 0.0L and -0.0L. */
430 static long double plus_zero = 0.0L;
432 memset (&arg_mem, 0, sizeof (long double));
434 if (memcmp (&plus_zero, &arg_mem, sizeof (long double)) != 0)
443 else if (flags & FLAG_SHOWSIGN)
445 else if (flags & FLAG_SPACE)
448 if (arg > 0.0L && arg + arg == arg)
450 if (dp->conversion == 'A')
452 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
456 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
462 long double mantissa;
465 mantissa = printf_frexpl (arg, &exponent);
473 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
475 /* Round the mantissa. */
476 long double tail = mantissa;
479 for (q = precision; ; q--)
481 int digit = (int) tail;
485 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
494 for (q = precision; q > 0; q--)
500 *p++ = dp->conversion - 'A' + 'X';
505 digit = (int) mantissa;
508 if ((flags & FLAG_ALT)
509 || mantissa > 0.0L || precision > 0)
512 localeconv () -> decimal_point;
513 /* The decimal point is always a single byte:
514 either '.' or ','. */
515 *p++ = (point[0] != '\0' ? point[0] : '.');
516 /* This loop terminates because we assume
517 that FLT_RADIX is a power of 2. */
518 while (mantissa > 0.0L)
521 digit = (int) mantissa;
526 : dp->conversion - 10);
530 while (precision > 0)
537 *p++ = dp->conversion - 'A' + 'P';
538 # if WIDE_CHAR_VERSION
540 static const wchar_t decimal_format[] =
541 { '%', '+', 'd', '\0' };
542 SNPRINTF (p, 6 + 1, decimal_format, exponent);
545 sprintf (p, "%+d", exponent);
555 double arg = a.arg[dp->arg_index].a.a_double;
559 if (dp->conversion == 'A')
561 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
565 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
579 /* Distinguish 0.0 and -0.0. */
580 static double plus_zero = 0.0;
582 memset (&arg_mem, 0, sizeof (double));
584 if (memcmp (&plus_zero, &arg_mem, sizeof (double)) != 0)
593 else if (flags & FLAG_SHOWSIGN)
595 else if (flags & FLAG_SPACE)
598 if (arg > 0.0 && arg + arg == arg)
600 if (dp->conversion == 'A')
602 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
606 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
615 mantissa = printf_frexp (arg, &exponent);
623 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
625 /* Round the mantissa. */
626 double tail = mantissa;
629 for (q = precision; ; q--)
631 int digit = (int) tail;
635 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
644 for (q = precision; q > 0; q--)
650 *p++ = dp->conversion - 'A' + 'X';
655 digit = (int) mantissa;
658 if ((flags & FLAG_ALT)
659 || mantissa > 0.0 || precision > 0)
662 localeconv () -> decimal_point;
663 /* The decimal point is always a single byte:
664 either '.' or ','. */
665 *p++ = (point[0] != '\0' ? point[0] : '.');
666 /* This loop terminates because we assume
667 that FLT_RADIX is a power of 2. */
668 while (mantissa > 0.0)
671 digit = (int) mantissa;
676 : dp->conversion - 10);
680 while (precision > 0)
687 *p++ = dp->conversion - 'A' + 'P';
688 # if WIDE_CHAR_VERSION
690 static const wchar_t decimal_format[] =
691 { '%', '+', 'd', '\0' };
692 SNPRINTF (p, 6 + 1, decimal_format, exponent);
695 sprintf (p, "%+d", exponent);
702 /* The generated string now extends from tmp to p, with the
703 zero padding insertion point being at pad_ptr. */
704 if (has_width && p - tmp < width)
706 size_t pad = width - (p - tmp);
707 CHAR_T *end = p + pad;
709 if (flags & FLAG_LEFT)
711 /* Pad with spaces on the right. */
712 for (; pad > 0; pad--)
715 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
717 /* Pad with zeroes. */
722 for (; pad > 0; pad--)
727 /* Pad with spaces on the left. */
732 for (; pad > 0; pad--)
740 size_t count = p - tmp;
742 if (count >= tmp_length)
743 /* tmp_length was incorrectly calculated - fix the
747 /* Make room for the result. */
748 if (count >= allocated - length)
750 size_t n = xsum (length, count);
752 ENSURE_ALLOCATION (n);
755 /* Append the result. */
756 memcpy (result + length, tmp, count * sizeof (CHAR_T));
765 arg_type type = a.arg[dp->arg_index].type;
767 unsigned int prefix_count;
774 /* Allocate a temporary buffer of sufficient size for calling
781 if (dp->width_start != dp->width_end)
783 if (dp->width_arg_index != ARG_NONE)
787 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
789 arg = a.arg[dp->width_arg_index].a.a_int;
790 width = (arg < 0 ? (unsigned int) (-arg) : arg);
794 const CHAR_T *digitp = dp->width_start;
797 width = xsum (xtimes (width, 10), *digitp++ - '0');
798 while (digitp != dp->width_end);
803 if (dp->precision_start != dp->precision_end)
805 if (dp->precision_arg_index != ARG_NONE)
809 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
811 arg = a.arg[dp->precision_arg_index].a.a_int;
812 precision = (arg < 0 ? 0 : arg);
816 const CHAR_T *digitp = dp->precision_start + 1;
819 while (digitp != dp->precision_end)
820 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
824 switch (dp->conversion)
827 case 'd': case 'i': case 'u':
828 # if HAVE_LONG_LONG_INT
829 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
831 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
832 * 0.30103 /* binary -> decimal */
834 + 1; /* turn floor into ceil */
837 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
839 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
840 * 0.30103 /* binary -> decimal */
842 + 1; /* turn floor into ceil */
845 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
846 * 0.30103 /* binary -> decimal */
848 + 1; /* turn floor into ceil */
849 if (tmp_length < precision)
850 tmp_length = precision;
851 /* Multiply by 2, as an estimate for FLAG_GROUP. */
852 tmp_length = xsum (tmp_length, tmp_length);
853 /* Add 1, to account for a leading sign. */
854 tmp_length = xsum (tmp_length, 1);
858 # if HAVE_LONG_LONG_INT
859 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
861 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
862 * 0.333334 /* binary -> octal */
864 + 1; /* turn floor into ceil */
867 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
869 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
870 * 0.333334 /* binary -> octal */
872 + 1; /* turn floor into ceil */
875 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
876 * 0.333334 /* binary -> octal */
878 + 1; /* turn floor into ceil */
879 if (tmp_length < precision)
880 tmp_length = precision;
881 /* Add 1, to account for a leading sign. */
882 tmp_length = xsum (tmp_length, 1);
886 # if HAVE_LONG_LONG_INT
887 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
889 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
890 * 0.25 /* binary -> hexadecimal */
892 + 1; /* turn floor into ceil */
895 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
897 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
898 * 0.25 /* binary -> hexadecimal */
900 + 1; /* turn floor into ceil */
903 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
904 * 0.25 /* binary -> hexadecimal */
906 + 1; /* turn floor into ceil */
907 if (tmp_length < precision)
908 tmp_length = precision;
909 /* Add 2, to account for a leading sign or alternate form. */
910 tmp_length = xsum (tmp_length, 2);
914 # if HAVE_LONG_DOUBLE
915 if (type == TYPE_LONGDOUBLE)
917 (unsigned int) (LDBL_MAX_EXP
918 * 0.30103 /* binary -> decimal */
919 * 2 /* estimate for FLAG_GROUP */
921 + 1 /* turn floor into ceil */
922 + 10; /* sign, decimal point etc. */
926 (unsigned int) (DBL_MAX_EXP
927 * 0.30103 /* binary -> decimal */
928 * 2 /* estimate for FLAG_GROUP */
930 + 1 /* turn floor into ceil */
931 + 10; /* sign, decimal point etc. */
932 tmp_length = xsum (tmp_length, precision);
935 case 'e': case 'E': case 'g': case 'G':
937 12; /* sign, decimal point, exponent etc. */
938 tmp_length = xsum (tmp_length, precision);
942 # if HAVE_LONG_DOUBLE
943 if (type == TYPE_LONGDOUBLE)
945 (unsigned int) (LDBL_DIG
946 * 0.831 /* decimal -> hexadecimal */
948 + 1; /* turn floor into ceil */
952 (unsigned int) (DBL_DIG
953 * 0.831 /* decimal -> hexadecimal */
955 + 1; /* turn floor into ceil */
956 if (tmp_length < precision)
957 tmp_length = precision;
958 /* Account for sign, decimal point etc. */
959 tmp_length = xsum (tmp_length, 12);
963 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
964 if (type == TYPE_WIDE_CHAR)
965 tmp_length = MB_CUR_MAX;
973 if (type == TYPE_WIDE_STRING)
976 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
978 # if !WIDE_CHAR_VERSION
979 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
984 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
989 (unsigned int) (sizeof (void *) * CHAR_BIT
990 * 0.25 /* binary -> hexadecimal */
992 + 1 /* turn floor into ceil */
993 + 2; /* account for leading 0x */
1000 if (tmp_length < width)
1003 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1006 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
1010 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1012 if (size_overflow_p (tmp_memsize))
1013 /* Overflow, would lead to out of memory. */
1015 tmp = (CHAR_T *) malloc (tmp_memsize);
1017 /* Out of memory. */
1022 /* Construct the format string for calling snprintf or
1026 if (dp->flags & FLAG_GROUP)
1028 if (dp->flags & FLAG_LEFT)
1030 if (dp->flags & FLAG_SHOWSIGN)
1032 if (dp->flags & FLAG_SPACE)
1034 if (dp->flags & FLAG_ALT)
1036 if (dp->flags & FLAG_ZERO)
1038 if (dp->width_start != dp->width_end)
1040 size_t n = dp->width_end - dp->width_start;
1041 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1044 if (dp->precision_start != dp->precision_end)
1046 size_t n = dp->precision_end - dp->precision_start;
1047 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1053 #if HAVE_LONG_LONG_INT
1054 case TYPE_LONGLONGINT:
1055 case TYPE_ULONGLONGINT:
1062 case TYPE_WIDE_CHAR:
1065 case TYPE_WIDE_STRING:
1069 #if HAVE_LONG_DOUBLE
1070 case TYPE_LONGDOUBLE:
1077 *p = dp->conversion;
1086 /* Construct the arguments for calling snprintf or sprintf. */
1088 if (dp->width_arg_index != ARG_NONE)
1090 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1092 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1094 if (dp->precision_arg_index != ARG_NONE)
1096 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1098 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1102 /* Prepare checking whether snprintf returns the count
1104 ENSURE_ALLOCATION (xsum (length, 1));
1105 result[length] = '\0';
1114 maxlen = allocated - length;
1119 /* SNPRINTF can fail if maxlen > INT_MAX. */
1120 if (maxlen > INT_MAX)
1122 # define SNPRINTF_BUF(arg) \
1123 switch (prefix_count) \
1126 retcount = SNPRINTF (result + length, maxlen, buf, \
1130 retcount = SNPRINTF (result + length, maxlen, buf, \
1131 prefixes[0], arg, &count); \
1134 retcount = SNPRINTF (result + length, maxlen, buf, \
1135 prefixes[0], prefixes[1], arg, \
1142 # define SNPRINTF_BUF(arg) \
1143 switch (prefix_count) \
1146 count = sprintf (tmp, buf, arg); \
1149 count = sprintf (tmp, buf, prefixes[0], arg); \
1152 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1164 int arg = a.arg[dp->arg_index].a.a_schar;
1170 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1176 int arg = a.arg[dp->arg_index].a.a_short;
1182 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1188 int arg = a.arg[dp->arg_index].a.a_int;
1194 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1200 long int arg = a.arg[dp->arg_index].a.a_longint;
1206 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1210 #if HAVE_LONG_LONG_INT
1211 case TYPE_LONGLONGINT:
1213 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1217 case TYPE_ULONGLONGINT:
1219 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1226 double arg = a.arg[dp->arg_index].a.a_double;
1230 #if HAVE_LONG_DOUBLE
1231 case TYPE_LONGDOUBLE:
1233 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1240 int arg = a.arg[dp->arg_index].a.a_char;
1245 case TYPE_WIDE_CHAR:
1247 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1254 const char *arg = a.arg[dp->arg_index].a.a_string;
1259 case TYPE_WIDE_STRING:
1261 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1268 void *arg = a.arg[dp->arg_index].a.a_pointer;
1277 /* Portability: Not all implementations of snprintf()
1278 are ISO C 99 compliant. Determine the number of
1279 bytes that snprintf() has produced or would have
1283 /* Verify that snprintf() has NUL-terminated its
1285 if (count < maxlen && result[length + count] != '\0')
1287 /* Portability hack. */
1288 if (retcount > count)
1293 /* snprintf() doesn't understand the '%n'
1297 /* Don't use the '%n' directive; instead, look
1298 at the snprintf() return value. */
1304 /* Look at the snprintf() return value. */
1307 /* HP-UX 10.20 snprintf() is doubly deficient:
1308 It doesn't understand the '%n' directive,
1309 *and* it returns -1 (rather than the length
1310 that would have been required) when the
1311 buffer is too small. */
1312 size_t bigger_need =
1313 xsum (xtimes (allocated, 2), 12);
1314 ENSURE_ALLOCATION (bigger_need);
1323 /* Attempt to handle failure. */
1326 if (!(result == resultbuf || result == NULL))
1328 if (buf_malloced != NULL)
1329 free (buf_malloced);
1336 if (count >= tmp_length)
1337 /* tmp_length was incorrectly calculated - fix the
1342 /* Make room for the result. */
1343 if (count >= maxlen)
1345 /* Need at least count bytes. But allocate
1346 proportionally, to avoid looping eternally if
1347 snprintf() reports a too small count. */
1349 xmax (xsum (length, count), xtimes (allocated, 2));
1351 ENSURE_ALLOCATION (n);
1358 /* The snprintf() result did fit. */
1360 /* Append the sprintf() result. */
1361 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1373 /* Add the final NUL. */
1374 ENSURE_ALLOCATION (xsum (length, 1));
1375 result[length] = '\0';
1377 if (result != resultbuf && length + 1 < allocated)
1379 /* Shrink the allocated memory if possible. */
1382 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1387 if (buf_malloced != NULL)
1388 free (buf_malloced);
1391 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1392 says that snprintf() fails with errno = EOVERFLOW in this case, but
1393 that's only because snprintf() returns an 'int'. This function does
1394 not have this limitation. */
1398 if (!(result == resultbuf || result == NULL))
1400 if (buf_malloced != NULL)
1401 free (buf_malloced);
1407 if (!(result == resultbuf || result == NULL))
1409 if (buf_malloced != NULL)
1410 free (buf_malloced);