blob: e497a8686ed321acf5119e290fd32b0fd74ed61d [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 :-)
Simon Glass931948a2011-10-21 18:51:33 +000010 *
11 * from hush: simple_itoa() was lifted from boa-0.93.15
wdenk153d5112002-08-30 11:07:04 +000012 */
13
14#include <stdarg.h>
15#include <linux/types.h>
16#include <linux/string.h>
17#include <linux/ctype.h>
Heiko Schocher8d4f35a2011-05-02 21:33:49 +000018#include <errno.h>
wdenk153d5112002-08-30 11:07:04 +000019
20#include <common.h>
21#if !defined (CONFIG_PANIC_HANG)
22#include <command.h>
wdenk153d5112002-08-30 11:07:04 +000023#endif
24
Dirk Behme3d8759e2009-07-22 17:51:56 +020025#include <div64.h>
Mike Frysinger2b745dc2009-02-16 23:21:36 -050026# define NUM_TYPE long long
Dirk Behme3d8759e2009-07-22 17:51:56 +020027#define noinline __attribute__((noinline))
28
Mike Frysinger2b745dc2009-02-16 23:21:36 -050029const char hex_asc[] = "0123456789abcdef";
30#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
31#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
32
33static inline char *pack_hex_byte(char *buf, u8 byte)
34{
35 *buf++ = hex_asc_hi(byte);
36 *buf++ = hex_asc_lo(byte);
37 return buf;
38}
39
wdenk153d5112002-08-30 11:07:04 +000040unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
41{
42 unsigned long result = 0,value;
43
44 if (*cp == '0') {
45 cp++;
46 if ((*cp == 'x') && isxdigit(cp[1])) {
47 base = 16;
48 cp++;
49 }
50 if (!base) {
51 base = 8;
52 }
53 }
54 if (!base) {
55 base = 10;
56 }
57 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
58 ? toupper(*cp) : *cp)-'A'+10) < base) {
59 result = result*base + value;
60 cp++;
61 }
62 if (endp)
63 *endp = (char *)cp;
64 return result;
65}
66
Heiko Schocher8d4f35a2011-05-02 21:33:49 +000067/**
68 * strict_strtoul - convert a string to an unsigned long strictly
69 * @cp: The string to be converted
70 * @base: The number base to use
71 * @res: The converted result value
72 *
73 * strict_strtoul converts a string to an unsigned long only if the
74 * string is really an unsigned long string, any string containing
75 * any invalid char at the tail will be rejected and -EINVAL is returned,
76 * only a newline char at the tail is acceptible because people generally
77 * change a module parameter in the following way:
78 *
79 * echo 1024 > /sys/module/e1000/parameters/copybreak
80 *
81 * echo will append a newline to the tail.
82 *
83 * It returns 0 if conversion is successful and *res is set to the converted
84 * value, otherwise it returns -EINVAL and *res is set to 0.
85 *
86 * simple_strtoul just ignores the successive invalid characters and
87 * return the converted value of prefix part of the string.
88 *
89 * Copied this function from Linux 2.6.38 commit ID:
90 * 521cb40b0c44418a4fd36dc633f575813d59a43d
91 *
92 */
93int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
94{
95 char *tail;
96 unsigned long val;
97 size_t len;
98
99 *res = 0;
100 len = strlen(cp);
101 if (len == 0)
102 return -EINVAL;
103
104 val = simple_strtoul(cp, &tail, base);
105 if (tail == cp)
106 return -EINVAL;
107
108 if ((*tail == '\0') ||
109 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
110 *res = val;
111 return 0;
112 }
113
114 return -EINVAL;
115}
116
wdenk153d5112002-08-30 11:07:04 +0000117long simple_strtol(const char *cp,char **endp,unsigned int base)
118{
119 if(*cp=='-')
120 return -simple_strtoul(cp+1,endp,base);
121 return simple_strtoul(cp,endp,base);
122}
123
Kyungmin Parkad7ae362008-11-19 16:32:36 +0100124int ustrtoul(const char *cp, char **endp, unsigned int base)
125{
126 unsigned long result = simple_strtoul(cp, endp, base);
127 switch (**endp) {
128 case 'G' :
129 result *= 1024;
130 /* fall through */
131 case 'M':
132 result *= 1024;
133 /* fall through */
134 case 'K':
135 case 'k':
136 result *= 1024;
137 if ((*endp)[1] == 'i') {
138 if ((*endp)[2] == 'B')
139 (*endp) += 3;
140 else
141 (*endp) += 2;
142 }
143 }
144 return result;
145}
146
wdenkf602aa02004-03-13 23:29:43 +0000147unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
148{
149 unsigned long long result = 0, value;
150
151 if (*cp == '0') {
152 cp++;
153 if ((*cp == 'x') && isxdigit (cp[1])) {
154 base = 16;
155 cp++;
156 }
157 if (!base) {
158 base = 8;
159 }
160 }
161 if (!base) {
162 base = 10;
163 }
164 while (isxdigit (*cp) && (value = isdigit (*cp)
165 ? *cp - '0'
166 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
167 result = result * base + value;
168 cp++;
169 }
170 if (endp)
171 *endp = (char *) cp;
172 return result;
173}
wdenkf602aa02004-03-13 23:29:43 +0000174
wdenk153d5112002-08-30 11:07:04 +0000175/* we use this so that we can do without the ctype library */
176#define is_digit(c) ((c) >= '0' && (c) <= '9')
177
178static int skip_atoi(const char **s)
179{
180 int i=0;
181
182 while (is_digit(**s))
183 i = i*10 + *((*s)++) - '0';
184 return i;
185}
186
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500187/* Decimal conversion is by far the most typical, and is used
188 * for /proc and /sys data. This directly impacts e.g. top performance
189 * with many processes running. We optimize it for speed
190 * using code from
191 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
192 * (with permission from the author, Douglas W. Jones). */
193
194/* Formats correctly any integer in [0,99999].
195 * Outputs from one to five digits depending on input.
196 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
197static char* put_dec_trunc(char *buf, unsigned q)
198{
199 unsigned d3, d2, d1, d0;
200 d1 = (q>>4) & 0xf;
201 d2 = (q>>8) & 0xf;
202 d3 = (q>>12);
203
204 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
205 q = (d0 * 0xcd) >> 11;
206 d0 = d0 - 10*q;
207 *buf++ = d0 + '0'; /* least significant digit */
208 d1 = q + 9*d3 + 5*d2 + d1;
209 if (d1 != 0) {
210 q = (d1 * 0xcd) >> 11;
211 d1 = d1 - 10*q;
212 *buf++ = d1 + '0'; /* next digit */
213
214 d2 = q + 2*d2;
215 if ((d2 != 0) || (d3 != 0)) {
216 q = (d2 * 0xd) >> 7;
217 d2 = d2 - 10*q;
218 *buf++ = d2 + '0'; /* next digit */
219
220 d3 = q + 4*d3;
221 if (d3 != 0) {
222 q = (d3 * 0xcd) >> 11;
223 d3 = d3 - 10*q;
224 *buf++ = d3 + '0'; /* next digit */
225 if (q != 0)
226 *buf++ = q + '0'; /* most sign. digit */
227 }
228 }
229 }
230 return buf;
231}
232/* Same with if's removed. Always emits five digits */
233static char* put_dec_full(char *buf, unsigned q)
234{
235 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
236 /* but anyway, gcc produces better code with full-sized ints */
237 unsigned d3, d2, d1, d0;
238 d1 = (q>>4) & 0xf;
239 d2 = (q>>8) & 0xf;
240 d3 = (q>>12);
241
Wolfgang Denke366f152009-04-05 00:27:57 +0200242 /*
243 * Possible ways to approx. divide by 10
244 * gcc -O2 replaces multiply with shifts and adds
245 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
246 * (x * 0x67) >> 10: 1100111
247 * (x * 0x34) >> 9: 110100 - same
248 * (x * 0x1a) >> 8: 11010 - same
249 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
250 */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500251
252 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
253 q = (d0 * 0xcd) >> 11;
254 d0 = d0 - 10*q;
255 *buf++ = d0 + '0';
256 d1 = q + 9*d3 + 5*d2 + d1;
257 q = (d1 * 0xcd) >> 11;
258 d1 = d1 - 10*q;
259 *buf++ = d1 + '0';
260
261 d2 = q + 2*d2;
262 q = (d2 * 0xd) >> 7;
263 d2 = d2 - 10*q;
264 *buf++ = d2 + '0';
265
266 d3 = q + 4*d3;
267 q = (d3 * 0xcd) >> 11; /* - shorter code */
268 /* q = (d3 * 0x67) >> 10; - would also work */
269 d3 = d3 - 10*q;
270 *buf++ = d3 + '0';
271 *buf++ = q + '0';
272 return buf;
273}
274/* No inlining helps gcc to use registers better */
275static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
276{
277 while (1) {
278 unsigned rem;
279 if (num < 100000)
280 return put_dec_trunc(buf, num);
281 rem = do_div(num, 100000);
282 buf = put_dec_full(buf, rem);
283 }
284}
285
wdenk153d5112002-08-30 11:07:04 +0000286#define ZEROPAD 1 /* pad with zero */
287#define SIGN 2 /* unsigned/signed long */
288#define PLUS 4 /* show plus */
289#define SPACE 8 /* space if plus */
290#define LEFT 16 /* left justified */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500291#define SMALL 32 /* Must be 32 == 0x20 */
292#define SPECIAL 64 /* 0x */
wdenk153d5112002-08-30 11:07:04 +0000293
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500294static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
wdenk153d5112002-08-30 11:07:04 +0000295{
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500296 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
297 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
298
299 char tmp[66];
300 char sign;
301 char locase;
302 int need_pfx = ((type & SPECIAL) && base != 10);
wdenk153d5112002-08-30 11:07:04 +0000303 int i;
304
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500305 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
306 * produces same digits or (maybe lowercased) letters */
307 locase = (type & SMALL);
wdenk153d5112002-08-30 11:07:04 +0000308 if (type & LEFT)
309 type &= ~ZEROPAD;
wdenk153d5112002-08-30 11:07:04 +0000310 sign = 0;
311 if (type & SIGN) {
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500312 if ((signed NUM_TYPE) num < 0) {
wdenk153d5112002-08-30 11:07:04 +0000313 sign = '-';
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500314 num = - (signed NUM_TYPE) num;
wdenk153d5112002-08-30 11:07:04 +0000315 size--;
316 } else if (type & PLUS) {
317 sign = '+';
318 size--;
319 } else if (type & SPACE) {
320 sign = ' ';
321 size--;
322 }
323 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500324 if (need_pfx) {
325 size--;
wdenk153d5112002-08-30 11:07:04 +0000326 if (base == 16)
wdenk153d5112002-08-30 11:07:04 +0000327 size--;
328 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500329
330 /* generate full string in tmp[], in reverse order */
wdenk153d5112002-08-30 11:07:04 +0000331 i = 0;
332 if (num == 0)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500333 tmp[i++] = '0';
334 /* Generic code, for any base:
335 else do {
336 tmp[i++] = (digits[do_div(num,base)] | locase);
337 } while (num != 0);
338 */
339 else if (base != 10) { /* 8 or 16 */
340 int mask = base - 1;
341 int shift = 3;
342 if (base == 16) shift = 4;
343 do {
344 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
345 num >>= shift;
346 } while (num);
347 } else { /* base 10 */
348 i = put_dec(tmp, num) - tmp;
349 }
350
351 /* printing 100 using %2d gives "100", not "00" */
wdenk153d5112002-08-30 11:07:04 +0000352 if (i > precision)
353 precision = i;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500354 /* leading space padding */
wdenk153d5112002-08-30 11:07:04 +0000355 size -= precision;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500356 if (!(type & (ZEROPAD+LEFT)))
357 while(--size >= 0)
358 *buf++ = ' ';
359 /* sign */
wdenk153d5112002-08-30 11:07:04 +0000360 if (sign)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500361 *buf++ = sign;
362 /* "0x" / "0" prefix */
363 if (need_pfx) {
364 *buf++ = '0';
365 if (base == 16)
366 *buf++ = ('X' | locase);
wdenk153d5112002-08-30 11:07:04 +0000367 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500368 /* zero or space padding */
369 if (!(type & LEFT)) {
370 char c = (type & ZEROPAD) ? '0' : ' ';
371 while (--size >= 0)
372 *buf++ = c;
373 }
374 /* hmm even more zero padding? */
375 while (i <= --precision)
376 *buf++ = '0';
377 /* actual digits of result */
378 while (--i >= 0)
379 *buf++ = tmp[i];
380 /* trailing space padding */
381 while (--size >= 0)
382 *buf++ = ' ';
383 return buf;
wdenk153d5112002-08-30 11:07:04 +0000384}
385
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500386static char *string(char *buf, char *s, int field_width, int precision, int flags)
387{
388 int len, i;
wdenk153d5112002-08-30 11:07:04 +0000389
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500390 if (s == 0)
391 s = "<NULL>";
392
393 len = strnlen(s, precision);
394
395 if (!(flags & LEFT))
396 while (len < field_width--)
397 *buf++ = ' ';
398 for (i = 0; i < len; ++i)
399 *buf++ = *s++;
400 while (len < field_width--)
401 *buf++ = ' ';
402 return buf;
403}
404
405#ifdef CONFIG_CMD_NET
406static char *mac_address_string(char *buf, u8 *addr, int field_width,
407 int precision, int flags)
wdenk153d5112002-08-30 11:07:04 +0000408{
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500409 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
410 char *p = mac_addr;
411 int i;
412
413 for (i = 0; i < 6; i++) {
414 p = pack_hex_byte(p, addr[i]);
415 if (!(flags & SPECIAL) && i != 5)
416 *p++ = ':';
417 }
418 *p = '\0';
419
420 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
421}
422
423static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
424 int precision, int flags)
425{
426 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
427 char *p = ip6_addr;
428 int i;
429
430 for (i = 0; i < 8; i++) {
431 p = pack_hex_byte(p, addr[2 * i]);
432 p = pack_hex_byte(p, addr[2 * i + 1]);
433 if (!(flags & SPECIAL) && i != 7)
434 *p++ = ':';
435 }
436 *p = '\0';
437
438 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
439}
440
441static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
442 int precision, int flags)
443{
444 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
445 char temp[3]; /* hold each IP quad in reverse order */
446 char *p = ip4_addr;
447 int i, digits;
448
449 for (i = 0; i < 4; i++) {
450 digits = put_dec_trunc(temp, addr[i]) - temp;
451 /* reverse the digits in the quad */
452 while (digits--)
453 *p++ = temp[digits];
454 if (i != 3)
455 *p++ = '.';
456 }
457 *p = '\0';
458
459 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
460}
461#endif
462
463/*
464 * Show a '%p' thing. A kernel extension is that the '%p' is followed
465 * by an extra set of alphanumeric characters that are extended format
466 * specifiers.
467 *
468 * Right now we handle:
469 *
470 * - 'M' For a 6-byte MAC address, it prints the address in the
471 * usual colon-separated hex notation
472 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
473 * decimal for v4 and colon separated network-order 16 bit hex for v6)
474 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
475 * currently the same
476 *
477 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
478 * function pointers are really function descriptors, which contain a
479 * pointer to the real address.
480 */
481static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
482{
483 if (!ptr)
484 return string(buf, "(null)", field_width, precision, flags);
485
486#ifdef CONFIG_CMD_NET
487 switch (*fmt) {
488 case 'm':
489 flags |= SPECIAL;
490 /* Fallthrough */
491 case 'M':
492 return mac_address_string(buf, ptr, field_width, precision, flags);
493 case 'i':
494 flags |= SPECIAL;
495 /* Fallthrough */
496 case 'I':
497 if (fmt[1] == '6')
498 return ip6_addr_string(buf, ptr, field_width, precision, flags);
499 if (fmt[1] == '4')
500 return ip4_addr_string(buf, ptr, field_width, precision, flags);
501 flags &= ~SPECIAL;
502 break;
503 }
wdenkf602aa02004-03-13 23:29:43 +0000504#endif
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500505 flags |= SMALL;
506 if (field_width == -1) {
507 field_width = 2*sizeof(void *);
508 flags |= ZEROPAD;
509 }
510 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
511}
512
513/**
514 * vsprintf - Format a string and place it in a buffer
515 * @buf: The buffer to place the result into
516 * @fmt: The format string to use
517 * @args: Arguments for the format string
518 *
519 * This function follows C99 vsprintf, but has some extensions:
520 * %pS output the name of a text symbol
521 * %pF output the name of a function pointer
522 * %pR output the address range in a struct resource
523 *
524 * The function returns the number of characters written
525 * into @buf.
526 *
527 * Call this function if you are already dealing with a va_list.
528 * You probably want sprintf() instead.
529 */
530int vsprintf(char *buf, const char *fmt, va_list args)
531{
532 unsigned NUM_TYPE num;
533 int base;
534 char *str;
wdenk153d5112002-08-30 11:07:04 +0000535
536 int flags; /* flags to number() */
537
538 int field_width; /* width of output field */
539 int precision; /* min. # of digits for integers; max
540 number of chars for from string */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500541 int qualifier; /* 'h', 'l', or 'L' for integer fields */
542 /* 'z' support added 23/7/1999 S.H. */
543 /* 'z' changed to 'Z' --davidm 1/25/99 */
544 /* 't' added for ptrdiff_t */
545
546 str = buf;
wdenk153d5112002-08-30 11:07:04 +0000547
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500548 for (; *fmt ; ++fmt) {
wdenk153d5112002-08-30 11:07:04 +0000549 if (*fmt != '%') {
550 *str++ = *fmt;
551 continue;
552 }
553
554 /* process flags */
555 flags = 0;
556 repeat:
557 ++fmt; /* this also skips first '%' */
558 switch (*fmt) {
559 case '-': flags |= LEFT; goto repeat;
560 case '+': flags |= PLUS; goto repeat;
561 case ' ': flags |= SPACE; goto repeat;
562 case '#': flags |= SPECIAL; goto repeat;
563 case '0': flags |= ZEROPAD; goto repeat;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500564 }
wdenk153d5112002-08-30 11:07:04 +0000565
566 /* get field width */
567 field_width = -1;
568 if (is_digit(*fmt))
569 field_width = skip_atoi(&fmt);
570 else if (*fmt == '*') {
571 ++fmt;
572 /* it's the next argument */
573 field_width = va_arg(args, int);
574 if (field_width < 0) {
575 field_width = -field_width;
576 flags |= LEFT;
577 }
578 }
579
580 /* get the precision */
581 precision = -1;
582 if (*fmt == '.') {
583 ++fmt;
584 if (is_digit(*fmt))
585 precision = skip_atoi(&fmt);
586 else if (*fmt == '*') {
587 ++fmt;
588 /* it's the next argument */
589 precision = va_arg(args, int);
590 }
591 if (precision < 0)
592 precision = 0;
593 }
594
595 /* get the conversion qualifier */
596 qualifier = -1;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200597 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500598 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
wdenk153d5112002-08-30 11:07:04 +0000599 qualifier = *fmt;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500600 ++fmt;
601 if (qualifier == 'l' && *fmt == 'l') {
602 qualifier = 'L';
James Yang7fc40152008-01-10 16:02:07 -0600603 ++fmt;
604 }
wdenk153d5112002-08-30 11:07:04 +0000605 }
606
607 /* default base */
608 base = 10;
609
610 switch (*fmt) {
611 case 'c':
612 if (!(flags & LEFT))
613 while (--field_width > 0)
614 *str++ = ' ';
615 *str++ = (unsigned char) va_arg(args, int);
616 while (--field_width > 0)
617 *str++ = ' ';
618 continue;
619
620 case 's':
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500621 str = string(str, va_arg(args, char *), field_width, precision, flags);
wdenk153d5112002-08-30 11:07:04 +0000622 continue;
623
624 case 'p':
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500625 str = pointer(fmt+1, str,
626 va_arg(args, void *),
627 field_width, precision, flags);
628 /* Skip all alphanumeric pointer suffixes */
629 while (isalnum(fmt[1]))
630 fmt++;
wdenk153d5112002-08-30 11:07:04 +0000631 continue;
632
wdenk153d5112002-08-30 11:07:04 +0000633 case 'n':
634 if (qualifier == 'l') {
635 long * ip = va_arg(args, long *);
636 *ip = (str - buf);
637 } else {
638 int * ip = va_arg(args, int *);
639 *ip = (str - buf);
640 }
641 continue;
642
643 case '%':
644 *str++ = '%';
645 continue;
646
647 /* integer number formats - set up the flags and "break" */
648 case 'o':
649 base = 8;
650 break;
651
wdenk153d5112002-08-30 11:07:04 +0000652 case 'x':
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500653 flags |= SMALL;
654 case 'X':
wdenk153d5112002-08-30 11:07:04 +0000655 base = 16;
656 break;
657
658 case 'd':
659 case 'i':
660 flags |= SIGN;
661 case 'u':
662 break;
663
664 default:
665 *str++ = '%';
666 if (*fmt)
667 *str++ = *fmt;
668 else
669 --fmt;
670 continue;
671 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500672 if (qualifier == 'L') /* "quad" for 64 bit variables */
wdenkf602aa02004-03-13 23:29:43 +0000673 num = va_arg(args, unsigned long long);
Heiko Schocher0f602e12009-12-03 11:21:21 +0100674 else if (qualifier == 'l') {
wdenk153d5112002-08-30 11:07:04 +0000675 num = va_arg(args, unsigned long);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500676 if (flags & SIGN)
677 num = (signed long) num;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200678 } else if (qualifier == 'Z' || qualifier == 'z') {
679 num = va_arg(args, size_t);
680 } else if (qualifier == 't') {
681 num = va_arg(args, ptrdiff_t);
682 } else if (qualifier == 'h') {
wdenk153d5112002-08-30 11:07:04 +0000683 num = (unsigned short) va_arg(args, int);
684 if (flags & SIGN)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500685 num = (signed short) num;
686 } else {
wdenk153d5112002-08-30 11:07:04 +0000687 num = va_arg(args, unsigned int);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500688 if (flags & SIGN)
689 num = (signed int) num;
690 }
wdenk153d5112002-08-30 11:07:04 +0000691 str = number(str, num, base, field_width, precision, flags);
692 }
693 *str = '\0';
694 return str-buf;
695}
696
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500697/**
698 * sprintf - Format a string and place it in a buffer
699 * @buf: The buffer to place the result into
700 * @fmt: The format string to use
701 * @...: Arguments for the format string
702 *
703 * The function returns the number of characters written
704 * into @buf.
705 *
706 * See the vsprintf() documentation for format string extensions over C99.
707 */
wdenk153d5112002-08-30 11:07:04 +0000708int sprintf(char * buf, const char *fmt, ...)
709{
710 va_list args;
711 int i;
712
713 va_start(args, fmt);
714 i=vsprintf(buf,fmt,args);
715 va_end(args);
716 return i;
717}
718
719void panic(const char *fmt, ...)
720{
721 va_list args;
722 va_start(args, fmt);
wdenkb00ec162003-06-19 23:40:20 +0000723 vprintf(fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000724 putc('\n');
725 va_end(args);
726#if defined (CONFIG_PANIC_HANG)
727 hang();
728#else
729 udelay (100000); /* allow messages to go out */
730 do_reset (NULL, 0, 0, NULL);
731#endif
Heiko Schocher8cce81f2011-07-26 20:55:57 +0000732 while (1)
733 ;
wdenk153d5112002-08-30 11:07:04 +0000734}
Simon Glass02aa9e62011-06-29 09:49:34 +0000735
736void __assert_fail(const char *assertion, const char *file, unsigned line,
737 const char *function)
738{
739 /* This will not return */
740 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
741 assertion);
742}
Simon Glass931948a2011-10-21 18:51:33 +0000743
744char *simple_itoa(ulong i)
745{
746 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
747 static char local[22];
748 char *p = &local[21];
749
750 *p-- = '\0';
751 do {
752 *p-- = '0' + i % 10;
753 i /= 10;
754 } while (i > 0);
755 return p + 1;
756}