subr_prf.c revision 1.107.10.2 1 1.107.10.2 pooka /* $NetBSD: subr_prf.c,v 1.107.10.2 2007/07/29 09:38:02 pooka Exp $ */
2 1.107.10.2 pooka
3 1.107.10.2 pooka /*-
4 1.107.10.2 pooka * Copyright (c) 1986, 1988, 1991, 1993
5 1.107.10.2 pooka * The Regents of the University of California. All rights reserved.
6 1.107.10.2 pooka * (c) UNIX System Laboratories, Inc.
7 1.107.10.2 pooka * All or some portions of this file are derived from material licensed
8 1.107.10.2 pooka * to the University of California by American Telephone and Telegraph
9 1.107.10.2 pooka * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.107.10.2 pooka * the permission of UNIX System Laboratories, Inc.
11 1.107.10.2 pooka *
12 1.107.10.2 pooka * Redistribution and use in source and binary forms, with or without
13 1.107.10.2 pooka * modification, are permitted provided that the following conditions
14 1.107.10.2 pooka * are met:
15 1.107.10.2 pooka * 1. Redistributions of source code must retain the above copyright
16 1.107.10.2 pooka * notice, this list of conditions and the following disclaimer.
17 1.107.10.2 pooka * 2. Redistributions in binary form must reproduce the above copyright
18 1.107.10.2 pooka * notice, this list of conditions and the following disclaimer in the
19 1.107.10.2 pooka * documentation and/or other materials provided with the distribution.
20 1.107.10.2 pooka * 3. Neither the name of the University nor the names of its contributors
21 1.107.10.2 pooka * may be used to endorse or promote products derived from this software
22 1.107.10.2 pooka * without specific prior written permission.
23 1.107.10.2 pooka *
24 1.107.10.2 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.107.10.2 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.107.10.2 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.107.10.2 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.107.10.2 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.107.10.2 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.107.10.2 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.107.10.2 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.107.10.2 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.107.10.2 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.107.10.2 pooka * SUCH DAMAGE.
35 1.107.10.2 pooka *
36 1.107.10.2 pooka * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
37 1.107.10.2 pooka */
38 1.107.10.2 pooka
39 1.107.10.2 pooka #include <sys/cdefs.h>
40 1.107.10.2 pooka __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.107.10.2 2007/07/29 09:38:02 pooka Exp $");
41 1.107.10.2 pooka
42 1.107.10.2 pooka #include "opt_ddb.h"
43 1.107.10.2 pooka #include "opt_ipkdb.h"
44 1.107.10.2 pooka #include "opt_kgdb.h"
45 1.107.10.2 pooka #include "opt_multiprocessor.h"
46 1.107.10.2 pooka #include "opt_dump.h"
47 1.107.10.2 pooka
48 1.107.10.2 pooka #include <sys/param.h>
49 1.107.10.2 pooka #include <sys/stdint.h>
50 1.107.10.2 pooka #include <sys/systm.h>
51 1.107.10.2 pooka #include <sys/buf.h>
52 1.107.10.2 pooka #include <sys/reboot.h>
53 1.107.10.2 pooka #include <sys/msgbuf.h>
54 1.107.10.2 pooka #include <sys/proc.h>
55 1.107.10.2 pooka #include <sys/ioctl.h>
56 1.107.10.2 pooka #include <sys/vnode.h>
57 1.107.10.2 pooka #include <sys/file.h>
58 1.107.10.2 pooka #include <sys/tty.h>
59 1.107.10.2 pooka #include <sys/tprintf.h>
60 1.107.10.2 pooka #include <sys/syslog.h>
61 1.107.10.2 pooka #include <sys/malloc.h>
62 1.107.10.2 pooka #include <sys/lock.h>
63 1.107.10.2 pooka #include <sys/kprintf.h>
64 1.107.10.2 pooka
65 1.107.10.2 pooka #include <dev/cons.h>
66 1.107.10.2 pooka
67 1.107.10.2 pooka #ifdef DDB
68 1.107.10.2 pooka #include <ddb/ddbvar.h>
69 1.107.10.2 pooka #include <machine/db_machdep.h>
70 1.107.10.2 pooka #include <ddb/db_command.h>
71 1.107.10.2 pooka #include <ddb/db_interface.h>
72 1.107.10.2 pooka #endif
73 1.107.10.2 pooka
74 1.107.10.2 pooka #ifdef IPKDB
75 1.107.10.2 pooka #include <ipkdb/ipkdb.h>
76 1.107.10.2 pooka #endif
77 1.107.10.2 pooka
78 1.107.10.2 pooka #if defined(MULTIPROCESSOR)
79 1.107.10.2 pooka struct simplelock kprintf_slock = SIMPLELOCK_INITIALIZER;
80 1.107.10.2 pooka #endif /* MULTIPROCESSOR */
81 1.107.10.2 pooka
82 1.107.10.2 pooka /*
83 1.107.10.2 pooka * note that stdarg.h and the ansi style va_start macro is used for both
84 1.107.10.2 pooka * ansi and traditional c complers.
85 1.107.10.2 pooka * XXX: this requires that stdarg.h define: va_alist and va_dcl
86 1.107.10.2 pooka */
87 1.107.10.2 pooka #include <machine/stdarg.h>
88 1.107.10.2 pooka
89 1.107.10.2 pooka
90 1.107.10.2 pooka #ifdef KGDB
91 1.107.10.2 pooka #include <sys/kgdb.h>
92 1.107.10.2 pooka #include <machine/cpu.h>
93 1.107.10.2 pooka #endif
94 1.107.10.2 pooka #ifdef DDB
95 1.107.10.2 pooka #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
96 1.107.10.2 pooka #endif
97 1.107.10.2 pooka
98 1.107.10.2 pooka
99 1.107.10.2 pooka /*
100 1.107.10.2 pooka * defines
101 1.107.10.2 pooka */
102 1.107.10.2 pooka
103 1.107.10.2 pooka
104 1.107.10.2 pooka /*
105 1.107.10.2 pooka * local prototypes
106 1.107.10.2 pooka */
107 1.107.10.2 pooka
108 1.107.10.2 pooka static void putchar(int, int, struct tty *);
109 1.107.10.2 pooka
110 1.107.10.2 pooka
111 1.107.10.2 pooka /*
112 1.107.10.2 pooka * globals
113 1.107.10.2 pooka */
114 1.107.10.2 pooka
115 1.107.10.2 pooka extern struct tty *constty; /* pointer to console "window" tty */
116 1.107.10.2 pooka extern int log_open; /* subr_log: is /dev/klog open? */
117 1.107.10.2 pooka const char *panicstr; /* arg to first call to panic (used as a flag
118 1.107.10.2 pooka to indicate that panic has already been called). */
119 1.107.10.2 pooka long panicstart, panicend; /* position in the msgbuf of the start and
120 1.107.10.2 pooka end of the formatted panicstr. */
121 1.107.10.2 pooka int doing_shutdown; /* set to indicate shutdown in progress */
122 1.107.10.2 pooka
123 1.107.10.2 pooka #ifndef DUMP_ON_PANIC
124 1.107.10.2 pooka #define DUMP_ON_PANIC 1
125 1.107.10.2 pooka #endif
126 1.107.10.2 pooka int dumponpanic = DUMP_ON_PANIC;
127 1.107.10.2 pooka
128 1.107.10.2 pooka /*
129 1.107.10.2 pooka * v_putc: routine to putc on virtual console
130 1.107.10.2 pooka *
131 1.107.10.2 pooka * the v_putc pointer can be used to redirect the console cnputc elsewhere
132 1.107.10.2 pooka * [e.g. to a "virtual console"].
133 1.107.10.2 pooka */
134 1.107.10.2 pooka
135 1.107.10.2 pooka void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */
136 1.107.10.2 pooka void (*v_flush)(void) = cnflush; /* start with cnflush (normal cons) */
137 1.107.10.2 pooka
138 1.107.10.2 pooka const char hexdigits[] = "0123456789abcdef";
139 1.107.10.2 pooka const char HEXDIGITS[] = "0123456789ABCDEF";
140 1.107.10.2 pooka
141 1.107.10.2 pooka
142 1.107.10.2 pooka /*
143 1.107.10.2 pooka * functions
144 1.107.10.2 pooka */
145 1.107.10.2 pooka
146 1.107.10.2 pooka /*
147 1.107.10.2 pooka * tablefull: warn that a system table is full
148 1.107.10.2 pooka */
149 1.107.10.2 pooka
150 1.107.10.2 pooka void
151 1.107.10.2 pooka tablefull(const char *tab, const char *hint)
152 1.107.10.2 pooka {
153 1.107.10.2 pooka if (hint)
154 1.107.10.2 pooka log(LOG_ERR, "%s: table is full - %s\n", tab, hint);
155 1.107.10.2 pooka else
156 1.107.10.2 pooka log(LOG_ERR, "%s: table is full\n", tab);
157 1.107.10.2 pooka }
158 1.107.10.2 pooka
159 1.107.10.2 pooka /*
160 1.107.10.2 pooka * twiddle: spin a little propellor on the console.
161 1.107.10.2 pooka */
162 1.107.10.2 pooka
163 1.107.10.2 pooka void
164 1.107.10.2 pooka twiddle(void)
165 1.107.10.2 pooka {
166 1.107.10.2 pooka static const char twiddle_chars[] = "|/-\\";
167 1.107.10.2 pooka static int pos;
168 1.107.10.2 pooka int s;
169 1.107.10.2 pooka
170 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
171 1.107.10.2 pooka
172 1.107.10.2 pooka putchar(twiddle_chars[pos++ & 3], TOCONS, NULL);
173 1.107.10.2 pooka putchar('\b', TOCONS, NULL);
174 1.107.10.2 pooka
175 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
176 1.107.10.2 pooka }
177 1.107.10.2 pooka
178 1.107.10.2 pooka /*
179 1.107.10.2 pooka * panic: handle an unresolvable fatal error
180 1.107.10.2 pooka *
181 1.107.10.2 pooka * prints "panic: <message>" and reboots. if called twice (i.e. recursive
182 1.107.10.2 pooka * call) we avoid trying to sync the disk and just reboot (to avoid
183 1.107.10.2 pooka * recursive panics).
184 1.107.10.2 pooka */
185 1.107.10.2 pooka
186 1.107.10.2 pooka void
187 1.107.10.2 pooka panic(const char *fmt, ...)
188 1.107.10.2 pooka {
189 1.107.10.2 pooka int bootopt;
190 1.107.10.2 pooka va_list ap;
191 1.107.10.2 pooka
192 1.107.10.2 pooka bootopt = RB_AUTOBOOT;
193 1.107.10.2 pooka if (dumponpanic)
194 1.107.10.2 pooka bootopt |= RB_DUMP;
195 1.107.10.2 pooka if (doing_shutdown)
196 1.107.10.2 pooka bootopt |= RB_NOSYNC;
197 1.107.10.2 pooka if (!panicstr)
198 1.107.10.2 pooka panicstr = fmt;
199 1.107.10.2 pooka doing_shutdown = 1;
200 1.107.10.2 pooka
201 1.107.10.2 pooka if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
202 1.107.10.2 pooka panicstart = msgbufp->msg_bufx;
203 1.107.10.2 pooka
204 1.107.10.2 pooka va_start(ap, fmt);
205 1.107.10.2 pooka printf("panic: ");
206 1.107.10.2 pooka vprintf(fmt, ap);
207 1.107.10.2 pooka printf("\n");
208 1.107.10.2 pooka va_end(ap);
209 1.107.10.2 pooka
210 1.107.10.2 pooka if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
211 1.107.10.2 pooka panicend = msgbufp->msg_bufx;
212 1.107.10.2 pooka
213 1.107.10.2 pooka #ifdef IPKDB
214 1.107.10.2 pooka ipkdb_panic();
215 1.107.10.2 pooka #endif
216 1.107.10.2 pooka #ifdef KGDB
217 1.107.10.2 pooka kgdb_panic();
218 1.107.10.2 pooka #endif
219 1.107.10.2 pooka #ifdef KADB
220 1.107.10.2 pooka if (boothowto & RB_KDB)
221 1.107.10.2 pooka kdbpanic();
222 1.107.10.2 pooka #endif
223 1.107.10.2 pooka #ifdef DDB
224 1.107.10.2 pooka if (db_onpanic == 1)
225 1.107.10.2 pooka Debugger();
226 1.107.10.2 pooka else if (db_onpanic >= 0) {
227 1.107.10.2 pooka static int intrace = 0;
228 1.107.10.2 pooka
229 1.107.10.2 pooka if (intrace == 0) {
230 1.107.10.2 pooka intrace = 1;
231 1.107.10.2 pooka printf("Begin traceback...\n");
232 1.107.10.2 pooka db_stack_trace_print(
233 1.107.10.2 pooka (db_expr_t)(intptr_t)__builtin_frame_address(0),
234 1.107.10.2 pooka true, 65535, "", printf);
235 1.107.10.2 pooka printf("End traceback...\n");
236 1.107.10.2 pooka intrace = 0;
237 1.107.10.2 pooka } else
238 1.107.10.2 pooka printf("Faulted in mid-traceback; aborting...");
239 1.107.10.2 pooka if (db_onpanic == 2)
240 1.107.10.2 pooka Debugger();
241 1.107.10.2 pooka }
242 1.107.10.2 pooka #endif
243 1.107.10.2 pooka cpu_reboot(bootopt, NULL);
244 1.107.10.2 pooka }
245 1.107.10.2 pooka
246 1.107.10.2 pooka /*
247 1.107.10.2 pooka * kernel logging functions: log, logpri, addlog
248 1.107.10.2 pooka */
249 1.107.10.2 pooka
250 1.107.10.2 pooka /*
251 1.107.10.2 pooka * log: write to the log buffer
252 1.107.10.2 pooka *
253 1.107.10.2 pooka * => will not sleep [so safe to call from interrupt]
254 1.107.10.2 pooka * => will log to console if /dev/klog isn't open
255 1.107.10.2 pooka */
256 1.107.10.2 pooka
257 1.107.10.2 pooka void
258 1.107.10.2 pooka log(int level, const char *fmt, ...)
259 1.107.10.2 pooka {
260 1.107.10.2 pooka int s;
261 1.107.10.2 pooka va_list ap;
262 1.107.10.2 pooka
263 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
264 1.107.10.2 pooka
265 1.107.10.2 pooka klogpri(level); /* log the level first */
266 1.107.10.2 pooka va_start(ap, fmt);
267 1.107.10.2 pooka kprintf(fmt, TOLOG, NULL, NULL, ap);
268 1.107.10.2 pooka va_end(ap);
269 1.107.10.2 pooka if (!log_open) {
270 1.107.10.2 pooka va_start(ap, fmt);
271 1.107.10.2 pooka kprintf(fmt, TOCONS, NULL, NULL, ap);
272 1.107.10.2 pooka va_end(ap);
273 1.107.10.2 pooka }
274 1.107.10.2 pooka
275 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
276 1.107.10.2 pooka
277 1.107.10.2 pooka logwakeup(); /* wake up anyone waiting for log msgs */
278 1.107.10.2 pooka }
279 1.107.10.2 pooka
280 1.107.10.2 pooka /*
281 1.107.10.2 pooka * vlog: write to the log buffer [already have va_alist]
282 1.107.10.2 pooka */
283 1.107.10.2 pooka
284 1.107.10.2 pooka void
285 1.107.10.2 pooka vlog(int level, const char *fmt, va_list ap)
286 1.107.10.2 pooka {
287 1.107.10.2 pooka int s;
288 1.107.10.2 pooka
289 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
290 1.107.10.2 pooka
291 1.107.10.2 pooka klogpri(level); /* log the level first */
292 1.107.10.2 pooka kprintf(fmt, TOLOG, NULL, NULL, ap);
293 1.107.10.2 pooka if (!log_open)
294 1.107.10.2 pooka kprintf(fmt, TOCONS, NULL, NULL, ap);
295 1.107.10.2 pooka
296 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
297 1.107.10.2 pooka
298 1.107.10.2 pooka logwakeup(); /* wake up anyone waiting for log msgs */
299 1.107.10.2 pooka }
300 1.107.10.2 pooka
301 1.107.10.2 pooka /*
302 1.107.10.2 pooka * logpri: log the priority level to the klog
303 1.107.10.2 pooka */
304 1.107.10.2 pooka
305 1.107.10.2 pooka void
306 1.107.10.2 pooka logpri(int level)
307 1.107.10.2 pooka {
308 1.107.10.2 pooka int s;
309 1.107.10.2 pooka
310 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
311 1.107.10.2 pooka klogpri(level);
312 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
313 1.107.10.2 pooka }
314 1.107.10.2 pooka
315 1.107.10.2 pooka /*
316 1.107.10.2 pooka * Note: we must be in the mutex here!
317 1.107.10.2 pooka */
318 1.107.10.2 pooka void
319 1.107.10.2 pooka klogpri(int level)
320 1.107.10.2 pooka {
321 1.107.10.2 pooka char *p;
322 1.107.10.2 pooka char snbuf[KPRINTF_BUFSIZE];
323 1.107.10.2 pooka
324 1.107.10.2 pooka putchar('<', TOLOG, NULL);
325 1.107.10.2 pooka snprintf(snbuf, sizeof(snbuf), "%d", level);
326 1.107.10.2 pooka for (p = snbuf ; *p ; p++)
327 1.107.10.2 pooka putchar(*p, TOLOG, NULL);
328 1.107.10.2 pooka putchar('>', TOLOG, NULL);
329 1.107.10.2 pooka }
330 1.107.10.2 pooka
331 1.107.10.2 pooka /*
332 1.107.10.2 pooka * addlog: add info to previous log message
333 1.107.10.2 pooka */
334 1.107.10.2 pooka
335 1.107.10.2 pooka void
336 1.107.10.2 pooka addlog(const char *fmt, ...)
337 1.107.10.2 pooka {
338 1.107.10.2 pooka int s;
339 1.107.10.2 pooka va_list ap;
340 1.107.10.2 pooka
341 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
342 1.107.10.2 pooka
343 1.107.10.2 pooka va_start(ap, fmt);
344 1.107.10.2 pooka kprintf(fmt, TOLOG, NULL, NULL, ap);
345 1.107.10.2 pooka va_end(ap);
346 1.107.10.2 pooka if (!log_open) {
347 1.107.10.2 pooka va_start(ap, fmt);
348 1.107.10.2 pooka kprintf(fmt, TOCONS, NULL, NULL, ap);
349 1.107.10.2 pooka va_end(ap);
350 1.107.10.2 pooka }
351 1.107.10.2 pooka
352 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
353 1.107.10.2 pooka
354 1.107.10.2 pooka logwakeup();
355 1.107.10.2 pooka }
356 1.107.10.2 pooka
357 1.107.10.2 pooka
358 1.107.10.2 pooka /*
359 1.107.10.2 pooka * putchar: print a single character on console or user terminal.
360 1.107.10.2 pooka *
361 1.107.10.2 pooka * => if console, then the last MSGBUFS chars are saved in msgbuf
362 1.107.10.2 pooka * for inspection later (e.g. dmesg/syslog)
363 1.107.10.2 pooka * => we must already be in the mutex!
364 1.107.10.2 pooka */
365 1.107.10.2 pooka static void
366 1.107.10.2 pooka putchar(int c, int flags, struct tty *tp)
367 1.107.10.2 pooka {
368 1.107.10.2 pooka struct kern_msgbuf *mbp;
369 1.107.10.2 pooka
370 1.107.10.2 pooka if (panicstr)
371 1.107.10.2 pooka constty = NULL;
372 1.107.10.2 pooka if ((flags & TOCONS) && tp == NULL && constty) {
373 1.107.10.2 pooka tp = constty;
374 1.107.10.2 pooka flags |= TOTTY;
375 1.107.10.2 pooka }
376 1.107.10.2 pooka if ((flags & TOTTY) && tp &&
377 1.107.10.2 pooka tputchar(c, flags, tp) < 0 &&
378 1.107.10.2 pooka (flags & TOCONS) && tp == constty)
379 1.107.10.2 pooka constty = NULL;
380 1.107.10.2 pooka if ((flags & TOLOG) &&
381 1.107.10.2 pooka c != '\0' && c != '\r' && c != 0177 && msgbufenabled) {
382 1.107.10.2 pooka mbp = msgbufp;
383 1.107.10.2 pooka if (mbp->msg_magic != MSG_MAGIC) {
384 1.107.10.2 pooka /*
385 1.107.10.2 pooka * Arguably should panic or somehow notify the
386 1.107.10.2 pooka * user... but how? Panic may be too drastic,
387 1.107.10.2 pooka * and would obliterate the message being kicked
388 1.107.10.2 pooka * out (maybe a panic itself), and printf
389 1.107.10.2 pooka * would invoke us recursively. Silently punt
390 1.107.10.2 pooka * for now. If syslog is running, it should
391 1.107.10.2 pooka * notice.
392 1.107.10.2 pooka */
393 1.107.10.2 pooka msgbufenabled = 0;
394 1.107.10.2 pooka } else {
395 1.107.10.2 pooka mbp->msg_bufc[mbp->msg_bufx++] = c;
396 1.107.10.2 pooka if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
397 1.107.10.2 pooka mbp->msg_bufx = 0;
398 1.107.10.2 pooka /* If the buffer is full, keep the most recent data. */
399 1.107.10.2 pooka if (mbp->msg_bufr == mbp->msg_bufx) {
400 1.107.10.2 pooka if (++mbp->msg_bufr >= mbp->msg_bufs)
401 1.107.10.2 pooka mbp->msg_bufr = 0;
402 1.107.10.2 pooka }
403 1.107.10.2 pooka }
404 1.107.10.2 pooka }
405 1.107.10.2 pooka if ((flags & TOCONS) && constty == NULL && c != '\0')
406 1.107.10.2 pooka (*v_putc)(c);
407 1.107.10.2 pooka #ifdef DDB
408 1.107.10.2 pooka if (flags & TODDB)
409 1.107.10.2 pooka db_putchar(c);
410 1.107.10.2 pooka #endif
411 1.107.10.2 pooka }
412 1.107.10.2 pooka
413 1.107.10.2 pooka
414 1.107.10.2 pooka /*
415 1.107.10.2 pooka * uprintf: print to the controlling tty of the current process
416 1.107.10.2 pooka *
417 1.107.10.2 pooka * => we may block if the tty queue is full
418 1.107.10.2 pooka * => no message is printed if the queue doesn't clear in a reasonable
419 1.107.10.2 pooka * time
420 1.107.10.2 pooka */
421 1.107.10.2 pooka
422 1.107.10.2 pooka void
423 1.107.10.2 pooka uprintf(const char *fmt, ...)
424 1.107.10.2 pooka {
425 1.107.10.2 pooka struct proc *p = curproc;
426 1.107.10.2 pooka va_list ap;
427 1.107.10.2 pooka
428 1.107.10.2 pooka /* mutex_enter(&proclist_mutex); XXXSMP */
429 1.107.10.2 pooka
430 1.107.10.2 pooka if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
431 1.107.10.2 pooka /* No mutex needed; going to process TTY. */
432 1.107.10.2 pooka va_start(ap, fmt);
433 1.107.10.2 pooka kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
434 1.107.10.2 pooka va_end(ap);
435 1.107.10.2 pooka }
436 1.107.10.2 pooka
437 1.107.10.2 pooka /* mutex_exit(&proclist_mutex); XXXSMP */
438 1.107.10.2 pooka }
439 1.107.10.2 pooka
440 1.107.10.2 pooka void
441 1.107.10.2 pooka uprintf_locked(const char *fmt, ...)
442 1.107.10.2 pooka {
443 1.107.10.2 pooka struct proc *p = curproc;
444 1.107.10.2 pooka va_list ap;
445 1.107.10.2 pooka
446 1.107.10.2 pooka if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
447 1.107.10.2 pooka /* No mutex needed; going to process TTY. */
448 1.107.10.2 pooka va_start(ap, fmt);
449 1.107.10.2 pooka kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
450 1.107.10.2 pooka va_end(ap);
451 1.107.10.2 pooka }
452 1.107.10.2 pooka }
453 1.107.10.2 pooka
454 1.107.10.2 pooka /*
455 1.107.10.2 pooka * tprintf functions: used to send messages to a specific process
456 1.107.10.2 pooka *
457 1.107.10.2 pooka * usage:
458 1.107.10.2 pooka * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
459 1.107.10.2 pooka * use the handle when calling "tprintf"
460 1.107.10.2 pooka * when done, do a "tprintf_close" to drop the handle
461 1.107.10.2 pooka */
462 1.107.10.2 pooka
463 1.107.10.2 pooka /*
464 1.107.10.2 pooka * tprintf_open: get a tprintf handle on a process "p"
465 1.107.10.2 pooka *
466 1.107.10.2 pooka * => returns NULL if process can't be printed to
467 1.107.10.2 pooka */
468 1.107.10.2 pooka
469 1.107.10.2 pooka tpr_t
470 1.107.10.2 pooka tprintf_open(struct proc *p)
471 1.107.10.2 pooka {
472 1.107.10.2 pooka tpr_t cookie;
473 1.107.10.2 pooka
474 1.107.10.2 pooka cookie = NULL;
475 1.107.10.2 pooka
476 1.107.10.2 pooka /* mutex_enter(&proclist_mutex); XXXSMP */
477 1.107.10.2 pooka if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
478 1.107.10.2 pooka SESSHOLD(p->p_session);
479 1.107.10.2 pooka cookie = (tpr_t)p->p_session;
480 1.107.10.2 pooka }
481 1.107.10.2 pooka /* mutex_exit(&proclist_mutex) XXXSMP */
482 1.107.10.2 pooka
483 1.107.10.2 pooka return cookie;
484 1.107.10.2 pooka }
485 1.107.10.2 pooka
486 1.107.10.2 pooka /*
487 1.107.10.2 pooka * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
488 1.107.10.2 pooka */
489 1.107.10.2 pooka
490 1.107.10.2 pooka void
491 1.107.10.2 pooka tprintf_close(tpr_t sess)
492 1.107.10.2 pooka {
493 1.107.10.2 pooka
494 1.107.10.2 pooka if (sess)
495 1.107.10.2 pooka SESSRELE((struct session *) sess);
496 1.107.10.2 pooka }
497 1.107.10.2 pooka
498 1.107.10.2 pooka /*
499 1.107.10.2 pooka * tprintf: given tprintf handle to a process [obtained with tprintf_open],
500 1.107.10.2 pooka * send a message to the controlling tty for that process.
501 1.107.10.2 pooka *
502 1.107.10.2 pooka * => also sends message to /dev/klog
503 1.107.10.2 pooka */
504 1.107.10.2 pooka void
505 1.107.10.2 pooka tprintf(tpr_t tpr, const char *fmt, ...)
506 1.107.10.2 pooka {
507 1.107.10.2 pooka struct session *sess = (struct session *)tpr;
508 1.107.10.2 pooka struct tty *tp = NULL;
509 1.107.10.2 pooka int s, flags = TOLOG;
510 1.107.10.2 pooka va_list ap;
511 1.107.10.2 pooka
512 1.107.10.2 pooka /* mutex_enter(&proclist_mutex); XXXSMP */
513 1.107.10.2 pooka if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
514 1.107.10.2 pooka flags |= TOTTY;
515 1.107.10.2 pooka tp = sess->s_ttyp;
516 1.107.10.2 pooka }
517 1.107.10.2 pooka
518 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
519 1.107.10.2 pooka
520 1.107.10.2 pooka klogpri(LOG_INFO);
521 1.107.10.2 pooka va_start(ap, fmt);
522 1.107.10.2 pooka kprintf(fmt, flags, tp, NULL, ap);
523 1.107.10.2 pooka va_end(ap);
524 1.107.10.2 pooka
525 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
526 1.107.10.2 pooka /* mutex_exit(&proclist_mutex); XXXSMP */
527 1.107.10.2 pooka
528 1.107.10.2 pooka logwakeup();
529 1.107.10.2 pooka }
530 1.107.10.2 pooka
531 1.107.10.2 pooka
532 1.107.10.2 pooka /*
533 1.107.10.2 pooka * ttyprintf: send a message to a specific tty
534 1.107.10.2 pooka *
535 1.107.10.2 pooka * => should be used only by tty driver or anything that knows the
536 1.107.10.2 pooka * underlying tty will not be revoked(2)'d away. [otherwise,
537 1.107.10.2 pooka * use tprintf]
538 1.107.10.2 pooka */
539 1.107.10.2 pooka void
540 1.107.10.2 pooka ttyprintf(struct tty *tp, const char *fmt, ...)
541 1.107.10.2 pooka {
542 1.107.10.2 pooka va_list ap;
543 1.107.10.2 pooka
544 1.107.10.2 pooka /* No mutex needed; going to process TTY. */
545 1.107.10.2 pooka va_start(ap, fmt);
546 1.107.10.2 pooka kprintf(fmt, TOTTY, tp, NULL, ap);
547 1.107.10.2 pooka va_end(ap);
548 1.107.10.2 pooka }
549 1.107.10.2 pooka
550 1.107.10.2 pooka #ifdef DDB
551 1.107.10.2 pooka
552 1.107.10.2 pooka /*
553 1.107.10.2 pooka * db_printf: printf for DDB (via db_putchar)
554 1.107.10.2 pooka */
555 1.107.10.2 pooka
556 1.107.10.2 pooka void
557 1.107.10.2 pooka db_printf(const char *fmt, ...)
558 1.107.10.2 pooka {
559 1.107.10.2 pooka va_list ap;
560 1.107.10.2 pooka
561 1.107.10.2 pooka /* No mutex needed; DDB pauses all processors. */
562 1.107.10.2 pooka va_start(ap, fmt);
563 1.107.10.2 pooka kprintf(fmt, TODDB, NULL, NULL, ap);
564 1.107.10.2 pooka va_end(ap);
565 1.107.10.2 pooka
566 1.107.10.2 pooka if (db_tee_msgbuf) {
567 1.107.10.2 pooka va_start(ap, fmt);
568 1.107.10.2 pooka kprintf(fmt, TOLOG, NULL, NULL, ap);
569 1.107.10.2 pooka va_end(ap);
570 1.107.10.2 pooka };
571 1.107.10.2 pooka }
572 1.107.10.2 pooka
573 1.107.10.2 pooka void
574 1.107.10.2 pooka db_vprintf(const char *fmt, va_list ap)
575 1.107.10.2 pooka {
576 1.107.10.2 pooka
577 1.107.10.2 pooka /* No mutex needed; DDB pauses all processors. */
578 1.107.10.2 pooka kprintf(fmt, TODDB, NULL, NULL, ap);
579 1.107.10.2 pooka if (db_tee_msgbuf)
580 1.107.10.2 pooka kprintf(fmt, TOLOG, NULL, NULL, ap);
581 1.107.10.2 pooka }
582 1.107.10.2 pooka
583 1.107.10.2 pooka #endif /* DDB */
584 1.107.10.2 pooka
585 1.107.10.2 pooka /*
586 1.107.10.2 pooka * Device autoconfiguration printf routines. These change their
587 1.107.10.2 pooka * behavior based on the AB_* flags in boothowto. If AB_SILENT
588 1.107.10.2 pooka * is set, messages never go to the console (but they still always
589 1.107.10.2 pooka * go to the log). AB_VERBOSE overrides AB_SILENT.
590 1.107.10.2 pooka */
591 1.107.10.2 pooka
592 1.107.10.2 pooka /*
593 1.107.10.2 pooka * aprint_normal: Send to console unless AB_QUIET. Always goes
594 1.107.10.2 pooka * to the log.
595 1.107.10.2 pooka */
596 1.107.10.2 pooka void
597 1.107.10.2 pooka aprint_normal(const char *fmt, ...)
598 1.107.10.2 pooka {
599 1.107.10.2 pooka va_list ap;
600 1.107.10.2 pooka int s, flags = TOLOG;
601 1.107.10.2 pooka
602 1.107.10.2 pooka if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
603 1.107.10.2 pooka (boothowto & AB_VERBOSE) != 0)
604 1.107.10.2 pooka flags |= TOCONS;
605 1.107.10.2 pooka
606 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
607 1.107.10.2 pooka
608 1.107.10.2 pooka va_start(ap, fmt);
609 1.107.10.2 pooka kprintf(fmt, flags, NULL, NULL, ap);
610 1.107.10.2 pooka va_end(ap);
611 1.107.10.2 pooka
612 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
613 1.107.10.2 pooka
614 1.107.10.2 pooka if (!panicstr)
615 1.107.10.2 pooka logwakeup();
616 1.107.10.2 pooka }
617 1.107.10.2 pooka
618 1.107.10.2 pooka /*
619 1.107.10.2 pooka * aprint_error: Send to console unless AB_QUIET. Always goes
620 1.107.10.2 pooka * to the log. Also counts the number of times called so other
621 1.107.10.2 pooka * parts of the kernel can report the number of errors during a
622 1.107.10.2 pooka * given phase of system startup.
623 1.107.10.2 pooka */
624 1.107.10.2 pooka static int aprint_error_count;
625 1.107.10.2 pooka
626 1.107.10.2 pooka int
627 1.107.10.2 pooka aprint_get_error_count(void)
628 1.107.10.2 pooka {
629 1.107.10.2 pooka int count, s;
630 1.107.10.2 pooka
631 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
632 1.107.10.2 pooka
633 1.107.10.2 pooka count = aprint_error_count;
634 1.107.10.2 pooka aprint_error_count = 0;
635 1.107.10.2 pooka
636 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
637 1.107.10.2 pooka
638 1.107.10.2 pooka return (count);
639 1.107.10.2 pooka }
640 1.107.10.2 pooka
641 1.107.10.2 pooka void
642 1.107.10.2 pooka aprint_error(const char *fmt, ...)
643 1.107.10.2 pooka {
644 1.107.10.2 pooka va_list ap;
645 1.107.10.2 pooka int s, flags = TOLOG;
646 1.107.10.2 pooka
647 1.107.10.2 pooka if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
648 1.107.10.2 pooka (boothowto & AB_VERBOSE) != 0)
649 1.107.10.2 pooka flags |= TOCONS;
650 1.107.10.2 pooka
651 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
652 1.107.10.2 pooka
653 1.107.10.2 pooka aprint_error_count++;
654 1.107.10.2 pooka
655 1.107.10.2 pooka va_start(ap, fmt);
656 1.107.10.2 pooka kprintf(fmt, flags, NULL, NULL, ap);
657 1.107.10.2 pooka va_end(ap);
658 1.107.10.2 pooka
659 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
660 1.107.10.2 pooka
661 1.107.10.2 pooka if (!panicstr)
662 1.107.10.2 pooka logwakeup();
663 1.107.10.2 pooka }
664 1.107.10.2 pooka
665 1.107.10.2 pooka /*
666 1.107.10.2 pooka * aprint_naive: Send to console only if AB_QUIET. Never goes
667 1.107.10.2 pooka * to the log.
668 1.107.10.2 pooka */
669 1.107.10.2 pooka void
670 1.107.10.2 pooka aprint_naive(const char *fmt, ...)
671 1.107.10.2 pooka {
672 1.107.10.2 pooka va_list ap;
673 1.107.10.2 pooka int s;
674 1.107.10.2 pooka
675 1.107.10.2 pooka if ((boothowto & (AB_QUIET|AB_SILENT|AB_VERBOSE)) == AB_QUIET) {
676 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
677 1.107.10.2 pooka
678 1.107.10.2 pooka va_start(ap, fmt);
679 1.107.10.2 pooka kprintf(fmt, TOCONS, NULL, NULL, ap);
680 1.107.10.2 pooka va_end(ap);
681 1.107.10.2 pooka
682 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
683 1.107.10.2 pooka }
684 1.107.10.2 pooka }
685 1.107.10.2 pooka
686 1.107.10.2 pooka /*
687 1.107.10.2 pooka * aprint_verbose: Send to console only if AB_VERBOSE. Always
688 1.107.10.2 pooka * goes to the log.
689 1.107.10.2 pooka */
690 1.107.10.2 pooka void
691 1.107.10.2 pooka aprint_verbose(const char *fmt, ...)
692 1.107.10.2 pooka {
693 1.107.10.2 pooka va_list ap;
694 1.107.10.2 pooka int s, flags = TOLOG;
695 1.107.10.2 pooka
696 1.107.10.2 pooka if (boothowto & AB_VERBOSE)
697 1.107.10.2 pooka flags |= TOCONS;
698 1.107.10.2 pooka
699 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
700 1.107.10.2 pooka
701 1.107.10.2 pooka va_start(ap, fmt);
702 1.107.10.2 pooka kprintf(fmt, flags, NULL, NULL, ap);
703 1.107.10.2 pooka va_end(ap);
704 1.107.10.2 pooka
705 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
706 1.107.10.2 pooka
707 1.107.10.2 pooka if (!panicstr)
708 1.107.10.2 pooka logwakeup();
709 1.107.10.2 pooka }
710 1.107.10.2 pooka
711 1.107.10.2 pooka /*
712 1.107.10.2 pooka * aprint_debug: Send to console and log only if AB_DEBUG.
713 1.107.10.2 pooka */
714 1.107.10.2 pooka void
715 1.107.10.2 pooka aprint_debug(const char *fmt, ...)
716 1.107.10.2 pooka {
717 1.107.10.2 pooka va_list ap;
718 1.107.10.2 pooka int s;
719 1.107.10.2 pooka
720 1.107.10.2 pooka if (boothowto & AB_DEBUG) {
721 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
722 1.107.10.2 pooka
723 1.107.10.2 pooka va_start(ap, fmt);
724 1.107.10.2 pooka kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
725 1.107.10.2 pooka va_end(ap);
726 1.107.10.2 pooka
727 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
728 1.107.10.2 pooka }
729 1.107.10.2 pooka }
730 1.107.10.2 pooka
731 1.107.10.2 pooka /*
732 1.107.10.2 pooka * printf_nolog: Like printf(), but does not send message to the log.
733 1.107.10.2 pooka */
734 1.107.10.2 pooka
735 1.107.10.2 pooka void
736 1.107.10.2 pooka printf_nolog(const char *fmt, ...)
737 1.107.10.2 pooka {
738 1.107.10.2 pooka va_list ap;
739 1.107.10.2 pooka int s;
740 1.107.10.2 pooka
741 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
742 1.107.10.2 pooka
743 1.107.10.2 pooka va_start(ap, fmt);
744 1.107.10.2 pooka kprintf(fmt, TOCONS, NULL, NULL, ap);
745 1.107.10.2 pooka va_end(ap);
746 1.107.10.2 pooka
747 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
748 1.107.10.2 pooka }
749 1.107.10.2 pooka
750 1.107.10.2 pooka /*
751 1.107.10.2 pooka * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
752 1.107.10.2 pooka */
753 1.107.10.2 pooka
754 1.107.10.2 pooka /*
755 1.107.10.2 pooka * printf: print a message to the console and the log
756 1.107.10.2 pooka */
757 1.107.10.2 pooka void
758 1.107.10.2 pooka printf(const char *fmt, ...)
759 1.107.10.2 pooka {
760 1.107.10.2 pooka va_list ap;
761 1.107.10.2 pooka int s;
762 1.107.10.2 pooka
763 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
764 1.107.10.2 pooka
765 1.107.10.2 pooka va_start(ap, fmt);
766 1.107.10.2 pooka kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
767 1.107.10.2 pooka va_end(ap);
768 1.107.10.2 pooka
769 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
770 1.107.10.2 pooka
771 1.107.10.2 pooka if (!panicstr)
772 1.107.10.2 pooka logwakeup();
773 1.107.10.2 pooka }
774 1.107.10.2 pooka
775 1.107.10.2 pooka /*
776 1.107.10.2 pooka * vprintf: print a message to the console and the log [already have
777 1.107.10.2 pooka * va_alist]
778 1.107.10.2 pooka */
779 1.107.10.2 pooka
780 1.107.10.2 pooka void
781 1.107.10.2 pooka vprintf(const char *fmt, va_list ap)
782 1.107.10.2 pooka {
783 1.107.10.2 pooka int s;
784 1.107.10.2 pooka
785 1.107.10.2 pooka KPRINTF_MUTEX_ENTER(s);
786 1.107.10.2 pooka
787 1.107.10.2 pooka kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
788 1.107.10.2 pooka
789 1.107.10.2 pooka KPRINTF_MUTEX_EXIT(s);
790 1.107.10.2 pooka
791 1.107.10.2 pooka if (!panicstr)
792 1.107.10.2 pooka logwakeup();
793 1.107.10.2 pooka }
794 1.107.10.2 pooka
795 1.107.10.2 pooka /*
796 1.107.10.2 pooka * sprintf: print a message to a buffer
797 1.107.10.2 pooka */
798 1.107.10.2 pooka int
799 1.107.10.2 pooka sprintf(char *bf, const char *fmt, ...)
800 1.107.10.2 pooka {
801 1.107.10.2 pooka int retval;
802 1.107.10.2 pooka va_list ap;
803 1.107.10.2 pooka
804 1.107.10.2 pooka va_start(ap, fmt);
805 1.107.10.2 pooka retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap);
806 1.107.10.2 pooka va_end(ap);
807 1.107.10.2 pooka *(bf + retval) = 0; /* null terminate */
808 1.107.10.2 pooka return(retval);
809 1.107.10.2 pooka }
810 1.107.10.2 pooka
811 1.107.10.2 pooka /*
812 1.107.10.2 pooka * vsprintf: print a message to a buffer [already have va_alist]
813 1.107.10.2 pooka */
814 1.107.10.2 pooka
815 1.107.10.2 pooka int
816 1.107.10.2 pooka vsprintf(char *bf, const char *fmt, va_list ap)
817 1.107.10.2 pooka {
818 1.107.10.2 pooka int retval;
819 1.107.10.2 pooka
820 1.107.10.2 pooka retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap);
821 1.107.10.2 pooka *(bf + retval) = 0; /* null terminate */
822 1.107.10.2 pooka return (retval);
823 1.107.10.2 pooka }
824 1.107.10.2 pooka
825 1.107.10.2 pooka /*
826 1.107.10.2 pooka * snprintf: print a message to a buffer
827 1.107.10.2 pooka */
828 1.107.10.2 pooka int
829 1.107.10.2 pooka snprintf(char *bf, size_t size, const char *fmt, ...)
830 1.107.10.2 pooka {
831 1.107.10.2 pooka int retval;
832 1.107.10.2 pooka va_list ap;
833 1.107.10.2 pooka char *p;
834 1.107.10.2 pooka
835 1.107.10.2 pooka if (size < 1)
836 1.107.10.2 pooka return (-1);
837 1.107.10.2 pooka p = bf + size - 1;
838 1.107.10.2 pooka va_start(ap, fmt);
839 1.107.10.2 pooka retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
840 1.107.10.2 pooka va_end(ap);
841 1.107.10.2 pooka *(p) = 0; /* null terminate */
842 1.107.10.2 pooka return(retval);
843 1.107.10.2 pooka }
844 1.107.10.2 pooka
845 1.107.10.2 pooka /*
846 1.107.10.2 pooka * vsnprintf: print a message to a buffer [already have va_alist]
847 1.107.10.2 pooka */
848 1.107.10.2 pooka int
849 1.107.10.2 pooka vsnprintf(char *bf, size_t size, const char *fmt, va_list ap)
850 1.107.10.2 pooka {
851 1.107.10.2 pooka int retval;
852 1.107.10.2 pooka char *p;
853 1.107.10.2 pooka
854 1.107.10.2 pooka if (size < 1)
855 1.107.10.2 pooka return (-1);
856 1.107.10.2 pooka p = bf + size - 1;
857 1.107.10.2 pooka retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
858 1.107.10.2 pooka *(p) = 0; /* null terminate */
859 1.107.10.2 pooka return(retval);
860 1.107.10.2 pooka }
861 1.107.10.2 pooka
862 1.107.10.2 pooka /*
863 1.107.10.2 pooka * kprintf: scaled down version of printf(3).
864 1.107.10.2 pooka *
865 1.107.10.2 pooka * this version based on vfprintf() from libc which was derived from
866 1.107.10.2 pooka * software contributed to Berkeley by Chris Torek.
867 1.107.10.2 pooka *
868 1.107.10.2 pooka * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
869 1.107.10.2 pooka */
870 1.107.10.2 pooka
871 1.107.10.2 pooka /*
872 1.107.10.2 pooka * macros for converting digits to letters and vice versa
873 1.107.10.2 pooka */
874 1.107.10.2 pooka #define to_digit(c) ((c) - '0')
875 1.107.10.2 pooka #define is_digit(c) ((unsigned)to_digit(c) <= 9)
876 1.107.10.2 pooka #define to_char(n) ((n) + '0')
877 1.107.10.2 pooka
878 1.107.10.2 pooka /*
879 1.107.10.2 pooka * flags used during conversion.
880 1.107.10.2 pooka */
881 1.107.10.2 pooka #define ALT 0x001 /* alternate form */
882 1.107.10.2 pooka #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
883 1.107.10.2 pooka #define LADJUST 0x004 /* left adjustment */
884 1.107.10.2 pooka #define LONGDBL 0x008 /* long double; unimplemented */
885 1.107.10.2 pooka #define LONGINT 0x010 /* long integer */
886 1.107.10.2 pooka #define QUADINT 0x020 /* quad integer */
887 1.107.10.2 pooka #define SHORTINT 0x040 /* short integer */
888 1.107.10.2 pooka #define MAXINT 0x080 /* intmax_t */
889 1.107.10.2 pooka #define PTRINT 0x100 /* intptr_t */
890 1.107.10.2 pooka #define SIZEINT 0x200 /* size_t */
891 1.107.10.2 pooka #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
892 1.107.10.2 pooka #define FPT 0x800 /* Floating point number */
893 1.107.10.2 pooka
894 1.107.10.2 pooka /*
895 1.107.10.2 pooka * To extend shorts properly, we need both signed and unsigned
896 1.107.10.2 pooka * argument extraction methods.
897 1.107.10.2 pooka */
898 1.107.10.2 pooka #define SARG() \
899 1.107.10.2 pooka (flags&MAXINT ? va_arg(ap, intmax_t) : \
900 1.107.10.2 pooka flags&PTRINT ? va_arg(ap, intptr_t) : \
901 1.107.10.2 pooka flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
902 1.107.10.2 pooka flags&QUADINT ? va_arg(ap, quad_t) : \
903 1.107.10.2 pooka flags&LONGINT ? va_arg(ap, long) : \
904 1.107.10.2 pooka flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
905 1.107.10.2 pooka (long)va_arg(ap, int))
906 1.107.10.2 pooka #define UARG() \
907 1.107.10.2 pooka (flags&MAXINT ? va_arg(ap, uintmax_t) : \
908 1.107.10.2 pooka flags&PTRINT ? va_arg(ap, uintptr_t) : \
909 1.107.10.2 pooka flags&SIZEINT ? va_arg(ap, size_t) : \
910 1.107.10.2 pooka flags&QUADINT ? va_arg(ap, u_quad_t) : \
911 1.107.10.2 pooka flags&LONGINT ? va_arg(ap, u_long) : \
912 1.107.10.2 pooka flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
913 1.107.10.2 pooka (u_long)va_arg(ap, u_int))
914 1.107.10.2 pooka
915 1.107.10.2 pooka #define KPRINTF_PUTCHAR(C) { \
916 1.107.10.2 pooka if (oflags == TOBUFONLY) { \
917 1.107.10.2 pooka if ((vp != NULL) && (sbuf == tailp)) { \
918 1.107.10.2 pooka ret += 1; /* indicate error */ \
919 1.107.10.2 pooka goto overflow; \
920 1.107.10.2 pooka } \
921 1.107.10.2 pooka *sbuf++ = (C); \
922 1.107.10.2 pooka } else { \
923 1.107.10.2 pooka putchar((C), oflags, (struct tty *)vp); \
924 1.107.10.2 pooka } \
925 1.107.10.2 pooka }
926 1.107.10.2 pooka
927 1.107.10.2 pooka /*
928 1.107.10.2 pooka * Guts of kernel printf. Note, we already expect to be in a mutex!
929 1.107.10.2 pooka */
930 1.107.10.2 pooka int
931 1.107.10.2 pooka kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
932 1.107.10.2 pooka {
933 1.107.10.2 pooka const char *fmt; /* format string */
934 1.107.10.2 pooka int ch; /* character from fmt */
935 1.107.10.2 pooka int n; /* handy integer (short term usage) */
936 1.107.10.2 pooka char *cp; /* handy char pointer (short term usage) */
937 1.107.10.2 pooka int flags; /* flags as above */
938 1.107.10.2 pooka int ret; /* return value accumulator */
939 1.107.10.2 pooka int width; /* width from format (%8d), or 0 */
940 1.107.10.2 pooka int prec; /* precision from format (%.3d), or -1 */
941 1.107.10.2 pooka char sign; /* sign prefix (' ', '+', '-', or \0) */
942 1.107.10.2 pooka
943 1.107.10.2 pooka u_quad_t _uquad; /* integer arguments %[diouxX] */
944 1.107.10.2 pooka enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
945 1.107.10.2 pooka int dprec; /* a copy of prec if [diouxX], 0 otherwise */
946 1.107.10.2 pooka int realsz; /* field size expanded by dprec */
947 1.107.10.2 pooka int size; /* size of converted field or string */
948 1.107.10.2 pooka const char *xdigs; /* digits for [xX] conversion */
949 1.107.10.2 pooka char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
950 1.107.10.2 pooka char *tailp; /* tail pointer for snprintf */
951 1.107.10.2 pooka
952 1.107.10.2 pooka tailp = NULL; /* XXX: shutup gcc */
953 1.107.10.2 pooka if (oflags == TOBUFONLY && (vp != NULL))
954 1.107.10.2 pooka tailp = *(char **)vp;
955 1.107.10.2 pooka
956 1.107.10.2 pooka cp = NULL; /* XXX: shutup gcc */
957 1.107.10.2 pooka size = 0; /* XXX: shutup gcc */
958 1.107.10.2 pooka
959 1.107.10.2 pooka fmt = fmt0;
960 1.107.10.2 pooka ret = 0;
961 1.107.10.2 pooka
962 1.107.10.2 pooka xdigs = NULL; /* XXX: shut up gcc warning */
963 1.107.10.2 pooka
964 1.107.10.2 pooka /*
965 1.107.10.2 pooka * Scan the format for conversions (`%' character).
966 1.107.10.2 pooka */
967 1.107.10.2 pooka for (;;) {
968 1.107.10.2 pooka while (*fmt != '%' && *fmt) {
969 1.107.10.2 pooka ret++;
970 1.107.10.2 pooka KPRINTF_PUTCHAR(*fmt++);
971 1.107.10.2 pooka }
972 1.107.10.2 pooka if (*fmt == 0)
973 1.107.10.2 pooka goto done;
974 1.107.10.2 pooka
975 1.107.10.2 pooka fmt++; /* skip over '%' */
976 1.107.10.2 pooka
977 1.107.10.2 pooka flags = 0;
978 1.107.10.2 pooka dprec = 0;
979 1.107.10.2 pooka width = 0;
980 1.107.10.2 pooka prec = -1;
981 1.107.10.2 pooka sign = '\0';
982 1.107.10.2 pooka
983 1.107.10.2 pooka rflag: ch = *fmt++;
984 1.107.10.2 pooka reswitch: switch (ch) {
985 1.107.10.2 pooka case ' ':
986 1.107.10.2 pooka /*
987 1.107.10.2 pooka * ``If the space and + flags both appear, the space
988 1.107.10.2 pooka * flag will be ignored.''
989 1.107.10.2 pooka * -- ANSI X3J11
990 1.107.10.2 pooka */
991 1.107.10.2 pooka if (!sign)
992 1.107.10.2 pooka sign = ' ';
993 1.107.10.2 pooka goto rflag;
994 1.107.10.2 pooka case '#':
995 1.107.10.2 pooka flags |= ALT;
996 1.107.10.2 pooka goto rflag;
997 1.107.10.2 pooka case '*':
998 1.107.10.2 pooka /*
999 1.107.10.2 pooka * ``A negative field width argument is taken as a
1000 1.107.10.2 pooka * - flag followed by a positive field width.''
1001 1.107.10.2 pooka * -- ANSI X3J11
1002 1.107.10.2 pooka * They don't exclude field widths read from args.
1003 1.107.10.2 pooka */
1004 1.107.10.2 pooka if ((width = va_arg(ap, int)) >= 0)
1005 1.107.10.2 pooka goto rflag;
1006 1.107.10.2 pooka width = -width;
1007 1.107.10.2 pooka /* FALLTHROUGH */
1008 1.107.10.2 pooka case '-':
1009 1.107.10.2 pooka flags |= LADJUST;
1010 1.107.10.2 pooka goto rflag;
1011 1.107.10.2 pooka case '+':
1012 1.107.10.2 pooka sign = '+';
1013 1.107.10.2 pooka goto rflag;
1014 1.107.10.2 pooka case '.':
1015 1.107.10.2 pooka if ((ch = *fmt++) == '*') {
1016 1.107.10.2 pooka n = va_arg(ap, int);
1017 1.107.10.2 pooka prec = n < 0 ? -1 : n;
1018 1.107.10.2 pooka goto rflag;
1019 1.107.10.2 pooka }
1020 1.107.10.2 pooka n = 0;
1021 1.107.10.2 pooka while (is_digit(ch)) {
1022 1.107.10.2 pooka n = 10 * n + to_digit(ch);
1023 1.107.10.2 pooka ch = *fmt++;
1024 1.107.10.2 pooka }
1025 1.107.10.2 pooka prec = n < 0 ? -1 : n;
1026 1.107.10.2 pooka goto reswitch;
1027 1.107.10.2 pooka case '0':
1028 1.107.10.2 pooka /*
1029 1.107.10.2 pooka * ``Note that 0 is taken as a flag, not as the
1030 1.107.10.2 pooka * beginning of a field width.''
1031 1.107.10.2 pooka * -- ANSI X3J11
1032 1.107.10.2 pooka */
1033 1.107.10.2 pooka flags |= ZEROPAD;
1034 1.107.10.2 pooka goto rflag;
1035 1.107.10.2 pooka case '1': case '2': case '3': case '4':
1036 1.107.10.2 pooka case '5': case '6': case '7': case '8': case '9':
1037 1.107.10.2 pooka n = 0;
1038 1.107.10.2 pooka do {
1039 1.107.10.2 pooka n = 10 * n + to_digit(ch);
1040 1.107.10.2 pooka ch = *fmt++;
1041 1.107.10.2 pooka } while (is_digit(ch));
1042 1.107.10.2 pooka width = n;
1043 1.107.10.2 pooka goto reswitch;
1044 1.107.10.2 pooka case 'h':
1045 1.107.10.2 pooka flags |= SHORTINT;
1046 1.107.10.2 pooka goto rflag;
1047 1.107.10.2 pooka case 'j':
1048 1.107.10.2 pooka flags |= MAXINT;
1049 1.107.10.2 pooka goto rflag;
1050 1.107.10.2 pooka case 'l':
1051 1.107.10.2 pooka if (*fmt == 'l') {
1052 1.107.10.2 pooka fmt++;
1053 1.107.10.2 pooka flags |= QUADINT;
1054 1.107.10.2 pooka } else {
1055 1.107.10.2 pooka flags |= LONGINT;
1056 1.107.10.2 pooka }
1057 1.107.10.2 pooka goto rflag;
1058 1.107.10.2 pooka case 'q':
1059 1.107.10.2 pooka flags |= QUADINT;
1060 1.107.10.2 pooka goto rflag;
1061 1.107.10.2 pooka case 't':
1062 1.107.10.2 pooka flags |= PTRINT;
1063 1.107.10.2 pooka goto rflag;
1064 1.107.10.2 pooka case 'z':
1065 1.107.10.2 pooka flags |= SIZEINT;
1066 1.107.10.2 pooka goto rflag;
1067 1.107.10.2 pooka case 'c':
1068 1.107.10.2 pooka *(cp = bf) = va_arg(ap, int);
1069 1.107.10.2 pooka size = 1;
1070 1.107.10.2 pooka sign = '\0';
1071 1.107.10.2 pooka break;
1072 1.107.10.2 pooka case 'D':
1073 1.107.10.2 pooka flags |= LONGINT;
1074 1.107.10.2 pooka /*FALLTHROUGH*/
1075 1.107.10.2 pooka case 'd':
1076 1.107.10.2 pooka case 'i':
1077 1.107.10.2 pooka _uquad = SARG();
1078 1.107.10.2 pooka if ((quad_t)_uquad < 0) {
1079 1.107.10.2 pooka _uquad = -_uquad;
1080 1.107.10.2 pooka sign = '-';
1081 1.107.10.2 pooka }
1082 1.107.10.2 pooka base = DEC;
1083 1.107.10.2 pooka goto number;
1084 1.107.10.2 pooka case 'n':
1085 1.107.10.2 pooka if (flags & MAXINT)
1086 1.107.10.2 pooka *va_arg(ap, intmax_t *) = ret;
1087 1.107.10.2 pooka else if (flags & PTRINT)
1088 1.107.10.2 pooka *va_arg(ap, intptr_t *) = ret;
1089 1.107.10.2 pooka else if (flags & SIZEINT)
1090 1.107.10.2 pooka *va_arg(ap, ssize_t *) = ret;
1091 1.107.10.2 pooka else if (flags & QUADINT)
1092 1.107.10.2 pooka *va_arg(ap, quad_t *) = ret;
1093 1.107.10.2 pooka else if (flags & LONGINT)
1094 1.107.10.2 pooka *va_arg(ap, long *) = ret;
1095 1.107.10.2 pooka else if (flags & SHORTINT)
1096 1.107.10.2 pooka *va_arg(ap, short *) = ret;
1097 1.107.10.2 pooka else
1098 1.107.10.2 pooka *va_arg(ap, int *) = ret;
1099 1.107.10.2 pooka continue; /* no output */
1100 1.107.10.2 pooka case 'O':
1101 1.107.10.2 pooka flags |= LONGINT;
1102 1.107.10.2 pooka /*FALLTHROUGH*/
1103 1.107.10.2 pooka case 'o':
1104 1.107.10.2 pooka _uquad = UARG();
1105 1.107.10.2 pooka base = OCT;
1106 1.107.10.2 pooka goto nosign;
1107 1.107.10.2 pooka case 'p':
1108 1.107.10.2 pooka /*
1109 1.107.10.2 pooka * ``The argument shall be a pointer to void. The
1110 1.107.10.2 pooka * value of the pointer is converted to a sequence
1111 1.107.10.2 pooka * of printable characters, in an implementation-
1112 1.107.10.2 pooka * defined manner.''
1113 1.107.10.2 pooka * -- ANSI X3J11
1114 1.107.10.2 pooka */
1115 1.107.10.2 pooka /* NOSTRICT */
1116 1.107.10.2 pooka _uquad = (u_long)va_arg(ap, void *);
1117 1.107.10.2 pooka base = HEX;
1118 1.107.10.2 pooka xdigs = hexdigits;
1119 1.107.10.2 pooka flags |= HEXPREFIX;
1120 1.107.10.2 pooka ch = 'x';
1121 1.107.10.2 pooka goto nosign;
1122 1.107.10.2 pooka case 's':
1123 1.107.10.2 pooka if ((cp = va_arg(ap, char *)) == NULL)
1124 1.107.10.2 pooka /*XXXUNCONST*/
1125 1.107.10.2 pooka cp = __UNCONST("(null)");
1126 1.107.10.2 pooka if (prec >= 0) {
1127 1.107.10.2 pooka /*
1128 1.107.10.2 pooka * can't use strlen; can only look for the
1129 1.107.10.2 pooka * NUL in the first `prec' characters, and
1130 1.107.10.2 pooka * strlen() will go further.
1131 1.107.10.2 pooka */
1132 1.107.10.2 pooka char *p = memchr(cp, 0, prec);
1133 1.107.10.2 pooka
1134 1.107.10.2 pooka if (p != NULL) {
1135 1.107.10.2 pooka size = p - cp;
1136 1.107.10.2 pooka if (size > prec)
1137 1.107.10.2 pooka size = prec;
1138 1.107.10.2 pooka } else
1139 1.107.10.2 pooka size = prec;
1140 1.107.10.2 pooka } else
1141 1.107.10.2 pooka size = strlen(cp);
1142 1.107.10.2 pooka sign = '\0';
1143 1.107.10.2 pooka break;
1144 1.107.10.2 pooka case 'U':
1145 1.107.10.2 pooka flags |= LONGINT;
1146 1.107.10.2 pooka /*FALLTHROUGH*/
1147 1.107.10.2 pooka case 'u':
1148 1.107.10.2 pooka _uquad = UARG();
1149 1.107.10.2 pooka base = DEC;
1150 1.107.10.2 pooka goto nosign;
1151 1.107.10.2 pooka case 'X':
1152 1.107.10.2 pooka xdigs = HEXDIGITS;
1153 1.107.10.2 pooka goto hex;
1154 1.107.10.2 pooka case 'x':
1155 1.107.10.2 pooka xdigs = hexdigits;
1156 1.107.10.2 pooka hex: _uquad = UARG();
1157 1.107.10.2 pooka base = HEX;
1158 1.107.10.2 pooka /* leading 0x/X only if non-zero */
1159 1.107.10.2 pooka if (flags & ALT && _uquad != 0)
1160 1.107.10.2 pooka flags |= HEXPREFIX;
1161 1.107.10.2 pooka
1162 1.107.10.2 pooka /* unsigned conversions */
1163 1.107.10.2 pooka nosign: sign = '\0';
1164 1.107.10.2 pooka /*
1165 1.107.10.2 pooka * ``... diouXx conversions ... if a precision is
1166 1.107.10.2 pooka * specified, the 0 flag will be ignored.''
1167 1.107.10.2 pooka * -- ANSI X3J11
1168 1.107.10.2 pooka */
1169 1.107.10.2 pooka number: if ((dprec = prec) >= 0)
1170 1.107.10.2 pooka flags &= ~ZEROPAD;
1171 1.107.10.2 pooka
1172 1.107.10.2 pooka /*
1173 1.107.10.2 pooka * ``The result of converting a zero value with an
1174 1.107.10.2 pooka * explicit precision of zero is no characters.''
1175 1.107.10.2 pooka * -- ANSI X3J11
1176 1.107.10.2 pooka */
1177 1.107.10.2 pooka cp = bf + KPRINTF_BUFSIZE;
1178 1.107.10.2 pooka if (_uquad != 0 || prec != 0) {
1179 1.107.10.2 pooka /*
1180 1.107.10.2 pooka * Unsigned mod is hard, and unsigned mod
1181 1.107.10.2 pooka * by a constant is easier than that by
1182 1.107.10.2 pooka * a variable; hence this switch.
1183 1.107.10.2 pooka */
1184 1.107.10.2 pooka switch (base) {
1185 1.107.10.2 pooka case OCT:
1186 1.107.10.2 pooka do {
1187 1.107.10.2 pooka *--cp = to_char(_uquad & 7);
1188 1.107.10.2 pooka _uquad >>= 3;
1189 1.107.10.2 pooka } while (_uquad);
1190 1.107.10.2 pooka /* handle octal leading 0 */
1191 1.107.10.2 pooka if (flags & ALT && *cp != '0')
1192 1.107.10.2 pooka *--cp = '0';
1193 1.107.10.2 pooka break;
1194 1.107.10.2 pooka
1195 1.107.10.2 pooka case DEC:
1196 1.107.10.2 pooka /* many numbers are 1 digit */
1197 1.107.10.2 pooka while (_uquad >= 10) {
1198 1.107.10.2 pooka *--cp = to_char(_uquad % 10);
1199 1.107.10.2 pooka _uquad /= 10;
1200 1.107.10.2 pooka }
1201 1.107.10.2 pooka *--cp = to_char(_uquad);
1202 1.107.10.2 pooka break;
1203 1.107.10.2 pooka
1204 1.107.10.2 pooka case HEX:
1205 1.107.10.2 pooka do {
1206 1.107.10.2 pooka *--cp = xdigs[_uquad & 15];
1207 1.107.10.2 pooka _uquad >>= 4;
1208 1.107.10.2 pooka } while (_uquad);
1209 1.107.10.2 pooka break;
1210 1.107.10.2 pooka
1211 1.107.10.2 pooka default:
1212 1.107.10.2 pooka /*XXXUNCONST*/
1213 1.107.10.2 pooka cp = __UNCONST("bug in kprintf: bad base");
1214 1.107.10.2 pooka size = strlen(cp);
1215 1.107.10.2 pooka goto skipsize;
1216 1.107.10.2 pooka }
1217 1.107.10.2 pooka }
1218 1.107.10.2 pooka size = bf + KPRINTF_BUFSIZE - cp;
1219 1.107.10.2 pooka skipsize:
1220 1.107.10.2 pooka break;
1221 1.107.10.2 pooka default: /* "%?" prints ?, unless ? is NUL */
1222 1.107.10.2 pooka if (ch == '\0')
1223 1.107.10.2 pooka goto done;
1224 1.107.10.2 pooka /* pretend it was %c with argument ch */
1225 1.107.10.2 pooka cp = bf;
1226 1.107.10.2 pooka *cp = ch;
1227 1.107.10.2 pooka size = 1;
1228 1.107.10.2 pooka sign = '\0';
1229 1.107.10.2 pooka break;
1230 1.107.10.2 pooka }
1231 1.107.10.2 pooka
1232 1.107.10.2 pooka /*
1233 1.107.10.2 pooka * All reasonable formats wind up here. At this point, `cp'
1234 1.107.10.2 pooka * points to a string which (if not flags&LADJUST) should be
1235 1.107.10.2 pooka * padded out to `width' places. If flags&ZEROPAD, it should
1236 1.107.10.2 pooka * first be prefixed by any sign or other prefix; otherwise,
1237 1.107.10.2 pooka * it should be blank padded before the prefix is emitted.
1238 1.107.10.2 pooka * After any left-hand padding and prefixing, emit zeroes
1239 1.107.10.2 pooka * required by a decimal [diouxX] precision, then print the
1240 1.107.10.2 pooka * string proper, then emit zeroes required by any leftover
1241 1.107.10.2 pooka * floating precision; finally, if LADJUST, pad with blanks.
1242 1.107.10.2 pooka *
1243 1.107.10.2 pooka * Compute actual size, so we know how much to pad.
1244 1.107.10.2 pooka * size excludes decimal prec; realsz includes it.
1245 1.107.10.2 pooka */
1246 1.107.10.2 pooka realsz = dprec > size ? dprec : size;
1247 1.107.10.2 pooka if (sign)
1248 1.107.10.2 pooka realsz++;
1249 1.107.10.2 pooka else if (flags & HEXPREFIX)
1250 1.107.10.2 pooka realsz+= 2;
1251 1.107.10.2 pooka
1252 1.107.10.2 pooka /* adjust ret */
1253 1.107.10.2 pooka ret += width > realsz ? width : realsz;
1254 1.107.10.2 pooka
1255 1.107.10.2 pooka /* right-adjusting blank padding */
1256 1.107.10.2 pooka if ((flags & (LADJUST|ZEROPAD)) == 0) {
1257 1.107.10.2 pooka n = width - realsz;
1258 1.107.10.2 pooka while (n-- > 0)
1259 1.107.10.2 pooka KPRINTF_PUTCHAR(' ');
1260 1.107.10.2 pooka }
1261 1.107.10.2 pooka
1262 1.107.10.2 pooka /* prefix */
1263 1.107.10.2 pooka if (sign) {
1264 1.107.10.2 pooka KPRINTF_PUTCHAR(sign);
1265 1.107.10.2 pooka } else if (flags & HEXPREFIX) {
1266 1.107.10.2 pooka KPRINTF_PUTCHAR('0');
1267 1.107.10.2 pooka KPRINTF_PUTCHAR(ch);
1268 1.107.10.2 pooka }
1269 1.107.10.2 pooka
1270 1.107.10.2 pooka /* right-adjusting zero padding */
1271 1.107.10.2 pooka if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1272 1.107.10.2 pooka n = width - realsz;
1273 1.107.10.2 pooka while (n-- > 0)
1274 1.107.10.2 pooka KPRINTF_PUTCHAR('0');
1275 1.107.10.2 pooka }
1276 1.107.10.2 pooka
1277 1.107.10.2 pooka /* leading zeroes from decimal precision */
1278 1.107.10.2 pooka n = dprec - size;
1279 1.107.10.2 pooka while (n-- > 0)
1280 1.107.10.2 pooka KPRINTF_PUTCHAR('0');
1281 1.107.10.2 pooka
1282 1.107.10.2 pooka /* the string or number proper */
1283 1.107.10.2 pooka while (size--)
1284 1.107.10.2 pooka KPRINTF_PUTCHAR(*cp++);
1285 1.107.10.2 pooka /* left-adjusting padding (always blank) */
1286 1.107.10.2 pooka if (flags & LADJUST) {
1287 1.107.10.2 pooka n = width - realsz;
1288 1.107.10.2 pooka while (n-- > 0)
1289 1.107.10.2 pooka KPRINTF_PUTCHAR(' ');
1290 1.107.10.2 pooka }
1291 1.107.10.2 pooka }
1292 1.107.10.2 pooka
1293 1.107.10.2 pooka done:
1294 1.107.10.2 pooka if ((oflags == TOBUFONLY) && (vp != NULL))
1295 1.107.10.2 pooka *(char **)vp = sbuf;
1296 1.107.10.2 pooka (*v_flush)();
1297 1.107.10.2 pooka overflow:
1298 1.107.10.2 pooka return (ret);
1299 1.107.10.2 pooka /* NOTREACHED */
1300 1.107.10.2 pooka }
1301