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,
20 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
24 # define _GNU_SOURCE 1
31 #include "glib/galloca.h"
36 #include "vasnprintf.h"
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 */
44 #include "printf-parse.h"
48 # define local_wcslen wcslen
50 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
51 a dependency towards this library, here is a local substitute.
52 Define this substitute only once, even if this file is included
53 twice in the same compilation unit. */
54 # ifndef local_wcslen_defined
55 # define local_wcslen_defined 1
57 local_wcslen (const wchar_t *s)
61 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
69 /* For those losing systems which don't have 'alloca' we have to add
70 some additional code emulating it. */
72 # define freea(p) /* nothing */
74 # define alloca(n) malloc (n)
75 # define freea(p) free (p)
78 #ifndef HAVE_LONG_LONG_FORMAT
80 print_long_long (char *buf,
86 unsigned long long number)
93 static const char *upper = "0123456789ABCDEFX";
94 static const char *lower = "0123456789abcdefx";
101 if (p - buf == len - 1) \
131 negative = (long long)number < 0;
133 number = -((long long)number);
138 pointer = bufferend = &buffer[sizeof(buffer) - 1];
140 for (i = 1; i < (int)sizeof(buffer); i++)
142 *pointer-- = digits[number % base];
149 width -= (bufferend - pointer) - 1;
151 /* Adjust precision */
154 precision -= (bufferend - pointer) - 1;
160 /* Adjust width further */
161 if (negative || (flags & FLAG_SHOWSIGN) || (flags & FLAG_SPACE))
163 if (flags & FLAG_ALT)
178 /* Output prefixes spaces if needed */
179 if (! ((flags & FLAG_LEFT) ||
180 ((flags & FLAG_ZERO) && (precision == -1))))
182 count = (precision == -1) ? 0 : precision;
183 while (width-- > count)
187 /* width has been adjusted for signs and alternatives */
192 else if (flags & FLAG_SHOWSIGN)
196 else if (flags & FLAG_SPACE)
201 if (flags & FLAG_ALT)
217 /* Output prefixed zero padding if needed */
218 if (flags & FLAG_ZERO)
222 while (precision-- > 0)
229 /* Output the number itself */
235 /* Output trailing spaces if needed */
236 if (flags & FLAG_LEFT)
249 vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
254 if (printf_parse (format, &d, &a) < 0)
265 if (printf_fetchargs (args, &a) < 0)
274 (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
278 /* Output string accumulator. */
283 if (resultbuf != NULL)
286 allocated = *lengthp;
295 result is either == resultbuf or == NULL or malloc-allocated.
296 If length > 0, then result != NULL. */
298 #define ENSURE_ALLOCATION(needed) \
299 if ((needed) > allocated) \
303 allocated = (allocated > 0 ? 2 * allocated : 12); \
304 if ((needed) > allocated) \
305 allocated = (needed); \
306 if (result == resultbuf || result == NULL) \
307 memory = (char *) malloc (allocated); \
309 memory = (char *) realloc (result, allocated); \
311 if (memory == NULL) \
313 if (!(result == resultbuf || result == NULL)) \
320 if (result == resultbuf && length > 0) \
321 memcpy (memory, result, length); \
325 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
327 if (cp != dp->dir_start)
329 size_t n = dp->dir_start - cp;
331 ENSURE_ALLOCATION (length + n);
332 memcpy (result + length, cp, n);
338 /* Execute a single directive. */
339 if (dp->conversion == '%')
341 if (!(dp->arg_index < 0))
343 ENSURE_ALLOCATION (length + 1);
344 result[length] = '%';
349 if (!(dp->arg_index >= 0))
352 if (dp->conversion == 'n')
354 switch (a.arg[dp->arg_index].type)
356 case TYPE_COUNT_SCHAR_POINTER:
357 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
359 case TYPE_COUNT_SHORT_POINTER:
360 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
362 case TYPE_COUNT_INT_POINTER:
363 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
365 case TYPE_COUNT_LONGINT_POINTER:
366 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
368 #ifdef HAVE_LONG_LONG
369 case TYPE_COUNT_LONGLONGINT_POINTER:
370 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
379 arg_type type = a.arg[dp->arg_index].type;
381 unsigned int prefix_count;
384 unsigned int tmp_length;
388 /* Allocate a temporary buffer of sufficient size for calling
392 unsigned int precision;
395 if (dp->width_start != dp->width_end)
397 if (dp->width_arg_index >= 0)
401 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
403 arg = a.arg[dp->width_arg_index].a.a_int;
404 width = (arg < 0 ? -arg : arg);
408 const char *digitp = dp->width_start;
411 width = width * 10 + (*digitp++ - '0');
412 while (digitp != dp->width_end);
417 if (dp->precision_start != dp->precision_end)
419 if (dp->precision_arg_index >= 0)
423 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
425 arg = a.arg[dp->precision_arg_index].a.a_int;
426 precision = (arg < 0 ? 0 : arg);
430 const char *digitp = dp->precision_start + 1;
433 while (digitp != dp->precision_end)
434 precision = precision * 10 + (*digitp++ - '0');
438 switch (dp->conversion)
440 case 'd': case 'i': case 'u':
441 # ifdef HAVE_LONG_LONG
442 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
444 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
445 * 0.30103 /* binary -> decimal */
446 * 2 /* estimate for FLAG_GROUP */
448 + 1 /* turn floor into ceil */
449 + 1; /* account for leading sign */
452 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
454 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
455 * 0.30103 /* binary -> decimal */
456 * 2 /* estimate for FLAG_GROUP */
458 + 1 /* turn floor into ceil */
459 + 1; /* account for leading sign */
462 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
463 * 0.30103 /* binary -> decimal */
464 * 2 /* estimate for FLAG_GROUP */
466 + 1 /* turn floor into ceil */
467 + 1; /* account for leading sign */
471 # ifdef HAVE_LONG_LONG
472 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
474 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
475 * 0.333334 /* binary -> octal */
477 + 1 /* turn floor into ceil */
478 + 1; /* account for leading sign */
481 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
483 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
484 * 0.333334 /* binary -> octal */
486 + 1 /* turn floor into ceil */
487 + 1; /* account for leading sign */
490 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
491 * 0.333334 /* binary -> octal */
493 + 1 /* turn floor into ceil */
494 + 1; /* account for leading sign */
498 # ifdef HAVE_LONG_LONG
499 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
501 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
502 * 0.25 /* binary -> hexadecimal */
504 + 1 /* turn floor into ceil */
505 + 2; /* account for leading sign or alternate form */
508 # ifdef HAVE_INT64_AND_I64
509 if (type == TYPE_INT64 || type == TYPE_UINT64)
511 (unsigned int) (sizeof (unsigned __int64) * CHAR_BIT
512 * 0.25 /* binary -> hexadecimal */
514 + 1 /* turn floor into ceil */
515 + 2; /* account for leading sign or alternate form */
518 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
520 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
521 * 0.25 /* binary -> hexadecimal */
523 + 1 /* turn floor into ceil */
524 + 2; /* account for leading sign or alternate form */
527 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
528 * 0.25 /* binary -> hexadecimal */
530 + 1 /* turn floor into ceil */
531 + 2; /* account for leading sign or alternate form */
535 # ifdef HAVE_LONG_DOUBLE
536 if (type == TYPE_LONGDOUBLE)
538 (unsigned int) (LDBL_MAX_EXP
539 * 0.30103 /* binary -> decimal */
540 * 2 /* estimate for FLAG_GROUP */
542 + 1 /* turn floor into ceil */
544 + 10; /* sign, decimal point etc. */
548 (unsigned int) (DBL_MAX_EXP
549 * 0.30103 /* binary -> decimal */
550 * 2 /* estimate for FLAG_GROUP */
552 + 1 /* turn floor into ceil */
554 + 10; /* sign, decimal point etc. */
557 case 'e': case 'E': case 'g': case 'G':
561 + 12; /* sign, decimal point, exponent etc. */
566 if (type == TYPE_WIDE_CHAR)
567 tmp_length = MB_CUR_MAX;
575 if (type == TYPE_WIDE_STRING)
577 (a.arg[dp->arg_index].a.a_wide_string == NULL
578 ? 6 /* wcslen(L"(null)") */
579 : local_wcslen (a.arg[dp->arg_index].a.a_wide_string))
583 tmp_length = a.arg[dp->arg_index].a.a_string == NULL
584 ? 6 /* strlen("(null)") */
585 : strlen (a.arg[dp->arg_index].a.a_string);
590 (unsigned int) (sizeof (void *) * CHAR_BIT
591 * 0.25 /* binary -> hexadecimal */
593 + 1 /* turn floor into ceil */
594 + 2; /* account for leading 0x */
601 if (tmp_length < width)
604 tmp_length++; /* account for trailing NUL */
607 if (tmp_length <= sizeof (tmpbuf))
611 tmp = (char *) malloc (tmp_length);
615 if (!(result == resultbuf || result == NULL))
625 /* Construct the format string for calling snprintf or
629 if (dp->flags & FLAG_GROUP)
631 if (dp->flags & FLAG_LEFT)
633 if (dp->flags & FLAG_SHOWSIGN)
635 if (dp->flags & FLAG_SPACE)
637 if (dp->flags & FLAG_ALT)
639 if (dp->flags & FLAG_ZERO)
641 if (dp->width_start != dp->width_end)
643 size_t n = dp->width_end - dp->width_start;
644 memcpy (p, dp->width_start, n);
647 if (dp->precision_start != dp->precision_end)
649 size_t n = dp->precision_end - dp->precision_start;
650 memcpy (p, dp->precision_start, n);
656 #ifdef HAVE_INT64_AND_I64
664 #ifdef HAVE_LONG_LONG
665 case TYPE_LONGLONGINT:
666 case TYPE_ULONGLONGINT:
667 #ifdef HAVE_INT64_AND_I64 /* The system (sn)printf uses %I64. Also assume
668 * that long long == __int64.
685 case TYPE_WIDE_STRING:
689 #ifdef HAVE_LONG_DOUBLE
690 case TYPE_LONGDOUBLE:
706 /* Construct the arguments for calling snprintf or sprintf. */
708 if (dp->width_arg_index >= 0)
710 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
712 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
714 if (dp->precision_arg_index >= 0)
716 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
718 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
722 /* Prepare checking whether snprintf returns the count
724 ENSURE_ALLOCATION (length + 1);
725 result[length] = '\0';
734 maxlen = allocated - length;
739 #define SNPRINTF_BUF(arg) \
740 switch (prefix_count) \
743 retcount = snprintf (result + length, maxlen, buf, \
747 retcount = snprintf (result + length, maxlen, buf, \
748 prefixes[0], arg, &count); \
751 retcount = snprintf (result + length, maxlen, buf, \
752 prefixes[0], prefixes[1], arg, \
759 #define SNPRINTF_BUF(arg) \
760 switch (prefix_count) \
763 count = sprintf (tmp, buf, arg); \
766 count = sprintf (tmp, buf, prefixes[0], arg); \
769 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
781 int arg = a.arg[dp->arg_index].a.a_schar;
787 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
793 int arg = a.arg[dp->arg_index].a.a_short;
799 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
805 int arg = a.arg[dp->arg_index].a.a_int;
811 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
817 long int arg = a.arg[dp->arg_index].a.a_longint;
823 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
827 #ifdef HAVE_INT64_AND_I64
830 __int64 arg = a.arg[dp->arg_index].a.a_int64;
836 unsigned __int64 arg = a.arg[dp->arg_index].a.a_uint64;
841 #ifdef HAVE_LONG_LONG
842 #ifndef HAVE_LONG_LONG_FORMAT
843 case TYPE_LONGLONGINT:
844 case TYPE_ULONGLONGINT:
846 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
851 if (dp->width_start != dp->width_end)
853 if (dp->width_arg_index >= 0)
857 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
859 arg = a.arg[dp->width_arg_index].a.a_int;
860 width = (arg < 0 ? -arg : arg);
864 const char *digitp = dp->width_start;
867 width = width * 10 + (*digitp++ - '0');
868 while (digitp != dp->width_end);
873 if (dp->precision_start != dp->precision_end)
875 if (dp->precision_arg_index >= 0)
879 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
881 arg = a.arg[dp->precision_arg_index].a.a_int;
882 precision = (arg < 0 ? 0 : arg);
886 const char *digitp = dp->precision_start + 1;
890 precision = precision * 10 + (*digitp++ - '0');
891 while (digitp != dp->precision_end);
896 count = print_long_long (result + length, maxlen,
902 count = print_long_long (tmp, tmp_length,
911 case TYPE_LONGLONGINT:
913 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
917 case TYPE_ULONGLONGINT:
919 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
927 double arg = a.arg[dp->arg_index].a.a_double;
931 #ifdef HAVE_LONG_DOUBLE
932 case TYPE_LONGDOUBLE:
934 long double arg = a.arg[dp->arg_index].a.a_longdouble;
941 int arg = a.arg[dp->arg_index].a.a_char;
948 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
955 const char *arg = a.arg[dp->arg_index].a.a_string == NULL
957 : a.arg[dp->arg_index].a.a_string;
962 case TYPE_WIDE_STRING:
964 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string == NULL
966 : a.arg[dp->arg_index].a.a_wide_string;
973 void *arg = a.arg[dp->arg_index].a.a_pointer;
982 /* Portability: Not all implementations of snprintf()
983 are ISO C 99 compliant. Determine the number of
984 bytes that snprintf() has produced or would have
988 /* Verify that snprintf() has NUL-terminated its
990 if (count < maxlen && result[length + count] != '\0')
992 /* Portability hack. */
993 if (retcount > count)
998 /* snprintf() doesn't understand the '%n'
1002 /* Don't use the '%n' directive; instead, look
1003 at the snprintf() return value. */
1011 /* Attempt to handle failure. */
1014 if (!(result == resultbuf || result == NULL))
1023 if (count >= tmp_length)
1024 /* tmp_length was incorrectly calculated - fix the
1029 /* Make room for the result. */
1030 if (count >= maxlen)
1032 /* Need at least count bytes. But allocate
1033 proportionally, to avoid looping eternally if
1034 snprintf() reports a too small count. */
1035 size_t n = length + count;
1037 if (n < 2 * allocated)
1040 ENSURE_ALLOCATION (n);
1047 /* The snprintf() result did fit. */
1049 /* Append the sprintf() result. */
1050 memcpy (result + length, tmp, count);
1062 /* Add the final NUL. */
1063 ENSURE_ALLOCATION (length + 1);
1064 result[length] = '\0';
1066 if (result != resultbuf && length + 1 < allocated)
1068 /* Shrink the allocated memory if possible. */
1071 memory = (char *) realloc (result, length + 1);