4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
18 #if !defined (CONFIG_PANIC_HANG)
21 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
24 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
26 unsigned long result = 0,value;
30 if ((*cp == 'x') && isxdigit(cp[1])) {
41 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
42 ? toupper(*cp) : *cp)-'A'+10) < base) {
43 result = result*base + value;
51 long simple_strtol(const char *cp,char **endp,unsigned int base)
54 return -simple_strtoul(cp+1,endp,base);
55 return simple_strtoul(cp,endp,base);
58 int ustrtoul(const char *cp, char **endp, unsigned int base)
60 unsigned long result = simple_strtoul(cp, endp, base);
71 if ((*endp)[1] == 'i') {
72 if ((*endp)[2] == 'B')
81 #ifdef CONFIG_SYS_64BIT_STRTOUL
82 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
84 unsigned long long result = 0, value;
88 if ((*cp == 'x') && isxdigit (cp[1])) {
99 while (isxdigit (*cp) && (value = isdigit (*cp)
101 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
102 result = result * base + value;
109 #endif /* CONFIG_SYS_64BIT_STRTOUL */
111 /* we use this so that we can do without the ctype library */
112 #define is_digit(c) ((c) >= '0' && (c) <= '9')
114 static int skip_atoi(const char **s)
118 while (is_digit(**s))
119 i = i*10 + *((*s)++) - '0';
123 #define ZEROPAD 1 /* pad with zero */
124 #define SIGN 2 /* unsigned/signed long */
125 #define PLUS 4 /* show plus */
126 #define SPACE 8 /* space if plus */
127 #define LEFT 16 /* left justified */
128 #define SPECIAL 32 /* 0x */
129 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
131 #ifdef CONFIG_SYS_64BIT_VSPRINTF
132 #define do_div(n,base) ({ \
133 unsigned int __res; \
134 __res = ((unsigned long long) n) % base; \
135 n = ((unsigned long long) n) / base; \
139 #define do_div(n,base) ({ \
141 __res = ((unsigned long) n) % base; \
142 n = ((unsigned long) n) / base; \
147 #ifdef CONFIG_SYS_64BIT_VSPRINTF
148 static char * number(char * str, long long num, unsigned int base, int size, int precision ,int type)
150 static char * number(char * str, long num, unsigned int base, int size, int precision ,int type)
154 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
158 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
161 if (base < 2 || base > 36)
163 c = (type & ZEROPAD) ? '0' : ' ';
170 } else if (type & PLUS) {
173 } else if (type & SPACE) {
178 if (type & SPECIAL) {
187 else while (num != 0)
188 tmp[i++] = digits[do_div(num,base)];
192 if (!(type&(ZEROPAD+LEFT)))
197 if (type & SPECIAL) {
208 while (i < precision--)
217 /* Forward decl. needed for IP address printing stuff... */
218 int sprintf(char * buf, const char *fmt, ...);
220 int vsprintf(char *buf, const char *fmt, va_list args)
223 #ifdef CONFIG_SYS_64BIT_VSPRINTF
224 unsigned long long num;
232 int flags; /* flags to number() */
234 int field_width; /* width of output field */
235 int precision; /* min. # of digits for integers; max
236 number of chars for from string */
237 int qualifier; /* 'h', 'l', or 'q' for integer fields */
239 for (str=buf ; *fmt ; ++fmt) {
248 ++fmt; /* this also skips first '%' */
250 case '-': flags |= LEFT; goto repeat;
251 case '+': flags |= PLUS; goto repeat;
252 case ' ': flags |= SPACE; goto repeat;
253 case '#': flags |= SPECIAL; goto repeat;
254 case '0': flags |= ZEROPAD; goto repeat;
257 /* get field width */
260 field_width = skip_atoi(&fmt);
261 else if (*fmt == '*') {
263 /* it's the next argument */
264 field_width = va_arg(args, int);
265 if (field_width < 0) {
266 field_width = -field_width;
271 /* get the precision */
276 precision = skip_atoi(&fmt);
277 else if (*fmt == '*') {
279 /* it's the next argument */
280 precision = va_arg(args, int);
286 /* get the conversion qualifier */
288 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
289 *fmt == 'Z' || *fmt == 'z' || *fmt == 't' ||
292 if (qualifier == 'l' && *(fmt+1) == 'l') {
305 while (--field_width > 0)
307 *str++ = (unsigned char) va_arg(args, int);
308 while (--field_width > 0)
313 s = va_arg(args, char *);
317 len = strnlen(s, precision);
320 while (len < field_width--)
322 for (i = 0; i < len; ++i)
324 while (len < field_width--)
329 if (field_width == -1) {
330 field_width = 2*sizeof(void *);
334 (unsigned long) va_arg(args, void *), 16,
335 field_width, precision, flags);
340 if (qualifier == 'l') {
341 long * ip = va_arg(args, long *);
344 int * ip = va_arg(args, int *);
353 /* integer number formats - set up the flags and "break" */
378 #ifdef CONFIG_SYS_64BIT_VSPRINTF
379 if (qualifier == 'q') /* "quad" for 64 bit variables */
380 num = va_arg(args, unsigned long long);
383 if (qualifier == 'l') {
384 num = va_arg(args, unsigned long);
385 } else if (qualifier == 'Z' || qualifier == 'z') {
386 num = va_arg(args, size_t);
387 } else if (qualifier == 't') {
388 num = va_arg(args, ptrdiff_t);
389 } else if (qualifier == 'h') {
390 num = (unsigned short) va_arg(args, int);
393 } else if (flags & SIGN)
394 num = va_arg(args, int);
396 num = va_arg(args, unsigned int);
397 str = number(str, num, base, field_width, precision, flags);
403 int sprintf(char * buf, const char *fmt, ...)
409 i=vsprintf(buf,fmt,args);
414 void panic(const char *fmt, ...)
421 #if defined (CONFIG_PANIC_HANG)
424 udelay (100000); /* allow messages to go out */
425 do_reset (NULL, 0, 0, NULL);