2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * This is the code responsible for handling positional arguments
41 * (%m$ and %m$.n$) for vfprintf() and vfwprintf().
44 #include "namespace.h"
45 #include <sys/types.h>
56 #include "un-namespace.h"
57 #include "printflocal.h"
60 #define MAX_POSARG NL_ARGMAX
62 #define MAX_POSARG 65536
66 * Type ids for argument type table.
69 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
70 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
71 T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SSIZET,
72 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
73 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
76 /* An expandable array of types. */
78 enum typeid *table; /* table of types */
79 enum typeid stattable[STATIC_ARG_TBL_SIZE];
80 u_int tablesize; /* current size of type table */
81 u_int tablemax; /* largest used index in table */
82 u_int nextarg; /* 1-based argument index */
85 static int __grow_type_table(struct typetable *);
86 static void build_arg_table (struct typetable *, va_list, union arg **);
89 * Initialize a struct typetable.
92 inittypes(struct typetable *types)
96 types->table = types->stattable;
97 types->tablesize = STATIC_ARG_TBL_SIZE;
100 for (n = 0; n < STATIC_ARG_TBL_SIZE; n++)
101 types->table[n] = T_UNUSED;
105 * struct typetable destructor.
108 freetypes(struct typetable *types)
111 if (types->table != types->stattable)
116 * Ensure that there is space to add a new argument type to the type table.
117 * Expand the table if necessary. Returns 0 on success.
120 _ensurespace(struct typetable *types)
123 if (types->nextarg >= types->tablesize) {
124 if (__grow_type_table(types))
127 if (types->nextarg > types->tablemax)
128 types->tablemax = types->nextarg;
133 * Add an argument type to the table, expanding if necessary.
134 * Returns 0 on success.
137 addtype(struct typetable *types, enum typeid type)
140 if (_ensurespace(types))
142 types->table[types->nextarg++] = type;
147 addsarg(struct typetable *types, int flags)
150 if (_ensurespace(types))
153 types->table[types->nextarg++] = T_INTMAXT;
154 else if (flags & SIZET)
155 types->table[types->nextarg++] = T_SSIZET;
156 else if (flags & PTRDIFFT)
157 types->table[types->nextarg++] = T_PTRDIFFT;
158 else if (flags & LLONGINT)
159 types->table[types->nextarg++] = T_LLONG;
160 else if (flags & LONGINT)
161 types->table[types->nextarg++] = T_LONG;
163 types->table[types->nextarg++] = T_INT;
168 adduarg(struct typetable *types, int flags)
171 if (_ensurespace(types))
174 types->table[types->nextarg++] = T_UINTMAXT;
175 else if (flags & SIZET)
176 types->table[types->nextarg++] = T_SIZET;
177 else if (flags & PTRDIFFT)
178 types->table[types->nextarg++] = T_SIZET;
179 else if (flags & LLONGINT)
180 types->table[types->nextarg++] = T_U_LLONG;
181 else if (flags & LONGINT)
182 types->table[types->nextarg++] = T_U_LONG;
184 types->table[types->nextarg++] = T_U_INT;
189 * Add * arguments to the type array.
192 addaster(struct typetable *types, char **fmtp)
199 while (is_digit(*cp)) {
200 n2 = 10 * n2 + to_digit(*cp);
204 u_int hold = types->nextarg;
206 if (addtype(types, T_INT))
208 types->nextarg = hold;
211 if (addtype(types, T_INT))
218 addwaster(struct typetable *types, wchar_t **fmtp)
225 while (is_digit(*cp)) {
226 n2 = 10 * n2 + to_digit(*cp);
230 u_int hold = types->nextarg;
232 if (addtype(types, T_INT))
234 types->nextarg = hold;
237 if (addtype(types, T_INT))
244 * Find all arguments when a positional parameter is encountered. Returns a
245 * table, indexed by argument number, of pointers to each arguments. The
246 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
247 * It will be replaces with a malloc-ed one if it overflows.
248 * Returns 0 on success. On failure, returns nonzero and sets errno.
251 __find_arguments (const char *fmt0, va_list ap, union arg **argtable)
253 char *fmt; /* format string */
254 int ch; /* character from fmt */
255 u_int n; /* handy integer (short term usage) */
257 int flags; /* flags as above */
258 struct typetable types; /* table of types */
265 * Scan the format for conversions (`%' character).
268 while ((ch = *fmt) != '\0' && ch != '%')
272 fmt++; /* skip over '%' */
277 reswitch: switch (ch) {
282 if ((error = addaster(&types, &fmt)))
290 if ((ch = *fmt++) == '*') {
291 if ((error = addaster(&types, &fmt)))
295 while (is_digit(ch)) {
301 case '1': case '2': case '3': case '4':
302 case '5': case '6': case '7': case '8': case '9':
305 n = 10 * n + to_digit(ch);
306 /* Detect overflow */
307 if (n > MAX_POSARG) {
312 } while (is_digit(ch));
318 #ifndef NO_FLOATING_POINT
324 if (flags & SHORTINT) {
334 if (flags & LONGINT) {
341 flags |= LLONGINT; /* not necessarily */
353 error = addtype(&types,
354 (flags & LONGINT) ? T_WINT : T_INT);
363 if ((error = addsarg(&types, flags)))
366 #ifndef NO_FLOATING_POINT
374 error = addtype(&types,
375 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
379 #endif /* !NO_FLOATING_POINT */
382 error = addtype(&types, TP_INTMAXT);
383 else if (flags & PTRDIFFT)
384 error = addtype(&types, TP_PTRDIFFT);
385 else if (flags & SIZET)
386 error = addtype(&types, TP_SSIZET);
387 else if (flags & LLONGINT)
388 error = addtype(&types, TP_LLONG);
389 else if (flags & LONGINT)
390 error = addtype(&types, TP_LONG);
391 else if (flags & SHORTINT)
392 error = addtype(&types, TP_SHORT);
393 else if (flags & CHARINT)
394 error = addtype(&types, TP_SCHAR);
396 error = addtype(&types, TP_INT);
399 continue; /* no output */
404 if ((error = adduarg(&types, flags)))
408 if ((error = addtype(&types, TP_VOID)))
415 error = addtype(&types,
416 (flags & LONGINT) ? TP_WCHAR : TP_CHAR);
426 if ((error = adduarg(&types, flags)))
429 default: /* "%?" prints ?, unless ? is NUL */
436 build_arg_table(&types, ap, argtable);
439 return (error || *argtable == NULL);
442 /* wchar version of __find_arguments. */
444 __find_warguments (const wchar_t *fmt0, va_list ap, union arg **argtable)
446 wchar_t *fmt; /* format string */
447 wchar_t ch; /* character from fmt */
448 u_int n; /* handy integer (short term usage) */
450 int flags; /* flags as above */
451 struct typetable types; /* table of types */
453 fmt = (wchar_t *)fmt0;
458 * Scan the format for conversions (`%' character).
461 while ((ch = *fmt) != '\0' && ch != '%')
465 fmt++; /* skip over '%' */
470 reswitch: switch (ch) {
475 if ((error = addwaster(&types, &fmt)))
483 if ((ch = *fmt++) == '*') {
484 if ((error = addwaster(&types, &fmt)))
488 while (is_digit(ch)) {
494 case '1': case '2': case '3': case '4':
495 case '5': case '6': case '7': case '8': case '9':
498 n = 10 * n + to_digit(ch);
499 /* Detect overflow */
500 if (n > MAX_POSARG) {
505 } while (is_digit(ch));
511 #ifndef NO_FLOATING_POINT
517 if (flags & SHORTINT) {
527 if (flags & LONGINT) {
534 flags |= LLONGINT; /* not necessarily */
546 error = addtype(&types,
547 (flags & LONGINT) ? T_WINT : T_INT);
556 if ((error = addsarg(&types, flags)))
559 #ifndef NO_FLOATING_POINT
567 error = addtype(&types,
568 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
572 #endif /* !NO_FLOATING_POINT */
575 error = addtype(&types, TP_INTMAXT);
576 else if (flags & PTRDIFFT)
577 error = addtype(&types, TP_PTRDIFFT);
578 else if (flags & SIZET)
579 error = addtype(&types, TP_SSIZET);
580 else if (flags & LLONGINT)
581 error = addtype(&types, TP_LLONG);
582 else if (flags & LONGINT)
583 error = addtype(&types, TP_LONG);
584 else if (flags & SHORTINT)
585 error = addtype(&types, TP_SHORT);
586 else if (flags & CHARINT)
587 error = addtype(&types, TP_SCHAR);
589 error = addtype(&types, TP_INT);
592 continue; /* no output */
597 if ((error = adduarg(&types, flags)))
601 if ((error = addtype(&types, TP_VOID)))
608 error = addtype(&types,
609 (flags & LONGINT) ? TP_WCHAR : TP_CHAR);
619 if ((error = adduarg(&types, flags)))
622 default: /* "%?" prints ?, unless ? is NUL */
629 build_arg_table(&types, ap, argtable);
632 return (error || *argtable == NULL);
636 * Increase the size of the type table. Returns 0 on success.
639 __grow_type_table(struct typetable *types)
641 enum typeid *const oldtable = types->table;
642 const int oldsize = types->tablesize;
643 enum typeid *newtable;
646 /* Detect overflow */
647 if (types->nextarg > NL_ARGMAX)
650 newsize = oldsize * 2;
651 if (newsize < types->nextarg + 1)
652 newsize = types->nextarg + 1;
653 if (oldsize == STATIC_ARG_TBL_SIZE) {
654 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
656 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
658 newtable = reallocarray(oldtable, newsize, sizeof(enum typeid));
659 if (newtable == NULL)
662 for (n = oldsize; n < newsize; n++)
663 newtable[n] = T_UNUSED;
665 types->table = newtable;
666 types->tablesize = newsize;
672 * Build the argument table from the completed type table.
673 * On malloc failure, *argtable is set to NULL.
676 build_arg_table(struct typetable *types, va_list ap, union arg **argtable)
680 if (types->tablemax >= STATIC_ARG_TBL_SIZE) {
681 *argtable = (union arg *)
682 malloc (sizeof (union arg) * (types->tablemax + 1));
683 if (*argtable == NULL)
687 (*argtable) [0].intarg = 0;
688 for (n = 1; n <= types->tablemax; n++) {
689 switch (types->table[n]) {
690 case T_UNUSED: /* whoops! */
691 (*argtable) [n].intarg = va_arg (ap, int);
694 (*argtable) [n].pschararg = va_arg (ap, signed char *);
697 (*argtable) [n].pshortarg = va_arg (ap, short *);
700 (*argtable) [n].intarg = va_arg (ap, int);
703 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
706 (*argtable) [n].pintarg = va_arg (ap, int *);
709 (*argtable) [n].longarg = va_arg (ap, long);
712 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
715 (*argtable) [n].plongarg = va_arg (ap, long *);
718 (*argtable) [n].longlongarg = va_arg (ap, long long);
721 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
724 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
727 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
730 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
733 (*argtable) [n].sizearg = va_arg (ap, size_t);
736 (*argtable) [n].sizearg = va_arg (ap, ssize_t);
739 (*argtable) [n].pssizearg = va_arg (ap, ssize_t *);
742 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
745 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
748 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
751 #ifndef NO_FLOATING_POINT
752 (*argtable) [n].doublearg = va_arg (ap, double);
756 #ifndef NO_FLOATING_POINT
757 (*argtable) [n].longdoublearg = va_arg (ap, long double);
761 (*argtable) [n].pchararg = va_arg (ap, char *);
764 (*argtable) [n].pvoidarg = va_arg (ap, void *);
767 (*argtable) [n].wintarg = va_arg (ap, wint_t);
770 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);