2 * linux/kernel/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>
17 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
19 unsigned long result
= 0,value
;
26 if ((*cp
== 'x') && isxdigit(cp
[1])) {
32 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
33 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
34 result
= result
*base
+ value
;
42 /* we use this so that we can do without the ctype library */
43 #define is_digit(c) ((c) >= '0' && (c) <= '9')
45 static int skip_atoi(const char **s
)
50 i
= i
*10 + *((*s
)++) - '0';
54 #define ZEROPAD 1 /* pad with zero */
55 #define SIGN 2 /* unsigned/signed long */
56 #define PLUS 4 /* show plus */
57 #define SPACE 8 /* space if plus */
58 #define LEFT 16 /* left justified */
59 #define SPECIAL 32 /* 0x */
60 #define SMALL 64 /* use 'abcdef' instead of 'ABCDEF' */
62 #define do_div(n,base) ({ \
64 __asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
67 static char * number(char * str
, int num
, int base
, int size
, int precision
71 const char *digits
="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
74 if (type
&SMALL
) digits
="0123456789abcdefghijklmnopqrstuvwxyz";
75 if (type
&LEFT
) type
&= ~ZEROPAD
;
76 if (base
<2 || base
>36)
78 c
= (type
& ZEROPAD
) ? '0' : ' ' ;
79 if (type
&SIGN
&& num
<0) {
83 sign
=(type
&PLUS
) ? '+' : ((type
&SPACE
) ? ' ' : 0);
86 if (base
==16) size
-= 2;
87 else if (base
==8) size
--;
92 tmp
[i
++]=digits
[do_div(num
,base
)];
93 if (i
>precision
) precision
=i
;
95 if (!(type
&(ZEROPAD
+LEFT
)))
119 int vsprintf(char *buf
, const char *fmt
, va_list args
)
127 int flags
; /* flags to number() */
129 int field_width
; /* width of output field */
130 int precision
; /* min. # of digits for integers; max
131 number of chars for from string */
132 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
134 for (str
=buf
; *fmt
; ++fmt
) {
143 ++fmt
; /* this also skips first '%' */
145 case '-': flags
|= LEFT
; goto repeat
;
146 case '+': flags
|= PLUS
; goto repeat
;
147 case ' ': flags
|= SPACE
; goto repeat
;
148 case '#': flags
|= SPECIAL
; goto repeat
;
149 case '0': flags
|= ZEROPAD
; goto repeat
;
152 /* get field width */
155 field_width
= skip_atoi(&fmt
);
156 else if (*fmt
== '*') {
157 /* it's the next argument */
158 field_width
= va_arg(args
, int);
159 if (field_width
< 0) {
160 field_width
= -field_width
;
165 /* get the precision */
170 precision
= skip_atoi(&fmt
);
171 else if (*fmt
== '*') {
172 /* it's the next argument */
173 precision
= va_arg(args
, int);
179 /* get the conversion qualifier */
181 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L') {
189 while (--field_width
> 0)
191 *str
++ = (unsigned char) va_arg(args
, int);
192 while (--field_width
> 0)
197 s
= va_arg(args
, char *);
203 else if (len
> precision
)
207 while (len
< field_width
--)
209 for (i
= 0; i
< len
; ++i
)
211 while (len
< field_width
--)
216 str
= number(str
, va_arg(args
, unsigned long), 8,
217 field_width
, precision
, flags
);
221 if (field_width
== -1) {
226 (unsigned long) va_arg(args
, void *), 16,
227 field_width
, precision
, flags
);
233 str
= number(str
, va_arg(args
, unsigned long), 16,
234 field_width
, precision
, flags
);
241 str
= number(str
, va_arg(args
, unsigned long), 10,
242 field_width
, precision
, flags
);
246 ip
= va_arg(args
, int *);
264 int sprintf(char * buf
, const char *fmt
, ...)
270 i
=vsprintf(buf
,fmt
,args
);