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 <langinfo.h>
48 # include "wprintf-parse.h"
50 # include "printf-parse.h"
53 /* Checked size_t computations. */
56 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
59 # include "printf-frexp.h"
61 # include "isnanl-nolibm.h"
62 # include "printf-frexpl.h"
67 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
69 # define EOVERFLOW E2BIG
74 # define local_wcslen wcslen
76 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
77 a dependency towards this library, here is a local substitute.
78 Define this substitute only once, even if this file is included
79 twice in the same compilation unit. */
80 # ifndef local_wcslen_defined
81 # define local_wcslen_defined 1
83 local_wcslen (const wchar_t *s)
87 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
96 # define VASNPRINTF vasnwprintf
97 # define CHAR_T wchar_t
98 # define DIRECTIVE wchar_t_directive
99 # define DIRECTIVES wchar_t_directives
100 # define PRINTF_PARSE wprintf_parse
101 # define USE_SNPRINTF 1
102 # if HAVE_DECL__SNWPRINTF
103 /* On Windows, the function swprintf() has a different signature than
104 on Unix; we use the _snwprintf() function instead. */
105 # define SNPRINTF _snwprintf
108 # define SNPRINTF swprintf
111 # define VASNPRINTF vasnprintf
113 # define DIRECTIVE char_directive
114 # define DIRECTIVES char_directives
115 # define PRINTF_PARSE printf_parse
116 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
117 # if HAVE_DECL__SNPRINTF
119 # define SNPRINTF _snprintf
122 # define SNPRINTF snprintf
123 /* Here we need to call the native snprintf, not rpl_snprintf. */
127 /* Here we need to call the native sprintf, not rpl_sprintf. */
131 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
136 if (PRINTF_PARSE (format, &d, &a) < 0)
147 if (printf_fetchargs (args, &a) < 0)
155 size_t buf_neededlength;
157 CHAR_T *buf_malloced;
161 /* Output string accumulator. */
166 /* Allocate a small buffer that will hold a directive passed to
167 sprintf or snprintf. */
169 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
171 if (buf_neededlength < 4000 / sizeof (CHAR_T))
173 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
179 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
180 if (size_overflow_p (buf_memsize))
181 goto out_of_memory_1;
182 buf = (CHAR_T *) malloc (buf_memsize);
184 goto out_of_memory_1;
188 if (resultbuf != NULL)
191 allocated = *lengthp;
200 result is either == resultbuf or == NULL or malloc-allocated.
201 If length > 0, then result != NULL. */
203 /* Ensures that allocated >= needed. Aborts through a jump to
204 out_of_memory if needed is SIZE_MAX or otherwise too big. */
205 #define ENSURE_ALLOCATION(needed) \
206 if ((needed) > allocated) \
208 size_t memory_size; \
211 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
212 if ((needed) > allocated) \
213 allocated = (needed); \
214 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
215 if (size_overflow_p (memory_size)) \
216 goto out_of_memory; \
217 if (result == resultbuf || result == NULL) \
218 memory = (CHAR_T *) malloc (memory_size); \
220 memory = (CHAR_T *) realloc (result, memory_size); \
221 if (memory == NULL) \
222 goto out_of_memory; \
223 if (result == resultbuf && length > 0) \
224 memcpy (memory, result, length * sizeof (CHAR_T)); \
228 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
230 if (cp != dp->dir_start)
232 size_t n = dp->dir_start - cp;
233 size_t augmented_length = xsum (length, n);
235 ENSURE_ALLOCATION (augmented_length);
236 memcpy (result + length, cp, n * sizeof (CHAR_T));
237 length = augmented_length;
242 /* Execute a single directive. */
243 if (dp->conversion == '%')
245 size_t augmented_length;
247 if (!(dp->arg_index == ARG_NONE))
249 augmented_length = xsum (length, 1);
250 ENSURE_ALLOCATION (augmented_length);
251 result[length] = '%';
252 length = augmented_length;
256 if (!(dp->arg_index != ARG_NONE))
259 if (dp->conversion == 'n')
261 switch (a.arg[dp->arg_index].type)
263 case TYPE_COUNT_SCHAR_POINTER:
264 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
266 case TYPE_COUNT_SHORT_POINTER:
267 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
269 case TYPE_COUNT_INT_POINTER:
270 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
272 case TYPE_COUNT_LONGINT_POINTER:
273 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
275 #if HAVE_LONG_LONG_INT
276 case TYPE_COUNT_LONGLONGINT_POINTER:
277 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
284 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
285 else if (dp->conversion == 'a' || dp->conversion == 'A')
287 arg_type type = a.arg[dp->arg_index].type;
288 int flags = dp->flags;
301 if (dp->width_start != dp->width_end)
303 if (dp->width_arg_index != ARG_NONE)
307 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
309 arg = a.arg[dp->width_arg_index].a.a_int;
312 /* "A negative field width is taken as a '-' flag
313 followed by a positive field width." */
315 width = (unsigned int) (-arg);
322 const CHAR_T *digitp = dp->width_start;
325 width = xsum (xtimes (width, 10), *digitp++ - '0');
326 while (digitp != dp->width_end);
333 if (dp->precision_start != dp->precision_end)
335 if (dp->precision_arg_index != ARG_NONE)
339 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
341 arg = a.arg[dp->precision_arg_index].a.a_int;
342 /* "A negative precision is taken as if the precision
352 const CHAR_T *digitp = dp->precision_start + 1;
355 while (digitp != dp->precision_end)
356 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
361 /* Allocate a temporary buffer of sufficient size. */
362 # if HAVE_LONG_DOUBLE
363 if (type == TYPE_LONGDOUBLE)
365 (unsigned int) ((LDBL_DIG + 1)
366 * 0.831 /* decimal -> hexadecimal */
368 + 1; /* turn floor into ceil */
372 (unsigned int) ((DBL_DIG + 1)
373 * 0.831 /* decimal -> hexadecimal */
375 + 1; /* turn floor into ceil */
376 if (tmp_length < precision)
377 tmp_length = precision;
378 /* Account for sign, decimal point etc. */
379 tmp_length = xsum (tmp_length, 12);
381 if (tmp_length < width)
384 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
386 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
390 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
392 if (size_overflow_p (tmp_memsize))
393 /* Overflow, would lead to out of memory. */
395 tmp = (CHAR_T *) malloc (tmp_memsize);
403 # if HAVE_LONG_DOUBLE
404 if (type == TYPE_LONGDOUBLE)
406 long double arg = a.arg[dp->arg_index].a.a_longdouble;
410 if (dp->conversion == 'A')
412 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
416 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
422 DECL_LONG_DOUBLE_ROUNDING
424 BEGIN_LONG_DOUBLE_ROUNDING ();
431 else if (arg == 0.0L)
433 /* Distinguish 0.0L and -0.0L. */
434 static long double plus_zero = 0.0L;
435 long double arg_mem = arg;
436 if (memcmp (&plus_zero, &arg_mem, SIZEOF_LDBL) != 0)
445 else if (flags & FLAG_SHOWSIGN)
447 else if (flags & FLAG_SPACE)
450 if (arg > 0.0L && arg + arg == arg)
452 if (dp->conversion == 'A')
454 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
458 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
464 long double mantissa;
467 mantissa = printf_frexpl (arg, &exponent);
475 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
477 /* Round the mantissa. */
478 long double tail = mantissa;
481 for (q = precision; ; q--)
483 int digit = (int) tail;
487 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
496 for (q = precision; q > 0; q--)
502 *p++ = dp->conversion - 'A' + 'X';
507 digit = (int) mantissa;
510 if ((flags & FLAG_ALT)
511 || mantissa > 0.0L || precision > 0)
514 /* Prefer nl_langinfo() over localeconv(),
515 since the latter is not multithread-
517 # if HAVE_NL_LANGINFO
518 point = nl_langinfo (RADIXCHAR);
520 point = localeconv () -> decimal_point;
522 /* The decimal point is always a single byte:
523 either '.' or ','. */
524 *p++ = (point[0] != '\0' ? point[0] : '.');
525 /* This loop terminates because we assume
526 that FLT_RADIX is a power of 2. */
527 while (mantissa > 0.0L)
530 digit = (int) mantissa;
535 : dp->conversion - 10);
539 while (precision > 0)
546 *p++ = dp->conversion - 'A' + 'P';
547 # if WIDE_CHAR_VERSION
549 static const wchar_t decimal_format[] =
550 { '%', '+', 'd', '\0' };
551 SNPRINTF (p, 6 + 1, decimal_format, exponent);
554 sprintf (p, "%+d", exponent);
560 END_LONG_DOUBLE_ROUNDING ();
566 double arg = a.arg[dp->arg_index].a.a_double;
570 if (dp->conversion == 'A')
572 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
576 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
590 /* Distinguish 0.0 and -0.0. */
591 static double plus_zero = 0.0;
592 double arg_mem = arg;
593 if (memcmp (&plus_zero, &arg_mem, SIZEOF_DBL) != 0)
602 else if (flags & FLAG_SHOWSIGN)
604 else if (flags & FLAG_SPACE)
607 if (arg > 0.0 && arg + arg == arg)
609 if (dp->conversion == 'A')
611 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
615 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
624 mantissa = printf_frexp (arg, &exponent);
632 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
634 /* Round the mantissa. */
635 double tail = mantissa;
638 for (q = precision; ; q--)
640 int digit = (int) tail;
644 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
653 for (q = precision; q > 0; q--)
659 *p++ = dp->conversion - 'A' + 'X';
664 digit = (int) mantissa;
667 if ((flags & FLAG_ALT)
668 || mantissa > 0.0 || precision > 0)
671 /* Prefer nl_langinfo() over localeconv(),
672 since the latter is not multithread-
674 # if HAVE_NL_LANGINFO
675 point = nl_langinfo (RADIXCHAR);
677 point = localeconv () -> decimal_point;
679 /* The decimal point is always a single byte:
680 either '.' or ','. */
681 *p++ = (point[0] != '\0' ? point[0] : '.');
682 /* This loop terminates because we assume
683 that FLT_RADIX is a power of 2. */
684 while (mantissa > 0.0)
687 digit = (int) mantissa;
692 : dp->conversion - 10);
696 while (precision > 0)
703 *p++ = dp->conversion - 'A' + 'P';
704 # if WIDE_CHAR_VERSION
706 static const wchar_t decimal_format[] =
707 { '%', '+', 'd', '\0' };
708 SNPRINTF (p, 6 + 1, decimal_format, exponent);
711 sprintf (p, "%+d", exponent);
718 /* The generated string now extends from tmp to p, with the
719 zero padding insertion point being at pad_ptr. */
720 if (has_width && p - tmp < width)
722 size_t pad = width - (p - tmp);
723 CHAR_T *end = p + pad;
725 if (flags & FLAG_LEFT)
727 /* Pad with spaces on the right. */
728 for (; pad > 0; pad--)
731 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
733 /* Pad with zeroes. */
738 for (; pad > 0; pad--)
743 /* Pad with spaces on the left. */
748 for (; pad > 0; pad--)
756 size_t count = p - tmp;
758 if (count >= tmp_length)
759 /* tmp_length was incorrectly calculated - fix the
763 /* Make room for the result. */
764 if (count >= allocated - length)
766 size_t n = xsum (length, count);
768 ENSURE_ALLOCATION (n);
771 /* Append the result. */
772 memcpy (result + length, tmp, count * sizeof (CHAR_T));
781 arg_type type = a.arg[dp->arg_index].type;
783 unsigned int prefix_count;
790 /* Allocate a temporary buffer of sufficient size for calling
797 if (dp->width_start != dp->width_end)
799 if (dp->width_arg_index != ARG_NONE)
803 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
805 arg = a.arg[dp->width_arg_index].a.a_int;
806 width = (arg < 0 ? (unsigned int) (-arg) : arg);
810 const CHAR_T *digitp = dp->width_start;
813 width = xsum (xtimes (width, 10), *digitp++ - '0');
814 while (digitp != dp->width_end);
819 if (dp->precision_start != dp->precision_end)
821 if (dp->precision_arg_index != ARG_NONE)
825 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
827 arg = a.arg[dp->precision_arg_index].a.a_int;
828 precision = (arg < 0 ? 0 : arg);
832 const CHAR_T *digitp = dp->precision_start + 1;
835 while (digitp != dp->precision_end)
836 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
840 switch (dp->conversion)
843 case 'd': case 'i': case 'u':
844 # if HAVE_LONG_LONG_INT
845 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
847 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
848 * 0.30103 /* binary -> decimal */
850 + 1; /* turn floor into ceil */
853 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
855 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
856 * 0.30103 /* binary -> decimal */
858 + 1; /* turn floor into ceil */
861 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
862 * 0.30103 /* binary -> decimal */
864 + 1; /* turn floor into ceil */
865 if (tmp_length < precision)
866 tmp_length = precision;
867 /* Multiply by 2, as an estimate for FLAG_GROUP. */
868 tmp_length = xsum (tmp_length, tmp_length);
869 /* Add 1, to account for a leading sign. */
870 tmp_length = xsum (tmp_length, 1);
874 # if HAVE_LONG_LONG_INT
875 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
877 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
878 * 0.333334 /* binary -> octal */
880 + 1; /* turn floor into ceil */
883 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
885 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
886 * 0.333334 /* binary -> octal */
888 + 1; /* turn floor into ceil */
891 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
892 * 0.333334 /* binary -> octal */
894 + 1; /* turn floor into ceil */
895 if (tmp_length < precision)
896 tmp_length = precision;
897 /* Add 1, to account for a leading sign. */
898 tmp_length = xsum (tmp_length, 1);
902 # if HAVE_LONG_LONG_INT
903 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
905 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
906 * 0.25 /* binary -> hexadecimal */
908 + 1; /* turn floor into ceil */
911 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
913 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
914 * 0.25 /* binary -> hexadecimal */
916 + 1; /* turn floor into ceil */
919 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
920 * 0.25 /* binary -> hexadecimal */
922 + 1; /* turn floor into ceil */
923 if (tmp_length < precision)
924 tmp_length = precision;
925 /* Add 2, to account for a leading sign or alternate form. */
926 tmp_length = xsum (tmp_length, 2);
930 # if HAVE_LONG_DOUBLE
931 if (type == TYPE_LONGDOUBLE)
933 (unsigned int) (LDBL_MAX_EXP
934 * 0.30103 /* binary -> decimal */
935 * 2 /* estimate for FLAG_GROUP */
937 + 1 /* turn floor into ceil */
938 + 10; /* sign, decimal point etc. */
942 (unsigned int) (DBL_MAX_EXP
943 * 0.30103 /* binary -> decimal */
944 * 2 /* estimate for FLAG_GROUP */
946 + 1 /* turn floor into ceil */
947 + 10; /* sign, decimal point etc. */
948 tmp_length = xsum (tmp_length, precision);
951 case 'e': case 'E': case 'g': case 'G':
953 12; /* sign, decimal point, exponent etc. */
954 tmp_length = xsum (tmp_length, precision);
958 # if HAVE_LONG_DOUBLE
959 if (type == TYPE_LONGDOUBLE)
961 (unsigned int) (LDBL_DIG
962 * 0.831 /* decimal -> hexadecimal */
964 + 1; /* turn floor into ceil */
968 (unsigned int) (DBL_DIG
969 * 0.831 /* decimal -> hexadecimal */
971 + 1; /* turn floor into ceil */
972 if (tmp_length < precision)
973 tmp_length = precision;
974 /* Account for sign, decimal point etc. */
975 tmp_length = xsum (tmp_length, 12);
979 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
980 if (type == TYPE_WIDE_CHAR)
981 tmp_length = MB_CUR_MAX;
989 if (type == TYPE_WIDE_STRING)
992 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
994 # if !WIDE_CHAR_VERSION
995 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
1000 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
1005 (unsigned int) (sizeof (void *) * CHAR_BIT
1006 * 0.25 /* binary -> hexadecimal */
1008 + 1 /* turn floor into ceil */
1009 + 2; /* account for leading 0x */
1016 if (tmp_length < width)
1019 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1022 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
1026 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1028 if (size_overflow_p (tmp_memsize))
1029 /* Overflow, would lead to out of memory. */
1031 tmp = (CHAR_T *) malloc (tmp_memsize);
1033 /* Out of memory. */
1038 /* Construct the format string for calling snprintf or
1042 if (dp->flags & FLAG_GROUP)
1044 if (dp->flags & FLAG_LEFT)
1046 if (dp->flags & FLAG_SHOWSIGN)
1048 if (dp->flags & FLAG_SPACE)
1050 if (dp->flags & FLAG_ALT)
1052 if (dp->flags & FLAG_ZERO)
1054 if (dp->width_start != dp->width_end)
1056 size_t n = dp->width_end - dp->width_start;
1057 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1060 if (dp->precision_start != dp->precision_end)
1062 size_t n = dp->precision_end - dp->precision_start;
1063 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1069 #if HAVE_LONG_LONG_INT
1070 case TYPE_LONGLONGINT:
1071 case TYPE_ULONGLONGINT:
1078 case TYPE_WIDE_CHAR:
1081 case TYPE_WIDE_STRING:
1085 #if HAVE_LONG_DOUBLE
1086 case TYPE_LONGDOUBLE:
1093 *p = dp->conversion;
1102 /* Construct the arguments for calling snprintf or sprintf. */
1104 if (dp->width_arg_index != ARG_NONE)
1106 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1108 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1110 if (dp->precision_arg_index != ARG_NONE)
1112 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1114 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1118 /* Prepare checking whether snprintf returns the count
1120 ENSURE_ALLOCATION (xsum (length, 1));
1121 result[length] = '\0';
1130 maxlen = allocated - length;
1135 /* SNPRINTF can fail if maxlen > INT_MAX. */
1136 if (maxlen > INT_MAX)
1138 # define SNPRINTF_BUF(arg) \
1139 switch (prefix_count) \
1142 retcount = SNPRINTF (result + length, maxlen, buf, \
1146 retcount = SNPRINTF (result + length, maxlen, buf, \
1147 prefixes[0], arg, &count); \
1150 retcount = SNPRINTF (result + length, maxlen, buf, \
1151 prefixes[0], prefixes[1], arg, \
1158 # define SNPRINTF_BUF(arg) \
1159 switch (prefix_count) \
1162 count = sprintf (tmp, buf, arg); \
1165 count = sprintf (tmp, buf, prefixes[0], arg); \
1168 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1180 int arg = a.arg[dp->arg_index].a.a_schar;
1186 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1192 int arg = a.arg[dp->arg_index].a.a_short;
1198 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1204 int arg = a.arg[dp->arg_index].a.a_int;
1210 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1216 long int arg = a.arg[dp->arg_index].a.a_longint;
1222 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1226 #if HAVE_LONG_LONG_INT
1227 case TYPE_LONGLONGINT:
1229 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1233 case TYPE_ULONGLONGINT:
1235 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1242 double arg = a.arg[dp->arg_index].a.a_double;
1246 #if HAVE_LONG_DOUBLE
1247 case TYPE_LONGDOUBLE:
1249 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1256 int arg = a.arg[dp->arg_index].a.a_char;
1261 case TYPE_WIDE_CHAR:
1263 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1270 const char *arg = a.arg[dp->arg_index].a.a_string;
1275 case TYPE_WIDE_STRING:
1277 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1284 void *arg = a.arg[dp->arg_index].a.a_pointer;
1293 /* Portability: Not all implementations of snprintf()
1294 are ISO C 99 compliant. Determine the number of
1295 bytes that snprintf() has produced or would have
1299 /* Verify that snprintf() has NUL-terminated its
1301 if (count < maxlen && result[length + count] != '\0')
1303 /* Portability hack. */
1304 if (retcount > count)
1309 /* snprintf() doesn't understand the '%n'
1313 /* Don't use the '%n' directive; instead, look
1314 at the snprintf() return value. */
1320 /* Look at the snprintf() return value. */
1323 /* HP-UX 10.20 snprintf() is doubly deficient:
1324 It doesn't understand the '%n' directive,
1325 *and* it returns -1 (rather than the length
1326 that would have been required) when the
1327 buffer is too small. */
1328 size_t bigger_need =
1329 xsum (xtimes (allocated, 2), 12);
1330 ENSURE_ALLOCATION (bigger_need);
1339 /* Attempt to handle failure. */
1342 if (!(result == resultbuf || result == NULL))
1344 if (buf_malloced != NULL)
1345 free (buf_malloced);
1352 if (count >= tmp_length)
1353 /* tmp_length was incorrectly calculated - fix the
1358 /* Make room for the result. */
1359 if (count >= maxlen)
1361 /* Need at least count bytes. But allocate
1362 proportionally, to avoid looping eternally if
1363 snprintf() reports a too small count. */
1365 xmax (xsum (length, count), xtimes (allocated, 2));
1367 ENSURE_ALLOCATION (n);
1374 /* The snprintf() result did fit. */
1376 /* Append the sprintf() result. */
1377 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1389 /* Add the final NUL. */
1390 ENSURE_ALLOCATION (xsum (length, 1));
1391 result[length] = '\0';
1393 if (result != resultbuf && length + 1 < allocated)
1395 /* Shrink the allocated memory if possible. */
1398 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1403 if (buf_malloced != NULL)
1404 free (buf_malloced);
1407 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1408 says that snprintf() fails with errno = EOVERFLOW in this case, but
1409 that's only because snprintf() returns an 'int'. This function does
1410 not have this limitation. */
1414 if (!(result == resultbuf || result == NULL))
1416 if (buf_malloced != NULL)
1417 free (buf_malloced);
1423 if (!(result == resultbuf || result == NULL))
1425 if (buf_malloced != NULL)
1426 free (buf_malloced);