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. 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>
39 * Standaloneified version of the FreeBSD kernel printf family.
42 #include <sys/types.h>
43 #include <sys/stddef.h>
44 #include <sys/stdint.h>
50 * Note that stdarg.h and the ANSI style va_start macro is used for both
51 * ANSI and traditional C compilers.
53 #include <machine/stdarg.h>
55 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
57 typedef void (kvprintf_fn_t)(int, void *);
59 static char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
60 static int kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap);
63 putchar_wrapper(int cc, void *arg)
70 printf(const char *fmt, ...)
76 retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
82 vprintf(const char *fmt, va_list ap)
85 return (kvprintf(fmt, putchar_wrapper, NULL, 10, ap));
89 sprintf(char *buf, const char *cfmt, ...)
95 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
107 snprint_func(int ch, void *arg)
109 struct print_buf *pbuf = arg;
111 if (pbuf->size < 2) {
113 * Reserve last buffer position for the terminating
123 asprintf(char **buf, const char *cfmt, ...)
126 struct print_buf arg;
131 retval = kvprintf(cfmt, NULL, NULL, 10, ap);
136 arg.size = retval + 1;
137 arg.buf = *buf = malloc(arg.size);
142 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
151 snprintf(char *buf, size_t size, const char *cfmt, ...)
155 struct print_buf arg;
161 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
170 vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
172 struct print_buf arg;
178 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
187 vsprintf(char *buf, const char *cfmt, va_list ap)
191 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
198 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
199 * order; return an optional length and a pointer to the last character
200 * written in the buffer (i.e., the first character of the string).
201 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
204 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
211 c = hex2ascii(num % base);
212 *++p = upper ? toupper(c) : c;
213 } while (num /= base);
220 * Scaled down version of printf(3).
222 * Two additional formats:
224 * The format %b is supported to decode error registers.
227 * printf("reg=%b\n", regval, "<base><arg>*");
229 * where <base> is the output base expressed as a control character, e.g.
230 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
231 * the first of which gives the bit number to be inspected (origin 1), and
232 * the next characters (up to a control character, i.e. a character <= 32),
233 * give the name of the register. Thus:
235 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
237 * would produce output:
239 * reg=3<BITTWO,BITONE>
241 * XXX: %D -- Hexdump, takes pointer and separator string:
242 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
243 * ("%*D", len, ptr, " " -> XX XX XX XX ...
246 kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap)
253 } else if (d != NULL) { \
261 const char *p, *percent, *q;
266 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
267 int cflag, hflag, jflag, tflag, zflag;
270 int stop = 0, retval = 0;
280 fmt = "(fmt null)\n";
282 if (radix < 2 || radix > 36)
288 while ((ch = (u_char)*fmt++) != '%' || stop) {
296 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
297 sign = 0; dot = 0; dwidth = 0; upper = 0;
298 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
299 reswitch: switch (ch = (u_char)*fmt++) {
317 width = va_arg(ap, int);
323 dwidth = va_arg(ap, int);
331 case '1': case '2': case '3': case '4':
332 case '5': case '6': case '7': case '8': case '9':
333 for (n = 0;; ++fmt) {
334 n = n * 10 + ch - '0';
336 if (ch < '0' || ch > '9')
345 num = (u_int)va_arg(ap, int);
346 p = va_arg(ap, char *);
347 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
355 if (num & (1 << (n - 1))) {
356 PCHAR(tmp ? ',' : '<');
357 for (; (n = *p) > ' '; ++p)
361 for (; *p > ' '; ++p)
368 PCHAR(va_arg(ap, int));
371 up = va_arg(ap, u_char *);
372 p = va_arg(ap, char *);
376 PCHAR(hex2ascii(*up >> 4));
377 PCHAR(hex2ascii(*up & 0x0f));
408 *(va_arg(ap, intmax_t *)) = retval;
410 *(va_arg(ap, quad_t *)) = retval;
412 *(va_arg(ap, long *)) = retval;
414 *(va_arg(ap, size_t *)) = retval;
416 *(va_arg(ap, short *)) = retval;
418 *(va_arg(ap, char *)) = retval;
420 *(va_arg(ap, int *)) = retval;
427 sharpflag = (width == 0);
429 num = (uintptr_t)va_arg(ap, void *);
440 p = va_arg(ap, char *);
446 for (n = 0; n < dwidth && p[n]; n++)
451 if (!ladjust && width > 0)
456 if (ladjust && width > 0)
460 case 'S': /* Assume console can cope with wide chars */
461 for (S = va_arg(ap, uint16_t *); *S != 0; S++)
485 num = va_arg(ap, uintmax_t);
487 num = va_arg(ap, u_quad_t);
489 num = va_arg(ap, ptrdiff_t);
491 num = va_arg(ap, u_long);
493 num = va_arg(ap, size_t);
495 num = (u_short)va_arg(ap, int);
497 num = (u_char)va_arg(ap, int);
499 num = va_arg(ap, u_int);
503 num = va_arg(ap, intmax_t);
505 num = va_arg(ap, quad_t);
507 num = va_arg(ap, ptrdiff_t);
509 num = va_arg(ap, long);
511 num = va_arg(ap, ssize_t);
513 num = (short)va_arg(ap, int);
515 num = (char)va_arg(ap, int);
517 num = va_arg(ap, int);
519 if (sign && (intmax_t)num < 0) {
521 num = -(intmax_t)num;
523 p = ksprintn(nbuf, num, base, &n, upper);
525 if (sharpflag && num != 0) {
534 if (!ladjust && padc == '0')
535 dwidth = width - tmp;
536 width -= tmp + imax(dwidth, n);
543 if (sharpflag && num != 0) {
546 } else if (base == 16) {
563 while (percent < fmt)
566 * Since we ignore a formatting argument it is no
567 * longer safe to obey the remaining formatting
568 * arguments as the arguments will no longer match