11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/lib/vsprintf.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Wirzenius wrote this portably, Torvalds fucked it up :-) 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 141da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions 151da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 161da177e4SLinus Torvalds * - scnprintf and vscnprintf 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <stdarg.h> 201da177e4SLinus Torvalds #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/types.h> 221da177e4SLinus Torvalds #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/ctype.h> 241da177e4SLinus Torvalds #include <linux/kernel.h> 250fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 260fe1ef24SLinus Torvalds #include <linux/uaccess.h> 27332d2e78SLinus Torvalds #include <linux/ioport.h> 281da177e4SLinus Torvalds 294e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 301da177e4SLinus Torvalds #include <asm/div64.h> 31deac93dfSJames Bottomley #include <asm/sections.h> /* for dereference_function_descriptor() */ 321da177e4SLinus Torvalds 339b706aeeSDenys Vlasenko /* Works only for digits and letters, but small and fast */ 349b706aeeSDenys Vlasenko #define TOLOWER(x) ((x) | 0x20) 359b706aeeSDenys Vlasenko 36aa46a63eSHarvey Harrison static unsigned int simple_guess_base(const char *cp) 37aa46a63eSHarvey Harrison { 38aa46a63eSHarvey Harrison if (cp[0] == '0') { 39aa46a63eSHarvey Harrison if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) 40aa46a63eSHarvey Harrison return 16; 41aa46a63eSHarvey Harrison else 42aa46a63eSHarvey Harrison return 8; 43aa46a63eSHarvey Harrison } else { 44aa46a63eSHarvey Harrison return 10; 45aa46a63eSHarvey Harrison } 46aa46a63eSHarvey Harrison } 47aa46a63eSHarvey Harrison 481da177e4SLinus Torvalds /** 491da177e4SLinus Torvalds * simple_strtoul - convert a string to an unsigned long 501da177e4SLinus Torvalds * @cp: The start of the string 511da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 521da177e4SLinus Torvalds * @base: The number base to use 531da177e4SLinus Torvalds */ 541da177e4SLinus Torvalds unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 551da177e4SLinus Torvalds { 56aa46a63eSHarvey Harrison unsigned long result = 0; 571da177e4SLinus Torvalds 58aa46a63eSHarvey Harrison if (!base) 59aa46a63eSHarvey Harrison base = simple_guess_base(cp); 60aa46a63eSHarvey Harrison 61aa46a63eSHarvey Harrison if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') 621da177e4SLinus Torvalds cp += 2; 63aa46a63eSHarvey Harrison 64aa46a63eSHarvey Harrison while (isxdigit(*cp)) { 65aa46a63eSHarvey Harrison unsigned int value; 66aa46a63eSHarvey Harrison 67aa46a63eSHarvey Harrison value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; 68aa46a63eSHarvey Harrison if (value >= base) 69aa46a63eSHarvey Harrison break; 701da177e4SLinus Torvalds result = result * base + value; 711da177e4SLinus Torvalds cp++; 721da177e4SLinus Torvalds } 73aa46a63eSHarvey Harrison 741da177e4SLinus Torvalds if (endp) 751da177e4SLinus Torvalds *endp = (char *)cp; 761da177e4SLinus Torvalds return result; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoul); 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /** 811da177e4SLinus Torvalds * simple_strtol - convert a string to a signed long 821da177e4SLinus Torvalds * @cp: The start of the string 831da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 841da177e4SLinus Torvalds * @base: The number base to use 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds long simple_strtol(const char *cp, char **endp, unsigned int base) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds if(*cp == '-') 891da177e4SLinus Torvalds return -simple_strtoul(cp + 1, endp, base); 901da177e4SLinus Torvalds return simple_strtoul(cp, endp, base); 911da177e4SLinus Torvalds } 921da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtol); 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /** 951da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 961da177e4SLinus Torvalds * @cp: The start of the string 971da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 981da177e4SLinus Torvalds * @base: The number base to use 991da177e4SLinus Torvalds */ 1001da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 1011da177e4SLinus Torvalds { 102aa46a63eSHarvey Harrison unsigned long long result = 0; 1031da177e4SLinus Torvalds 104aa46a63eSHarvey Harrison if (!base) 105aa46a63eSHarvey Harrison base = simple_guess_base(cp); 106aa46a63eSHarvey Harrison 107aa46a63eSHarvey Harrison if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') 1081da177e4SLinus Torvalds cp += 2; 109aa46a63eSHarvey Harrison 110aa46a63eSHarvey Harrison while (isxdigit(*cp)) { 111aa46a63eSHarvey Harrison unsigned int value; 112aa46a63eSHarvey Harrison 113aa46a63eSHarvey Harrison value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; 114aa46a63eSHarvey Harrison if (value >= base) 115aa46a63eSHarvey Harrison break; 1161da177e4SLinus Torvalds result = result * base + value; 1171da177e4SLinus Torvalds cp++; 1181da177e4SLinus Torvalds } 119aa46a63eSHarvey Harrison 1201da177e4SLinus Torvalds if (endp) 1211da177e4SLinus Torvalds *endp = (char *)cp; 1221da177e4SLinus Torvalds return result; 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /** 1271da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1281da177e4SLinus Torvalds * @cp: The start of the string 1291da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1301da177e4SLinus Torvalds * @base: The number base to use 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds if(*cp=='-') 1351da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1361da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 13906b2a76dSYi Yang /** 14006b2a76dSYi Yang * strict_strtoul - convert a string to an unsigned long strictly 14106b2a76dSYi Yang * @cp: The string to be converted 14206b2a76dSYi Yang * @base: The number base to use 14306b2a76dSYi Yang * @res: The converted result value 14406b2a76dSYi Yang * 14506b2a76dSYi Yang * strict_strtoul converts a string to an unsigned long only if the 14606b2a76dSYi Yang * string is really an unsigned long string, any string containing 14706b2a76dSYi Yang * any invalid char at the tail will be rejected and -EINVAL is returned, 14806b2a76dSYi Yang * only a newline char at the tail is acceptible because people generally 14906b2a76dSYi Yang * change a module parameter in the following way: 15006b2a76dSYi Yang * 15106b2a76dSYi Yang * echo 1024 > /sys/module/e1000/parameters/copybreak 15206b2a76dSYi Yang * 15306b2a76dSYi Yang * echo will append a newline to the tail. 15406b2a76dSYi Yang * 15506b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 15606b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 15706b2a76dSYi Yang * 15806b2a76dSYi Yang * simple_strtoul just ignores the successive invalid characters and 15906b2a76dSYi Yang * return the converted value of prefix part of the string. 16006b2a76dSYi Yang */ 1619d85db22SHarvey Harrison int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) 1629d85db22SHarvey Harrison { 1639d85db22SHarvey Harrison char *tail; 1649d85db22SHarvey Harrison unsigned long val; 1659d85db22SHarvey Harrison size_t len; 1669d85db22SHarvey Harrison 1679d85db22SHarvey Harrison *res = 0; 1689d85db22SHarvey Harrison len = strlen(cp); 1699d85db22SHarvey Harrison if (len == 0) 1709d85db22SHarvey Harrison return -EINVAL; 1719d85db22SHarvey Harrison 1729d85db22SHarvey Harrison val = simple_strtoul(cp, &tail, base); 1739d85db22SHarvey Harrison if ((*tail == '\0') || 1749d85db22SHarvey Harrison ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { 1759d85db22SHarvey Harrison *res = val; 1769d85db22SHarvey Harrison return 0; 1779d85db22SHarvey Harrison } 1789d85db22SHarvey Harrison 1799d85db22SHarvey Harrison return -EINVAL; 1809d85db22SHarvey Harrison } 1819d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoul); 18206b2a76dSYi Yang 18306b2a76dSYi Yang /** 18406b2a76dSYi Yang * strict_strtol - convert a string to a long strictly 18506b2a76dSYi Yang * @cp: The string to be converted 18606b2a76dSYi Yang * @base: The number base to use 18706b2a76dSYi Yang * @res: The converted result value 18806b2a76dSYi Yang * 18906b2a76dSYi Yang * strict_strtol is similiar to strict_strtoul, but it allows the first 19006b2a76dSYi Yang * character of a string is '-'. 19106b2a76dSYi Yang * 19206b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 19306b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 19406b2a76dSYi Yang */ 1959d85db22SHarvey Harrison int strict_strtol(const char *cp, unsigned int base, long *res) 1969d85db22SHarvey Harrison { 1979d85db22SHarvey Harrison int ret; 1989d85db22SHarvey Harrison if (*cp == '-') { 1999d85db22SHarvey Harrison ret = strict_strtoul(cp + 1, base, (unsigned long *)res); 2009d85db22SHarvey Harrison if (!ret) 2019d85db22SHarvey Harrison *res = -(*res); 2029d85db22SHarvey Harrison } else { 2039d85db22SHarvey Harrison ret = strict_strtoul(cp, base, (unsigned long *)res); 2049d85db22SHarvey Harrison } 2059d85db22SHarvey Harrison 2069d85db22SHarvey Harrison return ret; 2079d85db22SHarvey Harrison } 2089d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtol); 20906b2a76dSYi Yang 21006b2a76dSYi Yang /** 21106b2a76dSYi Yang * strict_strtoull - convert a string to an unsigned long long strictly 21206b2a76dSYi Yang * @cp: The string to be converted 21306b2a76dSYi Yang * @base: The number base to use 21406b2a76dSYi Yang * @res: The converted result value 21506b2a76dSYi Yang * 21606b2a76dSYi Yang * strict_strtoull converts a string to an unsigned long long only if the 21706b2a76dSYi Yang * string is really an unsigned long long string, any string containing 21806b2a76dSYi Yang * any invalid char at the tail will be rejected and -EINVAL is returned, 21906b2a76dSYi Yang * only a newline char at the tail is acceptible because people generally 22006b2a76dSYi Yang * change a module parameter in the following way: 22106b2a76dSYi Yang * 22206b2a76dSYi Yang * echo 1024 > /sys/module/e1000/parameters/copybreak 22306b2a76dSYi Yang * 22406b2a76dSYi Yang * echo will append a newline to the tail of the string. 22506b2a76dSYi Yang * 22606b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 22706b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 22806b2a76dSYi Yang * 22906b2a76dSYi Yang * simple_strtoull just ignores the successive invalid characters and 23006b2a76dSYi Yang * return the converted value of prefix part of the string. 23106b2a76dSYi Yang */ 2329d85db22SHarvey Harrison int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) 2339d85db22SHarvey Harrison { 2349d85db22SHarvey Harrison char *tail; 2359d85db22SHarvey Harrison unsigned long long val; 2369d85db22SHarvey Harrison size_t len; 2379d85db22SHarvey Harrison 2389d85db22SHarvey Harrison *res = 0; 2399d85db22SHarvey Harrison len = strlen(cp); 2409d85db22SHarvey Harrison if (len == 0) 2419d85db22SHarvey Harrison return -EINVAL; 2429d85db22SHarvey Harrison 2439d85db22SHarvey Harrison val = simple_strtoull(cp, &tail, base); 2449d85db22SHarvey Harrison if ((*tail == '\0') || 2459d85db22SHarvey Harrison ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { 2469d85db22SHarvey Harrison *res = val; 2479d85db22SHarvey Harrison return 0; 2489d85db22SHarvey Harrison } 2499d85db22SHarvey Harrison 2509d85db22SHarvey Harrison return -EINVAL; 2519d85db22SHarvey Harrison } 2529d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoull); 25306b2a76dSYi Yang 25406b2a76dSYi Yang /** 25506b2a76dSYi Yang * strict_strtoll - convert a string to a long long strictly 25606b2a76dSYi Yang * @cp: The string to be converted 25706b2a76dSYi Yang * @base: The number base to use 25806b2a76dSYi Yang * @res: The converted result value 25906b2a76dSYi Yang * 26006b2a76dSYi Yang * strict_strtoll is similiar to strict_strtoull, but it allows the first 26106b2a76dSYi Yang * character of a string is '-'. 26206b2a76dSYi Yang * 26306b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 26406b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 26506b2a76dSYi Yang */ 2669d85db22SHarvey Harrison int strict_strtoll(const char *cp, unsigned int base, long long *res) 2679d85db22SHarvey Harrison { 2689d85db22SHarvey Harrison int ret; 2699d85db22SHarvey Harrison if (*cp == '-') { 2709d85db22SHarvey Harrison ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); 2719d85db22SHarvey Harrison if (!ret) 2729d85db22SHarvey Harrison *res = -(*res); 2739d85db22SHarvey Harrison } else { 2749d85db22SHarvey Harrison ret = strict_strtoull(cp, base, (unsigned long long *)res); 2759d85db22SHarvey Harrison } 27606b2a76dSYi Yang 2779d85db22SHarvey Harrison return ret; 2789d85db22SHarvey Harrison } 27906b2a76dSYi Yang EXPORT_SYMBOL(strict_strtoll); 28006b2a76dSYi Yang 2811da177e4SLinus Torvalds static int skip_atoi(const char **s) 2821da177e4SLinus Torvalds { 2831da177e4SLinus Torvalds int i=0; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds while (isdigit(**s)) 2861da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 2871da177e4SLinus Torvalds return i; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2904277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used 2914277eeddSDenis Vlasenko * for /proc and /sys data. This directly impacts e.g. top performance 2924277eeddSDenis Vlasenko * with many processes running. We optimize it for speed 2934277eeddSDenis Vlasenko * using code from 2944277eeddSDenis Vlasenko * http://www.cs.uiowa.edu/~jones/bcd/decimal.html 2954277eeddSDenis Vlasenko * (with permission from the author, Douglas W. Jones). */ 2964277eeddSDenis Vlasenko 2974277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999]. 2984277eeddSDenis Vlasenko * Outputs from one to five digits depending on input. 2994277eeddSDenis Vlasenko * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ 3004277eeddSDenis Vlasenko static char* put_dec_trunc(char *buf, unsigned q) 3014277eeddSDenis Vlasenko { 3024277eeddSDenis Vlasenko unsigned d3, d2, d1, d0; 3034277eeddSDenis Vlasenko d1 = (q>>4) & 0xf; 3044277eeddSDenis Vlasenko d2 = (q>>8) & 0xf; 3054277eeddSDenis Vlasenko d3 = (q>>12); 3064277eeddSDenis Vlasenko 3074277eeddSDenis Vlasenko d0 = 6*(d3 + d2 + d1) + (q & 0xf); 3084277eeddSDenis Vlasenko q = (d0 * 0xcd) >> 11; 3094277eeddSDenis Vlasenko d0 = d0 - 10*q; 3104277eeddSDenis Vlasenko *buf++ = d0 + '0'; /* least significant digit */ 3114277eeddSDenis Vlasenko d1 = q + 9*d3 + 5*d2 + d1; 3124277eeddSDenis Vlasenko if (d1 != 0) { 3134277eeddSDenis Vlasenko q = (d1 * 0xcd) >> 11; 3144277eeddSDenis Vlasenko d1 = d1 - 10*q; 3154277eeddSDenis Vlasenko *buf++ = d1 + '0'; /* next digit */ 3164277eeddSDenis Vlasenko 3174277eeddSDenis Vlasenko d2 = q + 2*d2; 3184277eeddSDenis Vlasenko if ((d2 != 0) || (d3 != 0)) { 3194277eeddSDenis Vlasenko q = (d2 * 0xd) >> 7; 3204277eeddSDenis Vlasenko d2 = d2 - 10*q; 3214277eeddSDenis Vlasenko *buf++ = d2 + '0'; /* next digit */ 3224277eeddSDenis Vlasenko 3234277eeddSDenis Vlasenko d3 = q + 4*d3; 3244277eeddSDenis Vlasenko if (d3 != 0) { 3254277eeddSDenis Vlasenko q = (d3 * 0xcd) >> 11; 3264277eeddSDenis Vlasenko d3 = d3 - 10*q; 3274277eeddSDenis Vlasenko *buf++ = d3 + '0'; /* next digit */ 3284277eeddSDenis Vlasenko if (q != 0) 3294277eeddSDenis Vlasenko *buf++ = q + '0'; /* most sign. digit */ 3304277eeddSDenis Vlasenko } 3314277eeddSDenis Vlasenko } 3324277eeddSDenis Vlasenko } 3334277eeddSDenis Vlasenko return buf; 3344277eeddSDenis Vlasenko } 3354277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */ 3364277eeddSDenis Vlasenko static char* put_dec_full(char *buf, unsigned q) 3374277eeddSDenis Vlasenko { 3384277eeddSDenis Vlasenko /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ 3394277eeddSDenis Vlasenko /* but anyway, gcc produces better code with full-sized ints */ 3404277eeddSDenis Vlasenko unsigned d3, d2, d1, d0; 3414277eeddSDenis Vlasenko d1 = (q>>4) & 0xf; 3424277eeddSDenis Vlasenko d2 = (q>>8) & 0xf; 3434277eeddSDenis Vlasenko d3 = (q>>12); 3444277eeddSDenis Vlasenko 3454277eeddSDenis Vlasenko /* Possible ways to approx. divide by 10 */ 3464277eeddSDenis Vlasenko /* gcc -O2 replaces multiply with shifts and adds */ 3474277eeddSDenis Vlasenko // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) 3484277eeddSDenis Vlasenko // (x * 0x67) >> 10: 1100111 3494277eeddSDenis Vlasenko // (x * 0x34) >> 9: 110100 - same 3504277eeddSDenis Vlasenko // (x * 0x1a) >> 8: 11010 - same 3514277eeddSDenis Vlasenko // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) 3524277eeddSDenis Vlasenko 3534277eeddSDenis Vlasenko d0 = 6*(d3 + d2 + d1) + (q & 0xf); 3544277eeddSDenis Vlasenko q = (d0 * 0xcd) >> 11; 3554277eeddSDenis Vlasenko d0 = d0 - 10*q; 3564277eeddSDenis Vlasenko *buf++ = d0 + '0'; 3574277eeddSDenis Vlasenko d1 = q + 9*d3 + 5*d2 + d1; 3584277eeddSDenis Vlasenko q = (d1 * 0xcd) >> 11; 3594277eeddSDenis Vlasenko d1 = d1 - 10*q; 3604277eeddSDenis Vlasenko *buf++ = d1 + '0'; 3614277eeddSDenis Vlasenko 3624277eeddSDenis Vlasenko d2 = q + 2*d2; 3634277eeddSDenis Vlasenko q = (d2 * 0xd) >> 7; 3644277eeddSDenis Vlasenko d2 = d2 - 10*q; 3654277eeddSDenis Vlasenko *buf++ = d2 + '0'; 3664277eeddSDenis Vlasenko 3674277eeddSDenis Vlasenko d3 = q + 4*d3; 3684277eeddSDenis Vlasenko q = (d3 * 0xcd) >> 11; /* - shorter code */ 3694277eeddSDenis Vlasenko /* q = (d3 * 0x67) >> 10; - would also work */ 3704277eeddSDenis Vlasenko d3 = d3 - 10*q; 3714277eeddSDenis Vlasenko *buf++ = d3 + '0'; 3724277eeddSDenis Vlasenko *buf++ = q + '0'; 3734277eeddSDenis Vlasenko return buf; 3744277eeddSDenis Vlasenko } 3754277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */ 3764277eeddSDenis Vlasenko static noinline char* put_dec(char *buf, unsigned long long num) 3774277eeddSDenis Vlasenko { 3784277eeddSDenis Vlasenko while (1) { 3794277eeddSDenis Vlasenko unsigned rem; 3804277eeddSDenis Vlasenko if (num < 100000) 3814277eeddSDenis Vlasenko return put_dec_trunc(buf, num); 3824277eeddSDenis Vlasenko rem = do_div(num, 100000); 3834277eeddSDenis Vlasenko buf = put_dec_full(buf, rem); 3844277eeddSDenis Vlasenko } 3854277eeddSDenis Vlasenko } 3864277eeddSDenis Vlasenko 3871da177e4SLinus Torvalds #define ZEROPAD 1 /* pad with zero */ 3881da177e4SLinus Torvalds #define SIGN 2 /* unsigned/signed long */ 3891da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3901da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 3911da177e4SLinus Torvalds #define LEFT 16 /* left justified */ 3929b706aeeSDenys Vlasenko #define SMALL 32 /* Must be 32 == 0x20 */ 3939b706aeeSDenys Vlasenko #define SPECIAL 64 /* 0x */ 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type) 3961da177e4SLinus Torvalds { 3979b706aeeSDenys Vlasenko /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ 3989b706aeeSDenys Vlasenko static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ 3999b706aeeSDenys Vlasenko 4009b706aeeSDenys Vlasenko char tmp[66]; 4019b706aeeSDenys Vlasenko char sign; 4029b706aeeSDenys Vlasenko char locase; 403b39a7340SDenis Vlasenko int need_pfx = ((type & SPECIAL) && base != 10); 4041da177e4SLinus Torvalds int i; 4051da177e4SLinus Torvalds 4069b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4079b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 4089b706aeeSDenys Vlasenko locase = (type & SMALL); 4091da177e4SLinus Torvalds if (type & LEFT) 4101da177e4SLinus Torvalds type &= ~ZEROPAD; 4111da177e4SLinus Torvalds sign = 0; 4121da177e4SLinus Torvalds if (type & SIGN) { 4131da177e4SLinus Torvalds if ((signed long long) num < 0) { 4141da177e4SLinus Torvalds sign = '-'; 4151da177e4SLinus Torvalds num = - (signed long long) num; 4161da177e4SLinus Torvalds size--; 4171da177e4SLinus Torvalds } else if (type & PLUS) { 4181da177e4SLinus Torvalds sign = '+'; 4191da177e4SLinus Torvalds size--; 4201da177e4SLinus Torvalds } else if (type & SPACE) { 4211da177e4SLinus Torvalds sign = ' '; 4221da177e4SLinus Torvalds size--; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds } 425b39a7340SDenis Vlasenko if (need_pfx) { 426b39a7340SDenis Vlasenko size--; 4271da177e4SLinus Torvalds if (base == 16) 4281da177e4SLinus Torvalds size--; 4291da177e4SLinus Torvalds } 430b39a7340SDenis Vlasenko 431b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4321da177e4SLinus Torvalds i = 0; 4331da177e4SLinus Torvalds if (num == 0) 4341da177e4SLinus Torvalds tmp[i++] = '0'; 4354277eeddSDenis Vlasenko /* Generic code, for any base: 4364277eeddSDenis Vlasenko else do { 4379b706aeeSDenys Vlasenko tmp[i++] = (digits[do_div(num,base)] | locase); 4384277eeddSDenis Vlasenko } while (num != 0); 4394277eeddSDenis Vlasenko */ 440b39a7340SDenis Vlasenko else if (base != 10) { /* 8 or 16 */ 441b39a7340SDenis Vlasenko int mask = base - 1; 442b39a7340SDenis Vlasenko int shift = 3; 443b39a7340SDenis Vlasenko if (base == 16) shift = 4; 444b39a7340SDenis Vlasenko do { 4459b706aeeSDenys Vlasenko tmp[i++] = (digits[((unsigned char)num) & mask] | locase); 446b39a7340SDenis Vlasenko num >>= shift; 447b39a7340SDenis Vlasenko } while (num); 4484277eeddSDenis Vlasenko } else { /* base 10 */ 4494277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4504277eeddSDenis Vlasenko } 451b39a7340SDenis Vlasenko 452b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 4531da177e4SLinus Torvalds if (i > precision) 4541da177e4SLinus Torvalds precision = i; 455b39a7340SDenis Vlasenko /* leading space padding */ 4561da177e4SLinus Torvalds size -= precision; 4571da177e4SLinus Torvalds if (!(type & (ZEROPAD+LEFT))) { 458b39a7340SDenis Vlasenko while(--size >= 0) { 459f796937aSJeremy Fitzhardinge if (buf < end) 4601da177e4SLinus Torvalds *buf = ' '; 4611da177e4SLinus Torvalds ++buf; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds } 464b39a7340SDenis Vlasenko /* sign */ 4651da177e4SLinus Torvalds if (sign) { 466f796937aSJeremy Fitzhardinge if (buf < end) 4671da177e4SLinus Torvalds *buf = sign; 4681da177e4SLinus Torvalds ++buf; 4691da177e4SLinus Torvalds } 470b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 471b39a7340SDenis Vlasenko if (need_pfx) { 472f796937aSJeremy Fitzhardinge if (buf < end) 4731da177e4SLinus Torvalds *buf = '0'; 4741da177e4SLinus Torvalds ++buf; 475b39a7340SDenis Vlasenko if (base == 16) { 476f796937aSJeremy Fitzhardinge if (buf < end) 4779b706aeeSDenys Vlasenko *buf = ('X' | locase); 4781da177e4SLinus Torvalds ++buf; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds } 481b39a7340SDenis Vlasenko /* zero or space padding */ 4821da177e4SLinus Torvalds if (!(type & LEFT)) { 483b39a7340SDenis Vlasenko char c = (type & ZEROPAD) ? '0' : ' '; 484b39a7340SDenis Vlasenko while (--size >= 0) { 485f796937aSJeremy Fitzhardinge if (buf < end) 4861da177e4SLinus Torvalds *buf = c; 4871da177e4SLinus Torvalds ++buf; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds } 490b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 491b39a7340SDenis Vlasenko while (i <= --precision) { 492f796937aSJeremy Fitzhardinge if (buf < end) 4931da177e4SLinus Torvalds *buf = '0'; 4941da177e4SLinus Torvalds ++buf; 4951da177e4SLinus Torvalds } 496b39a7340SDenis Vlasenko /* actual digits of result */ 497b39a7340SDenis Vlasenko while (--i >= 0) { 498f796937aSJeremy Fitzhardinge if (buf < end) 4991da177e4SLinus Torvalds *buf = tmp[i]; 5001da177e4SLinus Torvalds ++buf; 5011da177e4SLinus Torvalds } 502b39a7340SDenis Vlasenko /* trailing space padding */ 503b39a7340SDenis Vlasenko while (--size >= 0) { 504f796937aSJeremy Fitzhardinge if (buf < end) 5051da177e4SLinus Torvalds *buf = ' '; 5061da177e4SLinus Torvalds ++buf; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds return buf; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5110f9bfa56SLinus Torvalds static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags) 5120f9bfa56SLinus Torvalds { 5130f9bfa56SLinus Torvalds int len, i; 5140f9bfa56SLinus Torvalds 5150f9bfa56SLinus Torvalds if ((unsigned long)s < PAGE_SIZE) 5160f9bfa56SLinus Torvalds s = "<NULL>"; 5170f9bfa56SLinus Torvalds 5180f9bfa56SLinus Torvalds len = strnlen(s, precision); 5190f9bfa56SLinus Torvalds 5200f9bfa56SLinus Torvalds if (!(flags & LEFT)) { 5210f9bfa56SLinus Torvalds while (len < field_width--) { 5220f9bfa56SLinus Torvalds if (buf < end) 5230f9bfa56SLinus Torvalds *buf = ' '; 5240f9bfa56SLinus Torvalds ++buf; 5250f9bfa56SLinus Torvalds } 5260f9bfa56SLinus Torvalds } 5270f9bfa56SLinus Torvalds for (i = 0; i < len; ++i) { 5280f9bfa56SLinus Torvalds if (buf < end) 5290f9bfa56SLinus Torvalds *buf = *s; 5300f9bfa56SLinus Torvalds ++buf; ++s; 5310f9bfa56SLinus Torvalds } 5320f9bfa56SLinus Torvalds while (len < field_width--) { 5330f9bfa56SLinus Torvalds if (buf < end) 5340f9bfa56SLinus Torvalds *buf = ' '; 5350f9bfa56SLinus Torvalds ++buf; 5360f9bfa56SLinus Torvalds } 5370f9bfa56SLinus Torvalds return buf; 5380f9bfa56SLinus Torvalds } 5390f9bfa56SLinus Torvalds 5400fe1ef24SLinus Torvalds static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags) 5410fe1ef24SLinus Torvalds { 5420fe1ef24SLinus Torvalds unsigned long value = (unsigned long) ptr; 5430fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 5440fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 5450fe1ef24SLinus Torvalds sprint_symbol(sym, value); 5460fe1ef24SLinus Torvalds return string(buf, end, sym, field_width, precision, flags); 5470fe1ef24SLinus Torvalds #else 5480fe1ef24SLinus Torvalds field_width = 2*sizeof(void *); 5490fe1ef24SLinus Torvalds flags |= SPECIAL | SMALL | ZEROPAD; 5500fe1ef24SLinus Torvalds return number(buf, end, value, 16, field_width, precision, flags); 5510fe1ef24SLinus Torvalds #endif 5520fe1ef24SLinus Torvalds } 5530fe1ef24SLinus Torvalds 554332d2e78SLinus Torvalds static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags) 555332d2e78SLinus Torvalds { 556332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 557332d2e78SLinus Torvalds #define IO_RSRC_PRINTK_SIZE 4 558332d2e78SLinus Torvalds #endif 559332d2e78SLinus Torvalds 560332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 561332d2e78SLinus Torvalds #define MEM_RSRC_PRINTK_SIZE 8 562332d2e78SLinus Torvalds #endif 563332d2e78SLinus Torvalds 564332d2e78SLinus Torvalds /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ 565332d2e78SLinus Torvalds char sym[4*sizeof(resource_size_t) + 8]; 566332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 567332d2e78SLinus Torvalds int size = -1; 568332d2e78SLinus Torvalds 569332d2e78SLinus Torvalds if (res->flags & IORESOURCE_IO) 570332d2e78SLinus Torvalds size = IO_RSRC_PRINTK_SIZE; 571332d2e78SLinus Torvalds else if (res->flags & IORESOURCE_MEM) 572332d2e78SLinus Torvalds size = MEM_RSRC_PRINTK_SIZE; 573332d2e78SLinus Torvalds 574332d2e78SLinus Torvalds *p++ = '['; 575332d2e78SLinus Torvalds p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD); 576332d2e78SLinus Torvalds *p++ = '-'; 577332d2e78SLinus Torvalds p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD); 578332d2e78SLinus Torvalds *p++ = ']'; 579332d2e78SLinus Torvalds *p = 0; 580332d2e78SLinus Torvalds 581332d2e78SLinus Torvalds return string(buf, end, sym, field_width, precision, flags); 582332d2e78SLinus Torvalds } 583332d2e78SLinus Torvalds 584dd45c9cfSHarvey Harrison static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width, 585dd45c9cfSHarvey Harrison int precision, int flags) 586dd45c9cfSHarvey Harrison { 587dd45c9cfSHarvey Harrison char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */ 588dd45c9cfSHarvey Harrison char *p = mac_addr; 589dd45c9cfSHarvey Harrison int i; 590dd45c9cfSHarvey Harrison 591dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 592dd45c9cfSHarvey Harrison p = pack_hex_byte(p, addr[i]); 593dd45c9cfSHarvey Harrison if (!(flags & SPECIAL) && i != 5) 594dd45c9cfSHarvey Harrison *p++ = ':'; 595dd45c9cfSHarvey Harrison } 596dd45c9cfSHarvey Harrison *p = '\0'; 597dd45c9cfSHarvey Harrison 598dd45c9cfSHarvey Harrison return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL); 599dd45c9cfSHarvey Harrison } 600dd45c9cfSHarvey Harrison 601689afa7dSHarvey Harrison static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, 602689afa7dSHarvey Harrison int precision, int flags) 603689afa7dSHarvey Harrison { 604689afa7dSHarvey Harrison char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ 605689afa7dSHarvey Harrison char *p = ip6_addr; 606689afa7dSHarvey Harrison int i; 607689afa7dSHarvey Harrison 608689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 609689afa7dSHarvey Harrison p = pack_hex_byte(p, addr[2 * i]); 610689afa7dSHarvey Harrison p = pack_hex_byte(p, addr[2 * i + 1]); 611689afa7dSHarvey Harrison if (!(flags & SPECIAL) && i != 7) 612689afa7dSHarvey Harrison *p++ = ':'; 613689afa7dSHarvey Harrison } 614689afa7dSHarvey Harrison *p = '\0'; 615689afa7dSHarvey Harrison 616689afa7dSHarvey Harrison return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL); 617689afa7dSHarvey Harrison } 618689afa7dSHarvey Harrison 6194aa99606SHarvey Harrison static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, 6204aa99606SHarvey Harrison int precision, int flags) 6214aa99606SHarvey Harrison { 6224aa99606SHarvey Harrison char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */ 623b9ac9985SHarvey Harrison char temp[3]; /* hold each IP quad in reverse order */ 6244aa99606SHarvey Harrison char *p = ip4_addr; 625b9ac9985SHarvey Harrison int i, digits; 6264aa99606SHarvey Harrison 6274aa99606SHarvey Harrison for (i = 0; i < 4; i++) { 628b9ac9985SHarvey Harrison digits = put_dec_trunc(temp, addr[i]) - temp; 629b9ac9985SHarvey Harrison /* reverse the digits in the quad */ 630b9ac9985SHarvey Harrison while (digits--) 631b9ac9985SHarvey Harrison *p++ = temp[digits]; 6324aa99606SHarvey Harrison if (i != 3) 6334aa99606SHarvey Harrison *p++ = '.'; 6344aa99606SHarvey Harrison } 6354aa99606SHarvey Harrison *p = '\0'; 6364aa99606SHarvey Harrison 6374aa99606SHarvey Harrison return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL); 6384aa99606SHarvey Harrison } 6394aa99606SHarvey Harrison 6404d8a743cSLinus Torvalds /* 6414d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 6424d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 6434d8a743cSLinus Torvalds * specifiers. 6444d8a743cSLinus Torvalds * 645332d2e78SLinus Torvalds * Right now we handle: 6460fe1ef24SLinus Torvalds * 647332d2e78SLinus Torvalds * - 'F' For symbolic function descriptor pointers 648332d2e78SLinus Torvalds * - 'S' For symbolic direct pointers 649332d2e78SLinus Torvalds * - 'R' For a struct resource pointer, it prints the range of 650332d2e78SLinus Torvalds * addresses (not the name nor the flags) 651dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 652dd45c9cfSHarvey Harrison * usual colon-separated hex notation 6534aa99606SHarvey Harrison * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated 6544aa99606SHarvey Harrison * decimal for v4 and colon separated network-order 16 bit hex for v6) 6554aa99606SHarvey Harrison * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is 6566b9a1066SHarvey Harrison * currently the same 657332d2e78SLinus Torvalds * 658332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 659332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 660332d2e78SLinus Torvalds * pointer to the real address. 6614d8a743cSLinus Torvalds */ 6624d8a743cSLinus Torvalds static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags) 66378a8bf69SLinus Torvalds { 6640fe1ef24SLinus Torvalds switch (*fmt) { 6650fe1ef24SLinus Torvalds case 'F': 6660fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 6670fe1ef24SLinus Torvalds /* Fallthrough */ 6680fe1ef24SLinus Torvalds case 'S': 6690fe1ef24SLinus Torvalds return symbol_string(buf, end, ptr, field_width, precision, flags); 670332d2e78SLinus Torvalds case 'R': 671332d2e78SLinus Torvalds return resource_string(buf, end, ptr, field_width, precision, flags); 672*411c41eeSHarvey Harrison case 'm': 673*411c41eeSHarvey Harrison flags |= SPECIAL; 674*411c41eeSHarvey Harrison /* Fallthrough */ 675dd45c9cfSHarvey Harrison case 'M': 676dd45c9cfSHarvey Harrison return mac_address_string(buf, end, ptr, field_width, precision, flags); 6774aa99606SHarvey Harrison case 'i': 6784aa99606SHarvey Harrison flags |= SPECIAL; 6794aa99606SHarvey Harrison /* Fallthrough */ 6804aa99606SHarvey Harrison case 'I': 6814aa99606SHarvey Harrison if (fmt[1] == '6') 6824aa99606SHarvey Harrison return ip6_addr_string(buf, end, ptr, field_width, precision, flags); 6834aa99606SHarvey Harrison if (fmt[1] == '4') 6844aa99606SHarvey Harrison return ip4_addr_string(buf, end, ptr, field_width, precision, flags); 6854aa99606SHarvey Harrison flags &= ~SPECIAL; 6864aa99606SHarvey Harrison break; 6870fe1ef24SLinus Torvalds } 68878a8bf69SLinus Torvalds flags |= SMALL; 68978a8bf69SLinus Torvalds if (field_width == -1) { 69078a8bf69SLinus Torvalds field_width = 2*sizeof(void *); 69178a8bf69SLinus Torvalds flags |= ZEROPAD; 69278a8bf69SLinus Torvalds } 69378a8bf69SLinus Torvalds return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags); 69478a8bf69SLinus Torvalds } 69578a8bf69SLinus Torvalds 6961da177e4SLinus Torvalds /** 6971da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 6981da177e4SLinus Torvalds * @buf: The buffer to place the result into 6991da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 7001da177e4SLinus Torvalds * @fmt: The format string to use 7011da177e4SLinus Torvalds * @args: Arguments for the format string 7021da177e4SLinus Torvalds * 70320036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 70420036fdcSAndi Kleen * %pS output the name of a text symbol 70520036fdcSAndi Kleen * %pF output the name of a function pointer 706332d2e78SLinus Torvalds * %pR output the address range in a struct resource 70720036fdcSAndi Kleen * 7081da177e4SLinus Torvalds * The return value is the number of characters which would 7091da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 7101da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 7111da177e4SLinus Torvalds * number of characters written into @buf as return value 71272fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 7131da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 7141da177e4SLinus Torvalds * string is truncated. 7151da177e4SLinus Torvalds * 7161da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 71772fd4a35SRobert P. J. Day * You probably want snprintf() instead. 7181da177e4SLinus Torvalds */ 7191da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds unsigned long long num; 7220f9bfa56SLinus Torvalds int base; 7231da177e4SLinus Torvalds char *str, *end, c; 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds int flags; /* flags to number() */ 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds int field_width; /* width of output field */ 7281da177e4SLinus Torvalds int precision; /* min. # of digits for integers; max 7291da177e4SLinus Torvalds number of chars for from string */ 7301da177e4SLinus Torvalds int qualifier; /* 'h', 'l', or 'L' for integer fields */ 7311da177e4SLinus Torvalds /* 'z' support added 23/7/1999 S.H. */ 7321da177e4SLinus Torvalds /* 'z' changed to 'Z' --davidm 1/25/99 */ 73380322306SAl Viro /* 't' added for ptrdiff_t */ 7341da177e4SLinus Torvalds 735f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 736f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 7371da177e4SLinus Torvalds if (unlikely((int) size < 0)) { 7381da177e4SLinus Torvalds /* There can be only one.. */ 739b39a7340SDenis Vlasenko static char warn = 1; 7401da177e4SLinus Torvalds WARN_ON(warn); 7411da177e4SLinus Torvalds warn = 0; 7421da177e4SLinus Torvalds return 0; 7431da177e4SLinus Torvalds } 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds str = buf; 746f796937aSJeremy Fitzhardinge end = buf + size; 7471da177e4SLinus Torvalds 748f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 749f796937aSJeremy Fitzhardinge if (end < buf) { 7501da177e4SLinus Torvalds end = ((void *)-1); 751f796937aSJeremy Fitzhardinge size = end - buf; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds for (; *fmt ; ++fmt) { 7551da177e4SLinus Torvalds if (*fmt != '%') { 756f796937aSJeremy Fitzhardinge if (str < end) 7571da177e4SLinus Torvalds *str = *fmt; 7581da177e4SLinus Torvalds ++str; 7591da177e4SLinus Torvalds continue; 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /* process flags */ 7631da177e4SLinus Torvalds flags = 0; 7641da177e4SLinus Torvalds repeat: 7651da177e4SLinus Torvalds ++fmt; /* this also skips first '%' */ 7661da177e4SLinus Torvalds switch (*fmt) { 7671da177e4SLinus Torvalds case '-': flags |= LEFT; goto repeat; 7681da177e4SLinus Torvalds case '+': flags |= PLUS; goto repeat; 7691da177e4SLinus Torvalds case ' ': flags |= SPACE; goto repeat; 7701da177e4SLinus Torvalds case '#': flags |= SPECIAL; goto repeat; 7711da177e4SLinus Torvalds case '0': flags |= ZEROPAD; goto repeat; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* get field width */ 7751da177e4SLinus Torvalds field_width = -1; 7761da177e4SLinus Torvalds if (isdigit(*fmt)) 7771da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 7781da177e4SLinus Torvalds else if (*fmt == '*') { 7791da177e4SLinus Torvalds ++fmt; 7801da177e4SLinus Torvalds /* it's the next argument */ 7811da177e4SLinus Torvalds field_width = va_arg(args, int); 7821da177e4SLinus Torvalds if (field_width < 0) { 7831da177e4SLinus Torvalds field_width = -field_width; 7841da177e4SLinus Torvalds flags |= LEFT; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* get the precision */ 7891da177e4SLinus Torvalds precision = -1; 7901da177e4SLinus Torvalds if (*fmt == '.') { 7911da177e4SLinus Torvalds ++fmt; 7921da177e4SLinus Torvalds if (isdigit(*fmt)) 7931da177e4SLinus Torvalds precision = skip_atoi(&fmt); 7941da177e4SLinus Torvalds else if (*fmt == '*') { 7951da177e4SLinus Torvalds ++fmt; 7961da177e4SLinus Torvalds /* it's the next argument */ 7971da177e4SLinus Torvalds precision = va_arg(args, int); 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds if (precision < 0) 8001da177e4SLinus Torvalds precision = 0; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* get the conversion qualifier */ 8041da177e4SLinus Torvalds qualifier = -1; 8051da177e4SLinus Torvalds if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || 80680322306SAl Viro *fmt =='Z' || *fmt == 'z' || *fmt == 't') { 8071da177e4SLinus Torvalds qualifier = *fmt; 8081da177e4SLinus Torvalds ++fmt; 8091da177e4SLinus Torvalds if (qualifier == 'l' && *fmt == 'l') { 8101da177e4SLinus Torvalds qualifier = 'L'; 8111da177e4SLinus Torvalds ++fmt; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds /* default base */ 8161da177e4SLinus Torvalds base = 10; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds switch (*fmt) { 8191da177e4SLinus Torvalds case 'c': 8201da177e4SLinus Torvalds if (!(flags & LEFT)) { 8211da177e4SLinus Torvalds while (--field_width > 0) { 822f796937aSJeremy Fitzhardinge if (str < end) 8231da177e4SLinus Torvalds *str = ' '; 8241da177e4SLinus Torvalds ++str; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 828f796937aSJeremy Fitzhardinge if (str < end) 8291da177e4SLinus Torvalds *str = c; 8301da177e4SLinus Torvalds ++str; 8311da177e4SLinus Torvalds while (--field_width > 0) { 832f796937aSJeremy Fitzhardinge if (str < end) 8331da177e4SLinus Torvalds *str = ' '; 8341da177e4SLinus Torvalds ++str; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds continue; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds case 's': 8390f9bfa56SLinus Torvalds str = string(str, end, va_arg(args, char *), field_width, precision, flags); 8401da177e4SLinus Torvalds continue; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds case 'p': 8434d8a743cSLinus Torvalds str = pointer(fmt+1, str, end, 8444d8a743cSLinus Torvalds va_arg(args, void *), 8454d8a743cSLinus Torvalds field_width, precision, flags); 8464d8a743cSLinus Torvalds /* Skip all alphanumeric pointer suffixes */ 8474d8a743cSLinus Torvalds while (isalnum(fmt[1])) 8484d8a743cSLinus Torvalds fmt++; 8491da177e4SLinus Torvalds continue; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds case 'n': 8521da177e4SLinus Torvalds /* FIXME: 8531da177e4SLinus Torvalds * What does C99 say about the overflow case here? */ 8541da177e4SLinus Torvalds if (qualifier == 'l') { 8551da177e4SLinus Torvalds long * ip = va_arg(args, long *); 8561da177e4SLinus Torvalds *ip = (str - buf); 8571da177e4SLinus Torvalds } else if (qualifier == 'Z' || qualifier == 'z') { 8581da177e4SLinus Torvalds size_t * ip = va_arg(args, size_t *); 8591da177e4SLinus Torvalds *ip = (str - buf); 8601da177e4SLinus Torvalds } else { 8611da177e4SLinus Torvalds int * ip = va_arg(args, int *); 8621da177e4SLinus Torvalds *ip = (str - buf); 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds continue; 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds case '%': 867f796937aSJeremy Fitzhardinge if (str < end) 8681da177e4SLinus Torvalds *str = '%'; 8691da177e4SLinus Torvalds ++str; 8701da177e4SLinus Torvalds continue; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds /* integer number formats - set up the flags and "break" */ 8731da177e4SLinus Torvalds case 'o': 8741da177e4SLinus Torvalds base = 8; 8751da177e4SLinus Torvalds break; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds case 'x': 8789b706aeeSDenys Vlasenko flags |= SMALL; 8799b706aeeSDenys Vlasenko case 'X': 8801da177e4SLinus Torvalds base = 16; 8811da177e4SLinus Torvalds break; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds case 'd': 8841da177e4SLinus Torvalds case 'i': 8851da177e4SLinus Torvalds flags |= SIGN; 8861da177e4SLinus Torvalds case 'u': 8871da177e4SLinus Torvalds break; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds default: 890f796937aSJeremy Fitzhardinge if (str < end) 8911da177e4SLinus Torvalds *str = '%'; 8921da177e4SLinus Torvalds ++str; 8931da177e4SLinus Torvalds if (*fmt) { 894f796937aSJeremy Fitzhardinge if (str < end) 8951da177e4SLinus Torvalds *str = *fmt; 8961da177e4SLinus Torvalds ++str; 8971da177e4SLinus Torvalds } else { 8981da177e4SLinus Torvalds --fmt; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds continue; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds if (qualifier == 'L') 9031da177e4SLinus Torvalds num = va_arg(args, long long); 9041da177e4SLinus Torvalds else if (qualifier == 'l') { 9051da177e4SLinus Torvalds num = va_arg(args, unsigned long); 9061da177e4SLinus Torvalds if (flags & SIGN) 9071da177e4SLinus Torvalds num = (signed long) num; 9081da177e4SLinus Torvalds } else if (qualifier == 'Z' || qualifier == 'z') { 9091da177e4SLinus Torvalds num = va_arg(args, size_t); 91080322306SAl Viro } else if (qualifier == 't') { 91180322306SAl Viro num = va_arg(args, ptrdiff_t); 9121da177e4SLinus Torvalds } else if (qualifier == 'h') { 9131da177e4SLinus Torvalds num = (unsigned short) va_arg(args, int); 9141da177e4SLinus Torvalds if (flags & SIGN) 9151da177e4SLinus Torvalds num = (signed short) num; 9161da177e4SLinus Torvalds } else { 9171da177e4SLinus Torvalds num = va_arg(args, unsigned int); 9181da177e4SLinus Torvalds if (flags & SIGN) 9191da177e4SLinus Torvalds num = (signed int) num; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds str = number(str, end, num, base, 9221da177e4SLinus Torvalds field_width, precision, flags); 9231da177e4SLinus Torvalds } 924f796937aSJeremy Fitzhardinge if (size > 0) { 925f796937aSJeremy Fitzhardinge if (str < end) 9261da177e4SLinus Torvalds *str = '\0'; 927f796937aSJeremy Fitzhardinge else 9280a6047eeSLinus Torvalds end[-1] = '\0'; 929f796937aSJeremy Fitzhardinge } 930f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 9311da177e4SLinus Torvalds return str-buf; 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds /** 9361da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 9371da177e4SLinus Torvalds * @buf: The buffer to place the result into 9381da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 9391da177e4SLinus Torvalds * @fmt: The format string to use 9401da177e4SLinus Torvalds * @args: Arguments for the format string 9411da177e4SLinus Torvalds * 9421da177e4SLinus Torvalds * The return value is the number of characters which have been written into 9431da177e4SLinus Torvalds * the @buf not including the trailing '\0'. If @size is <= 0 the function 9441da177e4SLinus Torvalds * returns 0. 9451da177e4SLinus Torvalds * 9461da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 94772fd4a35SRobert P. J. Day * You probably want scnprintf() instead. 94820036fdcSAndi Kleen * 94920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 9521da177e4SLinus Torvalds { 9531da177e4SLinus Torvalds int i; 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds i=vsnprintf(buf,size,fmt,args); 9561da177e4SLinus Torvalds return (i >= size) ? (size - 1) : i; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /** 9611da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 9621da177e4SLinus Torvalds * @buf: The buffer to place the result into 9631da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 9641da177e4SLinus Torvalds * @fmt: The format string to use 9651da177e4SLinus Torvalds * @...: Arguments for the format string 9661da177e4SLinus Torvalds * 9671da177e4SLinus Torvalds * The return value is the number of characters which would be 9681da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 9691da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 9701da177e4SLinus Torvalds * @size, the resulting string is truncated. 97120036fdcSAndi Kleen * 97220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 9731da177e4SLinus Torvalds */ 9741da177e4SLinus Torvalds int snprintf(char * buf, size_t size, const char *fmt, ...) 9751da177e4SLinus Torvalds { 9761da177e4SLinus Torvalds va_list args; 9771da177e4SLinus Torvalds int i; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds va_start(args, fmt); 9801da177e4SLinus Torvalds i=vsnprintf(buf,size,fmt,args); 9811da177e4SLinus Torvalds va_end(args); 9821da177e4SLinus Torvalds return i; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds /** 9871da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 9881da177e4SLinus Torvalds * @buf: The buffer to place the result into 9891da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 9901da177e4SLinus Torvalds * @fmt: The format string to use 9911da177e4SLinus Torvalds * @...: Arguments for the format string 9921da177e4SLinus Torvalds * 9931da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 994ea6f3281SMartin Peschke * the trailing '\0'. If @size is <= 0 the function returns 0. 9951da177e4SLinus Torvalds */ 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds int scnprintf(char * buf, size_t size, const char *fmt, ...) 9981da177e4SLinus Torvalds { 9991da177e4SLinus Torvalds va_list args; 10001da177e4SLinus Torvalds int i; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds va_start(args, fmt); 10031da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 10041da177e4SLinus Torvalds va_end(args); 10051da177e4SLinus Torvalds return (i >= size) ? (size - 1) : i; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /** 10101da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 10111da177e4SLinus Torvalds * @buf: The buffer to place the result into 10121da177e4SLinus Torvalds * @fmt: The format string to use 10131da177e4SLinus Torvalds * @args: Arguments for the format string 10141da177e4SLinus Torvalds * 10151da177e4SLinus Torvalds * The function returns the number of characters written 101672fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 10171da177e4SLinus Torvalds * buffer overflows. 10181da177e4SLinus Torvalds * 10191da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 102072fd4a35SRobert P. J. Day * You probably want sprintf() instead. 102120036fdcSAndi Kleen * 102220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 10231da177e4SLinus Torvalds */ 10241da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 10251da177e4SLinus Torvalds { 10261da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /** 10311da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 10321da177e4SLinus Torvalds * @buf: The buffer to place the result into 10331da177e4SLinus Torvalds * @fmt: The format string to use 10341da177e4SLinus Torvalds * @...: Arguments for the format string 10351da177e4SLinus Torvalds * 10361da177e4SLinus Torvalds * The function returns the number of characters written 103772fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 10381da177e4SLinus Torvalds * buffer overflows. 103920036fdcSAndi Kleen * 104020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 10411da177e4SLinus Torvalds */ 10421da177e4SLinus Torvalds int sprintf(char * buf, const char *fmt, ...) 10431da177e4SLinus Torvalds { 10441da177e4SLinus Torvalds va_list args; 10451da177e4SLinus Torvalds int i; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds va_start(args, fmt); 10481da177e4SLinus Torvalds i=vsnprintf(buf, INT_MAX, fmt, args); 10491da177e4SLinus Torvalds va_end(args); 10501da177e4SLinus Torvalds return i; 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds /** 10551da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 10561da177e4SLinus Torvalds * @buf: input buffer 10571da177e4SLinus Torvalds * @fmt: format of buffer 10581da177e4SLinus Torvalds * @args: arguments 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds int vsscanf(const char * buf, const char * fmt, va_list args) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds const char *str = buf; 10631da177e4SLinus Torvalds char *next; 10641da177e4SLinus Torvalds char digit; 10651da177e4SLinus Torvalds int num = 0; 10661da177e4SLinus Torvalds int qualifier; 10671da177e4SLinus Torvalds int base; 10681da177e4SLinus Torvalds int field_width; 10691da177e4SLinus Torvalds int is_sign = 0; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds while(*fmt && *str) { 10721da177e4SLinus Torvalds /* skip any white space in format */ 10731da177e4SLinus Torvalds /* white space in format matchs any amount of 10741da177e4SLinus Torvalds * white space, including none, in the input. 10751da177e4SLinus Torvalds */ 10761da177e4SLinus Torvalds if (isspace(*fmt)) { 10771da177e4SLinus Torvalds while (isspace(*fmt)) 10781da177e4SLinus Torvalds ++fmt; 10791da177e4SLinus Torvalds while (isspace(*str)) 10801da177e4SLinus Torvalds ++str; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 10841da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 10851da177e4SLinus Torvalds if (*fmt++ != *str++) 10861da177e4SLinus Torvalds break; 10871da177e4SLinus Torvalds continue; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds if (!*fmt) 10911da177e4SLinus Torvalds break; 10921da177e4SLinus Torvalds ++fmt; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds /* skip this conversion. 10951da177e4SLinus Torvalds * advance both strings to next white space 10961da177e4SLinus Torvalds */ 10971da177e4SLinus Torvalds if (*fmt == '*') { 10981da177e4SLinus Torvalds while (!isspace(*fmt) && *fmt) 10991da177e4SLinus Torvalds fmt++; 11001da177e4SLinus Torvalds while (!isspace(*str) && *str) 11011da177e4SLinus Torvalds str++; 11021da177e4SLinus Torvalds continue; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds /* get field width */ 11061da177e4SLinus Torvalds field_width = -1; 11071da177e4SLinus Torvalds if (isdigit(*fmt)) 11081da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds /* get conversion qualifier */ 11111da177e4SLinus Torvalds qualifier = -1; 11121da177e4SLinus Torvalds if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || 11131da177e4SLinus Torvalds *fmt == 'Z' || *fmt == 'z') { 11141da177e4SLinus Torvalds qualifier = *fmt++; 11151da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 11161da177e4SLinus Torvalds if (qualifier == 'h') { 11171da177e4SLinus Torvalds qualifier = 'H'; 11181da177e4SLinus Torvalds fmt++; 11191da177e4SLinus Torvalds } else if (qualifier == 'l') { 11201da177e4SLinus Torvalds qualifier = 'L'; 11211da177e4SLinus Torvalds fmt++; 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds base = 10; 11261da177e4SLinus Torvalds is_sign = 0; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds if (!*fmt || !*str) 11291da177e4SLinus Torvalds break; 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds switch(*fmt++) { 11321da177e4SLinus Torvalds case 'c': 11331da177e4SLinus Torvalds { 11341da177e4SLinus Torvalds char *s = (char *) va_arg(args,char*); 11351da177e4SLinus Torvalds if (field_width == -1) 11361da177e4SLinus Torvalds field_width = 1; 11371da177e4SLinus Torvalds do { 11381da177e4SLinus Torvalds *s++ = *str++; 11391da177e4SLinus Torvalds } while (--field_width > 0 && *str); 11401da177e4SLinus Torvalds num++; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds continue; 11431da177e4SLinus Torvalds case 's': 11441da177e4SLinus Torvalds { 11451da177e4SLinus Torvalds char *s = (char *) va_arg(args, char *); 11461da177e4SLinus Torvalds if(field_width == -1) 11471da177e4SLinus Torvalds field_width = INT_MAX; 11481da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 11491da177e4SLinus Torvalds while (isspace(*str)) 11501da177e4SLinus Torvalds str++; 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* now copy until next white space */ 11531da177e4SLinus Torvalds while (*str && !isspace(*str) && field_width--) { 11541da177e4SLinus Torvalds *s++ = *str++; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds *s = '\0'; 11571da177e4SLinus Torvalds num++; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds continue; 11601da177e4SLinus Torvalds case 'n': 11611da177e4SLinus Torvalds /* return number of characters read so far */ 11621da177e4SLinus Torvalds { 11631da177e4SLinus Torvalds int *i = (int *)va_arg(args,int*); 11641da177e4SLinus Torvalds *i = str - buf; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds continue; 11671da177e4SLinus Torvalds case 'o': 11681da177e4SLinus Torvalds base = 8; 11691da177e4SLinus Torvalds break; 11701da177e4SLinus Torvalds case 'x': 11711da177e4SLinus Torvalds case 'X': 11721da177e4SLinus Torvalds base = 16; 11731da177e4SLinus Torvalds break; 11741da177e4SLinus Torvalds case 'i': 11751da177e4SLinus Torvalds base = 0; 11761da177e4SLinus Torvalds case 'd': 11771da177e4SLinus Torvalds is_sign = 1; 11781da177e4SLinus Torvalds case 'u': 11791da177e4SLinus Torvalds break; 11801da177e4SLinus Torvalds case '%': 11811da177e4SLinus Torvalds /* looking for '%' in str */ 11821da177e4SLinus Torvalds if (*str++ != '%') 11831da177e4SLinus Torvalds return num; 11841da177e4SLinus Torvalds continue; 11851da177e4SLinus Torvalds default: 11861da177e4SLinus Torvalds /* invalid format; stop here */ 11871da177e4SLinus Torvalds return num; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds /* have some sort of integer conversion. 11911da177e4SLinus Torvalds * first, skip white space in buffer. 11921da177e4SLinus Torvalds */ 11931da177e4SLinus Torvalds while (isspace(*str)) 11941da177e4SLinus Torvalds str++; 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds digit = *str; 11971da177e4SLinus Torvalds if (is_sign && digit == '-') 11981da177e4SLinus Torvalds digit = *(str + 1); 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds if (!digit 12011da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 12021da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 12031da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 12041da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 12051da177e4SLinus Torvalds break; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds switch(qualifier) { 12081da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 12091da177e4SLinus Torvalds if (is_sign) { 12101da177e4SLinus Torvalds signed char *s = (signed char *) va_arg(args,signed char *); 12111da177e4SLinus Torvalds *s = (signed char) simple_strtol(str,&next,base); 12121da177e4SLinus Torvalds } else { 12131da177e4SLinus Torvalds unsigned char *s = (unsigned char *) va_arg(args, unsigned char *); 12141da177e4SLinus Torvalds *s = (unsigned char) simple_strtoul(str, &next, base); 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds break; 12171da177e4SLinus Torvalds case 'h': 12181da177e4SLinus Torvalds if (is_sign) { 12191da177e4SLinus Torvalds short *s = (short *) va_arg(args,short *); 12201da177e4SLinus Torvalds *s = (short) simple_strtol(str,&next,base); 12211da177e4SLinus Torvalds } else { 12221da177e4SLinus Torvalds unsigned short *s = (unsigned short *) va_arg(args, unsigned short *); 12231da177e4SLinus Torvalds *s = (unsigned short) simple_strtoul(str, &next, base); 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds break; 12261da177e4SLinus Torvalds case 'l': 12271da177e4SLinus Torvalds if (is_sign) { 12281da177e4SLinus Torvalds long *l = (long *) va_arg(args,long *); 12291da177e4SLinus Torvalds *l = simple_strtol(str,&next,base); 12301da177e4SLinus Torvalds } else { 12311da177e4SLinus Torvalds unsigned long *l = (unsigned long*) va_arg(args,unsigned long*); 12321da177e4SLinus Torvalds *l = simple_strtoul(str,&next,base); 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds break; 12351da177e4SLinus Torvalds case 'L': 12361da177e4SLinus Torvalds if (is_sign) { 12371da177e4SLinus Torvalds long long *l = (long long*) va_arg(args,long long *); 12381da177e4SLinus Torvalds *l = simple_strtoll(str,&next,base); 12391da177e4SLinus Torvalds } else { 12401da177e4SLinus Torvalds unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*); 12411da177e4SLinus Torvalds *l = simple_strtoull(str,&next,base); 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds break; 12441da177e4SLinus Torvalds case 'Z': 12451da177e4SLinus Torvalds case 'z': 12461da177e4SLinus Torvalds { 12471da177e4SLinus Torvalds size_t *s = (size_t*) va_arg(args,size_t*); 12481da177e4SLinus Torvalds *s = (size_t) simple_strtoul(str,&next,base); 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds break; 12511da177e4SLinus Torvalds default: 12521da177e4SLinus Torvalds if (is_sign) { 12531da177e4SLinus Torvalds int *i = (int *) va_arg(args, int*); 12541da177e4SLinus Torvalds *i = (int) simple_strtol(str,&next,base); 12551da177e4SLinus Torvalds } else { 12561da177e4SLinus Torvalds unsigned int *i = (unsigned int*) va_arg(args, unsigned int*); 12571da177e4SLinus Torvalds *i = (unsigned int) simple_strtoul(str,&next,base); 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds break; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds num++; 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds if (!next) 12641da177e4SLinus Torvalds break; 12651da177e4SLinus Torvalds str = next; 12661da177e4SLinus Torvalds } 1267c6b40d16SJohannes Berg 1268c6b40d16SJohannes Berg /* 1269c6b40d16SJohannes Berg * Now we've come all the way through so either the input string or the 1270c6b40d16SJohannes Berg * format ended. In the former case, there can be a %n at the current 1271c6b40d16SJohannes Berg * position in the format that needs to be filled. 1272c6b40d16SJohannes Berg */ 1273c6b40d16SJohannes Berg if (*fmt == '%' && *(fmt + 1) == 'n') { 1274c6b40d16SJohannes Berg int *p = (int *)va_arg(args, int *); 1275c6b40d16SJohannes Berg *p = str - buf; 1276c6b40d16SJohannes Berg } 1277c6b40d16SJohannes Berg 12781da177e4SLinus Torvalds return num; 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds /** 12831da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 12841da177e4SLinus Torvalds * @buf: input buffer 12851da177e4SLinus Torvalds * @fmt: formatting of buffer 12861da177e4SLinus Torvalds * @...: resulting arguments 12871da177e4SLinus Torvalds */ 12881da177e4SLinus Torvalds int sscanf(const char * buf, const char * fmt, ...) 12891da177e4SLinus Torvalds { 12901da177e4SLinus Torvalds va_list args; 12911da177e4SLinus Torvalds int i; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds va_start(args,fmt); 12941da177e4SLinus Torvalds i = vsscanf(buf,fmt,args); 12951da177e4SLinus Torvalds va_end(args); 12961da177e4SLinus Torvalds return i; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 1299