Remove suppression-related options.
[coreutils.git] / lib / vasnprintf.c
blobd49bc559e9919751cf6f080d7b852cc6c92175de
1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2004 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., 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. */
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE 1
23 #endif
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 #ifndef IN_LIBINTL
29 # include <alloca.h>
30 #endif
32 /* Specification. */
33 #if WIDE_CHAR_VERSION
34 # include "vasnwprintf.h"
35 #else
36 # include "vasnprintf.h"
37 #endif
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, INT_MAX */
44 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
45 #if WIDE_CHAR_VERSION
46 # include "wprintf-parse.h"
47 #else
48 # include "printf-parse.h"
49 #endif
51 #ifndef SIZE_MAX
52 # define SIZE_MAX ((size_t) -1)
53 #endif
55 #ifdef HAVE_WCHAR_T
56 # ifdef HAVE_WCSLEN
57 # define local_wcslen wcslen
58 # else
59 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
60 a dependency towards this library, here is a local substitute.
61 Define this substitute only once, even if this file is included
62 twice in the same compilation unit. */
63 # ifndef local_wcslen_defined
64 # define local_wcslen_defined 1
65 static size_t
66 local_wcslen (const wchar_t *s)
68 const wchar_t *ptr;
70 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
72 return ptr - s;
74 # endif
75 # endif
76 #endif
78 #if WIDE_CHAR_VERSION
79 # define VASNPRINTF vasnwprintf
80 # define CHAR_T wchar_t
81 # define DIRECTIVE wchar_t_directive
82 # define DIRECTIVES wchar_t_directives
83 # define PRINTF_PARSE wprintf_parse
84 # define USE_SNPRINTF 1
85 # if HAVE_DECL__SNWPRINTF
86 /* On Windows, the function swprintf() has a different signature than
87 on Unix; we use the _snwprintf() function instead. */
88 # define SNPRINTF _snwprintf
89 # else
90 /* Unix. */
91 # define SNPRINTF swprintf
92 # endif
93 #else
94 # define VASNPRINTF vasnprintf
95 # define CHAR_T char
96 # define DIRECTIVE char_directive
97 # define DIRECTIVES char_directives
98 # define PRINTF_PARSE printf_parse
99 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
100 # if HAVE_DECL__SNPRINTF
101 /* Windows. */
102 # define SNPRINTF _snprintf
103 # else
104 /* Unix. */
105 # define SNPRINTF snprintf
106 # endif
107 #endif
109 CHAR_T *
110 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
112 DIRECTIVES d;
113 arguments a;
115 if (PRINTF_PARSE (format, &d, &a) < 0)
117 errno = EINVAL;
118 return NULL;
121 #define CLEANUP() \
122 free (d.dir); \
123 if (a.arg) \
124 free (a.arg);
126 if (printf_fetchargs (args, &a) < 0)
128 CLEANUP ();
129 errno = EINVAL;
130 return NULL;
134 size_t buf_neededlength;
135 CHAR_T *buf;
136 CHAR_T *buf_malloced;
137 const CHAR_T *cp;
138 size_t i;
139 DIRECTIVE *dp;
140 /* Output string accumulator. */
141 CHAR_T *result;
142 size_t allocated;
143 size_t length;
145 /* Allocate a small buffer that will hold a directive passed to
146 sprintf or snprintf. */
147 buf_neededlength = 7 + d.max_width_length + d.max_precision_length + 6;
148 #if HAVE_ALLOCA
149 if (buf_neededlength < 4000 / sizeof (CHAR_T))
151 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
152 buf_malloced = NULL;
154 else
155 #endif
157 if (SIZE_MAX / sizeof (CHAR_T) < buf_neededlength)
158 goto out_of_memory_1;
159 buf = (CHAR_T *) malloc (buf_neededlength * sizeof (CHAR_T));
160 if (buf == NULL)
161 goto out_of_memory_1;
162 buf_malloced = buf;
165 if (resultbuf != NULL)
167 result = resultbuf;
168 allocated = *lengthp;
170 else
172 result = NULL;
173 allocated = 0;
175 length = 0;
176 /* Invariants:
177 result is either == resultbuf or == NULL or malloc-allocated.
178 If length > 0, then result != NULL. */
180 /* Ensures that allocated >= length + extra. Aborts through a jump to
181 out_of_memory if size is too big. */
182 #define ENSURE_ALLOCATION(extra) \
184 size_t needed = length + (extra); \
185 if (needed < length) \
186 goto out_of_memory; \
187 if (needed > allocated) \
189 size_t memory_size; \
190 CHAR_T *memory; \
192 allocated = (allocated > 0 ? 2 * allocated : 12); \
193 if (needed > allocated) \
194 allocated = needed; \
195 if (SIZE_MAX / sizeof (CHAR_T) < allocated) \
196 goto out_of_memory; \
197 memory_size = allocated * sizeof (CHAR_T); \
198 if (result == resultbuf || result == NULL) \
199 memory = (CHAR_T *) malloc (memory_size); \
200 else \
201 memory = (CHAR_T *) realloc (result, memory_size); \
202 if (memory == NULL) \
203 goto out_of_memory; \
204 if (result == resultbuf && length > 0) \
205 memcpy (memory, result, length * sizeof (CHAR_T)); \
206 result = memory; \
210 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
212 if (cp != dp->dir_start)
214 size_t n = dp->dir_start - cp;
216 ENSURE_ALLOCATION (n);
217 memcpy (result + length, cp, n * sizeof (CHAR_T));
218 length += n;
220 if (i == d.count)
221 break;
223 /* Execute a single directive. */
224 if (dp->conversion == '%')
226 if (!(dp->arg_index == ARG_NONE))
227 abort ();
228 ENSURE_ALLOCATION (1);
229 result[length] = '%';
230 length += 1;
232 else
234 if (!(dp->arg_index != ARG_NONE))
235 abort ();
237 if (dp->conversion == 'n')
239 switch (a.arg[dp->arg_index].type)
241 case TYPE_COUNT_SCHAR_POINTER:
242 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
243 break;
244 case TYPE_COUNT_SHORT_POINTER:
245 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
246 break;
247 case TYPE_COUNT_INT_POINTER:
248 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
249 break;
250 case TYPE_COUNT_LONGINT_POINTER:
251 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
252 break;
253 #ifdef HAVE_LONG_LONG
254 case TYPE_COUNT_LONGLONGINT_POINTER:
255 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
256 break;
257 #endif
258 default:
259 abort ();
262 else
264 arg_type type = a.arg[dp->arg_index].type;
265 CHAR_T *p;
266 unsigned int prefix_count;
267 int prefixes[2];
268 #if !USE_SNPRINTF
269 size_t tmp_length;
270 CHAR_T tmpbuf[700];
271 CHAR_T *tmp;
273 /* Allocate a temporary buffer of sufficient size for calling
274 sprintf. */
276 size_t width;
277 size_t precision;
279 width = 0;
280 if (dp->width_start != dp->width_end)
282 if (dp->width_arg_index != ARG_NONE)
284 int arg;
286 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
287 abort ();
288 arg = a.arg[dp->width_arg_index].a.a_int;
289 width = (arg < 0 ? (unsigned int) (-arg) : arg);
291 else
293 const CHAR_T *digitp = dp->width_start;
297 if (SIZE_MAX / 10 <= width)
298 goto out_of_memory;
299 width = width * 10 + (*digitp++ - '0');
301 while (digitp != dp->width_end);
305 precision = 6;
306 if (dp->precision_start != dp->precision_end)
308 if (dp->precision_arg_index != ARG_NONE)
310 int arg;
312 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
313 abort ();
314 arg = a.arg[dp->precision_arg_index].a.a_int;
315 precision = (arg < 0 ? 0 : arg);
317 else
319 const CHAR_T *digitp = dp->precision_start + 1;
321 precision = 0;
322 while (digitp != dp->precision_end)
324 size_t p1 = 10 * precision + (*digitp++ - '0');
325 precision = ((SIZE_MAX / 10 < precision
326 || p1 < precision)
327 ? SIZE_MAX : p1);
332 switch (dp->conversion)
335 case 'd': case 'i': case 'u':
336 # ifdef HAVE_LONG_LONG
337 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
338 tmp_length =
339 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
340 * 0.30103 /* binary -> decimal */
341 * 2 /* estimate for FLAG_GROUP */
343 + 1 /* turn floor into ceil */
344 + 1; /* account for leading sign */
345 else
346 # endif
347 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
348 tmp_length =
349 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
350 * 0.30103 /* binary -> decimal */
351 * 2 /* estimate for FLAG_GROUP */
353 + 1 /* turn floor into ceil */
354 + 1; /* account for leading sign */
355 else
356 tmp_length =
357 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
358 * 0.30103 /* binary -> decimal */
359 * 2 /* estimate for FLAG_GROUP */
361 + 1 /* turn floor into ceil */
362 + 1; /* account for leading sign */
363 break;
365 case 'o':
366 # ifdef HAVE_LONG_LONG
367 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
368 tmp_length =
369 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
370 * 0.333334 /* binary -> octal */
372 + 1 /* turn floor into ceil */
373 + 1; /* account for leading sign */
374 else
375 # endif
376 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
377 tmp_length =
378 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
379 * 0.333334 /* binary -> octal */
381 + 1 /* turn floor into ceil */
382 + 1; /* account for leading sign */
383 else
384 tmp_length =
385 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
386 * 0.333334 /* binary -> octal */
388 + 1 /* turn floor into ceil */
389 + 1; /* account for leading sign */
390 break;
392 case 'x': case 'X':
393 # ifdef HAVE_LONG_LONG
394 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
395 tmp_length =
396 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
397 * 0.25 /* binary -> hexadecimal */
399 + 1 /* turn floor into ceil */
400 + 2; /* account for leading sign or alternate form */
401 else
402 # endif
403 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
404 tmp_length =
405 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
406 * 0.25 /* binary -> hexadecimal */
408 + 1 /* turn floor into ceil */
409 + 2; /* account for leading sign or alternate form */
410 else
411 tmp_length =
412 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
413 * 0.25 /* binary -> hexadecimal */
415 + 1 /* turn floor into ceil */
416 + 2; /* account for leading sign or alternate form */
417 break;
419 case 'f': case 'F':
420 # ifdef HAVE_LONG_DOUBLE
421 if (type == TYPE_LONGDOUBLE)
422 tmp_length =
423 (unsigned int) (LDBL_MAX_EXP
424 * 0.30103 /* binary -> decimal */
425 * 2 /* estimate for FLAG_GROUP */
427 + 1 /* turn floor into ceil */
428 + 10; /* sign, decimal point etc. */
429 else
430 # endif
431 tmp_length =
432 (unsigned int) (DBL_MAX_EXP
433 * 0.30103 /* binary -> decimal */
434 * 2 /* estimate for FLAG_GROUP */
436 + 1 /* turn floor into ceil */
437 + 10; /* sign, decimal point etc. */
438 tmp_length += precision;
439 if (tmp_length < precision)
440 goto out_of_memory;
441 break;
443 case 'e': case 'E': case 'g': case 'G':
444 case 'a': case 'A':
445 tmp_length =
446 12; /* sign, decimal point, exponent etc. */
447 tmp_length += precision;
448 if (tmp_length < precision)
449 goto out_of_memory;
450 break;
452 case 'c':
453 # if defined HAVE_WINT_T && !WIDE_CHAR_VERSION
454 if (type == TYPE_WIDE_CHAR)
455 tmp_length = MB_CUR_MAX;
456 else
457 # endif
458 tmp_length = 1;
459 break;
461 case 's':
462 # ifdef HAVE_WCHAR_T
463 if (type == TYPE_WIDE_STRING)
465 tmp_length =
466 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
468 # if !WIDE_CHAR_VERSION
469 if (SIZE_MAX / MB_CUR_MAX < tmp_length)
470 goto out_of_memory;
471 tmp_length *= MB_CUR_MAX;
472 # endif
474 else
475 # endif
476 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
477 break;
479 case 'p':
480 tmp_length =
481 (unsigned int) (sizeof (void *) * CHAR_BIT
482 * 0.25 /* binary -> hexadecimal */
484 + 1 /* turn floor into ceil */
485 + 2; /* account for leading 0x */
486 break;
488 default:
489 abort ();
492 if (tmp_length < width)
493 tmp_length = width;
495 tmp_length++; /* account for trailing NUL */
496 if (!tmp_length)
497 goto out_of_memory;
500 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
501 tmp = tmpbuf;
502 else
504 if (SIZE_MAX / sizeof (CHAR_T) < tmp_length)
505 /* Overflow, would lead to out of memory. */
506 goto out_of_memory;
507 tmp = (CHAR_T *) malloc (tmp_length * sizeof (CHAR_T));
508 if (tmp == NULL)
509 /* Out of memory. */
510 goto out_of_memory;
512 #endif
514 /* Construct the format string for calling snprintf or
515 sprintf. */
516 p = buf;
517 *p++ = '%';
518 if (dp->flags & FLAG_GROUP)
519 *p++ = '\'';
520 if (dp->flags & FLAG_LEFT)
521 *p++ = '-';
522 if (dp->flags & FLAG_SHOWSIGN)
523 *p++ = '+';
524 if (dp->flags & FLAG_SPACE)
525 *p++ = ' ';
526 if (dp->flags & FLAG_ALT)
527 *p++ = '#';
528 if (dp->flags & FLAG_ZERO)
529 *p++ = '0';
530 if (dp->width_start != dp->width_end)
532 size_t n = dp->width_end - dp->width_start;
533 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
534 p += n;
536 if (dp->precision_start != dp->precision_end)
538 size_t n = dp->precision_end - dp->precision_start;
539 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
540 p += n;
543 switch (type)
545 #ifdef HAVE_LONG_LONG
546 case TYPE_LONGLONGINT:
547 case TYPE_ULONGLONGINT:
548 *p++ = 'l';
549 /*FALLTHROUGH*/
550 #endif
551 case TYPE_LONGINT:
552 case TYPE_ULONGINT:
553 #ifdef HAVE_WINT_T
554 case TYPE_WIDE_CHAR:
555 #endif
556 #ifdef HAVE_WCHAR_T
557 case TYPE_WIDE_STRING:
558 #endif
559 *p++ = 'l';
560 break;
561 #ifdef HAVE_LONG_DOUBLE
562 case TYPE_LONGDOUBLE:
563 *p++ = 'L';
564 break;
565 #endif
566 default:
567 break;
569 *p = dp->conversion;
570 #if USE_SNPRINTF
571 p[1] = '%';
572 p[2] = 'n';
573 p[3] = '\0';
574 #else
575 p[1] = '\0';
576 #endif
578 /* Construct the arguments for calling snprintf or sprintf. */
579 prefix_count = 0;
580 if (dp->width_arg_index != ARG_NONE)
582 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
583 abort ();
584 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
586 if (dp->precision_arg_index != ARG_NONE)
588 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
589 abort ();
590 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
593 #if USE_SNPRINTF
594 /* Prepare checking whether snprintf returns the count
595 via %n. */
596 ENSURE_ALLOCATION (1);
597 result[length] = '\0';
598 #endif
600 for (;;)
602 size_t maxlen;
603 int count;
604 int retcount;
606 maxlen = allocated - length;
607 count = -1;
608 retcount = 0;
610 #if USE_SNPRINTF
611 # define SNPRINTF_BUF(arg) \
612 switch (prefix_count) \
614 case 0: \
615 retcount = SNPRINTF (result + length, maxlen, buf, \
616 arg, &count); \
617 break; \
618 case 1: \
619 retcount = SNPRINTF (result + length, maxlen, buf, \
620 prefixes[0], arg, &count); \
621 break; \
622 case 2: \
623 retcount = SNPRINTF (result + length, maxlen, buf, \
624 prefixes[0], prefixes[1], arg, \
625 &count); \
626 break; \
627 default: \
628 abort (); \
630 #else
631 # define SNPRINTF_BUF(arg) \
632 switch (prefix_count) \
634 case 0: \
635 count = sprintf (tmp, buf, arg); \
636 break; \
637 case 1: \
638 count = sprintf (tmp, buf, prefixes[0], arg); \
639 break; \
640 case 2: \
641 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
642 arg); \
643 break; \
644 default: \
645 abort (); \
647 #endif
649 switch (type)
651 case TYPE_SCHAR:
653 int arg = a.arg[dp->arg_index].a.a_schar;
654 SNPRINTF_BUF (arg);
656 break;
657 case TYPE_UCHAR:
659 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
660 SNPRINTF_BUF (arg);
662 break;
663 case TYPE_SHORT:
665 int arg = a.arg[dp->arg_index].a.a_short;
666 SNPRINTF_BUF (arg);
668 break;
669 case TYPE_USHORT:
671 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
672 SNPRINTF_BUF (arg);
674 break;
675 case TYPE_INT:
677 int arg = a.arg[dp->arg_index].a.a_int;
678 SNPRINTF_BUF (arg);
680 break;
681 case TYPE_UINT:
683 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
684 SNPRINTF_BUF (arg);
686 break;
687 case TYPE_LONGINT:
689 long int arg = a.arg[dp->arg_index].a.a_longint;
690 SNPRINTF_BUF (arg);
692 break;
693 case TYPE_ULONGINT:
695 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
696 SNPRINTF_BUF (arg);
698 break;
699 #ifdef HAVE_LONG_LONG
700 case TYPE_LONGLONGINT:
702 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
703 SNPRINTF_BUF (arg);
705 break;
706 case TYPE_ULONGLONGINT:
708 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
709 SNPRINTF_BUF (arg);
711 break;
712 #endif
713 case TYPE_DOUBLE:
715 double arg = a.arg[dp->arg_index].a.a_double;
716 SNPRINTF_BUF (arg);
718 break;
719 #ifdef HAVE_LONG_DOUBLE
720 case TYPE_LONGDOUBLE:
722 long double arg = a.arg[dp->arg_index].a.a_longdouble;
723 SNPRINTF_BUF (arg);
725 break;
726 #endif
727 case TYPE_CHAR:
729 int arg = a.arg[dp->arg_index].a.a_char;
730 SNPRINTF_BUF (arg);
732 break;
733 #ifdef HAVE_WINT_T
734 case TYPE_WIDE_CHAR:
736 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
737 SNPRINTF_BUF (arg);
739 break;
740 #endif
741 case TYPE_STRING:
743 const char *arg = a.arg[dp->arg_index].a.a_string;
744 SNPRINTF_BUF (arg);
746 break;
747 #ifdef HAVE_WCHAR_T
748 case TYPE_WIDE_STRING:
750 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
751 SNPRINTF_BUF (arg);
753 break;
754 #endif
755 case TYPE_POINTER:
757 void *arg = a.arg[dp->arg_index].a.a_pointer;
758 SNPRINTF_BUF (arg);
760 break;
761 default:
762 abort ();
765 #if USE_SNPRINTF
766 /* Portability: Not all implementations of snprintf()
767 are ISO C 99 compliant. Determine the number of
768 bytes that snprintf() has produced or would have
769 produced. */
770 if (count >= 0)
772 /* Verify that snprintf() has NUL-terminated its
773 result. */
774 if (count < maxlen && result[length + count] != '\0')
775 abort ();
776 /* Portability hack. */
777 if (retcount > count)
778 count = retcount;
780 else
782 /* snprintf() doesn't understand the '%n'
783 directive. */
784 if (p[1] != '\0')
786 /* Don't use the '%n' directive; instead, look
787 at the snprintf() return value. */
788 p[1] = '\0';
789 continue;
791 else
793 /* Look at the snprintf() return value. */
794 if (retcount < 0)
796 /* HP-UX 10.20 snprintf() is doubly deficient:
797 It doesn't understand the '%n' directive,
798 *and* it returns -1 (rather than the length
799 that would have been required) when the
800 buffer is too small. */
801 size_t bigger_need =
802 (allocated > 12 ? allocated : 12);
803 ENSURE_ALLOCATION (bigger_need);
804 continue;
806 else
807 count = retcount;
810 #endif
812 /* Attempt to handle failure. */
813 if (count < 0)
815 if (!(result == resultbuf || result == NULL))
816 free (result);
817 if (buf_malloced != NULL)
818 free (buf_malloced);
819 CLEANUP ();
820 errno = EINVAL;
821 return NULL;
824 #if !USE_SNPRINTF
825 if (count >= tmp_length)
826 /* tmp_length was incorrectly calculated - fix the
827 code above! */
828 abort ();
829 #endif
831 /* Make room for the result. */
832 if (count >= maxlen)
834 /* Need at least count bytes. But allocate
835 proportionally, to avoid looping eternally if
836 snprintf() reports a too small count. */
837 ENSURE_ALLOCATION (count < allocated
838 ? allocated : count);
839 #if USE_SNPRINTF
840 continue;
841 #endif
844 #if USE_SNPRINTF
845 /* The snprintf() result did fit. */
846 #else
847 /* Append the sprintf() result. */
848 memcpy (result + length, tmp, count * sizeof (CHAR_T));
849 if (tmp != tmpbuf)
850 free (tmp);
851 #endif
853 length += count;
854 break;
860 /* Add the final NUL. */
861 ENSURE_ALLOCATION (1);
862 result[length] = '\0';
864 if (result != resultbuf && length + 1 < allocated)
866 /* Shrink the allocated memory if possible. */
867 CHAR_T *memory;
869 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
870 if (memory != NULL)
871 result = memory;
874 if (buf_malloced != NULL)
875 free (buf_malloced);
876 CLEANUP ();
877 *lengthp = length;
878 if (length > INT_MAX)
879 goto length_overflow;
880 return result;
882 length_overflow:
883 /* We could produce such a big string, but its length doesn't fit into
884 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
885 this case. */
886 if (result != resultbuf)
887 free (result);
888 errno = EOVERFLOW;
889 return NULL;
891 out_of_memory:
892 if (!(result == resultbuf || result == NULL))
893 free (result);
894 if (buf_malloced != NULL)
895 free (buf_malloced);
896 out_of_memory_1:
897 CLEANUP ();
898 errno = ENOMEM;
899 return NULL;
903 #undef SNPRINTF
904 #undef USE_SNPRINTF
905 #undef PRINTF_PARSE
906 #undef DIRECTIVES
907 #undef DIRECTIVE
908 #undef CHAR_T
909 #undef VASNPRINTF