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 * 4. 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
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * Standaloneified version of the FreeBSD kernel printf family.
44 #include <sys/types.h>
45 #include <sys/stddef.h>
46 #include <sys/stdint.h>
52 * Note that stdarg.h and the ANSI style va_start macro is used for both
53 * ANSI and traditional C compilers.
55 #include <machine/stdarg.h>
57 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
59 static char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
60 static int kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap);
63 printf(const char *fmt, ...)
69 retval = kvprintf(fmt, putchar, NULL, 10, ap);
75 vprintf(const char *fmt, va_list ap)
78 kvprintf(fmt, putchar, NULL, 10, ap);
82 sprintf(char *buf, const char *cfmt, ...)
88 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
95 vsprintf(char *buf, const char *cfmt, va_list ap)
99 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
104 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
105 * order; return an optional length and a pointer to the last character
106 * written in the buffer (i.e., the first character of the string).
107 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
110 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
117 c = hex2ascii(num % base);
118 *++p = upper ? toupper(c) : c;
119 } while (num /= base);
126 * Scaled down version of printf(3).
128 * Two additional formats:
130 * The format %b is supported to decode error registers.
133 * printf("reg=%b\n", regval, "<base><arg>*");
135 * where <base> is the output base expressed as a control character, e.g.
136 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
137 * the first of which gives the bit number to be inspected (origin 1), and
138 * the next characters (up to a control character, i.e. a character <= 32),
139 * give the name of the register. Thus:
141 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
143 * would produce output:
145 * reg=3<BITTWO,BITONE>
147 * XXX: %D -- Hexdump, takes pointer and separator string:
148 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
149 * ("%*D", len, ptr, " " -> XX XX XX XX ...
152 kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap)
154 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc); else *d++ = cc; retval++; }
157 const char *p, *percent, *q;
161 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
162 int cflag, hflag, jflag, tflag, zflag;
165 int stop = 0, retval = 0;
174 fmt = "(fmt null)\n";
176 if (radix < 2 || radix > 36)
182 while ((ch = (u_char)*fmt++) != '%' || stop) {
188 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
189 sign = 0; dot = 0; dwidth = 0; upper = 0;
190 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
191 reswitch: switch (ch = (u_char)*fmt++) {
209 width = va_arg(ap, int);
215 dwidth = va_arg(ap, int);
223 case '1': case '2': case '3': case '4':
224 case '5': case '6': case '7': case '8': case '9':
225 for (n = 0;; ++fmt) {
226 n = n * 10 + ch - '0';
228 if (ch < '0' || ch > '9')
237 num = (u_int)va_arg(ap, int);
238 p = va_arg(ap, char *);
239 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
247 if (num & (1 << (n - 1))) {
248 PCHAR(tmp ? ',' : '<');
249 for (; (n = *p) > ' '; ++p)
253 for (; *p > ' '; ++p)
260 PCHAR(va_arg(ap, int));
263 up = va_arg(ap, u_char *);
264 p = va_arg(ap, char *);
268 PCHAR(hex2ascii(*up >> 4));
269 PCHAR(hex2ascii(*up & 0x0f));
300 *(va_arg(ap, intmax_t *)) = retval;
302 *(va_arg(ap, quad_t *)) = retval;
304 *(va_arg(ap, long *)) = retval;
306 *(va_arg(ap, size_t *)) = retval;
308 *(va_arg(ap, short *)) = retval;
310 *(va_arg(ap, char *)) = retval;
312 *(va_arg(ap, int *)) = retval;
319 sharpflag = (width == 0);
321 num = (uintptr_t)va_arg(ap, void *);
332 p = va_arg(ap, char *);
338 for (n = 0; n < dwidth && p[n]; n++)
343 if (!ladjust && width > 0)
348 if (ladjust && width > 0)
373 num = va_arg(ap, uintmax_t);
375 num = va_arg(ap, u_quad_t);
377 num = va_arg(ap, ptrdiff_t);
379 num = va_arg(ap, u_long);
381 num = va_arg(ap, size_t);
383 num = (u_short)va_arg(ap, int);
385 num = (u_char)va_arg(ap, int);
387 num = va_arg(ap, u_int);
391 num = va_arg(ap, intmax_t);
393 num = va_arg(ap, quad_t);
395 num = va_arg(ap, ptrdiff_t);
397 num = va_arg(ap, long);
399 num = va_arg(ap, ssize_t);
401 num = (short)va_arg(ap, int);
403 num = (char)va_arg(ap, int);
405 num = va_arg(ap, int);
407 if (sign && (intmax_t)num < 0) {
409 num = -(intmax_t)num;
411 p = ksprintn(nbuf, num, base, &n, upper);
413 if (sharpflag && num != 0) {
422 if (!ladjust && padc == '0')
423 dwidth = width - tmp;
424 width -= tmp + imax(dwidth, n);
431 if (sharpflag && num != 0) {
434 } else if (base == 16) {
451 while (percent < fmt)
454 * Since we ignore an formatting argument it is no
455 * longer safe to obey the remaining formatting
456 * arguments as the arguments will no longer match