Update.
[gsasl.git] / gl / vasnprintf.c
blobe11f1e998306151821a900287727e9832fcd55ab
1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2007 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)
7 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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. */
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE 1
23 #endif
25 #include <config.h>
26 #ifndef IN_LIBINTL
27 # include <alloca.h>
28 #endif
30 /* Specification. */
31 #if WIDE_CHAR_VERSION
32 # include "vasnwprintf.h"
33 #else
34 # include "vasnprintf.h"
35 #endif
37 #include <locale.h> /* localeconv() */
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 #if HAVE_NL_LANGINFO
45 # include <langinfo.h>
46 #endif
47 #if WIDE_CHAR_VERSION
48 # include "wprintf-parse.h"
49 #else
50 # include "printf-parse.h"
51 #endif
53 /* Checked size_t computations. */
54 #include "xsize.h"
56 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
57 # include <math.h>
58 # include "isnan.h"
59 # include "printf-frexp.h"
60 # include "isnanl-nolibm.h"
61 # include "printf-frexpl.h"
62 # include "fpucw.h"
63 #endif
65 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
66 #ifndef EOVERFLOW
67 # define EOVERFLOW E2BIG
68 #endif
70 #if HAVE_WCHAR_T
71 # if HAVE_WCSLEN
72 # define local_wcslen wcslen
73 # else
74 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
75 a dependency towards this library, here is a local substitute.
76 Define this substitute only once, even if this file is included
77 twice in the same compilation unit. */
78 # ifndef local_wcslen_defined
79 # define local_wcslen_defined 1
80 static size_t
81 local_wcslen (const wchar_t *s)
83 const wchar_t *ptr;
85 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
87 return ptr - s;
89 # endif
90 # endif
91 #endif
93 #if WIDE_CHAR_VERSION
94 # define VASNPRINTF vasnwprintf
95 # define CHAR_T wchar_t
96 # define DIRECTIVE wchar_t_directive
97 # define DIRECTIVES wchar_t_directives
98 # define PRINTF_PARSE wprintf_parse
99 # define USE_SNPRINTF 1
100 # if HAVE_DECL__SNWPRINTF
101 /* On Windows, the function swprintf() has a different signature than
102 on Unix; we use the _snwprintf() function instead. */
103 # define SNPRINTF _snwprintf
104 # else
105 /* Unix. */
106 # define SNPRINTF swprintf
107 # endif
108 #else
109 # define VASNPRINTF vasnprintf
110 # define CHAR_T char
111 # define DIRECTIVE char_directive
112 # define DIRECTIVES char_directives
113 # define PRINTF_PARSE printf_parse
114 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
115 # if HAVE_DECL__SNPRINTF
116 /* Windows. */
117 # define SNPRINTF _snprintf
118 # else
119 /* Unix. */
120 # define SNPRINTF snprintf
121 /* Here we need to call the native snprintf, not rpl_snprintf. */
122 # undef snprintf
123 # endif
124 #endif
125 /* Here we need to call the native sprintf, not rpl_sprintf. */
126 #undef sprintf
128 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
129 /* Determine the decimal-point character according to the current locale. */
130 # ifndef decimal_point_char_defined
131 # define decimal_point_char_defined 1
132 static char
133 decimal_point_char ()
135 const char *point;
136 /* Determine it in a multithread-safe way. We know nl_langinfo is
137 multithread-safe on glibc systems, but is not required to be multithread-
138 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
139 is rarely multithread-safe. */
140 # if HAVE_NL_LANGINFO && __GLIBC__
141 point = nl_langinfo (RADIXCHAR);
142 # elif 1
143 char pointbuf[5];
144 sprintf (pointbuf, "%#.0f", 1.0);
145 point = &pointbuf[1];
146 # else
147 point = localeconv () -> decimal_point;
148 # endif
149 /* The decimal point is always a single byte: either '.' or ','. */
150 return (point[0] != '\0' ? point[0] : '.');
152 # endif
153 #endif
155 CHAR_T *
156 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
158 DIRECTIVES d;
159 arguments a;
161 if (PRINTF_PARSE (format, &d, &a) < 0)
163 errno = EINVAL;
164 return NULL;
167 #define CLEANUP() \
168 free (d.dir); \
169 if (a.arg) \
170 free (a.arg);
172 if (printf_fetchargs (args, &a) < 0)
174 CLEANUP ();
175 errno = EINVAL;
176 return NULL;
180 size_t buf_neededlength;
181 CHAR_T *buf;
182 CHAR_T *buf_malloced;
183 const CHAR_T *cp;
184 size_t i;
185 DIRECTIVE *dp;
186 /* Output string accumulator. */
187 CHAR_T *result;
188 size_t allocated;
189 size_t length;
191 /* Allocate a small buffer that will hold a directive passed to
192 sprintf or snprintf. */
193 buf_neededlength =
194 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
195 #if HAVE_ALLOCA
196 if (buf_neededlength < 4000 / sizeof (CHAR_T))
198 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
199 buf_malloced = NULL;
201 else
202 #endif
204 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
205 if (size_overflow_p (buf_memsize))
206 goto out_of_memory_1;
207 buf = (CHAR_T *) malloc (buf_memsize);
208 if (buf == NULL)
209 goto out_of_memory_1;
210 buf_malloced = buf;
213 if (resultbuf != NULL)
215 result = resultbuf;
216 allocated = *lengthp;
218 else
220 result = NULL;
221 allocated = 0;
223 length = 0;
224 /* Invariants:
225 result is either == resultbuf or == NULL or malloc-allocated.
226 If length > 0, then result != NULL. */
228 /* Ensures that allocated >= needed. Aborts through a jump to
229 out_of_memory if needed is SIZE_MAX or otherwise too big. */
230 #define ENSURE_ALLOCATION(needed) \
231 if ((needed) > allocated) \
233 size_t memory_size; \
234 CHAR_T *memory; \
236 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
237 if ((needed) > allocated) \
238 allocated = (needed); \
239 memory_size = xtimes (allocated, sizeof (CHAR_T)); \
240 if (size_overflow_p (memory_size)) \
241 goto out_of_memory; \
242 if (result == resultbuf || result == NULL) \
243 memory = (CHAR_T *) malloc (memory_size); \
244 else \
245 memory = (CHAR_T *) realloc (result, memory_size); \
246 if (memory == NULL) \
247 goto out_of_memory; \
248 if (result == resultbuf && length > 0) \
249 memcpy (memory, result, length * sizeof (CHAR_T)); \
250 result = memory; \
253 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
255 if (cp != dp->dir_start)
257 size_t n = dp->dir_start - cp;
258 size_t augmented_length = xsum (length, n);
260 ENSURE_ALLOCATION (augmented_length);
261 memcpy (result + length, cp, n * sizeof (CHAR_T));
262 length = augmented_length;
264 if (i == d.count)
265 break;
267 /* Execute a single directive. */
268 if (dp->conversion == '%')
270 size_t augmented_length;
272 if (!(dp->arg_index == ARG_NONE))
273 abort ();
274 augmented_length = xsum (length, 1);
275 ENSURE_ALLOCATION (augmented_length);
276 result[length] = '%';
277 length = augmented_length;
279 else
281 if (!(dp->arg_index != ARG_NONE))
282 abort ();
284 if (dp->conversion == 'n')
286 switch (a.arg[dp->arg_index].type)
288 case TYPE_COUNT_SCHAR_POINTER:
289 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
290 break;
291 case TYPE_COUNT_SHORT_POINTER:
292 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
293 break;
294 case TYPE_COUNT_INT_POINTER:
295 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
296 break;
297 case TYPE_COUNT_LONGINT_POINTER:
298 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
299 break;
300 #if HAVE_LONG_LONG_INT
301 case TYPE_COUNT_LONGLONGINT_POINTER:
302 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
303 break;
304 #endif
305 default:
306 abort ();
309 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
310 else if (dp->conversion == 'a' || dp->conversion == 'A')
312 arg_type type = a.arg[dp->arg_index].type;
313 int flags = dp->flags;
314 int has_width;
315 size_t width;
316 int has_precision;
317 size_t precision;
318 size_t tmp_length;
319 CHAR_T tmpbuf[700];
320 CHAR_T *tmp;
321 CHAR_T *pad_ptr;
322 CHAR_T *p;
324 has_width = 0;
325 width = 0;
326 if (dp->width_start != dp->width_end)
328 if (dp->width_arg_index != ARG_NONE)
330 int arg;
332 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
333 abort ();
334 arg = a.arg[dp->width_arg_index].a.a_int;
335 if (arg < 0)
337 /* "A negative field width is taken as a '-' flag
338 followed by a positive field width." */
339 flags |= FLAG_LEFT;
340 width = (unsigned int) (-arg);
342 else
343 width = arg;
345 else
347 const CHAR_T *digitp = dp->width_start;
350 width = xsum (xtimes (width, 10), *digitp++ - '0');
351 while (digitp != dp->width_end);
353 has_width = 1;
356 has_precision = 0;
357 precision = 0;
358 if (dp->precision_start != dp->precision_end)
360 if (dp->precision_arg_index != ARG_NONE)
362 int arg;
364 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
365 abort ();
366 arg = a.arg[dp->precision_arg_index].a.a_int;
367 /* "A negative precision is taken as if the precision
368 were omitted." */
369 if (arg >= 0)
371 precision = arg;
372 has_precision = 1;
375 else
377 const CHAR_T *digitp = dp->precision_start + 1;
379 precision = 0;
380 while (digitp != dp->precision_end)
381 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
382 has_precision = 1;
386 /* Allocate a temporary buffer of sufficient size. */
387 if (type == TYPE_LONGDOUBLE)
388 tmp_length =
389 (unsigned int) ((LDBL_DIG + 1)
390 * 0.831 /* decimal -> hexadecimal */
392 + 1; /* turn floor into ceil */
393 else
394 tmp_length =
395 (unsigned int) ((DBL_DIG + 1)
396 * 0.831 /* decimal -> hexadecimal */
398 + 1; /* turn floor into ceil */
399 if (tmp_length < precision)
400 tmp_length = precision;
401 /* Account for sign, decimal point etc. */
402 tmp_length = xsum (tmp_length, 12);
404 if (tmp_length < width)
405 tmp_length = width;
407 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
409 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
410 tmp = tmpbuf;
411 else
413 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
415 if (size_overflow_p (tmp_memsize))
416 /* Overflow, would lead to out of memory. */
417 goto out_of_memory;
418 tmp = (CHAR_T *) malloc (tmp_memsize);
419 if (tmp == NULL)
420 /* Out of memory. */
421 goto out_of_memory;
424 pad_ptr = NULL;
425 p = tmp;
426 if (type == TYPE_LONGDOUBLE)
428 long double arg = a.arg[dp->arg_index].a.a_longdouble;
430 if (isnanl (arg))
432 if (dp->conversion == 'A')
434 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
436 else
438 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
441 else
443 int sign = 0;
444 DECL_LONG_DOUBLE_ROUNDING
446 BEGIN_LONG_DOUBLE_ROUNDING ();
448 if (signbit (arg)) /* arg < 0.0L or negative zero */
450 sign = -1;
451 arg = -arg;
454 if (sign < 0)
455 *p++ = '-';
456 else if (flags & FLAG_SHOWSIGN)
457 *p++ = '+';
458 else if (flags & FLAG_SPACE)
459 *p++ = ' ';
461 if (arg > 0.0L && arg + arg == arg)
463 if (dp->conversion == 'A')
465 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
467 else
469 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
472 else
474 int exponent;
475 long double mantissa;
477 if (arg > 0.0L)
478 mantissa = printf_frexpl (arg, &exponent);
479 else
481 exponent = 0;
482 mantissa = 0.0L;
485 if (has_precision
486 && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
488 /* Round the mantissa. */
489 long double tail = mantissa;
490 size_t q;
492 for (q = precision; ; q--)
494 int digit = (int) tail;
495 tail -= digit;
496 if (q == 0)
498 if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
499 tail = 1 - tail;
500 else
501 tail = - tail;
502 break;
504 tail *= 16.0L;
506 if (tail != 0.0L)
507 for (q = precision; q > 0; q--)
508 tail *= 0.0625L;
509 mantissa += tail;
512 *p++ = '0';
513 *p++ = dp->conversion - 'A' + 'X';
514 pad_ptr = p;
516 int digit;
518 digit = (int) mantissa;
519 mantissa -= digit;
520 *p++ = '0' + digit;
521 if ((flags & FLAG_ALT)
522 || mantissa > 0.0L || precision > 0)
524 *p++ = decimal_point_char ();
525 /* This loop terminates because we assume
526 that FLT_RADIX is a power of 2. */
527 while (mantissa > 0.0L)
529 mantissa *= 16.0L;
530 digit = (int) mantissa;
531 mantissa -= digit;
532 *p++ = digit
533 + (digit < 10
534 ? '0'
535 : dp->conversion - 10);
536 if (precision > 0)
537 precision--;
539 while (precision > 0)
541 *p++ = '0';
542 precision--;
546 *p++ = dp->conversion - 'A' + 'P';
547 # if WIDE_CHAR_VERSION
549 static const wchar_t decimal_format[] =
550 { '%', '+', 'd', '\0' };
551 SNPRINTF (p, 6 + 1, decimal_format, exponent);
553 # else
554 sprintf (p, "%+d", exponent);
555 # endif
556 while (*p != '\0')
557 p++;
560 END_LONG_DOUBLE_ROUNDING ();
563 else
565 double arg = a.arg[dp->arg_index].a.a_double;
567 if (isnan (arg))
569 if (dp->conversion == 'A')
571 *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
573 else
575 *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
578 else
580 int sign = 0;
582 if (signbit (arg)) /* arg < 0.0 or negative zero */
584 sign = -1;
585 arg = -arg;
588 if (sign < 0)
589 *p++ = '-';
590 else if (flags & FLAG_SHOWSIGN)
591 *p++ = '+';
592 else if (flags & FLAG_SPACE)
593 *p++ = ' ';
595 if (arg > 0.0 && arg + arg == arg)
597 if (dp->conversion == 'A')
599 *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
601 else
603 *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
606 else
608 int exponent;
609 double mantissa;
611 if (arg > 0.0)
612 mantissa = printf_frexp (arg, &exponent);
613 else
615 exponent = 0;
616 mantissa = 0.0;
619 if (has_precision
620 && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
622 /* Round the mantissa. */
623 double tail = mantissa;
624 size_t q;
626 for (q = precision; ; q--)
628 int digit = (int) tail;
629 tail -= digit;
630 if (q == 0)
632 if (digit & 1 ? tail >= 0.5 : tail > 0.5)
633 tail = 1 - tail;
634 else
635 tail = - tail;
636 break;
638 tail *= 16.0;
640 if (tail != 0.0)
641 for (q = precision; q > 0; q--)
642 tail *= 0.0625;
643 mantissa += tail;
646 *p++ = '0';
647 *p++ = dp->conversion - 'A' + 'X';
648 pad_ptr = p;
650 int digit;
652 digit = (int) mantissa;
653 mantissa -= digit;
654 *p++ = '0' + digit;
655 if ((flags & FLAG_ALT)
656 || mantissa > 0.0 || precision > 0)
658 *p++ = decimal_point_char ();
659 /* This loop terminates because we assume
660 that FLT_RADIX is a power of 2. */
661 while (mantissa > 0.0)
663 mantissa *= 16.0;
664 digit = (int) mantissa;
665 mantissa -= digit;
666 *p++ = digit
667 + (digit < 10
668 ? '0'
669 : dp->conversion - 10);
670 if (precision > 0)
671 precision--;
673 while (precision > 0)
675 *p++ = '0';
676 precision--;
680 *p++ = dp->conversion - 'A' + 'P';
681 # if WIDE_CHAR_VERSION
683 static const wchar_t decimal_format[] =
684 { '%', '+', 'd', '\0' };
685 SNPRINTF (p, 6 + 1, decimal_format, exponent);
687 # else
688 sprintf (p, "%+d", exponent);
689 # endif
690 while (*p != '\0')
691 p++;
695 /* The generated string now extends from tmp to p, with the
696 zero padding insertion point being at pad_ptr. */
697 if (has_width && p - tmp < width)
699 size_t pad = width - (p - tmp);
700 CHAR_T *end = p + pad;
702 if (flags & FLAG_LEFT)
704 /* Pad with spaces on the right. */
705 for (; pad > 0; pad--)
706 *p++ = ' ';
708 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
710 /* Pad with zeroes. */
711 CHAR_T *q = end;
713 while (p > pad_ptr)
714 *--q = *--p;
715 for (; pad > 0; pad--)
716 *p++ = '0';
718 else
720 /* Pad with spaces on the left. */
721 CHAR_T *q = end;
723 while (p > tmp)
724 *--q = *--p;
725 for (; pad > 0; pad--)
726 *p++ = ' ';
729 p = end;
733 size_t count = p - tmp;
735 if (count >= tmp_length)
736 /* tmp_length was incorrectly calculated - fix the
737 code above! */
738 abort ();
740 /* Make room for the result. */
741 if (count >= allocated - length)
743 size_t n = xsum (length, count);
745 ENSURE_ALLOCATION (n);
748 /* Append the result. */
749 memcpy (result + length, tmp, count * sizeof (CHAR_T));
750 if (tmp != tmpbuf)
751 free (tmp);
752 length += count;
755 #endif
756 else
758 arg_type type = a.arg[dp->arg_index].type;
759 CHAR_T *p;
760 unsigned int prefix_count;
761 int prefixes[2];
762 #if !USE_SNPRINTF
763 size_t tmp_length;
764 CHAR_T tmpbuf[700];
765 CHAR_T *tmp;
767 /* Allocate a temporary buffer of sufficient size for calling
768 sprintf. */
770 size_t width;
771 size_t precision;
773 width = 0;
774 if (dp->width_start != dp->width_end)
776 if (dp->width_arg_index != ARG_NONE)
778 int arg;
780 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
781 abort ();
782 arg = a.arg[dp->width_arg_index].a.a_int;
783 width = (arg < 0 ? (unsigned int) (-arg) : arg);
785 else
787 const CHAR_T *digitp = dp->width_start;
790 width = xsum (xtimes (width, 10), *digitp++ - '0');
791 while (digitp != dp->width_end);
795 precision = 6;
796 if (dp->precision_start != dp->precision_end)
798 if (dp->precision_arg_index != ARG_NONE)
800 int arg;
802 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
803 abort ();
804 arg = a.arg[dp->precision_arg_index].a.a_int;
805 precision = (arg < 0 ? 0 : arg);
807 else
809 const CHAR_T *digitp = dp->precision_start + 1;
811 precision = 0;
812 while (digitp != dp->precision_end)
813 precision = xsum (xtimes (precision, 10), *digitp++ - '0');
817 switch (dp->conversion)
820 case 'd': case 'i': case 'u':
821 # if HAVE_LONG_LONG_INT
822 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
823 tmp_length =
824 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
825 * 0.30103 /* binary -> decimal */
827 + 1; /* turn floor into ceil */
828 else
829 # endif
830 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
831 tmp_length =
832 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
833 * 0.30103 /* binary -> decimal */
835 + 1; /* turn floor into ceil */
836 else
837 tmp_length =
838 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
839 * 0.30103 /* binary -> decimal */
841 + 1; /* turn floor into ceil */
842 if (tmp_length < precision)
843 tmp_length = precision;
844 /* Multiply by 2, as an estimate for FLAG_GROUP. */
845 tmp_length = xsum (tmp_length, tmp_length);
846 /* Add 1, to account for a leading sign. */
847 tmp_length = xsum (tmp_length, 1);
848 break;
850 case 'o':
851 # if HAVE_LONG_LONG_INT
852 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
853 tmp_length =
854 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
855 * 0.333334 /* binary -> octal */
857 + 1; /* turn floor into ceil */
858 else
859 # endif
860 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
861 tmp_length =
862 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
863 * 0.333334 /* binary -> octal */
865 + 1; /* turn floor into ceil */
866 else
867 tmp_length =
868 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
869 * 0.333334 /* binary -> octal */
871 + 1; /* turn floor into ceil */
872 if (tmp_length < precision)
873 tmp_length = precision;
874 /* Add 1, to account for a leading sign. */
875 tmp_length = xsum (tmp_length, 1);
876 break;
878 case 'x': case 'X':
879 # if HAVE_LONG_LONG_INT
880 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
881 tmp_length =
882 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
883 * 0.25 /* binary -> hexadecimal */
885 + 1; /* turn floor into ceil */
886 else
887 # endif
888 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
889 tmp_length =
890 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
891 * 0.25 /* binary -> hexadecimal */
893 + 1; /* turn floor into ceil */
894 else
895 tmp_length =
896 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
897 * 0.25 /* binary -> hexadecimal */
899 + 1; /* turn floor into ceil */
900 if (tmp_length < precision)
901 tmp_length = precision;
902 /* Add 2, to account for a leading sign or alternate form. */
903 tmp_length = xsum (tmp_length, 2);
904 break;
906 case 'f': case 'F':
907 if (type == TYPE_LONGDOUBLE)
908 tmp_length =
909 (unsigned int) (LDBL_MAX_EXP
910 * 0.30103 /* binary -> decimal */
911 * 2 /* estimate for FLAG_GROUP */
913 + 1 /* turn floor into ceil */
914 + 10; /* sign, decimal point etc. */
915 else
916 tmp_length =
917 (unsigned int) (DBL_MAX_EXP
918 * 0.30103 /* binary -> decimal */
919 * 2 /* estimate for FLAG_GROUP */
921 + 1 /* turn floor into ceil */
922 + 10; /* sign, decimal point etc. */
923 tmp_length = xsum (tmp_length, precision);
924 break;
926 case 'e': case 'E': case 'g': case 'G':
927 tmp_length =
928 12; /* sign, decimal point, exponent etc. */
929 tmp_length = xsum (tmp_length, precision);
930 break;
932 case 'a': case 'A':
933 if (type == TYPE_LONGDOUBLE)
934 tmp_length =
935 (unsigned int) (LDBL_DIG
936 * 0.831 /* decimal -> hexadecimal */
938 + 1; /* turn floor into ceil */
939 else
940 tmp_length =
941 (unsigned int) (DBL_DIG
942 * 0.831 /* decimal -> hexadecimal */
944 + 1; /* turn floor into ceil */
945 if (tmp_length < precision)
946 tmp_length = precision;
947 /* Account for sign, decimal point etc. */
948 tmp_length = xsum (tmp_length, 12);
949 break;
951 case 'c':
952 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
953 if (type == TYPE_WIDE_CHAR)
954 tmp_length = MB_CUR_MAX;
955 else
956 # endif
957 tmp_length = 1;
958 break;
960 case 's':
961 # if HAVE_WCHAR_T
962 if (type == TYPE_WIDE_STRING)
964 tmp_length =
965 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
967 # if !WIDE_CHAR_VERSION
968 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
969 # endif
971 else
972 # endif
973 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
974 break;
976 case 'p':
977 tmp_length =
978 (unsigned int) (sizeof (void *) * CHAR_BIT
979 * 0.25 /* binary -> hexadecimal */
981 + 1 /* turn floor into ceil */
982 + 2; /* account for leading 0x */
983 break;
985 default:
986 abort ();
989 if (tmp_length < width)
990 tmp_length = width;
992 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
995 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
996 tmp = tmpbuf;
997 else
999 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
1001 if (size_overflow_p (tmp_memsize))
1002 /* Overflow, would lead to out of memory. */
1003 goto out_of_memory;
1004 tmp = (CHAR_T *) malloc (tmp_memsize);
1005 if (tmp == NULL)
1006 /* Out of memory. */
1007 goto out_of_memory;
1009 #endif
1011 /* Construct the format string for calling snprintf or
1012 sprintf. */
1013 p = buf;
1014 *p++ = '%';
1015 if (dp->flags & FLAG_GROUP)
1016 *p++ = '\'';
1017 if (dp->flags & FLAG_LEFT)
1018 *p++ = '-';
1019 if (dp->flags & FLAG_SHOWSIGN)
1020 *p++ = '+';
1021 if (dp->flags & FLAG_SPACE)
1022 *p++ = ' ';
1023 if (dp->flags & FLAG_ALT)
1024 *p++ = '#';
1025 if (dp->flags & FLAG_ZERO)
1026 *p++ = '0';
1027 if (dp->width_start != dp->width_end)
1029 size_t n = dp->width_end - dp->width_start;
1030 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
1031 p += n;
1033 if (dp->precision_start != dp->precision_end)
1035 size_t n = dp->precision_end - dp->precision_start;
1036 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
1037 p += n;
1040 switch (type)
1042 #if HAVE_LONG_LONG_INT
1043 case TYPE_LONGLONGINT:
1044 case TYPE_ULONGLONGINT:
1045 *p++ = 'l';
1046 /*FALLTHROUGH*/
1047 #endif
1048 case TYPE_LONGINT:
1049 case TYPE_ULONGINT:
1050 #if HAVE_WINT_T
1051 case TYPE_WIDE_CHAR:
1052 #endif
1053 #if HAVE_WCHAR_T
1054 case TYPE_WIDE_STRING:
1055 #endif
1056 *p++ = 'l';
1057 break;
1058 case TYPE_LONGDOUBLE:
1059 *p++ = 'L';
1060 break;
1061 default:
1062 break;
1064 #if NEED_PRINTF_DIRECTIVE_F
1065 if (dp->conversion == 'F')
1066 *p = 'f';
1067 else
1068 #endif
1069 *p = dp->conversion;
1070 #if USE_SNPRINTF
1071 p[1] = '%';
1072 p[2] = 'n';
1073 p[3] = '\0';
1074 #else
1075 p[1] = '\0';
1076 #endif
1078 /* Construct the arguments for calling snprintf or sprintf. */
1079 prefix_count = 0;
1080 if (dp->width_arg_index != ARG_NONE)
1082 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1083 abort ();
1084 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
1086 if (dp->precision_arg_index != ARG_NONE)
1088 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1089 abort ();
1090 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
1093 #if USE_SNPRINTF
1094 /* Prepare checking whether snprintf returns the count
1095 via %n. */
1096 ENSURE_ALLOCATION (xsum (length, 1));
1097 result[length] = '\0';
1098 #endif
1100 for (;;)
1102 size_t maxlen;
1103 int count;
1104 int retcount;
1106 maxlen = allocated - length;
1107 count = -1;
1108 retcount = 0;
1110 #if USE_SNPRINTF
1111 /* SNPRINTF can fail if maxlen > INT_MAX. */
1112 if (maxlen > INT_MAX)
1113 goto overflow;
1114 # define SNPRINTF_BUF(arg) \
1115 switch (prefix_count) \
1117 case 0: \
1118 retcount = SNPRINTF (result + length, maxlen, buf, \
1119 arg, &count); \
1120 break; \
1121 case 1: \
1122 retcount = SNPRINTF (result + length, maxlen, buf, \
1123 prefixes[0], arg, &count); \
1124 break; \
1125 case 2: \
1126 retcount = SNPRINTF (result + length, maxlen, buf, \
1127 prefixes[0], prefixes[1], arg, \
1128 &count); \
1129 break; \
1130 default: \
1131 abort (); \
1133 #else
1134 # define SNPRINTF_BUF(arg) \
1135 switch (prefix_count) \
1137 case 0: \
1138 count = sprintf (tmp, buf, arg); \
1139 break; \
1140 case 1: \
1141 count = sprintf (tmp, buf, prefixes[0], arg); \
1142 break; \
1143 case 2: \
1144 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
1145 arg); \
1146 break; \
1147 default: \
1148 abort (); \
1150 #endif
1152 switch (type)
1154 case TYPE_SCHAR:
1156 int arg = a.arg[dp->arg_index].a.a_schar;
1157 SNPRINTF_BUF (arg);
1159 break;
1160 case TYPE_UCHAR:
1162 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
1163 SNPRINTF_BUF (arg);
1165 break;
1166 case TYPE_SHORT:
1168 int arg = a.arg[dp->arg_index].a.a_short;
1169 SNPRINTF_BUF (arg);
1171 break;
1172 case TYPE_USHORT:
1174 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
1175 SNPRINTF_BUF (arg);
1177 break;
1178 case TYPE_INT:
1180 int arg = a.arg[dp->arg_index].a.a_int;
1181 SNPRINTF_BUF (arg);
1183 break;
1184 case TYPE_UINT:
1186 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
1187 SNPRINTF_BUF (arg);
1189 break;
1190 case TYPE_LONGINT:
1192 long int arg = a.arg[dp->arg_index].a.a_longint;
1193 SNPRINTF_BUF (arg);
1195 break;
1196 case TYPE_ULONGINT:
1198 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
1199 SNPRINTF_BUF (arg);
1201 break;
1202 #if HAVE_LONG_LONG_INT
1203 case TYPE_LONGLONGINT:
1205 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
1206 SNPRINTF_BUF (arg);
1208 break;
1209 case TYPE_ULONGLONGINT:
1211 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
1212 SNPRINTF_BUF (arg);
1214 break;
1215 #endif
1216 case TYPE_DOUBLE:
1218 double arg = a.arg[dp->arg_index].a.a_double;
1219 SNPRINTF_BUF (arg);
1221 break;
1222 case TYPE_LONGDOUBLE:
1224 long double arg = a.arg[dp->arg_index].a.a_longdouble;
1225 SNPRINTF_BUF (arg);
1227 break;
1228 case TYPE_CHAR:
1230 int arg = a.arg[dp->arg_index].a.a_char;
1231 SNPRINTF_BUF (arg);
1233 break;
1234 #if HAVE_WINT_T
1235 case TYPE_WIDE_CHAR:
1237 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
1238 SNPRINTF_BUF (arg);
1240 break;
1241 #endif
1242 case TYPE_STRING:
1244 const char *arg = a.arg[dp->arg_index].a.a_string;
1245 SNPRINTF_BUF (arg);
1247 break;
1248 #if HAVE_WCHAR_T
1249 case TYPE_WIDE_STRING:
1251 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
1252 SNPRINTF_BUF (arg);
1254 break;
1255 #endif
1256 case TYPE_POINTER:
1258 void *arg = a.arg[dp->arg_index].a.a_pointer;
1259 SNPRINTF_BUF (arg);
1261 break;
1262 default:
1263 abort ();
1266 #if USE_SNPRINTF
1267 /* Portability: Not all implementations of snprintf()
1268 are ISO C 99 compliant. Determine the number of
1269 bytes that snprintf() has produced or would have
1270 produced. */
1271 if (count >= 0)
1273 /* Verify that snprintf() has NUL-terminated its
1274 result. */
1275 if (count < maxlen && result[length + count] != '\0')
1276 abort ();
1277 /* Portability hack. */
1278 if (retcount > count)
1279 count = retcount;
1281 else
1283 /* snprintf() doesn't understand the '%n'
1284 directive. */
1285 if (p[1] != '\0')
1287 /* Don't use the '%n' directive; instead, look
1288 at the snprintf() return value. */
1289 p[1] = '\0';
1290 continue;
1292 else
1294 /* Look at the snprintf() return value. */
1295 if (retcount < 0)
1297 /* HP-UX 10.20 snprintf() is doubly deficient:
1298 It doesn't understand the '%n' directive,
1299 *and* it returns -1 (rather than the length
1300 that would have been required) when the
1301 buffer is too small. */
1302 size_t bigger_need =
1303 xsum (xtimes (allocated, 2), 12);
1304 ENSURE_ALLOCATION (bigger_need);
1305 continue;
1307 else
1308 count = retcount;
1311 #endif
1313 /* Attempt to handle failure. */
1314 if (count < 0)
1316 if (!(result == resultbuf || result == NULL))
1317 free (result);
1318 if (buf_malloced != NULL)
1319 free (buf_malloced);
1320 CLEANUP ();
1321 errno = EINVAL;
1322 return NULL;
1325 #if !USE_SNPRINTF
1326 if (count >= tmp_length)
1327 /* tmp_length was incorrectly calculated - fix the
1328 code above! */
1329 abort ();
1330 #endif
1332 /* Make room for the result. */
1333 if (count >= maxlen)
1335 /* Need at least count bytes. But allocate
1336 proportionally, to avoid looping eternally if
1337 snprintf() reports a too small count. */
1338 size_t n =
1339 xmax (xsum (length, count), xtimes (allocated, 2));
1341 ENSURE_ALLOCATION (n);
1342 #if USE_SNPRINTF
1343 continue;
1344 #endif
1347 #if USE_SNPRINTF
1348 /* The snprintf() result did fit. */
1349 #else
1350 /* Append the sprintf() result. */
1351 memcpy (result + length, tmp, count * sizeof (CHAR_T));
1352 if (tmp != tmpbuf)
1353 free (tmp);
1354 #endif
1356 #if NEED_PRINTF_DIRECTIVE_F
1357 if (dp->conversion == 'F')
1359 /* Convert the %f result to upper case for %F. */
1360 CHAR_T *rp = result + length;
1361 size_t rc;
1362 for (rc = count; rc > 0; rc--, rp++)
1363 if (*rp >= 'a' && *rp <= 'z')
1364 *rp = *rp - 'a' + 'A';
1366 #endif
1368 length += count;
1369 break;
1375 /* Add the final NUL. */
1376 ENSURE_ALLOCATION (xsum (length, 1));
1377 result[length] = '\0';
1379 if (result != resultbuf && length + 1 < allocated)
1381 /* Shrink the allocated memory if possible. */
1382 CHAR_T *memory;
1384 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
1385 if (memory != NULL)
1386 result = memory;
1389 if (buf_malloced != NULL)
1390 free (buf_malloced);
1391 CLEANUP ();
1392 *lengthp = length;
1393 /* Note that we can produce a big string of a length > INT_MAX. POSIX
1394 says that snprintf() fails with errno = EOVERFLOW in this case, but
1395 that's only because snprintf() returns an 'int'. This function does
1396 not have this limitation. */
1397 return result;
1399 overflow:
1400 if (!(result == resultbuf || result == NULL))
1401 free (result);
1402 if (buf_malloced != NULL)
1403 free (buf_malloced);
1404 CLEANUP ();
1405 errno = EOVERFLOW;
1406 return NULL;
1408 out_of_memory:
1409 if (!(result == resultbuf || result == NULL))
1410 free (result);
1411 if (buf_malloced != NULL)
1412 free (buf_malloced);
1413 out_of_memory_1:
1414 CLEANUP ();
1415 errno = ENOMEM;
1416 return NULL;
1420 #undef SNPRINTF
1421 #undef USE_SNPRINTF
1422 #undef PRINTF_PARSE
1423 #undef DIRECTIVES
1424 #undef DIRECTIVE
1425 #undef CHAR_T
1426 #undef VASNPRINTF