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'))
17 /* We are using number() instead. So this code is obsoleted and should
21 static unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
23 unsigned long result = 0,value;
30 if ((*cp == 'x') && isxdigit(cp[1])) {
36 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
37 ? toupper(*cp) : *cp)-'A'+10) < base) {
38 result = result*base + value;
46 static long simple_strtol(const char *cp,char **endp,unsigned int base)
49 return -simple_strtoul(cp+1,endp,base);
50 return simple_strtoul(cp,endp,base);
54 static int skip_atoi(const char **s)
59 i = i*10 + *((*s)++) - '0';
63 #define ZEROPAD 1 /* pad with zero */
64 #define SIGN 2 /* unsigned/signed long */
65 #define PLUS 4 /* show plus */
66 #define SPACE 8 /* space if plus */
67 #define LEFT 16 /* left justified */
68 #define SPECIAL 32 /* 0x */
69 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
71 static int number(void (*tx_byte)(unsigned char byte),
72 unsigned long long num, int base, int size, int precision, int type)
75 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
80 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
83 if (base < 2 || base > 36)
85 c = (type & ZEROPAD) ? '0' : ' ';
88 if ((signed long long)num < 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)
143 unsigned long long num;
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') {
220 while (--field_width > 0)
221 tx_byte(' '), count++;
222 tx_byte((unsigned char) va_arg(args, int)), count++;
223 while (--field_width > 0)
224 tx_byte(' '), count++;
228 s = va_arg(args, char *);
232 len = strnlen(s, precision);
235 while (len < field_width--)
236 tx_byte(' '), count++;
237 for (i = 0; i < len; ++i)
238 tx_byte(*s++), count++;
239 while (len < field_width--)
240 tx_byte(' '), count++;
244 if (field_width == -1) {
245 field_width = 2*sizeof(void *);
248 count += number(tx_byte,
249 (unsigned long) va_arg(args, void *), 16,
250 field_width, precision, flags);
255 if (qualifier == 'L') {
256 long long *ip = va_arg(args, long long *);
258 } else if (qualifier == 'l') {
259 long * ip = va_arg(args, long *);
262 int * ip = va_arg(args, int *);
268 tx_byte('%'), count++;
271 /* integer number formats - set up the flags and "break" */
289 tx_byte('%'), count++;
291 tx_byte(*fmt), count++;
296 if (qualifier == 'L') {
297 num = va_arg(args, unsigned long long);
298 } else if (qualifier == 'l') {
299 num = va_arg(args, unsigned long);
300 } else if (qualifier == 'h') {
301 num = (unsigned short) va_arg(args, int);
304 } else if (flags & SIGN) {
305 num = va_arg(args, int);
307 num = va_arg(args, unsigned int);
309 count += number(tx_byte, num, base, field_width, precision, flags);