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