Optimize andes_clear_page() and andes_copy_page() with prefetch
[linux-2.6/linux-mips.git] / lib / vsprintf.c
blob3367fc3e191ad5d1643ad76027641d49dc464f32
1 /*
2 * linux/lib/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 #include <asm/div64.h>
19 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
21 unsigned long result = 0,value;
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;
34 while (isxdigit(*cp) &&
35 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
36 result = result*base + value;
37 cp++;
39 if (endp)
40 *endp = (char *)cp;
41 return result;
44 long simple_strtol(const char *cp,char **endp,unsigned int base)
46 if(*cp=='-')
47 return -simple_strtoul(cp+1,endp,base);
48 return simple_strtoul(cp,endp,base);
51 static int skip_atoi(const char **s)
53 int i=0;
55 while (isdigit(**s))
56 i = i*10 + *((*s)++) - '0';
57 return i;
60 #define ZEROPAD 1 /* pad with zero */
61 #define SIGN 2 /* unsigned/signed long */
62 #define PLUS 4 /* show plus */
63 #define SPACE 8 /* space if plus */
64 #define LEFT 16 /* left justified */
65 #define SPECIAL 32 /* 0x */
66 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
68 static char * number(char * str, long long num, int base, int size, int precision, int type)
70 char c,sign,tmp[66];
71 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
72 int i;
74 if (type & LARGE)
75 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
76 if (type & LEFT)
77 type &= ~ZEROPAD;
78 if (base < 2 || base > 36)
79 return 0;
80 c = (type & ZEROPAD) ? '0' : ' ';
81 sign = 0;
82 if (type & SIGN) {
83 if (num < 0) {
84 sign = '-';
85 num = -num;
86 size--;
87 } else if (type & PLUS) {
88 sign = '+';
89 size--;
90 } else if (type & SPACE) {
91 sign = ' ';
92 size--;
95 if (type & SPECIAL) {
96 if (base == 16)
97 size -= 2;
98 else if (base == 8)
99 size--;
101 i = 0;
102 if (num == 0)
103 tmp[i++]='0';
104 else while (num != 0)
105 tmp[i++] = digits[do_div(num,base)];
106 if (i > precision)
107 precision = i;
108 size -= precision;
109 if (!(type&(ZEROPAD+LEFT)))
110 while(size-->0)
111 *str++ = ' ';
112 if (sign)
113 *str++ = sign;
114 if (type & SPECIAL) {
115 if (base==8)
116 *str++ = '0';
117 else if (base==16) {
118 *str++ = '0';
119 *str++ = digits[33];
122 if (!(type & LEFT))
123 while (size-- > 0)
124 *str++ = c;
125 while (i < precision--)
126 *str++ = '0';
127 while (i-- > 0)
128 *str++ = tmp[i];
129 while (size-- > 0)
130 *str++ = ' ';
131 return str;
134 /* Forward decl. needed for IP address printing stuff... */
135 int sprintf(char * buf, const char *fmt, ...);
137 int vsprintf(char *buf, const char *fmt, va_list args)
139 int len;
140 unsigned long long num;
141 int i, base;
142 char * str;
143 const char *s;
145 int flags; /* flags to number() */
147 int field_width; /* width of output field */
148 int precision; /* min. # of digits for integers; max
149 number of chars for from string */
150 int qualifier; /* 'h', 'l', or 'L' for integer fields */
151 /* 'z' support added 23/7/1999 S.H. */
152 /* 'z' changed to 'Z' --davidm 1/25/99 */
155 for (str=buf ; *fmt ; ++fmt) {
156 if (*fmt != '%') {
157 *str++ = *fmt;
158 continue;
161 /* process flags */
162 flags = 0;
163 repeat:
164 ++fmt; /* this also skips first '%' */
165 switch (*fmt) {
166 case '-': flags |= LEFT; goto repeat;
167 case '+': flags |= PLUS; goto repeat;
168 case ' ': flags |= SPACE; goto repeat;
169 case '#': flags |= SPECIAL; goto repeat;
170 case '0': flags |= ZEROPAD; goto repeat;
173 /* get field width */
174 field_width = -1;
175 if (isdigit(*fmt))
176 field_width = skip_atoi(&fmt);
177 else if (*fmt == '*') {
178 ++fmt;
179 /* it's the next argument */
180 field_width = va_arg(args, int);
181 if (field_width < 0) {
182 field_width = -field_width;
183 flags |= LEFT;
187 /* get the precision */
188 precision = -1;
189 if (*fmt == '.') {
190 ++fmt;
191 if (isdigit(*fmt))
192 precision = skip_atoi(&fmt);
193 else if (*fmt == '*') {
194 ++fmt;
195 /* it's the next argument */
196 precision = va_arg(args, int);
198 if (precision < 0)
199 precision = 0;
202 /* get the conversion qualifier */
203 qualifier = -1;
204 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
205 qualifier = *fmt;
206 ++fmt;
209 /* default base */
210 base = 10;
212 switch (*fmt) {
213 case 'c':
214 if (!(flags & LEFT))
215 while (--field_width > 0)
216 *str++ = ' ';
217 *str++ = (unsigned char) va_arg(args, int);
218 while (--field_width > 0)
219 *str++ = ' ';
220 continue;
222 case 's':
223 s = va_arg(args, char *);
224 if (!s)
225 s = "<NULL>";
227 len = strnlen(s, precision);
229 if (!(flags & LEFT))
230 while (len < field_width--)
231 *str++ = ' ';
232 for (i = 0; i < len; ++i)
233 *str++ = *s++;
234 while (len < field_width--)
235 *str++ = ' ';
236 continue;
238 case 'p':
239 if (field_width == -1) {
240 field_width = 2*sizeof(void *);
241 flags |= ZEROPAD;
243 str = number(str,
244 (unsigned long) va_arg(args, void *), 16,
245 field_width, precision, flags);
246 continue;
249 case 'n':
250 if (qualifier == 'l') {
251 long * ip = va_arg(args, long *);
252 *ip = (str - buf);
253 } else if (qualifier == 'Z') {
254 size_t * ip = va_arg(args, size_t *);
255 *ip = (str - buf);
256 } else {
257 int * ip = va_arg(args, int *);
258 *ip = (str - buf);
260 continue;
262 case '%':
263 *str++ = '%';
264 continue;
266 /* integer number formats - set up the flags and "break" */
267 case 'o':
268 base = 8;
269 break;
271 case 'X':
272 flags |= LARGE;
273 case 'x':
274 base = 16;
275 break;
277 case 'd':
278 case 'i':
279 flags |= SIGN;
280 case 'u':
281 break;
283 default:
284 *str++ = '%';
285 if (*fmt)
286 *str++ = *fmt;
287 else
288 --fmt;
289 continue;
291 if (qualifier == 'L')
292 num = va_arg(args, long long);
293 else if (qualifier == 'l') {
294 num = va_arg(args, unsigned long);
295 if (flags & SIGN)
296 num = (signed long) num;
297 } else if (qualifier == 'Z') {
298 num = va_arg(args, size_t);
299 } else if (qualifier == 'h') {
300 num = (unsigned short) va_arg(args, int);
301 if (flags & SIGN)
302 num = (signed short) num;
303 } else {
304 num = va_arg(args, unsigned int);
305 if (flags & SIGN)
306 num = (signed int) num;
308 str = number(str, num, base, field_width, precision, flags);
310 *str = '\0';
311 return str-buf;
314 int sprintf(char * buf, const char *fmt, ...)
316 va_list args;
317 int i;
319 va_start(args, fmt);
320 i=vsprintf(buf,fmt,args);
321 va_end(args);
322 return i;