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