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
39 * $FreeBSD: src/lib/libstand/printf.c,v 1.14 2010/07/12 15:32:45 jkim Exp $
43 * Standaloneified version of the FreeBSD kernel printf family.
46 #include <sys/types.h>
47 #include <sys/stdint.h>
53 * Note that stdarg.h and the ANSI style va_start macro is used for both
54 * ANSI and traditional C compilers.
63 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
65 static char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
66 static int kvprintf(const char *, void (*)(int, void *), void *, int,
68 static void putchar_wrapper(int, void *);
69 static void snprintf_func(int, void *);
72 putchar_wrapper(int ch, void *arg __unused)
78 printf(const char *fmt, ...)
84 retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
90 vprintf(const char *fmt, va_list ap)
93 kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
97 sprintf(char *buf, const char *cfmt, ...)
103 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
110 vsprintf(char *buf, const char *cfmt, va_list ap)
114 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
119 snprintf(char *buf, size_t size, const char *cfmt, ...)
125 retval = vsnprintf(buf, size, cfmt, ap);
131 vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
133 struct snprintf_arg info;
138 retval = kvprintf(cfmt, snprintf_func, &info, 10, ap);
139 if (info.remain >= 1)
145 snprintf_func(int ch, void *arg)
147 struct snprintf_arg * const info = arg;
149 if (info->remain >= 2) {
156 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
157 * order; return an optional length and a pointer to the last character
158 * written in the buffer (i.e., the first character of the string).
159 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
162 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
169 c = hex2ascii(num % base);
170 *++p = upper ? toupper(c) : c;
171 } while (num /= base);
178 * Scaled down version of printf(3).
180 * Two additional formats:
182 * The format %b is supported to decode error registers.
185 * printf("reg=%b\n", regval, "<base><arg>*");
187 * where <base> is the output base expressed as a control character, e.g.
188 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
189 * the first of which gives the bit number to be inspected (origin 1), and
190 * the next characters (up to a control character, i.e. a character <= 32),
191 * give the name of the register. Thus:
193 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
195 * would produce output:
197 * reg=3<BITTWO,BITONE>
199 * XXX: %D -- Hexdump, takes pointer and separator string:
200 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
201 * ("%*D", len, ptr, " " -> XX XX XX XX ...
204 kvprintf(char const *fmt, void (*func)(int, void *), void *arg, int radix,
207 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc, arg); else *d++ = cc; retval++; }
210 const char *p, *percent, *q;
214 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
215 int cflag, hflag, jflag, tflag, zflag;
218 int stop = 0, retval = 0;
227 fmt = "(fmt null)\n";
229 if (radix < 2 || radix > 36)
235 while ((ch = (u_char)*fmt++) != '%' || stop) {
241 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
242 sign = 0; dot = 0; dwidth = 0; upper = 0;
243 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
244 reswitch: switch (ch = (u_char)*fmt++) {
262 width = va_arg(ap, int);
268 dwidth = va_arg(ap, int);
276 case '1': case '2': case '3': case '4':
277 case '5': case '6': case '7': case '8': case '9':
278 for (n = 0;; ++fmt) {
279 n = n * 10 + ch - '0';
281 if (ch < '0' || ch > '9')
290 num = (u_int)va_arg(ap, int);
291 p = va_arg(ap, char *);
292 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
300 if (num & (1 << (n - 1))) {
301 PCHAR(tmp ? ',' : '<');
302 for (; (n = *p) > ' '; ++p)
306 for (; *p > ' '; ++p)
313 PCHAR(va_arg(ap, int));
316 up = va_arg(ap, u_char *);
317 p = va_arg(ap, char *);
321 PCHAR(hex2ascii(*up >> 4));
322 PCHAR(hex2ascii(*up & 0x0f));
353 *(va_arg(ap, intmax_t *)) = retval;
355 *(va_arg(ap, quad_t *)) = retval;
357 *(va_arg(ap, long *)) = retval;
359 *(va_arg(ap, size_t *)) = retval;
361 *(va_arg(ap, short *)) = retval;
363 *(va_arg(ap, char *)) = retval;
365 *(va_arg(ap, int *)) = retval;
372 sharpflag = (width == 0);
374 num = (uintptr_t)va_arg(ap, void *);
385 p = va_arg(ap, char *);
391 for (n = 0; n < dwidth && p[n]; n++)
396 if (!ladjust && width > 0)
401 if (ladjust && width > 0)
426 num = va_arg(ap, uintmax_t);
428 num = va_arg(ap, u_quad_t);
430 num = va_arg(ap, ptrdiff_t);
432 num = va_arg(ap, u_long);
434 num = va_arg(ap, size_t);
436 num = (u_short)va_arg(ap, int);
438 num = (u_char)va_arg(ap, int);
440 num = va_arg(ap, u_int);
444 num = va_arg(ap, intmax_t);
446 num = va_arg(ap, quad_t);
448 num = va_arg(ap, ptrdiff_t);
450 num = va_arg(ap, long);
452 num = va_arg(ap, ssize_t);
454 num = (short)va_arg(ap, int);
456 num = (char)va_arg(ap, int);
458 num = va_arg(ap, int);
460 if (sign && (intmax_t)num < 0) {
462 num = -(intmax_t)num;
464 p = ksprintn(nbuf, num, base, &n, upper);
466 if (sharpflag && num != 0) {
475 if (!ladjust && padc == '0')
476 dwidth = width - tmp;
477 width -= tmp + imax(dwidth, n);
484 if (sharpflag && num != 0) {
487 } else if (base == 16) {
504 while (percent < fmt)
507 * Since we ignore an formatting argument it is no
508 * longer safe to obey the remaining formatting
509 * arguments as the arguments will no longer match