blob: 7f534c78537411c1cfe9673bd7443fa10dba5f9f [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
Mike Frysinger6c6166f2009-02-16 23:21:36 -050024#ifdef CONFIG_SYS_64BIT_VSPRINTF
25# define NUM_TYPE long long
26#else
27# define NUM_TYPE long
28#endif
29#define noinline __attribute__((noinline))
30
31#define do_div(n, base) ({ \
32 unsigned int __res; \
33 __res = ((unsigned NUM_TYPE) n) % base; \
34 n = ((unsigned NUM_TYPE) n) / base; \
35 __res; \
36})
37
38const char hex_asc[] = "0123456789abcdef";
39#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
40#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
41
42static inline char *pack_hex_byte(char *buf, u8 byte)
43{
44 *buf++ = hex_asc_hi(byte);
45 *buf++ = hex_asc_lo(byte);
46 return buf;
47}
48
wdenk153d5112002-08-30 11:07:04 +000049unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
50{
51 unsigned long result = 0,value;
52
53 if (*cp == '0') {
54 cp++;
55 if ((*cp == 'x') && isxdigit(cp[1])) {
56 base = 16;
57 cp++;
58 }
59 if (!base) {
60 base = 8;
61 }
62 }
63 if (!base) {
64 base = 10;
65 }
66 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
67 ? toupper(*cp) : *cp)-'A'+10) < base) {
68 result = result*base + value;
69 cp++;
70 }
71 if (endp)
72 *endp = (char *)cp;
73 return result;
74}
75
76long simple_strtol(const char *cp,char **endp,unsigned int base)
77{
78 if(*cp=='-')
79 return -simple_strtoul(cp+1,endp,base);
80 return simple_strtoul(cp,endp,base);
81}
82
Kyungmin Park7e6ee7a2008-11-19 16:32:36 +010083int ustrtoul(const char *cp, char **endp, unsigned int base)
84{
85 unsigned long result = simple_strtoul(cp, endp, base);
86 switch (**endp) {
87 case 'G' :
88 result *= 1024;
89 /* fall through */
90 case 'M':
91 result *= 1024;
92 /* fall through */
93 case 'K':
94 case 'k':
95 result *= 1024;
96 if ((*endp)[1] == 'i') {
97 if ((*endp)[2] == 'B')
98 (*endp) += 3;
99 else
100 (*endp) += 2;
101 }
102 }
103 return result;
104}
105
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200106#ifdef CONFIG_SYS_64BIT_STRTOUL
wdenkc40b2952004-03-13 23:29:43 +0000107unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
108{
109 unsigned long long result = 0, value;
110
111 if (*cp == '0') {
112 cp++;
113 if ((*cp == 'x') && isxdigit (cp[1])) {
114 base = 16;
115 cp++;
116 }
117 if (!base) {
118 base = 8;
119 }
120 }
121 if (!base) {
122 base = 10;
123 }
124 while (isxdigit (*cp) && (value = isdigit (*cp)
125 ? *cp - '0'
126 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
127 result = result * base + value;
128 cp++;
129 }
130 if (endp)
131 *endp = (char *) cp;
132 return result;
133}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200134#endif /* CONFIG_SYS_64BIT_STRTOUL */
wdenkc40b2952004-03-13 23:29:43 +0000135
wdenk153d5112002-08-30 11:07:04 +0000136/* we use this so that we can do without the ctype library */
137#define is_digit(c) ((c) >= '0' && (c) <= '9')
138
139static int skip_atoi(const char **s)
140{
141 int i=0;
142
143 while (is_digit(**s))
144 i = i*10 + *((*s)++) - '0';
145 return i;
146}
147
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500148/* Decimal conversion is by far the most typical, and is used
149 * for /proc and /sys data. This directly impacts e.g. top performance
150 * with many processes running. We optimize it for speed
151 * using code from
152 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
153 * (with permission from the author, Douglas W. Jones). */
154
155/* Formats correctly any integer in [0,99999].
156 * Outputs from one to five digits depending on input.
157 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
158static char* put_dec_trunc(char *buf, unsigned q)
159{
160 unsigned d3, d2, d1, d0;
161 d1 = (q>>4) & 0xf;
162 d2 = (q>>8) & 0xf;
163 d3 = (q>>12);
164
165 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
166 q = (d0 * 0xcd) >> 11;
167 d0 = d0 - 10*q;
168 *buf++ = d0 + '0'; /* least significant digit */
169 d1 = q + 9*d3 + 5*d2 + d1;
170 if (d1 != 0) {
171 q = (d1 * 0xcd) >> 11;
172 d1 = d1 - 10*q;
173 *buf++ = d1 + '0'; /* next digit */
174
175 d2 = q + 2*d2;
176 if ((d2 != 0) || (d3 != 0)) {
177 q = (d2 * 0xd) >> 7;
178 d2 = d2 - 10*q;
179 *buf++ = d2 + '0'; /* next digit */
180
181 d3 = q + 4*d3;
182 if (d3 != 0) {
183 q = (d3 * 0xcd) >> 11;
184 d3 = d3 - 10*q;
185 *buf++ = d3 + '0'; /* next digit */
186 if (q != 0)
187 *buf++ = q + '0'; /* most sign. digit */
188 }
189 }
190 }
191 return buf;
192}
193/* Same with if's removed. Always emits five digits */
194static char* put_dec_full(char *buf, unsigned q)
195{
196 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
197 /* but anyway, gcc produces better code with full-sized ints */
198 unsigned d3, d2, d1, d0;
199 d1 = (q>>4) & 0xf;
200 d2 = (q>>8) & 0xf;
201 d3 = (q>>12);
202
Wolfgang Denkc0a14ae2009-04-05 00:27:57 +0200203 /*
204 * Possible ways to approx. divide by 10
205 * gcc -O2 replaces multiply with shifts and adds
206 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
207 * (x * 0x67) >> 10: 1100111
208 * (x * 0x34) >> 9: 110100 - same
209 * (x * 0x1a) >> 8: 11010 - same
210 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
211 */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500212
213 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
214 q = (d0 * 0xcd) >> 11;
215 d0 = d0 - 10*q;
216 *buf++ = d0 + '0';
217 d1 = q + 9*d3 + 5*d2 + d1;
218 q = (d1 * 0xcd) >> 11;
219 d1 = d1 - 10*q;
220 *buf++ = d1 + '0';
221
222 d2 = q + 2*d2;
223 q = (d2 * 0xd) >> 7;
224 d2 = d2 - 10*q;
225 *buf++ = d2 + '0';
226
227 d3 = q + 4*d3;
228 q = (d3 * 0xcd) >> 11; /* - shorter code */
229 /* q = (d3 * 0x67) >> 10; - would also work */
230 d3 = d3 - 10*q;
231 *buf++ = d3 + '0';
232 *buf++ = q + '0';
233 return buf;
234}
235/* No inlining helps gcc to use registers better */
236static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
237{
238 while (1) {
239 unsigned rem;
240 if (num < 100000)
241 return put_dec_trunc(buf, num);
242 rem = do_div(num, 100000);
243 buf = put_dec_full(buf, rem);
244 }
245}
246
wdenk153d5112002-08-30 11:07:04 +0000247#define ZEROPAD 1 /* pad with zero */
248#define SIGN 2 /* unsigned/signed long */
249#define PLUS 4 /* show plus */
250#define SPACE 8 /* space if plus */
251#define LEFT 16 /* left justified */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500252#define SMALL 32 /* Must be 32 == 0x20 */
253#define SPECIAL 64 /* 0x */
wdenk153d5112002-08-30 11:07:04 +0000254
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500255static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
wdenk153d5112002-08-30 11:07:04 +0000256{
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500257 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
258 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
259
260 char tmp[66];
261 char sign;
262 char locase;
263 int need_pfx = ((type & SPECIAL) && base != 10);
wdenk153d5112002-08-30 11:07:04 +0000264 int i;
265
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500266 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
267 * produces same digits or (maybe lowercased) letters */
268 locase = (type & SMALL);
wdenk153d5112002-08-30 11:07:04 +0000269 if (type & LEFT)
270 type &= ~ZEROPAD;
wdenk153d5112002-08-30 11:07:04 +0000271 sign = 0;
272 if (type & SIGN) {
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500273 if ((signed NUM_TYPE) num < 0) {
wdenk153d5112002-08-30 11:07:04 +0000274 sign = '-';
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500275 num = - (signed NUM_TYPE) num;
wdenk153d5112002-08-30 11:07:04 +0000276 size--;
277 } else if (type & PLUS) {
278 sign = '+';
279 size--;
280 } else if (type & SPACE) {
281 sign = ' ';
282 size--;
283 }
284 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500285 if (need_pfx) {
286 size--;
wdenk153d5112002-08-30 11:07:04 +0000287 if (base == 16)
wdenk153d5112002-08-30 11:07:04 +0000288 size--;
289 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500290
291 /* generate full string in tmp[], in reverse order */
wdenk153d5112002-08-30 11:07:04 +0000292 i = 0;
293 if (num == 0)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500294 tmp[i++] = '0';
295 /* Generic code, for any base:
296 else do {
297 tmp[i++] = (digits[do_div(num,base)] | locase);
298 } while (num != 0);
299 */
300 else if (base != 10) { /* 8 or 16 */
301 int mask = base - 1;
302 int shift = 3;
303 if (base == 16) shift = 4;
304 do {
305 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
306 num >>= shift;
307 } while (num);
308 } else { /* base 10 */
309 i = put_dec(tmp, num) - tmp;
310 }
311
312 /* printing 100 using %2d gives "100", not "00" */
wdenk153d5112002-08-30 11:07:04 +0000313 if (i > precision)
314 precision = i;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500315 /* leading space padding */
wdenk153d5112002-08-30 11:07:04 +0000316 size -= precision;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500317 if (!(type & (ZEROPAD+LEFT)))
318 while(--size >= 0)
319 *buf++ = ' ';
320 /* sign */
wdenk153d5112002-08-30 11:07:04 +0000321 if (sign)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500322 *buf++ = sign;
323 /* "0x" / "0" prefix */
324 if (need_pfx) {
325 *buf++ = '0';
326 if (base == 16)
327 *buf++ = ('X' | locase);
wdenk153d5112002-08-30 11:07:04 +0000328 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500329 /* zero or space padding */
330 if (!(type & LEFT)) {
331 char c = (type & ZEROPAD) ? '0' : ' ';
332 while (--size >= 0)
333 *buf++ = c;
334 }
335 /* hmm even more zero padding? */
336 while (i <= --precision)
337 *buf++ = '0';
338 /* actual digits of result */
339 while (--i >= 0)
340 *buf++ = tmp[i];
341 /* trailing space padding */
342 while (--size >= 0)
343 *buf++ = ' ';
344 return buf;
wdenk153d5112002-08-30 11:07:04 +0000345}
346
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500347static char *string(char *buf, char *s, int field_width, int precision, int flags)
348{
349 int len, i;
wdenk153d5112002-08-30 11:07:04 +0000350
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500351 if (s == 0)
352 s = "<NULL>";
353
354 len = strnlen(s, precision);
355
356 if (!(flags & LEFT))
357 while (len < field_width--)
358 *buf++ = ' ';
359 for (i = 0; i < len; ++i)
360 *buf++ = *s++;
361 while (len < field_width--)
362 *buf++ = ' ';
363 return buf;
364}
365
366#ifdef CONFIG_CMD_NET
367static char *mac_address_string(char *buf, u8 *addr, int field_width,
368 int precision, int flags)
369{
370 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
371 char *p = mac_addr;
372 int i;
373
374 for (i = 0; i < 6; i++) {
375 p = pack_hex_byte(p, addr[i]);
376 if (!(flags & SPECIAL) && i != 5)
377 *p++ = ':';
378 }
379 *p = '\0';
380
381 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
382}
383
384static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
385 int precision, int flags)
386{
387 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
388 char *p = ip6_addr;
389 int i;
390
391 for (i = 0; i < 8; i++) {
392 p = pack_hex_byte(p, addr[2 * i]);
393 p = pack_hex_byte(p, addr[2 * i + 1]);
394 if (!(flags & SPECIAL) && i != 7)
395 *p++ = ':';
396 }
397 *p = '\0';
398
399 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
400}
401
402static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
403 int precision, int flags)
404{
405 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
406 char temp[3]; /* hold each IP quad in reverse order */
407 char *p = ip4_addr;
408 int i, digits;
409
410 for (i = 0; i < 4; i++) {
411 digits = put_dec_trunc(temp, addr[i]) - temp;
412 /* reverse the digits in the quad */
413 while (digits--)
414 *p++ = temp[digits];
415 if (i != 3)
416 *p++ = '.';
417 }
418 *p = '\0';
419
420 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
421}
422#endif
423
424/*
425 * Show a '%p' thing. A kernel extension is that the '%p' is followed
426 * by an extra set of alphanumeric characters that are extended format
427 * specifiers.
428 *
429 * Right now we handle:
430 *
431 * - 'M' For a 6-byte MAC address, it prints the address in the
432 * usual colon-separated hex notation
433 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
434 * decimal for v4 and colon separated network-order 16 bit hex for v6)
435 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
436 * currently the same
437 *
438 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
439 * function pointers are really function descriptors, which contain a
440 * pointer to the real address.
441 */
442static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
443{
444 if (!ptr)
445 return string(buf, "(null)", field_width, precision, flags);
446
447#ifdef CONFIG_CMD_NET
448 switch (*fmt) {
449 case 'm':
450 flags |= SPECIAL;
451 /* Fallthrough */
452 case 'M':
453 return mac_address_string(buf, ptr, field_width, precision, flags);
454 case 'i':
455 flags |= SPECIAL;
456 /* Fallthrough */
457 case 'I':
458 if (fmt[1] == '6')
459 return ip6_addr_string(buf, ptr, field_width, precision, flags);
460 if (fmt[1] == '4')
461 return ip4_addr_string(buf, ptr, field_width, precision, flags);
462 flags &= ~SPECIAL;
463 break;
464 }
465#endif
466 flags |= SMALL;
467 if (field_width == -1) {
468 field_width = 2*sizeof(void *);
469 flags |= ZEROPAD;
470 }
471 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
472}
473
474/**
475 * vsprintf - Format a string and place it in a buffer
476 * @buf: The buffer to place the result into
477 * @fmt: The format string to use
478 * @args: Arguments for the format string
479 *
480 * This function follows C99 vsprintf, but has some extensions:
481 * %pS output the name of a text symbol
482 * %pF output the name of a function pointer
483 * %pR output the address range in a struct resource
484 *
485 * The function returns the number of characters written
486 * into @buf.
487 *
488 * Call this function if you are already dealing with a va_list.
489 * You probably want sprintf() instead.
490 */
wdenk153d5112002-08-30 11:07:04 +0000491int vsprintf(char *buf, const char *fmt, va_list args)
492{
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500493 unsigned NUM_TYPE num;
494 int base;
495 char *str;
wdenk153d5112002-08-30 11:07:04 +0000496
497 int flags; /* flags to number() */
498
499 int field_width; /* width of output field */
500 int precision; /* min. # of digits for integers; max
501 number of chars for from string */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500502 int qualifier; /* 'h', 'l', or 'L' for integer fields */
503 /* 'z' support added 23/7/1999 S.H. */
504 /* 'z' changed to 'Z' --davidm 1/25/99 */
505 /* 't' added for ptrdiff_t */
wdenk153d5112002-08-30 11:07:04 +0000506
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500507 str = buf;
508
509 for (; *fmt ; ++fmt) {
wdenk153d5112002-08-30 11:07:04 +0000510 if (*fmt != '%') {
511 *str++ = *fmt;
512 continue;
513 }
514
515 /* process flags */
516 flags = 0;
517 repeat:
518 ++fmt; /* this also skips first '%' */
519 switch (*fmt) {
520 case '-': flags |= LEFT; goto repeat;
521 case '+': flags |= PLUS; goto repeat;
522 case ' ': flags |= SPACE; goto repeat;
523 case '#': flags |= SPECIAL; goto repeat;
524 case '0': flags |= ZEROPAD; goto repeat;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500525 }
wdenk153d5112002-08-30 11:07:04 +0000526
527 /* get field width */
528 field_width = -1;
529 if (is_digit(*fmt))
530 field_width = skip_atoi(&fmt);
531 else if (*fmt == '*') {
532 ++fmt;
533 /* it's the next argument */
534 field_width = va_arg(args, int);
535 if (field_width < 0) {
536 field_width = -field_width;
537 flags |= LEFT;
538 }
539 }
540
541 /* get the precision */
542 precision = -1;
543 if (*fmt == '.') {
544 ++fmt;
545 if (is_digit(*fmt))
546 precision = skip_atoi(&fmt);
547 else if (*fmt == '*') {
548 ++fmt;
549 /* it's the next argument */
550 precision = va_arg(args, int);
551 }
552 if (precision < 0)
553 precision = 0;
554 }
555
556 /* get the conversion qualifier */
557 qualifier = -1;
Jean-Christophe PLAGNIOL-VILLARDf354b732008-07-14 14:11:45 +0200558 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500559 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
wdenk153d5112002-08-30 11:07:04 +0000560 qualifier = *fmt;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500561 ++fmt;
562 if (qualifier == 'l' && *fmt == 'l') {
563 qualifier = 'L';
James Yangbf052932008-01-10 16:02:07 -0600564 ++fmt;
565 }
wdenk153d5112002-08-30 11:07:04 +0000566 }
567
568 /* default base */
569 base = 10;
570
571 switch (*fmt) {
572 case 'c':
573 if (!(flags & LEFT))
574 while (--field_width > 0)
575 *str++ = ' ';
576 *str++ = (unsigned char) va_arg(args, int);
577 while (--field_width > 0)
578 *str++ = ' ';
579 continue;
580
581 case 's':
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500582 str = string(str, va_arg(args, char *), field_width, precision, flags);
wdenk153d5112002-08-30 11:07:04 +0000583 continue;
584
585 case 'p':
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500586 str = pointer(fmt+1, str,
587 va_arg(args, void *),
588 field_width, precision, flags);
589 /* Skip all alphanumeric pointer suffixes */
590 while (isalnum(fmt[1]))
591 fmt++;
wdenk153d5112002-08-30 11:07:04 +0000592 continue;
593
wdenk153d5112002-08-30 11:07:04 +0000594 case 'n':
595 if (qualifier == 'l') {
596 long * ip = va_arg(args, long *);
597 *ip = (str - buf);
598 } else {
599 int * ip = va_arg(args, int *);
600 *ip = (str - buf);
601 }
602 continue;
603
604 case '%':
605 *str++ = '%';
606 continue;
607
608 /* integer number formats - set up the flags and "break" */
609 case 'o':
610 base = 8;
611 break;
612
wdenk153d5112002-08-30 11:07:04 +0000613 case 'x':
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500614 flags |= SMALL;
615 case 'X':
wdenk153d5112002-08-30 11:07:04 +0000616 base = 16;
617 break;
618
619 case 'd':
620 case 'i':
621 flags |= SIGN;
622 case 'u':
623 break;
624
625 default:
626 *str++ = '%';
627 if (*fmt)
628 *str++ = *fmt;
629 else
630 --fmt;
631 continue;
632 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200633#ifdef CONFIG_SYS_64BIT_VSPRINTF
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500634 if (qualifier == 'L') /* "quad" for 64 bit variables */
wdenkc40b2952004-03-13 23:29:43 +0000635 num = va_arg(args, unsigned long long);
636 else
637#endif
Jean-Christophe PLAGNIOL-VILLARDf354b732008-07-14 14:11:45 +0200638 if (qualifier == 'l') {
wdenk153d5112002-08-30 11:07:04 +0000639 num = va_arg(args, unsigned long);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500640 if (flags & SIGN)
641 num = (signed long) num;
Jean-Christophe PLAGNIOL-VILLARDf354b732008-07-14 14:11:45 +0200642 } else if (qualifier == 'Z' || qualifier == 'z') {
643 num = va_arg(args, size_t);
644 } else if (qualifier == 't') {
645 num = va_arg(args, ptrdiff_t);
646 } else if (qualifier == 'h') {
wdenk153d5112002-08-30 11:07:04 +0000647 num = (unsigned short) va_arg(args, int);
648 if (flags & SIGN)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500649 num = (signed short) num;
650 } else {
wdenk153d5112002-08-30 11:07:04 +0000651 num = va_arg(args, unsigned int);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500652 if (flags & SIGN)
653 num = (signed int) num;
654 }
wdenk153d5112002-08-30 11:07:04 +0000655 str = number(str, num, base, field_width, precision, flags);
656 }
657 *str = '\0';
658 return str-buf;
659}
660
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500661/**
662 * sprintf - Format a string and place it in a buffer
663 * @buf: The buffer to place the result into
664 * @fmt: The format string to use
665 * @...: Arguments for the format string
666 *
667 * The function returns the number of characters written
668 * into @buf.
669 *
670 * See the vsprintf() documentation for format string extensions over C99.
671 */
wdenk153d5112002-08-30 11:07:04 +0000672int sprintf(char * buf, const char *fmt, ...)
673{
674 va_list args;
675 int i;
676
677 va_start(args, fmt);
678 i=vsprintf(buf,fmt,args);
679 va_end(args);
680 return i;
681}
682
683void panic(const char *fmt, ...)
684{
685 va_list args;
686 va_start(args, fmt);
wdenk6dd652f2003-06-19 23:40:20 +0000687 vprintf(fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000688 putc('\n');
689 va_end(args);
690#if defined (CONFIG_PANIC_HANG)
691 hang();
692#else
693 udelay (100000); /* allow messages to go out */
694 do_reset (NULL, 0, 0, NULL);
695#endif
696}