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
36 * Standaloneified version of the FreeBSD kernel printf family.
39 #include <sys/types.h>
40 #include <sys/stddef.h>
41 #include <sys/stdint.h>
47 * Note that stdarg.h and the ANSI style va_start macro is used for both
48 * ANSI and traditional C compilers.
50 #include <machine/stdarg.h>
52 #define MAXNBUF (sizeof(intmax_t) * CHAR_BIT + 1)
54 typedef void (kvprintf_fn_t)(int, void *);
56 static char *ksprintn (char *buf, uintmax_t num, int base, int *len, int upper);
57 static int kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap);
60 putchar_wrapper(int cc, void *arg)
67 printf(const char *fmt, ...)
73 retval = kvprintf(fmt, putchar_wrapper, NULL, 10, ap);
79 vprintf(const char *fmt, va_list ap)
82 return (kvprintf(fmt, putchar_wrapper, NULL, 10, ap));
86 sprintf(char *buf, const char *cfmt, ...)
92 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
104 snprint_func(int ch, void *arg)
106 struct print_buf *pbuf = arg;
108 if (pbuf->size < 2) {
110 * Reserve last buffer position for the terminating
120 asprintf(char **buf, const char *cfmt, ...)
123 struct print_buf arg;
128 retval = kvprintf(cfmt, NULL, NULL, 10, ap);
133 arg.size = retval + 1;
134 arg.buf = *buf = malloc(arg.size);
139 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
148 snprintf(char *buf, size_t size, const char *cfmt, ...)
152 struct print_buf arg;
158 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
167 vsnprintf(char *buf, size_t size, const char *cfmt, va_list ap)
169 struct print_buf arg;
175 retval = kvprintf(cfmt, &snprint_func, &arg, 10, ap);
184 vsprintf(char *buf, const char *cfmt, va_list ap)
188 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
195 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
196 * order; return an optional length and a pointer to the last character
197 * written in the buffer (i.e., the first character of the string).
198 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
201 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
208 c = hex2ascii(num % base);
209 *++p = upper ? toupper(c) : c;
210 } while (num /= base);
217 * Scaled down version of printf(3).
219 * Two additional formats:
221 * The format %b is supported to decode error registers.
224 * printf("reg=%b\n", regval, "<base><arg>*");
226 * where <base> is the output base expressed as a control character, e.g.
227 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
228 * the first of which gives the bit number to be inspected (origin 1), and
229 * the next characters (up to a control character, i.e. a character <= 32),
230 * give the name of the register. Thus:
232 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
234 * would produce output:
236 * reg=3<BITTWO,BITONE>
238 * XXX: %D -- Hexdump, takes pointer and separator string:
239 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
240 * ("%*D", len, ptr, " " -> XX XX XX XX ...
243 kvprintf(char const *fmt, kvprintf_fn_t *func, void *arg, int radix, va_list ap)
250 } else if (d != NULL) { \
258 const char *p, *percent, *q;
263 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
264 int cflag, hflag, jflag, tflag, zflag;
267 int stop = 0, retval = 0;
277 fmt = "(fmt null)\n";
279 if (radix < 2 || radix > 36)
285 while ((ch = (u_char)*fmt++) != '%' || stop) {
293 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
294 sign = 0; dot = 0; dwidth = 0; upper = 0;
295 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
296 reswitch: switch (ch = (u_char)*fmt++) {
314 width = va_arg(ap, int);
320 dwidth = va_arg(ap, int);
328 case '1': case '2': case '3': case '4':
329 case '5': case '6': case '7': case '8': case '9':
330 for (n = 0;; ++fmt) {
331 n = n * 10 + ch - '0';
333 if (ch < '0' || ch > '9')
342 num = (u_int)va_arg(ap, int);
343 p = va_arg(ap, char *);
344 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
352 if (num & (1 << (n - 1))) {
353 PCHAR(tmp ? ',' : '<');
354 for (; (n = *p) > ' '; ++p)
358 for (; *p > ' '; ++p)
365 PCHAR(va_arg(ap, int));
368 up = va_arg(ap, u_char *);
369 p = va_arg(ap, char *);
373 PCHAR(hex2ascii(*up >> 4));
374 PCHAR(hex2ascii(*up & 0x0f));
405 *(va_arg(ap, intmax_t *)) = retval;
407 *(va_arg(ap, quad_t *)) = retval;
409 *(va_arg(ap, long *)) = retval;
411 *(va_arg(ap, size_t *)) = retval;
413 *(va_arg(ap, short *)) = retval;
415 *(va_arg(ap, char *)) = retval;
417 *(va_arg(ap, int *)) = retval;
424 sharpflag = (width == 0);
426 num = (uintptr_t)va_arg(ap, void *);
437 p = va_arg(ap, char *);
443 for (n = 0; n < dwidth && p[n]; n++)
448 if (!ladjust && width > 0)
453 if (ladjust && width > 0)
457 case 'S': /* Assume console can cope with wide chars */
458 for (S = va_arg(ap, uint16_t *); *S != 0; S++)
482 num = va_arg(ap, uintmax_t);
484 num = va_arg(ap, u_quad_t);
486 num = va_arg(ap, ptrdiff_t);
488 num = va_arg(ap, u_long);
490 num = va_arg(ap, size_t);
492 num = (u_short)va_arg(ap, int);
494 num = (u_char)va_arg(ap, int);
496 num = va_arg(ap, u_int);
500 num = va_arg(ap, intmax_t);
502 num = va_arg(ap, quad_t);
504 num = va_arg(ap, ptrdiff_t);
506 num = va_arg(ap, long);
508 num = va_arg(ap, ssize_t);
510 num = (short)va_arg(ap, int);
512 num = (char)va_arg(ap, int);
514 num = va_arg(ap, int);
516 if (sign && (intmax_t)num < 0) {
518 num = -(intmax_t)num;
520 p = ksprintn(nbuf, num, base, &n, upper);
522 if (sharpflag && num != 0) {
531 if (!ladjust && padc == '0')
532 dwidth = width - tmp;
533 width -= tmp + imax(dwidth, n);
540 if (sharpflag && num != 0) {
543 } else if (base == 16) {
560 while (percent < fmt)
563 * Since we ignore a formatting argument it is no
564 * longer safe to obey the remaining formatting
565 * arguments as the arguments will no longer match