]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwprintf.c
395bc637b87c97dabdbad87926f357ab659656de
[apple/libc.git] / stdio / FreeBSD / vfwprintf.c
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #if 0
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
42 __FBSDID("FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.58 2003/04/14 11:24:53 das Exp");
43 #endif
44 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.12 2003/04/19 23:53:19 das Exp $");
45
46 /*
47 * Actual wprintf innards.
48 *
49 * Avoid making gratuitous changes to this source file; it should be kept
50 * as close as possible to vfprintf.c for ease of maintenance.
51 */
52
53 #include "namespace.h"
54 #include <sys/types.h>
55
56 #include <ctype.h>
57 #include <limits.h>
58 #include <locale.h>
59 #include <stdarg.h>
60 #include <stddef.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <wchar.h>
66 #include <wctype.h>
67 #include "un-namespace.h"
68
69 #include "libc_private.h"
70 #include "local.h"
71 #include "fvwrite.h"
72
73 /* Define FLOATING_POINT to get floating point. */
74 #define FLOATING_POINT
75
76 union arg {
77 int intarg;
78 u_int uintarg;
79 long longarg;
80 u_long ulongarg;
81 long long longlongarg;
82 unsigned long long ulonglongarg;
83 ptrdiff_t ptrdiffarg;
84 size_t sizearg;
85 intmax_t intmaxarg;
86 uintmax_t uintmaxarg;
87 void *pvoidarg;
88 char *pchararg;
89 signed char *pschararg;
90 short *pshortarg;
91 int *pintarg;
92 long *plongarg;
93 long long *plonglongarg;
94 ptrdiff_t *pptrdiffarg;
95 size_t *psizearg;
96 intmax_t *pintmaxarg;
97 #ifdef FLOATING_POINT
98 double doublearg;
99 long double longdoublearg;
100 #endif
101 wint_t wintarg;
102 wchar_t *pwchararg;
103 };
104
105 /*
106 * Type ids for argument type table.
107 */
108 enum typeid {
109 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
110 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
111 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
112 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
113 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
114 };
115
116 static int __sbprintf(FILE *, const wchar_t *, va_list);
117 static wchar_t *__ujtoa(uintmax_t, wchar_t *, int, int, const wchar_t *, int,
118 char, const char *);
119 static wchar_t *__ultoa(u_long, wchar_t *, int, int, const wchar_t *, int,
120 char, const char *);
121 static wchar_t *__mbsconv(char *, int);
122 static void __find_arguments(const wchar_t *, va_list, union arg **);
123 static void __grow_type_table(int, enum typeid **, int *);
124
125 /*
126 * Helper function for `fprintf to unbuffered unix file': creates a
127 * temporary buffer. We only work on write-only files; this avoids
128 * worries about ungetc buffers and so forth.
129 */
130 static int
131 __sbprintf(FILE *fp, const wchar_t *fmt, va_list ap)
132 {
133 int ret;
134 FILE fake;
135 unsigned char buf[BUFSIZ];
136
137 /* copy the important variables */
138 fake._flags = fp->_flags & ~__SNBF;
139 fake._file = fp->_file;
140 fake._cookie = fp->_cookie;
141 fake._write = fp->_write;
142 fake._extra = fp->_extra;
143
144 /* set up the buffer */
145 fake._bf._base = fake._p = buf;
146 fake._bf._size = fake._w = sizeof(buf);
147 fake._lbfsize = 0; /* not actually used, but Just In Case */
148
149 /* do the work, then copy any error status */
150 ret = __vfwprintf(&fake, fmt, ap);
151 if (ret >= 0 && __fflush(&fake))
152 ret = WEOF;
153 if (fake._flags & __SERR)
154 fp->_flags |= __SERR;
155 return (ret);
156 }
157
158 /*
159 * Macros for converting digits to letters and vice versa
160 */
161 #define to_digit(c) ((c) - '0')
162 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
163 #define to_char(n) ((n) + '0')
164
165 /*
166 * Convert an unsigned long to ASCII for printf purposes, returning
167 * a pointer to the first character of the string representation.
168 * Octal numbers can be forced to have a leading zero; hex numbers
169 * use the given digits.
170 */
171 static wchar_t *
172 __ultoa(u_long val, wchar_t *endp, int base, int octzero, const wchar_t *xdigs,
173 int needgrp, char thousep, const char *grp)
174 {
175 wchar_t *cp = endp;
176 long sval;
177 int ndig;
178
179 /*
180 * Handle the three cases separately, in the hope of getting
181 * better/faster code.
182 */
183 switch (base) {
184 case 10:
185 if (val < 10) { /* many numbers are 1 digit */
186 *--cp = to_char(val);
187 return (cp);
188 }
189 ndig = 0;
190 /*
191 * On many machines, unsigned arithmetic is harder than
192 * signed arithmetic, so we do at most one unsigned mod and
193 * divide; this is sufficient to reduce the range of
194 * the incoming value to where signed arithmetic works.
195 */
196 if (val > LONG_MAX) {
197 *--cp = to_char(val % 10);
198 ndig++;
199 sval = val / 10;
200 } else
201 sval = val;
202 do {
203 *--cp = to_char(sval % 10);
204 ndig++;
205 /*
206 * If (*grp == CHAR_MAX) then no more grouping
207 * should be performed.
208 */
209 if (needgrp && ndig == *grp && *grp != CHAR_MAX
210 && sval > 9) {
211 *--cp = thousep;
212 ndig = 0;
213 /*
214 * If (*(grp+1) == '\0') then we have to
215 * use *grp character (last grouping rule)
216 * for all next cases
217 */
218 if (*(grp+1) != '\0')
219 grp++;
220 }
221 sval /= 10;
222 } while (sval != 0);
223 break;
224
225 case 8:
226 do {
227 *--cp = to_char(val & 7);
228 val >>= 3;
229 } while (val);
230 if (octzero && *cp != '0')
231 *--cp = '0';
232 break;
233
234 case 16:
235 do {
236 *--cp = xdigs[val & 15];
237 val >>= 4;
238 } while (val);
239 break;
240
241 default: /* oops */
242 abort();
243 }
244 return (cp);
245 }
246
247 /* Identical to __ultoa, but for intmax_t. */
248 static wchar_t *
249 __ujtoa(uintmax_t val, wchar_t *endp, int base, int octzero,
250 const wchar_t *xdigs, int needgrp, char thousep, const char *grp)
251 {
252 wchar_t *cp = endp;
253 intmax_t sval;
254 int ndig;
255
256 /* quick test for small values; __ultoa is typically much faster */
257 /* (perhaps instead we should run until small, then call __ultoa?) */
258 if (val <= ULONG_MAX)
259 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
260 needgrp, thousep, grp));
261 switch (base) {
262 case 10:
263 if (val < 10) {
264 *--cp = to_char(val % 10);
265 return (cp);
266 }
267 ndig = 0;
268 if (val > INTMAX_MAX) {
269 *--cp = to_char(val % 10);
270 ndig++;
271 sval = val / 10;
272 } else
273 sval = val;
274 do {
275 *--cp = to_char(sval % 10);
276 ndig++;
277 /*
278 * If (*grp == CHAR_MAX) then no more grouping
279 * should be performed.
280 */
281 if (needgrp && *grp != CHAR_MAX && ndig == *grp
282 && sval > 9) {
283 *--cp = thousep;
284 ndig = 0;
285 /*
286 * If (*(grp+1) == '\0') then we have to
287 * use *grp character (last grouping rule)
288 * for all next cases
289 */
290 if (*(grp+1) != '\0')
291 grp++;
292 }
293 sval /= 10;
294 } while (sval != 0);
295 break;
296
297 case 8:
298 do {
299 *--cp = to_char(val & 7);
300 val >>= 3;
301 } while (val);
302 if (octzero && *cp != '0')
303 *--cp = '0';
304 break;
305
306 case 16:
307 do {
308 *--cp = xdigs[val & 15];
309 val >>= 4;
310 } while (val);
311 break;
312
313 default:
314 abort();
315 }
316 return (cp);
317 }
318
319 /*
320 * Convert a multibyte character string argument for the %s format to a wide
321 * string representation. ``prec'' specifies the maximum number of bytes
322 * to output. If ``prec'' is greater than or equal to zero, we can't assume
323 * that the multibyte char. string ends in a null character.
324 */
325 static wchar_t *
326 __mbsconv(char *mbsarg, int prec)
327 {
328 wchar_t *convbuf, *wcp;
329 const char *p;
330 size_t insize, nchars, nconv;
331 mbstate_t mbs;
332
333 if (mbsarg == NULL)
334 return (NULL);
335
336 /*
337 * Supplied argument is a multibyte string; convert it to wide
338 * characters first.
339 */
340 if (prec >= 0) {
341 /*
342 * String is not guaranteed to be NUL-terminated. Find the
343 * number of characters to print.
344 */
345 memset(&mbs, 0, sizeof(mbs));
346 p = mbsarg;
347 insize = nchars = 0;
348 while (nchars != (size_t)prec) {
349 nconv = mbrlen(p, MB_CUR_MAX, &mbs);
350 if (nconv == 0 || nconv == (size_t)-1 ||
351 nconv == (size_t)-2)
352 break;
353 p += nconv;
354 nchars++;
355 insize += nconv;
356 }
357 if (nconv == (size_t)-1 || nconv == (size_t)-2)
358 return (NULL);
359 } else
360 insize = strlen(mbsarg);
361
362 /*
363 * Allocate buffer for the result and perform the conversion,
364 * converting at most `size' bytes of the input multibyte string to
365 * wide characters for printing.
366 */
367 convbuf = malloc((insize + 1) * sizeof(*convbuf));
368 if (convbuf == NULL)
369 return (NULL);
370 wcp = convbuf;
371 p = mbsarg;
372 memset(&mbs, 0, sizeof(mbs));
373 while (insize != 0) {
374 nconv = mbrtowc(wcp, p, insize, &mbs);
375 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
376 break;
377 wcp++;
378 p += nconv;
379 insize -= nconv;
380 }
381 if (nconv == (size_t)-1 || nconv == (size_t)-2) {
382 free(convbuf);
383 return (NULL);
384 }
385 *wcp = L'\0';
386
387 return (convbuf);
388 }
389
390 /*
391 * MT-safe version
392 */
393 int
394 vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
395
396 {
397 int ret;
398
399 FLOCKFILE(fp);
400 ret = __vfwprintf(fp, fmt0, ap);
401 FUNLOCKFILE(fp);
402 return (ret);
403 }
404
405 #ifdef FLOATING_POINT
406
407 #define dtoa __dtoa
408 #define freedtoa __freedtoa
409
410 #include <float.h>
411 #include <math.h>
412 #include "floatio.h"
413 #include "gdtoa.h"
414
415 #define DEFPREC 6
416
417 static int exponent(wchar_t *, int, wchar_t);
418
419 #endif /* FLOATING_POINT */
420
421 /*
422 * The size of the buffer we use as scratch space for integer
423 * conversions, among other things. Technically, we would need the
424 * most space for base 10 conversions with thousands' grouping
425 * characters between each pair of digits. 100 bytes is a
426 * conservative overestimate even for a 128-bit uintmax_t.
427 */
428 #define BUF 100
429
430 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
431
432 /*
433 * Flags used during conversion.
434 */
435 #define ALT 0x001 /* alternate form */
436 #define LADJUST 0x004 /* left adjustment */
437 #define LONGDBL 0x008 /* long double */
438 #define LONGINT 0x010 /* long integer */
439 #define LLONGINT 0x020 /* long long integer */
440 #define SHORTINT 0x040 /* short integer */
441 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
442 #define FPT 0x100 /* Floating point number */
443 #define GROUPING 0x200 /* use grouping ("'" flag) */
444 /* C99 additional size modifiers: */
445 #define SIZET 0x400 /* size_t */
446 #define PTRDIFFT 0x800 /* ptrdiff_t */
447 #define INTMAXT 0x1000 /* intmax_t */
448 #define CHARINT 0x2000 /* print char using int format */
449
450 /*
451 * Non-MT-safe version
452 */
453 int
454 __vfwprintf(FILE *fp, const wchar_t *fmt0, va_list ap)
455 {
456 wchar_t *fmt; /* format string */
457 wchar_t ch; /* character from fmt */
458 int n, n2, n3; /* handy integer (short term usage) */
459 wchar_t *cp; /* handy char pointer (short term usage) */
460 int flags; /* flags as above */
461 int ret; /* return value accumulator */
462 int width; /* width from format (%8d), or 0 */
463 int prec; /* precision from format; <0 for N/A */
464 wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */
465 char thousands_sep; /* locale specific thousands separator */
466 const char *grouping; /* locale specific numeric grouping rules */
467 #ifdef FLOATING_POINT
468 /*
469 * We can decompose the printed representation of floating
470 * point numbers into several parts, some of which may be empty:
471 *
472 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
473 * A B ---C--- D E F
474 *
475 * A: 'sign' holds this value if present; '\0' otherwise
476 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
477 * C: cp points to the string MMMNNN. Leading and trailing
478 * zeros are not in the string and must be added.
479 * D: expchar holds this character; '\0' if no exponent, e.g. %f
480 * F: at least two digits for decimal, at least one digit for hex
481 */
482 char *decimal_point; /* locale specific decimal point */
483 int signflag; /* true if float is negative */
484 union { /* floating point arguments %[aAeEfFgG] */
485 double dbl;
486 long double ldbl;
487 } fparg;
488 int expt; /* integer value of exponent */
489 char expchar; /* exponent character: [eEpP\0] */
490 char *dtoaend; /* pointer to end of converted digits */
491 int expsize; /* character count for expstr */
492 int lead; /* sig figs before decimal or group sep */
493 int ndig; /* actual number of digits returned by dtoa */
494 wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
495 char *dtoaresult; /* buffer allocated by dtoa */
496 int nseps; /* number of group separators with ' */
497 int nrepeats; /* number of repeats of the last group */
498 #endif
499 u_long ulval; /* integer arguments %[diouxX] */
500 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
501 int base; /* base for [diouxX] conversion */
502 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
503 int realsz; /* field size expanded by dprec, sign, etc */
504 int size; /* size of converted field or string */
505 int prsize; /* max size of printed field */
506 const wchar_t *xdigs; /* digits for [xX] conversion */
507 wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */
508 wchar_t ox[2]; /* space for 0x hex-prefix */
509 union arg *argtable; /* args, built due to positional arg */
510 union arg statargtable [STATIC_ARG_TBL_SIZE];
511 int nextarg; /* 1-based argument index */
512 va_list orgap; /* original argument pointer */
513 wchar_t *convbuf; /* multibyte to wide conversion result */
514
515 /*
516 * Choose PADSIZE to trade efficiency vs. size. If larger printf
517 * fields occur frequently, increase PADSIZE and make the initialisers
518 * below longer.
519 */
520 #define PADSIZE 16 /* pad chunk size */
521 static wchar_t blanks[PADSIZE] =
522 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
523 static wchar_t zeroes[PADSIZE] =
524 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
525
526 static const wchar_t xdigs_lower[16] = L"0123456789abcdef";
527 static const wchar_t xdigs_upper[16] = L"0123456789ABCDEF";
528
529 /*
530 * BEWARE, these `goto error' on error, PRINT uses `n2' and
531 * PAD uses `n'.
532 */
533 #define PRINT(ptr, len) do { \
534 for (n3 = 0; n3 < (len); n3++) \
535 __fputwc((ptr)[n3], fp); \
536 } while (0)
537 #define PAD(howmany, with) do { \
538 if ((n = (howmany)) > 0) { \
539 while (n > PADSIZE) { \
540 PRINT(with, PADSIZE); \
541 n -= PADSIZE; \
542 } \
543 PRINT(with, n); \
544 } \
545 } while (0)
546 #define PRINTANDPAD(p, ep, len, with) do { \
547 n2 = (ep) - (p); \
548 if (n2 > (len)) \
549 n2 = (len); \
550 if (n2 > 0) \
551 PRINT((p), n2); \
552 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
553 } while(0)
554
555 /*
556 * Get the argument indexed by nextarg. If the argument table is
557 * built, use it to get the argument. If its not, get the next
558 * argument (and arguments must be gotten sequentially).
559 */
560 #define GETARG(type) \
561 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
562 (nextarg++, va_arg(ap, type)))
563
564 /*
565 * To extend shorts properly, we need both signed and unsigned
566 * argument extraction methods.
567 */
568 #define SARG() \
569 (flags&LONGINT ? GETARG(long) : \
570 flags&SHORTINT ? (long)(short)GETARG(int) : \
571 flags&CHARINT ? (long)(signed char)GETARG(int) : \
572 (long)GETARG(int))
573 #define UARG() \
574 (flags&LONGINT ? GETARG(u_long) : \
575 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
576 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
577 (u_long)GETARG(u_int))
578 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
579 #define SJARG() \
580 (flags&INTMAXT ? GETARG(intmax_t) : \
581 flags&SIZET ? (intmax_t)GETARG(size_t) : \
582 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
583 (intmax_t)GETARG(long long))
584 #define UJARG() \
585 (flags&INTMAXT ? GETARG(uintmax_t) : \
586 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
587 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
588 (uintmax_t)GETARG(unsigned long long))
589
590 /*
591 * Get * arguments, including the form *nn$. Preserve the nextarg
592 * that the argument can be gotten once the type is determined.
593 */
594 #define GETASTER(val) \
595 n2 = 0; \
596 cp = fmt; \
597 while (is_digit(*cp)) { \
598 n2 = 10 * n2 + to_digit(*cp); \
599 cp++; \
600 } \
601 if (*cp == '$') { \
602 int hold = nextarg; \
603 if (argtable == NULL) { \
604 argtable = statargtable; \
605 __find_arguments (fmt0, orgap, &argtable); \
606 } \
607 nextarg = n2; \
608 val = GETARG (int); \
609 nextarg = hold; \
610 fmt = ++cp; \
611 } else { \
612 val = GETARG (int); \
613 }
614
615
616 thousands_sep = '\0';
617 grouping = NULL;
618 #ifdef FLOATING_POINT
619 decimal_point = localeconv()->decimal_point;
620 #endif
621 convbuf = NULL;
622 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
623 if (cantwrite(fp))
624 return (EOF);
625
626 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
627 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
628 fp->_file >= 0)
629 return (__sbprintf(fp, fmt0, ap));
630
631 fmt = (wchar_t *)fmt0;
632 argtable = NULL;
633 nextarg = 1;
634 va_copy(orgap, ap);
635 ret = 0;
636
637 /*
638 * Scan the format for conversions (`%' character).
639 */
640 for (;;) {
641 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
642 /* void */;
643 if ((n = fmt - cp) != 0) {
644 if ((unsigned)ret + n > INT_MAX) {
645 ret = EOF;
646 goto error;
647 }
648 PRINT(cp, n);
649 ret += n;
650 }
651 if (ch == '\0')
652 goto done;
653 fmt++; /* skip over '%' */
654
655 flags = 0;
656 dprec = 0;
657 width = 0;
658 prec = -1;
659 sign = '\0';
660 ox[1] = '\0';
661
662 rflag: ch = *fmt++;
663 reswitch: switch (ch) {
664 case ' ':
665 /*-
666 * ``If the space and + flags both appear, the space
667 * flag will be ignored.''
668 * -- ANSI X3J11
669 */
670 if (!sign)
671 sign = ' ';
672 goto rflag;
673 case '#':
674 flags |= ALT;
675 goto rflag;
676 case '*':
677 /*-
678 * ``A negative field width argument is taken as a
679 * - flag followed by a positive field width.''
680 * -- ANSI X3J11
681 * They don't exclude field widths read from args.
682 */
683 GETASTER (width);
684 if (width >= 0)
685 goto rflag;
686 width = -width;
687 /* FALLTHROUGH */
688 case '-':
689 flags |= LADJUST;
690 goto rflag;
691 case '+':
692 sign = '+';
693 goto rflag;
694 case '\'':
695 flags |= GROUPING;
696 thousands_sep = *(localeconv()->thousands_sep);
697 grouping = localeconv()->grouping;
698 goto rflag;
699 case '.':
700 if ((ch = *fmt++) == '*') {
701 GETASTER (prec);
702 goto rflag;
703 }
704 prec = 0;
705 while (is_digit(ch)) {
706 prec = 10 * prec + to_digit(ch);
707 ch = *fmt++;
708 }
709 goto reswitch;
710 case '0':
711 /*-
712 * ``Note that 0 is taken as a flag, not as the
713 * beginning of a field width.''
714 * -- ANSI X3J11
715 */
716 flags |= ZEROPAD;
717 goto rflag;
718 case '1': case '2': case '3': case '4':
719 case '5': case '6': case '7': case '8': case '9':
720 n = 0;
721 do {
722 n = 10 * n + to_digit(ch);
723 ch = *fmt++;
724 } while (is_digit(ch));
725 if (ch == '$') {
726 nextarg = n;
727 if (argtable == NULL) {
728 argtable = statargtable;
729 __find_arguments (fmt0, orgap,
730 &argtable);
731 }
732 goto rflag;
733 }
734 width = n;
735 goto reswitch;
736 #ifdef FLOATING_POINT
737 case 'L':
738 flags |= LONGDBL;
739 goto rflag;
740 #endif
741 case 'h':
742 if (flags & SHORTINT) {
743 flags &= ~SHORTINT;
744 flags |= CHARINT;
745 } else
746 flags |= SHORTINT;
747 goto rflag;
748 case 'j':
749 flags |= INTMAXT;
750 goto rflag;
751 case 'l':
752 if (flags & LONGINT) {
753 flags &= ~LONGINT;
754 flags |= LLONGINT;
755 } else
756 flags |= LONGINT;
757 goto rflag;
758 case 'q':
759 flags |= LLONGINT; /* not necessarily */
760 goto rflag;
761 case 't':
762 flags |= PTRDIFFT;
763 goto rflag;
764 case 'z':
765 flags |= SIZET;
766 goto rflag;
767 case 'C':
768 flags |= LONGINT;
769 /*FALLTHROUGH*/
770 case 'c':
771 if (flags & LONGINT)
772 *(cp = buf) = (wchar_t)GETARG(wint_t);
773 else
774 *(cp = buf) = (wchar_t)btowc(GETARG(int));
775 size = 1;
776 sign = '\0';
777 break;
778 case 'D':
779 flags |= LONGINT;
780 /*FALLTHROUGH*/
781 case 'd':
782 case 'i':
783 if (flags & INTMAX_SIZE) {
784 ujval = SJARG();
785 if ((intmax_t)ujval < 0) {
786 ujval = -ujval;
787 sign = '-';
788 }
789 } else {
790 ulval = SARG();
791 if ((long)ulval < 0) {
792 ulval = -ulval;
793 sign = '-';
794 }
795 }
796 base = 10;
797 goto number;
798 #ifdef FLOATING_POINT
799 #ifdef HEXFLOAT
800 case 'a':
801 case 'A':
802 if (ch == 'a') {
803 ox[1] = 'x';
804 xdigs = xdigs_lower;
805 expchar = 'p';
806 } else {
807 ox[1] = 'X';
808 xdigs = xdigs_upper;
809 expchar = 'P';
810 }
811 /*
812 * XXX We don't actually have a conversion
813 * XXX routine for this yet.
814 */
815 if (flags & LONGDBL) {
816 fparg.ldbl = (double)GETARG(long double);
817 dtoaresult =
818 __hldtoa(fparg.ldbl, xdigs, prec,
819 &expt, &signflag, &dtoaend);
820 } else {
821 fparg.dbl = GETARG(double);
822 dtoaresult =
823 __hdtoa(fparg.dbl, xdigs, prec,
824 &expt, &signflag, &dtoaend);
825 }
826 if (convbuf != NULL)
827 free(convbuf);
828 cp = convbuf = __mbsconv(dtoaresult, -1);
829 freedtoa(dtoaresult);
830 goto fp_begin;
831 #endif
832 case 'e':
833 case 'E':
834 expchar = ch;
835 if (prec < 0) /* account for digit before decpt */
836 prec = DEFPREC + 1;
837 else
838 prec++;
839 goto fp_begin;
840 case 'f':
841 case 'F':
842 expchar = '\0';
843 goto fp_begin;
844 case 'g':
845 case 'G':
846 expchar = ch - ('g' - 'e');
847 if (prec == 0)
848 prec = 1;
849 fp_begin:
850 if (prec < 0)
851 prec = DEFPREC;
852 if (convbuf != NULL)
853 free(convbuf);
854 if (flags & LONGDBL) {
855 fparg.ldbl = GETARG(long double);
856 dtoaresult =
857 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
858 &expt, &signflag, &dtoaend);
859 } else {
860 fparg.dbl = GETARG(double);
861 dtoaresult =
862 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
863 &expt, &signflag, &dtoaend);
864 if (expt == 9999)
865 expt = INT_MAX;
866 }
867 ndig = dtoaend - dtoaresult;
868 cp = convbuf = __mbsconv(dtoaresult, -1);
869 freedtoa(dtoaresult);
870 if (signflag)
871 sign = '-';
872 if (expt == INT_MAX) { /* inf or nan */
873 if (*cp == 'N') {
874 cp = (ch >= 'a') ? L"nan" : L"NAN";
875 sign = '\0';
876 } else
877 cp = (ch >= 'a') ? L"inf" : L"INF";
878 size = 3;
879 break;
880 }
881 flags |= FPT;
882 if (ch == 'g' || ch == 'G') {
883 if (expt > -4 && expt <= prec) {
884 /* Make %[gG] smell like %[fF] */
885 expchar = '\0';
886 if (flags & ALT)
887 prec -= expt;
888 else
889 prec = ndig - expt;
890 if (prec < 0)
891 prec = 0;
892 } else {
893 /*
894 * Make %[gG] smell like %[eE], but
895 * trim trailing zeroes if no # flag.
896 */
897 if (!(flags & ALT))
898 prec = ndig;
899 }
900 }
901 if (expchar) {
902 expsize = exponent(expstr, expt - 1, expchar);
903 size = expsize + prec;
904 if (prec > 1 || flags & ALT)
905 ++size;
906 } else {
907 /* space for digits before decimal point */
908 if (expt > 0)
909 size = expt;
910 else /* "0" */
911 size = 1;
912 /* space for decimal pt and following digits */
913 if (prec || flags & ALT)
914 size += prec + 1;
915 if (grouping && expt > 0) {
916 /* space for thousands' grouping */
917 nseps = nrepeats = 0;
918 lead = expt;
919 while (*grouping != CHAR_MAX) {
920 if (lead <= *grouping)
921 break;
922 lead -= *grouping;
923 if (*(grouping+1)) {
924 nseps++;
925 grouping++;
926 } else
927 nrepeats++;
928 }
929 size += nseps + nrepeats;
930 } else
931 lead = expt;
932 }
933 break;
934 #endif /* FLOATING_POINT */
935 case 'n':
936 /*
937 * Assignment-like behavior is specified if the
938 * value overflows or is otherwise unrepresentable.
939 * C99 says to use `signed char' for %hhn conversions.
940 */
941 if (flags & LLONGINT)
942 *GETARG(long long *) = ret;
943 else if (flags & SIZET)
944 *GETARG(ssize_t *) = (ssize_t)ret;
945 else if (flags & PTRDIFFT)
946 *GETARG(ptrdiff_t *) = ret;
947 else if (flags & INTMAXT)
948 *GETARG(intmax_t *) = ret;
949 else if (flags & LONGINT)
950 *GETARG(long *) = ret;
951 else if (flags & SHORTINT)
952 *GETARG(short *) = ret;
953 else if (flags & CHARINT)
954 *GETARG(signed char *) = ret;
955 else
956 *GETARG(int *) = ret;
957 continue; /* no output */
958 case 'O':
959 flags |= LONGINT;
960 /*FALLTHROUGH*/
961 case 'o':
962 if (flags & INTMAX_SIZE)
963 ujval = UJARG();
964 else
965 ulval = UARG();
966 base = 8;
967 goto nosign;
968 case 'p':
969 /*-
970 * ``The argument shall be a pointer to void. The
971 * value of the pointer is converted to a sequence
972 * of printable characters, in an implementation-
973 * defined manner.''
974 * -- ANSI X3J11
975 */
976 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
977 base = 16;
978 xdigs = xdigs_lower;
979 flags = flags | INTMAXT;
980 ox[1] = 'x';
981 goto nosign;
982 case 'S':
983 flags |= LONGINT;
984 /*FALLTHROUGH*/
985 case 's':
986 if (flags & LONGINT) {
987 if ((cp = GETARG(wchar_t *)) == NULL)
988 cp = L"(null)";
989 } else {
990 char *mbp;
991
992 if (convbuf != NULL)
993 free(convbuf);
994 if ((mbp = GETARG(char *)) == NULL)
995 cp = L"(null)";
996 else {
997 convbuf = __mbsconv(mbp, prec);
998 if (convbuf == NULL) {
999 fp->_flags |= __SERR;
1000 goto error;
1001 }
1002 cp = convbuf;
1003 }
1004 }
1005
1006 if (prec >= 0) {
1007 /*
1008 * can't use wcslen; can only look for the
1009 * NUL in the first `prec' characters, and
1010 * wcslen() will go further.
1011 */
1012 wchar_t *p = wmemchr(cp, 0, (size_t)prec);
1013
1014 if (p != NULL) {
1015 size = p - cp;
1016 if (size > prec)
1017 size = prec;
1018 } else
1019 size = prec;
1020 } else
1021 size = wcslen(cp);
1022 sign = '\0';
1023 break;
1024 case 'U':
1025 flags |= LONGINT;
1026 /*FALLTHROUGH*/
1027 case 'u':
1028 if (flags & INTMAX_SIZE)
1029 ujval = UJARG();
1030 else
1031 ulval = UARG();
1032 base = 10;
1033 goto nosign;
1034 case 'X':
1035 xdigs = xdigs_upper;
1036 goto hex;
1037 case 'x':
1038 xdigs = xdigs_lower;
1039 hex:
1040 if (flags & INTMAX_SIZE)
1041 ujval = UJARG();
1042 else
1043 ulval = UARG();
1044 base = 16;
1045 /* leading 0x/X only if non-zero */
1046 if (flags & ALT &&
1047 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1048 ox[1] = ch;
1049
1050 flags &= ~GROUPING;
1051 /* unsigned conversions */
1052 nosign: sign = '\0';
1053 /*-
1054 * ``... diouXx conversions ... if a precision is
1055 * specified, the 0 flag will be ignored.''
1056 * -- ANSI X3J11
1057 */
1058 number: if ((dprec = prec) >= 0)
1059 flags &= ~ZEROPAD;
1060
1061 /*-
1062 * ``The result of converting a zero value with an
1063 * explicit precision of zero is no characters.''
1064 * -- ANSI X3J11
1065 */
1066 cp = buf + BUF;
1067 if (flags & INTMAX_SIZE) {
1068 if (ujval != 0 || prec != 0)
1069 cp = __ujtoa(ujval, cp, base,
1070 flags & ALT, xdigs,
1071 flags & GROUPING, thousands_sep,
1072 grouping);
1073 } else {
1074 if (ulval != 0 || prec != 0)
1075 cp = __ultoa(ulval, cp, base,
1076 flags & ALT, xdigs,
1077 flags & GROUPING, thousands_sep,
1078 grouping);
1079 }
1080 size = buf + BUF - cp;
1081 if (size > BUF) /* should never happen */
1082 abort();
1083 break;
1084 default: /* "%?" prints ?, unless ? is NUL */
1085 if (ch == '\0')
1086 goto done;
1087 /* pretend it was %c with argument ch */
1088 cp = buf;
1089 *cp = ch;
1090 size = 1;
1091 sign = '\0';
1092 break;
1093 }
1094
1095 /*
1096 * All reasonable formats wind up here. At this point, `cp'
1097 * points to a string which (if not flags&LADJUST) should be
1098 * padded out to `width' places. If flags&ZEROPAD, it should
1099 * first be prefixed by any sign or other prefix; otherwise,
1100 * it should be blank padded before the prefix is emitted.
1101 * After any left-hand padding and prefixing, emit zeroes
1102 * required by a decimal [diouxX] precision, then print the
1103 * string proper, then emit zeroes required by any leftover
1104 * floating precision; finally, if LADJUST, pad with blanks.
1105 *
1106 * Compute actual size, so we know how much to pad.
1107 * size excludes decimal prec; realsz includes it.
1108 */
1109 realsz = dprec > size ? dprec : size;
1110 if (sign)
1111 realsz++;
1112 else if (ox[1])
1113 realsz += 2;
1114
1115 prsize = width > realsz ? width : realsz;
1116 if ((unsigned)ret + prsize > INT_MAX) {
1117 ret = EOF;
1118 goto error;
1119 }
1120
1121 /* right-adjusting blank padding */
1122 if ((flags & (LADJUST|ZEROPAD)) == 0)
1123 PAD(width - realsz, blanks);
1124
1125 /* prefix */
1126 if (sign) {
1127 PRINT(&sign, 1);
1128 } else if (ox[1]) { /* ox[1] is either x, X, or \0 */
1129 ox[0] = '0';
1130 PRINT(ox, 2);
1131 }
1132
1133 /* right-adjusting zero padding */
1134 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1135 PAD(width - realsz, zeroes);
1136
1137 /* leading zeroes from decimal precision */
1138 PAD(dprec - size, zeroes);
1139
1140 /* the string or number proper */
1141 #ifdef FLOATING_POINT
1142 if ((flags & FPT) == 0) {
1143 PRINT(cp, size);
1144 } else { /* glue together f_p fragments */
1145 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1146 if (expt <= 0) {
1147 PRINT(zeroes, 1);
1148 if (prec || flags & ALT)
1149 PRINT(decimal_point, 1);
1150 PAD(-expt, zeroes);
1151 /* already handled initial 0's */
1152 prec += expt;
1153 } else {
1154 PRINTANDPAD(cp, convbuf + ndig, lead, zeroes);
1155 cp += lead;
1156 if (grouping) {
1157 while (nseps>0 || nrepeats>0) {
1158 if (nrepeats > 0)
1159 nrepeats--;
1160 else {
1161 grouping--;
1162 nseps--;
1163 }
1164 PRINT(&thousands_sep,
1165 1);
1166 PRINTANDPAD(cp,
1167 convbuf + ndig,
1168 *grouping, zeroes);
1169 cp += *grouping;
1170 }
1171 if (cp > convbuf + ndig)
1172 cp = convbuf + ndig;
1173 }
1174 if (prec || flags & ALT) {
1175 buf[0] = *decimal_point;
1176 PRINT(buf, 1);
1177 }
1178 }
1179 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes);
1180 } else { /* %[eE] or sufficiently long %[gG] */
1181 if (prec > 1 || flags & ALT) {
1182 buf[0] = *cp++;
1183 buf[1] = *decimal_point;
1184 PRINT(buf, 2);
1185 PRINT(cp, ndig-1);
1186 PAD(prec - ndig, zeroes);
1187 } else /* XeYYY */
1188 PRINT(cp, 1);
1189 PRINT(expstr, expsize);
1190 }
1191 }
1192 #else
1193 PRINT(cp, size);
1194 #endif
1195 /* left-adjusting padding (always blank) */
1196 if (flags & LADJUST)
1197 PAD(width - realsz, blanks);
1198
1199 /* finally, adjust ret */
1200 ret += prsize;
1201 }
1202 done:
1203 error:
1204 if (convbuf != NULL)
1205 free(convbuf);
1206 if (__sferror(fp))
1207 ret = EOF;
1208 if ((argtable != NULL) && (argtable != statargtable))
1209 free (argtable);
1210 return (ret);
1211 /* NOTREACHED */
1212 }
1213
1214 /*
1215 * Find all arguments when a positional parameter is encountered. Returns a
1216 * table, indexed by argument number, of pointers to each arguments. The
1217 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1218 * It will be replaces with a malloc-ed one if it overflows.
1219 */
1220 static void
1221 __find_arguments (const wchar_t *fmt0, va_list ap, union arg **argtable)
1222 {
1223 wchar_t *fmt; /* format string */
1224 wchar_t ch; /* character from fmt */
1225 int n, n2; /* handy integer (short term usage) */
1226 wchar_t *cp; /* handy char pointer (short term usage) */
1227 int flags; /* flags as above */
1228 int width; /* width from format (%8d), or 0 */
1229 enum typeid *typetable; /* table of types */
1230 enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1231 int tablesize; /* current size of type table */
1232 int tablemax; /* largest used index in table */
1233 int nextarg; /* 1-based argument index */
1234
1235 /*
1236 * Add an argument type to the table, expanding if necessary.
1237 */
1238 #define ADDTYPE(type) \
1239 ((nextarg >= tablesize) ? \
1240 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1241 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1242 typetable[nextarg++] = type)
1243
1244 #define ADDSARG() \
1245 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1246 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1247 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1248 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1249 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1250
1251 #define ADDUARG() \
1252 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1253 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1254 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1255 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1256 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1257
1258 /*
1259 * Add * arguments to the type array.
1260 */
1261 #define ADDASTER() \
1262 n2 = 0; \
1263 cp = fmt; \
1264 while (is_digit(*cp)) { \
1265 n2 = 10 * n2 + to_digit(*cp); \
1266 cp++; \
1267 } \
1268 if (*cp == '$') { \
1269 int hold = nextarg; \
1270 nextarg = n2; \
1271 ADDTYPE (T_INT); \
1272 nextarg = hold; \
1273 fmt = ++cp; \
1274 } else { \
1275 ADDTYPE (T_INT); \
1276 }
1277 fmt = (wchar_t *)fmt0;
1278 typetable = stattypetable;
1279 tablesize = STATIC_ARG_TBL_SIZE;
1280 tablemax = 0;
1281 nextarg = 1;
1282 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1283
1284 /*
1285 * Scan the format for conversions (`%' character).
1286 */
1287 for (;;) {
1288 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1289 /* void */;
1290 if (ch == '\0')
1291 goto done;
1292 fmt++; /* skip over '%' */
1293
1294 flags = 0;
1295 width = 0;
1296
1297 rflag: ch = *fmt++;
1298 reswitch: switch (ch) {
1299 case ' ':
1300 case '#':
1301 goto rflag;
1302 case '*':
1303 ADDASTER ();
1304 goto rflag;
1305 case '-':
1306 case '+':
1307 case '\'':
1308 goto rflag;
1309 case '.':
1310 if ((ch = *fmt++) == '*') {
1311 ADDASTER ();
1312 goto rflag;
1313 }
1314 while (is_digit(ch)) {
1315 ch = *fmt++;
1316 }
1317 goto reswitch;
1318 case '0':
1319 goto rflag;
1320 case '1': case '2': case '3': case '4':
1321 case '5': case '6': case '7': case '8': case '9':
1322 n = 0;
1323 do {
1324 n = 10 * n + to_digit(ch);
1325 ch = *fmt++;
1326 } while (is_digit(ch));
1327 if (ch == '$') {
1328 nextarg = n;
1329 goto rflag;
1330 }
1331 width = n;
1332 goto reswitch;
1333 #ifdef FLOATING_POINT
1334 case 'L':
1335 flags |= LONGDBL;
1336 goto rflag;
1337 #endif
1338 case 'h':
1339 if (flags & SHORTINT) {
1340 flags &= ~SHORTINT;
1341 flags |= CHARINT;
1342 } else
1343 flags |= SHORTINT;
1344 goto rflag;
1345 case 'j':
1346 flags |= INTMAXT;
1347 goto rflag;
1348 case 'l':
1349 if (flags & LONGINT) {
1350 flags &= ~LONGINT;
1351 flags |= LLONGINT;
1352 } else
1353 flags |= LONGINT;
1354 goto rflag;
1355 case 'q':
1356 flags |= LLONGINT; /* not necessarily */
1357 goto rflag;
1358 case 't':
1359 flags |= PTRDIFFT;
1360 goto rflag;
1361 case 'z':
1362 flags |= SIZET;
1363 goto rflag;
1364 case 'C':
1365 flags |= LONGINT;
1366 /*FALLTHROUGH*/
1367 case 'c':
1368 if (flags & LONGINT)
1369 ADDTYPE(T_WINT);
1370 else
1371 ADDTYPE(T_INT);
1372 break;
1373 case 'D':
1374 flags |= LONGINT;
1375 /*FALLTHROUGH*/
1376 case 'd':
1377 case 'i':
1378 ADDSARG();
1379 break;
1380 #ifdef FLOATING_POINT
1381 #ifdef HEXFLOAT
1382 case 'a':
1383 case 'A':
1384 #endif
1385 case 'e':
1386 case 'E':
1387 case 'f':
1388 case 'g':
1389 case 'G':
1390 if (flags & LONGDBL)
1391 ADDTYPE(T_LONG_DOUBLE);
1392 else
1393 ADDTYPE(T_DOUBLE);
1394 break;
1395 #endif /* FLOATING_POINT */
1396 case 'n':
1397 if (flags & INTMAXT)
1398 ADDTYPE(TP_INTMAXT);
1399 else if (flags & PTRDIFFT)
1400 ADDTYPE(TP_PTRDIFFT);
1401 else if (flags & SIZET)
1402 ADDTYPE(TP_SIZET);
1403 else if (flags & LLONGINT)
1404 ADDTYPE(TP_LLONG);
1405 else if (flags & LONGINT)
1406 ADDTYPE(TP_LONG);
1407 else if (flags & SHORTINT)
1408 ADDTYPE(TP_SHORT);
1409 else if (flags & CHARINT)
1410 ADDTYPE(TP_SCHAR);
1411 else
1412 ADDTYPE(TP_INT);
1413 continue; /* no output */
1414 case 'O':
1415 flags |= LONGINT;
1416 /*FALLTHROUGH*/
1417 case 'o':
1418 ADDUARG();
1419 break;
1420 case 'p':
1421 ADDTYPE(TP_VOID);
1422 break;
1423 case 'S':
1424 flags |= LONGINT;
1425 /*FALLTHROUGH*/
1426 case 's':
1427 if (flags & LONGINT)
1428 ADDTYPE(TP_WCHAR);
1429 else
1430 ADDTYPE(TP_CHAR);
1431 break;
1432 case 'U':
1433 flags |= LONGINT;
1434 /*FALLTHROUGH*/
1435 case 'u':
1436 case 'X':
1437 case 'x':
1438 ADDUARG();
1439 break;
1440 default: /* "%?" prints ?, unless ? is NUL */
1441 if (ch == '\0')
1442 goto done;
1443 break;
1444 }
1445 }
1446 done:
1447 /*
1448 * Build the argument table.
1449 */
1450 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1451 *argtable = (union arg *)
1452 malloc (sizeof (union arg) * (tablemax + 1));
1453 }
1454
1455 (*argtable) [0].intarg = 0;
1456 for (n = 1; n <= tablemax; n++) {
1457 switch (typetable [n]) {
1458 case T_UNUSED: /* whoops! */
1459 (*argtable) [n].intarg = va_arg (ap, int);
1460 break;
1461 case TP_SCHAR:
1462 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1463 break;
1464 case TP_SHORT:
1465 (*argtable) [n].pshortarg = va_arg (ap, short *);
1466 break;
1467 case T_INT:
1468 (*argtable) [n].intarg = va_arg (ap, int);
1469 break;
1470 case T_U_INT:
1471 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
1472 break;
1473 case TP_INT:
1474 (*argtable) [n].pintarg = va_arg (ap, int *);
1475 break;
1476 case T_LONG:
1477 (*argtable) [n].longarg = va_arg (ap, long);
1478 break;
1479 case T_U_LONG:
1480 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1481 break;
1482 case TP_LONG:
1483 (*argtable) [n].plongarg = va_arg (ap, long *);
1484 break;
1485 case T_LLONG:
1486 (*argtable) [n].longlongarg = va_arg (ap, long long);
1487 break;
1488 case T_U_LLONG:
1489 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1490 break;
1491 case TP_LLONG:
1492 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
1493 break;
1494 case T_PTRDIFFT:
1495 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1496 break;
1497 case TP_PTRDIFFT:
1498 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1499 break;
1500 case T_SIZET:
1501 (*argtable) [n].sizearg = va_arg (ap, size_t);
1502 break;
1503 case TP_SIZET:
1504 (*argtable) [n].psizearg = va_arg (ap, ssize_t *);
1505 break;
1506 case T_INTMAXT:
1507 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1508 break;
1509 case T_UINTMAXT:
1510 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1511 break;
1512 case TP_INTMAXT:
1513 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1514 break;
1515 #ifdef FLOATING_POINT
1516 case T_DOUBLE:
1517 (*argtable) [n].doublearg = va_arg (ap, double);
1518 break;
1519 case T_LONG_DOUBLE:
1520 (*argtable) [n].longdoublearg = va_arg (ap, long double);
1521 break;
1522 #endif
1523 case TP_CHAR:
1524 (*argtable) [n].pchararg = va_arg (ap, char *);
1525 break;
1526 case TP_VOID:
1527 (*argtable) [n].pvoidarg = va_arg (ap, void *);
1528 break;
1529 case T_WINT:
1530 (*argtable) [n].wintarg = va_arg (ap, wint_t);
1531 break;
1532 case TP_WCHAR:
1533 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1534 break;
1535 }
1536 }
1537
1538 if ((typetable != NULL) && (typetable != stattypetable))
1539 free (typetable);
1540 }
1541
1542 /*
1543 * Increase the size of the type table.
1544 */
1545 static void
1546 __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize)
1547 {
1548 enum typeid *const oldtable = *typetable;
1549 const int oldsize = *tablesize;
1550 enum typeid *newtable;
1551 int newsize = oldsize * 2;
1552
1553 if (newsize < nextarg + 1)
1554 newsize = nextarg + 1;
1555 if (oldsize == STATIC_ARG_TBL_SIZE) {
1556 if ((newtable = malloc(newsize)) == NULL)
1557 abort(); /* XXX handle better */
1558 bcopy(oldtable, newtable, oldsize);
1559 } else {
1560 if ((newtable = reallocf(oldtable, newsize)) == NULL)
1561 abort(); /* XXX handle better */
1562 }
1563 memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
1564
1565 *typetable = newtable;
1566 *tablesize = newsize;
1567 }
1568
1569
1570 #ifdef FLOATING_POINT
1571
1572 static int
1573 exponent(wchar_t *p0, int exp, wchar_t fmtch)
1574 {
1575 wchar_t *p, *t;
1576 wchar_t expbuf[MAXEXPDIG];
1577
1578 p = p0;
1579 *p++ = fmtch;
1580 if (exp < 0) {
1581 exp = -exp;
1582 *p++ = '-';
1583 }
1584 else
1585 *p++ = '+';
1586 t = expbuf + MAXEXPDIG;
1587 if (exp > 9) {
1588 do {
1589 *--t = to_char(exp % 10);
1590 } while ((exp /= 10) > 9);
1591 *--t = to_char(exp);
1592 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1593 }
1594 else {
1595 /*
1596 * Exponents for decimal floating point conversions
1597 * (%[eEgG]) must be at least two characters long,
1598 * whereas exponents for hexadecimal conversions can
1599 * be only one character long.
1600 */
1601 if (fmtch == 'e' || fmtch == 'E')
1602 *p++ = '0';
1603 *p++ = to_char(exp);
1604 }
1605 return (p - p0);
1606 }
1607 #endif /* FLOATING_POINT */