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 #define do_div(n,base) ({ \
110 __res = ((unsigned long) n) % (unsigned) base; \
111 n = ((unsigned long) n) / (unsigned) base; \
115 #ifdef CFG_64BIT_VSPRINTF
116 static char * number(char * str
, long long num
, int base
, int size
, int precision
,int type
)
118 static char * number(char * str
, long num
, int base
, int size
, int precision
,int type
)
122 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
126 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
129 if (base
< 2 || base
> 36)
131 c
= (type
& ZEROPAD
) ? '0' : ' ';
138 } else if (type
& PLUS
) {
141 } else if (type
& SPACE
) {
146 if (type
& SPECIAL
) {
155 else while (num
!= 0)
156 tmp
[i
++] = digits
[do_div(num
,base
)];
160 if (!(type
&(ZEROPAD
+LEFT
)))
165 if (type
& SPECIAL
) {
176 while (i
< precision
--)
185 /* Forward decl. needed for IP address printing stuff... */
186 int sprintf(char * buf
, const char *fmt
, ...);
188 int vsprintf(char *buf
, const char *fmt
, va_list args
)
191 #ifdef CFG_64BIT_VSPRINTF
192 unsigned long long num
;
200 int flags
; /* flags to number() */
202 int field_width
; /* width of output field */
203 int precision
; /* min. # of digits for integers; max
204 number of chars for from string */
205 int qualifier
; /* 'h', 'l', or 'q' for integer fields */
207 for (str
=buf
; *fmt
; ++fmt
) {
216 ++fmt
; /* this also skips first '%' */
218 case '-': flags
|= LEFT
; goto repeat
;
219 case '+': flags
|= PLUS
; goto repeat
;
220 case ' ': flags
|= SPACE
; goto repeat
;
221 case '#': flags
|= SPECIAL
; goto repeat
;
222 case '0': flags
|= ZEROPAD
; goto repeat
;
225 /* get field width */
228 field_width
= skip_atoi(&fmt
);
229 else if (*fmt
== '*') {
231 /* it's the next argument */
232 field_width
= va_arg(args
, int);
233 if (field_width
< 0) {
234 field_width
= -field_width
;
239 /* get the precision */
244 precision
= skip_atoi(&fmt
);
245 else if (*fmt
== '*') {
247 /* it's the next argument */
248 precision
= va_arg(args
, int);
254 /* get the conversion qualifier */
256 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'q') {
267 while (--field_width
> 0)
269 *str
++ = (unsigned char) va_arg(args
, int);
270 while (--field_width
> 0)
275 s
= va_arg(args
, char *);
279 len
= strnlen(s
, precision
);
282 while (len
< field_width
--)
284 for (i
= 0; i
< len
; ++i
)
286 while (len
< field_width
--)
291 if (field_width
== -1) {
292 field_width
= 2*sizeof(void *);
296 (unsigned long) va_arg(args
, void *), 16,
297 field_width
, precision
, flags
);
302 if (qualifier
== 'l') {
303 long * ip
= va_arg(args
, long *);
306 int * ip
= va_arg(args
, int *);
315 /* integer number formats - set up the flags and "break" */
340 #ifdef CFG_64BIT_VSPRINTF
341 if (qualifier
== 'q') /* "quad" for 64 bit variables */
342 num
= va_arg(args
, unsigned long long);
345 if (qualifier
== 'l')
346 num
= va_arg(args
, unsigned long);
347 else if (qualifier
== 'h') {
348 num
= (unsigned short) va_arg(args
, int);
351 } else if (flags
& SIGN
)
352 num
= va_arg(args
, int);
354 num
= va_arg(args
, unsigned int);
355 str
= number(str
, num
, base
, field_width
, precision
, flags
);
361 int sprintf(char * buf
, const char *fmt
, ...)
367 i
=vsprintf(buf
,fmt
,args
);
372 void panic(const char *fmt
, ...)
379 #if defined (CONFIG_PANIC_HANG)
382 udelay (100000); /* allow messages to go out */
383 do_reset (NULL
, 0, 0, NULL
);
This page took 0.068067 seconds and 5 git commands to generate.