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 /* we use this so that we can do without the ctype library */
59 #define is_digit(c) ((c) >= '0' && (c) <= '9')
61 static int skip_atoi(const char **s)
66 i = i*10 + *((*s)++) - '0';
70 #define ZEROPAD 1 /* pad with zero */
71 #define SIGN 2 /* unsigned/signed long */
72 #define PLUS 4 /* show plus */
73 #define SPACE 8 /* space if plus */
74 #define LEFT 16 /* left justified */
75 #define SPECIAL 32 /* 0x */
76 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
78 #define do_div(n,base) ({ \
80 __res = ((unsigned long) n) % (unsigned) base; \
81 n = ((unsigned long) n) / (unsigned) base; \
84 static char * number(char * str, long num, int base, int size, int precision
88 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
92 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
95 if (base < 2 || base > 36)
97 c = (type & ZEROPAD) ? '0' : ' ';
104 } else if (type & PLUS) {
107 } else if (type & SPACE) {
112 if (type & SPECIAL) {
121 else while (num != 0)
122 tmp[i++] = digits[do_div(num,base)];
126 if (!(type&(ZEROPAD+LEFT)))
131 if (type & SPECIAL) {
142 while (i < precision--)
151 /* Forward decl. needed for IP address printing stuff... */
152 int sprintf(char * buf, const char *fmt, ...);
154 int vsprintf(char *buf, const char *fmt, va_list args)
162 int flags; /* flags to number() */
164 int field_width; /* width of output field */
165 int precision; /* min. # of digits for integers; max
166 number of chars for from string */
167 int qualifier; /* 'h', 'l', or 'L' for integer fields */
169 for (str=buf ; *fmt ; ++fmt) {
178 ++fmt; /* this also skips first '%' */
180 case '-': flags |= LEFT; goto repeat;
181 case '+': flags |= PLUS; goto repeat;
182 case ' ': flags |= SPACE; goto repeat;
183 case '#': flags |= SPECIAL; goto repeat;
184 case '0': flags |= ZEROPAD; goto repeat;
187 /* get field width */
190 field_width = skip_atoi(&fmt);
191 else if (*fmt == '*') {
193 /* it's the next argument */
194 field_width = va_arg(args, int);
195 if (field_width < 0) {
196 field_width = -field_width;
201 /* get the precision */
206 precision = skip_atoi(&fmt);
207 else if (*fmt == '*') {
209 /* it's the next argument */
210 precision = va_arg(args, int);
216 /* get the conversion qualifier */
218 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
229 while (--field_width > 0)
231 *str++ = (unsigned char) va_arg(args, int);
232 while (--field_width > 0)
237 s = va_arg(args, char *);
241 len = strnlen(s, precision);
244 while (len < field_width--)
246 for (i = 0; i < len; ++i)
248 while (len < field_width--)
253 if (field_width == -1) {
254 field_width = 2*sizeof(void *);
258 (unsigned long) va_arg(args, void *), 16,
259 field_width, precision, flags);
264 if (qualifier == 'l') {
265 long * ip = va_arg(args, long *);
268 int * ip = va_arg(args, int *);
277 /* integer number formats - set up the flags and "break" */
302 if (qualifier == 'l')
303 num = va_arg(args, unsigned long);
304 else if (qualifier == 'h') {
305 num = (unsigned short) va_arg(args, int);
308 } else if (flags & SIGN)
309 num = va_arg(args, int);
311 num = va_arg(args, unsigned int);
312 str = number(str, num, base, field_width, precision, flags);
318 int sprintf(char * buf, const char *fmt, ...)
324 i=vsprintf(buf,fmt,args);
329 void panic(const char *fmt, ...)
336 #if defined (CONFIG_PANIC_HANG)
339 udelay (100000); /* allow messages to go out */
340 do_reset (NULL, 0, 0, NULL);