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); 173e899aa82SPavel Machek if (tail == cp) 174e899aa82SPavel Machek return -EINVAL; 1759d85db22SHarvey Harrison if ((*tail == '\0') || 1769d85db22SHarvey Harrison ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { 1779d85db22SHarvey Harrison *res = val; 1789d85db22SHarvey Harrison return 0; 1799d85db22SHarvey Harrison } 1809d85db22SHarvey Harrison 1819d85db22SHarvey Harrison return -EINVAL; 1829d85db22SHarvey Harrison } 1839d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoul); 18406b2a76dSYi Yang 18506b2a76dSYi Yang /** 18606b2a76dSYi Yang * strict_strtol - convert a string to a long strictly 18706b2a76dSYi Yang * @cp: The string to be converted 18806b2a76dSYi Yang * @base: The number base to use 18906b2a76dSYi Yang * @res: The converted result value 19006b2a76dSYi Yang * 19106b2a76dSYi Yang * strict_strtol is similiar to strict_strtoul, but it allows the first 19206b2a76dSYi Yang * character of a string is '-'. 19306b2a76dSYi Yang * 19406b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 19506b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 19606b2a76dSYi Yang */ 1979d85db22SHarvey Harrison int strict_strtol(const char *cp, unsigned int base, long *res) 1989d85db22SHarvey Harrison { 1999d85db22SHarvey Harrison int ret; 2009d85db22SHarvey Harrison if (*cp == '-') { 2019d85db22SHarvey Harrison ret = strict_strtoul(cp + 1, base, (unsigned long *)res); 2029d85db22SHarvey Harrison if (!ret) 2039d85db22SHarvey Harrison *res = -(*res); 2049d85db22SHarvey Harrison } else { 2059d85db22SHarvey Harrison ret = strict_strtoul(cp, base, (unsigned long *)res); 2069d85db22SHarvey Harrison } 2079d85db22SHarvey Harrison 2089d85db22SHarvey Harrison return ret; 2099d85db22SHarvey Harrison } 2109d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtol); 21106b2a76dSYi Yang 21206b2a76dSYi Yang /** 21306b2a76dSYi Yang * strict_strtoull - convert a string to an unsigned long long strictly 21406b2a76dSYi Yang * @cp: The string to be converted 21506b2a76dSYi Yang * @base: The number base to use 21606b2a76dSYi Yang * @res: The converted result value 21706b2a76dSYi Yang * 21806b2a76dSYi Yang * strict_strtoull converts a string to an unsigned long long only if the 21906b2a76dSYi Yang * string is really an unsigned long long string, any string containing 22006b2a76dSYi Yang * any invalid char at the tail will be rejected and -EINVAL is returned, 22106b2a76dSYi Yang * only a newline char at the tail is acceptible because people generally 22206b2a76dSYi Yang * change a module parameter in the following way: 22306b2a76dSYi Yang * 22406b2a76dSYi Yang * echo 1024 > /sys/module/e1000/parameters/copybreak 22506b2a76dSYi Yang * 22606b2a76dSYi Yang * echo will append a newline to the tail of the string. 22706b2a76dSYi Yang * 22806b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 22906b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 23006b2a76dSYi Yang * 23106b2a76dSYi Yang * simple_strtoull just ignores the successive invalid characters and 23206b2a76dSYi Yang * return the converted value of prefix part of the string. 23306b2a76dSYi Yang */ 2349d85db22SHarvey Harrison int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) 2359d85db22SHarvey Harrison { 2369d85db22SHarvey Harrison char *tail; 2379d85db22SHarvey Harrison unsigned long long val; 2389d85db22SHarvey Harrison size_t len; 2399d85db22SHarvey Harrison 2409d85db22SHarvey Harrison *res = 0; 2419d85db22SHarvey Harrison len = strlen(cp); 2429d85db22SHarvey Harrison if (len == 0) 2439d85db22SHarvey Harrison return -EINVAL; 2449d85db22SHarvey Harrison 2459d85db22SHarvey Harrison val = simple_strtoull(cp, &tail, base); 246e899aa82SPavel Machek if (tail == cp) 247e899aa82SPavel Machek return -EINVAL; 2489d85db22SHarvey Harrison if ((*tail == '\0') || 2499d85db22SHarvey Harrison ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { 2509d85db22SHarvey Harrison *res = val; 2519d85db22SHarvey Harrison return 0; 2529d85db22SHarvey Harrison } 2539d85db22SHarvey Harrison 2549d85db22SHarvey Harrison return -EINVAL; 2559d85db22SHarvey Harrison } 2569d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoull); 25706b2a76dSYi Yang 25806b2a76dSYi Yang /** 25906b2a76dSYi Yang * strict_strtoll - convert a string to a long long strictly 26006b2a76dSYi Yang * @cp: The string to be converted 26106b2a76dSYi Yang * @base: The number base to use 26206b2a76dSYi Yang * @res: The converted result value 26306b2a76dSYi Yang * 26406b2a76dSYi Yang * strict_strtoll is similiar to strict_strtoull, but it allows the first 26506b2a76dSYi Yang * character of a string is '-'. 26606b2a76dSYi Yang * 26706b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 26806b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 26906b2a76dSYi Yang */ 2709d85db22SHarvey Harrison int strict_strtoll(const char *cp, unsigned int base, long long *res) 2719d85db22SHarvey Harrison { 2729d85db22SHarvey Harrison int ret; 2739d85db22SHarvey Harrison if (*cp == '-') { 2749d85db22SHarvey Harrison ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); 2759d85db22SHarvey Harrison if (!ret) 2769d85db22SHarvey Harrison *res = -(*res); 2779d85db22SHarvey Harrison } else { 2789d85db22SHarvey Harrison ret = strict_strtoull(cp, base, (unsigned long long *)res); 2799d85db22SHarvey Harrison } 28006b2a76dSYi Yang 2819d85db22SHarvey Harrison return ret; 2829d85db22SHarvey Harrison } 28306b2a76dSYi Yang EXPORT_SYMBOL(strict_strtoll); 28406b2a76dSYi Yang 2851da177e4SLinus Torvalds static int skip_atoi(const char **s) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds int i=0; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds while (isdigit(**s)) 2901da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 2911da177e4SLinus Torvalds return i; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2944277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used 2954277eeddSDenis Vlasenko * for /proc and /sys data. This directly impacts e.g. top performance 2964277eeddSDenis Vlasenko * with many processes running. We optimize it for speed 2974277eeddSDenis Vlasenko * using code from 2984277eeddSDenis Vlasenko * http://www.cs.uiowa.edu/~jones/bcd/decimal.html 2994277eeddSDenis Vlasenko * (with permission from the author, Douglas W. Jones). */ 3004277eeddSDenis Vlasenko 3014277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999]. 3024277eeddSDenis Vlasenko * Outputs from one to five digits depending on input. 3034277eeddSDenis Vlasenko * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ 3044277eeddSDenis Vlasenko static char* put_dec_trunc(char *buf, unsigned q) 3054277eeddSDenis Vlasenko { 3064277eeddSDenis Vlasenko unsigned d3, d2, d1, d0; 3074277eeddSDenis Vlasenko d1 = (q>>4) & 0xf; 3084277eeddSDenis Vlasenko d2 = (q>>8) & 0xf; 3094277eeddSDenis Vlasenko d3 = (q>>12); 3104277eeddSDenis Vlasenko 3114277eeddSDenis Vlasenko d0 = 6*(d3 + d2 + d1) + (q & 0xf); 3124277eeddSDenis Vlasenko q = (d0 * 0xcd) >> 11; 3134277eeddSDenis Vlasenko d0 = d0 - 10*q; 3144277eeddSDenis Vlasenko *buf++ = d0 + '0'; /* least significant digit */ 3154277eeddSDenis Vlasenko d1 = q + 9*d3 + 5*d2 + d1; 3164277eeddSDenis Vlasenko if (d1 != 0) { 3174277eeddSDenis Vlasenko q = (d1 * 0xcd) >> 11; 3184277eeddSDenis Vlasenko d1 = d1 - 10*q; 3194277eeddSDenis Vlasenko *buf++ = d1 + '0'; /* next digit */ 3204277eeddSDenis Vlasenko 3214277eeddSDenis Vlasenko d2 = q + 2*d2; 3224277eeddSDenis Vlasenko if ((d2 != 0) || (d3 != 0)) { 3234277eeddSDenis Vlasenko q = (d2 * 0xd) >> 7; 3244277eeddSDenis Vlasenko d2 = d2 - 10*q; 3254277eeddSDenis Vlasenko *buf++ = d2 + '0'; /* next digit */ 3264277eeddSDenis Vlasenko 3274277eeddSDenis Vlasenko d3 = q + 4*d3; 3284277eeddSDenis Vlasenko if (d3 != 0) { 3294277eeddSDenis Vlasenko q = (d3 * 0xcd) >> 11; 3304277eeddSDenis Vlasenko d3 = d3 - 10*q; 3314277eeddSDenis Vlasenko *buf++ = d3 + '0'; /* next digit */ 3324277eeddSDenis Vlasenko if (q != 0) 3334277eeddSDenis Vlasenko *buf++ = q + '0'; /* most sign. digit */ 3344277eeddSDenis Vlasenko } 3354277eeddSDenis Vlasenko } 3364277eeddSDenis Vlasenko } 3374277eeddSDenis Vlasenko return buf; 3384277eeddSDenis Vlasenko } 3394277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */ 3404277eeddSDenis Vlasenko static char* put_dec_full(char *buf, unsigned q) 3414277eeddSDenis Vlasenko { 3424277eeddSDenis Vlasenko /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ 3434277eeddSDenis Vlasenko /* but anyway, gcc produces better code with full-sized ints */ 3444277eeddSDenis Vlasenko unsigned d3, d2, d1, d0; 3454277eeddSDenis Vlasenko d1 = (q>>4) & 0xf; 3464277eeddSDenis Vlasenko d2 = (q>>8) & 0xf; 3474277eeddSDenis Vlasenko d3 = (q>>12); 3484277eeddSDenis Vlasenko 3494277eeddSDenis Vlasenko /* Possible ways to approx. divide by 10 */ 3504277eeddSDenis Vlasenko /* gcc -O2 replaces multiply with shifts and adds */ 3514277eeddSDenis Vlasenko // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) 3524277eeddSDenis Vlasenko // (x * 0x67) >> 10: 1100111 3534277eeddSDenis Vlasenko // (x * 0x34) >> 9: 110100 - same 3544277eeddSDenis Vlasenko // (x * 0x1a) >> 8: 11010 - same 3554277eeddSDenis Vlasenko // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) 3564277eeddSDenis Vlasenko 3574277eeddSDenis Vlasenko d0 = 6*(d3 + d2 + d1) + (q & 0xf); 3584277eeddSDenis Vlasenko q = (d0 * 0xcd) >> 11; 3594277eeddSDenis Vlasenko d0 = d0 - 10*q; 3604277eeddSDenis Vlasenko *buf++ = d0 + '0'; 3614277eeddSDenis Vlasenko d1 = q + 9*d3 + 5*d2 + d1; 3624277eeddSDenis Vlasenko q = (d1 * 0xcd) >> 11; 3634277eeddSDenis Vlasenko d1 = d1 - 10*q; 3644277eeddSDenis Vlasenko *buf++ = d1 + '0'; 3654277eeddSDenis Vlasenko 3664277eeddSDenis Vlasenko d2 = q + 2*d2; 3674277eeddSDenis Vlasenko q = (d2 * 0xd) >> 7; 3684277eeddSDenis Vlasenko d2 = d2 - 10*q; 3694277eeddSDenis Vlasenko *buf++ = d2 + '0'; 3704277eeddSDenis Vlasenko 3714277eeddSDenis Vlasenko d3 = q + 4*d3; 3724277eeddSDenis Vlasenko q = (d3 * 0xcd) >> 11; /* - shorter code */ 3734277eeddSDenis Vlasenko /* q = (d3 * 0x67) >> 10; - would also work */ 3744277eeddSDenis Vlasenko d3 = d3 - 10*q; 3754277eeddSDenis Vlasenko *buf++ = d3 + '0'; 3764277eeddSDenis Vlasenko *buf++ = q + '0'; 3774277eeddSDenis Vlasenko return buf; 3784277eeddSDenis Vlasenko } 3794277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */ 3804277eeddSDenis Vlasenko static noinline char* put_dec(char *buf, unsigned long long num) 3814277eeddSDenis Vlasenko { 3824277eeddSDenis Vlasenko while (1) { 3834277eeddSDenis Vlasenko unsigned rem; 3844277eeddSDenis Vlasenko if (num < 100000) 3854277eeddSDenis Vlasenko return put_dec_trunc(buf, num); 3864277eeddSDenis Vlasenko rem = do_div(num, 100000); 3874277eeddSDenis Vlasenko buf = put_dec_full(buf, rem); 3884277eeddSDenis Vlasenko } 3894277eeddSDenis Vlasenko } 3904277eeddSDenis Vlasenko 3911da177e4SLinus Torvalds #define ZEROPAD 1 /* pad with zero */ 3921da177e4SLinus Torvalds #define SIGN 2 /* unsigned/signed long */ 3931da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3941da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 3951da177e4SLinus Torvalds #define LEFT 16 /* left justified */ 3969b706aeeSDenys Vlasenko #define SMALL 32 /* Must be 32 == 0x20 */ 3979b706aeeSDenys Vlasenko #define SPECIAL 64 /* 0x */ 3981da177e4SLinus Torvalds 399fef20d9cSFrederic Weisbecker enum format_type { 400fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 401ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 402fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 403fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 404fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 405fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 406fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 407fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 408fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 409fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 410fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 411a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 412a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 413fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 414fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 415fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 416fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 417fef20d9cSFrederic Weisbecker FORMAT_TYPE_NRCHARS, 418fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 419fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 420fef20d9cSFrederic Weisbecker }; 421fef20d9cSFrederic Weisbecker 422fef20d9cSFrederic Weisbecker struct printf_spec { 423fef20d9cSFrederic Weisbecker enum format_type type; 424fef20d9cSFrederic Weisbecker int flags; /* flags to number() */ 425fef20d9cSFrederic Weisbecker int field_width; /* width of output field */ 426fef20d9cSFrederic Weisbecker int base; 427fef20d9cSFrederic Weisbecker int precision; /* # of digits/chars */ 428fef20d9cSFrederic Weisbecker int qualifier; 429fef20d9cSFrederic Weisbecker }; 430fef20d9cSFrederic Weisbecker 431fef20d9cSFrederic Weisbecker static char *number(char *buf, char *end, unsigned long long num, 432fef20d9cSFrederic Weisbecker struct printf_spec spec) 4331da177e4SLinus Torvalds { 4349b706aeeSDenys Vlasenko /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ 4359b706aeeSDenys Vlasenko static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ 4369b706aeeSDenys Vlasenko 4379b706aeeSDenys Vlasenko char tmp[66]; 4389b706aeeSDenys Vlasenko char sign; 4399b706aeeSDenys Vlasenko char locase; 440fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4411da177e4SLinus Torvalds int i; 4421da177e4SLinus Torvalds 4439b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4449b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 445fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 446fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 447fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4481da177e4SLinus Torvalds sign = 0; 449fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4501da177e4SLinus Torvalds if ((signed long long) num < 0) { 4511da177e4SLinus Torvalds sign = '-'; 4521da177e4SLinus Torvalds num = - (signed long long) num; 453fef20d9cSFrederic Weisbecker spec.field_width--; 454fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4551da177e4SLinus Torvalds sign = '+'; 456fef20d9cSFrederic Weisbecker spec.field_width--; 457fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4581da177e4SLinus Torvalds sign = ' '; 459fef20d9cSFrederic Weisbecker spec.field_width--; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds } 462b39a7340SDenis Vlasenko if (need_pfx) { 463fef20d9cSFrederic Weisbecker spec.field_width--; 464fef20d9cSFrederic Weisbecker if (spec.base == 16) 465fef20d9cSFrederic Weisbecker spec.field_width--; 4661da177e4SLinus Torvalds } 467b39a7340SDenis Vlasenko 468b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4691da177e4SLinus Torvalds i = 0; 4701da177e4SLinus Torvalds if (num == 0) 4711da177e4SLinus Torvalds tmp[i++] = '0'; 4724277eeddSDenis Vlasenko /* Generic code, for any base: 4734277eeddSDenis Vlasenko else do { 4749b706aeeSDenys Vlasenko tmp[i++] = (digits[do_div(num,base)] | locase); 4754277eeddSDenis Vlasenko } while (num != 0); 4764277eeddSDenis Vlasenko */ 477fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 478fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 479b39a7340SDenis Vlasenko int shift = 3; 480fef20d9cSFrederic Weisbecker if (spec.base == 16) shift = 4; 481b39a7340SDenis Vlasenko do { 4829b706aeeSDenys Vlasenko tmp[i++] = (digits[((unsigned char)num) & mask] | locase); 483b39a7340SDenis Vlasenko num >>= shift; 484b39a7340SDenis Vlasenko } while (num); 4854277eeddSDenis Vlasenko } else { /* base 10 */ 4864277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4874277eeddSDenis Vlasenko } 488b39a7340SDenis Vlasenko 489b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 490fef20d9cSFrederic Weisbecker if (i > spec.precision) 491fef20d9cSFrederic Weisbecker spec.precision = i; 492b39a7340SDenis Vlasenko /* leading space padding */ 493fef20d9cSFrederic Weisbecker spec.field_width -= spec.precision; 494fef20d9cSFrederic Weisbecker if (!(spec.flags & (ZEROPAD+LEFT))) { 495fef20d9cSFrederic Weisbecker while(--spec.field_width >= 0) { 496f796937aSJeremy Fitzhardinge if (buf < end) 4971da177e4SLinus Torvalds *buf = ' '; 4981da177e4SLinus Torvalds ++buf; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds } 501b39a7340SDenis Vlasenko /* sign */ 5021da177e4SLinus Torvalds if (sign) { 503f796937aSJeremy Fitzhardinge if (buf < end) 5041da177e4SLinus Torvalds *buf = sign; 5051da177e4SLinus Torvalds ++buf; 5061da177e4SLinus Torvalds } 507b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 508b39a7340SDenis Vlasenko if (need_pfx) { 509f796937aSJeremy Fitzhardinge if (buf < end) 5101da177e4SLinus Torvalds *buf = '0'; 5111da177e4SLinus Torvalds ++buf; 512fef20d9cSFrederic Weisbecker if (spec.base == 16) { 513f796937aSJeremy Fitzhardinge if (buf < end) 5149b706aeeSDenys Vlasenko *buf = ('X' | locase); 5151da177e4SLinus Torvalds ++buf; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds } 518b39a7340SDenis Vlasenko /* zero or space padding */ 519fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 520fef20d9cSFrederic Weisbecker char c = (spec.flags & ZEROPAD) ? '0' : ' '; 521fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 522f796937aSJeremy Fitzhardinge if (buf < end) 5231da177e4SLinus Torvalds *buf = c; 5241da177e4SLinus Torvalds ++buf; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds } 527b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 528fef20d9cSFrederic Weisbecker while (i <= --spec.precision) { 529f796937aSJeremy Fitzhardinge if (buf < end) 5301da177e4SLinus Torvalds *buf = '0'; 5311da177e4SLinus Torvalds ++buf; 5321da177e4SLinus Torvalds } 533b39a7340SDenis Vlasenko /* actual digits of result */ 534b39a7340SDenis Vlasenko while (--i >= 0) { 535f796937aSJeremy Fitzhardinge if (buf < end) 5361da177e4SLinus Torvalds *buf = tmp[i]; 5371da177e4SLinus Torvalds ++buf; 5381da177e4SLinus Torvalds } 539b39a7340SDenis Vlasenko /* trailing space padding */ 540fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 541f796937aSJeremy Fitzhardinge if (buf < end) 5421da177e4SLinus Torvalds *buf = ' '; 5431da177e4SLinus Torvalds ++buf; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds return buf; 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 548fef20d9cSFrederic Weisbecker static char *string(char *buf, char *end, char *s, struct printf_spec spec) 5490f9bfa56SLinus Torvalds { 5500f9bfa56SLinus Torvalds int len, i; 5510f9bfa56SLinus Torvalds 5520f9bfa56SLinus Torvalds if ((unsigned long)s < PAGE_SIZE) 5530f9bfa56SLinus Torvalds s = "<NULL>"; 5540f9bfa56SLinus Torvalds 555fef20d9cSFrederic Weisbecker len = strnlen(s, spec.precision); 5560f9bfa56SLinus Torvalds 557fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 558fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5590f9bfa56SLinus Torvalds if (buf < end) 5600f9bfa56SLinus Torvalds *buf = ' '; 5610f9bfa56SLinus Torvalds ++buf; 5620f9bfa56SLinus Torvalds } 5630f9bfa56SLinus Torvalds } 5640f9bfa56SLinus Torvalds for (i = 0; i < len; ++i) { 5650f9bfa56SLinus Torvalds if (buf < end) 5660f9bfa56SLinus Torvalds *buf = *s; 5670f9bfa56SLinus Torvalds ++buf; ++s; 5680f9bfa56SLinus Torvalds } 569fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5700f9bfa56SLinus Torvalds if (buf < end) 5710f9bfa56SLinus Torvalds *buf = ' '; 5720f9bfa56SLinus Torvalds ++buf; 5730f9bfa56SLinus Torvalds } 5740f9bfa56SLinus Torvalds return buf; 5750f9bfa56SLinus Torvalds } 5760f9bfa56SLinus Torvalds 577fef20d9cSFrederic Weisbecker static char *symbol_string(char *buf, char *end, void *ptr, 578*0c8b946eSFrederic Weisbecker struct printf_spec spec, char ext) 5790fe1ef24SLinus Torvalds { 5800fe1ef24SLinus Torvalds unsigned long value = (unsigned long) ptr; 5810fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 5820fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 583*0c8b946eSFrederic Weisbecker if (ext != 'f') 5840fe1ef24SLinus Torvalds sprint_symbol(sym, value); 585*0c8b946eSFrederic Weisbecker else 586*0c8b946eSFrederic Weisbecker kallsyms_lookup(value, NULL, NULL, NULL, sym); 587fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 5880fe1ef24SLinus Torvalds #else 589fef20d9cSFrederic Weisbecker spec.field_width = 2*sizeof(void *); 590fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL | SMALL | ZEROPAD; 591fef20d9cSFrederic Weisbecker spec.base = 16; 592fef20d9cSFrederic Weisbecker return number(buf, end, value, spec); 5930fe1ef24SLinus Torvalds #endif 5940fe1ef24SLinus Torvalds } 5950fe1ef24SLinus Torvalds 596fef20d9cSFrederic Weisbecker static char *resource_string(char *buf, char *end, struct resource *res, 597fef20d9cSFrederic Weisbecker struct printf_spec spec) 598332d2e78SLinus Torvalds { 599332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 600332d2e78SLinus Torvalds #define IO_RSRC_PRINTK_SIZE 4 601332d2e78SLinus Torvalds #endif 602332d2e78SLinus Torvalds 603332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 604332d2e78SLinus Torvalds #define MEM_RSRC_PRINTK_SIZE 8 605332d2e78SLinus Torvalds #endif 606fef20d9cSFrederic Weisbecker struct printf_spec num_spec = { 607fef20d9cSFrederic Weisbecker .base = 16, 608fef20d9cSFrederic Weisbecker .precision = -1, 609fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 610fef20d9cSFrederic Weisbecker }; 611332d2e78SLinus Torvalds /* room for the actual numbers, the two "0x", -, [, ] and the final zero */ 612332d2e78SLinus Torvalds char sym[4*sizeof(resource_size_t) + 8]; 613332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 614332d2e78SLinus Torvalds int size = -1; 615332d2e78SLinus Torvalds 616332d2e78SLinus Torvalds if (res->flags & IORESOURCE_IO) 617332d2e78SLinus Torvalds size = IO_RSRC_PRINTK_SIZE; 618332d2e78SLinus Torvalds else if (res->flags & IORESOURCE_MEM) 619332d2e78SLinus Torvalds size = MEM_RSRC_PRINTK_SIZE; 620332d2e78SLinus Torvalds 621332d2e78SLinus Torvalds *p++ = '['; 622fef20d9cSFrederic Weisbecker num_spec.field_width = size; 623fef20d9cSFrederic Weisbecker p = number(p, pend, res->start, num_spec); 624332d2e78SLinus Torvalds *p++ = '-'; 625fef20d9cSFrederic Weisbecker p = number(p, pend, res->end, num_spec); 626332d2e78SLinus Torvalds *p++ = ']'; 627332d2e78SLinus Torvalds *p = 0; 628332d2e78SLinus Torvalds 629fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 630332d2e78SLinus Torvalds } 631332d2e78SLinus Torvalds 632fef20d9cSFrederic Weisbecker static char *mac_address_string(char *buf, char *end, u8 *addr, 633fef20d9cSFrederic Weisbecker struct printf_spec spec) 634dd45c9cfSHarvey Harrison { 635dd45c9cfSHarvey Harrison char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */ 636dd45c9cfSHarvey Harrison char *p = mac_addr; 637dd45c9cfSHarvey Harrison int i; 638dd45c9cfSHarvey Harrison 639dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 640dd45c9cfSHarvey Harrison p = pack_hex_byte(p, addr[i]); 641fef20d9cSFrederic Weisbecker if (!(spec.flags & SPECIAL) && i != 5) 642dd45c9cfSHarvey Harrison *p++ = ':'; 643dd45c9cfSHarvey Harrison } 644dd45c9cfSHarvey Harrison *p = '\0'; 645fef20d9cSFrederic Weisbecker spec.flags &= ~SPECIAL; 646dd45c9cfSHarvey Harrison 647fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 648dd45c9cfSHarvey Harrison } 649dd45c9cfSHarvey Harrison 650fef20d9cSFrederic Weisbecker static char *ip6_addr_string(char *buf, char *end, u8 *addr, 651fef20d9cSFrederic Weisbecker struct printf_spec spec) 652689afa7dSHarvey Harrison { 653689afa7dSHarvey Harrison char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */ 654689afa7dSHarvey Harrison char *p = ip6_addr; 655689afa7dSHarvey Harrison int i; 656689afa7dSHarvey Harrison 657689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 658689afa7dSHarvey Harrison p = pack_hex_byte(p, addr[2 * i]); 659689afa7dSHarvey Harrison p = pack_hex_byte(p, addr[2 * i + 1]); 660fef20d9cSFrederic Weisbecker if (!(spec.flags & SPECIAL) && i != 7) 661689afa7dSHarvey Harrison *p++ = ':'; 662689afa7dSHarvey Harrison } 663689afa7dSHarvey Harrison *p = '\0'; 664fef20d9cSFrederic Weisbecker spec.flags &= ~SPECIAL; 665689afa7dSHarvey Harrison 666fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 667689afa7dSHarvey Harrison } 668689afa7dSHarvey Harrison 669fef20d9cSFrederic Weisbecker static char *ip4_addr_string(char *buf, char *end, u8 *addr, 670fef20d9cSFrederic Weisbecker struct printf_spec spec) 6714aa99606SHarvey Harrison { 6724aa99606SHarvey Harrison char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */ 673b9ac9985SHarvey Harrison char temp[3]; /* hold each IP quad in reverse order */ 6744aa99606SHarvey Harrison char *p = ip4_addr; 675b9ac9985SHarvey Harrison int i, digits; 6764aa99606SHarvey Harrison 6774aa99606SHarvey Harrison for (i = 0; i < 4; i++) { 678b9ac9985SHarvey Harrison digits = put_dec_trunc(temp, addr[i]) - temp; 679b9ac9985SHarvey Harrison /* reverse the digits in the quad */ 680b9ac9985SHarvey Harrison while (digits--) 681b9ac9985SHarvey Harrison *p++ = temp[digits]; 6824aa99606SHarvey Harrison if (i != 3) 6834aa99606SHarvey Harrison *p++ = '.'; 6844aa99606SHarvey Harrison } 6854aa99606SHarvey Harrison *p = '\0'; 686fef20d9cSFrederic Weisbecker spec.flags &= ~SPECIAL; 6874aa99606SHarvey Harrison 688fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 6894aa99606SHarvey Harrison } 6904aa99606SHarvey Harrison 6914d8a743cSLinus Torvalds /* 6924d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 6934d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 6944d8a743cSLinus Torvalds * specifiers. 6954d8a743cSLinus Torvalds * 696332d2e78SLinus Torvalds * Right now we handle: 6970fe1ef24SLinus Torvalds * 698*0c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 699*0c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 700332d2e78SLinus Torvalds * - 'S' For symbolic direct pointers 701332d2e78SLinus Torvalds * - 'R' For a struct resource pointer, it prints the range of 702332d2e78SLinus Torvalds * addresses (not the name nor the flags) 703dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 704dd45c9cfSHarvey Harrison * usual colon-separated hex notation 7054aa99606SHarvey Harrison * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated 7064aa99606SHarvey Harrison * decimal for v4 and colon separated network-order 16 bit hex for v6) 7074aa99606SHarvey Harrison * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is 7086b9a1066SHarvey Harrison * currently the same 709332d2e78SLinus Torvalds * 710332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 711332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 712332d2e78SLinus Torvalds * pointer to the real address. 7134d8a743cSLinus Torvalds */ 714fef20d9cSFrederic Weisbecker static char *pointer(const char *fmt, char *buf, char *end, void *ptr, 715fef20d9cSFrederic Weisbecker struct printf_spec spec) 71678a8bf69SLinus Torvalds { 717d97106abSLinus Torvalds if (!ptr) 718fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 719d97106abSLinus Torvalds 7200fe1ef24SLinus Torvalds switch (*fmt) { 7210fe1ef24SLinus Torvalds case 'F': 722*0c8b946eSFrederic Weisbecker case 'f': 7230fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 7240fe1ef24SLinus Torvalds /* Fallthrough */ 7250fe1ef24SLinus Torvalds case 'S': 726*0c8b946eSFrederic Weisbecker return symbol_string(buf, end, ptr, spec, *fmt); 727332d2e78SLinus Torvalds case 'R': 728fef20d9cSFrederic Weisbecker return resource_string(buf, end, ptr, spec); 729411c41eeSHarvey Harrison case 'm': 730fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL; 731411c41eeSHarvey Harrison /* Fallthrough */ 732dd45c9cfSHarvey Harrison case 'M': 733fef20d9cSFrederic Weisbecker return mac_address_string(buf, end, ptr, spec); 7344aa99606SHarvey Harrison case 'i': 735fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL; 7364aa99606SHarvey Harrison /* Fallthrough */ 7374aa99606SHarvey Harrison case 'I': 7384aa99606SHarvey Harrison if (fmt[1] == '6') 739fef20d9cSFrederic Weisbecker return ip6_addr_string(buf, end, ptr, spec); 7404aa99606SHarvey Harrison if (fmt[1] == '4') 741fef20d9cSFrederic Weisbecker return ip4_addr_string(buf, end, ptr, spec); 742fef20d9cSFrederic Weisbecker spec.flags &= ~SPECIAL; 7434aa99606SHarvey Harrison break; 7440fe1ef24SLinus Torvalds } 745fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 746fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 747fef20d9cSFrederic Weisbecker spec.field_width = 2*sizeof(void *); 748fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 74978a8bf69SLinus Torvalds } 750fef20d9cSFrederic Weisbecker spec.base = 16; 751fef20d9cSFrederic Weisbecker 752fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 753fef20d9cSFrederic Weisbecker } 754fef20d9cSFrederic Weisbecker 755fef20d9cSFrederic Weisbecker /* 756fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 757fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 758fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 759fef20d9cSFrederic Weisbecker * to go on the next call). 760fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 761fef20d9cSFrederic Weisbecker * 762fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 763fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 764fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 765fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 766fef20d9cSFrederic Weisbecker * 767fef20d9cSFrederic Weisbecker * @fmt: the format string 768fef20d9cSFrederic Weisbecker * @type of the token returned 769fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 770fef20d9cSFrederic Weisbecker * @field_width: overwritten width 771fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 772fef20d9cSFrederic Weisbecker * @precision: precision of a number 773fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 774fef20d9cSFrederic Weisbecker */ 775fef20d9cSFrederic Weisbecker static int format_decode(const char *fmt, struct printf_spec *spec) 776fef20d9cSFrederic Weisbecker { 777fef20d9cSFrederic Weisbecker const char *start = fmt; 778fef20d9cSFrederic Weisbecker 779fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 780ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 781fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 782fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 783fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 784fef20d9cSFrederic Weisbecker } 785fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 786fef20d9cSFrederic Weisbecker goto precision; 787fef20d9cSFrederic Weisbecker } 788fef20d9cSFrederic Weisbecker 789fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 790fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 791fef20d9cSFrederic Weisbecker if (spec->precision < 0) 792fef20d9cSFrederic Weisbecker spec->precision = 0; 793fef20d9cSFrederic Weisbecker 794fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 795fef20d9cSFrederic Weisbecker goto qualifier; 796fef20d9cSFrederic Weisbecker } 797fef20d9cSFrederic Weisbecker 798fef20d9cSFrederic Weisbecker /* By default */ 799fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 800fef20d9cSFrederic Weisbecker 801fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 802fef20d9cSFrederic Weisbecker if (*fmt == '%') 803fef20d9cSFrederic Weisbecker break; 804fef20d9cSFrederic Weisbecker } 805fef20d9cSFrederic Weisbecker 806fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 807fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 808fef20d9cSFrederic Weisbecker return fmt - start; 809fef20d9cSFrederic Weisbecker 810fef20d9cSFrederic Weisbecker /* Process flags */ 811fef20d9cSFrederic Weisbecker spec->flags = 0; 812fef20d9cSFrederic Weisbecker 813fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 814fef20d9cSFrederic Weisbecker bool found = true; 815fef20d9cSFrederic Weisbecker 816fef20d9cSFrederic Weisbecker ++fmt; 817fef20d9cSFrederic Weisbecker 818fef20d9cSFrederic Weisbecker switch (*fmt) { 819fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 820fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 821fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 822fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 823fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 824fef20d9cSFrederic Weisbecker default: found = false; 825fef20d9cSFrederic Weisbecker } 826fef20d9cSFrederic Weisbecker 827fef20d9cSFrederic Weisbecker if (!found) 828fef20d9cSFrederic Weisbecker break; 829fef20d9cSFrederic Weisbecker } 830fef20d9cSFrederic Weisbecker 831fef20d9cSFrederic Weisbecker /* get field width */ 832fef20d9cSFrederic Weisbecker spec->field_width = -1; 833fef20d9cSFrederic Weisbecker 834fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 835fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 836fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 837fef20d9cSFrederic Weisbecker /* it's the next argument */ 838ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 839fef20d9cSFrederic Weisbecker return ++fmt - start; 840fef20d9cSFrederic Weisbecker } 841fef20d9cSFrederic Weisbecker 842fef20d9cSFrederic Weisbecker precision: 843fef20d9cSFrederic Weisbecker /* get the precision */ 844fef20d9cSFrederic Weisbecker spec->precision = -1; 845fef20d9cSFrederic Weisbecker if (*fmt == '.') { 846fef20d9cSFrederic Weisbecker ++fmt; 847fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 848fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 849fef20d9cSFrederic Weisbecker if (spec->precision < 0) 850fef20d9cSFrederic Weisbecker spec->precision = 0; 851fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 852fef20d9cSFrederic Weisbecker /* it's the next argument */ 853adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 854fef20d9cSFrederic Weisbecker return ++fmt - start; 855fef20d9cSFrederic Weisbecker } 856fef20d9cSFrederic Weisbecker } 857fef20d9cSFrederic Weisbecker 858fef20d9cSFrederic Weisbecker qualifier: 859fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 860fef20d9cSFrederic Weisbecker spec->qualifier = -1; 861fef20d9cSFrederic Weisbecker if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || 862fef20d9cSFrederic Weisbecker *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { 863a4e94ef0SZhaolei spec->qualifier = *fmt++; 864a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 865a4e94ef0SZhaolei if (spec->qualifier == 'l') { 866fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 867fef20d9cSFrederic Weisbecker ++fmt; 868a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 869a4e94ef0SZhaolei spec->qualifier = 'H'; 870a4e94ef0SZhaolei ++fmt; 871a4e94ef0SZhaolei } 872fef20d9cSFrederic Weisbecker } 873fef20d9cSFrederic Weisbecker } 874fef20d9cSFrederic Weisbecker 875fef20d9cSFrederic Weisbecker /* default base */ 876fef20d9cSFrederic Weisbecker spec->base = 10; 877fef20d9cSFrederic Weisbecker switch (*fmt) { 878fef20d9cSFrederic Weisbecker case 'c': 879fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 880fef20d9cSFrederic Weisbecker return ++fmt - start; 881fef20d9cSFrederic Weisbecker 882fef20d9cSFrederic Weisbecker case 's': 883fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 884fef20d9cSFrederic Weisbecker return ++fmt - start; 885fef20d9cSFrederic Weisbecker 886fef20d9cSFrederic Weisbecker case 'p': 887fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 888fef20d9cSFrederic Weisbecker return fmt - start; 889fef20d9cSFrederic Weisbecker /* skip alnum */ 890fef20d9cSFrederic Weisbecker 891fef20d9cSFrederic Weisbecker case 'n': 892fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NRCHARS; 893fef20d9cSFrederic Weisbecker return ++fmt - start; 894fef20d9cSFrederic Weisbecker 895fef20d9cSFrederic Weisbecker case '%': 896fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 897fef20d9cSFrederic Weisbecker return ++fmt - start; 898fef20d9cSFrederic Weisbecker 899fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 900fef20d9cSFrederic Weisbecker case 'o': 901fef20d9cSFrederic Weisbecker spec->base = 8; 902fef20d9cSFrederic Weisbecker break; 903fef20d9cSFrederic Weisbecker 904fef20d9cSFrederic Weisbecker case 'x': 905fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 906fef20d9cSFrederic Weisbecker 907fef20d9cSFrederic Weisbecker case 'X': 908fef20d9cSFrederic Weisbecker spec->base = 16; 909fef20d9cSFrederic Weisbecker break; 910fef20d9cSFrederic Weisbecker 911fef20d9cSFrederic Weisbecker case 'd': 912fef20d9cSFrederic Weisbecker case 'i': 91339e874f8SFrederic Weisbecker spec->flags |= SIGN; 914fef20d9cSFrederic Weisbecker case 'u': 915fef20d9cSFrederic Weisbecker break; 916fef20d9cSFrederic Weisbecker 917fef20d9cSFrederic Weisbecker default: 918fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 919fef20d9cSFrederic Weisbecker return fmt - start; 920fef20d9cSFrederic Weisbecker } 921fef20d9cSFrederic Weisbecker 922fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 923fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 924fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 92539e874f8SFrederic Weisbecker if (spec->flags & SIGN) 926fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG; 927fef20d9cSFrederic Weisbecker else 928fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_ULONG; 929fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') { 930fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 931fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 932fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 933a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 934a4e94ef0SZhaolei if (spec->flags & SIGN) 935a4e94ef0SZhaolei spec->type = FORMAT_TYPE_BYTE; 936a4e94ef0SZhaolei else 937a4e94ef0SZhaolei spec->type = FORMAT_TYPE_UBYTE; 938fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 93939e874f8SFrederic Weisbecker if (spec->flags & SIGN) 940fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SHORT; 941fef20d9cSFrederic Weisbecker else 942fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_USHORT; 943fef20d9cSFrederic Weisbecker } else { 94439e874f8SFrederic Weisbecker if (spec->flags & SIGN) 945fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INT; 946fef20d9cSFrederic Weisbecker else 947fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_UINT; 948fef20d9cSFrederic Weisbecker } 949fef20d9cSFrederic Weisbecker 950fef20d9cSFrederic Weisbecker return ++fmt - start; 95178a8bf69SLinus Torvalds } 95278a8bf69SLinus Torvalds 9531da177e4SLinus Torvalds /** 9541da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 9551da177e4SLinus Torvalds * @buf: The buffer to place the result into 9561da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 9571da177e4SLinus Torvalds * @fmt: The format string to use 9581da177e4SLinus Torvalds * @args: Arguments for the format string 9591da177e4SLinus Torvalds * 96020036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 96120036fdcSAndi Kleen * %pS output the name of a text symbol 962*0c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 963*0c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 964332d2e78SLinus Torvalds * %pR output the address range in a struct resource 96520036fdcSAndi Kleen * 9661da177e4SLinus Torvalds * The return value is the number of characters which would 9671da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 9681da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 9691da177e4SLinus Torvalds * number of characters written into @buf as return value 97072fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 9711da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 9721da177e4SLinus Torvalds * string is truncated. 9731da177e4SLinus Torvalds * 9741da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 97572fd4a35SRobert P. J. Day * You probably want snprintf() instead. 9761da177e4SLinus Torvalds */ 9771da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds unsigned long long num; 9801da177e4SLinus Torvalds char *str, *end, c; 981fef20d9cSFrederic Weisbecker int read; 982fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 9831da177e4SLinus Torvalds 984f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 985f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 9861da177e4SLinus Torvalds if (unlikely((int) size < 0)) { 9871da177e4SLinus Torvalds /* There can be only one.. */ 988b39a7340SDenis Vlasenko static char warn = 1; 9891da177e4SLinus Torvalds WARN_ON(warn); 9901da177e4SLinus Torvalds warn = 0; 9911da177e4SLinus Torvalds return 0; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds str = buf; 995f796937aSJeremy Fitzhardinge end = buf + size; 9961da177e4SLinus Torvalds 997f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 998f796937aSJeremy Fitzhardinge if (end < buf) { 9991da177e4SLinus Torvalds end = ((void *)-1); 1000f796937aSJeremy Fitzhardinge size = end - buf; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 1003fef20d9cSFrederic Weisbecker while (*fmt) { 1004fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1005fef20d9cSFrederic Weisbecker 1006fef20d9cSFrederic Weisbecker read = format_decode(fmt, &spec); 1007fef20d9cSFrederic Weisbecker 1008fef20d9cSFrederic Weisbecker fmt += read; 1009fef20d9cSFrederic Weisbecker 1010fef20d9cSFrederic Weisbecker switch (spec.type) { 1011fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1012fef20d9cSFrederic Weisbecker int copy = read; 1013fef20d9cSFrederic Weisbecker if (str < end) { 1014fef20d9cSFrederic Weisbecker if (copy > end - str) 1015fef20d9cSFrederic Weisbecker copy = end - str; 1016fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1017fef20d9cSFrederic Weisbecker } 1018fef20d9cSFrederic Weisbecker str += read; 1019fef20d9cSFrederic Weisbecker break; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 1022ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1023fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1024fef20d9cSFrederic Weisbecker break; 10251da177e4SLinus Torvalds 1026fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1027fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1028fef20d9cSFrederic Weisbecker break; 10291da177e4SLinus Torvalds 1030fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 1031fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1032fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1033f796937aSJeremy Fitzhardinge if (str < end) 10341da177e4SLinus Torvalds *str = ' '; 10351da177e4SLinus Torvalds ++str; 1036fef20d9cSFrederic Weisbecker 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1040f796937aSJeremy Fitzhardinge if (str < end) 10411da177e4SLinus Torvalds *str = c; 10421da177e4SLinus Torvalds ++str; 1043fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1044f796937aSJeremy Fitzhardinge if (str < end) 10451da177e4SLinus Torvalds *str = ' '; 10461da177e4SLinus Torvalds ++str; 10471da177e4SLinus Torvalds } 1048fef20d9cSFrederic Weisbecker break; 10491da177e4SLinus Torvalds 1050fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1051fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1052fef20d9cSFrederic Weisbecker break; 10531da177e4SLinus Torvalds 1054fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1055fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, va_arg(args, void *), 1056fef20d9cSFrederic Weisbecker spec); 1057fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 10584d8a743cSLinus Torvalds fmt++; 1059fef20d9cSFrederic Weisbecker break; 10601da177e4SLinus Torvalds 1061fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1062f796937aSJeremy Fitzhardinge if (str < end) 10631da177e4SLinus Torvalds *str = '%'; 10641da177e4SLinus Torvalds ++str; 10651da177e4SLinus Torvalds break; 10661da177e4SLinus Torvalds 1067fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1068f796937aSJeremy Fitzhardinge if (str < end) 10691da177e4SLinus Torvalds *str = '%'; 10701da177e4SLinus Torvalds ++str; 1071fef20d9cSFrederic Weisbecker break; 1072fef20d9cSFrederic Weisbecker 1073fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 1074fef20d9cSFrederic Weisbecker int qualifier = spec.qualifier; 1075fef20d9cSFrederic Weisbecker 1076fef20d9cSFrederic Weisbecker if (qualifier == 'l') { 1077fef20d9cSFrederic Weisbecker long *ip = va_arg(args, long *); 1078fef20d9cSFrederic Weisbecker *ip = (str - buf); 1079fef20d9cSFrederic Weisbecker } else if (qualifier == 'Z' || 1080fef20d9cSFrederic Weisbecker qualifier == 'z') { 1081fef20d9cSFrederic Weisbecker size_t *ip = va_arg(args, size_t *); 1082fef20d9cSFrederic Weisbecker *ip = (str - buf); 10831da177e4SLinus Torvalds } else { 1084fef20d9cSFrederic Weisbecker int *ip = va_arg(args, int *); 1085fef20d9cSFrederic Weisbecker *ip = (str - buf); 1086fef20d9cSFrederic Weisbecker } 1087fef20d9cSFrederic Weisbecker break; 1088fef20d9cSFrederic Weisbecker } 1089fef20d9cSFrederic Weisbecker 1090fef20d9cSFrederic Weisbecker default: 1091fef20d9cSFrederic Weisbecker switch (spec.type) { 1092fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1093fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1094fef20d9cSFrederic Weisbecker break; 1095fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1096fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1097fef20d9cSFrederic Weisbecker break; 1098fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1099fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1100fef20d9cSFrederic Weisbecker break; 1101fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1102fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1103fef20d9cSFrederic Weisbecker break; 1104fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1105fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1106fef20d9cSFrederic Weisbecker break; 1107a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1108a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1109a4e94ef0SZhaolei break; 1110a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1111a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1112a4e94ef0SZhaolei break; 1113fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1114fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1115fef20d9cSFrederic Weisbecker break; 1116fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1117fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1118fef20d9cSFrederic Weisbecker break; 111939e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 112039e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1121fef20d9cSFrederic Weisbecker break; 1122fef20d9cSFrederic Weisbecker default: 1123fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 11241da177e4SLinus Torvalds } 1125fef20d9cSFrederic Weisbecker 1126fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 11271da177e4SLinus Torvalds } 1128fef20d9cSFrederic Weisbecker } 1129fef20d9cSFrederic Weisbecker 1130f796937aSJeremy Fitzhardinge if (size > 0) { 1131f796937aSJeremy Fitzhardinge if (str < end) 11321da177e4SLinus Torvalds *str = '\0'; 1133f796937aSJeremy Fitzhardinge else 11340a6047eeSLinus Torvalds end[-1] = '\0'; 1135f796937aSJeremy Fitzhardinge } 1136fef20d9cSFrederic Weisbecker 1137f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 11381da177e4SLinus Torvalds return str-buf; 1139fef20d9cSFrederic Weisbecker 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds /** 11441da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 11451da177e4SLinus Torvalds * @buf: The buffer to place the result into 11461da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 11471da177e4SLinus Torvalds * @fmt: The format string to use 11481da177e4SLinus Torvalds * @args: Arguments for the format string 11491da177e4SLinus Torvalds * 11501da177e4SLinus Torvalds * The return value is the number of characters which have been written into 11511da177e4SLinus Torvalds * the @buf not including the trailing '\0'. If @size is <= 0 the function 11521da177e4SLinus Torvalds * returns 0. 11531da177e4SLinus Torvalds * 11541da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 115572fd4a35SRobert P. J. Day * You probably want scnprintf() instead. 115620036fdcSAndi Kleen * 115720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 11581da177e4SLinus Torvalds */ 11591da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 11601da177e4SLinus Torvalds { 11611da177e4SLinus Torvalds int i; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds i=vsnprintf(buf,size,fmt,args); 11641da177e4SLinus Torvalds return (i >= size) ? (size - 1) : i; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds /** 11691da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 11701da177e4SLinus Torvalds * @buf: The buffer to place the result into 11711da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 11721da177e4SLinus Torvalds * @fmt: The format string to use 11731da177e4SLinus Torvalds * @...: Arguments for the format string 11741da177e4SLinus Torvalds * 11751da177e4SLinus Torvalds * The return value is the number of characters which would be 11761da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 11771da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 11781da177e4SLinus Torvalds * @size, the resulting string is truncated. 117920036fdcSAndi Kleen * 118020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 11811da177e4SLinus Torvalds */ 11821da177e4SLinus Torvalds int snprintf(char * buf, size_t size, const char *fmt, ...) 11831da177e4SLinus Torvalds { 11841da177e4SLinus Torvalds va_list args; 11851da177e4SLinus Torvalds int i; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds va_start(args, fmt); 11881da177e4SLinus Torvalds i=vsnprintf(buf,size,fmt,args); 11891da177e4SLinus Torvalds va_end(args); 11901da177e4SLinus Torvalds return i; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /** 11951da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 11961da177e4SLinus Torvalds * @buf: The buffer to place the result into 11971da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 11981da177e4SLinus Torvalds * @fmt: The format string to use 11991da177e4SLinus Torvalds * @...: Arguments for the format string 12001da177e4SLinus Torvalds * 12011da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 1202ea6f3281SMartin Peschke * the trailing '\0'. If @size is <= 0 the function returns 0. 12031da177e4SLinus Torvalds */ 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds int scnprintf(char * buf, size_t size, const char *fmt, ...) 12061da177e4SLinus Torvalds { 12071da177e4SLinus Torvalds va_list args; 12081da177e4SLinus Torvalds int i; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds va_start(args, fmt); 12111da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 12121da177e4SLinus Torvalds va_end(args); 12131da177e4SLinus Torvalds return (i >= size) ? (size - 1) : i; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds /** 12181da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 12191da177e4SLinus Torvalds * @buf: The buffer to place the result into 12201da177e4SLinus Torvalds * @fmt: The format string to use 12211da177e4SLinus Torvalds * @args: Arguments for the format string 12221da177e4SLinus Torvalds * 12231da177e4SLinus Torvalds * The function returns the number of characters written 122472fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 12251da177e4SLinus Torvalds * buffer overflows. 12261da177e4SLinus Torvalds * 12271da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 122872fd4a35SRobert P. J. Day * You probably want sprintf() instead. 122920036fdcSAndi Kleen * 123020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 12311da177e4SLinus Torvalds */ 12321da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 12331da177e4SLinus Torvalds { 12341da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds /** 12391da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 12401da177e4SLinus Torvalds * @buf: The buffer to place the result into 12411da177e4SLinus Torvalds * @fmt: The format string to use 12421da177e4SLinus Torvalds * @...: Arguments for the format string 12431da177e4SLinus Torvalds * 12441da177e4SLinus Torvalds * The function returns the number of characters written 124572fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 12461da177e4SLinus Torvalds * buffer overflows. 124720036fdcSAndi Kleen * 124820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 12491da177e4SLinus Torvalds */ 12501da177e4SLinus Torvalds int sprintf(char * buf, const char *fmt, ...) 12511da177e4SLinus Torvalds { 12521da177e4SLinus Torvalds va_list args; 12531da177e4SLinus Torvalds int i; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds va_start(args, fmt); 12561da177e4SLinus Torvalds i=vsnprintf(buf, INT_MAX, fmt, args); 12571da177e4SLinus Torvalds va_end(args); 12581da177e4SLinus Torvalds return i; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 12611da177e4SLinus Torvalds 12624370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 12634370aa4aSLai Jiangshan /* 12644370aa4aSLai Jiangshan * bprintf service: 12654370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 12664370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 12674370aa4aSLai Jiangshan */ 12684370aa4aSLai Jiangshan 12694370aa4aSLai Jiangshan /** 12704370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 12714370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 12724370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 12734370aa4aSLai Jiangshan * @fmt: The format string to use 12744370aa4aSLai Jiangshan * @args: Arguments for the format string 12754370aa4aSLai Jiangshan * 12764370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 12774370aa4aSLai Jiangshan * is skiped. 12784370aa4aSLai Jiangshan * 12794370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 12804370aa4aSLai Jiangshan * the given input. 12814370aa4aSLai Jiangshan * 12824370aa4aSLai Jiangshan * NOTE: 12834370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 12844370aa4aSLai Jiangshan * valid for bstr_printf(). 12854370aa4aSLai Jiangshan */ 12864370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 12874370aa4aSLai Jiangshan { 1288fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 12894370aa4aSLai Jiangshan char *str, *end; 1290fef20d9cSFrederic Weisbecker int read; 12914370aa4aSLai Jiangshan 12924370aa4aSLai Jiangshan str = (char *)bin_buf; 12934370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 12944370aa4aSLai Jiangshan 12954370aa4aSLai Jiangshan #define save_arg(type) \ 12964370aa4aSLai Jiangshan do { \ 12974370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 12984370aa4aSLai Jiangshan unsigned long long value; \ 12994370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 13004370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 13014370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 13024370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 13034370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 13044370aa4aSLai Jiangshan } \ 13054370aa4aSLai Jiangshan } else { \ 13064370aa4aSLai Jiangshan unsigned long value; \ 13074370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 13084370aa4aSLai Jiangshan value = va_arg(args, int); \ 13094370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 13104370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 13114370aa4aSLai Jiangshan } \ 13124370aa4aSLai Jiangshan str += sizeof(type); \ 13134370aa4aSLai Jiangshan } while (0) 13144370aa4aSLai Jiangshan 13154370aa4aSLai Jiangshan 1316fef20d9cSFrederic Weisbecker while (*fmt) { 1317fef20d9cSFrederic Weisbecker read = format_decode(fmt, &spec); 13184370aa4aSLai Jiangshan 1319fef20d9cSFrederic Weisbecker fmt += read; 1320fef20d9cSFrederic Weisbecker 1321fef20d9cSFrederic Weisbecker switch (spec.type) { 1322fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 1323fef20d9cSFrederic Weisbecker break; 1324fef20d9cSFrederic Weisbecker 1325ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1326fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 13274370aa4aSLai Jiangshan save_arg(int); 1328fef20d9cSFrederic Weisbecker break; 13294370aa4aSLai Jiangshan 1330fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 13314370aa4aSLai Jiangshan save_arg(char); 1332fef20d9cSFrederic Weisbecker break; 1333fef20d9cSFrederic Weisbecker 1334fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 13354370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 13364370aa4aSLai Jiangshan size_t len; 13374370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 13384370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 13394370aa4aSLai Jiangshan save_str = "<NULL>"; 13404370aa4aSLai Jiangshan len = strlen(save_str); 13414370aa4aSLai Jiangshan if (str + len + 1 < end) 13424370aa4aSLai Jiangshan memcpy(str, save_str, len + 1); 13434370aa4aSLai Jiangshan str += len + 1; 1344fef20d9cSFrederic Weisbecker break; 13454370aa4aSLai Jiangshan } 1346fef20d9cSFrederic Weisbecker 1347fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 13484370aa4aSLai Jiangshan save_arg(void *); 13494370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 1350fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 13514370aa4aSLai Jiangshan fmt++; 1352fef20d9cSFrederic Weisbecker break; 1353fef20d9cSFrederic Weisbecker 1354fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1355fef20d9cSFrederic Weisbecker break; 1356fef20d9cSFrederic Weisbecker 1357fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1358fef20d9cSFrederic Weisbecker break; 1359fef20d9cSFrederic Weisbecker 1360fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 13614370aa4aSLai Jiangshan /* skip %n 's argument */ 1362fef20d9cSFrederic Weisbecker int qualifier = spec.qualifier; 13634370aa4aSLai Jiangshan void *skip_arg; 13644370aa4aSLai Jiangshan if (qualifier == 'l') 13654370aa4aSLai Jiangshan skip_arg = va_arg(args, long *); 13664370aa4aSLai Jiangshan else if (qualifier == 'Z' || qualifier == 'z') 13674370aa4aSLai Jiangshan skip_arg = va_arg(args, size_t *); 13684370aa4aSLai Jiangshan else 13694370aa4aSLai Jiangshan skip_arg = va_arg(args, int *); 1370fef20d9cSFrederic Weisbecker break; 13714370aa4aSLai Jiangshan } 13724370aa4aSLai Jiangshan 1373fef20d9cSFrederic Weisbecker default: 1374fef20d9cSFrederic Weisbecker switch (spec.type) { 1375fef20d9cSFrederic Weisbecker 1376fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1377fef20d9cSFrederic Weisbecker save_arg(long long); 1378fef20d9cSFrederic Weisbecker break; 1379fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1380fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1381fef20d9cSFrederic Weisbecker save_arg(unsigned long); 1382fef20d9cSFrederic Weisbecker break; 1383fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1384fef20d9cSFrederic Weisbecker save_arg(size_t); 1385fef20d9cSFrederic Weisbecker break; 1386fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1387fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 1388fef20d9cSFrederic Weisbecker break; 1389a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1390a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1391a4e94ef0SZhaolei save_arg(char); 1392a4e94ef0SZhaolei break; 1393fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1394fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1395fef20d9cSFrederic Weisbecker save_arg(short); 1396fef20d9cSFrederic Weisbecker break; 1397fef20d9cSFrederic Weisbecker default: 1398fef20d9cSFrederic Weisbecker save_arg(int); 1399fef20d9cSFrederic Weisbecker } 1400fef20d9cSFrederic Weisbecker } 1401fef20d9cSFrederic Weisbecker } 14024370aa4aSLai Jiangshan return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 1403fef20d9cSFrederic Weisbecker 1404fef20d9cSFrederic Weisbecker #undef save_arg 14054370aa4aSLai Jiangshan } 14064370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 14074370aa4aSLai Jiangshan 14084370aa4aSLai Jiangshan /** 14094370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 14104370aa4aSLai Jiangshan * @buf: The buffer to place the result into 14114370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 14124370aa4aSLai Jiangshan * @fmt: The format string to use 14134370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 14144370aa4aSLai Jiangshan * 14154370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 14164370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 14174370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 14184370aa4aSLai Jiangshan * 14194370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 14204370aa4aSLai Jiangshan * %pS output the name of a text symbol 1421*0c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 1422*0c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 14234370aa4aSLai Jiangshan * %pR output the address range in a struct resource 14244370aa4aSLai Jiangshan * %n is ignored 14254370aa4aSLai Jiangshan * 14264370aa4aSLai Jiangshan * The return value is the number of characters which would 14274370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 14284370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 14294370aa4aSLai Jiangshan * number of characters written into @buf as return value 14304370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 14314370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 14324370aa4aSLai Jiangshan * string is truncated. 14334370aa4aSLai Jiangshan */ 14344370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 14354370aa4aSLai Jiangshan { 14364370aa4aSLai Jiangshan unsigned long long num; 14374370aa4aSLai Jiangshan char *str, *end, c; 14384370aa4aSLai Jiangshan const char *args = (const char *)bin_buf; 14394370aa4aSLai Jiangshan 1440fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 14414370aa4aSLai Jiangshan 14424370aa4aSLai Jiangshan if (unlikely((int) size < 0)) { 14434370aa4aSLai Jiangshan /* There can be only one.. */ 14444370aa4aSLai Jiangshan static char warn = 1; 14454370aa4aSLai Jiangshan WARN_ON(warn); 14464370aa4aSLai Jiangshan warn = 0; 14474370aa4aSLai Jiangshan return 0; 14484370aa4aSLai Jiangshan } 14494370aa4aSLai Jiangshan 14504370aa4aSLai Jiangshan str = buf; 14514370aa4aSLai Jiangshan end = buf + size; 14524370aa4aSLai Jiangshan 14534370aa4aSLai Jiangshan #define get_arg(type) \ 14544370aa4aSLai Jiangshan ({ \ 14554370aa4aSLai Jiangshan typeof(type) value; \ 14564370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 14574370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 14584370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 14594370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 14604370aa4aSLai Jiangshan } else { \ 14614370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 14624370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 14634370aa4aSLai Jiangshan } \ 14644370aa4aSLai Jiangshan args += sizeof(type); \ 14654370aa4aSLai Jiangshan value; \ 14664370aa4aSLai Jiangshan }) 14674370aa4aSLai Jiangshan 14684370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 14694370aa4aSLai Jiangshan if (end < buf) { 14704370aa4aSLai Jiangshan end = ((void *)-1); 14714370aa4aSLai Jiangshan size = end - buf; 14724370aa4aSLai Jiangshan } 14734370aa4aSLai Jiangshan 1474fef20d9cSFrederic Weisbecker while (*fmt) { 1475fef20d9cSFrederic Weisbecker int read; 1476fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1477fef20d9cSFrederic Weisbecker 1478fef20d9cSFrederic Weisbecker read = format_decode(fmt, &spec); 1479fef20d9cSFrederic Weisbecker 1480fef20d9cSFrederic Weisbecker fmt += read; 1481fef20d9cSFrederic Weisbecker 1482fef20d9cSFrederic Weisbecker switch (spec.type) { 1483fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1484fef20d9cSFrederic Weisbecker int copy = read; 1485fef20d9cSFrederic Weisbecker if (str < end) { 1486fef20d9cSFrederic Weisbecker if (copy > end - str) 1487fef20d9cSFrederic Weisbecker copy = end - str; 1488fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1489fef20d9cSFrederic Weisbecker } 1490fef20d9cSFrederic Weisbecker str += read; 1491fef20d9cSFrederic Weisbecker break; 14924370aa4aSLai Jiangshan } 14934370aa4aSLai Jiangshan 1494ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1495fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 1496fef20d9cSFrederic Weisbecker break; 14974370aa4aSLai Jiangshan 1498fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1499fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 1500fef20d9cSFrederic Weisbecker break; 15014370aa4aSLai Jiangshan 1502fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 1503fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1504fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 15054370aa4aSLai Jiangshan if (str < end) 15064370aa4aSLai Jiangshan *str = ' '; 15074370aa4aSLai Jiangshan ++str; 15084370aa4aSLai Jiangshan } 15094370aa4aSLai Jiangshan } 15104370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 15114370aa4aSLai Jiangshan if (str < end) 15124370aa4aSLai Jiangshan *str = c; 15134370aa4aSLai Jiangshan ++str; 1514fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 15154370aa4aSLai Jiangshan if (str < end) 15164370aa4aSLai Jiangshan *str = ' '; 15174370aa4aSLai Jiangshan ++str; 15184370aa4aSLai Jiangshan } 1519fef20d9cSFrederic Weisbecker break; 15204370aa4aSLai Jiangshan 1521fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 15224370aa4aSLai Jiangshan const char *str_arg = args; 15234370aa4aSLai Jiangshan size_t len = strlen(str_arg); 15244370aa4aSLai Jiangshan args += len + 1; 1525fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 1526fef20d9cSFrederic Weisbecker break; 15274370aa4aSLai Jiangshan } 15284370aa4aSLai Jiangshan 1529fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1530fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, get_arg(void *), spec); 1531fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 15324370aa4aSLai Jiangshan fmt++; 1533fef20d9cSFrederic Weisbecker break; 15344370aa4aSLai Jiangshan 1535fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 15364370aa4aSLai Jiangshan if (str < end) 15374370aa4aSLai Jiangshan *str = '%'; 15384370aa4aSLai Jiangshan ++str; 15394370aa4aSLai Jiangshan break; 15404370aa4aSLai Jiangshan 1541fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 15424370aa4aSLai Jiangshan if (str < end) 15434370aa4aSLai Jiangshan *str = '%'; 15444370aa4aSLai Jiangshan ++str; 1545fef20d9cSFrederic Weisbecker break; 1546fef20d9cSFrederic Weisbecker 1547fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: 1548fef20d9cSFrederic Weisbecker /* skip */ 1549fef20d9cSFrederic Weisbecker break; 1550fef20d9cSFrederic Weisbecker 1551fef20d9cSFrederic Weisbecker default: 1552fef20d9cSFrederic Weisbecker switch (spec.type) { 1553fef20d9cSFrederic Weisbecker 1554fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 15554370aa4aSLai Jiangshan num = get_arg(long long); 1556fef20d9cSFrederic Weisbecker break; 1557fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 15584370aa4aSLai Jiangshan num = get_arg(unsigned long); 1559fef20d9cSFrederic Weisbecker break; 1560fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1561fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 1562fef20d9cSFrederic Weisbecker break; 1563fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 15644370aa4aSLai Jiangshan num = get_arg(size_t); 1565fef20d9cSFrederic Weisbecker break; 1566fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 15674370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 1568fef20d9cSFrederic Weisbecker break; 1569a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1570a4e94ef0SZhaolei num = get_arg(unsigned char); 1571a4e94ef0SZhaolei break; 1572a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1573a4e94ef0SZhaolei num = get_arg(signed char); 1574a4e94ef0SZhaolei break; 1575fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1576fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 1577fef20d9cSFrederic Weisbecker break; 1578fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1579fef20d9cSFrederic Weisbecker num = get_arg(short); 1580fef20d9cSFrederic Weisbecker break; 1581fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 15824370aa4aSLai Jiangshan num = get_arg(unsigned int); 1583fef20d9cSFrederic Weisbecker break; 1584fef20d9cSFrederic Weisbecker default: 1585fef20d9cSFrederic Weisbecker num = get_arg(int); 15864370aa4aSLai Jiangshan } 1587fef20d9cSFrederic Weisbecker 1588fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 15894370aa4aSLai Jiangshan } 1590fef20d9cSFrederic Weisbecker } 1591fef20d9cSFrederic Weisbecker 15924370aa4aSLai Jiangshan if (size > 0) { 15934370aa4aSLai Jiangshan if (str < end) 15944370aa4aSLai Jiangshan *str = '\0'; 15954370aa4aSLai Jiangshan else 15964370aa4aSLai Jiangshan end[-1] = '\0'; 15974370aa4aSLai Jiangshan } 1598fef20d9cSFrederic Weisbecker 15994370aa4aSLai Jiangshan #undef get_arg 16004370aa4aSLai Jiangshan 16014370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 16024370aa4aSLai Jiangshan return str - buf; 16034370aa4aSLai Jiangshan } 16044370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 16054370aa4aSLai Jiangshan 16064370aa4aSLai Jiangshan /** 16074370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 16084370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 16094370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 16104370aa4aSLai Jiangshan * @fmt: The format string to use 16114370aa4aSLai Jiangshan * @...: Arguments for the format string 16124370aa4aSLai Jiangshan * 16134370aa4aSLai Jiangshan * The function returns the number of words(u32) written 16144370aa4aSLai Jiangshan * into @bin_buf. 16154370aa4aSLai Jiangshan */ 16164370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 16174370aa4aSLai Jiangshan { 16184370aa4aSLai Jiangshan va_list args; 16194370aa4aSLai Jiangshan int ret; 16204370aa4aSLai Jiangshan 16214370aa4aSLai Jiangshan va_start(args, fmt); 16224370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 16234370aa4aSLai Jiangshan va_end(args); 16244370aa4aSLai Jiangshan return ret; 16254370aa4aSLai Jiangshan } 16264370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 16274370aa4aSLai Jiangshan 16284370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 16294370aa4aSLai Jiangshan 16301da177e4SLinus Torvalds /** 16311da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 16321da177e4SLinus Torvalds * @buf: input buffer 16331da177e4SLinus Torvalds * @fmt: format of buffer 16341da177e4SLinus Torvalds * @args: arguments 16351da177e4SLinus Torvalds */ 16361da177e4SLinus Torvalds int vsscanf(const char * buf, const char * fmt, va_list args) 16371da177e4SLinus Torvalds { 16381da177e4SLinus Torvalds const char *str = buf; 16391da177e4SLinus Torvalds char *next; 16401da177e4SLinus Torvalds char digit; 16411da177e4SLinus Torvalds int num = 0; 16421da177e4SLinus Torvalds int qualifier; 16431da177e4SLinus Torvalds int base; 16441da177e4SLinus Torvalds int field_width; 16451da177e4SLinus Torvalds int is_sign = 0; 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds while(*fmt && *str) { 16481da177e4SLinus Torvalds /* skip any white space in format */ 16491da177e4SLinus Torvalds /* white space in format matchs any amount of 16501da177e4SLinus Torvalds * white space, including none, in the input. 16511da177e4SLinus Torvalds */ 16521da177e4SLinus Torvalds if (isspace(*fmt)) { 16531da177e4SLinus Torvalds while (isspace(*fmt)) 16541da177e4SLinus Torvalds ++fmt; 16551da177e4SLinus Torvalds while (isspace(*str)) 16561da177e4SLinus Torvalds ++str; 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 16601da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 16611da177e4SLinus Torvalds if (*fmt++ != *str++) 16621da177e4SLinus Torvalds break; 16631da177e4SLinus Torvalds continue; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds if (!*fmt) 16671da177e4SLinus Torvalds break; 16681da177e4SLinus Torvalds ++fmt; 16691da177e4SLinus Torvalds 16701da177e4SLinus Torvalds /* skip this conversion. 16711da177e4SLinus Torvalds * advance both strings to next white space 16721da177e4SLinus Torvalds */ 16731da177e4SLinus Torvalds if (*fmt == '*') { 16741da177e4SLinus Torvalds while (!isspace(*fmt) && *fmt) 16751da177e4SLinus Torvalds fmt++; 16761da177e4SLinus Torvalds while (!isspace(*str) && *str) 16771da177e4SLinus Torvalds str++; 16781da177e4SLinus Torvalds continue; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds /* get field width */ 16821da177e4SLinus Torvalds field_width = -1; 16831da177e4SLinus Torvalds if (isdigit(*fmt)) 16841da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* get conversion qualifier */ 16871da177e4SLinus Torvalds qualifier = -1; 16881da177e4SLinus Torvalds if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || 16891da177e4SLinus Torvalds *fmt == 'Z' || *fmt == 'z') { 16901da177e4SLinus Torvalds qualifier = *fmt++; 16911da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 16921da177e4SLinus Torvalds if (qualifier == 'h') { 16931da177e4SLinus Torvalds qualifier = 'H'; 16941da177e4SLinus Torvalds fmt++; 16951da177e4SLinus Torvalds } else if (qualifier == 'l') { 16961da177e4SLinus Torvalds qualifier = 'L'; 16971da177e4SLinus Torvalds fmt++; 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds } 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds base = 10; 17021da177e4SLinus Torvalds is_sign = 0; 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds if (!*fmt || !*str) 17051da177e4SLinus Torvalds break; 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds switch(*fmt++) { 17081da177e4SLinus Torvalds case 'c': 17091da177e4SLinus Torvalds { 17101da177e4SLinus Torvalds char *s = (char *) va_arg(args,char*); 17111da177e4SLinus Torvalds if (field_width == -1) 17121da177e4SLinus Torvalds field_width = 1; 17131da177e4SLinus Torvalds do { 17141da177e4SLinus Torvalds *s++ = *str++; 17151da177e4SLinus Torvalds } while (--field_width > 0 && *str); 17161da177e4SLinus Torvalds num++; 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds continue; 17191da177e4SLinus Torvalds case 's': 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds char *s = (char *) va_arg(args, char *); 17221da177e4SLinus Torvalds if(field_width == -1) 17231da177e4SLinus Torvalds field_width = INT_MAX; 17241da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 17251da177e4SLinus Torvalds while (isspace(*str)) 17261da177e4SLinus Torvalds str++; 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds /* now copy until next white space */ 17291da177e4SLinus Torvalds while (*str && !isspace(*str) && field_width--) { 17301da177e4SLinus Torvalds *s++ = *str++; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds *s = '\0'; 17331da177e4SLinus Torvalds num++; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds continue; 17361da177e4SLinus Torvalds case 'n': 17371da177e4SLinus Torvalds /* return number of characters read so far */ 17381da177e4SLinus Torvalds { 17391da177e4SLinus Torvalds int *i = (int *)va_arg(args,int*); 17401da177e4SLinus Torvalds *i = str - buf; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds continue; 17431da177e4SLinus Torvalds case 'o': 17441da177e4SLinus Torvalds base = 8; 17451da177e4SLinus Torvalds break; 17461da177e4SLinus Torvalds case 'x': 17471da177e4SLinus Torvalds case 'X': 17481da177e4SLinus Torvalds base = 16; 17491da177e4SLinus Torvalds break; 17501da177e4SLinus Torvalds case 'i': 17511da177e4SLinus Torvalds base = 0; 17521da177e4SLinus Torvalds case 'd': 17531da177e4SLinus Torvalds is_sign = 1; 17541da177e4SLinus Torvalds case 'u': 17551da177e4SLinus Torvalds break; 17561da177e4SLinus Torvalds case '%': 17571da177e4SLinus Torvalds /* looking for '%' in str */ 17581da177e4SLinus Torvalds if (*str++ != '%') 17591da177e4SLinus Torvalds return num; 17601da177e4SLinus Torvalds continue; 17611da177e4SLinus Torvalds default: 17621da177e4SLinus Torvalds /* invalid format; stop here */ 17631da177e4SLinus Torvalds return num; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds /* have some sort of integer conversion. 17671da177e4SLinus Torvalds * first, skip white space in buffer. 17681da177e4SLinus Torvalds */ 17691da177e4SLinus Torvalds while (isspace(*str)) 17701da177e4SLinus Torvalds str++; 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds digit = *str; 17731da177e4SLinus Torvalds if (is_sign && digit == '-') 17741da177e4SLinus Torvalds digit = *(str + 1); 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds if (!digit 17771da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 17781da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 17791da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 17801da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 17811da177e4SLinus Torvalds break; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds switch(qualifier) { 17841da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 17851da177e4SLinus Torvalds if (is_sign) { 17861da177e4SLinus Torvalds signed char *s = (signed char *) va_arg(args,signed char *); 17871da177e4SLinus Torvalds *s = (signed char) simple_strtol(str,&next,base); 17881da177e4SLinus Torvalds } else { 17891da177e4SLinus Torvalds unsigned char *s = (unsigned char *) va_arg(args, unsigned char *); 17901da177e4SLinus Torvalds *s = (unsigned char) simple_strtoul(str, &next, base); 17911da177e4SLinus Torvalds } 17921da177e4SLinus Torvalds break; 17931da177e4SLinus Torvalds case 'h': 17941da177e4SLinus Torvalds if (is_sign) { 17951da177e4SLinus Torvalds short *s = (short *) va_arg(args,short *); 17961da177e4SLinus Torvalds *s = (short) simple_strtol(str,&next,base); 17971da177e4SLinus Torvalds } else { 17981da177e4SLinus Torvalds unsigned short *s = (unsigned short *) va_arg(args, unsigned short *); 17991da177e4SLinus Torvalds *s = (unsigned short) simple_strtoul(str, &next, base); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds break; 18021da177e4SLinus Torvalds case 'l': 18031da177e4SLinus Torvalds if (is_sign) { 18041da177e4SLinus Torvalds long *l = (long *) va_arg(args,long *); 18051da177e4SLinus Torvalds *l = simple_strtol(str,&next,base); 18061da177e4SLinus Torvalds } else { 18071da177e4SLinus Torvalds unsigned long *l = (unsigned long*) va_arg(args,unsigned long*); 18081da177e4SLinus Torvalds *l = simple_strtoul(str,&next,base); 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds break; 18111da177e4SLinus Torvalds case 'L': 18121da177e4SLinus Torvalds if (is_sign) { 18131da177e4SLinus Torvalds long long *l = (long long*) va_arg(args,long long *); 18141da177e4SLinus Torvalds *l = simple_strtoll(str,&next,base); 18151da177e4SLinus Torvalds } else { 18161da177e4SLinus Torvalds unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*); 18171da177e4SLinus Torvalds *l = simple_strtoull(str,&next,base); 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds break; 18201da177e4SLinus Torvalds case 'Z': 18211da177e4SLinus Torvalds case 'z': 18221da177e4SLinus Torvalds { 18231da177e4SLinus Torvalds size_t *s = (size_t*) va_arg(args,size_t*); 18241da177e4SLinus Torvalds *s = (size_t) simple_strtoul(str,&next,base); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds break; 18271da177e4SLinus Torvalds default: 18281da177e4SLinus Torvalds if (is_sign) { 18291da177e4SLinus Torvalds int *i = (int *) va_arg(args, int*); 18301da177e4SLinus Torvalds *i = (int) simple_strtol(str,&next,base); 18311da177e4SLinus Torvalds } else { 18321da177e4SLinus Torvalds unsigned int *i = (unsigned int*) va_arg(args, unsigned int*); 18331da177e4SLinus Torvalds *i = (unsigned int) simple_strtoul(str,&next,base); 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds break; 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds num++; 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds if (!next) 18401da177e4SLinus Torvalds break; 18411da177e4SLinus Torvalds str = next; 18421da177e4SLinus Torvalds } 1843c6b40d16SJohannes Berg 1844c6b40d16SJohannes Berg /* 1845c6b40d16SJohannes Berg * Now we've come all the way through so either the input string or the 1846c6b40d16SJohannes Berg * format ended. In the former case, there can be a %n at the current 1847c6b40d16SJohannes Berg * position in the format that needs to be filled. 1848c6b40d16SJohannes Berg */ 1849c6b40d16SJohannes Berg if (*fmt == '%' && *(fmt + 1) == 'n') { 1850c6b40d16SJohannes Berg int *p = (int *)va_arg(args, int *); 1851c6b40d16SJohannes Berg *p = str - buf; 1852c6b40d16SJohannes Berg } 1853c6b40d16SJohannes Berg 18541da177e4SLinus Torvalds return num; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds /** 18591da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 18601da177e4SLinus Torvalds * @buf: input buffer 18611da177e4SLinus Torvalds * @fmt: formatting of buffer 18621da177e4SLinus Torvalds * @...: resulting arguments 18631da177e4SLinus Torvalds */ 18641da177e4SLinus Torvalds int sscanf(const char * buf, const char * fmt, ...) 18651da177e4SLinus Torvalds { 18661da177e4SLinus Torvalds va_list args; 18671da177e4SLinus Torvalds int i; 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds va_start(args,fmt); 18701da177e4SLinus Torvalds i = vsscanf(buf,fmt,args); 18711da177e4SLinus Torvalds va_end(args); 18721da177e4SLinus Torvalds return i; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 1875