]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib_generic/vsprintf.c
sh: Fix cannot work rtl8139 on r2dplus
[karo-tx-uboot.git] / lib_generic / vsprintf.c
1 /*
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>
20 /*cmd_boot.c*/
21 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
22 #endif
23
24 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
25 {
26         unsigned long result = 0,value;
27
28         if (*cp == '0') {
29                 cp++;
30                 if ((*cp == 'x') && isxdigit(cp[1])) {
31                         base = 16;
32                         cp++;
33                 }
34                 if (!base) {
35                         base = 8;
36                 }
37         }
38         if (!base) {
39                 base = 10;
40         }
41         while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
42             ? toupper(*cp) : *cp)-'A'+10) < base) {
43                 result = result*base + value;
44                 cp++;
45         }
46         if (endp)
47                 *endp = (char *)cp;
48         return result;
49 }
50
51 long simple_strtol(const char *cp,char **endp,unsigned int base)
52 {
53         if(*cp=='-')
54                 return -simple_strtoul(cp+1,endp,base);
55         return simple_strtoul(cp,endp,base);
56 }
57
58 int ustrtoul(const char *cp, char **endp, unsigned int base)
59 {
60         unsigned long result = simple_strtoul(cp, endp, base);
61         switch (**endp) {
62         case 'G' :
63                 result *= 1024;
64                 /* fall through */
65         case 'M':
66                 result *= 1024;
67                 /* fall through */
68         case 'K':
69         case 'k':
70                 result *= 1024;
71                 if ((*endp)[1] == 'i') {
72                         if ((*endp)[2] == 'B')
73                                 (*endp) += 3;
74                         else
75                                 (*endp) += 2;
76                 }
77         }
78         return result;
79 }
80
81 #ifdef CONFIG_SYS_64BIT_STRTOUL
82 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
83 {
84         unsigned long long result = 0, value;
85
86         if (*cp == '0') {
87                 cp++;
88                 if ((*cp == 'x') && isxdigit (cp[1])) {
89                         base = 16;
90                         cp++;
91                 }
92                 if (!base) {
93                         base = 8;
94                 }
95         }
96         if (!base) {
97                 base = 10;
98         }
99         while (isxdigit (*cp) && (value = isdigit (*cp)
100                                 ? *cp - '0'
101                                 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
102                 result = result * base + value;
103                 cp++;
104         }
105         if (endp)
106                 *endp = (char *) cp;
107         return result;
108 }
109 #endif /* CONFIG_SYS_64BIT_STRTOUL */
110
111 /* we use this so that we can do without the ctype library */
112 #define is_digit(c)     ((c) >= '0' && (c) <= '9')
113
114 static int skip_atoi(const char **s)
115 {
116         int i=0;
117
118         while (is_digit(**s))
119                 i = i*10 + *((*s)++) - '0';
120         return i;
121 }
122
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' */
130
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; \
136         __res; \
137 })
138 #else
139 #define do_div(n,base) ({ \
140         int __res; \
141         __res = ((unsigned long) n) % base; \
142         n = ((unsigned long) n) / base; \
143         __res; \
144 })
145 #endif
146
147 #ifdef CONFIG_SYS_64BIT_VSPRINTF
148 static char * number(char * str, long long num, unsigned int base, int size, int precision ,int type)
149 #else
150 static char * number(char * str, long num, unsigned int base, int size, int precision ,int type)
151 #endif
152 {
153         char c,sign,tmp[66];
154         const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
155         int i;
156
157         if (type & LARGE)
158                 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
159         if (type & LEFT)
160                 type &= ~ZEROPAD;
161         if (base < 2 || base > 36)
162                 return 0;
163         c = (type & ZEROPAD) ? '0' : ' ';
164         sign = 0;
165         if (type & SIGN) {
166                 if (num < 0) {
167                         sign = '-';
168                         num = -num;
169                         size--;
170                 } else if (type & PLUS) {
171                         sign = '+';
172                         size--;
173                 } else if (type & SPACE) {
174                         sign = ' ';
175                         size--;
176                 }
177         }
178         if (type & SPECIAL) {
179                 if (base == 16)
180                         size -= 2;
181                 else if (base == 8)
182                         size--;
183         }
184         i = 0;
185         if (num == 0)
186                 tmp[i++]='0';
187         else while (num != 0)
188                 tmp[i++] = digits[do_div(num,base)];
189         if (i > precision)
190                 precision = i;
191         size -= precision;
192         if (!(type&(ZEROPAD+LEFT)))
193                 while(size-->0)
194                         *str++ = ' ';
195         if (sign)
196                 *str++ = sign;
197         if (type & SPECIAL) {
198                 if (base==8)
199                         *str++ = '0';
200                 else if (base==16) {
201                         *str++ = '0';
202                         *str++ = digits[33];
203                 }
204         }
205         if (!(type & LEFT))
206                 while (size-- > 0)
207                         *str++ = c;
208         while (i < precision--)
209                 *str++ = '0';
210         while (i-- > 0)
211                 *str++ = tmp[i];
212         while (size-- > 0)
213                 *str++ = ' ';
214         return str;
215 }
216
217 /* Forward decl. needed for IP address printing stuff... */
218 int sprintf(char * buf, const char *fmt, ...);
219
220 int vsprintf(char *buf, const char *fmt, va_list args)
221 {
222         int len;
223 #ifdef CONFIG_SYS_64BIT_VSPRINTF
224         unsigned long long num;
225 #else
226         unsigned long num;
227 #endif
228         int i, base;
229         char * str;
230         const char *s;
231
232         int flags;              /* flags to number() */
233
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 */
238
239         for (str=buf ; *fmt ; ++fmt) {
240                 if (*fmt != '%') {
241                         *str++ = *fmt;
242                         continue;
243                 }
244
245                 /* process flags */
246                 flags = 0;
247                 repeat:
248                         ++fmt;          /* this also skips first '%' */
249                         switch (*fmt) {
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;
255                                 }
256
257                 /* get field width */
258                 field_width = -1;
259                 if (is_digit(*fmt))
260                         field_width = skip_atoi(&fmt);
261                 else if (*fmt == '*') {
262                         ++fmt;
263                         /* it's the next argument */
264                         field_width = va_arg(args, int);
265                         if (field_width < 0) {
266                                 field_width = -field_width;
267                                 flags |= LEFT;
268                         }
269                 }
270
271                 /* get the precision */
272                 precision = -1;
273                 if (*fmt == '.') {
274                         ++fmt;
275                         if (is_digit(*fmt))
276                                 precision = skip_atoi(&fmt);
277                         else if (*fmt == '*') {
278                                 ++fmt;
279                                 /* it's the next argument */
280                                 precision = va_arg(args, int);
281                         }
282                         if (precision < 0)
283                                 precision = 0;
284                 }
285
286                 /* get the conversion qualifier */
287                 qualifier = -1;
288                 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
289                     *fmt == 'Z' || *fmt == 'z' || *fmt == 't' ||
290                     *fmt == 'q' ) {
291                         qualifier = *fmt;
292                         if (qualifier == 'l' && *(fmt+1) == 'l') {
293                                 qualifier = 'q';
294                                 ++fmt;
295                         }
296                         ++fmt;
297                 }
298
299                 /* default base */
300                 base = 10;
301
302                 switch (*fmt) {
303                 case 'c':
304                         if (!(flags & LEFT))
305                                 while (--field_width > 0)
306                                         *str++ = ' ';
307                         *str++ = (unsigned char) va_arg(args, int);
308                         while (--field_width > 0)
309                                 *str++ = ' ';
310                         continue;
311
312                 case 's':
313                         s = va_arg(args, char *);
314                         if (!s)
315                                 s = "<NULL>";
316
317                         len = strnlen(s, precision);
318
319                         if (!(flags & LEFT))
320                                 while (len < field_width--)
321                                         *str++ = ' ';
322                         for (i = 0; i < len; ++i)
323                                 *str++ = *s++;
324                         while (len < field_width--)
325                                 *str++ = ' ';
326                         continue;
327
328                 case 'p':
329                         if (field_width == -1) {
330                                 field_width = 2*sizeof(void *);
331                                 flags |= ZEROPAD;
332                         }
333                         str = number(str,
334                                 (unsigned long) va_arg(args, void *), 16,
335                                 field_width, precision, flags);
336                         continue;
337
338
339                 case 'n':
340                         if (qualifier == 'l') {
341                                 long * ip = va_arg(args, long *);
342                                 *ip = (str - buf);
343                         } else {
344                                 int * ip = va_arg(args, int *);
345                                 *ip = (str - buf);
346                         }
347                         continue;
348
349                 case '%':
350                         *str++ = '%';
351                         continue;
352
353                 /* integer number formats - set up the flags and "break" */
354                 case 'o':
355                         base = 8;
356                         break;
357
358                 case 'X':
359                         flags |= LARGE;
360                 case 'x':
361                         base = 16;
362                         break;
363
364                 case 'd':
365                 case 'i':
366                         flags |= SIGN;
367                 case 'u':
368                         break;
369
370                 default:
371                         *str++ = '%';
372                         if (*fmt)
373                                 *str++ = *fmt;
374                         else
375                                 --fmt;
376                         continue;
377                 }
378 #ifdef CONFIG_SYS_64BIT_VSPRINTF
379                 if (qualifier == 'q')  /* "quad" for 64 bit variables */
380                         num = va_arg(args, unsigned long long);
381                 else
382 #endif
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);
391                         if (flags & SIGN)
392                                 num = (short) num;
393                 } else if (flags & SIGN)
394                         num = va_arg(args, int);
395                 else
396                         num = va_arg(args, unsigned int);
397                 str = number(str, num, base, field_width, precision, flags);
398         }
399         *str = '\0';
400         return str-buf;
401 }
402
403 int sprintf(char * buf, const char *fmt, ...)
404 {
405         va_list args;
406         int i;
407
408         va_start(args, fmt);
409         i=vsprintf(buf,fmt,args);
410         va_end(args);
411         return i;
412 }
413
414 void panic(const char *fmt, ...)
415 {
416         va_list args;
417         va_start(args, fmt);
418         vprintf(fmt, args);
419         putc('\n');
420         va_end(args);
421 #if defined (CONFIG_PANIC_HANG)
422         hang();
423 #else
424         udelay (100000);        /* allow messages to go out */
425         do_reset (NULL, 0, 0, NULL);
426 #endif
427 }