4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
14 /* haha, don't need ctype.c */
15 #define isdigit(c) ((c) >= '0' && (c) <= '9')
16 #define is_digit isdigit
17 #define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
18 #define islower(c) ((c) >= 'a' && (c) <= 'z')
19 #define toupper(c) __toupper(c)
21 static inline unsigned char __toupper(unsigned char c)
29 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
31 unsigned long result = 0,value;
38 if ((*cp == 'x') && isxdigit(cp[1])) {
44 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
45 ? toupper(*cp) : *cp)-'A'+10) < base) {
46 result = result*base + value;
54 long simple_strtol(const char *cp,char **endp,unsigned int base)
57 return -simple_strtoul(cp+1,endp,base);
58 return simple_strtoul(cp,endp,base);
62 static int skip_atoi(const char **s)
67 i = i*10 + *((*s)++) - '0';
71 #define ZEROPAD 1 /* pad with zero */
72 #define SIGN 2 /* unsigned/signed long */
73 #define PLUS 4 /* show plus */
74 #define SPACE 8 /* space if plus */
75 #define LEFT 16 /* left justified */
76 #define SPECIAL 32 /* 0x */
77 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
79 #define do_div(n,base) ({ \
81 __res = ((unsigned long) n) % (unsigned) base; \
82 n = ((unsigned long) n) / (unsigned) base; \
85 static int number(void (*tx_byte)(unsigned char byte), long num, int base, int size, int precision
89 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
94 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
97 if (base < 2 || base > 36)
99 c = (type & ZEROPAD) ? '0' : ' ';
106 } else if (type & PLUS) {
109 } else if (type & SPACE) {
114 if (type & SPECIAL) {
123 else while (num != 0)
124 tmp[i++] = digits[do_div(num,base)];
128 if (!(type&(ZEROPAD+LEFT)))
130 tx_byte(' '), count++;
132 tx_byte(sign), count++;
133 if (type & SPECIAL) {
135 tx_byte('0'), count++;
137 tx_byte('0'), count++;
138 tx_byte(digits[33]), count++;
144 while (i < precision--)
145 tx_byte('0'), count++;
147 tx_byte(tmp[i]), count++;
149 tx_byte(' '), count++;
154 int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
161 int flags; /* flags to number() */
163 int field_width; /* width of output field */
164 int precision; /* min. # of digits for integers; max
165 number of chars for from string */
166 int qualifier; /* 'h', 'l', or 'L' for integer fields */
170 for (count=0; *fmt ; ++fmt) {
172 tx_byte(*fmt), count++;
179 ++fmt; /* this also skips first '%' */
181 case '-': flags |= LEFT; goto repeat;
182 case '+': flags |= PLUS; goto repeat;
183 case ' ': flags |= SPACE; goto repeat;
184 case '#': flags |= SPECIAL; goto repeat;
185 case '0': flags |= ZEROPAD; goto repeat;
188 /* get field width */
191 field_width = skip_atoi(&fmt);
192 else if (*fmt == '*') {
194 /* it's the next argument */
195 field_width = va_arg(args, int);
196 if (field_width < 0) {
197 field_width = -field_width;
202 /* get the precision */
207 precision = skip_atoi(&fmt);
208 else if (*fmt == '*') {
210 /* it's the next argument */
211 precision = va_arg(args, int);
217 /* get the conversion qualifier */
219 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
230 while (--field_width > 0)
231 tx_byte(' '), count++;
232 tx_byte((unsigned char) va_arg(args, int)), count++;
233 while (--field_width > 0)
234 tx_byte(' '), count++;
238 s = va_arg(args, char *);
242 len = strnlen(s, precision);
245 while (len < field_width--)
246 tx_byte(' '), count++;
247 for (i = 0; i < len; ++i)
248 tx_byte(*s++), count++;
249 while (len < field_width--)
250 tx_byte(' '), count++;
254 if (field_width == -1) {
255 field_width = 2*sizeof(void *);
258 count += number(tx_byte,
259 (unsigned long) va_arg(args, void *), 16,
260 field_width, precision, flags);
265 if (qualifier == 'l') {
266 long * ip = va_arg(args, long *);
269 int * ip = va_arg(args, int *);
275 tx_byte('%'), count++;
278 /* integer number formats - set up the flags and "break" */
296 tx_byte('%'), count++;
298 tx_byte(*fmt), count++;
303 if (qualifier == 'l')
304 num = va_arg(args, unsigned long);
305 else if (qualifier == 'h') {
306 num = (unsigned short) va_arg(args, int);
309 } else if (flags & SIGN)
310 num = va_arg(args, int);
312 num = va_arg(args, unsigned int);
313 count += number(tx_byte, num, base, field_width, precision, flags);
318 /* FIXME this global makes vsprintf non-reentrant
320 static char *str_buf;
321 static void str_tx_byte(unsigned char byte)
327 int vsprintf(char * buf, const char *fmt, va_list args)
331 i = vtxprintf(str_tx_byte, fmt, args);
332 /* maeder/Ispiri -- The null termination was missing a deference */
333 /* and was just zeroing out the pointer instead */
338 int sprintf(char * buf, const char *fmt, ...)
344 i=vsprintf(buf,fmt,args);