4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
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
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/page.h> /* for PAGE_SIZE */
27 #include <asm/div64.h>
29 /* Works only for digits and letters, but small and fast */
30 #define TOLOWER(x) ((x) | 0x20)
33 * simple_strtoul - convert a string to an unsigned long
34 * @cp: The start of the string
35 * @endp: A pointer to the end of the parsed string will be placed here
36 * @base: The number base to use
38 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
40 unsigned long result = 0,value;
47 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
52 } else if (base == 16) {
53 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
56 while (isxdigit(*cp) &&
57 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
58 result = result*base + value;
66 EXPORT_SYMBOL(simple_strtoul);
69 * simple_strtol - convert a string to a signed long
70 * @cp: The start of the string
71 * @endp: A pointer to the end of the parsed string will be placed here
72 * @base: The number base to use
74 long simple_strtol(const char *cp,char **endp,unsigned int base)
77 return -simple_strtoul(cp+1,endp,base);
78 return simple_strtoul(cp,endp,base);
81 EXPORT_SYMBOL(simple_strtol);
84 * simple_strtoull - convert a string to an unsigned long long
85 * @cp: The start of the string
86 * @endp: A pointer to the end of the parsed string will be placed here
87 * @base: The number base to use
89 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
91 unsigned long long result = 0,value;
98 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
103 } else if (base == 16) {
104 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 && (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
109 result = result*base + value;
117 EXPORT_SYMBOL(simple_strtoull);
120 * simple_strtoll - convert a string to a signed long long
121 * @cp: The start of the string
122 * @endp: A pointer to the end of the parsed string will be placed here
123 * @base: The number base to use
125 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
128 return -simple_strtoull(cp+1,endp,base);
129 return simple_strtoull(cp,endp,base);
134 * strict_strtoul - convert a string to an unsigned long strictly
135 * @cp: The string to be converted
136 * @base: The number base to use
137 * @res: The converted result value
139 * strict_strtoul converts a string to an unsigned long only if the
140 * string is really an unsigned long string, any string containing
141 * any invalid char at the tail will be rejected and -EINVAL is returned,
142 * only a newline char at the tail is acceptible because people generally
143 * change a module parameter in the following way:
145 * echo 1024 > /sys/module/e1000/parameters/copybreak
147 * echo will append a newline to the tail.
149 * It returns 0 if conversion is successful and *res is set to the converted
150 * value, otherwise it returns -EINVAL and *res is set to 0.
152 * simple_strtoul just ignores the successive invalid characters and
153 * return the converted value of prefix part of the string.
155 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
158 * strict_strtol - convert a string to a long strictly
159 * @cp: The string to be converted
160 * @base: The number base to use
161 * @res: The converted result value
163 * strict_strtol is similiar to strict_strtoul, but it allows the first
164 * character of a string is '-'.
166 * It returns 0 if conversion is successful and *res is set to the converted
167 * value, otherwise it returns -EINVAL and *res is set to 0.
169 int strict_strtol(const char *cp, unsigned int base, long *res);
172 * strict_strtoull - convert a string to an unsigned long long strictly
173 * @cp: The string to be converted
174 * @base: The number base to use
175 * @res: The converted result value
177 * strict_strtoull converts a string to an unsigned long long only if the
178 * string is really an unsigned long long string, any string containing
179 * any invalid char at the tail will be rejected and -EINVAL is returned,
180 * only a newline char at the tail is acceptible because people generally
181 * change a module parameter in the following way:
183 * echo 1024 > /sys/module/e1000/parameters/copybreak
185 * echo will append a newline to the tail of the string.
187 * It returns 0 if conversion is successful and *res is set to the converted
188 * value, otherwise it returns -EINVAL and *res is set to 0.
190 * simple_strtoull just ignores the successive invalid characters and
191 * return the converted value of prefix part of the string.
193 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
196 * strict_strtoll - convert a string to a long long strictly
197 * @cp: The string to be converted
198 * @base: The number base to use
199 * @res: The converted result value
201 * strict_strtoll is similiar to strict_strtoull, but it allows the first
202 * character of a string is '-'.
204 * It returns 0 if conversion is successful and *res is set to the converted
205 * value, otherwise it returns -EINVAL and *res is set to 0.
207 int strict_strtoll(const char *cp, unsigned int base, long long *res);
209 #define define_strict_strtoux(type, valtype) \
210 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
221 val = simple_strtoul(cp, &tail, base); \
222 if ((*tail == '\0') || \
223 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
231 #define define_strict_strtox(type, valtype) \
232 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
236 ret = strict_strtou##type(cp+1, base, res); \
240 ret = strict_strtou##type(cp, base, res); \
245 define_strict_strtoux(l, unsigned long)
246 define_strict_strtox(l, long)
247 define_strict_strtoux(ll, unsigned long long)
248 define_strict_strtox(ll, long long)
250 EXPORT_SYMBOL(strict_strtoul);
251 EXPORT_SYMBOL(strict_strtol);
252 EXPORT_SYMBOL(strict_strtoll);
253 EXPORT_SYMBOL(strict_strtoull);
255 static int skip_atoi(const char **s)
260 i = i*10 + *((*s)++) - '0';
264 /* Decimal conversion is by far the most typical, and is used
265 * for /proc and /sys data. This directly impacts e.g. top performance
266 * with many processes running. We optimize it for speed
268 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
269 * (with permission from the author, Douglas W. Jones). */
271 /* Formats correctly any integer in [0,99999].
272 * Outputs from one to five digits depending on input.
273 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
274 static char* put_dec_trunc(char *buf, unsigned q)
276 unsigned d3, d2, d1, d0;
281 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
282 q = (d0 * 0xcd) >> 11;
284 *buf++ = d0 + '0'; /* least significant digit */
285 d1 = q + 9*d3 + 5*d2 + d1;
287 q = (d1 * 0xcd) >> 11;
289 *buf++ = d1 + '0'; /* next digit */
292 if ((d2 != 0) || (d3 != 0)) {
295 *buf++ = d2 + '0'; /* next digit */
299 q = (d3 * 0xcd) >> 11;
301 *buf++ = d3 + '0'; /* next digit */
303 *buf++ = q + '0'; /* most sign. digit */
309 /* Same with if's removed. Always emits five digits */
310 static char* put_dec_full(char *buf, unsigned q)
312 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
313 /* but anyway, gcc produces better code with full-sized ints */
314 unsigned d3, d2, d1, d0;
319 /* Possible ways to approx. divide by 10 */
320 /* gcc -O2 replaces multiply with shifts and adds */
321 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
322 // (x * 0x67) >> 10: 1100111
323 // (x * 0x34) >> 9: 110100 - same
324 // (x * 0x1a) >> 8: 11010 - same
325 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
327 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
328 q = (d0 * 0xcd) >> 11;
331 d1 = q + 9*d3 + 5*d2 + d1;
332 q = (d1 * 0xcd) >> 11;
342 q = (d3 * 0xcd) >> 11; /* - shorter code */
343 /* q = (d3 * 0x67) >> 10; - would also work */
349 /* No inlining helps gcc to use registers better */
350 static noinline char* put_dec(char *buf, unsigned long long num)
355 return put_dec_trunc(buf, num);
356 rem = do_div(num, 100000);
357 buf = put_dec_full(buf, rem);
361 #define ZEROPAD 1 /* pad with zero */
362 #define SIGN 2 /* unsigned/signed long */
363 #define PLUS 4 /* show plus */
364 #define SPACE 8 /* space if plus */
365 #define LEFT 16 /* left justified */
366 #define SMALL 32 /* Must be 32 == 0x20 */
367 #define SPECIAL 64 /* 0x */
369 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
371 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
372 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
377 int need_pfx = ((type & SPECIAL) && base != 10);
380 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
381 * produces same digits or (maybe lowercased) letters */
382 locase = (type & SMALL);
387 if ((signed long long) num < 0) {
389 num = - (signed long long) num;
391 } else if (type & PLUS) {
394 } else if (type & SPACE) {
405 /* generate full string in tmp[], in reverse order */
409 /* Generic code, for any base:
411 tmp[i++] = (digits[do_div(num,base)] | locase);
414 else if (base != 10) { /* 8 or 16 */
417 if (base == 16) shift = 4;
419 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
422 } else { /* base 10 */
423 i = put_dec(tmp, num) - tmp;
426 /* printing 100 using %2d gives "100", not "00" */
429 /* leading space padding */
431 if (!(type & (ZEROPAD+LEFT))) {
444 /* "0x" / "0" prefix */
451 *buf = ('X' | locase);
455 /* zero or space padding */
456 if (!(type & LEFT)) {
457 char c = (type & ZEROPAD) ? '0' : ' ';
458 while (--size >= 0) {
464 /* hmm even more zero padding? */
465 while (i <= --precision) {
470 /* actual digits of result */
476 /* trailing space padding */
477 while (--size >= 0) {
485 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
489 if ((unsigned long)s < PAGE_SIZE)
492 len = strnlen(s, precision);
494 if (!(flags & LEFT)) {
495 while (len < field_width--) {
501 for (i = 0; i < len; ++i) {
506 while (len < field_width--) {
515 * Show a '%p' thing. A kernel extension is that the '%p' is followed
516 * by an extra set of alphanumeric characters that are extended format
519 * Right now don't actually handle any such, but we will..
521 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
524 if (field_width == -1) {
525 field_width = 2*sizeof(void *);
528 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
532 * vsnprintf - Format a string and place it in a buffer
533 * @buf: The buffer to place the result into
534 * @size: The size of the buffer, including the trailing null space
535 * @fmt: The format string to use
536 * @args: Arguments for the format string
538 * The return value is the number of characters which would
539 * be generated for the given input, excluding the trailing
540 * '\0', as per ISO C99. If you want to have the exact
541 * number of characters written into @buf as return value
542 * (not including the trailing '\0'), use vscnprintf(). If the
543 * return is greater than or equal to @size, the resulting
544 * string is truncated.
546 * Call this function if you are already dealing with a va_list.
547 * You probably want snprintf() instead.
549 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
551 unsigned long long num;
555 int flags; /* flags to number() */
557 int field_width; /* width of output field */
558 int precision; /* min. # of digits for integers; max
559 number of chars for from string */
560 int qualifier; /* 'h', 'l', or 'L' for integer fields */
561 /* 'z' support added 23/7/1999 S.H. */
562 /* 'z' changed to 'Z' --davidm 1/25/99 */
563 /* 't' added for ptrdiff_t */
565 /* Reject out-of-range values early. Large positive sizes are
566 used for unknown buffer sizes. */
567 if (unlikely((int) size < 0)) {
568 /* There can be only one.. */
569 static char warn = 1;
578 /* Make sure end is always >= buf */
584 for (; *fmt ; ++fmt) {
595 ++fmt; /* this also skips first '%' */
597 case '-': flags |= LEFT; goto repeat;
598 case '+': flags |= PLUS; goto repeat;
599 case ' ': flags |= SPACE; goto repeat;
600 case '#': flags |= SPECIAL; goto repeat;
601 case '0': flags |= ZEROPAD; goto repeat;
604 /* get field width */
607 field_width = skip_atoi(&fmt);
608 else if (*fmt == '*') {
610 /* it's the next argument */
611 field_width = va_arg(args, int);
612 if (field_width < 0) {
613 field_width = -field_width;
618 /* get the precision */
623 precision = skip_atoi(&fmt);
624 else if (*fmt == '*') {
626 /* it's the next argument */
627 precision = va_arg(args, int);
633 /* get the conversion qualifier */
635 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
636 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
639 if (qualifier == 'l' && *fmt == 'l') {
650 if (!(flags & LEFT)) {
651 while (--field_width > 0) {
657 c = (unsigned char) va_arg(args, int);
661 while (--field_width > 0) {
669 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
673 str = pointer(fmt+1, str, end,
674 va_arg(args, void *),
675 field_width, precision, flags);
676 /* Skip all alphanumeric pointer suffixes */
677 while (isalnum(fmt[1]))
683 * What does C99 say about the overflow case here? */
684 if (qualifier == 'l') {
685 long * ip = va_arg(args, long *);
687 } else if (qualifier == 'Z' || qualifier == 'z') {
688 size_t * ip = va_arg(args, size_t *);
691 int * ip = va_arg(args, int *);
702 /* integer number formats - set up the flags and "break" */
732 if (qualifier == 'L')
733 num = va_arg(args, long long);
734 else if (qualifier == 'l') {
735 num = va_arg(args, unsigned long);
737 num = (signed long) num;
738 } else if (qualifier == 'Z' || qualifier == 'z') {
739 num = va_arg(args, size_t);
740 } else if (qualifier == 't') {
741 num = va_arg(args, ptrdiff_t);
742 } else if (qualifier == 'h') {
743 num = (unsigned short) va_arg(args, int);
745 num = (signed short) num;
747 num = va_arg(args, unsigned int);
749 num = (signed int) num;
751 str = number(str, end, num, base,
752 field_width, precision, flags);
760 /* the trailing null byte doesn't count towards the total */
764 EXPORT_SYMBOL(vsnprintf);
767 * vscnprintf - Format a string and place it in a buffer
768 * @buf: The buffer to place the result into
769 * @size: The size of the buffer, including the trailing null space
770 * @fmt: The format string to use
771 * @args: Arguments for the format string
773 * The return value is the number of characters which have been written into
774 * the @buf not including the trailing '\0'. If @size is <= 0 the function
777 * Call this function if you are already dealing with a va_list.
778 * You probably want scnprintf() instead.
780 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
784 i=vsnprintf(buf,size,fmt,args);
785 return (i >= size) ? (size - 1) : i;
788 EXPORT_SYMBOL(vscnprintf);
791 * snprintf - Format a string and place it in a buffer
792 * @buf: The buffer to place the result into
793 * @size: The size of the buffer, including the trailing null space
794 * @fmt: The format string to use
795 * @...: Arguments for the format string
797 * The return value is the number of characters which would be
798 * generated for the given input, excluding the trailing null,
799 * as per ISO C99. If the return is greater than or equal to
800 * @size, the resulting string is truncated.
802 int snprintf(char * buf, size_t size, const char *fmt, ...)
808 i=vsnprintf(buf,size,fmt,args);
813 EXPORT_SYMBOL(snprintf);
816 * scnprintf - Format a string and place it in a buffer
817 * @buf: The buffer to place the result into
818 * @size: The size of the buffer, including the trailing null space
819 * @fmt: The format string to use
820 * @...: Arguments for the format string
822 * The return value is the number of characters written into @buf not including
823 * the trailing '\0'. If @size is <= 0 the function returns 0.
826 int scnprintf(char * buf, size_t size, const char *fmt, ...)
832 i = vsnprintf(buf, size, fmt, args);
834 return (i >= size) ? (size - 1) : i;
836 EXPORT_SYMBOL(scnprintf);
839 * vsprintf - Format a string and place it in a buffer
840 * @buf: The buffer to place the result into
841 * @fmt: The format string to use
842 * @args: Arguments for the format string
844 * The function returns the number of characters written
845 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
848 * Call this function if you are already dealing with a va_list.
849 * You probably want sprintf() instead.
851 int vsprintf(char *buf, const char *fmt, va_list args)
853 return vsnprintf(buf, INT_MAX, fmt, args);
856 EXPORT_SYMBOL(vsprintf);
859 * sprintf - Format a string and place it in a buffer
860 * @buf: The buffer to place the result into
861 * @fmt: The format string to use
862 * @...: Arguments for the format string
864 * The function returns the number of characters written
865 * into @buf. Use snprintf() or scnprintf() in order to avoid
868 int sprintf(char * buf, const char *fmt, ...)
874 i=vsnprintf(buf, INT_MAX, fmt, args);
879 EXPORT_SYMBOL(sprintf);
882 * vsscanf - Unformat a buffer into a list of arguments
884 * @fmt: format of buffer
887 int vsscanf(const char * buf, const char * fmt, va_list args)
889 const char *str = buf;
898 while(*fmt && *str) {
899 /* skip any white space in format */
900 /* white space in format matchs any amount of
901 * white space, including none, in the input.
904 while (isspace(*fmt))
906 while (isspace(*str))
910 /* anything that is not a conversion must match exactly */
911 if (*fmt != '%' && *fmt) {
912 if (*fmt++ != *str++)
921 /* skip this conversion.
922 * advance both strings to next white space
925 while (!isspace(*fmt) && *fmt)
927 while (!isspace(*str) && *str)
932 /* get field width */
935 field_width = skip_atoi(&fmt);
937 /* get conversion qualifier */
939 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
940 *fmt == 'Z' || *fmt == 'z') {
942 if (unlikely(qualifier == *fmt)) {
943 if (qualifier == 'h') {
946 } else if (qualifier == 'l') {
961 char *s = (char *) va_arg(args,char*);
962 if (field_width == -1)
966 } while (--field_width > 0 && *str);
972 char *s = (char *) va_arg(args, char *);
973 if(field_width == -1)
974 field_width = INT_MAX;
975 /* first, skip leading white space in buffer */
976 while (isspace(*str))
979 /* now copy until next white space */
980 while (*str && !isspace(*str) && field_width--) {
988 /* return number of characters read so far */
990 int *i = (int *)va_arg(args,int*);
1008 /* looking for '%' in str */
1013 /* invalid format; stop here */
1017 /* have some sort of integer conversion.
1018 * first, skip white space in buffer.
1020 while (isspace(*str))
1024 if (is_sign && digit == '-')
1028 || (base == 16 && !isxdigit(digit))
1029 || (base == 10 && !isdigit(digit))
1030 || (base == 8 && (!isdigit(digit) || digit > '7'))
1031 || (base == 0 && !isdigit(digit)))
1035 case 'H': /* that's 'hh' in format */
1037 signed char *s = (signed char *) va_arg(args,signed char *);
1038 *s = (signed char) simple_strtol(str,&next,base);
1040 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1041 *s = (unsigned char) simple_strtoul(str, &next, base);
1046 short *s = (short *) va_arg(args,short *);
1047 *s = (short) simple_strtol(str,&next,base);
1049 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1050 *s = (unsigned short) simple_strtoul(str, &next, base);
1055 long *l = (long *) va_arg(args,long *);
1056 *l = simple_strtol(str,&next,base);
1058 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1059 *l = simple_strtoul(str,&next,base);
1064 long long *l = (long long*) va_arg(args,long long *);
1065 *l = simple_strtoll(str,&next,base);
1067 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1068 *l = simple_strtoull(str,&next,base);
1074 size_t *s = (size_t*) va_arg(args,size_t*);
1075 *s = (size_t) simple_strtoul(str,&next,base);
1080 int *i = (int *) va_arg(args, int*);
1081 *i = (int) simple_strtol(str,&next,base);
1083 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1084 *i = (unsigned int) simple_strtoul(str,&next,base);
1096 * Now we've come all the way through so either the input string or the
1097 * format ended. In the former case, there can be a %n at the current
1098 * position in the format that needs to be filled.
1100 if (*fmt == '%' && *(fmt + 1) == 'n') {
1101 int *p = (int *)va_arg(args, int *);
1108 EXPORT_SYMBOL(vsscanf);
1111 * sscanf - Unformat a buffer into a list of arguments
1112 * @buf: input buffer
1113 * @fmt: formatting of buffer
1114 * @...: resulting arguments
1116 int sscanf(const char * buf, const char * fmt, ...)
1122 i = vsscanf(buf,fmt,args);
1127 EXPORT_SYMBOL(sscanf);