1157873Simp/*-
2157873Simp * Copyright (c) 1998 Robert Nordier
3157873Simp * All rights reserved.
4157873Simp * Copyright (c) 2006 M. Warner Losh
5157873Simp * All rights reserved.
6157873Simp *
7157873Simp * Redistribution and use in source and binary forms are freely
8157873Simp * permitted provided that the above copyright notice and this
9157873Simp * paragraph and the following disclaimer are duplicated in all
10157873Simp * such forms.
11157873Simp *
12157873Simp * This software is provided "AS IS" and without any express or
13157873Simp * implied warranties, including, without limitation, the implied
14157873Simp * warranties of merchantability and fitness for a particular
15157873Simp * purpose.
16157873Simp *
17157873Simp * $FreeBSD$
18157873Simp */
19157873Simp
20157873Simp#include <stdarg.h>
21157873Simp#include "lib.h"
22157873Simp
23157873Simpvoid
24157873Simpprintf(const char *fmt,...)
25157873Simp{
26157873Simp	va_list ap;
27157873Simp	const char *hex = "0123456789abcdef";
28157873Simp	char buf[10];
29157873Simp	char *s;
30157873Simp	unsigned u;
31157873Simp	int c;
32157873Simp
33157873Simp	va_start(ap, fmt);
34157873Simp	while ((c = *fmt++)) {
35157873Simp		if (c == '%') {
36157873Simp			c = *fmt++;
37157873Simp			switch (c) {
38157873Simp			case 'c':
39163533Simp				xputchar(va_arg(ap, int));
40157873Simp				continue;
41157873Simp			case 's':
42157873Simp				for (s = va_arg(ap, char *); *s; s++)
43163533Simp					xputchar(*s);
44157873Simp				continue;
45163533Simp			case 'd':	/* A lie, always prints unsigned */
46157873Simp			case 'u':
47157873Simp				u = va_arg(ap, unsigned);
48157873Simp				s = buf;
49157873Simp				do
50157873Simp					*s++ = '0' + u % 10U;
51157873Simp				while (u /= 10U);
52157873Simp			dumpbuf:;
53157873Simp				while (--s >= buf)
54163533Simp					xputchar(*s);
55157873Simp				continue;
56157873Simp			case 'x':
57157873Simp				u = va_arg(ap, unsigned);
58157873Simp				s = buf;
59157873Simp				do
60157873Simp					*s++ = hex[u & 0xfu];
61157873Simp				while (u >>= 4);
62157873Simp				goto dumpbuf;
63157873Simp			}
64157873Simp		}
65163533Simp		xputchar(c);
66157873Simp	}
67157873Simp	va_end(ap);
68157873Simp
69157873Simp	return;
70157873Simp}
71