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