printf.c revision 1.1.8.2 1 1.1.8.2 jruoho /*-
2 1.1.8.2 jruoho * Copyright (c) 1998 Robert Nordier
3 1.1.8.2 jruoho * All rights reserved.
4 1.1.8.2 jruoho * Copyright (c) 2006 M. Warner Losh
5 1.1.8.2 jruoho * All rights reserved.
6 1.1.8.2 jruoho *
7 1.1.8.2 jruoho * Redistribution and use in source and binary forms are freely
8 1.1.8.2 jruoho * permitted provided that the above copyright notice and this
9 1.1.8.2 jruoho * paragraph and the following disclaimer are duplicated in all
10 1.1.8.2 jruoho * such forms.
11 1.1.8.2 jruoho *
12 1.1.8.2 jruoho * This software is provided "AS IS" and without any express or
13 1.1.8.2 jruoho * implied warranties, including, without limitation, the implied
14 1.1.8.2 jruoho * warranties of merchantability and fitness for a particular
15 1.1.8.2 jruoho * purpose.
16 1.1.8.2 jruoho *
17 1.1.8.2 jruoho * $FreeBSD: src/sys/boot/mips/emips/libemips/printf.c,v 1.2 2006/10/20 09:12:05 imp Exp $
18 1.1.8.2 jruoho */
19 1.1.8.2 jruoho
20 1.1.8.2 jruoho #include <machine/stdarg.h>
21 1.1.8.2 jruoho
22 1.1.8.2 jruoho void
23 1.1.8.2 jruoho xputchar(int ch)
24 1.1.8.2 jruoho {
25 1.1.8.2 jruoho if (ch == '\n')
26 1.1.8.2 jruoho putchar('\r');
27 1.1.8.2 jruoho putchar(ch);
28 1.1.8.2 jruoho }
29 1.1.8.2 jruoho
30 1.1.8.2 jruoho void
31 1.1.8.2 jruoho printf(const char *fmt,...)
32 1.1.8.2 jruoho {
33 1.1.8.2 jruoho va_list ap;
34 1.1.8.2 jruoho const char *hex = "0123456789abcdef";
35 1.1.8.2 jruoho char buf[10];
36 1.1.8.2 jruoho char *s;
37 1.1.8.2 jruoho unsigned u;
38 1.1.8.2 jruoho int c;
39 1.1.8.2 jruoho
40 1.1.8.2 jruoho va_start(ap, fmt);
41 1.1.8.2 jruoho while ((c = *fmt++)) {
42 1.1.8.2 jruoho if (c == '%') {
43 1.1.8.2 jruoho again:
44 1.1.8.2 jruoho c = *fmt++;
45 1.1.8.2 jruoho switch (c) {
46 1.1.8.2 jruoho case 'l':
47 1.1.8.2 jruoho goto again;
48 1.1.8.2 jruoho case 'c':
49 1.1.8.2 jruoho xputchar(va_arg(ap, int));
50 1.1.8.2 jruoho continue;
51 1.1.8.2 jruoho case 's':
52 1.1.8.2 jruoho for (s = va_arg(ap, char *); s && *s; s++)
53 1.1.8.2 jruoho xputchar(*s);
54 1.1.8.2 jruoho continue;
55 1.1.8.2 jruoho case 'd': /* A lie, always prints unsigned */
56 1.1.8.2 jruoho case 'u':
57 1.1.8.2 jruoho u = va_arg(ap, unsigned);
58 1.1.8.2 jruoho s = buf;
59 1.1.8.2 jruoho do
60 1.1.8.2 jruoho *s++ = '0' + u % 10U;
61 1.1.8.2 jruoho while (u /= 10U);
62 1.1.8.2 jruoho dumpbuf:;
63 1.1.8.2 jruoho while (--s >= buf)
64 1.1.8.2 jruoho xputchar(*s);
65 1.1.8.2 jruoho continue;
66 1.1.8.2 jruoho case 'x':
67 1.1.8.2 jruoho case 'p':
68 1.1.8.2 jruoho u = va_arg(ap, unsigned);
69 1.1.8.2 jruoho s = buf;
70 1.1.8.2 jruoho do
71 1.1.8.2 jruoho *s++ = hex[u & 0xfu];
72 1.1.8.2 jruoho while (u >>= 4);
73 1.1.8.2 jruoho goto dumpbuf;
74 1.1.8.2 jruoho case 0:
75 1.1.8.2 jruoho return;
76 1.1.8.2 jruoho }
77 1.1.8.2 jruoho }
78 1.1.8.2 jruoho xputchar(c);
79 1.1.8.2 jruoho }
80 1.1.8.2 jruoho va_end(ap);
81 1.1.8.2 jruoho
82 1.1.8.2 jruoho return;
83 1.1.8.2 jruoho }
84