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
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
31 #include "vasnprintf.h"
33 #include <stdio.h> /* snprintf(), sprintf() */
34 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
35 #include <string.h> /* memcpy(), strlen() */
36 #include <errno.h> /* errno */
37 #include <limits.h> /* CHAR_BIT */
38 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
39 #include "printf-parse.h"
41 /* For those losing systems which don't have 'alloca' we have to add
42 some additional code emulating it. */
44 # define freea(p) /* nothing */
46 # define alloca(n) malloc (n)
47 # define freea(p) free (p)
51 vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
56 if (printf_parse (format, &d, &a) < 0)
67 if (printf_fetchargs (args, &a) < 0)
76 (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
80 /* Output string accumulator. */
85 if (resultbuf != NULL)
97 result is either == resultbuf or == NULL or malloc-allocated.
98 If length > 0, then result != NULL. */
100 #define ENSURE_ALLOCATION(needed) \
101 if ((needed) > allocated) \
105 allocated = (allocated > 0 ? 2 * allocated : 12); \
106 if ((needed) > allocated) \
107 allocated = (needed); \
108 if (result == resultbuf || result == NULL) \
109 memory = (char *) malloc (allocated); \
111 memory = (char *) realloc (result, allocated); \
113 if (memory == NULL) \
115 if (!(result == resultbuf || result == NULL)) \
122 if (result == resultbuf && length > 0) \
123 memcpy (memory, result, length); \
127 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
129 if (cp != dp->dir_start)
131 size_t n = dp->dir_start - cp;
133 ENSURE_ALLOCATION (length + n);
134 memcpy (result + length, cp, n);
140 /* Execute a single directive. */
141 if (dp->conversion == '%')
143 if (!(dp->arg_index < 0))
145 ENSURE_ALLOCATION (length + 1);
146 result[length] = '%';
151 if (!(dp->arg_index >= 0))
154 if (dp->conversion == 'n')
156 switch (a.arg[dp->arg_index].type)
158 case TYPE_COUNT_SCHAR_POINTER:
159 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
161 case TYPE_COUNT_SHORT_POINTER:
162 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
164 case TYPE_COUNT_INT_POINTER:
165 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
167 case TYPE_COUNT_LONGINT_POINTER:
168 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
170 #ifdef HAVE_LONG_LONG
171 case TYPE_COUNT_LONGLONGINT_POINTER:
172 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
181 arg_type type = a.arg[dp->arg_index].type;
183 unsigned int prefix_count;
186 unsigned int tmp_length;
190 /* Allocate a temporary buffer of sufficient size for calling
194 unsigned int precision;
197 if (dp->width_start != dp->width_end)
199 if (dp->width_arg_index >= 0)
203 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
205 arg = a.arg[dp->width_arg_index].a.a_int;
206 width = (arg < 0 ? -arg : arg);
210 const char *digitp = dp->width_start;
213 width = width * 10 + (*digitp++ - '0');
214 while (digitp != dp->width_end);
219 if (dp->precision_start != dp->precision_end)
221 if (dp->precision_arg_index >= 0)
225 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
227 arg = a.arg[dp->precision_arg_index].a.a_int;
228 precision = (arg < 0 ? 0 : arg);
232 const char *digitp = dp->precision_start + 1;
236 precision = precision * 10 + (*digitp++ - '0');
237 while (digitp != dp->precision_end);
241 switch (dp->conversion)
244 case 'd': case 'i': case 'u':
245 # ifdef HAVE_LONG_LONG
246 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
248 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
249 * 0.30103 /* binary -> decimal */
250 * 2 /* estimate for FLAG_GROUP */
252 + 1 /* turn floor into ceil */
253 + 1; /* account for leading sign */
256 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
258 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
259 * 0.30103 /* binary -> decimal */
260 * 2 /* estimate for FLAG_GROUP */
262 + 1 /* turn floor into ceil */
263 + 1; /* account for leading sign */
266 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
267 * 0.30103 /* binary -> decimal */
268 * 2 /* estimate for FLAG_GROUP */
270 + 1 /* turn floor into ceil */
271 + 1; /* account for leading sign */
275 # ifdef HAVE_LONG_LONG
276 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
278 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
279 * 0.333334 /* binary -> octal */
281 + 1 /* turn floor into ceil */
282 + 1; /* account for leading sign */
285 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
287 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
288 * 0.333334 /* binary -> octal */
290 + 1 /* turn floor into ceil */
291 + 1; /* account for leading sign */
294 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
295 * 0.333334 /* binary -> octal */
297 + 1 /* turn floor into ceil */
298 + 1; /* account for leading sign */
302 # ifdef HAVE_LONG_LONG
303 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
305 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
306 * 0.25 /* binary -> hexadecimal */
308 + 1 /* turn floor into ceil */
309 + 2; /* account for leading sign or alternate form */
312 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
314 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
315 * 0.25 /* binary -> hexadecimal */
317 + 1 /* turn floor into ceil */
318 + 2; /* account for leading sign or alternate form */
321 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
322 * 0.25 /* binary -> hexadecimal */
324 + 1 /* turn floor into ceil */
325 + 2; /* account for leading sign or alternate form */
329 # ifdef HAVE_LONG_DOUBLE
330 if (type == TYPE_LONGDOUBLE)
332 (unsigned int) (LDBL_MAX_EXP
333 * 0.30103 /* binary -> decimal */
334 * 2 /* estimate for FLAG_GROUP */
336 + 1 /* turn floor into ceil */
338 + 10; /* sign, decimal point etc. */
342 (unsigned int) (DBL_MAX_EXP
343 * 0.30103 /* binary -> decimal */
344 * 2 /* estimate for FLAG_GROUP */
346 + 1 /* turn floor into ceil */
348 + 10; /* sign, decimal point etc. */
351 case 'e': case 'E': case 'g': case 'G':
355 + 12; /* sign, decimal point, exponent etc. */
360 if (type == TYPE_WIDE_CHAR)
361 tmp_length = MB_CUR_MAX;
369 if (type == TYPE_WIDE_STRING)
371 wcslen (a.arg[dp->arg_index].a.a_wide_string)
375 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
380 (unsigned int) (sizeof (void *) * CHAR_BIT
381 * 0.25 /* binary -> hexadecimal */
383 + 1 /* turn floor into ceil */
384 + 2; /* account for leading 0x */
391 if (tmp_length < width)
394 tmp_length++; /* account for trailing NUL */
397 if (tmp_length <= sizeof (tmpbuf))
401 tmp = (char *) malloc (tmp_length);
405 if (!(result == resultbuf || result == NULL))
415 /* Construct the format string for calling snprintf or
419 if (dp->flags & FLAG_GROUP)
421 if (dp->flags & FLAG_LEFT)
423 if (dp->flags & FLAG_SHOWSIGN)
425 if (dp->flags & FLAG_SPACE)
427 if (dp->flags & FLAG_ALT)
429 if (dp->flags & FLAG_ZERO)
431 if (dp->width_start != dp->width_end)
433 size_t n = dp->width_end - dp->width_start;
434 memcpy (p, dp->width_start, n);
437 if (dp->precision_start != dp->precision_end)
439 size_t n = dp->precision_end - dp->precision_start;
440 memcpy (p, dp->precision_start, n);
446 #ifdef HAVE_LONG_LONG
447 case TYPE_LONGLONGINT:
448 case TYPE_ULONGLONGINT:
458 case TYPE_WIDE_STRING:
462 #ifdef HAVE_LONG_DOUBLE
463 case TYPE_LONGDOUBLE:
479 /* Construct the arguments for calling snprintf or sprintf. */
481 if (dp->width_arg_index >= 0)
483 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
485 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
487 if (dp->precision_arg_index >= 0)
489 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
491 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
495 /* Prepare checking whether snprintf returns the count
497 ENSURE_ALLOCATION (length + 1);
498 result[length] = '\0';
507 maxlen = allocated - length;
512 #define SNPRINTF_BUF(arg) \
513 switch (prefix_count) \
516 retcount = snprintf (result + length, maxlen, buf, \
520 retcount = snprintf (result + length, maxlen, buf, \
521 prefixes[0], arg, &count); \
524 retcount = snprintf (result + length, maxlen, buf, \
525 prefixes[0], prefixes[1], arg, \
532 #define SNPRINTF_BUF(arg) \
533 switch (prefix_count) \
536 count = sprintf (tmp, buf, arg); \
539 count = sprintf (tmp, buf, prefixes[0], arg); \
542 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
554 int arg = a.arg[dp->arg_index].a.a_schar;
560 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
566 int arg = a.arg[dp->arg_index].a.a_short;
572 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
578 int arg = a.arg[dp->arg_index].a.a_int;
584 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
590 long int arg = a.arg[dp->arg_index].a.a_longint;
596 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
600 #ifdef HAVE_LONG_LONG
601 case TYPE_LONGLONGINT:
603 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
607 case TYPE_ULONGLONGINT:
609 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
616 double arg = a.arg[dp->arg_index].a.a_double;
620 #ifdef HAVE_LONG_DOUBLE
621 case TYPE_LONGDOUBLE:
623 long double arg = a.arg[dp->arg_index].a.a_longdouble;
630 int arg = a.arg[dp->arg_index].a.a_char;
637 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
644 const char *arg = a.arg[dp->arg_index].a.a_string;
649 case TYPE_WIDE_STRING:
651 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
658 void *arg = a.arg[dp->arg_index].a.a_pointer;
667 /* Portability: Not all implementations of snprintf()
668 are ISO C 99 compliant. Determine the number of
669 bytes that snprintf() has produced or would have
673 /* Verify that snprintf() has NUL-terminated its
675 if (count < maxlen && result[length + count] != '\0')
677 /* Portability hack. */
678 if (retcount > count)
683 /* snprintf() doesn't understand the '%n'
687 /* Don't use the '%n' directive; instead, look
688 at the snprintf() return value. */
696 /* Attempt to handle failure. */
699 if (!(result == resultbuf || result == NULL))
708 if (count >= tmp_length)
709 /* tmp_length was incorrectly calculated - fix the
714 /* Make room for the result. */
717 /* Need at least count bytes. But allocate
718 proportionally, to avoid looping eternally if
719 snprintf() reports a too small count. */
720 size_t n = length + count;
722 if (n < 2 * allocated)
725 ENSURE_ALLOCATION (n);
732 /* The snprintf() result did fit. */
734 /* Append the sprintf() result. */
735 memcpy (result + length, tmp, count);
747 /* Add the final NUL. */
748 ENSURE_ALLOCATION (length + 1);
749 result[length] = '\0';
751 if (result != resultbuf && length + 1 < allocated)
753 /* Shrink the allocated memory if possible. */
756 memory = (char *) realloc (result, length + 1);