blob: 129f02ea48b432c88e3386e4829ef7aafd3b0aab [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
Eric Biederman8ca8d762003-04-22 19:02:15 +000011#include <stdarg.h>
12#include <string.h>
13
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'))
Eric Biederman8ca8d762003-04-22 19:02:15 +000018
19unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
20{
21 unsigned long result = 0,value;
22
23 if (!base) {
24 base = 10;
25 if (*cp == '0') {
26 base = 8;
27 cp++;
28 if ((*cp == 'x') && isxdigit(cp[1])) {
29 cp++;
30 base = 16;
31 }
32 }
33 }
34 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
35 ? toupper(*cp) : *cp)-'A'+10) < base) {
36 result = result*base + value;
37 cp++;
38 }
39 if (endp)
40 *endp = (char *)cp;
41 return result;
42}
43
44long simple_strtol(const char *cp,char **endp,unsigned int base)
45{
46 if(*cp=='-')
47 return -simple_strtoul(cp+1,endp,base);
48 return simple_strtoul(cp,endp,base);
49}
50
51
52static int skip_atoi(const char **s)
53{
54 int i=0;
55
56 while (is_digit(**s))
57 i = i*10 + *((*s)++) - '0';
58 return i;
59}
60
61#define ZEROPAD 1 /* pad with zero */
62#define SIGN 2 /* unsigned/signed long */
63#define PLUS 4 /* show plus */
64#define SPACE 8 /* space if plus */
65#define LEFT 16 /* left justified */
66#define SPECIAL 32 /* 0x */
67#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
68
69#define do_div(n,base) ({ \
70int __res; \
71__res = ((unsigned long) n) % (unsigned) base; \
72n = ((unsigned long) n) / (unsigned) base; \
73__res; })
74
75static int number(void (*tx_byte)(unsigned char byte), long num, int base, int size, int precision
76 ,int type)
77{
78 char c,sign,tmp[66];
79 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
80 int i;
81 int count = 0;
82
83 if (type & LARGE)
84 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
85 if (type & LEFT)
86 type &= ~ZEROPAD;
87 if (base < 2 || base > 36)
88 return 0;
89 c = (type & ZEROPAD) ? '0' : ' ';
90 sign = 0;
91 if (type & SIGN) {
92 if (num < 0) {
93 sign = '-';
94 num = -num;
95 size--;
96 } else if (type & PLUS) {
97 sign = '+';
98 size--;
99 } else if (type & SPACE) {
100 sign = ' ';
101 size--;
102 }
103 }
104 if (type & SPECIAL) {
105 if (base == 16)
106 size -= 2;
107 else if (base == 8)
108 size--;
109 }
110 i = 0;
111 if (num == 0)
112 tmp[i++]='0';
113 else while (num != 0)
114 tmp[i++] = digits[do_div(num,base)];
115 if (i > precision)
116 precision = i;
117 size -= precision;
118 if (!(type&(ZEROPAD+LEFT)))
119 while(size-->0)
120 tx_byte(' '), count++;
121 if (sign)
122 tx_byte(sign), count++;
123 if (type & SPECIAL) {
124 if (base==8)
125 tx_byte('0'), count++;
126 else if (base==16) {
127 tx_byte('0'), count++;
128 tx_byte(digits[33]), count++;
129 }
130 }
131 if (!(type & LEFT))
132 while (size-- > 0)
133 tx_byte(c), count++;
134 while (i < precision--)
135 tx_byte('0'), count++;
136 while (i-- > 0)
137 tx_byte(tmp[i]), count++;
138 while (size-- > 0)
139 tx_byte(' '), count++;
140 return count;
141}
142
143
144int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
145{
146 int len;
147 unsigned long num;
148 int i, base;
149 const char *s;
150
151 int flags; /* flags to number() */
152
153 int field_width; /* width of output field */
154 int precision; /* min. # of digits for integers; max
155 number of chars for from string */
156 int qualifier; /* 'h', 'l', or 'L' for integer fields */
157
158 int count;
159
160 for (count=0; *fmt ; ++fmt) {
161 if (*fmt != '%') {
162 tx_byte(*fmt), count++;
163 continue;
164 }
165
166 /* process flags */
167 flags = 0;
168 repeat:
169 ++fmt; /* this also skips first '%' */
170 switch (*fmt) {
171 case '-': flags |= LEFT; goto repeat;
172 case '+': flags |= PLUS; goto repeat;
173 case ' ': flags |= SPACE; goto repeat;
174 case '#': flags |= SPECIAL; goto repeat;
175 case '0': flags |= ZEROPAD; goto repeat;
176 }
177
178 /* get field width */
179 field_width = -1;
180 if (is_digit(*fmt))
181 field_width = skip_atoi(&fmt);
182 else if (*fmt == '*') {
183 ++fmt;
184 /* it's the next argument */
185 field_width = va_arg(args, int);
186 if (field_width < 0) {
187 field_width = -field_width;
188 flags |= LEFT;
189 }
190 }
191
192 /* get the precision */
193 precision = -1;
194 if (*fmt == '.') {
195 ++fmt;
196 if (is_digit(*fmt))
197 precision = skip_atoi(&fmt);
198 else if (*fmt == '*') {
199 ++fmt;
200 /* it's the next argument */
201 precision = va_arg(args, int);
202 }
203 if (precision < 0)
204 precision = 0;
205 }
206
207 /* get the conversion qualifier */
208 qualifier = -1;
209 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
210 qualifier = *fmt;
211 ++fmt;
212 }
213
214 /* default base */
215 base = 10;
216
217 switch (*fmt) {
218 case 'c':
219 if (!(flags & LEFT))
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++;
225 continue;
226
227 case 's':
228 s = va_arg(args, char *);
229 if (!s)
230 s = "<NULL>";
231
232 len = strnlen(s, precision);
233
234 if (!(flags & LEFT))
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++;
241 continue;
242
243 case 'p':
244 if (field_width == -1) {
245 field_width = 2*sizeof(void *);
246 flags |= ZEROPAD;
247 }
248 count += number(tx_byte,
249 (unsigned long) va_arg(args, void *), 16,
250 field_width, precision, flags);
251 continue;
252
253
254 case 'n':
255 if (qualifier == 'l') {
256 long * ip = va_arg(args, long *);
257 *ip = count;
258 } else {
259 int * ip = va_arg(args, int *);
260 *ip = count;
261 }
262 continue;
263
264 case '%':
265 tx_byte('%'), count++;
266 continue;
267
268 /* integer number formats - set up the flags and "break" */
269 case 'o':
270 base = 8;
271 break;
272
273 case 'X':
274 flags |= LARGE;
275 case 'x':
276 base = 16;
277 break;
278
279 case 'd':
280 case 'i':
281 flags |= SIGN;
282 case 'u':
283 break;
284
285 default:
286 tx_byte('%'), count++;
287 if (*fmt)
288 tx_byte(*fmt), count++;
289 else
290 --fmt;
291 continue;
292 }
293 if (qualifier == 'l')
294 num = va_arg(args, unsigned long);
295 else if (qualifier == 'h') {
296 num = (unsigned short) va_arg(args, int);
297 if (flags & SIGN)
298 num = (short) num;
299 } else if (flags & SIGN)
300 num = va_arg(args, int);
301 else
302 num = va_arg(args, unsigned int);
303 count += number(tx_byte, num, base, field_width, precision, flags);
304 }
305 return count;
306}
307
308/* FIXME this global makes vsprintf non-reentrant
309 */
310static char *str_buf;
311static void str_tx_byte(unsigned char byte)
312{
313 *str_buf = byte;
314 str_buf++;
315}
316
317int vsprintf(char * buf, const char *fmt, va_list args)
318{
319 int i;
320 str_buf = buf;
321 i = vtxprintf(str_tx_byte, fmt, args);
322 /* maeder/Ispiri -- The null termination was missing a deference */
323 /* and was just zeroing out the pointer instead */
324 *str_buf = '\0';
325 return i;
326}
327
328int sprintf(char * buf, const char *fmt, ...)
329{
330 va_list args;
331 int i;
332
333 va_start(args, fmt);
334 i=vsprintf(buf,fmt,args);
335 va_end(args);
336 return i;
337}