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;
409 precision = precision * 10 + (*digitp++ - '0');
410 while (digitp != dp->precision_end);
414 switch (dp->conversion)
416 case 'd': case 'i': case 'u':
417 # ifdef HAVE_LONG_LONG
418 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
420 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
421 * 0.30103 /* binary -> decimal */
422 * 2 /* estimate for FLAG_GROUP */
424 + 1 /* turn floor into ceil */
425 + 1; /* account for leading sign */
428 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
430 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
431 * 0.30103 /* binary -> decimal */
432 * 2 /* estimate for FLAG_GROUP */
434 + 1 /* turn floor into ceil */
435 + 1; /* account for leading sign */
438 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
439 * 0.30103 /* binary -> decimal */
440 * 2 /* estimate for FLAG_GROUP */
442 + 1 /* turn floor into ceil */
443 + 1; /* account for leading sign */
447 # ifdef HAVE_LONG_LONG
448 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
450 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
451 * 0.333334 /* binary -> octal */
453 + 1 /* turn floor into ceil */
454 + 1; /* account for leading sign */
457 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
459 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
460 * 0.333334 /* binary -> octal */
462 + 1 /* turn floor into ceil */
463 + 1; /* account for leading sign */
466 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
467 * 0.333334 /* binary -> octal */
469 + 1 /* turn floor into ceil */
470 + 1; /* account for leading sign */
474 # ifdef HAVE_LONG_LONG
475 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
477 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
478 * 0.25 /* binary -> hexadecimal */
480 + 1 /* turn floor into ceil */
481 + 2; /* account for leading sign or alternate form */
484 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
486 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
487 * 0.25 /* binary -> hexadecimal */
489 + 1 /* turn floor into ceil */
490 + 2; /* account for leading sign or alternate form */
493 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
494 * 0.25 /* binary -> hexadecimal */
496 + 1 /* turn floor into ceil */
497 + 2; /* account for leading sign or alternate form */
501 # ifdef HAVE_LONG_DOUBLE
502 if (type == TYPE_LONGDOUBLE)
504 (unsigned int) (LDBL_MAX_EXP
505 * 0.30103 /* binary -> decimal */
506 * 2 /* estimate for FLAG_GROUP */
508 + 1 /* turn floor into ceil */
510 + 10; /* sign, decimal point etc. */
514 (unsigned int) (DBL_MAX_EXP
515 * 0.30103 /* binary -> decimal */
516 * 2 /* estimate for FLAG_GROUP */
518 + 1 /* turn floor into ceil */
520 + 10; /* sign, decimal point etc. */
523 case 'e': case 'E': case 'g': case 'G':
527 + 12; /* sign, decimal point, exponent etc. */
532 if (type == TYPE_WIDE_CHAR)
533 tmp_length = MB_CUR_MAX;
541 if (type == TYPE_WIDE_STRING)
543 wcslen (a.arg[dp->arg_index].a.a_wide_string)
547 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
552 (unsigned int) (sizeof (void *) * CHAR_BIT
553 * 0.25 /* binary -> hexadecimal */
555 + 1 /* turn floor into ceil */
556 + 2; /* account for leading 0x */
563 if (tmp_length < width)
566 tmp_length++; /* account for trailing NUL */
569 if (tmp_length <= sizeof (tmpbuf))
573 tmp = (char *) malloc (tmp_length);
577 if (!(result == resultbuf || result == NULL))
587 /* Construct the format string for calling snprintf or
591 if (dp->flags & FLAG_GROUP)
593 if (dp->flags & FLAG_LEFT)
595 if (dp->flags & FLAG_SHOWSIGN)
597 if (dp->flags & FLAG_SPACE)
599 if (dp->flags & FLAG_ALT)
601 if (dp->flags & FLAG_ZERO)
603 if (dp->width_start != dp->width_end)
605 size_t n = dp->width_end - dp->width_start;
606 memcpy (p, dp->width_start, n);
609 if (dp->precision_start != dp->precision_end)
611 size_t n = dp->precision_end - dp->precision_start;
612 memcpy (p, dp->precision_start, n);
618 #ifdef HAVE_INT64_AND_I64
626 #ifdef HAVE_LONG_LONG
627 case TYPE_LONGLONGINT:
628 case TYPE_ULONGLONGINT:
638 case TYPE_WIDE_STRING:
642 #ifdef HAVE_LONG_DOUBLE
643 case TYPE_LONGDOUBLE:
659 /* Construct the arguments for calling snprintf or sprintf. */
661 if (dp->width_arg_index >= 0)
663 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
665 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
667 if (dp->precision_arg_index >= 0)
669 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
671 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
675 /* Prepare checking whether snprintf returns the count
677 ENSURE_ALLOCATION (length + 1);
678 result[length] = '\0';
687 maxlen = allocated - length;
692 #define SNPRINTF_BUF(arg) \
693 switch (prefix_count) \
696 retcount = snprintf (result + length, maxlen, buf, \
700 retcount = snprintf (result + length, maxlen, buf, \
701 prefixes[0], arg, &count); \
704 retcount = snprintf (result + length, maxlen, buf, \
705 prefixes[0], prefixes[1], arg, \
712 #define SNPRINTF_BUF(arg) \
713 switch (prefix_count) \
716 count = sprintf (tmp, buf, arg); \
719 count = sprintf (tmp, buf, prefixes[0], arg); \
722 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
734 int arg = a.arg[dp->arg_index].a.a_schar;
740 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
746 int arg = a.arg[dp->arg_index].a.a_short;
752 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
758 int arg = a.arg[dp->arg_index].a.a_int;
764 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
770 long int arg = a.arg[dp->arg_index].a.a_longint;
776 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
780 #ifdef HAVE_INT64_AND_I64
783 __int64 arg = a.arg[dp->arg_index].a.a_int64;
789 unsigned __int64 arg = a.arg[dp->arg_index].a.a_uint64;
794 #ifdef HAVE_LONG_LONG
795 #ifndef HAVE_LONG_LONG_FORMAT
796 case TYPE_LONGLONGINT:
797 case TYPE_ULONGLONGINT:
799 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
804 if (dp->width_start != dp->width_end)
806 if (dp->width_arg_index >= 0)
810 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
812 arg = a.arg[dp->width_arg_index].a.a_int;
813 width = (arg < 0 ? -arg : arg);
817 const char *digitp = dp->width_start;
820 width = width * 10 + (*digitp++ - '0');
821 while (digitp != dp->width_end);
826 if (dp->precision_start != dp->precision_end)
828 if (dp->precision_arg_index >= 0)
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 *digitp = dp->precision_start + 1;
843 precision = precision * 10 + (*digitp++ - '0');
844 while (digitp != dp->precision_end);
848 count = print_long_long (result + length, maxlen,
856 case TYPE_LONGLONGINT:
858 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
862 case TYPE_ULONGLONGINT:
864 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
872 double arg = a.arg[dp->arg_index].a.a_double;
876 #ifdef HAVE_LONG_DOUBLE
877 case TYPE_LONGDOUBLE:
879 long double arg = a.arg[dp->arg_index].a.a_longdouble;
886 int arg = a.arg[dp->arg_index].a.a_char;
893 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
900 const char *arg = a.arg[dp->arg_index].a.a_string;
905 case TYPE_WIDE_STRING:
907 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
914 void *arg = a.arg[dp->arg_index].a.a_pointer;
923 /* Portability: Not all implementations of snprintf()
924 are ISO C 99 compliant. Determine the number of
925 bytes that snprintf() has produced or would have
929 /* Verify that snprintf() has NUL-terminated its
931 if (count < maxlen && result[length + count] != '\0')
933 /* Portability hack. */
934 if (retcount > count)
939 /* snprintf() doesn't understand the '%n'
943 /* Don't use the '%n' directive; instead, look
944 at the snprintf() return value. */
952 /* Attempt to handle failure. */
955 if (!(result == resultbuf || result == NULL))
964 if (count >= tmp_length)
965 /* tmp_length was incorrectly calculated - fix the
970 /* Make room for the result. */
973 /* Need at least count bytes. But allocate
974 proportionally, to avoid looping eternally if
975 snprintf() reports a too small count. */
976 size_t n = length + count;
978 if (n < 2 * allocated)
981 ENSURE_ALLOCATION (n);
988 /* The snprintf() result did fit. */
990 /* Append the sprintf() result. */
991 memcpy (result + length, tmp, count);
1003 /* Add the final NUL. */
1004 ENSURE_ALLOCATION (length + 1);
1005 result[length] = '\0';
1007 if (result != resultbuf && length + 1 < allocated)
1009 /* Shrink the allocated memory if possible. */
1012 memory = (char *) realloc (result, length + 1);