2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * This is the code responsible for handling positional arguments
43 * (%m$ and %m$.n$) for vfprintf() and vfwprintf().
46 #include "namespace.h"
47 #include <sys/types.h>
58 #include "un-namespace.h"
59 #include "printflocal.h"
62 #define MAX_POSARG NL_ARGMAX
64 #define MAX_POSARG 65536
68 * Type ids for argument type table.
71 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
72 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
73 T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SSIZET,
74 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
75 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
78 /* An expandable array of types. */
80 enum typeid *table; /* table of types */
81 enum typeid stattable[STATIC_ARG_TBL_SIZE];
82 u_int tablesize; /* current size of type table */
83 u_int tablemax; /* largest used index in table */
84 u_int nextarg; /* 1-based argument index */
87 static int __grow_type_table(struct typetable *);
88 static void build_arg_table (struct typetable *, va_list, union arg **);
91 * Initialize a struct typetable.
94 inittypes(struct typetable *types)
98 types->table = types->stattable;
99 types->tablesize = STATIC_ARG_TBL_SIZE;
102 for (n = 0; n < STATIC_ARG_TBL_SIZE; n++)
103 types->table[n] = T_UNUSED;
107 * struct typetable destructor.
110 freetypes(struct typetable *types)
113 if (types->table != types->stattable)
118 * Ensure that there is space to add a new argument type to the type table.
119 * Expand the table if necessary. Returns 0 on success.
122 _ensurespace(struct typetable *types)
125 if (types->nextarg >= types->tablesize) {
126 if (__grow_type_table(types))
129 if (types->nextarg > types->tablemax)
130 types->tablemax = types->nextarg;
135 * Add an argument type to the table, expanding if necessary.
136 * Returns 0 on success.
139 addtype(struct typetable *types, enum typeid type)
142 if (_ensurespace(types))
144 types->table[types->nextarg++] = type;
149 addsarg(struct typetable *types, int flags)
152 if (_ensurespace(types))
155 types->table[types->nextarg++] = T_INTMAXT;
156 else if (flags & SIZET)
157 types->table[types->nextarg++] = T_SSIZET;
158 else if (flags & PTRDIFFT)
159 types->table[types->nextarg++] = T_PTRDIFFT;
160 else if (flags & LLONGINT)
161 types->table[types->nextarg++] = T_LLONG;
162 else if (flags & LONGINT)
163 types->table[types->nextarg++] = T_LONG;
165 types->table[types->nextarg++] = T_INT;
170 adduarg(struct typetable *types, int flags)
173 if (_ensurespace(types))
176 types->table[types->nextarg++] = T_UINTMAXT;
177 else if (flags & SIZET)
178 types->table[types->nextarg++] = T_SIZET;
179 else if (flags & PTRDIFFT)
180 types->table[types->nextarg++] = T_SIZET;
181 else if (flags & LLONGINT)
182 types->table[types->nextarg++] = T_U_LLONG;
183 else if (flags & LONGINT)
184 types->table[types->nextarg++] = T_U_LONG;
186 types->table[types->nextarg++] = T_U_INT;
191 * Add * arguments to the type array.
194 addaster(struct typetable *types, char **fmtp)
201 while (is_digit(*cp)) {
202 n2 = 10 * n2 + to_digit(*cp);
206 u_int hold = types->nextarg;
208 if (addtype(types, T_INT))
210 types->nextarg = hold;
213 if (addtype(types, T_INT))
220 addwaster(struct typetable *types, wchar_t **fmtp)
227 while (is_digit(*cp)) {
228 n2 = 10 * n2 + to_digit(*cp);
232 u_int hold = types->nextarg;
234 if (addtype(types, T_INT))
236 types->nextarg = hold;
239 if (addtype(types, T_INT))
246 * Find all arguments when a positional parameter is encountered. Returns a
247 * table, indexed by argument number, of pointers to each arguments. The
248 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
249 * It will be replaces with a malloc-ed one if it overflows.
250 * Returns 0 on success. On failure, returns nonzero and sets errno.
253 __find_arguments (const char *fmt0, va_list ap, union arg **argtable)
255 char *fmt; /* format string */
256 int ch; /* character from fmt */
257 u_int n; /* handy integer (short term usage) */
259 int flags; /* flags as above */
260 struct typetable types; /* table of types */
267 * Scan the format for conversions (`%' character).
270 while ((ch = *fmt) != '\0' && ch != '%')
274 fmt++; /* skip over '%' */
279 reswitch: switch (ch) {
284 if ((error = addaster(&types, &fmt)))
292 if ((ch = *fmt++) == '*') {
293 if ((error = addaster(&types, &fmt)))
297 while (is_digit(ch)) {
303 case '1': case '2': case '3': case '4':
304 case '5': case '6': case '7': case '8': case '9':
307 n = 10 * n + to_digit(ch);
308 /* Detect overflow */
309 if (n > MAX_POSARG) {
314 } while (is_digit(ch));
320 #ifndef NO_FLOATING_POINT
326 if (flags & SHORTINT) {
336 if (flags & LONGINT) {
343 flags |= LLONGINT; /* not necessarily */
355 error = addtype(&types,
356 (flags & LONGINT) ? T_WINT : T_INT);
365 if ((error = addsarg(&types, flags)))
368 #ifndef NO_FLOATING_POINT
376 error = addtype(&types,
377 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
381 #endif /* !NO_FLOATING_POINT */
384 error = addtype(&types, TP_INTMAXT);
385 else if (flags & PTRDIFFT)
386 error = addtype(&types, TP_PTRDIFFT);
387 else if (flags & SIZET)
388 error = addtype(&types, TP_SSIZET);
389 else if (flags & LLONGINT)
390 error = addtype(&types, TP_LLONG);
391 else if (flags & LONGINT)
392 error = addtype(&types, TP_LONG);
393 else if (flags & SHORTINT)
394 error = addtype(&types, TP_SHORT);
395 else if (flags & CHARINT)
396 error = addtype(&types, TP_SCHAR);
398 error = addtype(&types, TP_INT);
401 continue; /* no output */
406 if ((error = adduarg(&types, flags)))
410 if ((error = addtype(&types, TP_VOID)))
417 error = addtype(&types,
418 (flags & LONGINT) ? TP_WCHAR : TP_CHAR);
428 if ((error = adduarg(&types, flags)))
431 default: /* "%?" prints ?, unless ? is NUL */
438 build_arg_table(&types, ap, argtable);
441 return (error || *argtable == NULL);
444 /* wchar version of __find_arguments. */
446 __find_warguments (const wchar_t *fmt0, va_list ap, union arg **argtable)
448 wchar_t *fmt; /* format string */
449 wchar_t ch; /* character from fmt */
450 u_int n; /* handy integer (short term usage) */
452 int flags; /* flags as above */
453 struct typetable types; /* table of types */
455 fmt = (wchar_t *)fmt0;
460 * Scan the format for conversions (`%' character).
463 while ((ch = *fmt) != '\0' && ch != '%')
467 fmt++; /* skip over '%' */
472 reswitch: switch (ch) {
477 if ((error = addwaster(&types, &fmt)))
485 if ((ch = *fmt++) == '*') {
486 if ((error = addwaster(&types, &fmt)))
490 while (is_digit(ch)) {
496 case '1': case '2': case '3': case '4':
497 case '5': case '6': case '7': case '8': case '9':
500 n = 10 * n + to_digit(ch);
501 /* Detect overflow */
502 if (n > MAX_POSARG) {
507 } while (is_digit(ch));
513 #ifndef NO_FLOATING_POINT
519 if (flags & SHORTINT) {
529 if (flags & LONGINT) {
536 flags |= LLONGINT; /* not necessarily */
548 error = addtype(&types,
549 (flags & LONGINT) ? T_WINT : T_INT);
558 if ((error = addsarg(&types, flags)))
561 #ifndef NO_FLOATING_POINT
569 error = addtype(&types,
570 (flags & LONGDBL) ? T_LONG_DOUBLE : T_DOUBLE);
574 #endif /* !NO_FLOATING_POINT */
577 error = addtype(&types, TP_INTMAXT);
578 else if (flags & PTRDIFFT)
579 error = addtype(&types, TP_PTRDIFFT);
580 else if (flags & SIZET)
581 error = addtype(&types, TP_SSIZET);
582 else if (flags & LLONGINT)
583 error = addtype(&types, TP_LLONG);
584 else if (flags & LONGINT)
585 error = addtype(&types, TP_LONG);
586 else if (flags & SHORTINT)
587 error = addtype(&types, TP_SHORT);
588 else if (flags & CHARINT)
589 error = addtype(&types, TP_SCHAR);
591 error = addtype(&types, TP_INT);
594 continue; /* no output */
599 if ((error = adduarg(&types, flags)))
603 if ((error = addtype(&types, TP_VOID)))
610 error = addtype(&types,
611 (flags & LONGINT) ? TP_WCHAR : TP_CHAR);
621 if ((error = adduarg(&types, flags)))
624 default: /* "%?" prints ?, unless ? is NUL */
631 build_arg_table(&types, ap, argtable);
634 return (error || *argtable == NULL);
638 * Increase the size of the type table. Returns 0 on success.
641 __grow_type_table(struct typetable *types)
643 enum typeid *const oldtable = types->table;
644 const int oldsize = types->tablesize;
645 enum typeid *newtable;
648 /* Detect overflow */
649 if (types->nextarg > NL_ARGMAX)
652 newsize = oldsize * 2;
653 if (newsize < types->nextarg + 1)
654 newsize = types->nextarg + 1;
655 if (oldsize == STATIC_ARG_TBL_SIZE) {
656 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
658 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
660 newtable = reallocarray(oldtable, newsize, sizeof(enum typeid));
661 if (newtable == NULL)
664 for (n = oldsize; n < newsize; n++)
665 newtable[n] = T_UNUSED;
667 types->table = newtable;
668 types->tablesize = newsize;
674 * Build the argument table from the completed type table.
675 * On malloc failure, *argtable is set to NULL.
678 build_arg_table(struct typetable *types, va_list ap, union arg **argtable)
682 if (types->tablemax >= STATIC_ARG_TBL_SIZE) {
683 *argtable = (union arg *)
684 malloc (sizeof (union arg) * (types->tablemax + 1));
685 if (*argtable == NULL)
689 (*argtable) [0].intarg = 0;
690 for (n = 1; n <= types->tablemax; n++) {
691 switch (types->table[n]) {
692 case T_UNUSED: /* whoops! */
693 (*argtable) [n].intarg = va_arg (ap, int);
696 (*argtable) [n].pschararg = va_arg (ap, signed char *);
699 (*argtable) [n].pshortarg = va_arg (ap, short *);
702 (*argtable) [n].intarg = va_arg (ap, int);
705 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
708 (*argtable) [n].pintarg = va_arg (ap, int *);
711 (*argtable) [n].longarg = va_arg (ap, long);
714 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
717 (*argtable) [n].plongarg = va_arg (ap, long *);
720 (*argtable) [n].longlongarg = va_arg (ap, long long);
723 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
726 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
729 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
732 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
735 (*argtable) [n].sizearg = va_arg (ap, size_t);
738 (*argtable) [n].sizearg = va_arg (ap, ssize_t);
741 (*argtable) [n].pssizearg = va_arg (ap, ssize_t *);
744 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
747 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
750 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
753 #ifndef NO_FLOATING_POINT
754 (*argtable) [n].doublearg = va_arg (ap, double);
758 #ifndef NO_FLOATING_POINT
759 (*argtable) [n].longdoublearg = va_arg (ap, long double);
763 (*argtable) [n].pchararg = va_arg (ap, char *);
766 (*argtable) [n].pvoidarg = va_arg (ap, void *);
769 (*argtable) [n].wintarg = va_arg (ap, wint_t);
772 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);