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