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