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
55 # include "printf-frexp.h"
57 # include "isnanl-nolibm.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;
195 result is either == resultbuf or == NULL or malloc-allocated.
196 If length > 0, then result != NULL. */
198 /* Ensures that allocated >= needed. Aborts through a jump to
199 out_of_memory if needed is SIZE_MAX or otherwise too big. */
200 #define ENSURE_ALLOCATION(needed) \
201 if ((needed) > allocated) \
203 size_t memory_size; \
206 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
207 if ((needed) > allocated) \
208 allocated = (needed); \
209 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
210 if (size_overflow_p (memory_size)) \
211 goto out_of_memory; \
212 if (result == resultbuf || result == NULL) \
213 memory = (CHAR_T *) malloc (memory_size); \
215 memory = (CHAR_T *) realloc (result, memory_size); \
216 if (memory == NULL) \
217 goto out_of_memory; \
218 if (result == resultbuf && length > 0) \
219 memcpy (memory, result, length * sizeof (CHAR_T)); \
223 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
225 if (cp != dp->dir_start)
227 size_t n = dp->dir_start - cp;
228 size_t augmented_length = xsum (length, n);
230 ENSURE_ALLOCATION (augmented_length);
231 memcpy (result + length, cp, n * sizeof (CHAR_T));
232 length = augmented_length;
237 /* Execute a single directive. */
238 if (dp->conversion == '%')
240 size_t augmented_length;
242 if (!(dp->arg_index == ARG_NONE))
244 augmented_length = xsum (length, 1);
245 ENSURE_ALLOCATION (augmented_length);
246 result[length] = '%';
247 length = augmented_length;
251 if (!(dp->arg_index != ARG_NONE))
254 if (dp->conversion == 'n')
256 switch (a.arg[dp->arg_index].type)
258 case TYPE_COUNT_SCHAR_POINTER:
259 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
261 case TYPE_COUNT_SHORT_POINTER:
262 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
264 case TYPE_COUNT_INT_POINTER:
265 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
267 case TYPE_COUNT_LONGINT_POINTER:
268 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
270 #if HAVE_LONG_LONG_INT
271 case TYPE_COUNT_LONGLONGINT_POINTER:
272 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
279 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
280 else if (dp->conversion == 'a' || dp->conversion == 'A')
282 arg_type type = a.arg[dp->arg_index].type;
283 int flags = dp->flags;
296 if (dp->width_start != dp->width_end)
298 if (dp->width_arg_index != ARG_NONE)
302 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
304 arg = a.arg[dp->width_arg_index].a.a_int;
307 /* "A negative field width is taken as a '-' flag
308 followed by a positive field width." */
310 width = (unsigned int) (-arg);
317 const CHAR_T *digitp = dp->width_start;
320 width = xsum (xtimes (width, 10), *digitp++ - '0');
321 while (digitp != dp->width_end);
328 if (dp->precision_start != dp->precision_end)
330 if (dp->precision_arg_index != ARG_NONE)
334 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
336 arg = a.arg[dp->precision_arg_index].a.a_int;
337 /* "A negative precision is taken as if the precision
347 const CHAR_T *digitp = dp->precision_start + 1;
350 while (digitp != dp->precision_end)
351 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
356 /* Allocate a temporary buffer of sufficient size. */
357 # if HAVE_LONG_DOUBLE
358 if (type == TYPE_LONGDOUBLE)
360 (unsigned int) ((LDBL_DIG + 1)
361 * 0.831 /* decimal -> hexadecimal */
363 + 1; /* turn floor into ceil */
367 (unsigned int) ((DBL_DIG + 1)
368 * 0.831 /* decimal -> hexadecimal */
370 + 1; /* turn floor into ceil */
371 if (tmp_length < precision)
372 tmp_length = precision;
373 /* Account for sign, decimal point etc. */
374 tmp_length = xsum (tmp_length, 12);
376 if (tmp_length < width)
379 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
381 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
385 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
387 if (size_overflow_p (tmp_memsize))
388 /* Overflow, would lead to out of memory. */
390 tmp = (CHAR_T *) malloc (tmp_memsize);
398 # if HAVE_LONG_DOUBLE
399 if (type == TYPE_LONGDOUBLE)
401 long double arg = a.arg[dp->arg_index].a.a_longdouble;
405 if (dp->conversion == 'A')
407 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
411 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
423 else if (arg == 0.0L)
425 /* Distinguish 0.0L and -0.0L. */
426 static long double plus_zero = 0.0L;
428 memset (&arg_mem, 0, sizeof (long double));
430 if (memcmp (&plus_zero, &arg_mem, sizeof (long double)) != 0)
439 else if (flags & FLAG_SHOWSIGN)
441 else if (flags & FLAG_SPACE)
444 if (arg > 0.0L && arg + arg == arg)
446 if (dp->conversion == 'A')
448 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
452 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
458 long double mantissa;
461 mantissa = printf_frexpl (arg, &exponent);
469 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
471 /* Round the mantissa. */
472 long double tail = mantissa;
475 for (q = precision; ; q--)
477 int digit = (int) tail;
481 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
490 for (q = precision; q > 0; q--)
496 *p++ = dp->conversion - 'A' + 'X';
501 digit = (int) mantissa;
504 if ((flags & FLAG_ALT)
505 || mantissa > 0.0L || precision > 0)
508 localeconv () -> decimal_point;
509 /* The decimal point is always a single byte:
510 either '.' or ','. */
511 *p++ = (point[0] != '\0' ? point[0] : '.');
512 /* This loop terminates because we assume
513 that FLT_RADIX is a power of 2. */
514 while (mantissa > 0.0L)
517 digit = (int) mantissa;
522 : dp->conversion - 10);
526 while (precision > 0)
533 *p++ = dp->conversion - 'A' + 'P';
534 # if WIDE_CHAR_VERSION
536 static const wchar_t decimal_format[] =
537 { '%', '+', 'd', '\0' };
538 SNPRINTF (p, 6 + 1, decimal_format, exponent);
541 sprintf (p, "%+d", exponent);
551 double arg = a.arg[dp->arg_index].a.a_double;
555 if (dp->conversion == 'A')
557 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
561 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
575 /* Distinguish 0.0 and -0.0. */
576 static double plus_zero = 0.0;
578 memset (&arg_mem, 0, sizeof (double));
580 if (memcmp (&plus_zero, &arg_mem, sizeof (double)) != 0)
589 else if (flags & FLAG_SHOWSIGN)
591 else if (flags & FLAG_SPACE)
594 if (arg > 0.0 && arg + arg == arg)
596 if (dp->conversion == 'A')
598 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
602 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
611 mantissa = printf_frexp (arg, &exponent);
619 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
621 /* Round the mantissa. */
622 double tail = mantissa;
625 for (q = precision; ; q--)
627 int digit = (int) tail;
631 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
640 for (q = precision; q > 0; q--)
646 *p++ = dp->conversion - 'A' + 'X';
651 digit = (int) mantissa;
654 if ((flags & FLAG_ALT)
655 || mantissa > 0.0 || precision > 0)
658 localeconv () -> decimal_point;
659 /* The decimal point is always a single byte:
660 either '.' or ','. */
661 *p++ = (point[0] != '\0' ? point[0] : '.');
662 /* This loop terminates because we assume
663 that FLT_RADIX is a power of 2. */
664 while (mantissa > 0.0)
667 digit = (int) mantissa;
672 : dp->conversion - 10);
676 while (precision > 0)
683 *p++ = dp->conversion - 'A' + 'P';
684 # if WIDE_CHAR_VERSION
686 static const wchar_t decimal_format[] =
687 { '%', '+', 'd', '\0' };
688 SNPRINTF (p, 6 + 1, decimal_format, exponent);
691 sprintf (p, "%+d", exponent);
698 /* The generated string now extends from tmp to p, with the
699 zero padding insertion point being at pad_ptr. */
700 if (has_width && p - tmp < width)
702 size_t pad = width - (p - tmp);
703 CHAR_T *end = p + pad;
705 if (flags & FLAG_LEFT)
707 /* Pad with spaces on the right. */
708 for (; pad > 0; pad--)
711 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
713 /* Pad with zeroes. */
718 for (; pad > 0; pad--)
723 /* Pad with spaces on the left. */
728 for (; pad > 0; pad--)
736 size_t count = p - tmp;
738 if (count >= tmp_length)
739 /* tmp_length was incorrectly calculated - fix the
743 /* Make room for the result. */
744 if (count >= allocated - length)
746 size_t n = xsum (length, count);
748 ENSURE_ALLOCATION (n);
751 /* Append the result. */
752 memcpy (result + length, tmp, count * sizeof (CHAR_T));
761 arg_type type = a.arg[dp->arg_index].type;
763 unsigned int prefix_count;
770 /* Allocate a temporary buffer of sufficient size for calling
777 if (dp->width_start != dp->width_end)
779 if (dp->width_arg_index != ARG_NONE)
783 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
785 arg = a.arg[dp->width_arg_index].a.a_int;
786 width = (arg < 0 ? (unsigned int) (-arg) : arg);
790 const CHAR_T *digitp = dp->width_start;
793 width = xsum (xtimes (width, 10), *digitp++ - '0');
794 while (digitp != dp->width_end);
799 if (dp->precision_start != dp->precision_end)
801 if (dp->precision_arg_index != ARG_NONE)
805 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
807 arg = a.arg[dp->precision_arg_index].a.a_int;
808 precision = (arg < 0 ? 0 : arg);
812 const CHAR_T *digitp = dp->precision_start + 1;
815 while (digitp != dp->precision_end)
816 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
820 switch (dp->conversion)
823 case 'd': case 'i': case 'u':
824 # if HAVE_LONG_LONG_INT
825 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
827 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
828 * 0.30103 /* binary -> decimal */
830 + 1; /* turn floor into ceil */
833 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
835 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
836 * 0.30103 /* binary -> decimal */
838 + 1; /* turn floor into ceil */
841 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
842 * 0.30103 /* binary -> decimal */
844 + 1; /* turn floor into ceil */
845 if (tmp_length < precision)
846 tmp_length = precision;
847 /* Multiply by 2, as an estimate for FLAG_GROUP. */
848 tmp_length = xsum (tmp_length, tmp_length);
849 /* Add 1, to account for a leading sign. */
850 tmp_length = xsum (tmp_length, 1);
854 # if HAVE_LONG_LONG_INT
855 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
857 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
858 * 0.333334 /* binary -> octal */
860 + 1; /* turn floor into ceil */
863 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
865 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
866 * 0.333334 /* binary -> octal */
868 + 1; /* turn floor into ceil */
871 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
872 * 0.333334 /* binary -> octal */
874 + 1; /* turn floor into ceil */
875 if (tmp_length < precision)
876 tmp_length = precision;
877 /* Add 1, to account for a leading sign. */
878 tmp_length = xsum (tmp_length, 1);
882 # if HAVE_LONG_LONG_INT
883 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
885 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
886 * 0.25 /* binary -> hexadecimal */
888 + 1; /* turn floor into ceil */
891 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
893 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
894 * 0.25 /* binary -> hexadecimal */
896 + 1; /* turn floor into ceil */
899 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
900 * 0.25 /* binary -> hexadecimal */
902 + 1; /* turn floor into ceil */
903 if (tmp_length < precision)
904 tmp_length = precision;
905 /* Add 2, to account for a leading sign or alternate form. */
906 tmp_length = xsum (tmp_length, 2);
910 # if HAVE_LONG_DOUBLE
911 if (type == TYPE_LONGDOUBLE)
913 (unsigned int) (LDBL_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. */
922 (unsigned int) (DBL_MAX_EXP
923 * 0.30103 /* binary -> decimal */
924 * 2 /* estimate for FLAG_GROUP */
926 + 1 /* turn floor into ceil */
927 + 10; /* sign, decimal point etc. */
928 tmp_length = xsum (tmp_length, precision);
931 case 'e': case 'E': case 'g': case 'G':
933 12; /* sign, decimal point, exponent etc. */
934 tmp_length = xsum (tmp_length, precision);
938 # if HAVE_LONG_DOUBLE
939 if (type == TYPE_LONGDOUBLE)
941 (unsigned int) (LDBL_DIG
942 * 0.831 /* decimal -> hexadecimal */
944 + 1; /* turn floor into ceil */
948 (unsigned int) (DBL_DIG
949 * 0.831 /* decimal -> hexadecimal */
951 + 1; /* turn floor into ceil */
952 if (tmp_length < precision)
953 tmp_length = precision;
954 /* Account for sign, decimal point etc. */
955 tmp_length = xsum (tmp_length, 12);
959 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
960 if (type == TYPE_WIDE_CHAR)
961 tmp_length = MB_CUR_MAX;
969 if (type == TYPE_WIDE_STRING)
972 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
974 # if !WIDE_CHAR_VERSION
975 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
980 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
985 (unsigned int) (sizeof (void *) * CHAR_BIT
986 * 0.25 /* binary -> hexadecimal */
988 + 1 /* turn floor into ceil */
989 + 2; /* account for leading 0x */
996 if (tmp_length < width)
999 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1002 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
1006 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1008 if (size_overflow_p (tmp_memsize))
1009 /* Overflow, would lead to out of memory. */
1011 tmp = (CHAR_T *) malloc (tmp_memsize);
1013 /* Out of memory. */
1018 /* Construct the format string for calling snprintf or
1022 if (dp->flags & FLAG_GROUP)
1024 if (dp->flags & FLAG_LEFT)
1026 if (dp->flags & FLAG_SHOWSIGN)
1028 if (dp->flags & FLAG_SPACE)
1030 if (dp->flags & FLAG_ALT)
1032 if (dp->flags & FLAG_ZERO)
1034 if (dp->width_start != dp->width_end)
1036 size_t n = dp->width_end - dp->width_start;
1037 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1040 if (dp->precision_start != dp->precision_end)
1042 size_t n = dp->precision_end - dp->precision_start;
1043 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1049 #if HAVE_LONG_LONG_INT
1050 case TYPE_LONGLONGINT:
1051 case TYPE_ULONGLONGINT:
1058 case TYPE_WIDE_CHAR:
1061 case TYPE_WIDE_STRING:
1065 #if HAVE_LONG_DOUBLE
1066 case TYPE_LONGDOUBLE:
1073 *p = dp->conversion;
1082 /* Construct the arguments for calling snprintf or sprintf. */
1084 if (dp->width_arg_index != ARG_NONE)
1086 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1088 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1090 if (dp->precision_arg_index != ARG_NONE)
1092 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1094 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1098 /* Prepare checking whether snprintf returns the count
1100 ENSURE_ALLOCATION (xsum (length, 1));
1101 result[length] = '\0';
1110 maxlen = allocated - length;
1115 /* SNPRINTF can fail if maxlen > INT_MAX. */
1116 if (maxlen > INT_MAX)
1118 # define SNPRINTF_BUF(arg) \
1119 switch (prefix_count) \
1122 retcount = SNPRINTF (result + length, maxlen, buf, \
1126 retcount = SNPRINTF (result + length, maxlen, buf, \
1127 prefixes[0], arg, &count); \
1130 retcount = SNPRINTF (result + length, maxlen, buf, \
1131 prefixes[0], prefixes[1], arg, \
1138 # define SNPRINTF_BUF(arg) \
1139 switch (prefix_count) \
1142 count = sprintf (tmp, buf, arg); \
1145 count = sprintf (tmp, buf, prefixes[0], arg); \
1148 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1160 int arg = a.arg[dp->arg_index].a.a_schar;
1166 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1172 int arg = a.arg[dp->arg_index].a.a_short;
1178 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1184 int arg = a.arg[dp->arg_index].a.a_int;
1190 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1196 long int arg = a.arg[dp->arg_index].a.a_longint;
1202 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1206 #if HAVE_LONG_LONG_INT
1207 case TYPE_LONGLONGINT:
1209 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1213 case TYPE_ULONGLONGINT:
1215 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1222 double arg = a.arg[dp->arg_index].a.a_double;
1226 #if HAVE_LONG_DOUBLE
1227 case TYPE_LONGDOUBLE:
1229 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1236 int arg = a.arg[dp->arg_index].a.a_char;
1241 case TYPE_WIDE_CHAR:
1243 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1250 const char *arg = a.arg[dp->arg_index].a.a_string;
1255 case TYPE_WIDE_STRING:
1257 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1264 void *arg = a.arg[dp->arg_index].a.a_pointer;
1273 /* Portability: Not all implementations of snprintf()
1274 are ISO C 99 compliant. Determine the number of
1275 bytes that snprintf() has produced or would have
1279 /* Verify that snprintf() has NUL-terminated its
1281 if (count < maxlen && result[length + count] != '\0')
1283 /* Portability hack. */
1284 if (retcount > count)
1289 /* snprintf() doesn't understand the '%n'
1293 /* Don't use the '%n' directive; instead, look
1294 at the snprintf() return value. */
1300 /* Look at the snprintf() return value. */
1303 /* HP-UX 10.20 snprintf() is doubly deficient:
1304 It doesn't understand the '%n' directive,
1305 *and* it returns -1 (rather than the length
1306 that would have been required) when the
1307 buffer is too small. */
1308 size_t bigger_need =
1309 xsum (xtimes (allocated, 2), 12);
1310 ENSURE_ALLOCATION (bigger_need);
1319 /* Attempt to handle failure. */
1322 if (!(result == resultbuf || result == NULL))
1324 if (buf_malloced != NULL)
1325 free (buf_malloced);
1332 if (count >= tmp_length)
1333 /* tmp_length was incorrectly calculated - fix the
1338 /* Make room for the result. */
1339 if (count >= maxlen)
1341 /* Need at least count bytes. But allocate
1342 proportionally, to avoid looping eternally if
1343 snprintf() reports a too small count. */
1345 xmax (xsum (length, count), xtimes (allocated, 2));
1347 ENSURE_ALLOCATION (n);
1354 /* The snprintf() result did fit. */
1356 /* Append the sprintf() result. */
1357 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1369 /* Add the final NUL. */
1370 ENSURE_ALLOCATION (xsum (length, 1));
1371 result[length] = '\0';
1373 if (result != resultbuf && length + 1 < allocated)
1375 /* Shrink the allocated memory if possible. */
1378 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1383 if (buf_malloced != NULL)
1384 free (buf_malloced);
1387 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1388 says that snprintf() fails with errno = EOVERFLOW in this case, but
1389 that's only because snprintf() returns an 'int'. This function does
1390 not have this limitation. */
1394 if (!(result == resultbuf || result == NULL))
1396 if (buf_malloced != NULL)
1397 free (buf_malloced);
1403 if (!(result == resultbuf || result == NULL))
1405 if (buf_malloced != NULL)
1406 free (buf_malloced);