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
34 # include "vasnwprintf.h"
36 # include "vasnprintf.h"
39 #include <stdio.h> /* snprintf(), sprintf() */
40 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
41 #include <string.h> /* memcpy(), strlen() */
42 #include <errno.h> /* errno */
43 #include <limits.h> /* CHAR_BIT */
44 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
46 # include "wprintf-parse.h"
48 # include "printf-parse.h"
51 /* For those losing systems which don't have 'alloca' we have to add
52 some additional code emulating it. */
54 # define freea(p) /* nothing */
56 # define alloca(n) malloc (n)
57 # define freea(p) free (p)
62 # define local_wcslen wcslen
64 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
65 a dependency towards this library, here is a local substitute.
66 Define this substitute only once, even if this file is included
67 twice in the same compilation unit. */
68 # ifndef local_wcslen_defined
69 # define local_wcslen_defined 1
71 local_wcslen (const wchar_t *s)
75 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
84 # define VASNPRINTF vasnwprintf
85 # define CHAR_T wchar_t
86 # define DIRECTIVE wchar_t_directive
87 # define DIRECTIVES wchar_t_directives
88 # define PRINTF_PARSE wprintf_parse
89 # define USE_SNPRINTF 1
90 # if HAVE_DECL__SNWPRINTF
91 /* On Windows, the function swprintf() has a different signature than
92 on Unix; we use the _snwprintf() function instead. */
93 # define SNPRINTF _snwprintf
96 # define SNPRINTF swprintf
99 # define VASNPRINTF vasnprintf
101 # define DIRECTIVE char_directive
102 # define DIRECTIVES char_directives
103 # define PRINTF_PARSE printf_parse
104 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
105 # if HAVE_DECL__SNPRINTF
107 # define SNPRINTF _snprintf
110 # define SNPRINTF snprintf
115 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
120 if (PRINTF_PARSE (format, &d, &a) < 0)
131 if (printf_fetchargs (args, &a) < 0)
140 (CHAR_T *) alloca ((7 + d.max_width_length + d.max_precision_length + 6)
145 /* Output string accumulator. */
150 if (resultbuf != NULL)
153 allocated = *lengthp;
162 result is either == resultbuf or == NULL or malloc-allocated.
163 If length > 0, then result != NULL. */
165 #define ENSURE_ALLOCATION(needed) \
166 if ((needed) > allocated) \
170 allocated = (allocated > 0 ? 2 * allocated : 12); \
171 if ((needed) > allocated) \
172 allocated = (needed); \
173 if (result == resultbuf || result == NULL) \
174 memory = (CHAR_T *) malloc (allocated * sizeof (CHAR_T)); \
176 memory = (CHAR_T *) realloc (result, allocated * sizeof (CHAR_T)); \
178 if (memory == NULL) \
180 if (!(result == resultbuf || result == NULL)) \
187 if (result == resultbuf && length > 0) \
188 memcpy (memory, result, length * sizeof (CHAR_T)); \
192 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
194 if (cp != dp->dir_start)
196 size_t n = dp->dir_start - cp;
198 ENSURE_ALLOCATION (length + n);
199 memcpy (result + length, cp, n * sizeof (CHAR_T));
205 /* Execute a single directive. */
206 if (dp->conversion == '%')
208 if (!(dp->arg_index < 0))
210 ENSURE_ALLOCATION (length + 1);
211 result[length] = '%';
216 if (!(dp->arg_index >= 0))
219 if (dp->conversion == 'n')
221 switch (a.arg[dp->arg_index].type)
223 case TYPE_COUNT_SCHAR_POINTER:
224 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
226 case TYPE_COUNT_SHORT_POINTER:
227 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
229 case TYPE_COUNT_INT_POINTER:
230 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
232 case TYPE_COUNT_LONGINT_POINTER:
233 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
235 #ifdef HAVE_LONG_LONG
236 case TYPE_COUNT_LONGLONGINT_POINTER:
237 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
246 arg_type type = a.arg[dp->arg_index].type;
248 unsigned int prefix_count;
251 unsigned int tmp_length;
255 /* Allocate a temporary buffer of sufficient size for calling
259 unsigned int precision;
262 if (dp->width_start != dp->width_end)
264 if (dp->width_arg_index >= 0)
268 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
270 arg = a.arg[dp->width_arg_index].a.a_int;
271 width = (arg < 0 ? -arg : arg);
275 const CHAR_T *digitp = dp->width_start;
278 width = width * 10 + (*digitp++ - '0');
279 while (digitp != dp->width_end);
284 if (dp->precision_start != dp->precision_end)
286 if (dp->precision_arg_index >= 0)
290 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
292 arg = a.arg[dp->precision_arg_index].a.a_int;
293 precision = (arg < 0 ? 0 : arg);
297 const CHAR_T *digitp = dp->precision_start + 1;
301 precision = precision * 10 + (*digitp++ - '0');
302 while (digitp != dp->precision_end);
306 switch (dp->conversion)
309 case 'd': case 'i': case 'u':
310 # ifdef HAVE_LONG_LONG
311 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
313 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
314 * 0.30103 /* binary -> decimal */
315 * 2 /* estimate for FLAG_GROUP */
317 + 1 /* turn floor into ceil */
318 + 1; /* account for leading sign */
321 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
323 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
324 * 0.30103 /* binary -> decimal */
325 * 2 /* estimate for FLAG_GROUP */
327 + 1 /* turn floor into ceil */
328 + 1; /* account for leading sign */
331 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
332 * 0.30103 /* binary -> decimal */
333 * 2 /* estimate for FLAG_GROUP */
335 + 1 /* turn floor into ceil */
336 + 1; /* account for leading sign */
340 # ifdef HAVE_LONG_LONG
341 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
343 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
344 * 0.333334 /* binary -> octal */
346 + 1 /* turn floor into ceil */
347 + 1; /* account for leading sign */
350 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
352 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
353 * 0.333334 /* binary -> octal */
355 + 1 /* turn floor into ceil */
356 + 1; /* account for leading sign */
359 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
360 * 0.333334 /* binary -> octal */
362 + 1 /* turn floor into ceil */
363 + 1; /* account for leading sign */
367 # ifdef HAVE_LONG_LONG
368 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
370 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
371 * 0.25 /* binary -> hexadecimal */
373 + 1 /* turn floor into ceil */
374 + 2; /* account for leading sign or alternate form */
377 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
379 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
380 * 0.25 /* binary -> hexadecimal */
382 + 1 /* turn floor into ceil */
383 + 2; /* account for leading sign or alternate form */
386 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
387 * 0.25 /* binary -> hexadecimal */
389 + 1 /* turn floor into ceil */
390 + 2; /* account for leading sign or alternate form */
394 # ifdef HAVE_LONG_DOUBLE
395 if (type == TYPE_LONGDOUBLE)
397 (unsigned int) (LDBL_MAX_EXP
398 * 0.30103 /* binary -> decimal */
399 * 2 /* estimate for FLAG_GROUP */
401 + 1 /* turn floor into ceil */
403 + 10; /* sign, decimal point etc. */
407 (unsigned int) (DBL_MAX_EXP
408 * 0.30103 /* binary -> decimal */
409 * 2 /* estimate for FLAG_GROUP */
411 + 1 /* turn floor into ceil */
413 + 10; /* sign, decimal point etc. */
416 case 'e': case 'E': case 'g': case 'G':
420 + 12; /* sign, decimal point, exponent etc. */
424 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
425 if (type == TYPE_WIDE_CHAR)
426 tmp_length = MB_CUR_MAX;
434 if (type == TYPE_WIDE_STRING)
435 # if WIDE_CHAR_VERSION
437 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
440 local_wcslen (a.arg[dp->arg_index].a.a_wide_string)
445 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
450 (unsigned int) (sizeof (void *) * CHAR_BIT
451 * 0.25 /* binary -> hexadecimal */
453 + 1 /* turn floor into ceil */
454 + 2; /* account for leading 0x */
461 if (tmp_length < width)
464 tmp_length++; /* account for trailing NUL */
467 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
471 tmp = (CHAR_T *) malloc (tmp_length * sizeof (CHAR_T));
475 if (!(result == resultbuf || result == NULL))
485 /* Construct the format string for calling snprintf or
489 if (dp->flags & FLAG_GROUP)
491 if (dp->flags & FLAG_LEFT)
493 if (dp->flags & FLAG_SHOWSIGN)
495 if (dp->flags & FLAG_SPACE)
497 if (dp->flags & FLAG_ALT)
499 if (dp->flags & FLAG_ZERO)
501 if (dp->width_start != dp->width_end)
503 size_t n = dp->width_end - dp->width_start;
504 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
507 if (dp->precision_start != dp->precision_end)
509 size_t n = dp->precision_end - dp->precision_start;
510 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
516 #ifdef HAVE_LONG_LONG
517 case TYPE_LONGLONGINT:
518 case TYPE_ULONGLONGINT:
528 case TYPE_WIDE_STRING:
532 #ifdef HAVE_LONG_DOUBLE
533 case TYPE_LONGDOUBLE:
549 /* Construct the arguments for calling snprintf or sprintf. */
551 if (dp->width_arg_index >= 0)
553 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
555 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
557 if (dp->precision_arg_index >= 0)
559 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
561 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
565 /* Prepare checking whether snprintf returns the count
567 ENSURE_ALLOCATION (length + 1);
568 result[length] = '\0';
577 maxlen = allocated - length;
582 # define SNPRINTF_BUF(arg) \
583 switch (prefix_count) \
586 retcount = SNPRINTF (result + length, maxlen, buf, \
590 retcount = SNPRINTF (result + length, maxlen, buf, \
591 prefixes[0], arg, &count); \
594 retcount = SNPRINTF (result + length, maxlen, buf, \
595 prefixes[0], prefixes[1], arg, \
602 # define SNPRINTF_BUF(arg) \
603 switch (prefix_count) \
606 count = sprintf (tmp, buf, arg); \
609 count = sprintf (tmp, buf, prefixes[0], arg); \
612 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
624 int arg = a.arg[dp->arg_index].a.a_schar;
630 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
636 int arg = a.arg[dp->arg_index].a.a_short;
642 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
648 int arg = a.arg[dp->arg_index].a.a_int;
654 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
660 long int arg = a.arg[dp->arg_index].a.a_longint;
666 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
670 #ifdef HAVE_LONG_LONG
671 case TYPE_LONGLONGINT:
673 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
677 case TYPE_ULONGLONGINT:
679 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
686 double arg = a.arg[dp->arg_index].a.a_double;
690 #ifdef HAVE_LONG_DOUBLE
691 case TYPE_LONGDOUBLE:
693 long double arg = a.arg[dp->arg_index].a.a_longdouble;
700 int arg = a.arg[dp->arg_index].a.a_char;
707 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
714 const char *arg = a.arg[dp->arg_index].a.a_string;
719 case TYPE_WIDE_STRING:
721 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
728 void *arg = a.arg[dp->arg_index].a.a_pointer;
737 /* Portability: Not all implementations of snprintf()
738 are ISO C 99 compliant. Determine the number of
739 bytes that snprintf() has produced or would have
743 /* Verify that snprintf() has NUL-terminated its
745 if (count < maxlen && result[length + count] != '\0')
747 /* Portability hack. */
748 if (retcount > count)
753 /* snprintf() doesn't understand the '%n'
757 /* Don't use the '%n' directive; instead, look
758 at the snprintf() return value. */
764 /* Look at the snprintf() return value. */
767 /* HP-UX 10.20 snprintf() is doubly deficient:
768 It doesn't understand the '%n' directive,
769 *and* it returns -1 (rather than the length
770 that would have been required) when the
771 buffer is too small. */
772 size_t bigger_need = 2 * allocated + 12;
773 ENSURE_ALLOCATION (bigger_need);
782 /* Attempt to handle failure. */
785 if (!(result == resultbuf || result == NULL))
794 if (count >= tmp_length)
795 /* tmp_length was incorrectly calculated - fix the
800 /* Make room for the result. */
803 /* Need at least count bytes. But allocate
804 proportionally, to avoid looping eternally if
805 snprintf() reports a too small count. */
806 size_t n = length + count;
808 if (n < 2 * allocated)
811 ENSURE_ALLOCATION (n);
818 /* The snprintf() result did fit. */
820 /* Append the sprintf() result. */
821 memcpy (result + length, tmp, count * sizeof (CHAR_T));
833 /* Add the final NUL. */
834 ENSURE_ALLOCATION (length + 1);
835 result[length] = '\0';
837 if (result != resultbuf && length + 1 < allocated)
839 /* Shrink the allocated memory if possible. */
842 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));