Home | History | Annotate | Line # | Download | only in common
printf.c revision 1.3.12.1
      1       1.3  joerg /*	$NetBSD: printf.c,v 1.3.12.1 2014/08/20 00:02:52 tls 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.3.12.1    tls #include "common.h"
     23       1.1  pooka 
     24       1.1  pooka void
     25       1.1  pooka xputchar(int ch)
     26       1.1  pooka {
     27       1.1  pooka     if (ch == '\n')
     28       1.1  pooka 	putchar('\r');
     29       1.1  pooka     putchar(ch);
     30       1.1  pooka }
     31       1.1  pooka 
     32       1.1  pooka void
     33       1.1  pooka printf(const char *fmt,...)
     34       1.1  pooka {
     35       1.1  pooka 	va_list ap;
     36       1.1  pooka 	const char *hex = "0123456789abcdef";
     37       1.1  pooka 	char buf[10];
     38       1.1  pooka 	char *s;
     39       1.1  pooka 	unsigned u;
     40       1.1  pooka 	int c;
     41       1.1  pooka 
     42       1.1  pooka 	va_start(ap, fmt);
     43       1.1  pooka 	while ((c = *fmt++)) {
     44       1.1  pooka 		if (c == '%') {
     45       1.1  pooka         again:
     46       1.1  pooka 			c = *fmt++;
     47       1.1  pooka 			switch (c) {
     48       1.1  pooka 			case 'l':
     49       1.1  pooka 				goto again;
     50       1.1  pooka 			case 'c':
     51       1.1  pooka 				xputchar(va_arg(ap, int));
     52       1.1  pooka 				continue;
     53       1.1  pooka 			case 's':
     54       1.1  pooka 				for (s = va_arg(ap, char *); s && *s; s++)
     55       1.1  pooka 					xputchar(*s);
     56       1.1  pooka 				continue;
     57       1.1  pooka 			case 'd':	/* A lie, always prints unsigned */
     58       1.1  pooka 			case 'u':
     59       1.1  pooka 				u = va_arg(ap, unsigned);
     60       1.1  pooka 				s = buf;
     61       1.1  pooka 				do
     62       1.1  pooka 					*s++ = '0' + u % 10U;
     63       1.1  pooka 				while (u /= 10U);
     64       1.1  pooka 			dumpbuf:;
     65       1.1  pooka 				while (--s >= buf)
     66       1.1  pooka 					xputchar(*s);
     67       1.1  pooka 				continue;
     68       1.1  pooka 			case 'x':
     69       1.1  pooka 			case 'p':
     70       1.1  pooka 				u = va_arg(ap, unsigned);
     71       1.1  pooka 				s = buf;
     72       1.1  pooka 				do
     73       1.1  pooka 					*s++ = hex[u & 0xfu];
     74       1.1  pooka 				while (u >>= 4);
     75       1.1  pooka 				goto dumpbuf;
     76       1.1  pooka             case 0:
     77       1.1  pooka                 return;
     78       1.1  pooka 			}
     79       1.1  pooka 		}
     80       1.1  pooka 		xputchar(c);
     81       1.1  pooka 	}
     82       1.1  pooka 	va_end(ap);
     83       1.1  pooka 
     84       1.1  pooka 	return;
     85       1.1  pooka }
     86