blob: 3db6c3f936ce8c418d7a69342ff8faf7b1fc1985 [file] [log] [blame]
wdenk153d5112002-08-30 11:07:04 +00001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
12#include <stdarg.h>
13#include <linux/types.h>
14#include <linux/string.h>
15#include <linux/ctype.h>
16
17#include <common.h>
18#if !defined (CONFIG_PANIC_HANG)
19#include <command.h>
wdenk8bde7f72003-06-27 21:31:46 +000020/*cmd_boot.c*/
21extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenk153d5112002-08-30 11:07:04 +000022#endif
23
24unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
25{
26 unsigned long result = 0,value;
27
28 if (*cp == '0') {
29 cp++;
30 if ((*cp == 'x') && isxdigit(cp[1])) {
31 base = 16;
32 cp++;
33 }
34 if (!base) {
35 base = 8;
36 }
37 }
38 if (!base) {
39 base = 10;
40 }
41 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
42 ? toupper(*cp) : *cp)-'A'+10) < base) {
43 result = result*base + value;
44 cp++;
45 }
46 if (endp)
47 *endp = (char *)cp;
48 return result;
49}
50
51long simple_strtol(const char *cp,char **endp,unsigned int base)
52{
53 if(*cp=='-')
54 return -simple_strtoul(cp+1,endp,base);
55 return simple_strtoul(cp,endp,base);
56}
57
wdenk42dfe7a2004-03-14 22:25:36 +000058#ifdef CFG_64BIT_STRTOUL
wdenkc40b2952004-03-13 23:29:43 +000059unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
60{
61 unsigned long long result = 0, value;
62
63 if (*cp == '0') {
64 cp++;
65 if ((*cp == 'x') && isxdigit (cp[1])) {
66 base = 16;
67 cp++;
68 }
69 if (!base) {
70 base = 8;
71 }
72 }
73 if (!base) {
74 base = 10;
75 }
76 while (isxdigit (*cp) && (value = isdigit (*cp)
77 ? *cp - '0'
78 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
79 result = result * base + value;
80 cp++;
81 }
82 if (endp)
83 *endp = (char *) cp;
84 return result;
85}
86#endif /* CFG_64BIT_STRTOUL */
87
wdenk153d5112002-08-30 11:07:04 +000088/* we use this so that we can do without the ctype library */
89#define is_digit(c) ((c) >= '0' && (c) <= '9')
90
91static int skip_atoi(const char **s)
92{
93 int i=0;
94
95 while (is_digit(**s))
96 i = i*10 + *((*s)++) - '0';
97 return i;
98}
99
100#define ZEROPAD 1 /* pad with zero */
101#define SIGN 2 /* unsigned/signed long */
102#define PLUS 4 /* show plus */
103#define SPACE 8 /* space if plus */
104#define LEFT 16 /* left justified */
105#define SPECIAL 32 /* 0x */
106#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
107
James Yangbf052932008-01-10 16:02:07 -0600108#ifdef CFG_64BIT_VSPRINTF
wdenk153d5112002-08-30 11:07:04 +0000109#define do_div(n,base) ({ \
James Yangbf052932008-01-10 16:02:07 -0600110 unsigned int __res; \
111 __res = ((unsigned long long) n) % base; \
112 n = ((unsigned long long) n) / base; \
wdenkc40b2952004-03-13 23:29:43 +0000113 __res; \
114})
James Yangbf052932008-01-10 16:02:07 -0600115#else
116#define do_div(n,base) ({ \
117 int __res; \
118 __res = ((unsigned long) n) % base; \
119 n = ((unsigned long) n) / base; \
120 __res; \
121})
122#endif
wdenk153d5112002-08-30 11:07:04 +0000123
wdenk42dfe7a2004-03-14 22:25:36 +0000124#ifdef CFG_64BIT_VSPRINTF
James Yangbf052932008-01-10 16:02:07 -0600125static char * number(char * str, long long num, unsigned int base, int size, int precision ,int type)
wdenkc40b2952004-03-13 23:29:43 +0000126#else
James Yangbf052932008-01-10 16:02:07 -0600127static char * number(char * str, long num, unsigned int base, int size, int precision ,int type)
wdenkc40b2952004-03-13 23:29:43 +0000128#endif
wdenk153d5112002-08-30 11:07:04 +0000129{
130 char c,sign,tmp[66];
131 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
132 int i;
133
134 if (type & LARGE)
135 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
136 if (type & LEFT)
137 type &= ~ZEROPAD;
138 if (base < 2 || base > 36)
139 return 0;
140 c = (type & ZEROPAD) ? '0' : ' ';
141 sign = 0;
142 if (type & SIGN) {
143 if (num < 0) {
144 sign = '-';
145 num = -num;
146 size--;
147 } else if (type & PLUS) {
148 sign = '+';
149 size--;
150 } else if (type & SPACE) {
151 sign = ' ';
152 size--;
153 }
154 }
155 if (type & SPECIAL) {
156 if (base == 16)
157 size -= 2;
158 else if (base == 8)
159 size--;
160 }
161 i = 0;
162 if (num == 0)
163 tmp[i++]='0';
164 else while (num != 0)
165 tmp[i++] = digits[do_div(num,base)];
166 if (i > precision)
167 precision = i;
168 size -= precision;
169 if (!(type&(ZEROPAD+LEFT)))
170 while(size-->0)
171 *str++ = ' ';
172 if (sign)
173 *str++ = sign;
174 if (type & SPECIAL) {
175 if (base==8)
176 *str++ = '0';
177 else if (base==16) {
178 *str++ = '0';
179 *str++ = digits[33];
180 }
181 }
182 if (!(type & LEFT))
183 while (size-- > 0)
184 *str++ = c;
185 while (i < precision--)
186 *str++ = '0';
187 while (i-- > 0)
188 *str++ = tmp[i];
189 while (size-- > 0)
190 *str++ = ' ';
191 return str;
192}
193
194/* Forward decl. needed for IP address printing stuff... */
195int sprintf(char * buf, const char *fmt, ...);
196
197int vsprintf(char *buf, const char *fmt, va_list args)
198{
199 int len;
wdenk42dfe7a2004-03-14 22:25:36 +0000200#ifdef CFG_64BIT_VSPRINTF
wdenkc40b2952004-03-13 23:29:43 +0000201 unsigned long long num;
202#else
wdenk153d5112002-08-30 11:07:04 +0000203 unsigned long num;
wdenkc40b2952004-03-13 23:29:43 +0000204#endif
wdenk153d5112002-08-30 11:07:04 +0000205 int i, base;
206 char * str;
207 const char *s;
208
209 int flags; /* flags to number() */
210
211 int field_width; /* width of output field */
212 int precision; /* min. # of digits for integers; max
213 number of chars for from string */
wdenkc40b2952004-03-13 23:29:43 +0000214 int qualifier; /* 'h', 'l', or 'q' for integer fields */
wdenk153d5112002-08-30 11:07:04 +0000215
216 for (str=buf ; *fmt ; ++fmt) {
217 if (*fmt != '%') {
218 *str++ = *fmt;
219 continue;
220 }
221
222 /* process flags */
223 flags = 0;
224 repeat:
225 ++fmt; /* this also skips first '%' */
226 switch (*fmt) {
227 case '-': flags |= LEFT; goto repeat;
228 case '+': flags |= PLUS; goto repeat;
229 case ' ': flags |= SPACE; goto repeat;
230 case '#': flags |= SPECIAL; goto repeat;
231 case '0': flags |= ZEROPAD; goto repeat;
232 }
233
234 /* get field width */
235 field_width = -1;
236 if (is_digit(*fmt))
237 field_width = skip_atoi(&fmt);
238 else if (*fmt == '*') {
239 ++fmt;
240 /* it's the next argument */
241 field_width = va_arg(args, int);
242 if (field_width < 0) {
243 field_width = -field_width;
244 flags |= LEFT;
245 }
246 }
247
248 /* get the precision */
249 precision = -1;
250 if (*fmt == '.') {
251 ++fmt;
252 if (is_digit(*fmt))
253 precision = skip_atoi(&fmt);
254 else if (*fmt == '*') {
255 ++fmt;
256 /* it's the next argument */
257 precision = va_arg(args, int);
258 }
259 if (precision < 0)
260 precision = 0;
261 }
262
263 /* get the conversion qualifier */
264 qualifier = -1;
wdenkc40b2952004-03-13 23:29:43 +0000265 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'q') {
wdenk153d5112002-08-30 11:07:04 +0000266 qualifier = *fmt;
James Yangbf052932008-01-10 16:02:07 -0600267 if (qualifier == 'l' && *(fmt+1) == 'l') {
268 qualifier = 'q';
269 ++fmt;
270 }
wdenk153d5112002-08-30 11:07:04 +0000271 ++fmt;
272 }
273
274 /* default base */
275 base = 10;
276
277 switch (*fmt) {
278 case 'c':
279 if (!(flags & LEFT))
280 while (--field_width > 0)
281 *str++ = ' ';
282 *str++ = (unsigned char) va_arg(args, int);
283 while (--field_width > 0)
284 *str++ = ' ';
285 continue;
286
287 case 's':
288 s = va_arg(args, char *);
289 if (!s)
290 s = "<NULL>";
291
292 len = strnlen(s, precision);
293
294 if (!(flags & LEFT))
295 while (len < field_width--)
296 *str++ = ' ';
297 for (i = 0; i < len; ++i)
298 *str++ = *s++;
299 while (len < field_width--)
300 *str++ = ' ';
301 continue;
302
303 case 'p':
304 if (field_width == -1) {
305 field_width = 2*sizeof(void *);
306 flags |= ZEROPAD;
307 }
308 str = number(str,
309 (unsigned long) va_arg(args, void *), 16,
310 field_width, precision, flags);
311 continue;
312
313
314 case 'n':
315 if (qualifier == 'l') {
316 long * ip = va_arg(args, long *);
317 *ip = (str - buf);
318 } else {
319 int * ip = va_arg(args, int *);
320 *ip = (str - buf);
321 }
322 continue;
323
324 case '%':
325 *str++ = '%';
326 continue;
327
328 /* integer number formats - set up the flags and "break" */
329 case 'o':
330 base = 8;
331 break;
332
333 case 'X':
334 flags |= LARGE;
335 case 'x':
336 base = 16;
337 break;
338
339 case 'd':
340 case 'i':
341 flags |= SIGN;
342 case 'u':
343 break;
344
345 default:
346 *str++ = '%';
347 if (*fmt)
348 *str++ = *fmt;
349 else
350 --fmt;
351 continue;
352 }
wdenk42dfe7a2004-03-14 22:25:36 +0000353#ifdef CFG_64BIT_VSPRINTF
wdenkc40b2952004-03-13 23:29:43 +0000354 if (qualifier == 'q') /* "quad" for 64 bit variables */
355 num = va_arg(args, unsigned long long);
356 else
357#endif
wdenk153d5112002-08-30 11:07:04 +0000358 if (qualifier == 'l')
359 num = va_arg(args, unsigned long);
360 else if (qualifier == 'h') {
361 num = (unsigned short) va_arg(args, int);
362 if (flags & SIGN)
363 num = (short) num;
364 } else if (flags & SIGN)
365 num = va_arg(args, int);
366 else
367 num = va_arg(args, unsigned int);
368 str = number(str, num, base, field_width, precision, flags);
369 }
370 *str = '\0';
371 return str-buf;
372}
373
374int sprintf(char * buf, const char *fmt, ...)
375{
376 va_list args;
377 int i;
378
379 va_start(args, fmt);
380 i=vsprintf(buf,fmt,args);
381 va_end(args);
382 return i;
383}
384
385void panic(const char *fmt, ...)
386{
387 va_list args;
388 va_start(args, fmt);
wdenk6dd652f2003-06-19 23:40:20 +0000389 vprintf(fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000390 putc('\n');
391 va_end(args);
392#if defined (CONFIG_PANIC_HANG)
393 hang();
394#else
395 udelay (100000); /* allow messages to go out */
396 do_reset (NULL, 0, 0, NULL);
397#endif
398}