|
|
1.1 root 1: /*-
2: * Copyright (c) 1991 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by the University of
16: * California, Berkeley and its contributors.
17: * 4. Neither the name of the University nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: *
33: * @(#)printf.c 5.6 (Berkeley) 5/25/91
34: */
35:
36: /*
37: * Scaled down version of printf(3).
38: *
39: * One additional format:
40: *
41: * The format %b is supported to decode error registers.
42: * Its usage is:
43: *
44: * printf("reg=%b\n", regval, "<base><arg>*");
45: *
46: * where <base> is the output base expressed as a control character, e.g.
47: * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
48: * the first of which gives the bit number to be inspected (origin 1), and
49: * the next characters (up to a control character, i.e. a character <= 32),
50: * give the name of the register. Thus:
51: *
52: * printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
53: *
54: * would produce output:
55: *
56: * reg=3<BITTWO,BITONE>
57: */
58:
59: #include <sys/cdefs.h>
60: #include <sys/types.h>
61:
62: /*
63: * Note that stdarg.h and the ANSI style va_start macro is used for both
64: * ANSI and traditional C compilers.
65: */
66: #define KERNEL
67: #include <machine/stdarg.h>
68: #undef KERNEL
69:
70: static void kprintn __P((u_long, int));
71:
72: void
73: #if __STDC__
74: printf(const char *fmt, ...)
75: #else
76: printf(fmt /* , va_alist */)
77: char *fmt;
78: #endif
79: {
80: register char *p;
81: register int ch, n;
82: unsigned long ul;
83: int lflag, set;
84: va_list ap;
85:
86: va_start(ap, fmt);
87: for (;;) {
88: while ((ch = *fmt++) != '%') {
89: if (ch == '\0')
90: return;
91: putchar(ch);
92: }
93: lflag = 0;
94: reswitch: switch (ch = *fmt++) {
95: case 'l':
96: lflag = 1;
97: goto reswitch;
1.1.1.2 ! root 98: #ifdef notdef
1.1 root 99: case 'b':
100: ul = va_arg(ap, int);
101: p = va_arg(ap, char *);
102: kprintn(ul, *p++);
103:
104: if (!ul)
105: break;
106:
107: for (set = 0; n = *p++;) {
108: if (ul & (1 << (n - 1))) {
109: putchar(set ? ',' : '<');
110: for (; (n = *p) > ' '; ++p)
111: putchar(n);
112: set = 1;
113: } else
114: for (; *p > ' '; ++p);
115: }
116: if (set)
117: putchar('>');
118: break;
1.1.1.2 ! root 119: #endif
1.1 root 120: case 'c':
121: ch = va_arg(ap, int);
122: putchar(ch & 0x7f);
123: break;
124: case 's':
125: p = va_arg(ap, char *);
126: while (ch = *p++)
127: putchar(ch);
128: break;
129: case 'd':
130: ul = lflag ?
131: va_arg(ap, long) : va_arg(ap, int);
132: if ((long)ul < 0) {
133: putchar('-');
134: ul = -(long)ul;
135: }
136: kprintn(ul, 10);
137: break;
138: case 'o':
139: ul = lflag ?
140: va_arg(ap, u_long) : va_arg(ap, u_int);
141: kprintn(ul, 8);
142: break;
143: case 'u':
144: ul = lflag ?
145: va_arg(ap, u_long) : va_arg(ap, u_int);
146: kprintn(ul, 10);
147: break;
148: case 'x':
149: ul = lflag ?
150: va_arg(ap, u_long) : va_arg(ap, u_int);
151: kprintn(ul, 16);
152: break;
153: default:
154: putchar('%');
155: if (lflag)
156: putchar('l');
157: putchar(ch);
158: }
159: }
160: va_end(ap);
161: }
162:
163: static void
164: kprintn(ul, base)
165: unsigned long ul;
166: int base;
167: {
168: /* hold a long in base 8 */
169: char *p, buf[(sizeof(long) * NBBY / 3) + 1];
170:
171: p = buf;
172: do {
173: *p++ = "0123456789abcdef"[ul % base];
174: } while (ul /= base);
175: do {
176: putchar(*--p);
177: } while (p > buf);
178: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.