2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 * Standaloneified version of the FreeBSD kernel printf family.
48 #include <sys/types.h>
53 * Note that stdarg.h and the ANSI style va_start macro is used for both
54 * ANSI and traditional C compilers.
56 #include <machine/stdarg.h>
58 static char *ksprintn (u_long num, int base, int *len);
59 static int kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap);
62 printf(const char *fmt, ...)
68 retval = kvprintf(fmt, putchar, NULL, 10, ap);
74 vprintf(const char *fmt, va_list ap)
77 kvprintf(fmt, putchar, NULL, 10, ap);
81 sprintf(char *buf, const char *cfmt, ...)
87 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
94 vsprintf(char *buf, const char *cfmt, va_list ap)
98 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
103 * Put a number (base <= 16) in a buffer in reverse order; return an
104 * optional length and a pointer to the NULL terminated (preceded?)
108 ksprintn(ul, base, lenp)
111 { /* A long in base 8, plus NULL. */
112 static char buf[sizeof(long) * NBBY / 3 + 2];
117 *++p = hex2ascii(ul % base);
118 } while (ul /= base);
125 * Scaled down version of printf(3).
127 * Two additional formats:
129 * The format %b is supported to decode error registers.
132 * printf("reg=%b\n", regval, "<base><arg>*");
134 * where <base> is the output base expressed as a control character, e.g.
135 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
136 * the first of which gives the bit number to be inspected (origin 1), and
137 * the next characters (up to a control character, i.e. a character <= 32),
138 * give the name of the register. Thus:
140 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
142 * would produce output:
144 * reg=3<BITTWO,BITONE>
146 * XXX: %D -- Hexdump, takes pointer and separator string:
147 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
148 * ("%*D", len, ptr, " " -> XX XX XX XX ...
151 kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap)
153 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc); else *d++ = cc; retval++; }
158 int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
169 fmt = "(fmt null)\n";
171 if (radix < 2 || radix > 36)
177 while ((ch = (u_char)*fmt++) != '%') {
182 lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
183 sign = 0; dot = 0; dwidth = 0;
184 reswitch: switch (ch = (u_char)*fmt++) {
202 width = va_arg(ap, int);
208 dwidth = va_arg(ap, int);
216 case '1': case '2': case '3': case '4':
217 case '5': case '6': case '7': case '8': case '9':
218 for (n = 0;; ++fmt) {
219 n = n * 10 + ch - '0';
221 if (ch < '0' || ch > '9')
230 ul = va_arg(ap, int);
231 p = va_arg(ap, char *);
232 for (q = ksprintn(ul, *p++, NULL); *q;)
240 if (ul & (1 << (n - 1))) {
241 PCHAR(tmp ? ',' : '<');
242 for (; (n = *p) > ' '; ++p)
246 for (; *p > ' '; ++p)
253 PCHAR(va_arg(ap, int));
256 up = va_arg(ap, u_char *);
257 p = va_arg(ap, char *);
261 PCHAR(hex2ascii(*up >> 4));
262 PCHAR(hex2ascii(*up & 0x0f));
270 ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
278 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
282 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
286 ul = (u_long)va_arg(ap, void *);
291 p = va_arg(ap, char *);
297 for (n = 0; n < dwidth && p[n]; n++)
302 if (!ladjust && width > 0)
307 if (ladjust && width > 0)
312 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
316 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
318 number: if (sign && (long)ul < 0L) {
322 p = ksprintn(ul, base, &tmp);
323 if (sharpflag && ul != 0) {
332 if (!ladjust && width && (width -= tmp) > 0)
337 if (sharpflag && ul != 0) {
340 } else if (base == 16) {
349 if (ladjust && width && (width -= tmp) > 0)