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. */
119 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
124 if (PRINTF_PARSE (format, &d, &a) < 0)
135 if (printf_fetchargs (args, &a) < 0)
143 size_t buf_neededlength;
145 CHAR_T *buf_malloced;
149 /* Output string accumulator. */
154 /* Allocate a small buffer that will hold a directive passed to
155 sprintf or snprintf. */
157 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
159 if (buf_neededlength < 4000 / sizeof (CHAR_T))
161 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
167 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
168 if (size_overflow_p (buf_memsize))
169 goto out_of_memory_1;
170 buf = (CHAR_T *) malloc (buf_memsize);
172 goto out_of_memory_1;
176 if (resultbuf != NULL)
179 allocated = *lengthp;
188 result is either == resultbuf or == NULL or malloc-allocated.
189 If length > 0, then result != NULL. */
191 /* Ensures that allocated >= needed. Aborts through a jump to
192 out_of_memory if needed is SIZE_MAX or otherwise too big. */
193 #define ENSURE_ALLOCATION(needed) \
194 if ((needed) > allocated) \
196 size_t memory_size; \
199 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
200 if ((needed) > allocated) \
201 allocated = (needed); \
202 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
203 if (size_overflow_p (memory_size)) \
204 goto out_of_memory; \
205 if (result == resultbuf || result == NULL) \
206 memory = (CHAR_T *) malloc (memory_size); \
208 memory = (CHAR_T *) realloc (result, memory_size); \
209 if (memory == NULL) \
210 goto out_of_memory; \
211 if (result == resultbuf && length > 0) \
212 memcpy (memory, result, length * sizeof (CHAR_T)); \
216 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
218 if (cp != dp->dir_start)
220 size_t n = dp->dir_start - cp;
221 size_t augmented_length = xsum (length, n);
223 ENSURE_ALLOCATION (augmented_length);
224 memcpy (result + length, cp, n * sizeof (CHAR_T));
225 length = augmented_length;
230 /* Execute a single directive. */
231 if (dp->conversion == '%')
233 size_t augmented_length;
235 if (!(dp->arg_index == ARG_NONE))
237 augmented_length = xsum (length, 1);
238 ENSURE_ALLOCATION (augmented_length);
239 result[length] = '%';
240 length = augmented_length;
244 if (!(dp->arg_index != ARG_NONE))
247 if (dp->conversion == 'n')
249 switch (a.arg[dp->arg_index].type)
251 case TYPE_COUNT_SCHAR_POINTER:
252 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
254 case TYPE_COUNT_SHORT_POINTER:
255 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
257 case TYPE_COUNT_INT_POINTER:
258 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
260 case TYPE_COUNT_LONGINT_POINTER:
261 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
263 #if HAVE_LONG_LONG_INT
264 case TYPE_COUNT_LONGLONGINT_POINTER:
265 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
272 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
273 else if (dp->conversion == 'a' || dp->conversion == 'A')
275 arg_type type = a.arg[dp->arg_index].type;
276 int flags = dp->flags;
289 if (dp->width_start != dp->width_end)
291 if (dp->width_arg_index != ARG_NONE)
295 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
297 arg = a.arg[dp->width_arg_index].a.a_int;
300 /* "A negative field width is taken as a '-' flag
301 followed by a positive field width." */
303 width = (unsigned int) (-arg);
310 const CHAR_T *digitp = dp->width_start;
313 width = xsum (xtimes (width, 10), *digitp++ - '0');
314 while (digitp != dp->width_end);
321 if (dp->precision_start != dp->precision_end)
323 if (dp->precision_arg_index != ARG_NONE)
327 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
329 arg = a.arg[dp->precision_arg_index].a.a_int;
330 /* "A negative precision is taken as if the precision
340 const CHAR_T *digitp = dp->precision_start + 1;
343 while (digitp != dp->precision_end)
344 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
349 /* Allocate a temporary buffer of sufficient size. */
350 # if HAVE_LONG_DOUBLE
351 if (type == TYPE_LONGDOUBLE)
353 (unsigned int) ((LDBL_DIG + 1)
354 * 0.831 /* decimal -> hexadecimal */
356 + 1; /* turn floor into ceil */
360 (unsigned int) ((DBL_DIG + 1)
361 * 0.831 /* decimal -> hexadecimal */
363 + 1; /* turn floor into ceil */
364 if (tmp_length < precision)
365 tmp_length = precision;
366 /* Account for sign, decimal point etc. */
367 tmp_length = xsum (tmp_length, 12);
369 if (tmp_length < width)
372 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
374 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
378 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
380 if (size_overflow_p (tmp_memsize))
381 /* Overflow, would lead to out of memory. */
383 tmp = (CHAR_T *) malloc (tmp_memsize);
391 # if HAVE_LONG_DOUBLE
392 if (type == TYPE_LONGDOUBLE)
394 long double arg = a.arg[dp->arg_index].a.a_longdouble;
398 if (dp->conversion == 'A')
400 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
404 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
416 else if (arg == 0.0L)
418 /* Distinguish 0.0L and -0.0L. */
419 static long double plus_zero = 0.0L;
420 long double arg_mem = arg;
421 if (memcmp (&plus_zero, &arg_mem, sizeof (long double)) != 0)
430 else if (flags & FLAG_SHOWSIGN)
432 else if (flags & FLAG_SPACE)
435 if (arg > 0.0L && arg + arg == arg)
437 if (dp->conversion == 'A')
439 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
443 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
449 long double mantissa;
452 mantissa = printf_frexpl (arg, &exponent);
460 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
462 /* Round the mantissa. */
463 long double tail = mantissa;
466 for (q = precision; ; q--)
468 int digit = (int) tail;
472 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
481 for (q = precision; q > 0; q--)
487 *p++ = dp->conversion - 'A' + 'X';
492 digit = (int) mantissa;
495 if ((flags & FLAG_ALT)
496 || mantissa > 0.0L || precision > 0)
499 localeconv () -> decimal_point;
500 /* The decimal point is always a single byte:
501 either '.' or ','. */
502 *p++ = (point[0] != '\0' ? point[0] : '.');
503 /* This loop terminates because we assume
504 that FLT_RADIX is a power of 2. */
505 while (mantissa > 0.0L)
508 digit = (int) mantissa;
513 : dp->conversion - 10);
517 while (precision > 0)
524 *p++ = dp->conversion - 'A' + 'P';
525 # if WIDE_CHAR_VERSION
527 static const wchar_t decimal_format[] =
528 { '%', '+', 'd', '\0' };
529 SNPRINTF (p, 6 + 1, decimal_format, exponent);
532 sprintf (p, "%+d", exponent);
542 double arg = a.arg[dp->arg_index].a.a_double;
546 if (dp->conversion == 'A')
548 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
552 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
566 /* Distinguish 0.0 and -0.0. */
567 static double plus_zero = 0.0;
568 double arg_mem = arg;
569 if (memcmp (&plus_zero, &arg_mem, sizeof (double)) != 0)
578 else if (flags & FLAG_SHOWSIGN)
580 else if (flags & FLAG_SPACE)
583 if (arg > 0.0 && arg + arg == arg)
585 if (dp->conversion == 'A')
587 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
591 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
600 mantissa = printf_frexp (arg, &exponent);
608 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
610 /* Round the mantissa. */
611 double tail = mantissa;
614 for (q = precision; ; q--)
616 int digit = (int) tail;
620 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
629 for (q = precision; q > 0; q--)
635 *p++ = dp->conversion - 'A' + 'X';
640 digit = (int) mantissa;
643 if ((flags & FLAG_ALT)
644 || mantissa > 0.0 || precision > 0)
647 localeconv () -> decimal_point;
648 /* The decimal point is always a single byte:
649 either '.' or ','. */
650 *p++ = (point[0] != '\0' ? point[0] : '.');
651 /* This loop terminates because we assume
652 that FLT_RADIX is a power of 2. */
653 while (mantissa > 0.0)
656 digit = (int) mantissa;
661 : dp->conversion - 10);
665 while (precision > 0)
672 *p++ = dp->conversion - 'A' + 'P';
673 # if WIDE_CHAR_VERSION
675 static const wchar_t decimal_format[] =
676 { '%', '+', 'd', '\0' };
677 SNPRINTF (p, 6 + 1, decimal_format, exponent);
680 sprintf (p, "%+d", exponent);
687 /* The generated string now extends from tmp to p, with the
688 zero padding insertion point being at pad_ptr. */
689 if (has_width && p - tmp < width)
691 size_t pad = width - (p - tmp);
692 CHAR_T *end = p + pad;
694 if (flags & FLAG_LEFT)
696 /* Pad with spaces on the right. */
697 for (; pad > 0; pad--)
700 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
702 /* Pad with zeroes. */
707 for (; pad > 0; pad--)
712 /* Pad with spaces on the left. */
717 for (; pad > 0; pad--)
725 size_t count = p - tmp;
727 if (count >= tmp_length)
728 /* tmp_length was incorrectly calculated - fix the
732 /* Make room for the result. */
733 if (count >= allocated - length)
735 size_t n = xsum (length, count);
737 ENSURE_ALLOCATION (n);
740 /* Append the result. */
741 memcpy (result + length, tmp, count * sizeof (CHAR_T));
750 arg_type type = a.arg[dp->arg_index].type;
752 unsigned int prefix_count;
759 /* Allocate a temporary buffer of sufficient size for calling
766 if (dp->width_start != dp->width_end)
768 if (dp->width_arg_index != ARG_NONE)
772 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
774 arg = a.arg[dp->width_arg_index].a.a_int;
775 width = (arg < 0 ? (unsigned int) (-arg) : arg);
779 const CHAR_T *digitp = dp->width_start;
782 width = xsum (xtimes (width, 10), *digitp++ - '0');
783 while (digitp != dp->width_end);
788 if (dp->precision_start != dp->precision_end)
790 if (dp->precision_arg_index != ARG_NONE)
794 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
796 arg = a.arg[dp->precision_arg_index].a.a_int;
797 precision = (arg < 0 ? 0 : arg);
801 const CHAR_T *digitp = dp->precision_start + 1;
804 while (digitp != dp->precision_end)
805 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
809 switch (dp->conversion)
812 case 'd': case 'i': case 'u':
813 # if HAVE_LONG_LONG_INT
814 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
816 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
817 * 0.30103 /* binary -> decimal */
819 + 1; /* turn floor into ceil */
822 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
824 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
825 * 0.30103 /* binary -> decimal */
827 + 1; /* turn floor into ceil */
830 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
831 * 0.30103 /* binary -> decimal */
833 + 1; /* turn floor into ceil */
834 if (tmp_length < precision)
835 tmp_length = precision;
836 /* Multiply by 2, as an estimate for FLAG_GROUP. */
837 tmp_length = xsum (tmp_length, tmp_length);
838 /* Add 1, to account for a leading sign. */
839 tmp_length = xsum (tmp_length, 1);
843 # if HAVE_LONG_LONG_INT
844 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
846 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
847 * 0.333334 /* binary -> octal */
849 + 1; /* turn floor into ceil */
852 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
854 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
855 * 0.333334 /* binary -> octal */
857 + 1; /* turn floor into ceil */
860 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
861 * 0.333334 /* binary -> octal */
863 + 1; /* turn floor into ceil */
864 if (tmp_length < precision)
865 tmp_length = precision;
866 /* Add 1, to account for a leading sign. */
867 tmp_length = xsum (tmp_length, 1);
871 # if HAVE_LONG_LONG_INT
872 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
874 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
875 * 0.25 /* binary -> hexadecimal */
877 + 1; /* turn floor into ceil */
880 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
882 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
883 * 0.25 /* binary -> hexadecimal */
885 + 1; /* turn floor into ceil */
888 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
889 * 0.25 /* binary -> hexadecimal */
891 + 1; /* turn floor into ceil */
892 if (tmp_length < precision)
893 tmp_length = precision;
894 /* Add 2, to account for a leading sign or alternate form. */
895 tmp_length = xsum (tmp_length, 2);
899 # if HAVE_LONG_DOUBLE
900 if (type == TYPE_LONGDOUBLE)
902 (unsigned int) (LDBL_MAX_EXP
903 * 0.30103 /* binary -> decimal */
904 * 2 /* estimate for FLAG_GROUP */
906 + 1 /* turn floor into ceil */
907 + 10; /* sign, decimal point etc. */
911 (unsigned int) (DBL_MAX_EXP
912 * 0.30103 /* binary -> decimal */
913 * 2 /* estimate for FLAG_GROUP */
915 + 1 /* turn floor into ceil */
916 + 10; /* sign, decimal point etc. */
917 tmp_length = xsum (tmp_length, precision);
920 case 'e': case 'E': case 'g': case 'G':
922 12; /* sign, decimal point, exponent etc. */
923 tmp_length = xsum (tmp_length, precision);
927 # if HAVE_LONG_DOUBLE
928 if (type == TYPE_LONGDOUBLE)
930 (unsigned int) (LDBL_DIG
931 * 0.831 /* decimal -> hexadecimal */
933 + 1; /* turn floor into ceil */
937 (unsigned int) (DBL_DIG
938 * 0.831 /* decimal -> hexadecimal */
940 + 1; /* turn floor into ceil */
941 if (tmp_length < precision)
942 tmp_length = precision;
943 /* Account for sign, decimal point etc. */
944 tmp_length = xsum (tmp_length, 12);
948 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
949 if (type == TYPE_WIDE_CHAR)
950 tmp_length = MB_CUR_MAX;
958 if (type == TYPE_WIDE_STRING)
961 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
963 # if !WIDE_CHAR_VERSION
964 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
969 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
974 (unsigned int) (sizeof (void *) * CHAR_BIT
975 * 0.25 /* binary -> hexadecimal */
977 + 1 /* turn floor into ceil */
978 + 2; /* account for leading 0x */
985 if (tmp_length < width)
988 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
991 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
995 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
997 if (size_overflow_p (tmp_memsize))
998 /* Overflow, would lead to out of memory. */
1000 tmp = (CHAR_T *) malloc (tmp_memsize);
1002 /* Out of memory. */
1007 /* Construct the format string for calling snprintf or
1011 if (dp->flags & FLAG_GROUP)
1013 if (dp->flags & FLAG_LEFT)
1015 if (dp->flags & FLAG_SHOWSIGN)
1017 if (dp->flags & FLAG_SPACE)
1019 if (dp->flags & FLAG_ALT)
1021 if (dp->flags & FLAG_ZERO)
1023 if (dp->width_start != dp->width_end)
1025 size_t n = dp->width_end - dp->width_start;
1026 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1029 if (dp->precision_start != dp->precision_end)
1031 size_t n = dp->precision_end - dp->precision_start;
1032 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1038 #if HAVE_LONG_LONG_INT
1039 case TYPE_LONGLONGINT:
1040 case TYPE_ULONGLONGINT:
1047 case TYPE_WIDE_CHAR:
1050 case TYPE_WIDE_STRING:
1054 #if HAVE_LONG_DOUBLE
1055 case TYPE_LONGDOUBLE:
1062 *p = dp->conversion;
1071 /* Construct the arguments for calling snprintf or sprintf. */
1073 if (dp->width_arg_index != ARG_NONE)
1075 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1077 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1079 if (dp->precision_arg_index != ARG_NONE)
1081 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1083 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1087 /* Prepare checking whether snprintf returns the count
1089 ENSURE_ALLOCATION (xsum (length, 1));
1090 result[length] = '\0';
1099 maxlen = allocated - length;
1104 # define SNPRINTF_BUF(arg) \
1105 switch (prefix_count) \
1108 retcount = SNPRINTF (result + length, maxlen, buf, \
1112 retcount = SNPRINTF (result + length, maxlen, buf, \
1113 prefixes[0], arg, &count); \
1116 retcount = SNPRINTF (result + length, maxlen, buf, \
1117 prefixes[0], prefixes[1], arg, \
1124 # define SNPRINTF_BUF(arg) \
1125 switch (prefix_count) \
1128 count = sprintf (tmp, buf, arg); \
1131 count = sprintf (tmp, buf, prefixes[0], arg); \
1134 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1146 int arg = a.arg[dp->arg_index].a.a_schar;
1152 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1158 int arg = a.arg[dp->arg_index].a.a_short;
1164 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1170 int arg = a.arg[dp->arg_index].a.a_int;
1176 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1182 long int arg = a.arg[dp->arg_index].a.a_longint;
1188 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1192 #if HAVE_LONG_LONG_INT
1193 case TYPE_LONGLONGINT:
1195 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1199 case TYPE_ULONGLONGINT:
1201 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1208 double arg = a.arg[dp->arg_index].a.a_double;
1212 #if HAVE_LONG_DOUBLE
1213 case TYPE_LONGDOUBLE:
1215 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1222 int arg = a.arg[dp->arg_index].a.a_char;
1227 case TYPE_WIDE_CHAR:
1229 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1236 const char *arg = a.arg[dp->arg_index].a.a_string;
1241 case TYPE_WIDE_STRING:
1243 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1250 void *arg = a.arg[dp->arg_index].a.a_pointer;
1259 /* Portability: Not all implementations of snprintf()
1260 are ISO C 99 compliant. Determine the number of
1261 bytes that snprintf() has produced or would have
1265 /* Verify that snprintf() has NUL-terminated its
1267 if (count < maxlen && result[length + count] != '\0')
1269 /* Portability hack. */
1270 if (retcount > count)
1275 /* snprintf() doesn't understand the '%n'
1279 /* Don't use the '%n' directive; instead, look
1280 at the snprintf() return value. */
1286 /* Look at the snprintf() return value. */
1289 /* HP-UX 10.20 snprintf() is doubly deficient:
1290 It doesn't understand the '%n' directive,
1291 *and* it returns -1 (rather than the length
1292 that would have been required) when the
1293 buffer is too small. */
1294 size_t bigger_need =
1295 xsum (xtimes (allocated, 2), 12);
1296 ENSURE_ALLOCATION (bigger_need);
1305 /* Attempt to handle failure. */
1308 if (!(result == resultbuf || result == NULL))
1310 if (buf_malloced != NULL)
1311 free (buf_malloced);
1318 if (count >= tmp_length)
1319 /* tmp_length was incorrectly calculated - fix the
1324 /* Make room for the result. */
1325 if (count >= maxlen)
1327 /* Need at least count bytes. But allocate
1328 proportionally, to avoid looping eternally if
1329 snprintf() reports a too small count. */
1331 xmax (xsum (length, count), xtimes (allocated, 2));
1333 ENSURE_ALLOCATION (n);
1340 /* The snprintf() result did fit. */
1342 /* Append the sprintf() result. */
1343 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1355 /* Add the final NUL. */
1356 ENSURE_ALLOCATION (xsum (length, 1));
1357 result[length] = '\0';
1359 if (result != resultbuf && length + 1 < allocated)
1361 /* Shrink the allocated memory if possible. */
1364 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1369 if (buf_malloced != NULL)
1370 free (buf_malloced);
1373 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1374 says that snprintf() fails with errno = EOVERFLOW in this case, but
1375 that's only because snprintf() returns an 'int'. This function does
1376 not have this limitation. */
1380 if (!(result == resultbuf || result == NULL))
1382 if (buf_malloced != NULL)
1383 free (buf_malloced);