4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 static char rcsid[] = "$Id$";
19 /* haha, don't need ctype.c */
20 #define isdigit(c) ((c) >= '0' && (c) <= '9')
21 #define is_digit isdigit
22 #define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
23 #define islower(c) ((c) >= 'a' && (c) <= 'z')
24 #define toupper(c) __toupper(c)
26 static inline unsigned char __toupper(unsigned char c)
34 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
36 unsigned long result = 0,value;
43 if ((*cp == 'x') && isxdigit(cp[1])) {
49 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
50 ? toupper(*cp) : *cp)-'A'+10) < base) {
51 result = result*base + value;
59 long simple_strtol(const char *cp,char **endp,unsigned int base)
62 return -simple_strtoul(cp+1,endp,base);
63 return simple_strtoul(cp,endp,base);
67 static int skip_atoi(const char **s)
72 i = i*10 + *((*s)++) - '0';
76 #define ZEROPAD 1 /* pad with zero */
77 #define SIGN 2 /* unsigned/signed long */
78 #define PLUS 4 /* show plus */
79 #define SPACE 8 /* space if plus */
80 #define LEFT 16 /* left justified */
81 #define SPECIAL 32 /* 0x */
82 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
84 #define do_div(n,base) ({ \
86 __res = ((unsigned long) n) % (unsigned) base; \
87 n = ((unsigned long) n) / (unsigned) base; \
90 static int number(void (*tx_byte)(unsigned char byte), long num, int base, int size, int precision
94 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
99 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
102 if (base < 2 || base > 36)
104 c = (type & ZEROPAD) ? '0' : ' ';
111 } else if (type & PLUS) {
114 } else if (type & SPACE) {
119 if (type & SPECIAL) {
128 else while (num != 0)
129 tmp[i++] = digits[do_div(num,base)];
133 if (!(type&(ZEROPAD+LEFT)))
135 tx_byte(' '), count++;
137 tx_byte(sign), count++;
138 if (type & SPECIAL) {
140 tx_byte('0'), count++;
142 tx_byte('0'), count++;
143 tx_byte(digits[33]), count++;
149 while (i < precision--)
150 tx_byte('0'), count++;
152 tx_byte(tmp[i]), count++;
154 tx_byte(' '), count++;
159 int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
166 int flags; /* flags to number() */
168 int field_width; /* width of output field */
169 int precision; /* min. # of digits for integers; max
170 number of chars for from string */
171 int qualifier; /* 'h', 'l', or 'L' for integer fields */
175 for (count=0; *fmt ; ++fmt) {
177 tx_byte(*fmt), count++;
184 ++fmt; /* this also skips first '%' */
186 case '-': flags |= LEFT; goto repeat;
187 case '+': flags |= PLUS; goto repeat;
188 case ' ': flags |= SPACE; goto repeat;
189 case '#': flags |= SPECIAL; goto repeat;
190 case '0': flags |= ZEROPAD; goto repeat;
193 /* get field width */
196 field_width = skip_atoi(&fmt);
197 else if (*fmt == '*') {
199 /* it's the next argument */
200 field_width = va_arg(args, int);
201 if (field_width < 0) {
202 field_width = -field_width;
207 /* get the precision */
212 precision = skip_atoi(&fmt);
213 else if (*fmt == '*') {
215 /* it's the next argument */
216 precision = va_arg(args, int);
222 /* get the conversion qualifier */
224 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
235 while (--field_width > 0)
236 tx_byte(' '), count++;
237 tx_byte((unsigned char) va_arg(args, int)), count++;
238 while (--field_width > 0)
239 tx_byte(' '), count++;
243 s = va_arg(args, char *);
247 len = strnlen(s, precision);
250 while (len < field_width--)
251 tx_byte(' '), count++;
252 for (i = 0; i < len; ++i)
253 tx_byte(*s++), count++;
254 while (len < field_width--)
255 tx_byte(' '), count++;
259 if (field_width == -1) {
260 field_width = 2*sizeof(void *);
263 count += number(tx_byte,
264 (unsigned long) va_arg(args, void *), 16,
265 field_width, precision, flags);
270 if (qualifier == 'l') {
271 long * ip = va_arg(args, long *);
274 int * ip = va_arg(args, int *);
280 tx_byte('%'), count++;
283 /* integer number formats - set up the flags and "break" */
301 tx_byte('%'), count++;
303 tx_byte(*fmt), count++;
308 if (qualifier == 'l')
309 num = va_arg(args, unsigned long);
310 else if (qualifier == 'h') {
311 num = (unsigned short) va_arg(args, int);
314 } else if (flags & SIGN)
315 num = va_arg(args, int);
317 num = va_arg(args, unsigned int);
318 count += number(tx_byte, num, base, field_width, precision, flags);
323 /* FIXME this global makes vsprintf non-reentrant
325 static char *str_buf;
326 static void str_tx_byte(unsigned char byte)
332 int vsprintf(char * buf, const char *fmt, va_list args)
336 i = vtxprintf(str_tx_byte, fmt, args);
337 /* maeder/Ispiri -- The null termination was missing a deference */
338 /* and was just zeroing out the pointer instead */
343 int sprintf(char * buf, const char *fmt, ...)
349 i=vsprintf(buf,fmt,args);