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