[PATCH] optimize writer path in time_interpolator_get_counter()
[linux-2.6.22.y-op.git] / lib / vsprintf.c
blobe4e9031dd9c38709b82fd7de2da931ada0cec248
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 /*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
19 #include <stdarg.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/kernel.h>
26 #include <asm/div64.h>
28 /**
29 * simple_strtoul - convert a string to an unsigned long
30 * @cp: The start of the string
31 * @endp: A pointer to the end of the parsed string will be placed here
32 * @base: The number base to use
34 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
36 unsigned long result = 0,value;
38 if (!base) {
39 base = 10;
40 if (*cp == '0') {
41 base = 8;
42 cp++;
43 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
44 cp++;
45 base = 16;
48 } else if (base == 16) {
49 if (cp[0] == '0' && toupper(cp[1]) == 'X')
50 cp += 2;
52 while (isxdigit(*cp) &&
53 (value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
54 result = result*base + value;
55 cp++;
57 if (endp)
58 *endp = (char *)cp;
59 return result;
62 EXPORT_SYMBOL(simple_strtoul);
64 /**
65 * simple_strtol - convert a string to a signed long
66 * @cp: The start of the string
67 * @endp: A pointer to the end of the parsed string will be placed here
68 * @base: The number base to use
70 long simple_strtol(const char *cp,char **endp,unsigned int base)
72 if(*cp=='-')
73 return -simple_strtoul(cp+1,endp,base);
74 return simple_strtoul(cp,endp,base);
77 EXPORT_SYMBOL(simple_strtol);
79 /**
80 * simple_strtoull - convert a string to an unsigned long long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
85 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
87 unsigned long long result = 0,value;
89 if (!base) {
90 base = 10;
91 if (*cp == '0') {
92 base = 8;
93 cp++;
94 if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
95 cp++;
96 base = 16;
99 } else if (base == 16) {
100 if (cp[0] == '0' && toupper(cp[1]) == 'X')
101 cp += 2;
103 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
104 ? toupper(*cp) : *cp)-'A'+10) < base) {
105 result = result*base + value;
106 cp++;
108 if (endp)
109 *endp = (char *)cp;
110 return result;
113 EXPORT_SYMBOL(simple_strtoull);
116 * simple_strtoll - convert a string to a signed long long
117 * @cp: The start of the string
118 * @endp: A pointer to the end of the parsed string will be placed here
119 * @base: The number base to use
121 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
123 if(*cp=='-')
124 return -simple_strtoull(cp+1,endp,base);
125 return simple_strtoull(cp,endp,base);
128 static int skip_atoi(const char **s)
130 int i=0;
132 while (isdigit(**s))
133 i = i*10 + *((*s)++) - '0';
134 return i;
137 #define ZEROPAD 1 /* pad with zero */
138 #define SIGN 2 /* unsigned/signed long */
139 #define PLUS 4 /* show plus */
140 #define SPACE 8 /* space if plus */
141 #define LEFT 16 /* left justified */
142 #define SPECIAL 32 /* 0x */
143 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
145 static char * number(char * buf, char * end, unsigned long long num, int base, int size, int precision, int type)
147 char c,sign,tmp[66];
148 const char *digits;
149 static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
150 static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
151 int i;
153 digits = (type & LARGE) ? large_digits : small_digits;
154 if (type & LEFT)
155 type &= ~ZEROPAD;
156 if (base < 2 || base > 36)
157 return NULL;
158 c = (type & ZEROPAD) ? '0' : ' ';
159 sign = 0;
160 if (type & SIGN) {
161 if ((signed long long) num < 0) {
162 sign = '-';
163 num = - (signed long long) num;
164 size--;
165 } else if (type & PLUS) {
166 sign = '+';
167 size--;
168 } else if (type & SPACE) {
169 sign = ' ';
170 size--;
173 if (type & SPECIAL) {
174 if (base == 16)
175 size -= 2;
176 else if (base == 8)
177 size--;
179 i = 0;
180 if (num == 0)
181 tmp[i++]='0';
182 else while (num != 0)
183 tmp[i++] = digits[do_div(num,base)];
184 if (i > precision)
185 precision = i;
186 size -= precision;
187 if (!(type&(ZEROPAD+LEFT))) {
188 while(size-->0) {
189 if (buf <= end)
190 *buf = ' ';
191 ++buf;
194 if (sign) {
195 if (buf <= end)
196 *buf = sign;
197 ++buf;
199 if (type & SPECIAL) {
200 if (base==8) {
201 if (buf <= end)
202 *buf = '0';
203 ++buf;
204 } else if (base==16) {
205 if (buf <= end)
206 *buf = '0';
207 ++buf;
208 if (buf <= end)
209 *buf = digits[33];
210 ++buf;
213 if (!(type & LEFT)) {
214 while (size-- > 0) {
215 if (buf <= end)
216 *buf = c;
217 ++buf;
220 while (i < precision--) {
221 if (buf <= end)
222 *buf = '0';
223 ++buf;
225 while (i-- > 0) {
226 if (buf <= end)
227 *buf = tmp[i];
228 ++buf;
230 while (size-- > 0) {
231 if (buf <= end)
232 *buf = ' ';
233 ++buf;
235 return buf;
239 * vsnprintf - Format a string and place it in a buffer
240 * @buf: The buffer to place the result into
241 * @size: The size of the buffer, including the trailing null space
242 * @fmt: The format string to use
243 * @args: Arguments for the format string
245 * The return value is the number of characters which would
246 * be generated for the given input, excluding the trailing
247 * '\0', as per ISO C99. If you want to have the exact
248 * number of characters written into @buf as return value
249 * (not including the trailing '\0'), use vscnprintf. If the
250 * return is greater than or equal to @size, the resulting
251 * string is truncated.
253 * Call this function if you are already dealing with a va_list.
254 * You probably want snprintf instead.
256 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
258 int len;
259 unsigned long long num;
260 int i, base;
261 char *str, *end, c;
262 const char *s;
264 int flags; /* flags to number() */
266 int field_width; /* width of output field */
267 int precision; /* min. # of digits for integers; max
268 number of chars for from string */
269 int qualifier; /* 'h', 'l', or 'L' for integer fields */
270 /* 'z' support added 23/7/1999 S.H. */
271 /* 'z' changed to 'Z' --davidm 1/25/99 */
272 /* 't' added for ptrdiff_t */
274 /* Reject out-of-range values early */
275 if (unlikely((int) size < 0)) {
276 /* There can be only one.. */
277 static int warn = 1;
278 WARN_ON(warn);
279 warn = 0;
280 return 0;
283 str = buf;
284 end = buf + size - 1;
286 if (end < buf - 1) {
287 end = ((void *) -1);
288 size = end - buf + 1;
291 for (; *fmt ; ++fmt) {
292 if (*fmt != '%') {
293 if (str <= end)
294 *str = *fmt;
295 ++str;
296 continue;
299 /* process flags */
300 flags = 0;
301 repeat:
302 ++fmt; /* this also skips first '%' */
303 switch (*fmt) {
304 case '-': flags |= LEFT; goto repeat;
305 case '+': flags |= PLUS; goto repeat;
306 case ' ': flags |= SPACE; goto repeat;
307 case '#': flags |= SPECIAL; goto repeat;
308 case '0': flags |= ZEROPAD; goto repeat;
311 /* get field width */
312 field_width = -1;
313 if (isdigit(*fmt))
314 field_width = skip_atoi(&fmt);
315 else if (*fmt == '*') {
316 ++fmt;
317 /* it's the next argument */
318 field_width = va_arg(args, int);
319 if (field_width < 0) {
320 field_width = -field_width;
321 flags |= LEFT;
325 /* get the precision */
326 precision = -1;
327 if (*fmt == '.') {
328 ++fmt;
329 if (isdigit(*fmt))
330 precision = skip_atoi(&fmt);
331 else if (*fmt == '*') {
332 ++fmt;
333 /* it's the next argument */
334 precision = va_arg(args, int);
336 if (precision < 0)
337 precision = 0;
340 /* get the conversion qualifier */
341 qualifier = -1;
342 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
343 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
344 qualifier = *fmt;
345 ++fmt;
346 if (qualifier == 'l' && *fmt == 'l') {
347 qualifier = 'L';
348 ++fmt;
352 /* default base */
353 base = 10;
355 switch (*fmt) {
356 case 'c':
357 if (!(flags & LEFT)) {
358 while (--field_width > 0) {
359 if (str <= end)
360 *str = ' ';
361 ++str;
364 c = (unsigned char) va_arg(args, int);
365 if (str <= end)
366 *str = c;
367 ++str;
368 while (--field_width > 0) {
369 if (str <= end)
370 *str = ' ';
371 ++str;
373 continue;
375 case 's':
376 s = va_arg(args, char *);
377 if ((unsigned long)s < PAGE_SIZE)
378 s = "<NULL>";
380 len = strnlen(s, precision);
382 if (!(flags & LEFT)) {
383 while (len < field_width--) {
384 if (str <= end)
385 *str = ' ';
386 ++str;
389 for (i = 0; i < len; ++i) {
390 if (str <= end)
391 *str = *s;
392 ++str; ++s;
394 while (len < field_width--) {
395 if (str <= end)
396 *str = ' ';
397 ++str;
399 continue;
401 case 'p':
402 if (field_width == -1) {
403 field_width = 2*sizeof(void *);
404 flags |= ZEROPAD;
406 str = number(str, end,
407 (unsigned long) va_arg(args, void *),
408 16, field_width, precision, flags);
409 continue;
412 case 'n':
413 /* FIXME:
414 * What does C99 say about the overflow case here? */
415 if (qualifier == 'l') {
416 long * ip = va_arg(args, long *);
417 *ip = (str - buf);
418 } else if (qualifier == 'Z' || qualifier == 'z') {
419 size_t * ip = va_arg(args, size_t *);
420 *ip = (str - buf);
421 } else {
422 int * ip = va_arg(args, int *);
423 *ip = (str - buf);
425 continue;
427 case '%':
428 if (str <= end)
429 *str = '%';
430 ++str;
431 continue;
433 /* integer number formats - set up the flags and "break" */
434 case 'o':
435 base = 8;
436 break;
438 case 'X':
439 flags |= LARGE;
440 case 'x':
441 base = 16;
442 break;
444 case 'd':
445 case 'i':
446 flags |= SIGN;
447 case 'u':
448 break;
450 default:
451 if (str <= end)
452 *str = '%';
453 ++str;
454 if (*fmt) {
455 if (str <= end)
456 *str = *fmt;
457 ++str;
458 } else {
459 --fmt;
461 continue;
463 if (qualifier == 'L')
464 num = va_arg(args, long long);
465 else if (qualifier == 'l') {
466 num = va_arg(args, unsigned long);
467 if (flags & SIGN)
468 num = (signed long) num;
469 } else if (qualifier == 'Z' || qualifier == 'z') {
470 num = va_arg(args, size_t);
471 } else if (qualifier == 't') {
472 num = va_arg(args, ptrdiff_t);
473 } else if (qualifier == 'h') {
474 num = (unsigned short) va_arg(args, int);
475 if (flags & SIGN)
476 num = (signed short) num;
477 } else {
478 num = va_arg(args, unsigned int);
479 if (flags & SIGN)
480 num = (signed int) num;
482 str = number(str, end, num, base,
483 field_width, precision, flags);
485 if (str <= end)
486 *str = '\0';
487 else if (size > 0)
488 /* don't write out a null byte if the buf size is zero */
489 *end = '\0';
490 /* the trailing null byte doesn't count towards the total
491 * ++str;
493 return str-buf;
496 EXPORT_SYMBOL(vsnprintf);
499 * vscnprintf - Format a string and place it in a buffer
500 * @buf: The buffer to place the result into
501 * @size: The size of the buffer, including the trailing null space
502 * @fmt: The format string to use
503 * @args: Arguments for the format string
505 * The return value is the number of characters which have been written into
506 * the @buf not including the trailing '\0'. If @size is <= 0 the function
507 * returns 0.
509 * Call this function if you are already dealing with a va_list.
510 * You probably want scnprintf instead.
512 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
514 int i;
516 i=vsnprintf(buf,size,fmt,args);
517 return (i >= size) ? (size - 1) : i;
520 EXPORT_SYMBOL(vscnprintf);
523 * snprintf - Format a string and place it in a buffer
524 * @buf: The buffer to place the result into
525 * @size: The size of the buffer, including the trailing null space
526 * @fmt: The format string to use
527 * @...: Arguments for the format string
529 * The return value is the number of characters which would be
530 * generated for the given input, excluding the trailing null,
531 * as per ISO C99. If the return is greater than or equal to
532 * @size, the resulting string is truncated.
534 int snprintf(char * buf, size_t size, const char *fmt, ...)
536 va_list args;
537 int i;
539 va_start(args, fmt);
540 i=vsnprintf(buf,size,fmt,args);
541 va_end(args);
542 return i;
545 EXPORT_SYMBOL(snprintf);
548 * scnprintf - Format a string and place it in a buffer
549 * @buf: The buffer to place the result into
550 * @size: The size of the buffer, including the trailing null space
551 * @fmt: The format string to use
552 * @...: Arguments for the format string
554 * The return value is the number of characters written into @buf not including
555 * the trailing '\0'. If @size is <= 0 the function returns 0. If the return is
556 * greater than or equal to @size, the resulting string is truncated.
559 int scnprintf(char * buf, size_t size, const char *fmt, ...)
561 va_list args;
562 int i;
564 va_start(args, fmt);
565 i = vsnprintf(buf, size, fmt, args);
566 va_end(args);
567 return (i >= size) ? (size - 1) : i;
569 EXPORT_SYMBOL(scnprintf);
572 * vsprintf - Format a string and place it in a buffer
573 * @buf: The buffer to place the result into
574 * @fmt: The format string to use
575 * @args: Arguments for the format string
577 * The function returns the number of characters written
578 * into @buf. Use vsnprintf or vscnprintf in order to avoid
579 * buffer overflows.
581 * Call this function if you are already dealing with a va_list.
582 * You probably want sprintf instead.
584 int vsprintf(char *buf, const char *fmt, va_list args)
586 return vsnprintf(buf, INT_MAX, fmt, args);
589 EXPORT_SYMBOL(vsprintf);
592 * sprintf - Format a string and place it in a buffer
593 * @buf: The buffer to place the result into
594 * @fmt: The format string to use
595 * @...: Arguments for the format string
597 * The function returns the number of characters written
598 * into @buf. Use snprintf or scnprintf in order to avoid
599 * buffer overflows.
601 int sprintf(char * buf, const char *fmt, ...)
603 va_list args;
604 int i;
606 va_start(args, fmt);
607 i=vsnprintf(buf, INT_MAX, fmt, args);
608 va_end(args);
609 return i;
612 EXPORT_SYMBOL(sprintf);
615 * vsscanf - Unformat a buffer into a list of arguments
616 * @buf: input buffer
617 * @fmt: format of buffer
618 * @args: arguments
620 int vsscanf(const char * buf, const char * fmt, va_list args)
622 const char *str = buf;
623 char *next;
624 char digit;
625 int num = 0;
626 int qualifier;
627 int base;
628 int field_width;
629 int is_sign = 0;
631 while(*fmt && *str) {
632 /* skip any white space in format */
633 /* white space in format matchs any amount of
634 * white space, including none, in the input.
636 if (isspace(*fmt)) {
637 while (isspace(*fmt))
638 ++fmt;
639 while (isspace(*str))
640 ++str;
643 /* anything that is not a conversion must match exactly */
644 if (*fmt != '%' && *fmt) {
645 if (*fmt++ != *str++)
646 break;
647 continue;
650 if (!*fmt)
651 break;
652 ++fmt;
654 /* skip this conversion.
655 * advance both strings to next white space
657 if (*fmt == '*') {
658 while (!isspace(*fmt) && *fmt)
659 fmt++;
660 while (!isspace(*str) && *str)
661 str++;
662 continue;
665 /* get field width */
666 field_width = -1;
667 if (isdigit(*fmt))
668 field_width = skip_atoi(&fmt);
670 /* get conversion qualifier */
671 qualifier = -1;
672 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
673 *fmt == 'Z' || *fmt == 'z') {
674 qualifier = *fmt++;
675 if (unlikely(qualifier == *fmt)) {
676 if (qualifier == 'h') {
677 qualifier = 'H';
678 fmt++;
679 } else if (qualifier == 'l') {
680 qualifier = 'L';
681 fmt++;
685 base = 10;
686 is_sign = 0;
688 if (!*fmt || !*str)
689 break;
691 switch(*fmt++) {
692 case 'c':
694 char *s = (char *) va_arg(args,char*);
695 if (field_width == -1)
696 field_width = 1;
697 do {
698 *s++ = *str++;
699 } while (--field_width > 0 && *str);
700 num++;
702 continue;
703 case 's':
705 char *s = (char *) va_arg(args, char *);
706 if(field_width == -1)
707 field_width = INT_MAX;
708 /* first, skip leading white space in buffer */
709 while (isspace(*str))
710 str++;
712 /* now copy until next white space */
713 while (*str && !isspace(*str) && field_width--) {
714 *s++ = *str++;
716 *s = '\0';
717 num++;
719 continue;
720 case 'n':
721 /* return number of characters read so far */
723 int *i = (int *)va_arg(args,int*);
724 *i = str - buf;
726 continue;
727 case 'o':
728 base = 8;
729 break;
730 case 'x':
731 case 'X':
732 base = 16;
733 break;
734 case 'i':
735 base = 0;
736 case 'd':
737 is_sign = 1;
738 case 'u':
739 break;
740 case '%':
741 /* looking for '%' in str */
742 if (*str++ != '%')
743 return num;
744 continue;
745 default:
746 /* invalid format; stop here */
747 return num;
750 /* have some sort of integer conversion.
751 * first, skip white space in buffer.
753 while (isspace(*str))
754 str++;
756 digit = *str;
757 if (is_sign && digit == '-')
758 digit = *(str + 1);
760 if (!digit
761 || (base == 16 && !isxdigit(digit))
762 || (base == 10 && !isdigit(digit))
763 || (base == 8 && (!isdigit(digit) || digit > '7'))
764 || (base == 0 && !isdigit(digit)))
765 break;
767 switch(qualifier) {
768 case 'H': /* that's 'hh' in format */
769 if (is_sign) {
770 signed char *s = (signed char *) va_arg(args,signed char *);
771 *s = (signed char) simple_strtol(str,&next,base);
772 } else {
773 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
774 *s = (unsigned char) simple_strtoul(str, &next, base);
776 break;
777 case 'h':
778 if (is_sign) {
779 short *s = (short *) va_arg(args,short *);
780 *s = (short) simple_strtol(str,&next,base);
781 } else {
782 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
783 *s = (unsigned short) simple_strtoul(str, &next, base);
785 break;
786 case 'l':
787 if (is_sign) {
788 long *l = (long *) va_arg(args,long *);
789 *l = simple_strtol(str,&next,base);
790 } else {
791 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
792 *l = simple_strtoul(str,&next,base);
794 break;
795 case 'L':
796 if (is_sign) {
797 long long *l = (long long*) va_arg(args,long long *);
798 *l = simple_strtoll(str,&next,base);
799 } else {
800 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
801 *l = simple_strtoull(str,&next,base);
803 break;
804 case 'Z':
805 case 'z':
807 size_t *s = (size_t*) va_arg(args,size_t*);
808 *s = (size_t) simple_strtoul(str,&next,base);
810 break;
811 default:
812 if (is_sign) {
813 int *i = (int *) va_arg(args, int*);
814 *i = (int) simple_strtol(str,&next,base);
815 } else {
816 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
817 *i = (unsigned int) simple_strtoul(str,&next,base);
819 break;
821 num++;
823 if (!next)
824 break;
825 str = next;
827 return num;
830 EXPORT_SYMBOL(vsscanf);
833 * sscanf - Unformat a buffer into a list of arguments
834 * @buf: input buffer
835 * @fmt: formatting of buffer
836 * @...: resulting arguments
838 int sscanf(const char * buf, const char * fmt, ...)
840 va_list args;
841 int i;
843 va_start(args,fmt);
844 i = vsscanf(buf,fmt,args);
845 va_end(args);
846 return i;
849 EXPORT_SYMBOL(sscanf);