debuggers.hg

view xen/common/vsprintf.c @ 3705:4294cfa9fad3

bitkeeper revision 1.1159.212.95 (4204aa0ee0re5Xx1zWrJ9ejxzgRs3w)

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