10 typedef unsigned long u_quad_t;
13 typedef unsigned long u_long;
14 typedef unsigned int u_int;
15 typedef unsigned short u_short;
16 typedef long intmax_t;
17 typedef unsigned long uintmax_t;
18 typedef long intptr_t;
19 typedef unsigned long uintptr_t;
25 * Copyright (c) 1986, 1988, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * From NetBSD: subr_prf.c,v 1.86 2002/11/02 07:25:22 perry Exp
58 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
61 /* flags for kprintf */
62 #define TOCONS 0x01 /* to the console */
63 #define TOTTY 0x02 /* to the process' tty */
64 #define TOLOG 0x04 /* to the kernel message buffer */
65 #define TOBUFONLY 0x08 /* to the buffer (only) [for snprintf] */
66 #define TODDB 0x10 /* to ddb console */
68 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
69 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
76 static int kprintf __P((const char *, int, void *,
80 * vsnprintf: print a message to a buffer [already have va_alist]
83 vsnprintf(buf, size, fmt, ap)
95 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
96 *(p) = 0; /* null terminate */
102 * kprintf: scaled down version of printf(3).
104 * this version based on vfprintf() from libc which was derived from
105 * software contributed to Berkeley by Chris Torek.
107 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
111 * macros for converting digits to letters and vice versa
113 #define to_digit(c) ((c) - '0')
114 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
115 #define to_char(n) ((n) + '0')
118 * flags used during conversion.
120 #define ALT 0x001 /* alternate form */
121 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
122 #define LADJUST 0x004 /* left adjustment */
123 #define LONGDBL 0x008 /* long double; unimplemented */
124 #define LONGINT 0x010 /* long integer */
125 #define QUADINT 0x020 /* quad integer */
126 #define SHORTINT 0x040 /* short integer */
127 #define MAXINT 0x080 /* intmax_t */
128 #define PTRINT 0x100 /* intptr_t */
129 #define SIZEINT 0x200 /* size_t */
130 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
131 #define FPT 0x800 /* Floating point number */
134 * To extend shorts properly, we need both signed and unsigned
135 * argument extraction methods.
138 (flags&MAXINT ? va_arg(ap, intmax_t) : \
139 flags&PTRINT ? va_arg(ap, intptr_t) : \
140 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
141 flags&QUADINT ? va_arg(ap, quad_t) : \
142 flags&LONGINT ? va_arg(ap, long) : \
143 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
144 (long)va_arg(ap, int))
146 (flags&MAXINT ? va_arg(ap, uintmax_t) : \
147 flags&PTRINT ? va_arg(ap, uintptr_t) : \
148 flags&SIZEINT ? va_arg(ap, size_t) : \
149 flags&QUADINT ? va_arg(ap, u_quad_t) : \
150 flags&LONGINT ? va_arg(ap, u_long) : \
151 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
152 (u_long)va_arg(ap, u_int))
154 #define KPRINTF_PUTCHAR(C) { \
155 if (oflags == TOBUFONLY) { \
156 if ((vp != NULL) && (sbuf == tailp)) { \
157 ret += 1; /* indicate error */ \
165 * Guts of kernel printf. Note, we already expect to be in a mutex!
168 kprintf(fmt0, oflags, vp, sbuf, ap)
175 char *fmt; /* format string */
176 int ch; /* character from fmt */
177 int n; /* handy integer (short term usage) */
178 char *cp; /* handy char pointer (short term usage) */
179 int flags; /* flags as above */
180 int ret; /* return value accumulator */
181 int width; /* width from format (%8d), or 0 */
182 int prec; /* precision from format (%.3d), or -1 */
183 char sign; /* sign prefix (' ', '+', '-', or \0) */
185 u_quad_t _uquad; /* integer arguments %[diouxX] */
186 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
187 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
188 int realsz; /* field size expanded by dprec */
189 int size; /* size of converted field or string */
190 char *xdigs; /* digits for [xX] conversion */
191 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
192 char *tailp; /* tail pointer for snprintf */
194 tailp = NULL; /* XXX: shutup gcc */
195 if (oflags == TOBUFONLY && (vp != NULL))
196 tailp = *(char **)vp;
198 cp = NULL; /* XXX: shutup gcc */
199 size = 0; /* XXX: shutup gcc */
204 xdigs = NULL; /* XXX: shut up gcc warning */
207 * Scan the format for conversions (`%' character).
210 while (*fmt != '%' && *fmt) {
212 KPRINTF_PUTCHAR(*fmt++);
217 fmt++; /* skip over '%' */
226 reswitch: switch (ch) {
229 * ``If the space and + flags both appear, the space
230 * flag will be ignored.''
241 * ``A negative field width argument is taken as a
242 * - flag followed by a positive field width.''
244 * They don't exclude field widths read from args.
246 if ((width = va_arg(ap, int)) >= 0)
257 if ((ch = *fmt++) == '*') {
259 prec = n < 0 ? -1 : n;
263 while (is_digit(ch)) {
264 n = 10 * n + to_digit(ch);
267 prec = n < 0 ? -1 : n;
271 * ``Note that 0 is taken as a flag, not as the
272 * beginning of a field width.''
277 case '1': case '2': case '3': case '4':
278 case '5': case '6': case '7': case '8': case '9':
281 n = 10 * n + to_digit(ch);
283 } while (is_digit(ch));
310 *(cp = buf) = va_arg(ap, int);
320 if ((quad_t)_uquad < 0) {
328 *va_arg(ap, intmax_t *) = ret;
329 else if (flags & PTRINT)
330 *va_arg(ap, intptr_t *) = ret;
331 else if (flags & SIZEINT)
332 *va_arg(ap, ssize_t *) = ret;
333 else if (flags & QUADINT)
334 *va_arg(ap, quad_t *) = ret;
335 else if (flags & LONGINT)
336 *va_arg(ap, long *) = ret;
337 else if (flags & SHORTINT)
338 *va_arg(ap, short *) = ret;
340 *va_arg(ap, int *) = ret;
341 continue; /* no output */
351 * ``The argument shall be a pointer to void. The
352 * value of the pointer is converted to a sequence
353 * of printable characters, in an implementation-
358 _uquad = (u_long)va_arg(ap, void *);
360 xdigs = "0123456789abcdef";
365 if ((cp = va_arg(ap, char *)) == NULL)
369 * can't use strlen; can only look for the
370 * NUL in the first `prec' characters, and
371 * strlen() will go further.
373 char *p = memchr(cp, 0, prec);
393 xdigs = "0123456789ABCDEF";
396 xdigs = "0123456789abcdef";
397 hex: _uquad = UARG();
399 /* leading 0x/X only if non-zero */
400 if (flags & ALT && _uquad != 0)
403 /* unsigned conversions */
406 * ``... diouXx conversions ... if a precision is
407 * specified, the 0 flag will be ignored.''
410 number: if ((dprec = prec) >= 0)
414 * ``The result of converting a zero value with an
415 * explicit precision of zero is no characters.''
418 cp = buf + KPRINTF_BUFSIZE;
419 if (_uquad != 0 || prec != 0) {
421 * Unsigned mod is hard, and unsigned mod
422 * by a constant is easier than that by
423 * a variable; hence this switch.
428 *--cp = to_char(_uquad & 7);
431 /* handle octal leading 0 */
432 if (flags & ALT && *cp != '0')
437 /* many numbers are 1 digit */
438 while (_uquad >= 10) {
439 *--cp = to_char(_uquad % 10);
442 *--cp = to_char(_uquad);
447 *--cp = xdigs[_uquad & 15];
453 cp = "bug in kprintf: bad base";
458 size = buf + KPRINTF_BUFSIZE - cp;
461 default: /* "%?" prints ?, unless ? is NUL */
464 /* pretend it was %c with argument ch */
473 * All reasonable formats wind up here. At this point, `cp'
474 * points to a string which (if not flags&LADJUST) should be
475 * padded out to `width' places. If flags&ZEROPAD, it should
476 * first be prefixed by any sign or other prefix; otherwise,
477 * it should be blank padded before the prefix is emitted.
478 * After any left-hand padding and prefixing, emit zeroes
479 * required by a decimal [diouxX] precision, then print the
480 * string proper, then emit zeroes required by any leftover
481 * floating precision; finally, if LADJUST, pad with blanks.
483 * Compute actual size, so we know how much to pad.
484 * size excludes decimal prec; realsz includes it.
486 realsz = dprec > size ? dprec : size;
489 else if (flags & HEXPREFIX)
493 ret += width > realsz ? width : realsz;
495 /* right-adjusting blank padding */
496 if ((flags & (LADJUST|ZEROPAD)) == 0) {
499 KPRINTF_PUTCHAR(' ');
504 KPRINTF_PUTCHAR(sign);
505 } else if (flags & HEXPREFIX) {
506 KPRINTF_PUTCHAR('0');
510 /* right-adjusting zero padding */
511 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
514 KPRINTF_PUTCHAR('0');
517 /* leading zeroes from decimal precision */
520 KPRINTF_PUTCHAR('0');
522 /* the string or number proper */
524 KPRINTF_PUTCHAR(*cp++);
525 /* left-adjusting padding (always blank) */
526 if (flags & LADJUST) {
529 KPRINTF_PUTCHAR(' ');
534 if ((oflags == TOBUFONLY) && (vp != NULL))