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