* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-0.99 / kernel / vsprintf.c
blobef449a2dfdec06f813ff9367f247ae9d31b875ed
1 /*
2 * linux/kernel/vsprintf.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8 /*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
12 #include <stdarg.h>
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
17 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
19 unsigned long result = 0,value;
21 if (!base) {
22 base = 10;
23 if (*cp == '0') {
24 base = 8;
25 cp++;
26 if ((*cp == 'x') && isxdigit(cp[1])) {
27 cp++;
28 base = 16;
32 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
33 ? toupper(*cp) : *cp)-'A'+10) < base) {
34 result = result*base + value;
35 cp++;
37 if (endp)
38 *endp = (char *)cp;
39 return result;
42 /* we use this so that we can do without the ctype library */
43 #define is_digit(c) ((c) >= '0' && (c) <= '9')
45 static int skip_atoi(const char **s)
47 int i=0;
49 while (is_digit(**s))
50 i = i*10 + *((*s)++) - '0';
51 return i;
54 #define ZEROPAD 1 /* pad with zero */
55 #define SIGN 2 /* unsigned/signed long */
56 #define PLUS 4 /* show plus */
57 #define SPACE 8 /* space if plus */
58 #define LEFT 16 /* left justified */
59 #define SPECIAL 32 /* 0x */
60 #define SMALL 64 /* use 'abcdef' instead of 'ABCDEF' */
62 #define do_div(n,base) ({ \
63 int __res; \
64 __asm__("divl %4":"=a" (n),"=d" (__res):"0" (n),"1" (0),"r" (base)); \
65 __res; })
67 static char * number(char * str, int num, int base, int size, int precision
68 ,int type)
70 char c,sign,tmp[36];
71 const char *digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
72 int i;
74 if (type&SMALL) digits="0123456789abcdefghijklmnopqrstuvwxyz";
75 if (type&LEFT) type &= ~ZEROPAD;
76 if (base<2 || base>36)
77 return 0;
78 c = (type & ZEROPAD) ? '0' : ' ' ;
79 if (type&SIGN && num<0) {
80 sign='-';
81 num = -num;
82 } else
83 sign=(type&PLUS) ? '+' : ((type&SPACE) ? ' ' : 0);
84 if (sign) size--;
85 if (type&SPECIAL)
86 if (base==16) size -= 2;
87 else if (base==8) size--;
88 i=0;
89 if (num==0)
90 tmp[i++]='0';
91 else while (num!=0)
92 tmp[i++]=digits[do_div(num,base)];
93 if (i>precision) precision=i;
94 size -= precision;
95 if (!(type&(ZEROPAD+LEFT)))
96 while(size-->0)
97 *str++ = ' ';
98 if (sign)
99 *str++ = sign;
100 if (type&SPECIAL)
101 if (base==8)
102 *str++ = '0';
103 else if (base==16) {
104 *str++ = '0';
105 *str++ = digits[33];
107 if (!(type&LEFT))
108 while(size-->0)
109 *str++ = c;
110 while(i<precision--)
111 *str++ = '0';
112 while(i-->0)
113 *str++ = tmp[i];
114 while(size-->0)
115 *str++ = ' ';
116 return str;
119 int vsprintf(char *buf, const char *fmt, va_list args)
121 int len;
122 int i;
123 char * str;
124 char *s;
125 int *ip;
127 int flags; /* flags to number() */
129 int field_width; /* width of output field */
130 int precision; /* min. # of digits for integers; max
131 number of chars for from string */
132 int qualifier; /* 'h', 'l', or 'L' for integer fields */
134 for (str=buf ; *fmt ; ++fmt) {
135 if (*fmt != '%') {
136 *str++ = *fmt;
137 continue;
140 /* process flags */
141 flags = 0;
142 repeat:
143 ++fmt; /* this also skips first '%' */
144 switch (*fmt) {
145 case '-': flags |= LEFT; goto repeat;
146 case '+': flags |= PLUS; goto repeat;
147 case ' ': flags |= SPACE; goto repeat;
148 case '#': flags |= SPECIAL; goto repeat;
149 case '0': flags |= ZEROPAD; goto repeat;
152 /* get field width */
153 field_width = -1;
154 if (is_digit(*fmt))
155 field_width = skip_atoi(&fmt);
156 else if (*fmt == '*') {
157 /* it's the next argument */
158 field_width = va_arg(args, int);
159 if (field_width < 0) {
160 field_width = -field_width;
161 flags |= LEFT;
165 /* get the precision */
166 precision = -1;
167 if (*fmt == '.') {
168 ++fmt;
169 if (is_digit(*fmt))
170 precision = skip_atoi(&fmt);
171 else if (*fmt == '*') {
172 /* it's the next argument */
173 precision = va_arg(args, int);
175 if (precision < 0)
176 precision = 0;
179 /* get the conversion qualifier */
180 qualifier = -1;
181 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
182 qualifier = *fmt;
183 ++fmt;
186 switch (*fmt) {
187 case 'c':
188 if (!(flags & LEFT))
189 while (--field_width > 0)
190 *str++ = ' ';
191 *str++ = (unsigned char) va_arg(args, int);
192 while (--field_width > 0)
193 *str++ = ' ';
194 break;
196 case 's':
197 s = va_arg(args, char *);
198 if (!s)
199 s = "<NULL>";
200 len = strlen(s);
201 if (precision < 0)
202 precision = len;
203 else if (len > precision)
204 len = precision;
206 if (!(flags & LEFT))
207 while (len < field_width--)
208 *str++ = ' ';
209 for (i = 0; i < len; ++i)
210 *str++ = *s++;
211 while (len < field_width--)
212 *str++ = ' ';
213 break;
215 case 'o':
216 str = number(str, va_arg(args, unsigned long), 8,
217 field_width, precision, flags);
218 break;
220 case 'p':
221 if (field_width == -1) {
222 field_width = 8;
223 flags |= ZEROPAD;
225 str = number(str,
226 (unsigned long) va_arg(args, void *), 16,
227 field_width, precision, flags);
228 break;
230 case 'x':
231 flags |= SMALL;
232 case 'X':
233 str = number(str, va_arg(args, unsigned long), 16,
234 field_width, precision, flags);
235 break;
237 case 'd':
238 case 'i':
239 flags |= SIGN;
240 case 'u':
241 str = number(str, va_arg(args, unsigned long), 10,
242 field_width, precision, flags);
243 break;
245 case 'n':
246 ip = va_arg(args, int *);
247 *ip = (str - buf);
248 break;
250 default:
251 if (*fmt != '%')
252 *str++ = '%';
253 if (*fmt)
254 *str++ = *fmt;
255 else
256 --fmt;
257 break;
260 *str = '\0';
261 return str-buf;
264 int sprintf(char * buf, const char *fmt, ...)
266 va_list args;
267 int i;
269 va_start(args, fmt);
270 i=vsprintf(buf,fmt,args);
271 va_end(args);
272 return i;