Try to fix today's NFS-related failure: Treat ESTALE like EACCES.
[coreutils.git] / lib / vasnprintf.c
blob8be7ac48086b1f9e4e6a98d6e9c23f9b24a5226f
1 /* vsprintf with automatic memory allocation.
2 This file is intended to provide exactly the same functionality
3 as the version in gnulib, but without the need for the xsize module.
5 Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE 1
26 #endif
28 #include <config.h>
29 #ifndef IN_LIBINTL
30 # include <alloca.h>
31 #endif
33 /* Specification. */
34 #if WIDE_CHAR_VERSION
35 # include "vasnwprintf.h"
36 #else
37 # include "vasnprintf.h"
38 #endif
40 #include <stdio.h> /* snprintf(), sprintf() */
41 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
42 #include <stdint.h> /* SIZE_MAX */
43 #include <string.h> /* memcpy(), strlen() */
44 #include <errno.h> /* errno */
45 #include <limits.h> /* CHAR_BIT, INT_MAX */
46 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
47 #if WIDE_CHAR_VERSION
48 # include "wprintf-parse.h"
49 #else
50 # include "printf-parse.h"
51 #endif
53 #if HAVE_WCHAR_T
54 # if HAVE_WCSLEN
55 # define local_wcslen wcslen
56 # else
57 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
58 a dependency towards this library, here is a local substitute.
59 Define this substitute only once, even if this file is included
60 twice in the same compilation unit. */
61 # ifndef local_wcslen_defined
62 # define local_wcslen_defined 1
63 static size_t
64 local_wcslen (const wchar_t *s)
66 const wchar_t *ptr;
68 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
70 return ptr - s;
72 # endif
73 # endif
74 #endif
76 #if WIDE_CHAR_VERSION
77 # define VASNPRINTF vasnwprintf
78 # define CHAR_T wchar_t
79 # define DIRECTIVE wchar_t_directive
80 # define DIRECTIVES wchar_t_directives
81 # define PRINTF_PARSE wprintf_parse
82 # define USE_SNPRINTF 1
83 # if HAVE_DECL__SNWPRINTF
84 /* On Windows, the function swprintf() has a different signature than
85 on Unix; we use the _snwprintf() function instead. */
86 # define SNPRINTF _snwprintf
87 # else
88 /* Unix. */
89 # define SNPRINTF swprintf
90 # endif
91 #else
92 # define VASNPRINTF vasnprintf
93 # define CHAR_T char
94 # define DIRECTIVE char_directive
95 # define DIRECTIVES char_directives
96 # define PRINTF_PARSE printf_parse
97 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
98 # if HAVE_DECL__SNPRINTF
99 /* Windows. */
100 # define SNPRINTF _snprintf
101 # else
102 /* Unix. */
103 # define SNPRINTF snprintf
104 # endif
105 #endif
107 CHAR_T *
108 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
110 DIRECTIVES d;
111 arguments a;
113 if (PRINTF_PARSE (format, &d, &a) < 0)
115 errno = EINVAL;
116 return NULL;
119 #define CLEANUP() \
120 free (d.dir); \
121 if (a.arg) \
122 free (a.arg);
124 if (printf_fetchargs (args, &a) < 0)
126 CLEANUP ();
127 errno = EINVAL;
128 return NULL;
132 size_t buf_neededlength;
133 CHAR_T *buf;
134 CHAR_T *buf_malloced;
135 const CHAR_T *cp;
136 size_t i;
137 DIRECTIVE *dp;
138 /* Output string accumulator. */
139 CHAR_T *result;
140 size_t allocated;
141 size_t length;
143 /* Allocate a small buffer that will hold a directive passed to
144 sprintf or snprintf. */
145 buf_neededlength = 7 + d.max_width_length + d.max_precision_length + 6;
146 #if HAVE_ALLOCA
147 if (buf_neededlength < 4000 / sizeof (CHAR_T))
149 buf = alloca (buf_neededlength * sizeof (CHAR_T));
150 buf_malloced = NULL;
152 else
153 #endif
155 if (SIZE_MAX / sizeof (CHAR_T) < buf_neededlength)
156 goto out_of_memory_1;
157 buf = (CHAR_T *) malloc (buf_neededlength * sizeof (CHAR_T));
158 if (buf == NULL)
159 goto out_of_memory_1;
160 buf_malloced = buf;
163 if (resultbuf != NULL)
165 result = resultbuf;
166 allocated = *lengthp;
168 else
170 result = NULL;
171 allocated = 0;
173 length = 0;
174 /* Invariants:
175 result is either == resultbuf or == NULL or malloc-allocated.
176 If length > 0, then result != NULL. */
178 /* Ensures that allocated >= length + extra. Aborts through a jump to
179 out_of_memory if size is too big. */
180 #define ENSURE_ALLOCATION(extra) \
182 size_t needed = length + (extra); \
183 if (needed < length) \
184 goto out_of_memory; \
185 if (needed > allocated) \
187 size_t memory_size; \
188 CHAR_T *memory; \
190 allocated = (allocated > 0 ? 2 * allocated : 12); \
191 if (needed > allocated) \
192 allocated = needed; \
193 if (SIZE_MAX / sizeof (CHAR_T) < allocated) \
194 goto out_of_memory; \
195 memory_size = allocated * sizeof (CHAR_T); \
196 if (result == resultbuf || result == NULL) \
197 memory = (CHAR_T *) malloc (memory_size); \
198 else \
199 memory = (CHAR_T *) realloc (result, memory_size); \
200 if (memory == NULL) \
201 goto out_of_memory; \
202 if (result == resultbuf && length > 0) \
203 memcpy (memory, result, length * sizeof (CHAR_T)); \
204 result = memory; \
208 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
210 if (cp != dp->dir_start)
212 size_t n = dp->dir_start - cp;
214 ENSURE_ALLOCATION (n);
215 memcpy (result + length, cp, n * sizeof (CHAR_T));
216 length += n;
218 if (i == d.count)
219 break;
221 /* Execute a single directive. */
222 if (dp->conversion == '%')
224 if (!(dp->arg_index == ARG_NONE))
225 abort ();
226 ENSURE_ALLOCATION (1);
227 result[length] = '%';
228 length += 1;
230 else
232 if (!(dp->arg_index != ARG_NONE))
233 abort ();
235 if (dp->conversion == 'n')
237 switch (a.arg[dp->arg_index].type)
239 case TYPE_COUNT_SCHAR_POINTER:
240 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
241 break;
242 case TYPE_COUNT_SHORT_POINTER:
243 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
244 break;
245 case TYPE_COUNT_INT_POINTER:
246 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
247 break;
248 case TYPE_COUNT_LONGINT_POINTER:
249 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
250 break;
251 #if HAVE_LONG_LONG_INT
252 case TYPE_COUNT_LONGLONGINT_POINTER:
253 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
254 break;
255 #endif
256 default:
257 abort ();
260 else
262 arg_type type = a.arg[dp->arg_index].type;
263 CHAR_T *p;
264 unsigned int prefix_count;
265 int prefixes[2];
266 #if !USE_SNPRINTF
267 size_t tmp_length;
268 CHAR_T tmpbuf[700];
269 CHAR_T *tmp;
271 /* Allocate a temporary buffer of sufficient size for calling
272 sprintf. */
274 size_t width;
275 size_t precision;
277 width = 0;
278 if (dp->width_start != dp->width_end)
280 if (dp->width_arg_index != ARG_NONE)
282 int arg;
284 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
285 abort ();
286 arg = a.arg[dp->width_arg_index].a.a_int;
287 width = (arg < 0 ? (unsigned int) (-arg) : arg);
289 else
291 const CHAR_T *digitp = dp->width_start;
295 size_t w_tmp = width * 10 + (*digitp++ - '0');
296 if (SIZE_MAX / 10 < width || w_tmp < width)
297 goto out_of_memory;
298 width = w_tmp;
300 while (digitp != dp->width_end);
304 precision = 6;
305 if (dp->precision_start != dp->precision_end)
307 if (dp->precision_arg_index != ARG_NONE)
309 int arg;
311 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
312 abort ();
313 arg = a.arg[dp->precision_arg_index].a.a_int;
314 precision = (arg < 0 ? 0 : arg);
316 else
318 const CHAR_T *digitp = dp->precision_start + 1;
320 precision = 0;
321 while (digitp != dp->precision_end)
323 size_t p1 = 10 * precision + (*digitp++ - '0');
324 precision = ((SIZE_MAX / 10 < precision
325 || p1 < precision)
326 ? SIZE_MAX : p1);
331 switch (dp->conversion)
334 case 'd': case 'i': case 'u':
335 # if HAVE_LONG_LONG_INT
336 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
337 tmp_length =
338 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
339 * 0.30103 /* binary -> decimal */
341 + 1; /* turn floor into ceil */
342 else
343 # endif
344 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
345 tmp_length =
346 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
347 * 0.30103 /* binary -> decimal */
349 + 1; /* turn floor into ceil */
350 else
351 tmp_length =
352 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
353 * 0.30103 /* binary -> decimal */
355 + 1; /* turn floor into ceil */
356 if (tmp_length < precision)
357 tmp_length = precision;
358 /* Multiply by 2, as an estimate for FLAG_GROUP. */
359 /* Add 1, to account for a leading sign. */
360 tmp_length = (tmp_length < SIZE_MAX / 2
361 ? 2 * tmp_length + 1
362 : SIZE_MAX);
363 break;
365 case 'o':
366 # if HAVE_LONG_LONG_INT
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 else
374 # endif
375 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
376 tmp_length =
377 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
378 * 0.333334 /* binary -> octal */
380 + 1; /* turn floor into ceil */
381 else
382 tmp_length =
383 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
384 * 0.333334 /* binary -> octal */
386 + 1; /* turn floor into ceil */
387 if (tmp_length < precision)
388 tmp_length = precision;
389 /* Add 1, to account for a leading sign. */
390 tmp_length += (tmp_length < SIZE_MAX);
391 break;
393 case 'x': case 'X':
394 # if HAVE_LONG_LONG_INT
395 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
396 tmp_length =
397 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
398 * 0.25 /* binary -> hexadecimal */
400 + 1; /* turn floor into ceil */
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 else
410 tmp_length =
411 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
412 * 0.25 /* binary -> hexadecimal */
414 + 1; /* turn floor into ceil */
415 if (tmp_length < precision)
416 tmp_length = precision;
417 /* Add 2, to account for a leading sign or alternate form. */
418 if (tmp_length <= SIZE_MAX / 2)
419 tmp_length *= 2;
420 break;
422 case 'f': case 'F':
423 # if HAVE_LONG_DOUBLE
424 if (type == TYPE_LONGDOUBLE)
425 tmp_length =
426 (unsigned int) (LDBL_MAX_EXP
427 * 0.30103 /* binary -> decimal */
428 * 2 /* estimate for FLAG_GROUP */
430 + 1 /* turn floor into ceil */
431 + 10; /* sign, decimal point etc. */
432 else
433 # endif
434 tmp_length =
435 (unsigned int) (DBL_MAX_EXP
436 * 0.30103 /* binary -> decimal */
437 * 2 /* estimate for FLAG_GROUP */
439 + 1 /* turn floor into ceil */
440 + 10; /* sign, decimal point etc. */
441 tmp_length += precision;
442 if (tmp_length < precision)
443 goto out_of_memory;
444 break;
446 case 'e': case 'E': case 'g': case 'G':
447 tmp_length =
448 12; /* sign, decimal point, exponent etc. */
449 tmp_length += precision;
450 if (tmp_length < precision)
451 goto out_of_memory;
452 break;
454 case 'a': case 'A':
455 # if HAVE_LONG_DOUBLE
456 if (type == TYPE_LONGDOUBLE)
457 tmp_length =
458 (unsigned int) (LDBL_DIG
459 * 0.831 /* decimal -> hexadecimal */
461 + 1; /* turn floor into ceil */
462 else
463 # endif
464 tmp_length =
465 (unsigned int) (DBL_DIG
466 * 0.831 /* decimal -> hexadecimal */
468 + 1; /* turn floor into ceil */
469 if (tmp_length < precision)
470 tmp_length = precision;
471 /* Account for sign, decimal point etc. */
472 tmp_length += 12;
473 if (tmp_length < 12)
474 goto out_of_memory;
475 break;
477 case 'c':
478 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
479 if (type == TYPE_WIDE_CHAR)
480 tmp_length = MB_CUR_MAX;
481 else
482 # endif
483 tmp_length = 1;
484 break;
486 case 's':
487 # if HAVE_WCHAR_T
488 if (type == TYPE_WIDE_STRING)
490 tmp_length =
491 local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
493 # if !WIDE_CHAR_VERSION
494 if (SIZE_MAX / MB_CUR_MAX < tmp_length)
495 goto out_of_memory;
496 tmp_length *= MB_CUR_MAX;
497 # endif
499 else
500 # endif
501 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
502 break;
504 case 'p':
505 tmp_length =
506 (unsigned int) (sizeof (void *) * CHAR_BIT
507 * 0.25 /* binary -> hexadecimal */
509 + 1 /* turn floor into ceil */
510 + 2; /* account for leading 0x */
511 break;
513 default:
514 abort ();
517 if (tmp_length < width)
518 tmp_length = width;
520 tmp_length++; /* account for trailing NUL */
521 if (!tmp_length)
522 goto out_of_memory;
525 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
526 tmp = tmpbuf;
527 else
529 if (SIZE_MAX / sizeof (CHAR_T) < tmp_length)
530 /* Overflow, would lead to out of memory. */
531 goto out_of_memory;
532 tmp = (CHAR_T *) malloc (tmp_length * sizeof (CHAR_T));
533 if (tmp == NULL)
534 /* Out of memory. */
535 goto out_of_memory;
537 #endif
539 /* Construct the format string for calling snprintf or
540 sprintf. */
541 p = buf;
542 *p++ = '%';
543 if (dp->flags & FLAG_GROUP)
544 *p++ = '\'';
545 if (dp->flags & FLAG_LEFT)
546 *p++ = '-';
547 if (dp->flags & FLAG_SHOWSIGN)
548 *p++ = '+';
549 if (dp->flags & FLAG_SPACE)
550 *p++ = ' ';
551 if (dp->flags & FLAG_ALT)
552 *p++ = '#';
553 if (dp->flags & FLAG_ZERO)
554 *p++ = '0';
555 if (dp->width_start != dp->width_end)
557 size_t n = dp->width_end - dp->width_start;
558 memcpy (p, dp->width_start, n * sizeof (CHAR_T));
559 p += n;
561 if (dp->precision_start != dp->precision_end)
563 size_t n = dp->precision_end - dp->precision_start;
564 memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
565 p += n;
568 switch (type)
570 #if HAVE_LONG_LONG_INT
571 case TYPE_LONGLONGINT:
572 case TYPE_ULONGLONGINT:
573 *p++ = 'l';
574 /*FALLTHROUGH*/
575 #endif
576 case TYPE_LONGINT:
577 case TYPE_ULONGINT:
578 #if HAVE_WINT_T
579 case TYPE_WIDE_CHAR:
580 #endif
581 #if HAVE_WCHAR_T
582 case TYPE_WIDE_STRING:
583 #endif
584 *p++ = 'l';
585 break;
586 #if HAVE_LONG_DOUBLE
587 case TYPE_LONGDOUBLE:
588 *p++ = 'L';
589 break;
590 #endif
591 default:
592 break;
594 *p = dp->conversion;
595 #if USE_SNPRINTF
596 p[1] = '%';
597 p[2] = 'n';
598 p[3] = '\0';
599 #else
600 p[1] = '\0';
601 #endif
603 /* Construct the arguments for calling snprintf or sprintf. */
604 prefix_count = 0;
605 if (dp->width_arg_index != ARG_NONE)
607 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
608 abort ();
609 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
611 if (dp->precision_arg_index != ARG_NONE)
613 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
614 abort ();
615 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
618 #if USE_SNPRINTF
619 /* Prepare checking whether snprintf returns the count
620 via %n. */
621 ENSURE_ALLOCATION (1);
622 result[length] = '\0';
623 #endif
625 for (;;)
627 size_t maxlen;
628 int count;
629 int retcount;
631 maxlen = allocated - length;
632 count = -1;
633 retcount = 0;
635 #if USE_SNPRINTF
636 # define SNPRINTF_BUF(arg) \
637 switch (prefix_count) \
639 case 0: \
640 retcount = SNPRINTF (result + length, maxlen, buf, \
641 arg, &count); \
642 break; \
643 case 1: \
644 retcount = SNPRINTF (result + length, maxlen, buf, \
645 prefixes[0], arg, &count); \
646 break; \
647 case 2: \
648 retcount = SNPRINTF (result + length, maxlen, buf, \
649 prefixes[0], prefixes[1], arg, \
650 &count); \
651 break; \
652 default: \
653 abort (); \
655 #else
656 # define SNPRINTF_BUF(arg) \
657 switch (prefix_count) \
659 case 0: \
660 count = sprintf (tmp, buf, arg); \
661 break; \
662 case 1: \
663 count = sprintf (tmp, buf, prefixes[0], arg); \
664 break; \
665 case 2: \
666 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
667 arg); \
668 break; \
669 default: \
670 abort (); \
672 #endif
674 switch (type)
676 case TYPE_SCHAR:
678 int arg = a.arg[dp->arg_index].a.a_schar;
679 SNPRINTF_BUF (arg);
681 break;
682 case TYPE_UCHAR:
684 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
685 SNPRINTF_BUF (arg);
687 break;
688 case TYPE_SHORT:
690 int arg = a.arg[dp->arg_index].a.a_short;
691 SNPRINTF_BUF (arg);
693 break;
694 case TYPE_USHORT:
696 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
697 SNPRINTF_BUF (arg);
699 break;
700 case TYPE_INT:
702 int arg = a.arg[dp->arg_index].a.a_int;
703 SNPRINTF_BUF (arg);
705 break;
706 case TYPE_UINT:
708 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
709 SNPRINTF_BUF (arg);
711 break;
712 case TYPE_LONGINT:
714 long int arg = a.arg[dp->arg_index].a.a_longint;
715 SNPRINTF_BUF (arg);
717 break;
718 case TYPE_ULONGINT:
720 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
721 SNPRINTF_BUF (arg);
723 break;
724 #if HAVE_LONG_LONG_INT
725 case TYPE_LONGLONGINT:
727 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
728 SNPRINTF_BUF (arg);
730 break;
731 case TYPE_ULONGLONGINT:
733 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
734 SNPRINTF_BUF (arg);
736 break;
737 #endif
738 case TYPE_DOUBLE:
740 double arg = a.arg[dp->arg_index].a.a_double;
741 SNPRINTF_BUF (arg);
743 break;
744 #if HAVE_LONG_DOUBLE
745 case TYPE_LONGDOUBLE:
747 long double arg = a.arg[dp->arg_index].a.a_longdouble;
748 SNPRINTF_BUF (arg);
750 break;
751 #endif
752 case TYPE_CHAR:
754 int arg = a.arg[dp->arg_index].a.a_char;
755 SNPRINTF_BUF (arg);
757 break;
758 #if HAVE_WINT_T
759 case TYPE_WIDE_CHAR:
761 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
762 SNPRINTF_BUF (arg);
764 break;
765 #endif
766 case TYPE_STRING:
768 const char *arg = a.arg[dp->arg_index].a.a_string;
769 SNPRINTF_BUF (arg);
771 break;
772 #if HAVE_WCHAR_T
773 case TYPE_WIDE_STRING:
775 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
776 SNPRINTF_BUF (arg);
778 break;
779 #endif
780 case TYPE_POINTER:
782 void *arg = a.arg[dp->arg_index].a.a_pointer;
783 SNPRINTF_BUF (arg);
785 break;
786 default:
787 abort ();
790 #if USE_SNPRINTF
791 /* Portability: Not all implementations of snprintf()
792 are ISO C 99 compliant. Determine the number of
793 bytes that snprintf() has produced or would have
794 produced. */
795 if (count >= 0)
797 /* Verify that snprintf() has NUL-terminated its
798 result. */
799 if (count < maxlen && result[length + count] != '\0')
800 abort ();
801 /* Portability hack. */
802 if (retcount > count)
803 count = retcount;
805 else
807 /* snprintf() doesn't understand the '%n'
808 directive. */
809 if (p[1] != '\0')
811 /* Don't use the '%n' directive; instead, look
812 at the snprintf() return value. */
813 p[1] = '\0';
814 continue;
816 else
818 /* Look at the snprintf() return value. */
819 if (retcount < 0)
821 /* HP-UX 10.20 snprintf() is doubly deficient:
822 It doesn't understand the '%n' directive,
823 *and* it returns -1 (rather than the length
824 that would have been required) when the
825 buffer is too small. */
826 size_t bigger_need =
827 (allocated > 12 ? allocated : 12);
828 ENSURE_ALLOCATION (bigger_need);
829 continue;
831 else
832 count = retcount;
835 #endif
837 /* Attempt to handle failure. */
838 if (count < 0)
840 if (!(result == resultbuf || result == NULL))
841 free (result);
842 if (buf_malloced != NULL)
843 free (buf_malloced);
844 CLEANUP ();
845 errno = EINVAL;
846 return NULL;
849 #if !USE_SNPRINTF
850 if (count >= tmp_length)
851 /* tmp_length was incorrectly calculated - fix the
852 code above! */
853 abort ();
854 #endif
856 /* Make room for the result. */
857 if (count >= maxlen)
859 /* Need at least count bytes. But allocate
860 proportionally, to avoid looping eternally if
861 snprintf() reports a too small count. */
862 ENSURE_ALLOCATION (count < allocated
863 ? allocated : count);
864 #if USE_SNPRINTF
865 continue;
866 #endif
869 #if USE_SNPRINTF
870 /* The snprintf() result did fit. */
871 #else
872 /* Append the sprintf() result. */
873 memcpy (result + length, tmp, count * sizeof (CHAR_T));
874 if (tmp != tmpbuf)
875 free (tmp);
876 #endif
878 length += count;
879 break;
885 /* Add the final NUL. */
886 ENSURE_ALLOCATION (1);
887 result[length] = '\0';
889 if (result != resultbuf && length + 1 < allocated)
891 /* Shrink the allocated memory if possible. */
892 CHAR_T *memory;
894 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
895 if (memory != NULL)
896 result = memory;
899 if (buf_malloced != NULL)
900 free (buf_malloced);
901 CLEANUP ();
902 *lengthp = length;
903 if (length > INT_MAX)
904 goto length_overflow;
905 return result;
907 length_overflow:
908 /* We could produce such a big string, but its length doesn't fit into
909 an 'int'. POSIX says that snprintf() fails with errno = EOVERFLOW in
910 this case. */
911 if (result != resultbuf)
912 free (result);
913 errno = EOVERFLOW;
914 return NULL;
916 out_of_memory:
917 if (!(result == resultbuf || result == NULL))
918 free (result);
919 if (buf_malloced != NULL)
920 free (buf_malloced);
921 out_of_memory_1:
922 CLEANUP ();
923 errno = ENOMEM;
924 return NULL;
928 #undef SNPRINTF
929 #undef USE_SNPRINTF
930 #undef PRINTF_PARSE
931 #undef DIRECTIVES
932 #undef DIRECTIVE
933 #undef CHAR_T
934 #undef VASNPRINTF