Fix estimate of size needed for a 'a' or 'A' conversion.
[gnulib.git] / lib / vasnprintf.c
1 /* vsprintf with automatic memory allocation.
2    Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
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
24
25 #include <config.h>
26 #ifndef IN_LIBINTL
27 # include <alloca.h>
28 #endif
29
30 /* Specification.  */
31 #if WIDE_CHAR_VERSION
32 # include "vasnwprintf.h"
33 #else
34 # include "vasnprintf.h"
35 #endif
36
37 #include <stdio.h>      /* snprintf(), sprintf() */
38 #include <stdlib.h>     /* abort(), malloc(), realloc(), free() */
39 #include <string.h>     /* memcpy(), strlen() */
40 #include <errno.h>      /* errno */
41 #include <limits.h>     /* CHAR_BIT */
42 #include <float.h>      /* DBL_MAX_EXP, LDBL_MAX_EXP */
43 #if WIDE_CHAR_VERSION
44 # include "wprintf-parse.h"
45 #else
46 # include "printf-parse.h"
47 #endif
48
49 /* Checked size_t computations.  */
50 #include "xsize.h"
51
52 #if HAVE_WCHAR_T
53 # if HAVE_WCSLEN
54 #  define local_wcslen wcslen
55 # else
56    /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
57       a dependency towards this library, here is a local substitute.
58       Define this substitute only once, even if this file is included
59       twice in the same compilation unit.  */
60 #  ifndef local_wcslen_defined
61 #   define local_wcslen_defined 1
62 static size_t
63 local_wcslen (const wchar_t *s)
64 {
65   const wchar_t *ptr;
66
67   for (ptr = s; *ptr != (wchar_t) 0; ptr++)
68     ;
69   return ptr - s;
70 }
71 #  endif
72 # endif
73 #endif
74
75 #if WIDE_CHAR_VERSION
76 # define VASNPRINTF vasnwprintf
77 # define CHAR_T wchar_t
78 # define DIRECTIVE wchar_t_directive
79 # define DIRECTIVES wchar_t_directives
80 # define PRINTF_PARSE wprintf_parse
81 # define USE_SNPRINTF 1
82 # if HAVE_DECL__SNWPRINTF
83    /* On Windows, the function swprintf() has a different signature than
84       on Unix; we use the _snwprintf() function instead.  */
85 #  define SNPRINTF _snwprintf
86 # else
87    /* Unix.  */
88 #  define SNPRINTF swprintf
89 # endif
90 #else
91 # define VASNPRINTF vasnprintf
92 # define CHAR_T char
93 # define DIRECTIVE char_directive
94 # define DIRECTIVES char_directives
95 # define PRINTF_PARSE printf_parse
96 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
97 # if HAVE_DECL__SNPRINTF
98    /* Windows.  */
99 #  define SNPRINTF _snprintf
100 # else
101    /* Unix.  */
102 #  define SNPRINTF snprintf
103 # endif
104 #endif
105
106 CHAR_T *
107 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
108 {
109   DIRECTIVES d;
110   arguments a;
111
112   if (PRINTF_PARSE (format, &d, &a) < 0)
113     {
114       errno = EINVAL;
115       return NULL;
116     }
117
118 #define CLEANUP() \
119   free (d.dir);                                                         \
120   if (a.arg)                                                            \
121     free (a.arg);
122
123   if (printf_fetchargs (args, &a) < 0)
124     {
125       CLEANUP ();
126       errno = EINVAL;
127       return NULL;
128     }
129
130   {
131     size_t buf_neededlength;
132     CHAR_T *buf;
133     CHAR_T *buf_malloced;
134     const CHAR_T *cp;
135     size_t i;
136     DIRECTIVE *dp;
137     /* Output string accumulator.  */
138     CHAR_T *result;
139     size_t allocated;
140     size_t length;
141
142     /* Allocate a small buffer that will hold a directive passed to
143        sprintf or snprintf.  */
144     buf_neededlength =
145       xsum4 (7, d.max_width_length, d.max_precision_length, 6);
146 #if HAVE_ALLOCA
147     if (buf_neededlength < 4000 / sizeof (CHAR_T))
148       {
149         buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T));
150         buf_malloced = NULL;
151       }
152     else
153 #endif
154       {
155         size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T));
156         if (size_overflow_p (buf_memsize))
157           goto out_of_memory_1;
158         buf = (CHAR_T *) malloc (buf_memsize);
159         if (buf == NULL)
160           goto out_of_memory_1;
161         buf_malloced = buf;
162       }
163
164     if (resultbuf != NULL)
165       {
166         result = resultbuf;
167         allocated = *lengthp;
168       }
169     else
170       {
171         result = NULL;
172         allocated = 0;
173       }
174     length = 0;
175     /* Invariants:
176        result is either == resultbuf or == NULL or malloc-allocated.
177        If length > 0, then result != NULL.  */
178
179     /* Ensures that allocated >= needed.  Aborts through a jump to
180        out_of_memory if needed is SIZE_MAX or otherwise too big.  */
181 #define ENSURE_ALLOCATION(needed) \
182     if ((needed) > allocated)                                                \
183       {                                                                      \
184         size_t memory_size;                                                  \
185         CHAR_T *memory;                                                      \
186                                                                              \
187         allocated = (allocated > 0 ? xtimes (allocated, 2) : 12);            \
188         if ((needed) > allocated)                                            \
189           allocated = (needed);                                              \
190         memory_size = xtimes (allocated, sizeof (CHAR_T));                   \
191         if (size_overflow_p (memory_size))                                   \
192           goto out_of_memory;                                                \
193         if (result == resultbuf || result == NULL)                           \
194           memory = (CHAR_T *) malloc (memory_size);                          \
195         else                                                                 \
196           memory = (CHAR_T *) realloc (result, memory_size);                 \
197         if (memory == NULL)                                                  \
198           goto out_of_memory;                                                \
199         if (result == resultbuf && length > 0)                               \
200           memcpy (memory, result, length * sizeof (CHAR_T));                 \
201         result = memory;                                                     \
202       }
203
204     for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
205       {
206         if (cp != dp->dir_start)
207           {
208             size_t n = dp->dir_start - cp;
209             size_t augmented_length = xsum (length, n);
210
211             ENSURE_ALLOCATION (augmented_length);
212             memcpy (result + length, cp, n * sizeof (CHAR_T));
213             length = augmented_length;
214           }
215         if (i == d.count)
216           break;
217
218         /* Execute a single directive.  */
219         if (dp->conversion == '%')
220           {
221             size_t augmented_length;
222
223             if (!(dp->arg_index == ARG_NONE))
224               abort ();
225             augmented_length = xsum (length, 1);
226             ENSURE_ALLOCATION (augmented_length);
227             result[length] = '%';
228             length = augmented_length;
229           }
230         else
231           {
232             if (!(dp->arg_index != ARG_NONE))
233               abort ();
234
235             if (dp->conversion == 'n')
236               {
237                 switch (a.arg[dp->arg_index].type)
238                   {
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 ();
258                   }
259               }
260             else
261               {
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;
270
271                 /* Allocate a temporary buffer of sufficient size for calling
272                    sprintf.  */
273                 {
274                   size_t width;
275                   size_t precision;
276
277                   width = 0;
278                   if (dp->width_start != dp->width_end)
279                     {
280                       if (dp->width_arg_index != ARG_NONE)
281                         {
282                           int arg;
283
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);
288                         }
289                       else
290                         {
291                           const CHAR_T *digitp = dp->width_start;
292
293                           do
294                             width = xsum (xtimes (width, 10), *digitp++ - '0');
295                           while (digitp != dp->width_end);
296                         }
297                     }
298
299                   precision = 6;
300                   if (dp->precision_start != dp->precision_end)
301                     {
302                       if (dp->precision_arg_index != ARG_NONE)
303                         {
304                           int arg;
305
306                           if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
307                             abort ();
308                           arg = a.arg[dp->precision_arg_index].a.a_int;
309                           precision = (arg < 0 ? 0 : arg);
310                         }
311                       else
312                         {
313                           const CHAR_T *digitp = dp->precision_start + 1;
314
315                           precision = 0;
316                           while (digitp != dp->precision_end)
317                             precision = xsum (xtimes (precision, 10), *digitp++ - '0');
318                         }
319                     }
320
321                   switch (dp->conversion)
322                     {
323
324                     case 'd': case 'i': case 'u':
325 # if HAVE_LONG_LONG_INT
326                       if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
327                         tmp_length =
328                           (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
329                                           * 0.30103 /* binary -> decimal */
330                                          )
331                           + 1; /* turn floor into ceil */
332                       else
333 # endif
334                       if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
335                         tmp_length =
336                           (unsigned int) (sizeof (unsigned long) * CHAR_BIT
337                                           * 0.30103 /* binary -> decimal */
338                                          )
339                           + 1; /* turn floor into ceil */
340                       else
341                         tmp_length =
342                           (unsigned int) (sizeof (unsigned int) * CHAR_BIT
343                                           * 0.30103 /* binary -> decimal */
344                                          )
345                           + 1; /* turn floor into ceil */
346                       if (tmp_length < precision)
347                         tmp_length = precision;
348                       /* Multiply by 2, as an estimate for FLAG_GROUP.  */
349                       tmp_length = xsum (tmp_length, tmp_length);
350                       /* Add 1, to account for a leading sign.  */
351                       tmp_length = xsum (tmp_length, 1);
352                       break;
353
354                     case 'o':
355 # if HAVE_LONG_LONG_INT
356                       if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
357                         tmp_length =
358                           (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
359                                           * 0.333334 /* binary -> octal */
360                                          )
361                           + 1; /* turn floor into ceil */
362                       else
363 # endif
364                       if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
365                         tmp_length =
366                           (unsigned int) (sizeof (unsigned long) * CHAR_BIT
367                                           * 0.333334 /* binary -> octal */
368                                          )
369                           + 1; /* turn floor into ceil */
370                       else
371                         tmp_length =
372                           (unsigned int) (sizeof (unsigned int) * CHAR_BIT
373                                           * 0.333334 /* binary -> octal */
374                                          )
375                           + 1; /* turn floor into ceil */
376                       if (tmp_length < precision)
377                         tmp_length = precision;
378                       /* Add 1, to account for a leading sign.  */
379                       tmp_length = xsum (tmp_length, 1);
380                       break;
381
382                     case 'x': case 'X':
383 # if HAVE_LONG_LONG_INT
384                       if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
385                         tmp_length =
386                           (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
387                                           * 0.25 /* binary -> hexadecimal */
388                                          )
389                           + 1; /* turn floor into ceil */
390                       else
391 # endif
392                       if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
393                         tmp_length =
394                           (unsigned int) (sizeof (unsigned long) * CHAR_BIT
395                                           * 0.25 /* binary -> hexadecimal */
396                                          )
397                           + 1; /* turn floor into ceil */
398                       else
399                         tmp_length =
400                           (unsigned int) (sizeof (unsigned int) * CHAR_BIT
401                                           * 0.25 /* binary -> hexadecimal */
402                                          )
403                           + 1; /* turn floor into ceil */
404                       if (tmp_length < precision)
405                         tmp_length = precision;
406                       /* Add 2, to account for a leading sign or alternate form.  */
407                       tmp_length = xsum (tmp_length, 2);
408                       break;
409
410                     case 'f': case 'F':
411 # if HAVE_LONG_DOUBLE
412                       if (type == TYPE_LONGDOUBLE)
413                         tmp_length =
414                           (unsigned int) (LDBL_MAX_EXP
415                                           * 0.30103 /* binary -> decimal */
416                                           * 2 /* estimate for FLAG_GROUP */
417                                          )
418                           + 1 /* turn floor into ceil */
419                           + 10; /* sign, decimal point etc. */
420                       else
421 # endif
422                         tmp_length =
423                           (unsigned int) (DBL_MAX_EXP
424                                           * 0.30103 /* binary -> decimal */
425                                           * 2 /* estimate for FLAG_GROUP */
426                                          )
427                           + 1 /* turn floor into ceil */
428                           + 10; /* sign, decimal point etc. */
429                       tmp_length = xsum (tmp_length, precision);
430                       break;
431
432                     case 'e': case 'E': case 'g': case 'G':
433                       tmp_length =
434                         12; /* sign, decimal point, exponent etc. */
435                       tmp_length = xsum (tmp_length, precision);
436                       break;
437
438                     case 'a': case 'A':
439 # if HAVE_LONG_DOUBLE
440                       if (type == TYPE_LONGDOUBLE)
441                         tmp_length =
442                           (unsigned int) (LDBL_DIG
443                                           * 0.831 /* decimal -> hexadecimal */
444                                          )
445                           + 1; /* turn floor into ceil */
446                       else
447 # endif
448                         tmp_length =
449                           (unsigned int) (DBL_DIG
450                                           * 0.831 /* decimal -> hexadecimal */
451                                          )
452                           + 1; /* turn floor into ceil */
453                       if (tmp_length < precision)
454                         tmp_length = precision;
455                       /* Account for sign, decimal point etc. */
456                       tmp_length = xsum (tmp_length, 12);
457                       break;
458
459                     case 'c':
460 # if HAVE_WINT_T && !WIDE_CHAR_VERSION
461                       if (type == TYPE_WIDE_CHAR)
462                         tmp_length = MB_CUR_MAX;
463                       else
464 # endif
465                         tmp_length = 1;
466                       break;
467
468                     case 's':
469 # if HAVE_WCHAR_T
470                       if (type == TYPE_WIDE_STRING)
471                         {
472                           tmp_length =
473                             local_wcslen (a.arg[dp->arg_index].a.a_wide_string);
474
475 #  if !WIDE_CHAR_VERSION
476                           tmp_length = xtimes (tmp_length, MB_CUR_MAX);
477 #  endif
478                         }
479                       else
480 # endif
481                         tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
482                       break;
483
484                     case 'p':
485                       tmp_length =
486                         (unsigned int) (sizeof (void *) * CHAR_BIT
487                                         * 0.25 /* binary -> hexadecimal */
488                                        )
489                           + 1 /* turn floor into ceil */
490                           + 2; /* account for leading 0x */
491                       break;
492
493                     default:
494                       abort ();
495                     }
496
497                   if (tmp_length < width)
498                     tmp_length = width;
499
500                   tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
501                 }
502
503                 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T))
504                   tmp = tmpbuf;
505                 else
506                   {
507                     size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T));
508
509                     if (size_overflow_p (tmp_memsize))
510                       /* Overflow, would lead to out of memory.  */
511                       goto out_of_memory;
512                     tmp = (CHAR_T *) malloc (tmp_memsize);
513                     if (tmp == NULL)
514                       /* Out of memory.  */
515                       goto out_of_memory;
516                   }
517 #endif
518
519                 /* Construct the format string for calling snprintf or
520                    sprintf.  */
521                 p = buf;
522                 *p++ = '%';
523                 if (dp->flags & FLAG_GROUP)
524                   *p++ = '\'';
525                 if (dp->flags & FLAG_LEFT)
526                   *p++ = '-';
527                 if (dp->flags & FLAG_SHOWSIGN)
528                   *p++ = '+';
529                 if (dp->flags & FLAG_SPACE)
530                   *p++ = ' ';
531                 if (dp->flags & FLAG_ALT)
532                   *p++ = '#';
533                 if (dp->flags & FLAG_ZERO)
534                   *p++ = '0';
535                 if (dp->width_start != dp->width_end)
536                   {
537                     size_t n = dp->width_end - dp->width_start;
538                     memcpy (p, dp->width_start, n * sizeof (CHAR_T));
539                     p += n;
540                   }
541                 if (dp->precision_start != dp->precision_end)
542                   {
543                     size_t n = dp->precision_end - dp->precision_start;
544                     memcpy (p, dp->precision_start, n * sizeof (CHAR_T));
545                     p += n;
546                   }
547
548                 switch (type)
549                   {
550 #if HAVE_LONG_LONG_INT
551                   case TYPE_LONGLONGINT:
552                   case TYPE_ULONGLONGINT:
553                     *p++ = 'l';
554                     /*FALLTHROUGH*/
555 #endif
556                   case TYPE_LONGINT:
557                   case TYPE_ULONGINT:
558 #if HAVE_WINT_T
559                   case TYPE_WIDE_CHAR:
560 #endif
561 #if HAVE_WCHAR_T
562                   case TYPE_WIDE_STRING:
563 #endif
564                     *p++ = 'l';
565                     break;
566 #if HAVE_LONG_DOUBLE
567                   case TYPE_LONGDOUBLE:
568                     *p++ = 'L';
569                     break;
570 #endif
571                   default:
572                     break;
573                   }
574                 *p = dp->conversion;
575 #if USE_SNPRINTF
576                 p[1] = '%';
577                 p[2] = 'n';
578                 p[3] = '\0';
579 #else
580                 p[1] = '\0';
581 #endif
582
583                 /* Construct the arguments for calling snprintf or sprintf.  */
584                 prefix_count = 0;
585                 if (dp->width_arg_index != ARG_NONE)
586                   {
587                     if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
588                       abort ();
589                     prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
590                   }
591                 if (dp->precision_arg_index != ARG_NONE)
592                   {
593                     if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
594                       abort ();
595                     prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
596                   }
597
598 #if USE_SNPRINTF
599                 /* Prepare checking whether snprintf returns the count
600                    via %n.  */
601                 ENSURE_ALLOCATION (xsum (length, 1));
602                 result[length] = '\0';
603 #endif
604
605                 for (;;)
606                   {
607                     size_t maxlen;
608                     int count;
609                     int retcount;
610
611                     maxlen = allocated - length;
612                     count = -1;
613                     retcount = 0;
614
615 #if USE_SNPRINTF
616 # define SNPRINTF_BUF(arg) \
617                     switch (prefix_count)                                   \
618                       {                                                     \
619                       case 0:                                               \
620                         retcount = SNPRINTF (result + length, maxlen, buf,  \
621                                              arg, &count);                  \
622                         break;                                              \
623                       case 1:                                               \
624                         retcount = SNPRINTF (result + length, maxlen, buf,  \
625                                              prefixes[0], arg, &count);     \
626                         break;                                              \
627                       case 2:                                               \
628                         retcount = SNPRINTF (result + length, maxlen, buf,  \
629                                              prefixes[0], prefixes[1], arg, \
630                                              &count);                       \
631                         break;                                              \
632                       default:                                              \
633                         abort ();                                           \
634                       }
635 #else
636 # define SNPRINTF_BUF(arg) \
637                     switch (prefix_count)                                   \
638                       {                                                     \
639                       case 0:                                               \
640                         count = sprintf (tmp, buf, arg);                    \
641                         break;                                              \
642                       case 1:                                               \
643                         count = sprintf (tmp, buf, prefixes[0], arg);       \
644                         break;                                              \
645                       case 2:                                               \
646                         count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
647                                          arg);                              \
648                         break;                                              \
649                       default:                                              \
650                         abort ();                                           \
651                       }
652 #endif
653
654                     switch (type)
655                       {
656                       case TYPE_SCHAR:
657                         {
658                           int arg = a.arg[dp->arg_index].a.a_schar;
659                           SNPRINTF_BUF (arg);
660                         }
661                         break;
662                       case TYPE_UCHAR:
663                         {
664                           unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
665                           SNPRINTF_BUF (arg);
666                         }
667                         break;
668                       case TYPE_SHORT:
669                         {
670                           int arg = a.arg[dp->arg_index].a.a_short;
671                           SNPRINTF_BUF (arg);
672                         }
673                         break;
674                       case TYPE_USHORT:
675                         {
676                           unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
677                           SNPRINTF_BUF (arg);
678                         }
679                         break;
680                       case TYPE_INT:
681                         {
682                           int arg = a.arg[dp->arg_index].a.a_int;
683                           SNPRINTF_BUF (arg);
684                         }
685                         break;
686                       case TYPE_UINT:
687                         {
688                           unsigned int arg = a.arg[dp->arg_index].a.a_uint;
689                           SNPRINTF_BUF (arg);
690                         }
691                         break;
692                       case TYPE_LONGINT:
693                         {
694                           long int arg = a.arg[dp->arg_index].a.a_longint;
695                           SNPRINTF_BUF (arg);
696                         }
697                         break;
698                       case TYPE_ULONGINT:
699                         {
700                           unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
701                           SNPRINTF_BUF (arg);
702                         }
703                         break;
704 #if HAVE_LONG_LONG_INT
705                       case TYPE_LONGLONGINT:
706                         {
707                           long long int arg = a.arg[dp->arg_index].a.a_longlongint;
708                           SNPRINTF_BUF (arg);
709                         }
710                         break;
711                       case TYPE_ULONGLONGINT:
712                         {
713                           unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
714                           SNPRINTF_BUF (arg);
715                         }
716                         break;
717 #endif
718                       case TYPE_DOUBLE:
719                         {
720                           double arg = a.arg[dp->arg_index].a.a_double;
721                           SNPRINTF_BUF (arg);
722                         }
723                         break;
724 #if HAVE_LONG_DOUBLE
725                       case TYPE_LONGDOUBLE:
726                         {
727                           long double arg = a.arg[dp->arg_index].a.a_longdouble;
728                           SNPRINTF_BUF (arg);
729                         }
730                         break;
731 #endif
732                       case TYPE_CHAR:
733                         {
734                           int arg = a.arg[dp->arg_index].a.a_char;
735                           SNPRINTF_BUF (arg);
736                         }
737                         break;
738 #if HAVE_WINT_T
739                       case TYPE_WIDE_CHAR:
740                         {
741                           wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
742                           SNPRINTF_BUF (arg);
743                         }
744                         break;
745 #endif
746                       case TYPE_STRING:
747                         {
748                           const char *arg = a.arg[dp->arg_index].a.a_string;
749                           SNPRINTF_BUF (arg);
750                         }
751                         break;
752 #if HAVE_WCHAR_T
753                       case TYPE_WIDE_STRING:
754                         {
755                           const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
756                           SNPRINTF_BUF (arg);
757                         }
758                         break;
759 #endif
760                       case TYPE_POINTER:
761                         {
762                           void *arg = a.arg[dp->arg_index].a.a_pointer;
763                           SNPRINTF_BUF (arg);
764                         }
765                         break;
766                       default:
767                         abort ();
768                       }
769
770 #if USE_SNPRINTF
771                     /* Portability: Not all implementations of snprintf()
772                        are ISO C 99 compliant.  Determine the number of
773                        bytes that snprintf() has produced or would have
774                        produced.  */
775                     if (count >= 0)
776                       {
777                         /* Verify that snprintf() has NUL-terminated its
778                            result.  */
779                         if (count < maxlen && result[length + count] != '\0')
780                           abort ();
781                         /* Portability hack.  */
782                         if (retcount > count)
783                           count = retcount;
784                       }
785                     else
786                       {
787                         /* snprintf() doesn't understand the '%n'
788                            directive.  */
789                         if (p[1] != '\0')
790                           {
791                             /* Don't use the '%n' directive; instead, look
792                                at the snprintf() return value.  */
793                             p[1] = '\0';
794                             continue;
795                           }
796                         else
797                           {
798                             /* Look at the snprintf() return value.  */
799                             if (retcount < 0)
800                               {
801                                 /* HP-UX 10.20 snprintf() is doubly deficient:
802                                    It doesn't understand the '%n' directive,
803                                    *and* it returns -1 (rather than the length
804                                    that would have been required) when the
805                                    buffer is too small.  */
806                                 size_t bigger_need =
807                                   xsum (xtimes (allocated, 2), 12);
808                                 ENSURE_ALLOCATION (bigger_need);
809                                 continue;
810                               }
811                             else
812                               count = retcount;
813                           }
814                       }
815 #endif
816
817                     /* Attempt to handle failure.  */
818                     if (count < 0)
819                       {
820                         if (!(result == resultbuf || result == NULL))
821                           free (result);
822                         if (buf_malloced != NULL)
823                           free (buf_malloced);
824                         CLEANUP ();
825                         errno = EINVAL;
826                         return NULL;
827                       }
828
829 #if !USE_SNPRINTF
830                     if (count >= tmp_length)
831                       /* tmp_length was incorrectly calculated - fix the
832                          code above!  */
833                       abort ();
834 #endif
835
836                     /* Make room for the result.  */
837                     if (count >= maxlen)
838                       {
839                         /* Need at least count bytes.  But allocate
840                            proportionally, to avoid looping eternally if
841                            snprintf() reports a too small count.  */
842                         size_t n =
843                           xmax (xsum (length, count), xtimes (allocated, 2));
844
845                         ENSURE_ALLOCATION (n);
846 #if USE_SNPRINTF
847                         continue;
848 #endif
849                       }
850
851 #if USE_SNPRINTF
852                     /* The snprintf() result did fit.  */
853 #else
854                     /* Append the sprintf() result.  */
855                     memcpy (result + length, tmp, count * sizeof (CHAR_T));
856                     if (tmp != tmpbuf)
857                       free (tmp);
858 #endif
859
860                     length += count;
861                     break;
862                   }
863               }
864           }
865       }
866
867     /* Add the final NUL.  */
868     ENSURE_ALLOCATION (xsum (length, 1));
869     result[length] = '\0';
870
871     if (result != resultbuf && length + 1 < allocated)
872       {
873         /* Shrink the allocated memory if possible.  */
874         CHAR_T *memory;
875
876         memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T));
877         if (memory != NULL)
878           result = memory;
879       }
880
881     if (buf_malloced != NULL)
882       free (buf_malloced);
883     CLEANUP ();
884     *lengthp = length;
885     /* Note that we can produce a big string of a length > INT_MAX.  POSIX
886        says that snprintf() fails with errno = EOVERFLOW in this case, but
887        that's only because snprintf() returns an 'int'.  This function does
888        not have this limitation.  */
889     return result;
890
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;
900   }
901 }
902
903 #undef SNPRINTF
904 #undef USE_SNPRINTF
905 #undef PRINTF_PARSE
906 #undef DIRECTIVES
907 #undef DIRECTIVE
908 #undef CHAR_T
909 #undef VASNPRINTF