subr_prf.c revision 1.11 1 /* $NetBSD: subr_prf.c,v 1.11 2005/05/17 04:14:58 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)printf.c 8.1 (Berkeley) 6/11/93
32 */
33
34 /*
35 * Scaled down version of printf(3).
36 */
37
38 #include <sys/cdefs.h>
39 #include <sys/types.h>
40 #include <machine/stdarg.h>
41
42 #include "stand.h"
43
44 static void kprintn(void (*)(int), u_long, int);
45 static void sputchar(int);
46 static void kdoprnt(void (*)(int), const char *, va_list);
47
48 static char *sbuf, *ebuf;
49
50 const char hexdigits[] = "0123456789abcdef";
51
52 static void
53 sputchar(int c)
54 {
55
56 if (sbuf < ebuf)
57 *sbuf++ = c;
58 }
59
60 void
61 vprintf(const char *fmt, va_list ap)
62 {
63
64 kdoprnt(putchar, fmt, ap);
65 }
66
67 int
68 vsnprintf(char *buf, size_t size, const char *fmt, va_list ap)
69 {
70
71 sbuf = buf;
72 ebuf = buf + size - 1;
73 kdoprnt(sputchar, fmt, ap);
74 *sbuf = '\0';
75 return (sbuf - buf);
76 }
77
78 static void
79 kdoprnt(void (*put)(int), const char *fmt, va_list ap)
80 {
81 char *p;
82 int ch;
83 unsigned long ul;
84 int lflag;
85
86 for (;;) {
87 while ((ch = *fmt++) != '%') {
88 if (ch == '\0')
89 return;
90 put(ch);
91 }
92 lflag = 0;
93 reswitch: switch (ch = *fmt++) {
94 case 'l':
95 lflag = 1;
96 goto reswitch;
97 case 'c':
98 ch = va_arg(ap, int);
99 put(ch & 0x7f);
100 break;
101 case 's':
102 p = va_arg(ap, char *);
103 while ((ch = *p++))
104 put(ch);
105 break;
106 case 'd':
107 ul = lflag ?
108 va_arg(ap, long) : va_arg(ap, int);
109 if ((long)ul < 0) {
110 put('-');
111 ul = -(long)ul;
112 }
113 kprintn(put, ul, 10);
114 break;
115 case 'o':
116 ul = lflag ?
117 va_arg(ap, u_long) : va_arg(ap, u_int);
118 kprintn(put, ul, 8);
119 break;
120 case 'u':
121 ul = lflag ?
122 va_arg(ap, u_long) : va_arg(ap, u_int);
123 kprintn(put, ul, 10);
124 break;
125 case 'p':
126 put('0');
127 put('x');
128 lflag = 1;
129 /* fall through */
130 case 'x':
131 ul = lflag ?
132 va_arg(ap, u_long) : va_arg(ap, u_int);
133 kprintn(put, ul, 16);
134 break;
135 default:
136 put('%');
137 if (lflag)
138 put('l');
139 if (ch == '\0')
140 return;
141 put(ch);
142 }
143 }
144 }
145
146 static void
147 kprintn(void (*put)(int), unsigned long ul, int base)
148 {
149 /* hold a long in base 8 */
150 char *p, buf[(sizeof(long) * NBBY / 3) + 1];
151
152 p = buf;
153 do {
154 *p++ = hexdigits[ul % base];
155 } while (ul /= base);
156 do {
157 put(*--p);
158 } while (p > buf);
159 }
160