Fix.
[shishi.git] / gl / vasnprintf.c
blob8eb950551d0206fca5f417ae0c7f365f879782ae
1 /* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
19 This must come before <config.h> because <config.h> may include
20 <features.h>, and once <features.h> has been included, it's too late. */
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE 1
23 #endif
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 #include <alloca.h>
30 /* Specification. */
31 #include "vasnprintf.h"
33 #include <stdio.h> /* snprintf(), sprintf() */
34 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
35 #include <string.h> /* memcpy(), strlen() */
36 #include <errno.h> /* errno */
37 #include <limits.h> /* CHAR_BIT */
38 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
39 #include "printf-parse.h"
41 /* For those losing systems which don't have 'alloca' we have to add
42 some additional code emulating it. */
43 #ifdef HAVE_ALLOCA
44 # define freea(p) /* nothing */
45 #else
46 # define alloca(n) malloc (n)
47 # define freea(p) free (p)
48 #endif
50 #ifdef HAVE_WCHAR_T
51 # ifdef HAVE_WCSLEN
52 # define local_wcslen wcslen
53 # else
54 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
55 a dependency towards this library, here is a local substitute. */
56 static size_t
57 local_wcslen (const wchar_t *s)
59 const wchar_t *ptr;
61 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
63 return ptr - s;
65 # endif
66 #endif
68 char *
69 vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
71 char_directives d;
72 arguments a;
74 if (printf_parse (format, &d, &a) < 0)
76 errno = EINVAL;
77 return NULL;
80 #define CLEANUP() \
81 free (d.dir); \
82 if (a.arg) \
83 free (a.arg);
85 if (printf_fetchargs (args, &a) < 0)
87 CLEANUP ();
88 errno = EINVAL;
89 return NULL;
93 char *buf =
94 (char *) alloca (7 + d.max_width_length + d.max_precision_length + 6);
95 const char *cp;
96 unsigned int i;
97 char_directive *dp;
98 /* Output string accumulator. */
99 char *result;
100 size_t allocated;
101 size_t length;
103 if (resultbuf != NULL)
105 result = resultbuf;
106 allocated = *lengthp;
108 else
110 result = NULL;
111 allocated = 0;
113 length = 0;
114 /* Invariants:
115 result is either == resultbuf or == NULL or malloc-allocated.
116 If length > 0, then result != NULL. */
118 #define ENSURE_ALLOCATION(needed) \
119 if ((needed) > allocated) \
121 char *memory; \
123 allocated = (allocated > 0 ? 2 * allocated : 12); \
124 if ((needed) > allocated) \
125 allocated = (needed); \
126 if (result == resultbuf || result == NULL) \
127 memory = (char *) malloc (allocated); \
128 else \
129 memory = (char *) realloc (result, allocated); \
131 if (memory == NULL) \
133 if (!(result == resultbuf || result == NULL)) \
134 free (result); \
135 freea (buf); \
136 CLEANUP (); \
137 errno = ENOMEM; \
138 return NULL; \
140 if (result == resultbuf && length > 0) \
141 memcpy (memory, result, length); \
142 result = memory; \
145 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
147 if (cp != dp->dir_start)
149 size_t n = dp->dir_start - cp;
151 ENSURE_ALLOCATION (length + n);
152 memcpy (result + length, cp, n);
153 length += n;
155 if (i == d.count)
156 break;
158 /* Execute a single directive. */
159 if (dp->conversion == '%')
161 if (!(dp->arg_index < 0))
162 abort ();
163 ENSURE_ALLOCATION (length + 1);
164 result[length] = '%';
165 length += 1;
167 else
169 if (!(dp->arg_index >= 0))
170 abort ();
172 if (dp->conversion == 'n')
174 switch (a.arg[dp->arg_index].type)
176 case TYPE_COUNT_SCHAR_POINTER:
177 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
178 break;
179 case TYPE_COUNT_SHORT_POINTER:
180 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
181 break;
182 case TYPE_COUNT_INT_POINTER:
183 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
184 break;
185 case TYPE_COUNT_LONGINT_POINTER:
186 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
187 break;
188 #ifdef HAVE_LONG_LONG
189 case TYPE_COUNT_LONGLONGINT_POINTER:
190 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
191 break;
192 #endif
193 default:
194 abort ();
197 else
199 arg_type type = a.arg[dp->arg_index].type;
200 char *p;
201 unsigned int prefix_count;
202 int prefixes[2];
203 #if !HAVE_SNPRINTF
204 unsigned int tmp_length;
205 char tmpbuf[700];
206 char *tmp;
208 /* Allocate a temporary buffer of sufficient size for calling
209 sprintf. */
211 unsigned int width;
212 unsigned int precision;
214 width = 0;
215 if (dp->width_start != dp->width_end)
217 if (dp->width_arg_index >= 0)
219 int arg;
221 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
222 abort ();
223 arg = a.arg[dp->width_arg_index].a.a_int;
224 width = (arg < 0 ? -arg : arg);
226 else
228 const char *digitp = dp->width_start;
231 width = width * 10 + (*digitp++ - '0');
232 while (digitp != dp->width_end);
236 precision = 6;
237 if (dp->precision_start != dp->precision_end)
239 if (dp->precision_arg_index >= 0)
241 int arg;
243 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
244 abort ();
245 arg = a.arg[dp->precision_arg_index].a.a_int;
246 precision = (arg < 0 ? 0 : arg);
248 else
250 const char *digitp = dp->precision_start + 1;
252 precision = 0;
254 precision = precision * 10 + (*digitp++ - '0');
255 while (digitp != dp->precision_end);
259 switch (dp->conversion)
262 case 'd': case 'i': case 'u':
263 # ifdef HAVE_LONG_LONG
264 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
265 tmp_length =
266 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
267 * 0.30103 /* binary -> decimal */
268 * 2 /* estimate for FLAG_GROUP */
270 + 1 /* turn floor into ceil */
271 + 1; /* account for leading sign */
272 else
273 # endif
274 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
275 tmp_length =
276 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
277 * 0.30103 /* binary -> decimal */
278 * 2 /* estimate for FLAG_GROUP */
280 + 1 /* turn floor into ceil */
281 + 1; /* account for leading sign */
282 else
283 tmp_length =
284 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
285 * 0.30103 /* binary -> decimal */
286 * 2 /* estimate for FLAG_GROUP */
288 + 1 /* turn floor into ceil */
289 + 1; /* account for leading sign */
290 break;
292 case 'o':
293 # ifdef HAVE_LONG_LONG
294 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
295 tmp_length =
296 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
297 * 0.333334 /* binary -> octal */
299 + 1 /* turn floor into ceil */
300 + 1; /* account for leading sign */
301 else
302 # endif
303 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
304 tmp_length =
305 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
306 * 0.333334 /* binary -> octal */
308 + 1 /* turn floor into ceil */
309 + 1; /* account for leading sign */
310 else
311 tmp_length =
312 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
313 * 0.333334 /* binary -> octal */
315 + 1 /* turn floor into ceil */
316 + 1; /* account for leading sign */
317 break;
319 case 'x': case 'X':
320 # ifdef HAVE_LONG_LONG
321 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
322 tmp_length =
323 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
324 * 0.25 /* binary -> hexadecimal */
326 + 1 /* turn floor into ceil */
327 + 2; /* account for leading sign or alternate form */
328 else
329 # endif
330 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
331 tmp_length =
332 (unsigned int) (sizeof (unsigned long) * CHAR_BIT
333 * 0.25 /* binary -> hexadecimal */
335 + 1 /* turn floor into ceil */
336 + 2; /* account for leading sign or alternate form */
337 else
338 tmp_length =
339 (unsigned int) (sizeof (unsigned int) * CHAR_BIT
340 * 0.25 /* binary -> hexadecimal */
342 + 1 /* turn floor into ceil */
343 + 2; /* account for leading sign or alternate form */
344 break;
346 case 'f': case 'F':
347 # ifdef HAVE_LONG_DOUBLE
348 if (type == TYPE_LONGDOUBLE)
349 tmp_length =
350 (unsigned int) (LDBL_MAX_EXP
351 * 0.30103 /* binary -> decimal */
352 * 2 /* estimate for FLAG_GROUP */
354 + 1 /* turn floor into ceil */
355 + precision
356 + 10; /* sign, decimal point etc. */
357 else
358 # endif
359 tmp_length =
360 (unsigned int) (DBL_MAX_EXP
361 * 0.30103 /* binary -> decimal */
362 * 2 /* estimate for FLAG_GROUP */
364 + 1 /* turn floor into ceil */
365 + precision
366 + 10; /* sign, decimal point etc. */
367 break;
369 case 'e': case 'E': case 'g': case 'G':
370 case 'a': case 'A':
371 tmp_length =
372 precision
373 + 12; /* sign, decimal point, exponent etc. */
374 break;
376 case 'c':
377 # ifdef HAVE_WINT_T
378 if (type == TYPE_WIDE_CHAR)
379 tmp_length = MB_CUR_MAX;
380 else
381 # endif
382 tmp_length = 1;
383 break;
385 case 's':
386 # ifdef HAVE_WCHAR_T
387 if (type == TYPE_WIDE_STRING)
388 tmp_length =
389 local_wcslen (a.arg[dp->arg_index].a.a_wide_string)
390 * MB_CUR_MAX;
391 else
392 # endif
393 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
394 break;
396 case 'p':
397 tmp_length =
398 (unsigned int) (sizeof (void *) * CHAR_BIT
399 * 0.25 /* binary -> hexadecimal */
401 + 1 /* turn floor into ceil */
402 + 2; /* account for leading 0x */
403 break;
405 default:
406 abort ();
409 if (tmp_length < width)
410 tmp_length = width;
412 tmp_length++; /* account for trailing NUL */
415 if (tmp_length <= sizeof (tmpbuf))
416 tmp = tmpbuf;
417 else
419 tmp = (char *) malloc (tmp_length);
420 if (tmp == NULL)
422 /* Out of memory. */
423 if (!(result == resultbuf || result == NULL))
424 free (result);
425 freea (buf);
426 CLEANUP ();
427 errno = ENOMEM;
428 return NULL;
431 #endif
433 /* Construct the format string for calling snprintf or
434 sprintf. */
435 p = buf;
436 *p++ = '%';
437 if (dp->flags & FLAG_GROUP)
438 *p++ = '\'';
439 if (dp->flags & FLAG_LEFT)
440 *p++ = '-';
441 if (dp->flags & FLAG_SHOWSIGN)
442 *p++ = '+';
443 if (dp->flags & FLAG_SPACE)
444 *p++ = ' ';
445 if (dp->flags & FLAG_ALT)
446 *p++ = '#';
447 if (dp->flags & FLAG_ZERO)
448 *p++ = '0';
449 if (dp->width_start != dp->width_end)
451 size_t n = dp->width_end - dp->width_start;
452 memcpy (p, dp->width_start, n);
453 p += n;
455 if (dp->precision_start != dp->precision_end)
457 size_t n = dp->precision_end - dp->precision_start;
458 memcpy (p, dp->precision_start, n);
459 p += n;
462 switch (type)
464 #ifdef HAVE_LONG_LONG
465 case TYPE_LONGLONGINT:
466 case TYPE_ULONGLONGINT:
467 *p++ = 'l';
468 /*FALLTHROUGH*/
469 #endif
470 case TYPE_LONGINT:
471 case TYPE_ULONGINT:
472 #ifdef HAVE_WINT_T
473 case TYPE_WIDE_CHAR:
474 #endif
475 #ifdef HAVE_WCHAR_T
476 case TYPE_WIDE_STRING:
477 #endif
478 *p++ = 'l';
479 break;
480 #ifdef HAVE_LONG_DOUBLE
481 case TYPE_LONGDOUBLE:
482 *p++ = 'L';
483 break;
484 #endif
485 default:
486 break;
488 *p = dp->conversion;
489 #if HAVE_SNPRINTF
490 p[1] = '%';
491 p[2] = 'n';
492 p[3] = '\0';
493 #else
494 p[1] = '\0';
495 #endif
497 /* Construct the arguments for calling snprintf or sprintf. */
498 prefix_count = 0;
499 if (dp->width_arg_index >= 0)
501 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
502 abort ();
503 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
505 if (dp->precision_arg_index >= 0)
507 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
508 abort ();
509 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
512 #if HAVE_SNPRINTF
513 /* Prepare checking whether snprintf returns the count
514 via %n. */
515 ENSURE_ALLOCATION (length + 1);
516 result[length] = '\0';
517 #endif
519 for (;;)
521 size_t maxlen;
522 int count;
523 int retcount;
525 maxlen = allocated - length;
526 count = -1;
527 retcount = 0;
529 #if HAVE_SNPRINTF
530 #define SNPRINTF_BUF(arg) \
531 switch (prefix_count) \
533 case 0: \
534 retcount = snprintf (result + length, maxlen, buf, \
535 arg, &count); \
536 break; \
537 case 1: \
538 retcount = snprintf (result + length, maxlen, buf, \
539 prefixes[0], arg, &count); \
540 break; \
541 case 2: \
542 retcount = snprintf (result + length, maxlen, buf, \
543 prefixes[0], prefixes[1], arg, \
544 &count); \
545 break; \
546 default: \
547 abort (); \
549 #else
550 #define SNPRINTF_BUF(arg) \
551 switch (prefix_count) \
553 case 0: \
554 count = sprintf (tmp, buf, arg); \
555 break; \
556 case 1: \
557 count = sprintf (tmp, buf, prefixes[0], arg); \
558 break; \
559 case 2: \
560 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
561 arg); \
562 break; \
563 default: \
564 abort (); \
566 #endif
568 switch (type)
570 case TYPE_SCHAR:
572 int arg = a.arg[dp->arg_index].a.a_schar;
573 SNPRINTF_BUF (arg);
575 break;
576 case TYPE_UCHAR:
578 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
579 SNPRINTF_BUF (arg);
581 break;
582 case TYPE_SHORT:
584 int arg = a.arg[dp->arg_index].a.a_short;
585 SNPRINTF_BUF (arg);
587 break;
588 case TYPE_USHORT:
590 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
591 SNPRINTF_BUF (arg);
593 break;
594 case TYPE_INT:
596 int arg = a.arg[dp->arg_index].a.a_int;
597 SNPRINTF_BUF (arg);
599 break;
600 case TYPE_UINT:
602 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
603 SNPRINTF_BUF (arg);
605 break;
606 case TYPE_LONGINT:
608 long int arg = a.arg[dp->arg_index].a.a_longint;
609 SNPRINTF_BUF (arg);
611 break;
612 case TYPE_ULONGINT:
614 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
615 SNPRINTF_BUF (arg);
617 break;
618 #ifdef HAVE_LONG_LONG
619 case TYPE_LONGLONGINT:
621 long long int arg = a.arg[dp->arg_index].a.a_longlongint;
622 SNPRINTF_BUF (arg);
624 break;
625 case TYPE_ULONGLONGINT:
627 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
628 SNPRINTF_BUF (arg);
630 break;
631 #endif
632 case TYPE_DOUBLE:
634 double arg = a.arg[dp->arg_index].a.a_double;
635 SNPRINTF_BUF (arg);
637 break;
638 #ifdef HAVE_LONG_DOUBLE
639 case TYPE_LONGDOUBLE:
641 long double arg = a.arg[dp->arg_index].a.a_longdouble;
642 SNPRINTF_BUF (arg);
644 break;
645 #endif
646 case TYPE_CHAR:
648 int arg = a.arg[dp->arg_index].a.a_char;
649 SNPRINTF_BUF (arg);
651 break;
652 #ifdef HAVE_WINT_T
653 case TYPE_WIDE_CHAR:
655 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
656 SNPRINTF_BUF (arg);
658 break;
659 #endif
660 case TYPE_STRING:
662 const char *arg = a.arg[dp->arg_index].a.a_string;
663 SNPRINTF_BUF (arg);
665 break;
666 #ifdef HAVE_WCHAR_T
667 case TYPE_WIDE_STRING:
669 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
670 SNPRINTF_BUF (arg);
672 break;
673 #endif
674 case TYPE_POINTER:
676 void *arg = a.arg[dp->arg_index].a.a_pointer;
677 SNPRINTF_BUF (arg);
679 break;
680 default:
681 abort ();
684 #if HAVE_SNPRINTF
685 /* Portability: Not all implementations of snprintf()
686 are ISO C 99 compliant. Determine the number of
687 bytes that snprintf() has produced or would have
688 produced. */
689 if (count >= 0)
691 /* Verify that snprintf() has NUL-terminated its
692 result. */
693 if (count < maxlen && result[length + count] != '\0')
694 abort ();
695 /* Portability hack. */
696 if (retcount > count)
697 count = retcount;
699 else
701 /* snprintf() doesn't understand the '%n'
702 directive. */
703 if (p[1] != '\0')
705 /* Don't use the '%n' directive; instead, look
706 at the snprintf() return value. */
707 p[1] = '\0';
708 continue;
710 count = retcount;
712 #endif
714 /* Attempt to handle failure. */
715 if (count < 0)
717 if (!(result == resultbuf || result == NULL))
718 free (result);
719 freea (buf);
720 CLEANUP ();
721 errno = EINVAL;
722 return NULL;
725 #if !HAVE_SNPRINTF
726 if (count >= tmp_length)
727 /* tmp_length was incorrectly calculated - fix the
728 code above! */
729 abort ();
730 #endif
732 /* Make room for the result. */
733 if (count >= maxlen)
735 /* Need at least count bytes. But allocate
736 proportionally, to avoid looping eternally if
737 snprintf() reports a too small count. */
738 size_t n = length + count;
740 if (n < 2 * allocated)
741 n = 2 * allocated;
743 ENSURE_ALLOCATION (n);
744 #if HAVE_SNPRINTF
745 continue;
746 #endif
749 #if HAVE_SNPRINTF
750 /* The snprintf() result did fit. */
751 #else
752 /* Append the sprintf() result. */
753 memcpy (result + length, tmp, count);
754 if (tmp != tmpbuf)
755 free (tmp);
756 #endif
758 length += count;
759 break;
765 /* Add the final NUL. */
766 ENSURE_ALLOCATION (length + 1);
767 result[length] = '\0';
769 if (result != resultbuf && length + 1 < allocated)
771 /* Shrink the allocated memory if possible. */
772 char *memory;
774 memory = (char *) realloc (result, length + 1);
775 if (memory != NULL)
776 result = memory;
779 freea (buf);
780 CLEANUP ();
781 *lengthp = length;
782 return result;