4 * Copyright (C) 1991, 1992 Linus Torvalds
10 /* haha, don't need ctype.c */
11 #define isdigit(c) ((c) >= '0' && (c) <= '9')
12 #define is_digit isdigit
13 #define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
15 static unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
17 unsigned long result = 0,value;
24 if ((*cp == 'x') && isxdigit(cp[1])) {
30 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
31 ? toupper(*cp) : *cp)-'A'+10) < base) {
32 result = result*base + value;
40 static long simple_strtol(const char *cp,char **endp,unsigned int base)
43 return -simple_strtoul(cp+1,endp,base);
44 return simple_strtoul(cp,endp,base);
48 static int skip_atoi(const char **s)
53 i = i*10 + *((*s)++) - '0';
57 #define ZEROPAD 1 /* pad with zero */
58 #define SIGN 2 /* unsigned/signed long */
59 #define PLUS 4 /* show plus */
60 #define SPACE 8 /* space if plus */
61 #define LEFT 16 /* left justified */
62 #define SPECIAL 32 /* 0x */
63 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
65 #define do_div(n,base) ({ \
67 __res = ((unsigned long) n) % (unsigned) base; \
68 n = ((unsigned long) n) / (unsigned) base; \
71 static int number(void (*tx_byte)(unsigned char byte), long num, int base, int size, int precision
75 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
80 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
83 if (base < 2 || base > 36)
85 c = (type & ZEROPAD) ? '0' : ' ';
92 } else if (type & PLUS) {
95 } else if (type & SPACE) {
100 if (type & SPECIAL) {
109 else while (num != 0)
110 tmp[i++] = digits[do_div(num,base)];
114 if (!(type&(ZEROPAD+LEFT)))
116 tx_byte(' '), count++;
118 tx_byte(sign), count++;
119 if (type & SPECIAL) {
121 tx_byte('0'), count++;
123 tx_byte('0'), count++;
124 tx_byte(digits[33]), count++;
130 while (i < precision--)
131 tx_byte('0'), count++;
133 tx_byte(tmp[i]), count++;
135 tx_byte(' '), count++;
140 int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
147 int flags; /* flags to number() */
149 int field_width; /* width of output field */
150 int precision; /* min. # of digits for integers; max
151 number of chars for from string */
152 int qualifier; /* 'h', 'l', or 'L' for integer fields */
156 for (count=0; *fmt ; ++fmt) {
158 tx_byte(*fmt), count++;
165 ++fmt; /* this also skips first '%' */
167 case '-': flags |= LEFT; goto repeat;
168 case '+': flags |= PLUS; goto repeat;
169 case ' ': flags |= SPACE; goto repeat;
170 case '#': flags |= SPECIAL; goto repeat;
171 case '0': flags |= ZEROPAD; goto repeat;
174 /* get field width */
177 field_width = skip_atoi(&fmt);
178 else if (*fmt == '*') {
180 /* it's the next argument */
181 field_width = va_arg(args, int);
182 if (field_width < 0) {
183 field_width = -field_width;
188 /* get the precision */
193 precision = skip_atoi(&fmt);
194 else if (*fmt == '*') {
196 /* it's the next argument */
197 precision = va_arg(args, int);
203 /* get the conversion qualifier */
205 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
216 while (--field_width > 0)
217 tx_byte(' '), count++;
218 tx_byte((unsigned char) va_arg(args, int)), count++;
219 while (--field_width > 0)
220 tx_byte(' '), count++;
224 s = va_arg(args, char *);
228 len = strnlen(s, precision);
231 while (len < field_width--)
232 tx_byte(' '), count++;
233 for (i = 0; i < len; ++i)
234 tx_byte(*s++), count++;
235 while (len < field_width--)
236 tx_byte(' '), count++;
240 if (field_width == -1) {
241 field_width = 2*sizeof(void *);
244 count += number(tx_byte,
245 (unsigned long) va_arg(args, void *), 16,
246 field_width, precision, flags);
251 if (qualifier == 'l') {
252 long * ip = va_arg(args, long *);
255 int * ip = va_arg(args, int *);
261 tx_byte('%'), count++;
264 /* integer number formats - set up the flags and "break" */
282 tx_byte('%'), count++;
284 tx_byte(*fmt), count++;
289 if (qualifier == 'l')
290 num = va_arg(args, unsigned long);
291 else if (qualifier == 'h') {
292 num = (unsigned short) va_arg(args, int);
295 } else if (flags & SIGN)
296 num = va_arg(args, int);
298 num = va_arg(args, unsigned int);
299 count += number(tx_byte, num, base, field_width, precision, flags);