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