]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib_generic/vsprintf.c
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 #ifdef CFG_64BIT_STRTOUL
59 unsigned long long simple_strtoull (const char *cp
, char **endp
, unsigned int base
)
61 unsigned long long result
= 0, value
;
65 if ((*cp
== 'x') && isxdigit (cp
[1])) {
76 while (isxdigit (*cp
) && (value
= isdigit (*cp
)
78 : (islower (*cp
) ? toupper (*cp
) : *cp
) - 'A' + 10) < base
) {
79 result
= result
* base
+ value
;
86 #endif /* CFG_64BIT_STRTOUL */
88 /* we use this so that we can do without the ctype library */
89 #define is_digit(c) ((c) >= '0' && (c) <= '9')
91 static int skip_atoi(const char **s
)
96 i
= i
*10 + *((*s
)++) - '0';
100 #define ZEROPAD 1 /* pad with zero */
101 #define SIGN 2 /* unsigned/signed long */
102 #define PLUS 4 /* show plus */
103 #define SPACE 8 /* space if plus */
104 #define LEFT 16 /* left justified */
105 #define SPECIAL 32 /* 0x */
106 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
108 #ifdef CFG_64BIT_VSPRINTF
109 #define do_div(n,base) ({ \
110 unsigned int __res; \
111 __res = ((unsigned long long) n) % base; \
112 n = ((unsigned long long) n) / base; \
116 #define do_div(n,base) ({ \
118 __res = ((unsigned long) n) % base; \
119 n = ((unsigned long) n) / base; \
124 #ifdef CFG_64BIT_VSPRINTF
125 static char * number(char * str
, long long num
, unsigned int base
, int size
, int precision
,int type
)
127 static char * number(char * str
, long num
, unsigned int base
, int size
, int precision
,int type
)
131 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
135 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
138 if (base
< 2 || base
> 36)
140 c
= (type
& ZEROPAD
) ? '0' : ' ';
147 } else if (type
& PLUS
) {
150 } else if (type
& SPACE
) {
155 if (type
& SPECIAL
) {
164 else while (num
!= 0)
165 tmp
[i
++] = digits
[do_div(num
,base
)];
169 if (!(type
&(ZEROPAD
+LEFT
)))
174 if (type
& SPECIAL
) {
185 while (i
< precision
--)
194 /* Forward decl. needed for IP address printing stuff... */
195 int sprintf(char * buf
, const char *fmt
, ...);
197 int vsprintf(char *buf
, const char *fmt
, va_list args
)
200 #ifdef CFG_64BIT_VSPRINTF
201 unsigned long long num
;
209 int flags
; /* flags to number() */
211 int field_width
; /* width of output field */
212 int precision
; /* min. # of digits for integers; max
213 number of chars for from string */
214 int qualifier
; /* 'h', 'l', or 'q' for integer fields */
216 for (str
=buf
; *fmt
; ++fmt
) {
225 ++fmt
; /* this also skips first '%' */
227 case '-': flags
|= LEFT
; goto repeat
;
228 case '+': flags
|= PLUS
; goto repeat
;
229 case ' ': flags
|= SPACE
; goto repeat
;
230 case '#': flags
|= SPECIAL
; goto repeat
;
231 case '0': flags
|= ZEROPAD
; goto repeat
;
234 /* get field width */
237 field_width
= skip_atoi(&fmt
);
238 else if (*fmt
== '*') {
240 /* it's the next argument */
241 field_width
= va_arg(args
, int);
242 if (field_width
< 0) {
243 field_width
= -field_width
;
248 /* get the precision */
253 precision
= skip_atoi(&fmt
);
254 else if (*fmt
== '*') {
256 /* it's the next argument */
257 precision
= va_arg(args
, int);
263 /* get the conversion qualifier */
265 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'q') {
267 if (qualifier
== 'l' && *(fmt
+1) == 'l') {
280 while (--field_width
> 0)
282 *str
++ = (unsigned char) va_arg(args
, int);
283 while (--field_width
> 0)
288 s
= va_arg(args
, char *);
292 len
= strnlen(s
, precision
);
295 while (len
< field_width
--)
297 for (i
= 0; i
< len
; ++i
)
299 while (len
< field_width
--)
304 if (field_width
== -1) {
305 field_width
= 2*sizeof(void *);
309 (unsigned long) va_arg(args
, void *), 16,
310 field_width
, precision
, flags
);
315 if (qualifier
== 'l') {
316 long * ip
= va_arg(args
, long *);
319 int * ip
= va_arg(args
, int *);
328 /* integer number formats - set up the flags and "break" */
353 #ifdef CFG_64BIT_VSPRINTF
354 if (qualifier
== 'q') /* "quad" for 64 bit variables */
355 num
= va_arg(args
, unsigned long long);
358 if (qualifier
== 'l')
359 num
= va_arg(args
, unsigned long);
360 else if (qualifier
== 'h') {
361 num
= (unsigned short) va_arg(args
, int);
364 } else if (flags
& SIGN
)
365 num
= va_arg(args
, int);
367 num
= va_arg(args
, unsigned int);
368 str
= number(str
, num
, base
, field_width
, precision
, flags
);
374 int sprintf(char * buf
, const char *fmt
, ...)
380 i
=vsprintf(buf
,fmt
,args
);
385 void panic(const char *fmt
, ...)
392 #if defined (CONFIG_PANIC_HANG)
395 udelay (100000); /* allow messages to go out */
396 do_reset (NULL
, 0, 0, NULL
);