4 * Copyright (C) 1991, 1992 Linus Torvalds
11 /* haha, don't need ctype.c */
12 #define isdigit(c) ((c) >= '0' && (c) <= '9')
13 #define is_digit isdigit
14 #define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
16 static unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
18 unsigned long result = 0,value;
25 if ((*cp == 'x') && isxdigit(cp[1])) {
31 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
32 ? toupper(*cp) : *cp)-'A'+10) < base) {
33 result = result*base + value;
41 static long simple_strtol(const char *cp,char **endp,unsigned int base)
44 return -simple_strtoul(cp+1,endp,base);
45 return simple_strtoul(cp,endp,base);
49 static int skip_atoi(const char **s)
54 i = i*10 + *((*s)++) - '0';
58 #define ZEROPAD 1 /* pad with zero */
59 #define SIGN 2 /* unsigned/signed long */
60 #define PLUS 4 /* show plus */
61 #define SPACE 8 /* space if plus */
62 #define LEFT 16 /* left justified */
63 #define SPECIAL 32 /* 0x */
64 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
66 static int number(void (*tx_byte)(unsigned char byte),
67 unsigned long long num, int base, int size, int precision, int type)
70 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
75 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
78 if (base < 2 || base > 36)
80 c = (type & ZEROPAD) ? '0' : ' ';
83 if ((signed long long)num < 0) {
87 } else if (type & PLUS) {
90 } else if (type & SPACE) {
104 else while (num != 0)
105 tmp[i++] = digits[do_div(num,base)];
109 if (!(type&(ZEROPAD+LEFT)))
111 tx_byte(' '), count++;
113 tx_byte(sign), count++;
114 if (type & SPECIAL) {
116 tx_byte('0'), count++;
118 tx_byte('0'), count++;
119 tx_byte(digits[33]), count++;
125 while (i < precision--)
126 tx_byte('0'), count++;
128 tx_byte(tmp[i]), count++;
130 tx_byte(' '), count++;
135 int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
138 unsigned long long num;
142 int flags; /* flags to number() */
144 int field_width; /* width of output field */
145 int precision; /* min. # of digits for integers; max
146 number of chars for from string */
147 int qualifier; /* 'h', 'l', or 'L' for integer fields */
151 for (count=0; *fmt ; ++fmt) {
153 tx_byte(*fmt), count++;
160 ++fmt; /* this also skips first '%' */
162 case '-': flags |= LEFT; goto repeat;
163 case '+': flags |= PLUS; goto repeat;
164 case ' ': flags |= SPACE; goto repeat;
165 case '#': flags |= SPECIAL; goto repeat;
166 case '0': flags |= ZEROPAD; goto repeat;
169 /* get field width */
172 field_width = skip_atoi(&fmt);
173 else if (*fmt == '*') {
175 /* it's the next argument */
176 field_width = va_arg(args, int);
177 if (field_width < 0) {
178 field_width = -field_width;
183 /* get the precision */
188 precision = skip_atoi(&fmt);
189 else if (*fmt == '*') {
191 /* it's the next argument */
192 precision = va_arg(args, int);
198 /* get the conversion qualifier */
200 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
215 while (--field_width > 0)
216 tx_byte(' '), count++;
217 tx_byte((unsigned char) va_arg(args, int)), count++;
218 while (--field_width > 0)
219 tx_byte(' '), count++;
223 s = va_arg(args, char *);
227 len = strnlen(s, precision);
230 while (len < field_width--)
231 tx_byte(' '), count++;
232 for (i = 0; i < len; ++i)
233 tx_byte(*s++), count++;
234 while (len < field_width--)
235 tx_byte(' '), count++;
239 if (field_width == -1) {
240 field_width = 2*sizeof(void *);
243 count += number(tx_byte,
244 (unsigned long) va_arg(args, void *), 16,
245 field_width, precision, flags);
250 if (qualifier == 'L') {
251 long long *ip = va_arg(args, long long *);
253 } else if (qualifier == 'l') {
254 long * ip = va_arg(args, long *);
257 int * ip = va_arg(args, int *);
263 tx_byte('%'), count++;
266 /* integer number formats - set up the flags and "break" */
284 tx_byte('%'), count++;
286 tx_byte(*fmt), count++;
291 if (qualifier == 'L') {
292 num = va_arg(args, unsigned long long);
293 } else if (qualifier == 'l') {
294 num = va_arg(args, unsigned long);
295 } else if (qualifier == 'h') {
296 num = (unsigned short) va_arg(args, int);
299 } else if (flags & SIGN) {
300 num = va_arg(args, int);
302 num = va_arg(args, unsigned int);
304 count += number(tx_byte, num, base, field_width, precision, flags);