1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
23 # define _GNU_SOURCE 1
29 #include "glib/galloca.h"
34 #include "vasnprintf.h"
36 #include <stdio.h> /* snprintf(), sprintf() */
37 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
38 #include <string.h> /* memcpy(), strlen() */
39 #include <errno.h> /* errno */
40 #include <limits.h> /* CHAR_BIT */
41 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
42 #include "printf-parse.h"
44 /* For those losing systems which don't have 'alloca' we have to add
45 some additional code emulating it. */
47 # define freea(p) /* nothing */
49 # define alloca(n) malloc (n)
50 # define freea(p) free (p)
53 #ifndef HAVE_LONG_LONG_FORMAT
55 print_long_long (char *buf,
61 unsigned long long number)
68 static const char *upper = "0123456789ABCDEFX";
69 static const char *lower = "0123456789abcdefx";
76 if (p - buf == len - 1) \
106 negative = (long long)number < 0;
108 number = -((long long)number);
113 pointer = bufferend = &buffer[sizeof(buffer) - 1];
115 for (i = 1; i < (int)sizeof(buffer); i++)
117 *pointer-- = digits[number % base];
124 width -= (bufferend - pointer) - 1;
126 /* Adjust precision */
129 precision -= (bufferend - pointer) - 1;
135 /* Adjust width further */
136 if (negative || (flags & FLAG_SHOWSIGN) || (flags & FLAG_SPACE))
138 if (flags & FLAG_ALT)
153 /* Output prefixes spaces if needed */
154 if (! ((flags & FLAG_LEFT) ||
155 ((flags & FLAG_ZERO) && (precision == -1))))
157 count = (precision == -1) ? 0 : precision;
158 while (width-- > count)
162 /* width has been adjusted for signs and alternatives */
167 else if (flags & FLAG_SHOWSIGN)
171 else if (flags & FLAG_SPACE)
176 if (flags & FLAG_ALT)
192 /* Output prefixed zero padding if needed */
193 if (flags & FLAG_ZERO)
197 while (precision-- > 0)
204 /* Output the number itself */
210 /* Output trailing spaces if needed */
211 if (flags & FLAG_LEFT)
224 vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
229 if (printf_parse (format, &d, &a) < 0)
240 if (printf_fetchargs (args, &a) < 0)
249 (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
253 /* Output string accumulator. */
258 if (resultbuf != NULL)
261 allocated = *lengthp;
270 result is either == resultbuf or == NULL or malloc-allocated.
271 If length > 0, then result != NULL. */
273 #define ENSURE_ALLOCATION(needed) \
274 if ((needed) > allocated) \
278 allocated = (allocated > 0 ? 2 * allocated : 12); \
279 if ((needed) > allocated) \
280 allocated = (needed); \
281 if (result == resultbuf || result == NULL) \
282 memory = (char *) malloc (allocated); \
284 memory = (char *) realloc (result, allocated); \
286 if (memory == NULL) \
288 if (!(result == resultbuf || result == NULL)) \
295 if (result == resultbuf && length > 0) \
296 memcpy (memory, result, length); \
300 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
302 if (cp != dp->dir_start)
304 size_t n = dp->dir_start - cp;
306 ENSURE_ALLOCATION (length + n);
307 memcpy (result + length, cp, n);
313 /* Execute a single directive. */
314 if (dp->conversion == '%')
316 if (!(dp->arg_index < 0))
318 ENSURE_ALLOCATION (length + 1);
319 result[length] = '%';
324 if (!(dp->arg_index >= 0))
327 if (dp->conversion == 'n')
329 switch (a.arg[dp->arg_index].type)
331 case TYPE_COUNT_SCHAR_POINTER:
332 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
334 case TYPE_COUNT_SHORT_POINTER:
335 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
337 case TYPE_COUNT_INT_POINTER:
338 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
340 case TYPE_COUNT_LONGINT_POINTER:
341 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
343 #ifdef HAVE_LONG_LONG
344 case TYPE_COUNT_LONGLONGINT_POINTER:
345 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
354 arg_type type = a.arg[dp->arg_index].type;
356 unsigned int prefix_count;
359 unsigned int tmp_length;
363 /* Allocate a temporary buffer of sufficient size for calling
367 unsigned int precision;
370 if (dp->width_start != dp->width_end)
372 if (dp->width_arg_index >= 0)
376 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
378 arg = a.arg[dp->width_arg_index].a.a_int;
379 width = (arg < 0 ? -arg : arg);
383 const char *digitp = dp->width_start;
386 width = width * 10 + (*digitp++ - '0');
387 while (digitp != dp->width_end);
392 if (dp->precision_start != dp->precision_end)
394 if (dp->precision_arg_index >= 0)
398 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
400 arg = a.arg[dp->precision_arg_index].a.a_int;
401 precision = (arg < 0 ? 0 : arg);
405 const char *digitp = dp->precision_start + 1;
408 while (digitp != dp->precision_end)
409 precision = precision * 10 + (*digitp++ - '0');
413 switch (dp->conversion)
415 case 'd': case 'i': case 'u':
416 # ifdef HAVE_LONG_LONG
417 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
419 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
420 * 0.30103 /* binary -> decimal */
421 * 2 /* estimate for FLAG_GROUP */
423 + 1 /* turn floor into ceil */
424 + 1; /* account for leading sign */
427 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
429 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
430 * 0.30103 /* binary -> decimal */
431 * 2 /* estimate for FLAG_GROUP */
433 + 1 /* turn floor into ceil */
434 + 1; /* account for leading sign */
437 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
438 * 0.30103 /* binary -> decimal */
439 * 2 /* estimate for FLAG_GROUP */
441 + 1 /* turn floor into ceil */
442 + 1; /* account for leading sign */
446 # ifdef HAVE_LONG_LONG
447 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
449 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
450 * 0.333334 /* binary -> octal */
452 + 1 /* turn floor into ceil */
453 + 1; /* account for leading sign */
456 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
458 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
459 * 0.333334 /* binary -> octal */
461 + 1 /* turn floor into ceil */
462 + 1; /* account for leading sign */
465 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
466 * 0.333334 /* binary -> octal */
468 + 1 /* turn floor into ceil */
469 + 1; /* account for leading sign */
473 # ifdef HAVE_LONG_LONG
474 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
476 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
477 * 0.25 /* binary -> hexadecimal */
479 + 1 /* turn floor into ceil */
480 + 2; /* account for leading sign or alternate form */
483 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
485 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
486 * 0.25 /* binary -> hexadecimal */
488 + 1 /* turn floor into ceil */
489 + 2; /* account for leading sign or alternate form */
492 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
493 * 0.25 /* binary -> hexadecimal */
495 + 1 /* turn floor into ceil */
496 + 2; /* account for leading sign or alternate form */
500 # ifdef HAVE_LONG_DOUBLE
501 if (type == TYPE_LONGDOUBLE)
503 (unsigned int) (LDBL_MAX_EXP
504 * 0.30103 /* binary -> decimal */
505 * 2 /* estimate for FLAG_GROUP */
507 + 1 /* turn floor into ceil */
509 + 10; /* sign, decimal point etc. */
513 (unsigned int) (DBL_MAX_EXP
514 * 0.30103 /* binary -> decimal */
515 * 2 /* estimate for FLAG_GROUP */
517 + 1 /* turn floor into ceil */
519 + 10; /* sign, decimal point etc. */
522 case 'e': case 'E': case 'g': case 'G':
526 + 12; /* sign, decimal point, exponent etc. */
531 if (type == TYPE_WIDE_CHAR)
532 tmp_length = MB_CUR_MAX;
540 if (type == TYPE_WIDE_STRING)
542 wcslen (a.arg[dp->arg_index].a.a_wide_string)
546 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
551 (unsigned int) (sizeof (void *) * CHAR_BIT
552 * 0.25 /* binary -> hexadecimal */
554 + 1 /* turn floor into ceil */
555 + 2; /* account for leading 0x */
562 if (tmp_length < width)
565 tmp_length++; /* account for trailing NUL */
568 if (tmp_length <= sizeof (tmpbuf))
572 tmp = (char *) malloc (tmp_length);
576 if (!(result == resultbuf || result == NULL))
586 /* Construct the format string for calling snprintf or
590 if (dp->flags & FLAG_GROUP)
592 if (dp->flags & FLAG_LEFT)
594 if (dp->flags & FLAG_SHOWSIGN)
596 if (dp->flags & FLAG_SPACE)
598 if (dp->flags & FLAG_ALT)
600 if (dp->flags & FLAG_ZERO)
602 if (dp->width_start != dp->width_end)
604 size_t n = dp->width_end - dp->width_start;
605 memcpy (p, dp->width_start, n);
608 if (dp->precision_start != dp->precision_end)
610 size_t n = dp->precision_end - dp->precision_start;
611 memcpy (p, dp->precision_start, n);
617 #ifdef HAVE_INT64_AND_I64
625 #ifdef HAVE_LONG_LONG
626 case TYPE_LONGLONGINT:
627 case TYPE_ULONGLONGINT:
628 #ifdef HAVE_INT64_AND_I64 /* The system (sn)printf uses %I64. Also assume
629 * that long long == __int64.
646 case TYPE_WIDE_STRING:
650 #ifdef HAVE_LONG_DOUBLE
651 case TYPE_LONGDOUBLE:
667 /* Construct the arguments for calling snprintf or sprintf. */
669 if (dp->width_arg_index >= 0)
671 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
673 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
675 if (dp->precision_arg_index >= 0)
677 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
679 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
683 /* Prepare checking whether snprintf returns the count
685 ENSURE_ALLOCATION (length + 1);
686 result[length] = '\0';
695 maxlen = allocated - length;
700 #define SNPRINTF_BUF(arg) \
701 switch (prefix_count) \
704 retcount = snprintf (result + length, maxlen, buf, \
708 retcount = snprintf (result + length, maxlen, buf, \
709 prefixes[0], arg, &count); \
712 retcount = snprintf (result + length, maxlen, buf, \
713 prefixes[0], prefixes[1], arg, \
720 #define SNPRINTF_BUF(arg) \
721 switch (prefix_count) \
724 count = sprintf (tmp, buf, arg); \
727 count = sprintf (tmp, buf, prefixes[0], arg); \
730 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
742 int arg = a.arg[dp->arg_index].a.a_schar;
748 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
754 int arg = a.arg[dp->arg_index].a.a_short;
760 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
766 int arg = a.arg[dp->arg_index].a.a_int;
772 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
778 long int arg = a.arg[dp->arg_index].a.a_longint;
784 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
788 #ifdef HAVE_INT64_AND_I64
791 __int64 arg = a.arg[dp->arg_index].a.a_int64;
797 unsigned __int64 arg = a.arg[dp->arg_index].a.a_uint64;
802 #ifdef HAVE_LONG_LONG
803 #ifndef HAVE_LONG_LONG_FORMAT
804 case TYPE_LONGLONGINT:
805 case TYPE_ULONGLONGINT:
807 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
812 if (dp->width_start != dp->width_end)
814 if (dp->width_arg_index >= 0)
818 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
820 arg = a.arg[dp->width_arg_index].a.a_int;
821 width = (arg < 0 ? -arg : arg);
825 const char *digitp = dp->width_start;
828 width = width * 10 + (*digitp++ - '0');
829 while (digitp != dp->width_end);
834 if (dp->precision_start != dp->precision_end)
836 if (dp->precision_arg_index >= 0)
840 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
842 arg = a.arg[dp->precision_arg_index].a.a_int;
843 precision = (arg < 0 ? 0 : arg);
847 const char *digitp = dp->precision_start + 1;
851 precision = precision * 10 + (*digitp++ - '0');
852 while (digitp != dp->precision_end);
856 count = print_long_long (result + length, maxlen,
864 case TYPE_LONGLONGINT:
866 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
870 case TYPE_ULONGLONGINT:
872 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
880 double arg = a.arg[dp->arg_index].a.a_double;
884 #ifdef HAVE_LONG_DOUBLE
885 case TYPE_LONGDOUBLE:
887 long double arg = a.arg[dp->arg_index].a.a_longdouble;
894 int arg = a.arg[dp->arg_index].a.a_char;
901 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
908 const char *arg = a.arg[dp->arg_index].a.a_string;
913 case TYPE_WIDE_STRING:
915 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
922 void *arg = a.arg[dp->arg_index].a.a_pointer;
931 /* Portability: Not all implementations of snprintf()
932 are ISO C 99 compliant. Determine the number of
933 bytes that snprintf() has produced or would have
937 /* Verify that snprintf() has NUL-terminated its
939 if (count < maxlen && result[length + count] != '\0')
941 /* Portability hack. */
942 if (retcount > count)
947 /* snprintf() doesn't understand the '%n'
951 /* Don't use the '%n' directive; instead, look
952 at the snprintf() return value. */
960 /* Attempt to handle failure. */
963 if (!(result == resultbuf || result == NULL))
972 if (count >= tmp_length)
973 /* tmp_length was incorrectly calculated - fix the
978 /* Make room for the result. */
981 /* Need at least count bytes. But allocate
982 proportionally, to avoid looping eternally if
983 snprintf() reports a too small count. */
984 size_t n = length + count;
986 if (n < 2 * allocated)
989 ENSURE_ALLOCATION (n);
996 /* The snprintf() result did fit. */
998 /* Append the sprintf() result. */
999 memcpy (result + length, tmp, count);
1011 /* Add the final NUL. */
1012 ENSURE_ALLOCATION (length + 1);
1013 result[length] = '\0';
1015 if (result != resultbuf && length + 1 < allocated)
1017 /* Shrink the allocated memory if possible. */
1020 memory = (char *) realloc (result, length + 1);