ia64/xen-unstable

view xen/common/vsprintf.c @ 6832:5959fae4722a

Set NE bit for VMX guest CR0. VMCS guest CR0.NE bit must
be set, else it will cause "vm-entry failed".

Signed-off-by: Chengyuan Li <chengyuan.li@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Sep 14 13:37:50 2005 +0000 (2005-09-14)
parents a01199a95070
children b866ed85fad3
line source
1 /*
2 * linux/lib/vsprintf.c
3 *
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 :-)
10 */
12 /*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 */
17 #include <stdarg.h>
18 #include <xen/ctype.h>
19 #include <xen/lib.h>
21 /**
22 * simple_strtoul - convert a string to an unsigned long
23 * @cp: The start of the string
24 * @endp: A pointer to the end of the parsed string will be placed here
25 * @base: The number base to use
26 */
27 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
28 {
29 unsigned long result = 0,value;
31 if (!base) {
32 base = 10;
33 if (*cp == '0') {
34 base = 8;
35 cp++;
36 if ((*cp == 'x') && isxdigit(cp[1])) {
37 cp++;
38 base = 16;
39 }
40 }
41 }
42 while (isxdigit(*cp) &&
43 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
44 result = result*base + value;
45 cp++;
46 }
47 if (endp)
48 *endp = (char *)cp;
49 return result;
50 }
52 /**
53 * simple_strtol - convert a string to a signed long
54 * @cp: The start of the string
55 * @endp: A pointer to the end of the parsed string will be placed here
56 * @base: The number base to use
57 */
58 long simple_strtol(const char *cp,char **endp,unsigned int base)
59 {
60 if(*cp=='-')
61 return -simple_strtoul(cp+1,endp,base);
62 return simple_strtoul(cp,endp,base);
63 }
65 /**
66 * simple_strtoull - convert a string to an unsigned long long
67 * @cp: The start of the string
68 * @endp: A pointer to the end of the parsed string will be placed here
69 * @base: The number base to use
70 */
71 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
72 {
73 unsigned long long result = 0,value;
75 if (!base) {
76 base = 10;
77 if (*cp == '0') {
78 base = 8;
79 cp++;
80 if ((*cp == 'x') && isxdigit(cp[1])) {
81 cp++;
82 base = 16;
83 }
84 }
85 }
86 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
87 ? toupper(*cp) : *cp)-'A'+10) < base) {
88 result = result*base + value;
89 cp++;
90 }
91 if (endp)
92 *endp = (char *)cp;
93 return result;
94 }
96 /**
97 * simple_strtoll - convert a string to a signed long long
98 * @cp: The start of the string
99 * @endp: A pointer to the end of the parsed string will be placed here
100 * @base: The number base to use
101 */
102 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
103 {
104 if(*cp=='-')
105 return -simple_strtoull(cp+1,endp,base);
106 return simple_strtoull(cp,endp,base);
107 }
109 static int skip_atoi(const char **s)
110 {
111 int i=0;
113 while (isdigit(**s))
114 i = i*10 + *((*s)++) - '0';
115 return i;
116 }
118 #define ZEROPAD 1 /* pad with zero */
119 #define SIGN 2 /* unsigned/signed long */
120 #define PLUS 4 /* show plus */
121 #define SPACE 8 /* space if plus */
122 #define LEFT 16 /* left justified */
123 #define SPECIAL 32 /* 0x */
124 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
126 static char * number(char * buf, char * end, long long num, int base, int size, int precision, int type)
127 {
128 char c,sign,tmp[66];
129 const char *digits;
130 const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
131 const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
132 int i;
134 digits = (type & LARGE) ? large_digits : small_digits;
135 if (type & LEFT)
136 type &= ~ZEROPAD;
137 if (base < 2 || base > 36)
138 return buf;
139 c = (type & ZEROPAD) ? '0' : ' ';
140 sign = 0;
141 if (type & SIGN) {
142 if (num < 0) {
143 sign = '-';
144 num = -num;
145 size--;
146 } else if (type & PLUS) {
147 sign = '+';
148 size--;
149 } else if (type & SPACE) {
150 sign = ' ';
151 size--;
152 }
153 }
154 if (type & SPECIAL) {
155 if (base == 16)
156 size -= 2;
157 else if (base == 8)
158 size--;
159 }
160 i = 0;
161 if (num == 0)
162 tmp[i++]='0';
163 else
164 {
165 /* XXX KAF: force unsigned mod and div. */
166 unsigned long long num2=(unsigned long long)num;
167 unsigned int base2=(unsigned int)base;
168 while (num2 != 0) { tmp[i++] = digits[num2%base2]; num2 /= base2; }
169 }
170 if (i > precision)
171 precision = i;
172 size -= precision;
173 if (!(type&(ZEROPAD+LEFT))) {
174 while(size-->0) {
175 if (buf <= end)
176 *buf = ' ';
177 ++buf;
178 }
179 }
180 if (sign) {
181 if (buf <= end)
182 *buf = sign;
183 ++buf;
184 }
185 if (type & SPECIAL) {
186 if (base==8) {
187 if (buf <= end)
188 *buf = '0';
189 ++buf;
190 } else if (base==16) {
191 if (buf <= end)
192 *buf = '0';
193 ++buf;
194 if (buf <= end)
195 *buf = digits[33];
196 ++buf;
197 }
198 }
199 if (!(type & LEFT)) {
200 while (size-- > 0) {
201 if (buf <= end)
202 *buf = c;
203 ++buf;
204 }
205 }
206 while (i < precision--) {
207 if (buf <= end)
208 *buf = '0';
209 ++buf;
210 }
211 while (i-- > 0) {
212 if (buf <= end)
213 *buf = tmp[i];
214 ++buf;
215 }
216 while (size-- > 0) {
217 if (buf <= end)
218 *buf = ' ';
219 ++buf;
220 }
221 return buf;
222 }
224 /**
225 * vsnprintf - Format a string and place it in a buffer
226 * @buf: The buffer to place the result into
227 * @size: The size of the buffer, including the trailing null space
228 * @fmt: The format string to use
229 * @args: Arguments for the format string
230 *
231 * Call this function if you are already dealing with a va_list.
232 * You probably want snprintf instead.
233 */
234 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
235 {
236 int len;
237 unsigned long long num;
238 int i, base;
239 char *str, *end, c;
240 const char *s;
242 int flags; /* flags to number() */
244 int field_width; /* width of output field */
245 int precision; /* min. # of digits for integers; max
246 number of chars for from string */
247 int qualifier; /* 'h', 'l', or 'L' for integer fields */
248 /* 'z' support added 23/7/1999 S.H. */
249 /* 'z' changed to 'Z' --davidm 1/25/99 */
251 str = buf;
252 end = buf + size - 1;
254 if (end < buf - 1) {
255 end = ((void *) -1);
256 size = end - buf + 1;
257 }
259 for (; *fmt ; ++fmt) {
260 if (*fmt != '%') {
261 if (str <= end)
262 *str = *fmt;
263 ++str;
264 continue;
265 }
267 /* process flags */
268 flags = 0;
269 repeat:
270 ++fmt; /* this also skips first '%' */
271 switch (*fmt) {
272 case '-': flags |= LEFT; goto repeat;
273 case '+': flags |= PLUS; goto repeat;
274 case ' ': flags |= SPACE; goto repeat;
275 case '#': flags |= SPECIAL; goto repeat;
276 case '0': flags |= ZEROPAD; goto repeat;
277 }
279 /* get field width */
280 field_width = -1;
281 if (isdigit(*fmt))
282 field_width = skip_atoi(&fmt);
283 else if (*fmt == '*') {
284 ++fmt;
285 /* it's the next argument */
286 field_width = va_arg(args, int);
287 if (field_width < 0) {
288 field_width = -field_width;
289 flags |= LEFT;
290 }
291 }
293 /* get the precision */
294 precision = -1;
295 if (*fmt == '.') {
296 ++fmt;
297 if (isdigit(*fmt))
298 precision = skip_atoi(&fmt);
299 else if (*fmt == '*') {
300 ++fmt;
301 /* it's the next argument */
302 precision = va_arg(args, int);
303 }
304 if (precision < 0)
305 precision = 0;
306 }
308 /* get the conversion qualifier */
309 qualifier = -1;
310 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
311 qualifier = *fmt;
312 ++fmt;
313 if (qualifier == 'l' && *fmt == 'l') {
314 qualifier = 'L';
315 ++fmt;
316 }
317 }
318 if (*fmt == 'q') {
319 qualifier = 'L';
320 ++fmt;
321 }
323 /* default base */
324 base = 10;
326 switch (*fmt) {
327 case 'c':
328 if (!(flags & LEFT)) {
329 while (--field_width > 0) {
330 if (str <= end)
331 *str = ' ';
332 ++str;
333 }
334 }
335 c = (unsigned char) va_arg(args, int);
336 if (str <= end)
337 *str = c;
338 ++str;
339 while (--field_width > 0) {
340 if (str <= end)
341 *str = ' ';
342 ++str;
343 }
344 continue;
346 case 's':
347 s = va_arg(args, char *);
348 if (!s)
349 s = "<NULL>";
351 len = strnlen(s, precision);
353 if (!(flags & LEFT)) {
354 while (len < field_width--) {
355 if (str <= end)
356 *str = ' ';
357 ++str;
358 }
359 }
360 for (i = 0; i < len; ++i) {
361 if (str <= end)
362 *str = *s;
363 ++str; ++s;
364 }
365 while (len < field_width--) {
366 if (str <= end)
367 *str = ' ';
368 ++str;
369 }
370 continue;
372 case 'p':
373 if (field_width == -1) {
374 field_width = 2*sizeof(void *);
375 flags |= ZEROPAD;
376 }
377 str = number(str, end,
378 (unsigned long) va_arg(args, void *),
379 16, field_width, precision, flags);
380 continue;
383 case 'n':
384 /* FIXME:
385 * What does C99 say about the overflow case here? */
386 if (qualifier == 'l') {
387 long * ip = va_arg(args, long *);
388 *ip = (str - buf);
389 } else if (qualifier == 'Z') {
390 size_t * ip = va_arg(args, size_t *);
391 *ip = (str - buf);
392 } else {
393 int * ip = va_arg(args, int *);
394 *ip = (str - buf);
395 }
396 continue;
398 case '%':
399 if (str <= end)
400 *str = '%';
401 ++str;
402 continue;
404 /* integer number formats - set up the flags and "break" */
405 case 'o':
406 base = 8;
407 break;
409 case 'X':
410 flags |= LARGE;
411 case 'x':
412 base = 16;
413 break;
415 case 'd':
416 case 'i':
417 flags |= SIGN;
418 case 'u':
419 break;
421 default:
422 if (str <= end)
423 *str = '%';
424 ++str;
425 if (*fmt) {
426 if (str <= end)
427 *str = *fmt;
428 ++str;
429 } else {
430 --fmt;
431 }
432 continue;
433 }
434 if (qualifier == 'L')
435 num = va_arg(args, long long);
436 else if (qualifier == 'l') {
437 num = va_arg(args, unsigned long);
438 if (flags & SIGN)
439 num = (signed long) num;
440 } else if (qualifier == 'Z') {
441 num = va_arg(args, size_t);
442 } else if (qualifier == 'h') {
443 num = (unsigned short) va_arg(args, int);
444 if (flags & SIGN)
445 num = (signed short) num;
446 } else {
447 num = va_arg(args, unsigned int);
448 if (flags & SIGN)
449 num = (signed int) num;
450 }
452 str = number(str, end, num, base,
453 field_width, precision, flags);
454 }
455 if (str <= end)
456 *str = '\0';
457 else if (size > 0)
458 /* don't write out a null byte if the buf size is zero */
459 *end = '\0';
460 /* the trailing null byte doesn't count towards the total
461 * ++str;
462 */
463 return str-buf;
464 }
466 /**
467 * snprintf - Format a string and place it in a buffer
468 * @buf: The buffer to place the result into
469 * @size: The size of the buffer, including the trailing null space
470 * @fmt: The format string to use
471 * @...: Arguments for the format string
472 */
473 int snprintf(char * buf, size_t size, const char *fmt, ...)
474 {
475 va_list args;
476 int i;
478 va_start(args, fmt);
479 i=vsnprintf(buf,size,fmt,args);
480 va_end(args);
481 return i;
482 }
484 /**
485 * vsprintf - Format a string and place it in a buffer
486 * @buf: The buffer to place the result into
487 * @fmt: The format string to use
488 * @args: Arguments for the format string
489 *
490 * Call this function if you are already dealing with a va_list.
491 * You probably want sprintf instead.
492 */
493 int vsprintf(char *buf, const char *fmt, va_list args)
494 {
495 return vsnprintf(buf, 0xFFFFFFFFUL, fmt, args);
496 }
499 /**
500 * sprintf - Format a string and place it in a buffer
501 * @buf: The buffer to place the result into
502 * @fmt: The format string to use
503 * @...: Arguments for the format string
504 */
505 int sprintf(char * buf, const char *fmt, ...)
506 {
507 va_list args;
508 int i;
510 va_start(args, fmt);
511 i=vsprintf(buf,fmt,args);
512 va_end(args);
513 return i;
514 }
517 /*
518 * Local variables:
519 * mode: C
520 * c-set-style: "BSD"
521 * c-basic-offset: 4
522 * tab-width: 4
523 * indent-tabs-mode: nil
524 * End:
525 */