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>
25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
28 #include <asm/page.h> /* for PAGE_SIZE */
29 #include <asm/div64.h>
30 #include <asm/sections.h> /* for dereference_function_descriptor() */
32 /* Works only for digits and letters, but small and fast */
33 #define TOLOWER(x) ((x) | 0x20)
36 * simple_strtoul - convert a string to an unsigned long
37 * @cp: The start of the string
38 * @endp: A pointer to the end of the parsed string will be placed here
39 * @base: The number base to use
41 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
43 unsigned long result = 0,value;
50 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
55 } else if (base == 16) {
56 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
59 while (isxdigit(*cp) &&
60 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
61 result = result*base + value;
69 EXPORT_SYMBOL(simple_strtoul);
72 * simple_strtol - convert a string to a signed long
73 * @cp: The start of the string
74 * @endp: A pointer to the end of the parsed string will be placed here
75 * @base: The number base to use
77 long simple_strtol(const char *cp,char **endp,unsigned int base)
80 return -simple_strtoul(cp+1,endp,base);
81 return simple_strtoul(cp,endp,base);
84 EXPORT_SYMBOL(simple_strtol);
87 * simple_strtoull - convert a string to an unsigned long long
88 * @cp: The start of the string
89 * @endp: A pointer to the end of the parsed string will be placed here
90 * @base: The number base to use
92 unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
94 unsigned long long result = 0,value;
101 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
106 } else if (base == 16) {
107 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
111 && (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
112 result = result*base + value;
120 EXPORT_SYMBOL(simple_strtoull);
123 * simple_strtoll - convert a string to a signed long long
124 * @cp: The start of the string
125 * @endp: A pointer to the end of the parsed string will be placed here
126 * @base: The number base to use
128 long long simple_strtoll(const char *cp,char **endp,unsigned int base)
131 return -simple_strtoull(cp+1,endp,base);
132 return simple_strtoull(cp,endp,base);
137 * strict_strtoul - convert a string to an unsigned long strictly
138 * @cp: The string to be converted
139 * @base: The number base to use
140 * @res: The converted result value
142 * strict_strtoul converts a string to an unsigned long only if the
143 * string is really an unsigned long string, any string containing
144 * any invalid char at the tail will be rejected and -EINVAL is returned,
145 * only a newline char at the tail is acceptible because people generally
146 * change a module parameter in the following way:
148 * echo 1024 > /sys/module/e1000/parameters/copybreak
150 * echo will append a newline to the tail.
152 * It returns 0 if conversion is successful and *res is set to the converted
153 * value, otherwise it returns -EINVAL and *res is set to 0.
155 * simple_strtoul just ignores the successive invalid characters and
156 * return the converted value of prefix part of the string.
158 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
161 * strict_strtol - convert a string to a long strictly
162 * @cp: The string to be converted
163 * @base: The number base to use
164 * @res: The converted result value
166 * strict_strtol is similiar to strict_strtoul, but it allows the first
167 * character of a string is '-'.
169 * It returns 0 if conversion is successful and *res is set to the converted
170 * value, otherwise it returns -EINVAL and *res is set to 0.
172 int strict_strtol(const char *cp, unsigned int base, long *res);
175 * strict_strtoull - convert a string to an unsigned long long strictly
176 * @cp: The string to be converted
177 * @base: The number base to use
178 * @res: The converted result value
180 * strict_strtoull converts a string to an unsigned long long only if the
181 * string is really an unsigned long long string, any string containing
182 * any invalid char at the tail will be rejected and -EINVAL is returned,
183 * only a newline char at the tail is acceptible because people generally
184 * change a module parameter in the following way:
186 * echo 1024 > /sys/module/e1000/parameters/copybreak
188 * echo will append a newline to the tail of the string.
190 * It returns 0 if conversion is successful and *res is set to the converted
191 * value, otherwise it returns -EINVAL and *res is set to 0.
193 * simple_strtoull just ignores the successive invalid characters and
194 * return the converted value of prefix part of the string.
196 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res);
199 * strict_strtoll - convert a string to a long long strictly
200 * @cp: The string to be converted
201 * @base: The number base to use
202 * @res: The converted result value
204 * strict_strtoll is similiar to strict_strtoull, but it allows the first
205 * character of a string is '-'.
207 * It returns 0 if conversion is successful and *res is set to the converted
208 * value, otherwise it returns -EINVAL and *res is set to 0.
210 int strict_strtoll(const char *cp, unsigned int base, long long *res);
212 #define define_strict_strtoux(type, valtype) \
213 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
224 val = simple_strtou##type(cp, &tail, base); \
225 if ((*tail == '\0') || \
226 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
234 #define define_strict_strtox(type, valtype) \
235 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
239 ret = strict_strtou##type(cp+1, base, res); \
243 ret = strict_strtou##type(cp, base, res); \
248 define_strict_strtoux(l, unsigned long)
249 define_strict_strtox(l, long)
250 define_strict_strtoux(ll, unsigned long long)
251 define_strict_strtox(ll, long long)
253 EXPORT_SYMBOL(strict_strtoul);
254 EXPORT_SYMBOL(strict_strtol);
255 EXPORT_SYMBOL(strict_strtoll);
256 EXPORT_SYMBOL(strict_strtoull);
258 static int skip_atoi(const char **s)
263 i = i*10 + *((*s)++) - '0';
267 /* Decimal conversion is by far the most typical, and is used
268 * for /proc and /sys data. This directly impacts e.g. top performance
269 * with many processes running. We optimize it for speed
271 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
272 * (with permission from the author, Douglas W. Jones). */
274 /* Formats correctly any integer in [0,99999].
275 * Outputs from one to five digits depending on input.
276 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
277 static char* put_dec_trunc(char *buf, unsigned q)
279 unsigned d3, d2, d1, d0;
284 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
285 q = (d0 * 0xcd) >> 11;
287 *buf++ = d0 + '0'; /* least significant digit */
288 d1 = q + 9*d3 + 5*d2 + d1;
290 q = (d1 * 0xcd) >> 11;
292 *buf++ = d1 + '0'; /* next digit */
295 if ((d2 != 0) || (d3 != 0)) {
298 *buf++ = d2 + '0'; /* next digit */
302 q = (d3 * 0xcd) >> 11;
304 *buf++ = d3 + '0'; /* next digit */
306 *buf++ = q + '0'; /* most sign. digit */
312 /* Same with if's removed. Always emits five digits */
313 static char* put_dec_full(char *buf, unsigned q)
315 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
316 /* but anyway, gcc produces better code with full-sized ints */
317 unsigned d3, d2, d1, d0;
322 /* Possible ways to approx. divide by 10 */
323 /* gcc -O2 replaces multiply with shifts and adds */
324 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
325 // (x * 0x67) >> 10: 1100111
326 // (x * 0x34) >> 9: 110100 - same
327 // (x * 0x1a) >> 8: 11010 - same
328 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
330 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
331 q = (d0 * 0xcd) >> 11;
334 d1 = q + 9*d3 + 5*d2 + d1;
335 q = (d1 * 0xcd) >> 11;
345 q = (d3 * 0xcd) >> 11; /* - shorter code */
346 /* q = (d3 * 0x67) >> 10; - would also work */
352 /* No inlining helps gcc to use registers better */
353 static noinline char* put_dec(char *buf, unsigned long long num)
358 return put_dec_trunc(buf, num);
359 rem = do_div(num, 100000);
360 buf = put_dec_full(buf, rem);
364 #define ZEROPAD 1 /* pad with zero */
365 #define SIGN 2 /* unsigned/signed long */
366 #define PLUS 4 /* show plus */
367 #define SPACE 8 /* space if plus */
368 #define LEFT 16 /* left justified */
369 #define SMALL 32 /* Must be 32 == 0x20 */
370 #define SPECIAL 64 /* 0x */
372 static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
374 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
375 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
380 int need_pfx = ((type & SPECIAL) && base != 10);
383 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
384 * produces same digits or (maybe lowercased) letters */
385 locase = (type & SMALL);
390 if ((signed long long) num < 0) {
392 num = - (signed long long) num;
394 } else if (type & PLUS) {
397 } else if (type & SPACE) {
408 /* generate full string in tmp[], in reverse order */
412 /* Generic code, for any base:
414 tmp[i++] = (digits[do_div(num,base)] | locase);
417 else if (base != 10) { /* 8 or 16 */
420 if (base == 16) shift = 4;
422 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
425 } else { /* base 10 */
426 i = put_dec(tmp, num) - tmp;
429 /* printing 100 using %2d gives "100", not "00" */
432 /* leading space padding */
434 if (!(type & (ZEROPAD+LEFT))) {
447 /* "0x" / "0" prefix */
454 *buf = ('X' | locase);
458 /* zero or space padding */
459 if (!(type & LEFT)) {
460 char c = (type & ZEROPAD) ? '0' : ' ';
461 while (--size >= 0) {
467 /* hmm even more zero padding? */
468 while (i <= --precision) {
473 /* actual digits of result */
479 /* trailing space padding */
480 while (--size >= 0) {
488 static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
492 if ((unsigned long)s < PAGE_SIZE)
495 len = strnlen(s, precision);
497 if (!(flags & LEFT)) {
498 while (len < field_width--) {
504 for (i = 0; i < len; ++i) {
509 while (len < field_width--) {
517 static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
519 unsigned long value = (unsigned long) ptr;
520 #ifdef CONFIG_KALLSYMS
521 char sym[KSYM_SYMBOL_LEN];
522 sprint_symbol(sym, value);
523 return string(buf, end, sym, field_width, precision, flags);
525 field_width = 2*sizeof(void *);
526 flags |= SPECIAL | SMALL | ZEROPAD;
527 return number(buf, end, value, 16, field_width, precision, flags);
532 * Show a '%p' thing. A kernel extension is that the '%p' is followed
533 * by an extra set of alphanumeric characters that are extended format
536 * Right now we just handle 'F' (for symbolic Function descriptor pointers)
537 * and 'S' (for Symbolic direct pointers), but this can easily be
538 * extended in the future (network address types etc).
540 * The difference between 'S' and 'F' is that on ia64 and ppc64 function
541 * pointers are really function descriptors, which contain a pointer the
544 static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
548 ptr = dereference_function_descriptor(ptr);
551 return symbol_string(buf, end, ptr, field_width, precision, flags);
554 if (field_width == -1) {
555 field_width = 2*sizeof(void *);
558 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
562 * vsnprintf - Format a string and place it in a buffer
563 * @buf: The buffer to place the result into
564 * @size: The size of the buffer, including the trailing null space
565 * @fmt: The format string to use
566 * @args: Arguments for the format string
568 * The return value is the number of characters which would
569 * be generated for the given input, excluding the trailing
570 * '\0', as per ISO C99. If you want to have the exact
571 * number of characters written into @buf as return value
572 * (not including the trailing '\0'), use vscnprintf(). If the
573 * return is greater than or equal to @size, the resulting
574 * string is truncated.
576 * Call this function if you are already dealing with a va_list.
577 * You probably want snprintf() instead.
579 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
581 unsigned long long num;
585 int flags; /* flags to number() */
587 int field_width; /* width of output field */
588 int precision; /* min. # of digits for integers; max
589 number of chars for from string */
590 int qualifier; /* 'h', 'l', or 'L' for integer fields */
591 /* 'z' support added 23/7/1999 S.H. */
592 /* 'z' changed to 'Z' --davidm 1/25/99 */
593 /* 't' added for ptrdiff_t */
595 /* Reject out-of-range values early. Large positive sizes are
596 used for unknown buffer sizes. */
597 if (unlikely((int) size < 0)) {
598 /* There can be only one.. */
599 static char warn = 1;
608 /* Make sure end is always >= buf */
614 for (; *fmt ; ++fmt) {
625 ++fmt; /* this also skips first '%' */
627 case '-': flags |= LEFT; goto repeat;
628 case '+': flags |= PLUS; goto repeat;
629 case ' ': flags |= SPACE; goto repeat;
630 case '#': flags |= SPECIAL; goto repeat;
631 case '0': flags |= ZEROPAD; goto repeat;
634 /* get field width */
637 field_width = skip_atoi(&fmt);
638 else if (*fmt == '*') {
640 /* it's the next argument */
641 field_width = va_arg(args, int);
642 if (field_width < 0) {
643 field_width = -field_width;
648 /* get the precision */
653 precision = skip_atoi(&fmt);
654 else if (*fmt == '*') {
656 /* it's the next argument */
657 precision = va_arg(args, int);
663 /* get the conversion qualifier */
665 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
666 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
669 if (qualifier == 'l' && *fmt == 'l') {
680 if (!(flags & LEFT)) {
681 while (--field_width > 0) {
687 c = (unsigned char) va_arg(args, int);
691 while (--field_width > 0) {
699 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
703 str = pointer(fmt+1, str, end,
704 va_arg(args, void *),
705 field_width, precision, flags);
706 /* Skip all alphanumeric pointer suffixes */
707 while (isalnum(fmt[1]))
713 * What does C99 say about the overflow case here? */
714 if (qualifier == 'l') {
715 long * ip = va_arg(args, long *);
717 } else if (qualifier == 'Z' || qualifier == 'z') {
718 size_t * ip = va_arg(args, size_t *);
721 int * ip = va_arg(args, int *);
732 /* integer number formats - set up the flags and "break" */
762 if (qualifier == 'L')
763 num = va_arg(args, long long);
764 else if (qualifier == 'l') {
765 num = va_arg(args, unsigned long);
767 num = (signed long) num;
768 } else if (qualifier == 'Z' || qualifier == 'z') {
769 num = va_arg(args, size_t);
770 } else if (qualifier == 't') {
771 num = va_arg(args, ptrdiff_t);
772 } else if (qualifier == 'h') {
773 num = (unsigned short) va_arg(args, int);
775 num = (signed short) num;
777 num = va_arg(args, unsigned int);
779 num = (signed int) num;
781 str = number(str, end, num, base,
782 field_width, precision, flags);
790 /* the trailing null byte doesn't count towards the total */
794 EXPORT_SYMBOL(vsnprintf);
797 * vscnprintf - Format a string and place it in a buffer
798 * @buf: The buffer to place the result into
799 * @size: The size of the buffer, including the trailing null space
800 * @fmt: The format string to use
801 * @args: Arguments for the format string
803 * The return value is the number of characters which have been written into
804 * the @buf not including the trailing '\0'. If @size is <= 0 the function
807 * Call this function if you are already dealing with a va_list.
808 * You probably want scnprintf() instead.
810 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
814 i=vsnprintf(buf,size,fmt,args);
815 return (i >= size) ? (size - 1) : i;
818 EXPORT_SYMBOL(vscnprintf);
821 * snprintf - Format a string and place it in a buffer
822 * @buf: The buffer to place the result into
823 * @size: The size of the buffer, including the trailing null space
824 * @fmt: The format string to use
825 * @...: Arguments for the format string
827 * The return value is the number of characters which would be
828 * generated for the given input, excluding the trailing null,
829 * as per ISO C99. If the return is greater than or equal to
830 * @size, the resulting string is truncated.
832 int snprintf(char * buf, size_t size, const char *fmt, ...)
838 i=vsnprintf(buf,size,fmt,args);
843 EXPORT_SYMBOL(snprintf);
846 * scnprintf - Format a string and place it in a buffer
847 * @buf: The buffer to place the result into
848 * @size: The size of the buffer, including the trailing null space
849 * @fmt: The format string to use
850 * @...: Arguments for the format string
852 * The return value is the number of characters written into @buf not including
853 * the trailing '\0'. If @size is <= 0 the function returns 0.
856 int scnprintf(char * buf, size_t size, const char *fmt, ...)
862 i = vsnprintf(buf, size, fmt, args);
864 return (i >= size) ? (size - 1) : i;
866 EXPORT_SYMBOL(scnprintf);
869 * vsprintf - Format a string and place it in a buffer
870 * @buf: The buffer to place the result into
871 * @fmt: The format string to use
872 * @args: Arguments for the format string
874 * The function returns the number of characters written
875 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
878 * Call this function if you are already dealing with a va_list.
879 * You probably want sprintf() instead.
881 int vsprintf(char *buf, const char *fmt, va_list args)
883 return vsnprintf(buf, INT_MAX, fmt, args);
886 EXPORT_SYMBOL(vsprintf);
889 * sprintf - Format a string and place it in a buffer
890 * @buf: The buffer to place the result into
891 * @fmt: The format string to use
892 * @...: Arguments for the format string
894 * The function returns the number of characters written
895 * into @buf. Use snprintf() or scnprintf() in order to avoid
898 int sprintf(char * buf, const char *fmt, ...)
904 i=vsnprintf(buf, INT_MAX, fmt, args);
909 EXPORT_SYMBOL(sprintf);
912 * vsscanf - Unformat a buffer into a list of arguments
914 * @fmt: format of buffer
917 int vsscanf(const char * buf, const char * fmt, va_list args)
919 const char *str = buf;
928 while(*fmt && *str) {
929 /* skip any white space in format */
930 /* white space in format matchs any amount of
931 * white space, including none, in the input.
934 while (isspace(*fmt))
936 while (isspace(*str))
940 /* anything that is not a conversion must match exactly */
941 if (*fmt != '%' && *fmt) {
942 if (*fmt++ != *str++)
951 /* skip this conversion.
952 * advance both strings to next white space
955 while (!isspace(*fmt) && *fmt)
957 while (!isspace(*str) && *str)
962 /* get field width */
965 field_width = skip_atoi(&fmt);
967 /* get conversion qualifier */
969 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
970 *fmt == 'Z' || *fmt == 'z') {
972 if (unlikely(qualifier == *fmt)) {
973 if (qualifier == 'h') {
976 } else if (qualifier == 'l') {
991 char *s = (char *) va_arg(args,char*);
992 if (field_width == -1)
996 } while (--field_width > 0 && *str);
1002 char *s = (char *) va_arg(args, char *);
1003 if(field_width == -1)
1004 field_width = INT_MAX;
1005 /* first, skip leading white space in buffer */
1006 while (isspace(*str))
1009 /* now copy until next white space */
1010 while (*str && !isspace(*str) && field_width--) {
1018 /* return number of characters read so far */
1020 int *i = (int *)va_arg(args,int*);
1038 /* looking for '%' in str */
1043 /* invalid format; stop here */
1047 /* have some sort of integer conversion.
1048 * first, skip white space in buffer.
1050 while (isspace(*str))
1054 if (is_sign && digit == '-')
1058 || (base == 16 && !isxdigit(digit))
1059 || (base == 10 && !isdigit(digit))
1060 || (base == 8 && (!isdigit(digit) || digit > '7'))
1061 || (base == 0 && !isdigit(digit)))
1065 case 'H': /* that's 'hh' in format */
1067 signed char *s = (signed char *) va_arg(args,signed char *);
1068 *s = (signed char) simple_strtol(str,&next,base);
1070 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1071 *s = (unsigned char) simple_strtoul(str, &next, base);
1076 short *s = (short *) va_arg(args,short *);
1077 *s = (short) simple_strtol(str,&next,base);
1079 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1080 *s = (unsigned short) simple_strtoul(str, &next, base);
1085 long *l = (long *) va_arg(args,long *);
1086 *l = simple_strtol(str,&next,base);
1088 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1089 *l = simple_strtoul(str,&next,base);
1094 long long *l = (long long*) va_arg(args,long long *);
1095 *l = simple_strtoll(str,&next,base);
1097 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1098 *l = simple_strtoull(str,&next,base);
1104 size_t *s = (size_t*) va_arg(args,size_t*);
1105 *s = (size_t) simple_strtoul(str,&next,base);
1110 int *i = (int *) va_arg(args, int*);
1111 *i = (int) simple_strtol(str,&next,base);
1113 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1114 *i = (unsigned int) simple_strtoul(str,&next,base);
1126 * Now we've come all the way through so either the input string or the
1127 * format ended. In the former case, there can be a %n at the current
1128 * position in the format that needs to be filled.
1130 if (*fmt == '%' && *(fmt + 1) == 'n') {
1131 int *p = (int *)va_arg(args, int *);
1138 EXPORT_SYMBOL(vsscanf);
1141 * sscanf - Unformat a buffer into a list of arguments
1142 * @buf: input buffer
1143 * @fmt: formatting of buffer
1144 * @...: resulting arguments
1146 int sscanf(const char * buf, const char * fmt, ...)
1152 i = vsscanf(buf,fmt,args);
1157 EXPORT_SYMBOL(sscanf);