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"
60 # include "isnanl-nolibm.h"
61 # include "printf-frexpl.h"
65 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
67 # define EOVERFLOW E2BIG
72 # define local_wcslen wcslen
74 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
75 a dependency towards this library, here is a local substitute.
76 Define this substitute only once, even if this file is included
77 twice in the same compilation unit. */
78 # ifndef local_wcslen_defined
79 # define local_wcslen_defined 1
81 local_wcslen (const wchar_t *s)
85 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
94 # define VASNPRINTF vasnwprintf
95 # define CHAR_T wchar_t
96 # define DIRECTIVE wchar_t_directive
97 # define DIRECTIVES wchar_t_directives
98 # define PRINTF_PARSE wprintf_parse
99 # define USE_SNPRINTF 1
100 # if HAVE_DECL__SNWPRINTF
101 /* On Windows, the function swprintf() has a different signature than
102 on Unix; we use the _snwprintf() function instead. */
103 # define SNPRINTF _snwprintf
106 # define SNPRINTF swprintf
109 # define VASNPRINTF vasnprintf
111 # define DIRECTIVE char_directive
112 # define DIRECTIVES char_directives
113 # define PRINTF_PARSE printf_parse
114 # /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
115 But don't use it on BeOS, since BeOS snprintf produces no output if the
116 size argument is >= 0x3000000. */
117 # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__
118 # define USE_SNPRINTF 1
120 # define USE_SNPRINTF 0
122 # if HAVE_DECL__SNPRINTF
124 # define SNPRINTF _snprintf
127 # define SNPRINTF snprintf
128 /* Here we need to call the native snprintf, not rpl_snprintf. */
132 /* Here we need to call the native sprintf, not rpl_sprintf. */
135 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
136 /* Determine the decimal-point character according to the current locale. */
137 # ifndef decimal_point_char_defined
138 # define decimal_point_char_defined 1
140 decimal_point_char ()
143 /* Determine it in a multithread-safe way. We know nl_langinfo is
144 multithread-safe on glibc systems, but is not required to be multithread-
145 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
146 is rarely multithread-safe. */
147 # if HAVE_NL_LANGINFO && __GLIBC__
148 point = nl_langinfo (RADIXCHAR);
151 sprintf (pointbuf, "%#.0f", 1.0);
152 point = &pointbuf[1];
154 point = localeconv () -> decimal_point;
156 /* The decimal point is always a single byte: either '.' or ','. */
157 return (point[0] != '\0' ? point[0] : '.');
163 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
168 if (PRINTF_PARSE (format, &d, &a) < 0)
179 if (printf_fetchargs (args, &a) < 0)
187 size_t buf_neededlength;
189 CHAR_T *buf_malloced;
193 /* Output string accumulator. */
198 /* Allocate a small buffer that will hold a directive passed to
199 sprintf or snprintf. */
201 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
203 if (buf_neededlength < 4000 / sizeof (CHAR_T))
205 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
211 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
212 if (size_overflow_p (buf_memsize))
213 goto out_of_memory_1;
214 buf = (CHAR_T *) malloc (buf_memsize);
216 goto out_of_memory_1;
220 if (resultbuf != NULL)
223 allocated = *lengthp;
232 result is either == resultbuf or == NULL or malloc-allocated.
233 If length > 0, then result != NULL. */
235 /* Ensures that allocated >= needed. Aborts through a jump to
236 out_of_memory if needed is SIZE_MAX or otherwise too big. */
237 #define ENSURE_ALLOCATION(needed) \
238 if ((needed) > allocated) \
240 size_t memory_size; \
243 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
244 if ((needed) > allocated) \
245 allocated = (needed); \
246 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
247 if (size_overflow_p (memory_size)) \
248 goto out_of_memory; \
249 if (result == resultbuf || result == NULL) \
250 memory = (CHAR_T *) malloc (memory_size); \
252 memory = (CHAR_T *) realloc (result, memory_size); \
253 if (memory == NULL) \
254 goto out_of_memory; \
255 if (result == resultbuf && length > 0) \
256 memcpy (memory, result, length * sizeof (CHAR_T)); \
260 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
262 if (cp != dp->dir_start)
264 size_t n = dp->dir_start - cp;
265 size_t augmented_length = xsum (length, n);
267 ENSURE_ALLOCATION (augmented_length);
268 memcpy (result + length, cp, n * sizeof (CHAR_T));
269 length = augmented_length;
274 /* Execute a single directive. */
275 if (dp->conversion == '%')
277 size_t augmented_length;
279 if (!(dp->arg_index == ARG_NONE))
281 augmented_length = xsum (length, 1);
282 ENSURE_ALLOCATION (augmented_length);
283 result[length] = '%';
284 length = augmented_length;
288 if (!(dp->arg_index != ARG_NONE))
291 if (dp->conversion == 'n')
293 switch (a.arg[dp->arg_index].type)
295 case TYPE_COUNT_SCHAR_POINTER:
296 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
298 case TYPE_COUNT_SHORT_POINTER:
299 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
301 case TYPE_COUNT_INT_POINTER:
302 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
304 case TYPE_COUNT_LONGINT_POINTER:
305 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
307 #if HAVE_LONG_LONG_INT
308 case TYPE_COUNT_LONGLONGINT_POINTER:
309 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
316 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
317 else if (dp->conversion == 'a' || dp->conversion == 'A')
319 arg_type type = a.arg[dp->arg_index].type;
320 int flags = dp->flags;
333 if (dp->width_start != dp->width_end)
335 if (dp->width_arg_index != ARG_NONE)
339 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
341 arg = a.arg[dp->width_arg_index].a.a_int;
344 /* "A negative field width is taken as a '-' flag
345 followed by a positive field width." */
347 width = (unsigned int) (-arg);
354 const CHAR_T *digitp = dp->width_start;
357 width = xsum (xtimes (width, 10), *digitp++ - '0');
358 while (digitp != dp->width_end);
365 if (dp->precision_start != dp->precision_end)
367 if (dp->precision_arg_index != ARG_NONE)
371 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
373 arg = a.arg[dp->precision_arg_index].a.a_int;
374 /* "A negative precision is taken as if the precision
384 const CHAR_T *digitp = dp->precision_start + 1;
387 while (digitp != dp->precision_end)
388 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
393 /* Allocate a temporary buffer of sufficient size. */
394 if (type == TYPE_LONGDOUBLE)
396 (unsigned int) ((LDBL_DIG + 1)
397 * 0.831 /* decimal -> hexadecimal */
399 + 1; /* turn floor into ceil */
402 (unsigned int) ((DBL_DIG + 1)
403 * 0.831 /* decimal -> hexadecimal */
405 + 1; /* turn floor into ceil */
406 if (tmp_length < precision)
407 tmp_length = precision;
408 /* Account for sign, decimal point etc. */
409 tmp_length = xsum (tmp_length, 12);
411 if (tmp_length < width)
414 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
416 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
420 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
422 if (size_overflow_p (tmp_memsize))
423 /* Overflow, would lead to out of memory. */
425 tmp = (CHAR_T *) malloc (tmp_memsize);
433 if (type == TYPE_LONGDOUBLE)
435 long double arg = a.arg[dp->arg_index].a.a_longdouble;
439 if (dp->conversion == 'A')
441 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
445 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
451 DECL_LONG_DOUBLE_ROUNDING
453 BEGIN_LONG_DOUBLE_ROUNDING ();
455 if (signbit (arg)) /* arg < 0.0L or negative zero */
463 else if (flags & FLAG_SHOWSIGN)
465 else if (flags & FLAG_SPACE)
468 if (arg > 0.0L && arg + arg == arg)
470 if (dp->conversion == 'A')
472 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
476 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
482 long double mantissa;
485 mantissa = printf_frexpl (arg, &exponent);
493 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
495 /* Round the mantissa. */
496 long double tail = mantissa;
499 for (q = precision; ; q--)
501 int digit = (int) tail;
505 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
514 for (q = precision; q > 0; q--)
520 *p++ = dp->conversion - 'A' + 'X';
525 digit = (int) mantissa;
528 if ((flags & FLAG_ALT)
529 || mantissa > 0.0L || precision > 0)
531 *p++ = decimal_point_char ();
532 /* This loop terminates because we assume
533 that FLT_RADIX is a power of 2. */
534 while (mantissa > 0.0L)
537 digit = (int) mantissa;
542 : dp->conversion - 10);
546 while (precision > 0)
553 *p++ = dp->conversion - 'A' + 'P';
554 # if WIDE_CHAR_VERSION
556 static const wchar_t decimal_format[] =
557 { '%', '+', 'd', '\0' };
558 SNPRINTF (p, 6 + 1, decimal_format, exponent);
561 sprintf (p, "%+d", exponent);
567 END_LONG_DOUBLE_ROUNDING ();
572 double arg = a.arg[dp->arg_index].a.a_double;
576 if (dp->conversion == 'A')
578 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
582 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
589 if (signbit (arg)) /* arg < 0.0 or negative zero */
597 else if (flags & FLAG_SHOWSIGN)
599 else if (flags & FLAG_SPACE)
602 if (arg > 0.0 && arg + arg == arg)
604 if (dp->conversion == 'A')
606 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
610 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
619 mantissa = printf_frexp (arg, &exponent);
627 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
629 /* Round the mantissa. */
630 double tail = mantissa;
633 for (q = precision; ; q--)
635 int digit = (int) tail;
639 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
648 for (q = precision; q > 0; q--)
654 *p++ = dp->conversion - 'A' + 'X';
659 digit = (int) mantissa;
662 if ((flags & FLAG_ALT)
663 || mantissa > 0.0 || precision > 0)
665 *p++ = decimal_point_char ();
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;
766 int flags = dp->flags;
767 #if !USE_SNPRINTF || NEED_PRINTF_FLAG_ZERO
771 #if NEED_PRINTF_FLAG_ZERO
774 # define pad_ourselves 0
777 unsigned int prefix_count;
785 #if !USE_SNPRINTF || NEED_PRINTF_FLAG_ZERO
788 if (dp->width_start != dp->width_end)
790 if (dp->width_arg_index != ARG_NONE)
794 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
796 arg = a.arg[dp->width_arg_index].a.a_int;
799 /* "A negative field width is taken as a '-' flag
800 followed by a positive field width." */
802 width = (unsigned int) (-arg);
809 const CHAR_T *digitp = dp->width_start;
812 width = xsum (xtimes (width, 10), *digitp++ - '0');
813 while (digitp != dp->width_end);
820 /* Allocate a temporary buffer of sufficient size for calling
826 if (dp->precision_start != dp->precision_end)
828 if (dp->precision_arg_index != ARG_NONE)
832 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
834 arg = a.arg[dp->precision_arg_index].a.a_int;
835 precision = (arg < 0 ? 0 : arg);
839 const CHAR_T *digitp = dp->precision_start + 1;
842 while (digitp != dp->precision_end)
843 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
847 switch (dp->conversion)
850 case 'd': case 'i': case 'u':
851 # if HAVE_LONG_LONG_INT
852 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
854 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
855 * 0.30103 /* binary -> decimal */
857 + 1; /* turn floor into ceil */
860 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
862 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
863 * 0.30103 /* binary -> decimal */
865 + 1; /* turn floor into ceil */
868 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
869 * 0.30103 /* binary -> decimal */
871 + 1; /* turn floor into ceil */
872 if (tmp_length < precision)
873 tmp_length = precision;
874 /* Multiply by 2, as an estimate for FLAG_GROUP. */
875 tmp_length = xsum (tmp_length, tmp_length);
876 /* Add 1, to account for a leading sign. */
877 tmp_length = xsum (tmp_length, 1);
881 # if HAVE_LONG_LONG_INT
882 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
884 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
885 * 0.333334 /* binary -> octal */
887 + 1; /* turn floor into ceil */
890 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
892 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
893 * 0.333334 /* binary -> octal */
895 + 1; /* turn floor into ceil */
898 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
899 * 0.333334 /* binary -> octal */
901 + 1; /* turn floor into ceil */
902 if (tmp_length < precision)
903 tmp_length = precision;
904 /* Add 1, to account for a leading sign. */
905 tmp_length = xsum (tmp_length, 1);
909 # if HAVE_LONG_LONG_INT
910 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
912 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
913 * 0.25 /* binary -> hexadecimal */
915 + 1; /* turn floor into ceil */
918 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
920 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
921 * 0.25 /* binary -> hexadecimal */
923 + 1; /* turn floor into ceil */
926 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
927 * 0.25 /* binary -> hexadecimal */
929 + 1; /* turn floor into ceil */
930 if (tmp_length < precision)
931 tmp_length = precision;
932 /* Add 2, to account for a leading sign or alternate form. */
933 tmp_length = xsum (tmp_length, 2);
937 if (type == TYPE_LONGDOUBLE)
939 (unsigned int) (LDBL_MAX_EXP
940 * 0.30103 /* binary -> decimal */
941 * 2 /* estimate for FLAG_GROUP */
943 + 1 /* turn floor into ceil */
944 + 10; /* sign, decimal point etc. */
947 (unsigned int) (DBL_MAX_EXP
948 * 0.30103 /* binary -> decimal */
949 * 2 /* estimate for FLAG_GROUP */
951 + 1 /* turn floor into ceil */
952 + 10; /* sign, decimal point etc. */
953 tmp_length = xsum (tmp_length, precision);
956 case 'e': case 'E': case 'g': case 'G':
958 12; /* sign, decimal point, exponent etc. */
959 tmp_length = xsum (tmp_length, precision);
963 if (type == TYPE_LONGDOUBLE)
965 (unsigned int) (LDBL_DIG
966 * 0.831 /* decimal -> hexadecimal */
968 + 1; /* turn floor into ceil */
971 (unsigned int) (DBL_DIG
972 * 0.831 /* decimal -> hexadecimal */
974 + 1; /* turn floor into ceil */
975 if (tmp_length < precision)
976 tmp_length = precision;
977 /* Account for sign, decimal point etc. */
978 tmp_length = xsum (tmp_length, 12);
982 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
983 if (type == TYPE_WIDE_CHAR)
984 tmp_length = MB_CUR_MAX;
992 if (type == TYPE_WIDE_STRING)
995 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
997 # if !WIDE_CHAR_VERSION
998 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
1003 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
1008 (unsigned int) (sizeof (void *) * CHAR_BIT
1009 * 0.25 /* binary -> hexadecimal */
1011 + 1 /* turn floor into ceil */
1012 + 2; /* account for leading 0x */
1019 if (tmp_length < width)
1022 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
1025 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
1029 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1031 if (size_overflow_p (tmp_memsize))
1032 /* Overflow, would lead to out of memory. */
1034 tmp = (CHAR_T *) malloc (tmp_memsize);
1036 /* Out of memory. */
1041 /* Decide whether to perform the padding ourselves. */
1042 #if NEED_PRINTF_FLAG_ZERO
1043 switch (dp->conversion)
1045 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
1055 /* Construct the format string for calling snprintf or
1059 #if NEED_PRINTF_FLAG_GROUPING
1060 /* The underlying implementation doesn't support the ' flag.
1061 Produce no grouping characters in this case; this is
1062 acceptable because the grouping is locale dependent. */
1064 if (flags & FLAG_GROUP)
1067 if (flags & FLAG_LEFT)
1069 if (flags & FLAG_SHOWSIGN)
1071 if (flags & FLAG_SPACE)
1073 if (flags & FLAG_ALT)
1077 if (flags & FLAG_ZERO)
1079 if (dp->width_start != dp->width_end)
1081 size_t n = dp->width_end - dp->width_start;
1082 memcpy (fbp, dp->width_start, n * sizeof (CHAR_T));
1086 if (dp->precision_start != dp->precision_end)
1088 size_t n = dp->precision_end - dp->precision_start;
1089 memcpy (fbp, dp->precision_start, n * sizeof (CHAR_T));
1095 #if HAVE_LONG_LONG_INT
1096 case TYPE_LONGLONGINT:
1097 case TYPE_ULONGLONGINT:
1098 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
1111 case TYPE_WIDE_CHAR:
1114 case TYPE_WIDE_STRING:
1118 case TYPE_LONGDOUBLE:
1124 #if NEED_PRINTF_DIRECTIVE_F
1125 if (dp->conversion == 'F')
1129 *fbp = dp->conversion;
1138 /* Construct the arguments for calling snprintf or sprintf. */
1140 if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
1142 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1144 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1146 if (dp->precision_arg_index != ARG_NONE)
1148 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1150 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1154 /* Prepare checking whether snprintf returns the count
1156 ENSURE_ALLOCATION (xsum (length, 1));
1157 result[length] = '\0';
1166 maxlen = allocated - length;
1171 /* SNPRINTF can fail if maxlen > INT_MAX. */
1172 if (maxlen > INT_MAX)
1174 # define SNPRINTF_BUF(arg) \
1175 switch (prefix_count) \
1178 retcount = SNPRINTF (result + length, maxlen, buf, \
1182 retcount = SNPRINTF (result + length, maxlen, buf, \
1183 prefixes[0], arg, &count); \
1186 retcount = SNPRINTF (result + length, maxlen, buf, \
1187 prefixes[0], prefixes[1], arg, \
1194 # define SNPRINTF_BUF(arg) \
1195 switch (prefix_count) \
1198 count = sprintf (tmp, buf, arg); \
1201 count = sprintf (tmp, buf, prefixes[0], arg); \
1204 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1216 int arg = a.arg[dp->arg_index].a.a_schar;
1222 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1228 int arg = a.arg[dp->arg_index].a.a_short;
1234 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1240 int arg = a.arg[dp->arg_index].a.a_int;
1246 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1252 long int arg = a.arg[dp->arg_index].a.a_longint;
1258 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1262 #if HAVE_LONG_LONG_INT
1263 case TYPE_LONGLONGINT:
1265 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1269 case TYPE_ULONGLONGINT:
1271 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1278 double arg = a.arg[dp->arg_index].a.a_double;
1282 case TYPE_LONGDOUBLE:
1284 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1290 int arg = a.arg[dp->arg_index].a.a_char;
1295 case TYPE_WIDE_CHAR:
1297 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1304 const char *arg = a.arg[dp->arg_index].a.a_string;
1309 case TYPE_WIDE_STRING:
1311 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1318 void *arg = a.arg[dp->arg_index].a.a_pointer;
1327 /* Portability: Not all implementations of snprintf()
1328 are ISO C 99 compliant. Determine the number of
1329 bytes that snprintf() has produced or would have
1333 /* Verify that snprintf() has NUL-terminated its
1335 if (count < maxlen && result[length + count] != '\0')
1337 /* Portability hack. */
1338 if (retcount > count)
1343 /* snprintf() doesn't understand the '%n'
1347 /* Don't use the '%n' directive; instead, look
1348 at the snprintf() return value. */
1354 /* Look at the snprintf() return value. */
1357 /* HP-UX 10.20 snprintf() is doubly deficient:
1358 It doesn't understand the '%n' directive,
1359 *and* it returns -1 (rather than the length
1360 that would have been required) when the
1361 buffer is too small. */
1362 size_t bigger_need =
1363 xsum (xtimes (allocated, 2), 12);
1364 ENSURE_ALLOCATION (bigger_need);
1373 /* Attempt to handle failure. */
1376 if (!(result == resultbuf || result == NULL))
1378 if (buf_malloced != NULL)
1379 free (buf_malloced);
1385 /* Perform padding. */
1386 #if NEED_PRINTF_FLAG_ZERO
1387 if (pad_ourselves && has_width && count < width)
1390 /* Make room for the result. */
1391 if (width >= maxlen)
1393 /* Need at least width bytes. But allocate
1394 proportionally, to avoid looping eternally if
1395 snprintf() reports a too small count. */
1397 xmax (xsum (length, width),
1398 xtimes (allocated, 2));
1401 ENSURE_ALLOCATION (n);
1403 maxlen = allocated - length; /* >= width */
1408 CHAR_T * const rp = result + length;
1410 CHAR_T * const rp = tmp;
1412 CHAR_T *p = rp + count;
1413 size_t pad = width - count;
1414 CHAR_T *end = p + pad;
1415 CHAR_T *pad_ptr = (*rp == '-' ? rp + 1 : rp);
1416 /* No zero-padding of "inf" and "nan". */
1417 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
1418 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
1420 /* The generated string now extends from rp to p,
1421 with the zero padding insertion point being at
1424 if (flags & FLAG_LEFT)
1426 /* Pad with spaces on the right. */
1427 for (; pad > 0; pad--)
1430 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
1432 /* Pad with zeroes. */
1437 for (; pad > 0; pad--)
1442 /* Pad with spaces on the left. */
1447 for (; pad > 0; pad--)
1451 count = width; /* = count + pad = end - rp */
1457 if (count >= tmp_length)
1458 /* tmp_length was incorrectly calculated - fix the
1463 /* Make room for the result. */
1464 if (count >= maxlen)
1466 /* Need at least count bytes. But allocate
1467 proportionally, to avoid looping eternally if
1468 snprintf() reports a too small count. */
1470 xmax (xsum (length, count), xtimes (allocated, 2));
1472 ENSURE_ALLOCATION (n);
1479 /* The snprintf() result did fit. */
1481 /* Append the sprintf() result. */
1482 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1487 #if NEED_PRINTF_DIRECTIVE_F
1488 if (dp->conversion == 'F')
1490 /* Convert the %f result to upper case for %F. */
1491 CHAR_T *rp = result + length;
1493 for (rc = count; rc > 0; rc--, rp++)
1494 if (*rp >= 'a' && *rp <= 'z')
1495 *rp = *rp - 'a' + 'A';
1506 /* Add the final NUL. */
1507 ENSURE_ALLOCATION (xsum (length, 1));
1508 result[length] = '\0';
1510 if (result != resultbuf && length + 1 < allocated)
1512 /* Shrink the allocated memory if possible. */
1515 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1520 if (buf_malloced != NULL)
1521 free (buf_malloced);
1524 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1525 says that snprintf() fails with errno = EOVERFLOW in this case, but
1526 that's only because snprintf() returns an 'int'. This function does
1527 not have this limitation. */
1531 if (!(result == resultbuf || result == NULL))
1533 if (buf_malloced != NULL)
1534 free (buf_malloced);
1540 if (!(result == resultbuf || result == NULL))
1542 if (buf_malloced != NULL)
1543 free (buf_malloced);