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"
64 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
66 # define EOVERFLOW E2BIG
71 # define local_wcslen wcslen
73 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
74 a dependency towards this library, here is a local substitute.
75 Define this substitute only once, even if this file is included
76 twice in the same compilation unit. */
77 # ifndef local_wcslen_defined
78 # define local_wcslen_defined 1
80 local_wcslen (const wchar_t *s)
84 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
93 # define VASNPRINTF vasnwprintf
94 # define CHAR_T wchar_t
95 # define DIRECTIVE wchar_t_directive
96 # define DIRECTIVES wchar_t_directives
97 # define PRINTF_PARSE wprintf_parse
98 # define USE_SNPRINTF 1
99 # if HAVE_DECL__SNWPRINTF
100 /* On Windows, the function swprintf() has a different signature than
101 on Unix; we use the _snwprintf() function instead. */
102 # define SNPRINTF _snwprintf
105 # define SNPRINTF swprintf
108 # define VASNPRINTF vasnprintf
110 # define DIRECTIVE char_directive
111 # define DIRECTIVES char_directives
112 # define PRINTF_PARSE printf_parse
113 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
114 # if HAVE_DECL__SNPRINTF
116 # define SNPRINTF _snprintf
119 # define SNPRINTF snprintf
120 /* Here we need to call the native snprintf, not rpl_snprintf. */
124 /* Here we need to call the native sprintf, not rpl_sprintf. */
128 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
133 if (PRINTF_PARSE (format, &d, &a) < 0)
144 if (printf_fetchargs (args, &a) < 0)
152 size_t buf_neededlength;
154 CHAR_T *buf_malloced;
158 /* Output string accumulator. */
163 /* Allocate a small buffer that will hold a directive passed to
164 sprintf or snprintf. */
166 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
168 if (buf_neededlength < 4000 / sizeof (CHAR_T))
170 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
176 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
177 if (size_overflow_p (buf_memsize))
178 goto out_of_memory_1;
179 buf = (CHAR_T *) malloc (buf_memsize);
181 goto out_of_memory_1;
185 if (resultbuf != NULL)
188 allocated = *lengthp;
197 result is either == resultbuf or == NULL or malloc-allocated.
198 If length > 0, then result != NULL. */
200 /* Ensures that allocated >= needed. Aborts through a jump to
201 out_of_memory if needed is SIZE_MAX or otherwise too big. */
202 #define ENSURE_ALLOCATION(needed) \
203 if ((needed) > allocated) \
205 size_t memory_size; \
208 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
209 if ((needed) > allocated) \
210 allocated = (needed); \
211 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
212 if (size_overflow_p (memory_size)) \
213 goto out_of_memory; \
214 if (result == resultbuf || result == NULL) \
215 memory = (CHAR_T *) malloc (memory_size); \
217 memory = (CHAR_T *) realloc (result, memory_size); \
218 if (memory == NULL) \
219 goto out_of_memory; \
220 if (result == resultbuf && length > 0) \
221 memcpy (memory, result, length * sizeof (CHAR_T)); \
225 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
227 if (cp != dp->dir_start)
229 size_t n = dp->dir_start - cp;
230 size_t augmented_length = xsum (length, n);
232 ENSURE_ALLOCATION (augmented_length);
233 memcpy (result + length, cp, n * sizeof (CHAR_T));
234 length = augmented_length;
239 /* Execute a single directive. */
240 if (dp->conversion == '%')
242 size_t augmented_length;
244 if (!(dp->arg_index == ARG_NONE))
246 augmented_length = xsum (length, 1);
247 ENSURE_ALLOCATION (augmented_length);
248 result[length] = '%';
249 length = augmented_length;
253 if (!(dp->arg_index != ARG_NONE))
256 if (dp->conversion == 'n')
258 switch (a.arg[dp->arg_index].type)
260 case TYPE_COUNT_SCHAR_POINTER:
261 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
263 case TYPE_COUNT_SHORT_POINTER:
264 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
266 case TYPE_COUNT_INT_POINTER:
267 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
269 case TYPE_COUNT_LONGINT_POINTER:
270 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
272 #if HAVE_LONG_LONG_INT
273 case TYPE_COUNT_LONGLONGINT_POINTER:
274 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
281 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
282 else if (dp->conversion == 'a' || dp->conversion == 'A')
284 arg_type type = a.arg[dp->arg_index].type;
285 int flags = dp->flags;
298 if (dp->width_start != dp->width_end)
300 if (dp->width_arg_index != ARG_NONE)
304 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
306 arg = a.arg[dp->width_arg_index].a.a_int;
309 /* "A negative field width is taken as a '-' flag
310 followed by a positive field width." */
312 width = (unsigned int) (-arg);
319 const CHAR_T *digitp = dp->width_start;
322 width = xsum (xtimes (width, 10), *digitp++ - '0');
323 while (digitp != dp->width_end);
330 if (dp->precision_start != dp->precision_end)
332 if (dp->precision_arg_index != ARG_NONE)
336 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
338 arg = a.arg[dp->precision_arg_index].a.a_int;
339 /* "A negative precision is taken as if the precision
349 const CHAR_T *digitp = dp->precision_start + 1;
352 while (digitp != dp->precision_end)
353 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
358 /* Allocate a temporary buffer of sufficient size. */
359 # if HAVE_LONG_DOUBLE
360 if (type == TYPE_LONGDOUBLE)
362 (unsigned int) ((LDBL_DIG + 1)
363 * 0.831 /* decimal -> hexadecimal */
365 + 1; /* turn floor into ceil */
369 (unsigned int) ((DBL_DIG + 1)
370 * 0.831 /* decimal -> hexadecimal */
372 + 1; /* turn floor into ceil */
373 if (tmp_length < precision)
374 tmp_length = precision;
375 /* Account for sign, decimal point etc. */
376 tmp_length = xsum (tmp_length, 12);
378 if (tmp_length < width)
381 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
383 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
387 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
389 if (size_overflow_p (tmp_memsize))
390 /* Overflow, would lead to out of memory. */
392 tmp = (CHAR_T *) malloc (tmp_memsize);
400 # if HAVE_LONG_DOUBLE
401 if (type == TYPE_LONGDOUBLE)
403 long double arg = a.arg[dp->arg_index].a.a_longdouble;
407 if (dp->conversion == 'A')
409 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
413 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
419 DECL_LONG_DOUBLE_ROUNDING
421 BEGIN_LONG_DOUBLE_ROUNDING ();
428 else if (arg == 0.0L)
430 /* Distinguish 0.0L and -0.0L. */
431 static long double plus_zero = 0.0L;
432 long double arg_mem = arg;
433 if (memcmp (&plus_zero, &arg_mem, SIZEOF_LDBL) != 0)
442 else if (flags & FLAG_SHOWSIGN)
444 else if (flags & FLAG_SPACE)
447 if (arg > 0.0L && arg + arg == arg)
449 if (dp->conversion == 'A')
451 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
455 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
461 long double mantissa;
464 mantissa = printf_frexpl (arg, &exponent);
472 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
474 /* Round the mantissa. */
475 long double tail = mantissa;
478 for (q = precision; ; q--)
480 int digit = (int) tail;
484 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
493 for (q = precision; q > 0; q--)
499 *p++ = dp->conversion - 'A' + 'X';
504 digit = (int) mantissa;
507 if ((flags & FLAG_ALT)
508 || mantissa > 0.0L || precision > 0)
511 localeconv () -> decimal_point;
512 /* The decimal point is always a single byte:
513 either '.' or ','. */
514 *p++ = (point[0] != '\0' ? point[0] : '.');
515 /* This loop terminates because we assume
516 that FLT_RADIX is a power of 2. */
517 while (mantissa > 0.0L)
520 digit = (int) mantissa;
525 : dp->conversion - 10);
529 while (precision > 0)
536 *p++ = dp->conversion - 'A' + 'P';
537 # if WIDE_CHAR_VERSION
539 static const wchar_t decimal_format[] =
540 { '%', '+', 'd', '\0' };
541 SNPRINTF (p, 6 + 1, decimal_format, exponent);
544 sprintf (p, "%+d", exponent);
550 END_LONG_DOUBLE_ROUNDING ();
556 double arg = a.arg[dp->arg_index].a.a_double;
560 if (dp->conversion == 'A')
562 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
566 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
580 /* Distinguish 0.0 and -0.0. */
581 static double plus_zero = 0.0;
582 double arg_mem = arg;
583 if (memcmp (&plus_zero, &arg_mem, SIZEOF_DBL) != 0)
592 else if (flags & FLAG_SHOWSIGN)
594 else if (flags & FLAG_SPACE)
597 if (arg > 0.0 && arg + arg == arg)
599 if (dp->conversion == 'A')
601 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
605 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
614 mantissa = printf_frexp (arg, &exponent);
622 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
624 /* Round the mantissa. */
625 double tail = mantissa;
628 for (q = precision; ; q--)
630 int digit = (int) tail;
634 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
643 for (q = precision; q > 0; q--)
649 *p++ = dp->conversion - 'A' + 'X';
654 digit = (int) mantissa;
657 if ((flags & FLAG_ALT)
658 || mantissa > 0.0 || precision > 0)
661 localeconv () -> decimal_point;
662 /* The decimal point is always a single byte:
663 either '.' or ','. */
664 *p++ = (point[0] != '\0' ? point[0] : '.');
665 /* This loop terminates because we assume
666 that FLT_RADIX is a power of 2. */
667 while (mantissa > 0.0)
670 digit = (int) mantissa;
675 : dp->conversion - 10);
679 while (precision > 0)
686 *p++ = dp->conversion - 'A' + 'P';
687 # if WIDE_CHAR_VERSION
689 static const wchar_t decimal_format[] =
690 { '%', '+', 'd', '\0' };
691 SNPRINTF (p, 6 + 1, decimal_format, exponent);
694 sprintf (p, "%+d", exponent);
701 /* The generated string now extends from tmp to p, with the
702 zero padding insertion point being at pad_ptr. */
703 if (has_width && p - tmp < width)
705 size_t pad = width - (p - tmp);
706 CHAR_T *end = p + pad;
708 if (flags & FLAG_LEFT)
710 /* Pad with spaces on the right. */
711 for (; pad > 0; pad--)
714 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
716 /* Pad with zeroes. */
721 for (; pad > 0; pad--)
726 /* Pad with spaces on the left. */
731 for (; pad > 0; pad--)
739 size_t count = p - tmp;
741 if (count >= tmp_length)
742 /* tmp_length was incorrectly calculated - fix the
746 /* Make room for the result. */
747 if (count >= allocated - length)
749 size_t n = xsum (length, count);
751 ENSURE_ALLOCATION (n);
754 /* Append the result. */
755 memcpy (result + length, tmp, count * sizeof (CHAR_T));
764 arg_type type = a.arg[dp->arg_index].type;
766 unsigned int prefix_count;
773 /* Allocate a temporary buffer of sufficient size for calling
780 if (dp->width_start != dp->width_end)
782 if (dp->width_arg_index != ARG_NONE)
786 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
788 arg = a.arg[dp->width_arg_index].a.a_int;
789 width = (arg < 0 ? (unsigned int) (-arg) : arg);
793 const CHAR_T *digitp = dp->width_start;
796 width = xsum (xtimes (width, 10), *digitp++ - '0');
797 while (digitp != dp->width_end);
802 if (dp->precision_start != dp->precision_end)
804 if (dp->precision_arg_index != ARG_NONE)
808 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
810 arg = a.arg[dp->precision_arg_index].a.a_int;
811 precision = (arg < 0 ? 0 : arg);
815 const CHAR_T *digitp = dp->precision_start + 1;
818 while (digitp != dp->precision_end)
819 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
823 switch (dp->conversion)
826 case 'd': case 'i': case 'u':
827 # if HAVE_LONG_LONG_INT
828 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
830 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
831 * 0.30103 /* binary -> decimal */
833 + 1; /* turn floor into ceil */
836 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
838 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
839 * 0.30103 /* binary -> decimal */
841 + 1; /* turn floor into ceil */
844 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
845 * 0.30103 /* binary -> decimal */
847 + 1; /* turn floor into ceil */
848 if (tmp_length < precision)
849 tmp_length = precision;
850 /* Multiply by 2, as an estimate for FLAG_GROUP. */
851 tmp_length = xsum (tmp_length, tmp_length);
852 /* Add 1, to account for a leading sign. */
853 tmp_length = xsum (tmp_length, 1);
857 # if HAVE_LONG_LONG_INT
858 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
860 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
861 * 0.333334 /* binary -> octal */
863 + 1; /* turn floor into ceil */
866 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
868 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
869 * 0.333334 /* binary -> octal */
871 + 1; /* turn floor into ceil */
874 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
875 * 0.333334 /* binary -> octal */
877 + 1; /* turn floor into ceil */
878 if (tmp_length < precision)
879 tmp_length = precision;
880 /* Add 1, to account for a leading sign. */
881 tmp_length = xsum (tmp_length, 1);
885 # if HAVE_LONG_LONG_INT
886 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
888 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
889 * 0.25 /* binary -> hexadecimal */
891 + 1; /* turn floor into ceil */
894 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
896 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
897 * 0.25 /* binary -> hexadecimal */
899 + 1; /* turn floor into ceil */
902 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
903 * 0.25 /* binary -> hexadecimal */
905 + 1; /* turn floor into ceil */
906 if (tmp_length < precision)
907 tmp_length = precision;
908 /* Add 2, to account for a leading sign or alternate form. */
909 tmp_length = xsum (tmp_length, 2);
913 # if HAVE_LONG_DOUBLE
914 if (type == TYPE_LONGDOUBLE)
916 (unsigned int) (LDBL_MAX_EXP
917 * 0.30103 /* binary -> decimal */
918 * 2 /* estimate for FLAG_GROUP */
920 + 1 /* turn floor into ceil */
921 + 10; /* sign, decimal point etc. */
925 (unsigned int) (DBL_MAX_EXP
926 * 0.30103 /* binary -> decimal */
927 * 2 /* estimate for FLAG_GROUP */
929 + 1 /* turn floor into ceil */
930 + 10; /* sign, decimal point etc. */
931 tmp_length = xsum (tmp_length, precision);
934 case 'e': case 'E': case 'g': case 'G':
936 12; /* sign, decimal point, exponent etc. */
937 tmp_length = xsum (tmp_length, precision);
941 # if HAVE_LONG_DOUBLE
942 if (type == TYPE_LONGDOUBLE)
944 (unsigned int) (LDBL_DIG
945 * 0.831 /* decimal -> hexadecimal */
947 + 1; /* turn floor into ceil */
951 (unsigned int) (DBL_DIG
952 * 0.831 /* decimal -> hexadecimal */
954 + 1; /* turn floor into ceil */
955 if (tmp_length < precision)
956 tmp_length = precision;
957 /* Account for sign, decimal point etc. */
958 tmp_length = xsum (tmp_length, 12);
962 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
963 if (type == TYPE_WIDE_CHAR)
964 tmp_length = MB_CUR_MAX;
972 if (type == TYPE_WIDE_STRING)
975 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
977 # if !WIDE_CHAR_VERSION
978 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
983 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
988 (unsigned int) (sizeof (void *) * CHAR_BIT
989 * 0.25 /* binary -> hexadecimal */
991 + 1 /* turn floor into ceil */
992 + 2; /* account for leading 0x */
999 if (tmp_length < width)
1002 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1005 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
1009 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1011 if (size_overflow_p (tmp_memsize))
1012 /* Overflow, would lead to out of memory. */
1014 tmp = (CHAR_T *) malloc (tmp_memsize);
1016 /* Out of memory. */
1021 /* Construct the format string for calling snprintf or
1025 if (dp->flags & FLAG_GROUP)
1027 if (dp->flags & FLAG_LEFT)
1029 if (dp->flags & FLAG_SHOWSIGN)
1031 if (dp->flags & FLAG_SPACE)
1033 if (dp->flags & FLAG_ALT)
1035 if (dp->flags & FLAG_ZERO)
1037 if (dp->width_start != dp->width_end)
1039 size_t n = dp->width_end - dp->width_start;
1040 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1043 if (dp->precision_start != dp->precision_end)
1045 size_t n = dp->precision_end - dp->precision_start;
1046 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1052 #if HAVE_LONG_LONG_INT
1053 case TYPE_LONGLONGINT:
1054 case TYPE_ULONGLONGINT:
1061 case TYPE_WIDE_CHAR:
1064 case TYPE_WIDE_STRING:
1068 #if HAVE_LONG_DOUBLE
1069 case TYPE_LONGDOUBLE:
1076 *p = dp->conversion;
1085 /* Construct the arguments for calling snprintf or sprintf. */
1087 if (dp->width_arg_index != ARG_NONE)
1089 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1091 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1093 if (dp->precision_arg_index != ARG_NONE)
1095 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1097 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1101 /* Prepare checking whether snprintf returns the count
1103 ENSURE_ALLOCATION (xsum (length, 1));
1104 result[length] = '\0';
1113 maxlen = allocated - length;
1118 /* SNPRINTF can fail if maxlen > INT_MAX. */
1119 if (maxlen > INT_MAX)
1121 # define SNPRINTF_BUF(arg) \
1122 switch (prefix_count) \
1125 retcount = SNPRINTF (result + length, maxlen, buf, \
1129 retcount = SNPRINTF (result + length, maxlen, buf, \
1130 prefixes[0], arg, &count); \
1133 retcount = SNPRINTF (result + length, maxlen, buf, \
1134 prefixes[0], prefixes[1], arg, \
1141 # define SNPRINTF_BUF(arg) \
1142 switch (prefix_count) \
1145 count = sprintf (tmp, buf, arg); \
1148 count = sprintf (tmp, buf, prefixes[0], arg); \
1151 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1163 int arg = a.arg[dp->arg_index].a.a_schar;
1169 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1175 int arg = a.arg[dp->arg_index].a.a_short;
1181 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1187 int arg = a.arg[dp->arg_index].a.a_int;
1193 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1199 long int arg = a.arg[dp->arg_index].a.a_longint;
1205 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1209 #if HAVE_LONG_LONG_INT
1210 case TYPE_LONGLONGINT:
1212 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1216 case TYPE_ULONGLONGINT:
1218 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1225 double arg = a.arg[dp->arg_index].a.a_double;
1229 #if HAVE_LONG_DOUBLE
1230 case TYPE_LONGDOUBLE:
1232 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1239 int arg = a.arg[dp->arg_index].a.a_char;
1244 case TYPE_WIDE_CHAR:
1246 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1253 const char *arg = a.arg[dp->arg_index].a.a_string;
1258 case TYPE_WIDE_STRING:
1260 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1267 void *arg = a.arg[dp->arg_index].a.a_pointer;
1276 /* Portability: Not all implementations of snprintf()
1277 are ISO C 99 compliant. Determine the number of
1278 bytes that snprintf() has produced or would have
1282 /* Verify that snprintf() has NUL-terminated its
1284 if (count < maxlen && result[length + count] != '\0')
1286 /* Portability hack. */
1287 if (retcount > count)
1292 /* snprintf() doesn't understand the '%n'
1296 /* Don't use the '%n' directive; instead, look
1297 at the snprintf() return value. */
1303 /* Look at the snprintf() return value. */
1306 /* HP-UX 10.20 snprintf() is doubly deficient:
1307 It doesn't understand the '%n' directive,
1308 *and* it returns -1 (rather than the length
1309 that would have been required) when the
1310 buffer is too small. */
1311 size_t bigger_need =
1312 xsum (xtimes (allocated, 2), 12);
1313 ENSURE_ALLOCATION (bigger_need);
1322 /* Attempt to handle failure. */
1325 if (!(result == resultbuf || result == NULL))
1327 if (buf_malloced != NULL)
1328 free (buf_malloced);
1335 if (count >= tmp_length)
1336 /* tmp_length was incorrectly calculated - fix the
1341 /* Make room for the result. */
1342 if (count >= maxlen)
1344 /* Need at least count bytes. But allocate
1345 proportionally, to avoid looping eternally if
1346 snprintf() reports a too small count. */
1348 xmax (xsum (length, count), xtimes (allocated, 2));
1350 ENSURE_ALLOCATION (n);
1357 /* The snprintf() result did fit. */
1359 /* Append the sprintf() result. */
1360 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1372 /* Add the final NUL. */
1373 ENSURE_ALLOCATION (xsum (length, 1));
1374 result[length] = '\0';
1376 if (result != resultbuf && length + 1 < allocated)
1378 /* Shrink the allocated memory if possible. */
1381 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1386 if (buf_malloced != NULL)
1387 free (buf_malloced);
1390 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1391 says that snprintf() fails with errno = EOVERFLOW in this case, but
1392 that's only because snprintf() returns an 'int'. This function does
1393 not have this limitation. */
1397 if (!(result == resultbuf || result == NULL))
1399 if (buf_malloced != NULL)
1400 free (buf_malloced);
1406 if (!(result == resultbuf || result == NULL))
1408 if (buf_malloced != NULL)
1409 free (buf_malloced);