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>
49 #include <sys/stddef.h>
50 #include <sys/stdint.h>
56 * Note that stdarg.h and the ANSI style va_start macro is used for both
57 * ANSI and traditional C compilers.
59 #include <machine/stdarg.h>
61 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
63 static char *ksprintn (char *buf, uintmax_t num, int base, int *len);
64 static int kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap);
67 printf(const char *fmt, ...)
73 retval = kvprintf(fmt, putchar, NULL, 10, ap);
79 vprintf(const char *fmt, va_list ap)
82 kvprintf(fmt, putchar, NULL, 10, ap);
86 sprintf(char *buf, const char *cfmt, ...)
92 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
99 vsprintf(char *buf, const char *cfmt, va_list ap)
103 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
108 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
109 * order; return an optional length and a pointer to the last character
110 * written in the buffer (i.e., the first character of the string).
111 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
114 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp)
121 *++p = hex2ascii(num % base);
122 } while (num /= base);
129 * Scaled down version of printf(3).
131 * Two additional formats:
133 * The format %b is supported to decode error registers.
136 * printf("reg=%b\n", regval, "<base><arg>*");
138 * where <base> is the output base expressed as a control character, e.g.
139 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
140 * the first of which gives the bit number to be inspected (origin 1), and
141 * the next characters (up to a control character, i.e. a character <= 32),
142 * give the name of the register. Thus:
144 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
146 * would produce output:
148 * reg=3<BITTWO,BITONE>
150 * XXX: %D -- Hexdump, takes pointer and separator string:
151 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
152 * ("%*D", len, ptr, " " -> XX XX XX XX ...
155 kvprintf(char const *fmt, void (*func)(int), void *arg, int radix, va_list ap)
157 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc); else *d++ = cc; retval++; }
160 const char *p, *percent, *q;
164 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
165 int jflag, tflag, zflag;
177 fmt = "(fmt null)\n";
179 if (radix < 2 || radix > 36)
185 while ((ch = (u_char)*fmt++) != '%') {
191 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
192 sign = 0; dot = 0; dwidth = 0;
193 jflag = 0; tflag = 0; zflag = 0;
194 reswitch: switch (ch = (u_char)*fmt++) {
212 width = va_arg(ap, int);
218 dwidth = va_arg(ap, int);
226 case '1': case '2': case '3': case '4':
227 case '5': case '6': case '7': case '8': case '9':
228 for (n = 0;; ++fmt) {
229 n = n * 10 + ch - '0';
231 if (ch < '0' || ch > '9')
240 num = va_arg(ap, int);
241 p = va_arg(ap, char *);
242 for (q = ksprintn(nbuf, num, *p++, NULL); *q;)
250 if (num & (1 << (n - 1))) {
251 PCHAR(tmp ? ',' : '<');
252 for (; (n = *p) > ' '; ++p)
256 for (; *p > ' '; ++p)
263 PCHAR(va_arg(ap, int));
266 up = va_arg(ap, u_char *);
267 p = va_arg(ap, char *);
271 PCHAR(hex2ascii(*up >> 4));
272 PCHAR(hex2ascii(*up & 0x0f));
296 *(va_arg(ap, intmax_t *)) = retval;
298 *(va_arg(ap, quad_t *)) = retval;
300 *(va_arg(ap, long *)) = retval;
302 *(va_arg(ap, size_t *)) = retval;
304 *(va_arg(ap, int *)) = retval;
311 sharpflag = (width == 0);
313 num = (uintptr_t)va_arg(ap, void *);
324 p = va_arg(ap, char *);
330 for (n = 0; n < dwidth && p[n]; n++)
335 if (!ladjust && width > 0)
340 if (ladjust && width > 0)
364 num = va_arg(ap, uintmax_t);
366 num = va_arg(ap, u_quad_t);
368 num = va_arg(ap, ptrdiff_t);
370 num = va_arg(ap, u_long);
372 num = va_arg(ap, size_t);
374 num = va_arg(ap, u_int);
378 num = va_arg(ap, intmax_t);
380 num = va_arg(ap, quad_t);
382 num = va_arg(ap, ptrdiff_t);
384 num = va_arg(ap, long);
386 num = va_arg(ap, size_t);
388 num = va_arg(ap, int);
390 if (sign && (intmax_t)num < 0) {
392 num = -(intmax_t)num;
394 p = ksprintn(nbuf, num, base, &tmp);
395 if (sharpflag && num != 0) {
404 if (!ladjust && width && (width -= tmp) > 0)
409 if (sharpflag && num != 0) {
412 } else if (base == 16) {
421 if (ladjust && width && (width -= tmp) > 0)
427 while (percent < fmt)