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)
20 #include <cmd_boot.h> /* for do_reset() prototype */
23 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
25 unsigned long result = 0,value;
29 if ((*cp == 'x') && isxdigit(cp[1])) {
40 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
41 ? toupper(*cp) : *cp)-'A'+10) < base) {
42 result = result*base + value;
50 long simple_strtol(const char *cp,char **endp,unsigned int base)
53 return -simple_strtoul(cp+1,endp,base);
54 return simple_strtoul(cp,endp,base);
57 /* we use this so that we can do without the ctype library */
58 #define is_digit(c) ((c) >= '0' && (c) <= '9')
60 static int skip_atoi(const char **s)
65 i = i*10 + *((*s)++) - '0';
69 #define ZEROPAD 1 /* pad with zero */
70 #define SIGN 2 /* unsigned/signed long */
71 #define PLUS 4 /* show plus */
72 #define SPACE 8 /* space if plus */
73 #define LEFT 16 /* left justified */
74 #define SPECIAL 32 /* 0x */
75 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
77 #define do_div(n,base) ({ \
79 __res = ((unsigned long) n) % (unsigned) base; \
80 n = ((unsigned long) n) / (unsigned) base; \
83 static char * number(char * str, long num, int base, int size, int precision
87 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
91 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
94 if (base < 2 || base > 36)
96 c = (type & ZEROPAD) ? '0' : ' ';
103 } else if (type & PLUS) {
106 } else if (type & SPACE) {
111 if (type & SPECIAL) {
120 else while (num != 0)
121 tmp[i++] = digits[do_div(num,base)];
125 if (!(type&(ZEROPAD+LEFT)))
130 if (type & SPECIAL) {
141 while (i < precision--)
150 /* Forward decl. needed for IP address printing stuff... */
151 int sprintf(char * buf, const char *fmt, ...);
153 int vsprintf(char *buf, const char *fmt, va_list args)
161 int flags; /* flags to number() */
163 int field_width; /* width of output field */
164 int precision; /* min. # of digits for integers; max
165 number of chars for from string */
166 int qualifier; /* 'h', 'l', or 'L' for integer fields */
168 for (str=buf ; *fmt ; ++fmt) {
177 ++fmt; /* this also skips first '%' */
179 case '-': flags |= LEFT; goto repeat;
180 case '+': flags |= PLUS; goto repeat;
181 case ' ': flags |= SPACE; goto repeat;
182 case '#': flags |= SPECIAL; goto repeat;
183 case '0': flags |= ZEROPAD; goto repeat;
186 /* get field width */
189 field_width = skip_atoi(&fmt);
190 else if (*fmt == '*') {
192 /* it's the next argument */
193 field_width = va_arg(args, int);
194 if (field_width < 0) {
195 field_width = -field_width;
200 /* get the precision */
205 precision = skip_atoi(&fmt);
206 else if (*fmt == '*') {
208 /* it's the next argument */
209 precision = va_arg(args, int);
215 /* get the conversion qualifier */
217 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
228 while (--field_width > 0)
230 *str++ = (unsigned char) va_arg(args, int);
231 while (--field_width > 0)
236 s = va_arg(args, char *);
240 len = strnlen(s, precision);
243 while (len < field_width--)
245 for (i = 0; i < len; ++i)
247 while (len < field_width--)
252 if (field_width == -1) {
253 field_width = 2*sizeof(void *);
257 (unsigned long) va_arg(args, void *), 16,
258 field_width, precision, flags);
263 if (qualifier == 'l') {
264 long * ip = va_arg(args, long *);
267 int * ip = va_arg(args, int *);
276 /* integer number formats - set up the flags and "break" */
301 if (qualifier == 'l')
302 num = va_arg(args, unsigned long);
303 else if (qualifier == 'h') {
304 num = (unsigned short) va_arg(args, int);
307 } else if (flags & SIGN)
308 num = va_arg(args, int);
310 num = va_arg(args, unsigned int);
311 str = number(str, num, base, field_width, precision, flags);
317 int sprintf(char * buf, const char *fmt, ...)
323 i=vsprintf(buf,fmt,args);
328 void panic(const char *fmt, ...)
335 #if defined (CONFIG_PANIC_HANG)
338 udelay (100000); /* allow messages to go out */
339 do_reset (NULL, 0, 0, NULL);