xref: /netbsd-src/sys/arch/emips/stand/common/printf.c (revision 28adc7db173f575aed8bb7faee253f2f2b6a2aa5)
1*28adc7dbSdholland /*	$NetBSD: printf.c,v 1.7 2016/02/14 18:05:31 dholland Exp $	*/
25f7e80a8Spooka /*-
35f7e80a8Spooka  * Copyright (c) 1998 Robert Nordier
45f7e80a8Spooka  * All rights reserved.
55f7e80a8Spooka  * Copyright (c) 2006 M. Warner Losh
65f7e80a8Spooka  * All rights reserved.
75f7e80a8Spooka  *
85f7e80a8Spooka  * Redistribution and use in source and binary forms are freely
95f7e80a8Spooka  * permitted provided that the above copyright notice and this
105f7e80a8Spooka  * paragraph and the following disclaimer are duplicated in all
115f7e80a8Spooka  * such forms.
125f7e80a8Spooka  *
135f7e80a8Spooka  * This software is provided "AS IS" and without any express or
145f7e80a8Spooka  * implied warranties, including, without limitation, the implied
155f7e80a8Spooka  * warranties of merchantability and fitness for a particular
165f7e80a8Spooka  * purpose.
175f7e80a8Spooka  *
185f7e80a8Spooka  * $FreeBSD: src/sys/boot/mips/emips/libemips/printf.c,v 1.2 2006/10/20 09:12:05 imp Exp $
195f7e80a8Spooka  */
205f7e80a8Spooka 
2131b6de9fSjoerg #include <lib/libsa/stand.h>
225bfa93ffSmartin #include "common.h"
232c07e798Smartin 
245f7e80a8Spooka void
xputchar(int ch)255f7e80a8Spooka xputchar(int ch)
265f7e80a8Spooka {
275f7e80a8Spooka     if (ch == '\n')
285f7e80a8Spooka 	putchar('\r');
295f7e80a8Spooka     putchar(ch);
305f7e80a8Spooka }
315f7e80a8Spooka 
325f7e80a8Spooka void
printf(const char * fmt,...)335f7e80a8Spooka printf(const char *fmt,...)
345f7e80a8Spooka {
355f7e80a8Spooka 	va_list ap;
365f7e80a8Spooka 	const char *hex = "0123456789abcdef";
375f7e80a8Spooka 	char buf[10];
385f7e80a8Spooka 	char *s;
395f7e80a8Spooka 	unsigned u;
405f7e80a8Spooka 	int c;
415f7e80a8Spooka 
425f7e80a8Spooka 	va_start(ap, fmt);
435f7e80a8Spooka 	while ((c = *fmt++)) {
445f7e80a8Spooka 		if (c == '%') {
455f7e80a8Spooka         again:
465f7e80a8Spooka 			c = *fmt++;
475f7e80a8Spooka 			switch (c) {
485f7e80a8Spooka 			case 'l':
495f7e80a8Spooka 				goto again;
505f7e80a8Spooka 			case 'c':
515f7e80a8Spooka 				xputchar(va_arg(ap, int));
525f7e80a8Spooka 				continue;
535f7e80a8Spooka 			case 's':
545f7e80a8Spooka 				for (s = va_arg(ap, char *); s && *s; s++)
555f7e80a8Spooka 					xputchar(*s);
565f7e80a8Spooka 				continue;
575f7e80a8Spooka 			case 'd':	/* A lie, always prints unsigned */
585f7e80a8Spooka 			case 'u':
595f7e80a8Spooka 				u = va_arg(ap, unsigned);
605f7e80a8Spooka 				s = buf;
615f7e80a8Spooka 				do
625f7e80a8Spooka 					*s++ = '0' + u % 10U;
635f7e80a8Spooka 				while (u /= 10U);
645f7e80a8Spooka 			dumpbuf:;
655f7e80a8Spooka 				while (--s >= buf)
665f7e80a8Spooka 					xputchar(*s);
675f7e80a8Spooka 				continue;
685f7e80a8Spooka 			case 'x':
695f7e80a8Spooka 			case 'p':
705f7e80a8Spooka 				u = va_arg(ap, unsigned);
715f7e80a8Spooka 				s = buf;
725f7e80a8Spooka 				do
735f7e80a8Spooka 					*s++ = hex[u & 0xfu];
745f7e80a8Spooka 				while (u >>= 4);
755f7e80a8Spooka 				goto dumpbuf;
765f7e80a8Spooka 			case 0:
7748ddc7deSdholland 				va_end(ap);
785f7e80a8Spooka 				return;
795f7e80a8Spooka 			}
805f7e80a8Spooka 		}
815f7e80a8Spooka 		xputchar(c);
825f7e80a8Spooka 	}
835f7e80a8Spooka 	va_end(ap);
845f7e80a8Spooka 
855f7e80a8Spooka 	return;
865f7e80a8Spooka }
87