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