move __di_check_ahbprot call to di.c, to allow me to disable DVDX stub loading again
[libogc.git] / libogc / kprintf.c
blob976a756704818bdda6bdbcb54263d5362c0f9f3d
1 /*
2 * linux/lib/vsprintf.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
12 #include <stdarg.h>
13 #include <string.h>
14 #include <unistd.h>
16 /* we use this so that we can do without the ctype library */
17 #define is_digit(c) ((c) >= '0' && (c) <= '9')
19 static int skip_atoi(const char **s)
21 int i=0;
23 while (is_digit(**s))
24 i = i*10 + *((*s)++) - '0';
25 return i;
28 #define ZEROPAD 1 /* pad with zero */
29 #define SIGN 2 /* unsigned/signed long */
30 #define PLUS 4 /* show plus */
31 #define SPACE 8 /* space if plus */
32 #define LEFT 16 /* left justified */
33 #define SPECIAL 32 /* 0x */
34 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
36 #define do_div(n,base) ({ \
37 int __res; \
38 __res = ((unsigned long) n) % (unsigned) base; \
39 n = ((unsigned long) n) / (unsigned) base; \
40 __res; })
42 static char * number(char * str, long num, int base, int size, int precision
43 ,int type)
45 char c,sign,tmp[66];
46 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
47 int i;
49 if (type & LARGE)
50 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
51 if (type & LEFT)
52 type &= ~ZEROPAD;
53 if (base < 2 || base > 36)
54 return 0;
55 c = (type & ZEROPAD) ? '0' : ' ';
56 sign = 0;
57 if (type & SIGN) {
58 if (num < 0) {
59 sign = '-';
60 num = -num;
61 size--;
62 } else if (type & PLUS) {
63 sign = '+';
64 size--;
65 } else if (type & SPACE) {
66 sign = ' ';
67 size--;
70 if (type & SPECIAL) {
71 if (base == 16)
72 size -= 2;
73 else if (base == 8)
74 size--;
76 i = 0;
77 if (num == 0)
78 tmp[i++]='0';
79 else while (num != 0)
80 tmp[i++] = digits[do_div(num,base)];
81 if (i > precision)
82 precision = i;
83 size -= precision;
84 if (!(type&(ZEROPAD+LEFT)))
85 while(size-->0)
86 *str++ = ' ';
87 if (sign)
88 *str++ = sign;
89 if (type & SPECIAL) {
90 if (base==8)
91 *str++ = '0';
92 else if (base==16) {
93 *str++ = '0';
94 *str++ = digits[33];
97 if (!(type & LEFT))
98 while (size-- > 0)
99 *str++ = c;
100 while (i < precision--)
101 *str++ = '0';
102 while (i-- > 0)
103 *str++ = tmp[i];
104 while (size-- > 0)
105 *str++ = ' ';
106 return str;
109 int kvsprintf(char *buf, const char *fmt, va_list args)
111 int len;
112 unsigned long num;
113 int i, base;
114 char * str;
115 const char *s;
117 int flags; /* flags to number() */
119 int field_width; /* width of output field */
120 int precision; /* min. # of digits for integers; max
121 number of chars for from string */
122 int qualifier; /* 'h', 'l', or 'L' for integer fields */
124 for (str=buf ; *fmt ; ++fmt) {
125 if (*fmt != '%') {
126 *str++ = *fmt;
127 continue;
130 /* process flags */
131 flags = 0;
132 repeat:
133 ++fmt; /* this also skips first '%' */
134 switch (*fmt) {
135 case '-': flags |= LEFT; goto repeat;
136 case '+': flags |= PLUS; goto repeat;
137 case ' ': flags |= SPACE; goto repeat;
138 case '#': flags |= SPECIAL; goto repeat;
139 case '0': flags |= ZEROPAD; goto repeat;
142 /* get field width */
143 field_width = -1;
144 if (is_digit(*fmt))
145 field_width = skip_atoi(&fmt);
146 else if (*fmt == '*') {
147 ++fmt;
148 /* it's the next argument */
149 field_width = va_arg(args, int);
150 if (field_width < 0) {
151 field_width = -field_width;
152 flags |= LEFT;
156 /* get the precision */
157 precision = -1;
158 if (*fmt == '.') {
159 ++fmt;
160 if (is_digit(*fmt))
161 precision = skip_atoi(&fmt);
162 else if (*fmt == '*') {
163 ++fmt;
164 /* it's the next argument */
165 precision = va_arg(args, int);
167 if (precision < 0)
168 precision = 0;
171 /* get the conversion qualifier */
172 qualifier = -1;
173 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
174 qualifier = *fmt;
175 ++fmt;
178 /* default base */
179 base = 10;
181 switch (*fmt) {
182 case 'c':
183 if (!(flags & LEFT))
184 while (--field_width > 0)
185 *str++ = ' ';
186 *str++ = (unsigned char) va_arg(args, int);
187 while (--field_width > 0)
188 *str++ = ' ';
189 continue;
191 case 's':
192 s = va_arg(args, char *);
193 if (!s)
194 s = "<NULL>";
196 len = strnlen(s, precision);
198 if (!(flags & LEFT))
199 while (len < field_width--)
200 *str++ = ' ';
201 for (i = 0; i < len; ++i)
202 *str++ = *s++;
203 while (len < field_width--)
204 *str++ = ' ';
205 continue;
207 case 'p':
208 if (field_width == -1) {
209 field_width = 2*sizeof(void *);
210 flags |= ZEROPAD;
212 str = number(str,
213 (unsigned long) va_arg(args, void *), 16,
214 field_width, precision, flags);
215 continue;
218 case 'n':
219 if (qualifier == 'l') {
220 long * ip = va_arg(args, long *);
221 *ip = (str - buf);
222 } else {
223 int * ip = va_arg(args, int *);
224 *ip = (str - buf);
226 continue;
228 case '%':
229 *str++ = '%';
230 continue;
232 /* integer number formats - set up the flags and "break" */
233 case 'o':
234 base = 8;
235 break;
237 case 'X':
238 flags |= LARGE;
239 case 'x':
240 base = 16;
241 break;
243 case 'd':
244 case 'i':
245 flags |= SIGN;
246 case 'u':
247 break;
249 default:
250 *str++ = '%';
251 if (*fmt)
252 *str++ = *fmt;
253 else
254 --fmt;
255 continue;
257 if (qualifier == 'l')
258 num = va_arg(args, unsigned long);
259 else if (qualifier == 'h') {
260 num = (unsigned short) va_arg(args, int);
261 if (flags & SIGN)
262 num = (short) num;
263 } else if (flags & SIGN)
264 num = va_arg(args, int);
265 else
266 num = va_arg(args, unsigned int);
267 str = number(str, num, base, field_width, precision, flags);
269 *str = '\0';
270 return str-buf;
273 #define __DOUTBUFSIZE 256
275 char __outstr[__DOUTBUFSIZE];
277 //---------------------------------------------------------------------------------
278 void kprintf(const char *str, ...)
279 //---------------------------------------------------------------------------------
282 int len;
284 va_list args;
286 va_start(args, str);
287 len=kvsprintf(__outstr,str,args);
288 va_end(args);
290 write(2, __outstr, len);