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
32 #include "vasnprintf.h"
34 #include <stdio.h> /* snprintf(), sprintf() */
35 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
36 #include <string.h> /* memcpy(), strlen() */
37 #include <errno.h> /* errno */
38 #include <limits.h> /* CHAR_BIT */
39 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
40 #include "printf-parse.h"
42 /* For those losing systems which don't have 'alloca' we have to add
43 some additional code emulating it. */
45 # define freea(p) /* nothing */
47 # define alloca(n) malloc (n)
48 # define freea(p) free (p)
52 vasnprintf (char *resultbuf
, size_t *lengthp
, const char *format
, va_list args
)
57 if (printf_parse (format
, &d
, &a
) < 0)
68 if (printf_fetchargs (args
, &a
) < 0)
77 (char *) alloca (7 + d
.max_width_length
+ d
.max_precision_length
+ 6);
81 /* Output string accumulator. */
86 if (resultbuf
!= NULL
)
98 result is either == resultbuf or == NULL or malloc-allocated.
99 If length > 0, then result != NULL. */
101 #define ENSURE_ALLOCATION(needed) \
102 if ((needed) > allocated) \
106 allocated = (allocated > 0 ? 2 * allocated : 12); \
107 if ((needed) > allocated) \
108 allocated = (needed); \
109 if (result == resultbuf || result == NULL) \
110 memory = (char *) malloc (allocated); \
112 memory = (char *) realloc (result, allocated); \
114 if (memory == NULL) \
116 if (!(result == resultbuf || result == NULL)) \
123 if (result == resultbuf && length > 0) \
124 memcpy (memory, result, length); \
128 for (cp
= format
, i
= 0, dp
= &d
.dir
[0]; ; cp
= dp
->dir_end
, i
++, dp
++)
130 if (cp
!= dp
->dir_start
)
132 size_t n
= dp
->dir_start
- cp
;
134 ENSURE_ALLOCATION (length
+ n
);
135 memcpy (result
+ length
, cp
, n
);
141 /* Execute a single directive. */
142 if (dp
->conversion
== '%')
144 if (!(dp
->arg_index
< 0))
146 ENSURE_ALLOCATION (length
+ 1);
147 result
[length
] = '%';
152 if (!(dp
->arg_index
>= 0))
155 if (dp
->conversion
== 'n')
157 switch (a
.arg
[dp
->arg_index
].type
)
159 case TYPE_COUNT_SCHAR_POINTER
:
160 *a
.arg
[dp
->arg_index
].a
.a_count_schar_pointer
= length
;
162 case TYPE_COUNT_SHORT_POINTER
:
163 *a
.arg
[dp
->arg_index
].a
.a_count_short_pointer
= length
;
165 case TYPE_COUNT_INT_POINTER
:
166 *a
.arg
[dp
->arg_index
].a
.a_count_int_pointer
= length
;
168 case TYPE_COUNT_LONGINT_POINTER
:
169 *a
.arg
[dp
->arg_index
].a
.a_count_longint_pointer
= length
;
171 #ifdef HAVE_LONG_LONG
172 case TYPE_COUNT_LONGLONGINT_POINTER
:
173 *a
.arg
[dp
->arg_index
].a
.a_count_longlongint_pointer
= length
;
182 arg_type type
= a
.arg
[dp
->arg_index
].type
;
184 unsigned int prefix_count
;
187 unsigned int tmp_length
;
191 /* Allocate a temporary buffer of sufficient size for calling
195 unsigned int precision
;
198 if (dp
->width_start
!= dp
->width_end
)
200 if (dp
->width_arg_index
>= 0)
204 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
206 arg
= a
.arg
[dp
->width_arg_index
].a
.a_int
;
207 width
= (arg
< 0 ? -arg
: arg
);
211 const char *digitp
= dp
->width_start
;
214 width
= width
* 10 + (*digitp
++ - '0');
215 while (digitp
!= dp
->width_end
);
220 if (dp
->precision_start
!= dp
->precision_end
)
222 if (dp
->precision_arg_index
>= 0)
226 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
228 arg
= a
.arg
[dp
->precision_arg_index
].a
.a_int
;
229 precision
= (arg
< 0 ? 0 : arg
);
233 const char *digitp
= dp
->precision_start
+ 1;
237 precision
= precision
* 10 + (*digitp
++ - '0');
238 while (digitp
!= dp
->precision_end
);
242 switch (dp
->conversion
)
245 case 'd': case 'i': case 'u':
246 # ifdef HAVE_LONG_LONG
247 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
249 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
250 * 0.30103 /* binary -> decimal */
251 * 2 /* estimate for FLAG_GROUP */
253 + 1 /* turn floor into ceil */
254 + 1; /* account for leading sign */
257 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
259 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
260 * 0.30103 /* binary -> decimal */
261 * 2 /* estimate for FLAG_GROUP */
263 + 1 /* turn floor into ceil */
264 + 1; /* account for leading sign */
267 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
268 * 0.30103 /* binary -> decimal */
269 * 2 /* estimate for FLAG_GROUP */
271 + 1 /* turn floor into ceil */
272 + 1; /* account for leading sign */
276 # ifdef HAVE_LONG_LONG
277 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
279 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
280 * 0.333334 /* binary -> octal */
282 + 1 /* turn floor into ceil */
283 + 1; /* account for leading sign */
286 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
288 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
289 * 0.333334 /* binary -> octal */
291 + 1 /* turn floor into ceil */
292 + 1; /* account for leading sign */
295 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
296 * 0.333334 /* binary -> octal */
298 + 1 /* turn floor into ceil */
299 + 1; /* account for leading sign */
303 # ifdef HAVE_LONG_LONG
304 if (type
== TYPE_LONGLONGINT
|| type
== TYPE_ULONGLONGINT
)
306 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
307 * 0.25 /* binary -> hexadecimal */
309 + 1 /* turn floor into ceil */
310 + 2; /* account for leading sign or alternate form */
313 if (type
== TYPE_LONGINT
|| type
== TYPE_ULONGINT
)
315 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
316 * 0.25 /* binary -> hexadecimal */
318 + 1 /* turn floor into ceil */
319 + 2; /* account for leading sign or alternate form */
322 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
323 * 0.25 /* binary -> hexadecimal */
325 + 1 /* turn floor into ceil */
326 + 2; /* account for leading sign or alternate form */
330 # ifdef HAVE_LONG_DOUBLE
331 if (type
== TYPE_LONGDOUBLE
)
333 (unsigned int) (LDBL_MAX_EXP
334 * 0.30103 /* binary -> decimal */
335 * 2 /* estimate for FLAG_GROUP */
337 + 1 /* turn floor into ceil */
339 + 10; /* sign, decimal point etc. */
343 (unsigned int) (DBL_MAX_EXP
344 * 0.30103 /* binary -> decimal */
345 * 2 /* estimate for FLAG_GROUP */
347 + 1 /* turn floor into ceil */
349 + 10; /* sign, decimal point etc. */
352 case 'e': case 'E': case 'g': case 'G':
356 + 12; /* sign, decimal point, exponent etc. */
361 if (type
== TYPE_WIDE_CHAR
)
362 tmp_length
= MB_CUR_MAX
;
370 if (type
== TYPE_WIDE_STRING
)
372 wcslen (a
.arg
[dp
->arg_index
].a
.a_wide_string
)
376 tmp_length
= strlen (a
.arg
[dp
->arg_index
].a
.a_string
);
381 (unsigned int) (sizeof (void *) * CHAR_BIT
382 * 0.25 /* binary -> hexadecimal */
384 + 1 /* turn floor into ceil */
385 + 2; /* account for leading 0x */
392 if (tmp_length
< width
)
395 tmp_length
++; /* account for trailing NUL */
398 if (tmp_length
<= sizeof (tmpbuf
))
402 tmp
= (char *) malloc (tmp_length
);
406 if (!(result
== resultbuf
|| result
== NULL
))
416 /* Construct the format string for calling snprintf or
420 if (dp
->flags
& FLAG_GROUP
)
422 if (dp
->flags
& FLAG_LEFT
)
424 if (dp
->flags
& FLAG_SHOWSIGN
)
426 if (dp
->flags
& FLAG_SPACE
)
428 if (dp
->flags
& FLAG_ALT
)
430 if (dp
->flags
& FLAG_ZERO
)
432 if (dp
->width_start
!= dp
->width_end
)
434 size_t n
= dp
->width_end
- dp
->width_start
;
435 memcpy (p
, dp
->width_start
, n
);
438 if (dp
->precision_start
!= dp
->precision_end
)
440 size_t n
= dp
->precision_end
- dp
->precision_start
;
441 memcpy (p
, dp
->precision_start
, n
);
447 #ifdef HAVE_LONG_LONG
448 case TYPE_LONGLONGINT
:
449 case TYPE_ULONGLONGINT
:
459 case TYPE_WIDE_STRING
:
463 #ifdef HAVE_LONG_DOUBLE
464 case TYPE_LONGDOUBLE
:
480 /* Construct the arguments for calling snprintf or sprintf. */
482 if (dp
->width_arg_index
>= 0)
484 if (!(a
.arg
[dp
->width_arg_index
].type
== TYPE_INT
))
486 prefixes
[prefix_count
++] = a
.arg
[dp
->width_arg_index
].a
.a_int
;
488 if (dp
->precision_arg_index
>= 0)
490 if (!(a
.arg
[dp
->precision_arg_index
].type
== TYPE_INT
))
492 prefixes
[prefix_count
++] = a
.arg
[dp
->precision_arg_index
].a
.a_int
;
496 /* Prepare checking whether snprintf returns the count
498 ENSURE_ALLOCATION (length
+ 1);
499 result
[length
] = '\0';
508 maxlen
= allocated
- length
;
513 #define SNPRINTF_BUF(arg) \
514 switch (prefix_count) \
517 retcount = snprintf (result + length, maxlen, buf, \
521 retcount = snprintf (result + length, maxlen, buf, \
522 prefixes[0], arg, &count); \
525 retcount = snprintf (result + length, maxlen, buf, \
526 prefixes[0], prefixes[1], arg, \
533 #define SNPRINTF_BUF(arg) \
534 switch (prefix_count) \
537 count = sprintf (tmp, buf, arg); \
540 count = sprintf (tmp, buf, prefixes[0], arg); \
543 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
555 int arg
= a
.arg
[dp
->arg_index
].a
.a_schar
;
561 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uchar
;
567 int arg
= a
.arg
[dp
->arg_index
].a
.a_short
;
573 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_ushort
;
579 int arg
= a
.arg
[dp
->arg_index
].a
.a_int
;
585 unsigned int arg
= a
.arg
[dp
->arg_index
].a
.a_uint
;
591 long int arg
= a
.arg
[dp
->arg_index
].a
.a_longint
;
597 unsigned long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulongint
;
601 #ifdef HAVE_LONG_LONG
602 case TYPE_LONGLONGINT
:
604 long long int arg
= a
.arg
[dp
->arg_index
].a
.a_longlongint
;
608 case TYPE_ULONGLONGINT
:
610 unsigned long long int arg
= a
.arg
[dp
->arg_index
].a
.a_ulonglongint
;
617 double arg
= a
.arg
[dp
->arg_index
].a
.a_double
;
621 #ifdef HAVE_LONG_DOUBLE
622 case TYPE_LONGDOUBLE
:
624 long double arg
= a
.arg
[dp
->arg_index
].a
.a_longdouble
;
631 int arg
= a
.arg
[dp
->arg_index
].a
.a_char
;
638 wint_t arg
= a
.arg
[dp
->arg_index
].a
.a_wide_char
;
645 const char *arg
= a
.arg
[dp
->arg_index
].a
.a_string
;
650 case TYPE_WIDE_STRING
:
652 const wchar_t *arg
= a
.arg
[dp
->arg_index
].a
.a_wide_string
;
659 void *arg
= a
.arg
[dp
->arg_index
].a
.a_pointer
;
668 /* Portability: Not all implementations of snprintf()
669 are ISO C 99 compliant. Determine the number of
670 bytes that snprintf() has produced or would have
674 /* Verify that snprintf() has NUL-terminated its
676 if (count
< maxlen
&& result
[length
+ count
] != '\0')
678 /* Portability hack. */
679 if (retcount
> count
)
684 /* snprintf() doesn't understand the '%n'
688 /* Don't use the '%n' directive; instead, look
689 at the snprintf() return value. */
697 /* Attempt to handle failure. */
700 if (!(result
== resultbuf
|| result
== NULL
))
709 if (count
>= tmp_length
)
710 /* tmp_length was incorrectly calculated - fix the
715 /* Make room for the result. */
718 /* Need at least count bytes. But allocate
719 proportionally, to avoid looping eternally if
720 snprintf() reports a too small count. */
721 size_t n
= length
+ count
;
723 if (n
< 2 * allocated
)
726 ENSURE_ALLOCATION (n
);
733 /* The snprintf() result did fit. */
735 /* Append the sprintf() result. */
736 memcpy (result
+ length
, tmp
, count
);
748 /* Add the final NUL. */
749 ENSURE_ALLOCATION (length
+ 1);
750 result
[length
] = '\0';
752 if (result
!= resultbuf
&& length
+ 1 < allocated
)
754 /* Shrink the allocated memory if possible. */
757 memory
= (char *) realloc (result
, length
+ 1);