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$
18 */
19
20 #include <stdarg.h>
21 #include "lib.h"
22
23 void
printf(const char * fmt,...)24 printf(const char *fmt,...)
25 {
26 va_list ap;
27 const char *hex = "0123456789abcdef";
28 char buf[10];
29 char *s;
30 unsigned u;
31 int c;
32
33 va_start(ap, fmt);
34 while ((c = *fmt++)) {
35 if (c == '%') {
36 c = *fmt++;
37 switch (c) {
38 case 'c':
39 xputchar(va_arg(ap, int));
40 continue;
41 case 's':
42 for (s = va_arg(ap, char *); *s; s++)
43 xputchar(*s);
44 continue;
45 case 'd': /* A lie, always prints unsigned */
46 case 'u':
47 u = va_arg(ap, unsigned);
48 s = buf;
49 do
50 *s++ = '0' + u % 10U;
51 while (u /= 10U);
52 dumpbuf:;
53 while (--s >= buf)
54 xputchar(*s);
55 continue;
56 case 'x':
57 u = va_arg(ap, unsigned);
58 s = buf;
59 do
60 *s++ = hex[u & 0xfu];
61 while (u >>= 4);
62 goto dumpbuf;
63 }
64 }
65 xputchar(c);
66 }
67 va_end(ap);
68
69 return;
70 }
71