|
|
1.1 root 1: /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
2: /*
3: * Wirzenius wrote this portably, Torvalds fucked it up :-)
4: */
5:
6: #include <stdarg.h>
7: #include <string.h>
8:
9: /* we use this so that we can do without the ctype library */
10: #define is_digit(c) ((c) >= '0' && (c) <= '9')
11:
12: static int skip_atoi(const char **s)
13: {
14: int i=0;
15:
16: while (is_digit(**s))
17: i = i*10 + *((*s)++) - '0';
18: return i;
19: }
20:
21: #define ZEROPAD 1 /* pad with zero */
22: #define SIGN 2 /* unsigned/signed long */
23: #define PLUS 4 /* show plus */
24: #define SPACE 8 /* space if plus */
25: #define LEFT 16 /* left justified */
26: #define SPECIAL 32 /* 0x */
27: #define SMALL 64 /* use 'abcdef' instead of 'ABCDEF' */
28:
29: #define do_div(n,base) ({ \
30: int __res; \
31: __asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
32: __res; })
33:
34: static char * number(char * str, int num, int base, int size, int precision
35: ,int type)
36: {
37: char c,sign,tmp[36];
38: const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
39: int i;
40:
41: if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz";
42: if (type&LEFT) type &= ~ZEROPAD;
43: if (base<2 || base>36)
44: return 0;
45: c = (type & ZEROPAD) ? '0' : ' ' ;
46: if (type&SIGN && num<0) {
47: sign='-';
48: num = -num;
49: } else
50: sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
51: if (sign) size--;
1.1.1.2 ! root 52: if (type&SPECIAL){
1.1 root 53: if (base==16) size -= 2;
54: else if (base==8) size--;
1.1.1.2 ! root 55: }
1.1 root 56: i=0;
57: if (num==0)
58: tmp[i++]='0';
59: else while (num!=0)
60: tmp[i++]=digits[do_div(num,base)];
61: if (i>precision) precision=i;
62: size -= precision;
63: if (!(type&(ZEROPAD+LEFT)))
64: while(size-->0)
65: *str++ = ' ';
66: if (sign)
67: *str++ = sign;
1.1.1.2 ! root 68: if (type&SPECIAL) {
1.1 root 69: if (base==8)
70: *str++ = '0';
71: else if (base==16) {
72: *str++ = '0';
73: *str++ = digits[33];
74: }
1.1.1.2 ! root 75: }
1.1 root 76: if (!(type&LEFT))
77: while(size-->0)
78: *str++ = c;
79: while(i<precision--)
80: *str++ = '0';
81: while(i-->0)
82: *str++ = tmp[i];
83: while(size-->0)
84: *str++ = ' ';
85: return str;
86: }
87:
88: int vsprintf(char *buf, const char *fmt, va_list args)
89: {
90: int len;
91: int i;
92: char * str;
93: char *s;
94: int *ip;
95:
96: int flags; /* flags to number() */
97:
98: int field_width; /* width of output field */
99: int precision; /* min. # of digits for integers; max
100: number of chars for from string */
101: int qualifier; /* 'h', 'l', or 'L' for integer fields */
102:
103: for (str=buf ; *fmt ; ++fmt) {
104: if (*fmt != '%') {
105: *str++ = *fmt;
106: continue;
107: }
108:
109: /* process flags */
110: flags = 0;
111: repeat:
112: ++fmt; /* this also skips first '%' */
113: switch (*fmt) {
114: case '-': flags |= LEFT; goto repeat;
115: case '+': flags |= PLUS; goto repeat;
116: case ' ': flags |= SPACE; goto repeat;
117: case '#': flags |= SPECIAL; goto repeat;
118: case '0': flags |= ZEROPAD; goto repeat;
119: }
120:
121: /* get field width */
122: field_width = -1;
123: if (is_digit(*fmt))
124: field_width = skip_atoi(&fmt);
125: else if (*fmt == '*') {
126: /* it's the next argument */
127: field_width = va_arg(args, int);
128: if (field_width < 0) {
129: field_width = -field_width;
130: flags |= LEFT;
131: }
132: }
133:
134: /* get the precision */
135: precision = -1;
136: if (*fmt == '.') {
137: ++fmt;
138: if (is_digit(*fmt))
139: precision = skip_atoi(&fmt);
140: else if (*fmt == '*') {
141: /* it's the next argument */
142: precision = va_arg(args, int);
143: }
144: if (precision < 0)
145: precision = 0;
146: }
147:
148: /* get the conversion qualifier */
149: qualifier = -1;
150: if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
151: qualifier = *fmt;
152: ++fmt;
153: }
154:
155: switch (*fmt) {
156: case 'c':
157: if (!(flags & LEFT))
158: while (--field_width > 0)
159: *str++ = ' ';
160: *str++ = (unsigned char) va_arg(args, int);
161: while (--field_width > 0)
162: *str++ = ' ';
163: break;
164:
165: case 's':
166: s = va_arg(args, char *);
167: len = strlen(s);
168: if (precision < 0)
169: precision = len;
170: else if (len > precision)
171: len = precision;
172:
173: if (!(flags & LEFT))
174: while (len < field_width--)
175: *str++ = ' ';
176: for (i = 0; i < len; ++i)
177: *str++ = *s++;
178: while (len < field_width--)
179: *str++ = ' ';
180: break;
181:
182: case 'o':
183: str = number(str, va_arg(args, unsigned long), 8,
184: field_width, precision, flags);
185: break;
186:
187: case 'p':
188: if (field_width == -1) {
189: field_width = 8;
190: flags |= ZEROPAD;
191: }
192: str = number(str,
193: (unsigned long) va_arg(args, void *), 16,
194: field_width, precision, flags);
195: break;
196:
197: case 'x':
198: flags |= SMALL;
199: case 'X':
200: str = number(str, va_arg(args, unsigned long), 16,
201: field_width, precision, flags);
202: break;
203:
204: case 'd':
205: case 'i':
206: flags |= SIGN;
207: case 'u':
208: str = number(str, va_arg(args, unsigned long), 10,
209: field_width, precision, flags);
210: break;
211:
212: case 'n':
213: ip = va_arg(args, int *);
214: *ip = (str - buf);
215: break;
216:
217: default:
218: if (*fmt != '%')
219: *str++ = '%';
220: if (*fmt)
221: *str++ = *fmt;
222: else
223: --fmt;
224: break;
225: }
226: }
227: *str = '\0';
228: return str-buf;
229: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.