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