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> 288a27f7c9SJoe Perches #include <net/addrconf.h> 291da177e4SLinus Torvalds 304e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 311da177e4SLinus Torvalds #include <asm/div64.h> 32deac93dfSJames Bottomley #include <asm/sections.h> /* for dereference_function_descriptor() */ 331da177e4SLinus Torvalds 349b706aeeSDenys Vlasenko /* Works only for digits and letters, but small and fast */ 359b706aeeSDenys Vlasenko #define TOLOWER(x) ((x) | 0x20) 369b706aeeSDenys Vlasenko 37aa46a63eSHarvey Harrison static unsigned int simple_guess_base(const char *cp) 38aa46a63eSHarvey Harrison { 39aa46a63eSHarvey Harrison if (cp[0] == '0') { 40aa46a63eSHarvey Harrison if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2])) 41aa46a63eSHarvey Harrison return 16; 42aa46a63eSHarvey Harrison else 43aa46a63eSHarvey Harrison return 8; 44aa46a63eSHarvey Harrison } else { 45aa46a63eSHarvey Harrison return 10; 46aa46a63eSHarvey Harrison } 47aa46a63eSHarvey Harrison } 48aa46a63eSHarvey Harrison 491da177e4SLinus Torvalds /** 501da177e4SLinus Torvalds * simple_strtoul - convert a string to an unsigned long 511da177e4SLinus Torvalds * @cp: The start of the string 521da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 531da177e4SLinus Torvalds * @base: The number base to use 541da177e4SLinus Torvalds */ 551da177e4SLinus Torvalds unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 561da177e4SLinus Torvalds { 57aa46a63eSHarvey Harrison unsigned long result = 0; 581da177e4SLinus Torvalds 59aa46a63eSHarvey Harrison if (!base) 60aa46a63eSHarvey Harrison base = simple_guess_base(cp); 61aa46a63eSHarvey Harrison 62aa46a63eSHarvey Harrison if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') 631da177e4SLinus Torvalds cp += 2; 64aa46a63eSHarvey Harrison 65aa46a63eSHarvey Harrison while (isxdigit(*cp)) { 66aa46a63eSHarvey Harrison unsigned int value; 67aa46a63eSHarvey Harrison 68aa46a63eSHarvey Harrison value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; 69aa46a63eSHarvey Harrison if (value >= base) 70aa46a63eSHarvey Harrison break; 711da177e4SLinus Torvalds result = result * base + value; 721da177e4SLinus Torvalds cp++; 731da177e4SLinus Torvalds } 74aa46a63eSHarvey Harrison 751da177e4SLinus Torvalds if (endp) 761da177e4SLinus Torvalds *endp = (char *)cp; 771da177e4SLinus Torvalds return result; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoul); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /** 821da177e4SLinus Torvalds * simple_strtol - convert a string to a signed long 831da177e4SLinus Torvalds * @cp: The start of the string 841da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 851da177e4SLinus Torvalds * @base: The number base to use 861da177e4SLinus Torvalds */ 871da177e4SLinus Torvalds long simple_strtol(const char *cp, char **endp, unsigned int base) 881da177e4SLinus Torvalds { 891da177e4SLinus Torvalds if(*cp == '-') 901da177e4SLinus Torvalds return -simple_strtoul(cp + 1, endp, base); 911da177e4SLinus Torvalds return simple_strtoul(cp, endp, base); 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtol); 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds /** 961da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 971da177e4SLinus Torvalds * @cp: The start of the string 981da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 991da177e4SLinus Torvalds * @base: The number base to use 1001da177e4SLinus Torvalds */ 1011da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 1021da177e4SLinus Torvalds { 103aa46a63eSHarvey Harrison unsigned long long result = 0; 1041da177e4SLinus Torvalds 105aa46a63eSHarvey Harrison if (!base) 106aa46a63eSHarvey Harrison base = simple_guess_base(cp); 107aa46a63eSHarvey Harrison 108aa46a63eSHarvey Harrison if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x') 1091da177e4SLinus Torvalds cp += 2; 110aa46a63eSHarvey Harrison 111aa46a63eSHarvey Harrison while (isxdigit(*cp)) { 112aa46a63eSHarvey Harrison unsigned int value; 113aa46a63eSHarvey Harrison 114aa46a63eSHarvey Harrison value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10; 115aa46a63eSHarvey Harrison if (value >= base) 116aa46a63eSHarvey Harrison break; 1171da177e4SLinus Torvalds result = result * base + value; 1181da177e4SLinus Torvalds cp++; 1191da177e4SLinus Torvalds } 120aa46a63eSHarvey Harrison 1211da177e4SLinus Torvalds if (endp) 1221da177e4SLinus Torvalds *endp = (char *)cp; 1231da177e4SLinus Torvalds return result; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /** 1281da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1291da177e4SLinus Torvalds * @cp: The start of the string 1301da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1311da177e4SLinus Torvalds * @base: The number base to use 1321da177e4SLinus Torvalds */ 1331da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds if(*cp=='-') 1361da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1371da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 14006b2a76dSYi Yang /** 14106b2a76dSYi Yang * strict_strtoul - convert a string to an unsigned long strictly 14206b2a76dSYi Yang * @cp: The string to be converted 14306b2a76dSYi Yang * @base: The number base to use 14406b2a76dSYi Yang * @res: The converted result value 14506b2a76dSYi Yang * 14606b2a76dSYi Yang * strict_strtoul converts a string to an unsigned long only if the 14706b2a76dSYi Yang * string is really an unsigned long string, any string containing 14806b2a76dSYi Yang * any invalid char at the tail will be rejected and -EINVAL is returned, 14906b2a76dSYi Yang * only a newline char at the tail is acceptible because people generally 15006b2a76dSYi Yang * change a module parameter in the following way: 15106b2a76dSYi Yang * 15206b2a76dSYi Yang * echo 1024 > /sys/module/e1000/parameters/copybreak 15306b2a76dSYi Yang * 15406b2a76dSYi Yang * echo will append a newline to the tail. 15506b2a76dSYi Yang * 15606b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 15706b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 15806b2a76dSYi Yang * 15906b2a76dSYi Yang * simple_strtoul just ignores the successive invalid characters and 16006b2a76dSYi Yang * return the converted value of prefix part of the string. 16106b2a76dSYi Yang */ 1629d85db22SHarvey Harrison int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) 1639d85db22SHarvey Harrison { 1649d85db22SHarvey Harrison char *tail; 1659d85db22SHarvey Harrison unsigned long val; 1669d85db22SHarvey Harrison size_t len; 1679d85db22SHarvey Harrison 1689d85db22SHarvey Harrison *res = 0; 1699d85db22SHarvey Harrison len = strlen(cp); 1709d85db22SHarvey Harrison if (len == 0) 1719d85db22SHarvey Harrison return -EINVAL; 1729d85db22SHarvey Harrison 1739d85db22SHarvey Harrison val = simple_strtoul(cp, &tail, base); 174e899aa82SPavel Machek if (tail == cp) 175e899aa82SPavel Machek return -EINVAL; 1769d85db22SHarvey Harrison if ((*tail == '\0') || 1779d85db22SHarvey Harrison ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { 1789d85db22SHarvey Harrison *res = val; 1799d85db22SHarvey Harrison return 0; 1809d85db22SHarvey Harrison } 1819d85db22SHarvey Harrison 1829d85db22SHarvey Harrison return -EINVAL; 1839d85db22SHarvey Harrison } 1849d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoul); 18506b2a76dSYi Yang 18606b2a76dSYi Yang /** 18706b2a76dSYi Yang * strict_strtol - convert a string to a long strictly 18806b2a76dSYi Yang * @cp: The string to be converted 18906b2a76dSYi Yang * @base: The number base to use 19006b2a76dSYi Yang * @res: The converted result value 19106b2a76dSYi Yang * 19206b2a76dSYi Yang * strict_strtol is similiar to strict_strtoul, but it allows the first 19306b2a76dSYi Yang * character of a string is '-'. 19406b2a76dSYi Yang * 19506b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 19606b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 19706b2a76dSYi Yang */ 1989d85db22SHarvey Harrison int strict_strtol(const char *cp, unsigned int base, long *res) 1999d85db22SHarvey Harrison { 2009d85db22SHarvey Harrison int ret; 2019d85db22SHarvey Harrison if (*cp == '-') { 2029d85db22SHarvey Harrison ret = strict_strtoul(cp + 1, base, (unsigned long *)res); 2039d85db22SHarvey Harrison if (!ret) 2049d85db22SHarvey Harrison *res = -(*res); 2059d85db22SHarvey Harrison } else { 2069d85db22SHarvey Harrison ret = strict_strtoul(cp, base, (unsigned long *)res); 2079d85db22SHarvey Harrison } 2089d85db22SHarvey Harrison 2099d85db22SHarvey Harrison return ret; 2109d85db22SHarvey Harrison } 2119d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtol); 21206b2a76dSYi Yang 21306b2a76dSYi Yang /** 21406b2a76dSYi Yang * strict_strtoull - convert a string to an unsigned long long strictly 21506b2a76dSYi Yang * @cp: The string to be converted 21606b2a76dSYi Yang * @base: The number base to use 21706b2a76dSYi Yang * @res: The converted result value 21806b2a76dSYi Yang * 21906b2a76dSYi Yang * strict_strtoull converts a string to an unsigned long long only if the 22006b2a76dSYi Yang * string is really an unsigned long long string, any string containing 22106b2a76dSYi Yang * any invalid char at the tail will be rejected and -EINVAL is returned, 22206b2a76dSYi Yang * only a newline char at the tail is acceptible because people generally 22306b2a76dSYi Yang * change a module parameter in the following way: 22406b2a76dSYi Yang * 22506b2a76dSYi Yang * echo 1024 > /sys/module/e1000/parameters/copybreak 22606b2a76dSYi Yang * 22706b2a76dSYi Yang * echo will append a newline to the tail of the string. 22806b2a76dSYi Yang * 22906b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 23006b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 23106b2a76dSYi Yang * 23206b2a76dSYi Yang * simple_strtoull just ignores the successive invalid characters and 23306b2a76dSYi Yang * return the converted value of prefix part of the string. 23406b2a76dSYi Yang */ 2359d85db22SHarvey Harrison int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res) 2369d85db22SHarvey Harrison { 2379d85db22SHarvey Harrison char *tail; 2389d85db22SHarvey Harrison unsigned long long val; 2399d85db22SHarvey Harrison size_t len; 2409d85db22SHarvey Harrison 2419d85db22SHarvey Harrison *res = 0; 2429d85db22SHarvey Harrison len = strlen(cp); 2439d85db22SHarvey Harrison if (len == 0) 2449d85db22SHarvey Harrison return -EINVAL; 2459d85db22SHarvey Harrison 2469d85db22SHarvey Harrison val = simple_strtoull(cp, &tail, base); 247e899aa82SPavel Machek if (tail == cp) 248e899aa82SPavel Machek return -EINVAL; 2499d85db22SHarvey Harrison if ((*tail == '\0') || 2509d85db22SHarvey Harrison ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { 2519d85db22SHarvey Harrison *res = val; 2529d85db22SHarvey Harrison return 0; 2539d85db22SHarvey Harrison } 2549d85db22SHarvey Harrison 2559d85db22SHarvey Harrison return -EINVAL; 2569d85db22SHarvey Harrison } 2579d85db22SHarvey Harrison EXPORT_SYMBOL(strict_strtoull); 25806b2a76dSYi Yang 25906b2a76dSYi Yang /** 26006b2a76dSYi Yang * strict_strtoll - convert a string to a long long strictly 26106b2a76dSYi Yang * @cp: The string to be converted 26206b2a76dSYi Yang * @base: The number base to use 26306b2a76dSYi Yang * @res: The converted result value 26406b2a76dSYi Yang * 26506b2a76dSYi Yang * strict_strtoll is similiar to strict_strtoull, but it allows the first 26606b2a76dSYi Yang * character of a string is '-'. 26706b2a76dSYi Yang * 26806b2a76dSYi Yang * It returns 0 if conversion is successful and *res is set to the converted 26906b2a76dSYi Yang * value, otherwise it returns -EINVAL and *res is set to 0. 27006b2a76dSYi Yang */ 2719d85db22SHarvey Harrison int strict_strtoll(const char *cp, unsigned int base, long long *res) 2729d85db22SHarvey Harrison { 2739d85db22SHarvey Harrison int ret; 2749d85db22SHarvey Harrison if (*cp == '-') { 2759d85db22SHarvey Harrison ret = strict_strtoull(cp + 1, base, (unsigned long long *)res); 2769d85db22SHarvey Harrison if (!ret) 2779d85db22SHarvey Harrison *res = -(*res); 2789d85db22SHarvey Harrison } else { 2799d85db22SHarvey Harrison ret = strict_strtoull(cp, base, (unsigned long long *)res); 2809d85db22SHarvey Harrison } 28106b2a76dSYi Yang 2829d85db22SHarvey Harrison return ret; 2839d85db22SHarvey Harrison } 28406b2a76dSYi Yang EXPORT_SYMBOL(strict_strtoll); 28506b2a76dSYi Yang 2861da177e4SLinus Torvalds static int skip_atoi(const char **s) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds int i=0; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds while (isdigit(**s)) 2911da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 2921da177e4SLinus Torvalds return i; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds 2954277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used 2964277eeddSDenis Vlasenko * for /proc and /sys data. This directly impacts e.g. top performance 2974277eeddSDenis Vlasenko * with many processes running. We optimize it for speed 2984277eeddSDenis Vlasenko * using code from 2994277eeddSDenis Vlasenko * http://www.cs.uiowa.edu/~jones/bcd/decimal.html 3004277eeddSDenis Vlasenko * (with permission from the author, Douglas W. Jones). */ 3014277eeddSDenis Vlasenko 3024277eeddSDenis Vlasenko /* Formats correctly any integer in [0,99999]. 3034277eeddSDenis Vlasenko * Outputs from one to five digits depending on input. 3044277eeddSDenis Vlasenko * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ 3054277eeddSDenis Vlasenko static char* put_dec_trunc(char *buf, unsigned q) 3064277eeddSDenis Vlasenko { 3074277eeddSDenis Vlasenko unsigned d3, d2, d1, d0; 3084277eeddSDenis Vlasenko d1 = (q>>4) & 0xf; 3094277eeddSDenis Vlasenko d2 = (q>>8) & 0xf; 3104277eeddSDenis Vlasenko d3 = (q>>12); 3114277eeddSDenis Vlasenko 3124277eeddSDenis Vlasenko d0 = 6*(d3 + d2 + d1) + (q & 0xf); 3134277eeddSDenis Vlasenko q = (d0 * 0xcd) >> 11; 3144277eeddSDenis Vlasenko d0 = d0 - 10*q; 3154277eeddSDenis Vlasenko *buf++ = d0 + '0'; /* least significant digit */ 3164277eeddSDenis Vlasenko d1 = q + 9*d3 + 5*d2 + d1; 3174277eeddSDenis Vlasenko if (d1 != 0) { 3184277eeddSDenis Vlasenko q = (d1 * 0xcd) >> 11; 3194277eeddSDenis Vlasenko d1 = d1 - 10*q; 3204277eeddSDenis Vlasenko *buf++ = d1 + '0'; /* next digit */ 3214277eeddSDenis Vlasenko 3224277eeddSDenis Vlasenko d2 = q + 2*d2; 3234277eeddSDenis Vlasenko if ((d2 != 0) || (d3 != 0)) { 3244277eeddSDenis Vlasenko q = (d2 * 0xd) >> 7; 3254277eeddSDenis Vlasenko d2 = d2 - 10*q; 3264277eeddSDenis Vlasenko *buf++ = d2 + '0'; /* next digit */ 3274277eeddSDenis Vlasenko 3284277eeddSDenis Vlasenko d3 = q + 4*d3; 3294277eeddSDenis Vlasenko if (d3 != 0) { 3304277eeddSDenis Vlasenko q = (d3 * 0xcd) >> 11; 3314277eeddSDenis Vlasenko d3 = d3 - 10*q; 3324277eeddSDenis Vlasenko *buf++ = d3 + '0'; /* next digit */ 3334277eeddSDenis Vlasenko if (q != 0) 3344277eeddSDenis Vlasenko *buf++ = q + '0'; /* most sign. digit */ 3354277eeddSDenis Vlasenko } 3364277eeddSDenis Vlasenko } 3374277eeddSDenis Vlasenko } 3384277eeddSDenis Vlasenko return buf; 3394277eeddSDenis Vlasenko } 3404277eeddSDenis Vlasenko /* Same with if's removed. Always emits five digits */ 3414277eeddSDenis Vlasenko static char* put_dec_full(char *buf, unsigned q) 3424277eeddSDenis Vlasenko { 3434277eeddSDenis Vlasenko /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ 3444277eeddSDenis Vlasenko /* but anyway, gcc produces better code with full-sized ints */ 3454277eeddSDenis Vlasenko unsigned d3, d2, d1, d0; 3464277eeddSDenis Vlasenko d1 = (q>>4) & 0xf; 3474277eeddSDenis Vlasenko d2 = (q>>8) & 0xf; 3484277eeddSDenis Vlasenko d3 = (q>>12); 3494277eeddSDenis Vlasenko 3504277eeddSDenis Vlasenko /* Possible ways to approx. divide by 10 */ 3514277eeddSDenis Vlasenko /* gcc -O2 replaces multiply with shifts and adds */ 3524277eeddSDenis Vlasenko // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) 3534277eeddSDenis Vlasenko // (x * 0x67) >> 10: 1100111 3544277eeddSDenis Vlasenko // (x * 0x34) >> 9: 110100 - same 3554277eeddSDenis Vlasenko // (x * 0x1a) >> 8: 11010 - same 3564277eeddSDenis Vlasenko // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) 3574277eeddSDenis Vlasenko 3584277eeddSDenis Vlasenko d0 = 6*(d3 + d2 + d1) + (q & 0xf); 3594277eeddSDenis Vlasenko q = (d0 * 0xcd) >> 11; 3604277eeddSDenis Vlasenko d0 = d0 - 10*q; 3614277eeddSDenis Vlasenko *buf++ = d0 + '0'; 3624277eeddSDenis Vlasenko d1 = q + 9*d3 + 5*d2 + d1; 3634277eeddSDenis Vlasenko q = (d1 * 0xcd) >> 11; 3644277eeddSDenis Vlasenko d1 = d1 - 10*q; 3654277eeddSDenis Vlasenko *buf++ = d1 + '0'; 3664277eeddSDenis Vlasenko 3674277eeddSDenis Vlasenko d2 = q + 2*d2; 3684277eeddSDenis Vlasenko q = (d2 * 0xd) >> 7; 3694277eeddSDenis Vlasenko d2 = d2 - 10*q; 3704277eeddSDenis Vlasenko *buf++ = d2 + '0'; 3714277eeddSDenis Vlasenko 3724277eeddSDenis Vlasenko d3 = q + 4*d3; 3734277eeddSDenis Vlasenko q = (d3 * 0xcd) >> 11; /* - shorter code */ 3744277eeddSDenis Vlasenko /* q = (d3 * 0x67) >> 10; - would also work */ 3754277eeddSDenis Vlasenko d3 = d3 - 10*q; 3764277eeddSDenis Vlasenko *buf++ = d3 + '0'; 3774277eeddSDenis Vlasenko *buf++ = q + '0'; 3784277eeddSDenis Vlasenko return buf; 3794277eeddSDenis Vlasenko } 3804277eeddSDenis Vlasenko /* No inlining helps gcc to use registers better */ 3814277eeddSDenis Vlasenko static noinline char* put_dec(char *buf, unsigned long long num) 3824277eeddSDenis Vlasenko { 3834277eeddSDenis Vlasenko while (1) { 3844277eeddSDenis Vlasenko unsigned rem; 3854277eeddSDenis Vlasenko if (num < 100000) 3864277eeddSDenis Vlasenko return put_dec_trunc(buf, num); 3874277eeddSDenis Vlasenko rem = do_div(num, 100000); 3884277eeddSDenis Vlasenko buf = put_dec_full(buf, rem); 3894277eeddSDenis Vlasenko } 3904277eeddSDenis Vlasenko } 3914277eeddSDenis Vlasenko 3921da177e4SLinus Torvalds #define ZEROPAD 1 /* pad with zero */ 3931da177e4SLinus Torvalds #define SIGN 2 /* unsigned/signed long */ 3941da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3951da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 3961da177e4SLinus Torvalds #define LEFT 16 /* left justified */ 3979b706aeeSDenys Vlasenko #define SMALL 32 /* Must be 32 == 0x20 */ 3989b706aeeSDenys Vlasenko #define SPECIAL 64 /* 0x */ 3991da177e4SLinus Torvalds 400fef20d9cSFrederic Weisbecker enum format_type { 401fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 402ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 403fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 404fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 405fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 406fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 407fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 408fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 409fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 410fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 411fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 412a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 413a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 414fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 415fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 416fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 417fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 418fef20d9cSFrederic Weisbecker FORMAT_TYPE_NRCHARS, 419fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 420fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 421fef20d9cSFrederic Weisbecker }; 422fef20d9cSFrederic Weisbecker 423fef20d9cSFrederic Weisbecker struct printf_spec { 424fef20d9cSFrederic Weisbecker enum format_type type; 425fef20d9cSFrederic Weisbecker int flags; /* flags to number() */ 426fef20d9cSFrederic Weisbecker int field_width; /* width of output field */ 427fef20d9cSFrederic Weisbecker int base; 428fef20d9cSFrederic Weisbecker int precision; /* # of digits/chars */ 429fef20d9cSFrederic Weisbecker int qualifier; 430fef20d9cSFrederic Weisbecker }; 431fef20d9cSFrederic Weisbecker 432fef20d9cSFrederic Weisbecker static char *number(char *buf, char *end, unsigned long long num, 433fef20d9cSFrederic Weisbecker struct printf_spec spec) 4341da177e4SLinus Torvalds { 4359b706aeeSDenys Vlasenko /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ 4369b706aeeSDenys Vlasenko static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ 4379b706aeeSDenys Vlasenko 4389b706aeeSDenys Vlasenko char tmp[66]; 4399b706aeeSDenys Vlasenko char sign; 4409b706aeeSDenys Vlasenko char locase; 441fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4421da177e4SLinus Torvalds int i; 4431da177e4SLinus Torvalds 4449b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4459b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 446fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 447fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 448fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4491da177e4SLinus Torvalds sign = 0; 450fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4511da177e4SLinus Torvalds if ((signed long long) num < 0) { 4521da177e4SLinus Torvalds sign = '-'; 4531da177e4SLinus Torvalds num = - (signed long long) num; 454fef20d9cSFrederic Weisbecker spec.field_width--; 455fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4561da177e4SLinus Torvalds sign = '+'; 457fef20d9cSFrederic Weisbecker spec.field_width--; 458fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4591da177e4SLinus Torvalds sign = ' '; 460fef20d9cSFrederic Weisbecker spec.field_width--; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds } 463b39a7340SDenis Vlasenko if (need_pfx) { 464fef20d9cSFrederic Weisbecker spec.field_width--; 465fef20d9cSFrederic Weisbecker if (spec.base == 16) 466fef20d9cSFrederic Weisbecker spec.field_width--; 4671da177e4SLinus Torvalds } 468b39a7340SDenis Vlasenko 469b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4701da177e4SLinus Torvalds i = 0; 4711da177e4SLinus Torvalds if (num == 0) 4721da177e4SLinus Torvalds tmp[i++] = '0'; 4734277eeddSDenis Vlasenko /* Generic code, for any base: 4744277eeddSDenis Vlasenko else do { 4759b706aeeSDenys Vlasenko tmp[i++] = (digits[do_div(num,base)] | locase); 4764277eeddSDenis Vlasenko } while (num != 0); 4774277eeddSDenis Vlasenko */ 478fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 479fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 480b39a7340SDenis Vlasenko int shift = 3; 481fef20d9cSFrederic Weisbecker if (spec.base == 16) shift = 4; 482b39a7340SDenis Vlasenko do { 4839b706aeeSDenys Vlasenko tmp[i++] = (digits[((unsigned char)num) & mask] | locase); 484b39a7340SDenis Vlasenko num >>= shift; 485b39a7340SDenis Vlasenko } while (num); 4864277eeddSDenis Vlasenko } else { /* base 10 */ 4874277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4884277eeddSDenis Vlasenko } 489b39a7340SDenis Vlasenko 490b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 491fef20d9cSFrederic Weisbecker if (i > spec.precision) 492fef20d9cSFrederic Weisbecker spec.precision = i; 493b39a7340SDenis Vlasenko /* leading space padding */ 494fef20d9cSFrederic Weisbecker spec.field_width -= spec.precision; 495fef20d9cSFrederic Weisbecker if (!(spec.flags & (ZEROPAD+LEFT))) { 496fef20d9cSFrederic Weisbecker while(--spec.field_width >= 0) { 497f796937aSJeremy Fitzhardinge if (buf < end) 4981da177e4SLinus Torvalds *buf = ' '; 4991da177e4SLinus Torvalds ++buf; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds } 502b39a7340SDenis Vlasenko /* sign */ 5031da177e4SLinus Torvalds if (sign) { 504f796937aSJeremy Fitzhardinge if (buf < end) 5051da177e4SLinus Torvalds *buf = sign; 5061da177e4SLinus Torvalds ++buf; 5071da177e4SLinus Torvalds } 508b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 509b39a7340SDenis Vlasenko if (need_pfx) { 510f796937aSJeremy Fitzhardinge if (buf < end) 5111da177e4SLinus Torvalds *buf = '0'; 5121da177e4SLinus Torvalds ++buf; 513fef20d9cSFrederic Weisbecker if (spec.base == 16) { 514f796937aSJeremy Fitzhardinge if (buf < end) 5159b706aeeSDenys Vlasenko *buf = ('X' | locase); 5161da177e4SLinus Torvalds ++buf; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds } 519b39a7340SDenis Vlasenko /* zero or space padding */ 520fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 521fef20d9cSFrederic Weisbecker char c = (spec.flags & ZEROPAD) ? '0' : ' '; 522fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 523f796937aSJeremy Fitzhardinge if (buf < end) 5241da177e4SLinus Torvalds *buf = c; 5251da177e4SLinus Torvalds ++buf; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds } 528b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 529fef20d9cSFrederic Weisbecker while (i <= --spec.precision) { 530f796937aSJeremy Fitzhardinge if (buf < end) 5311da177e4SLinus Torvalds *buf = '0'; 5321da177e4SLinus Torvalds ++buf; 5331da177e4SLinus Torvalds } 534b39a7340SDenis Vlasenko /* actual digits of result */ 535b39a7340SDenis Vlasenko while (--i >= 0) { 536f796937aSJeremy Fitzhardinge if (buf < end) 5371da177e4SLinus Torvalds *buf = tmp[i]; 5381da177e4SLinus Torvalds ++buf; 5391da177e4SLinus Torvalds } 540b39a7340SDenis Vlasenko /* trailing space padding */ 541fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 542f796937aSJeremy Fitzhardinge if (buf < end) 5431da177e4SLinus Torvalds *buf = ' '; 5441da177e4SLinus Torvalds ++buf; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds return buf; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 549*0f4f81dcSAndré Goddard Rosa static char *string(char *buf, char *end, const char *s, struct printf_spec spec) 5500f9bfa56SLinus Torvalds { 5510f9bfa56SLinus Torvalds int len, i; 5520f9bfa56SLinus Torvalds 5530f9bfa56SLinus Torvalds if ((unsigned long)s < PAGE_SIZE) 554*0f4f81dcSAndré Goddard Rosa s = "(null)"; 5550f9bfa56SLinus Torvalds 556fef20d9cSFrederic Weisbecker len = strnlen(s, spec.precision); 5570f9bfa56SLinus Torvalds 558fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 559fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5600f9bfa56SLinus Torvalds if (buf < end) 5610f9bfa56SLinus Torvalds *buf = ' '; 5620f9bfa56SLinus Torvalds ++buf; 5630f9bfa56SLinus Torvalds } 5640f9bfa56SLinus Torvalds } 5650f9bfa56SLinus Torvalds for (i = 0; i < len; ++i) { 5660f9bfa56SLinus Torvalds if (buf < end) 5670f9bfa56SLinus Torvalds *buf = *s; 5680f9bfa56SLinus Torvalds ++buf; ++s; 5690f9bfa56SLinus Torvalds } 570fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5710f9bfa56SLinus Torvalds if (buf < end) 5720f9bfa56SLinus Torvalds *buf = ' '; 5730f9bfa56SLinus Torvalds ++buf; 5740f9bfa56SLinus Torvalds } 5750f9bfa56SLinus Torvalds return buf; 5760f9bfa56SLinus Torvalds } 5770f9bfa56SLinus Torvalds 578fef20d9cSFrederic Weisbecker static char *symbol_string(char *buf, char *end, void *ptr, 5790c8b946eSFrederic Weisbecker struct printf_spec spec, char ext) 5800fe1ef24SLinus Torvalds { 5810fe1ef24SLinus Torvalds unsigned long value = (unsigned long) ptr; 5820fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 5830fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 58491adcd2cSSteven Rostedt if (ext != 'f' && ext != 's') 5850fe1ef24SLinus Torvalds sprint_symbol(sym, value); 5860c8b946eSFrederic Weisbecker else 5870c8b946eSFrederic Weisbecker kallsyms_lookup(value, NULL, NULL, NULL, sym); 588fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 5890fe1ef24SLinus Torvalds #else 590fef20d9cSFrederic Weisbecker spec.field_width = 2*sizeof(void *); 591fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL | SMALL | ZEROPAD; 592fef20d9cSFrederic Weisbecker spec.base = 16; 593fef20d9cSFrederic Weisbecker return number(buf, end, value, spec); 5940fe1ef24SLinus Torvalds #endif 5950fe1ef24SLinus Torvalds } 5960fe1ef24SLinus Torvalds 597fef20d9cSFrederic Weisbecker static char *resource_string(char *buf, char *end, struct resource *res, 598fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 599332d2e78SLinus Torvalds { 600332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 60128405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 602332d2e78SLinus Torvalds #endif 603332d2e78SLinus Torvalds 604332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 60528405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 606332d2e78SLinus Torvalds #endif 607c91d3376SBjorn Helgaas struct printf_spec hex_spec = { 608fef20d9cSFrederic Weisbecker .base = 16, 609fef20d9cSFrederic Weisbecker .precision = -1, 610fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 611fef20d9cSFrederic Weisbecker }; 612c91d3376SBjorn Helgaas struct printf_spec dec_spec = { 613c91d3376SBjorn Helgaas .base = 10, 614c91d3376SBjorn Helgaas .precision = -1, 615c91d3376SBjorn Helgaas .flags = 0, 616c91d3376SBjorn Helgaas }; 617fd95541eSBjorn Helgaas struct printf_spec str_spec = { 618fd95541eSBjorn Helgaas .field_width = -1, 619fd95541eSBjorn Helgaas .precision = 10, 620fd95541eSBjorn Helgaas .flags = LEFT, 621fd95541eSBjorn Helgaas }; 622fd95541eSBjorn Helgaas struct printf_spec flag_spec = { 623fd95541eSBjorn Helgaas .base = 16, 624fd95541eSBjorn Helgaas .precision = -1, 625fd95541eSBjorn Helgaas .flags = SPECIAL | SMALL, 626fd95541eSBjorn Helgaas }; 627c7dabef8SBjorn Helgaas 628c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 629c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 630c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 631c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 632c7dabef8SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref disabled]") 633c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 634c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 635c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 636c7dabef8SBjorn Helgaas 637332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 638c91d3376SBjorn Helgaas int size = -1, addr = 0; 639c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 640332d2e78SLinus Torvalds 641c91d3376SBjorn Helgaas if (res->flags & IORESOURCE_IO) { 642332d2e78SLinus Torvalds size = IO_RSRC_PRINTK_SIZE; 643c91d3376SBjorn Helgaas addr = 1; 644c91d3376SBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 645332d2e78SLinus Torvalds size = MEM_RSRC_PRINTK_SIZE; 646c91d3376SBjorn Helgaas addr = 1; 647c91d3376SBjorn Helgaas } 648332d2e78SLinus Torvalds 649332d2e78SLinus Torvalds *p++ = '['; 650fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_IO) 651fd95541eSBjorn Helgaas p = string(p, pend, "io ", str_spec); 652fd95541eSBjorn Helgaas else if (res->flags & IORESOURCE_MEM) 653fd95541eSBjorn Helgaas p = string(p, pend, "mem ", str_spec); 654fd95541eSBjorn Helgaas else if (res->flags & IORESOURCE_IRQ) 655fd95541eSBjorn Helgaas p = string(p, pend, "irq ", str_spec); 656fd95541eSBjorn Helgaas else if (res->flags & IORESOURCE_DMA) 657fd95541eSBjorn Helgaas p = string(p, pend, "dma ", str_spec); 658c7dabef8SBjorn Helgaas else { 659c7dabef8SBjorn Helgaas p = string(p, pend, "??? ", str_spec); 660c7dabef8SBjorn Helgaas decode = 0; 661fd95541eSBjorn Helgaas } 662c91d3376SBjorn Helgaas hex_spec.field_width = size; 663c91d3376SBjorn Helgaas p = number(p, pend, res->start, addr ? hex_spec : dec_spec); 664c91d3376SBjorn Helgaas if (res->start != res->end) { 665332d2e78SLinus Torvalds *p++ = '-'; 666c91d3376SBjorn Helgaas p = number(p, pend, res->end, addr ? hex_spec : dec_spec); 667c91d3376SBjorn Helgaas } 668c7dabef8SBjorn Helgaas if (decode) { 669fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 670fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 671fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 672fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 673fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 674fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 675c7dabef8SBjorn Helgaas } else { 676fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 677c7dabef8SBjorn Helgaas p = number(p, pend, res->flags, flag_spec); 678fd95541eSBjorn Helgaas } 679332d2e78SLinus Torvalds *p++ = ']'; 680c7dabef8SBjorn Helgaas *p = '\0'; 681332d2e78SLinus Torvalds 682fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 683332d2e78SLinus Torvalds } 684332d2e78SLinus Torvalds 685fef20d9cSFrederic Weisbecker static char *mac_address_string(char *buf, char *end, u8 *addr, 6868a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 687dd45c9cfSHarvey Harrison { 6888a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 689dd45c9cfSHarvey Harrison char *p = mac_addr; 690dd45c9cfSHarvey Harrison int i; 691dd45c9cfSHarvey Harrison 692dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 693dd45c9cfSHarvey Harrison p = pack_hex_byte(p, addr[i]); 6948a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 695dd45c9cfSHarvey Harrison *p++ = ':'; 696dd45c9cfSHarvey Harrison } 697dd45c9cfSHarvey Harrison *p = '\0'; 698dd45c9cfSHarvey Harrison 699fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 700dd45c9cfSHarvey Harrison } 701dd45c9cfSHarvey Harrison 7028a27f7c9SJoe Perches static char *ip4_string(char *p, const u8 *addr, bool leading_zeros) 703689afa7dSHarvey Harrison { 704689afa7dSHarvey Harrison int i; 705689afa7dSHarvey Harrison 7068a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 7078a27f7c9SJoe Perches char temp[3]; /* hold each IP quad in reverse order */ 7088a27f7c9SJoe Perches int digits = put_dec_trunc(temp, addr[i]) - temp; 7098a27f7c9SJoe Perches if (leading_zeros) { 7108a27f7c9SJoe Perches if (digits < 3) 7118a27f7c9SJoe Perches *p++ = '0'; 7128a27f7c9SJoe Perches if (digits < 2) 7138a27f7c9SJoe Perches *p++ = '0'; 7148a27f7c9SJoe Perches } 7158a27f7c9SJoe Perches /* reverse the digits in the quad */ 7168a27f7c9SJoe Perches while (digits--) 7178a27f7c9SJoe Perches *p++ = temp[digits]; 7188a27f7c9SJoe Perches if (i < 3) 7198a27f7c9SJoe Perches *p++ = '.'; 7208a27f7c9SJoe Perches } 7218a27f7c9SJoe Perches 7228a27f7c9SJoe Perches *p = '\0'; 7238a27f7c9SJoe Perches return p; 7248a27f7c9SJoe Perches } 7258a27f7c9SJoe Perches 726eb78cd26SJoe Perches static char *ip6_compressed_string(char *p, const char *addr) 7278a27f7c9SJoe Perches { 7288a27f7c9SJoe Perches int i; 7298a27f7c9SJoe Perches int j; 7308a27f7c9SJoe Perches int range; 7318a27f7c9SJoe Perches unsigned char zerolength[8]; 7328a27f7c9SJoe Perches int longest = 1; 7338a27f7c9SJoe Perches int colonpos = -1; 7348a27f7c9SJoe Perches u16 word; 7358a27f7c9SJoe Perches u8 hi; 7368a27f7c9SJoe Perches u8 lo; 7378a27f7c9SJoe Perches bool needcolon = false; 738eb78cd26SJoe Perches bool useIPv4; 739eb78cd26SJoe Perches struct in6_addr in6; 740eb78cd26SJoe Perches 741eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 742eb78cd26SJoe Perches 743eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 7448a27f7c9SJoe Perches 7458a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 7468a27f7c9SJoe Perches 7478a27f7c9SJoe Perches if (useIPv4) 7488a27f7c9SJoe Perches range = 6; 7498a27f7c9SJoe Perches else 7508a27f7c9SJoe Perches range = 8; 7518a27f7c9SJoe Perches 7528a27f7c9SJoe Perches /* find position of longest 0 run */ 7538a27f7c9SJoe Perches for (i = 0; i < range; i++) { 7548a27f7c9SJoe Perches for (j = i; j < range; j++) { 755eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 7568a27f7c9SJoe Perches break; 7578a27f7c9SJoe Perches zerolength[i]++; 7588a27f7c9SJoe Perches } 7598a27f7c9SJoe Perches } 7608a27f7c9SJoe Perches for (i = 0; i < range; i++) { 7618a27f7c9SJoe Perches if (zerolength[i] > longest) { 7628a27f7c9SJoe Perches longest = zerolength[i]; 7638a27f7c9SJoe Perches colonpos = i; 7648a27f7c9SJoe Perches } 7658a27f7c9SJoe Perches } 7668a27f7c9SJoe Perches 7678a27f7c9SJoe Perches /* emit address */ 7688a27f7c9SJoe Perches for (i = 0; i < range; i++) { 7698a27f7c9SJoe Perches if (i == colonpos) { 7708a27f7c9SJoe Perches if (needcolon || i == 0) 7718a27f7c9SJoe Perches *p++ = ':'; 7728a27f7c9SJoe Perches *p++ = ':'; 7738a27f7c9SJoe Perches needcolon = false; 7748a27f7c9SJoe Perches i += longest - 1; 7758a27f7c9SJoe Perches continue; 7768a27f7c9SJoe Perches } 7778a27f7c9SJoe Perches if (needcolon) { 7788a27f7c9SJoe Perches *p++ = ':'; 7798a27f7c9SJoe Perches needcolon = false; 7808a27f7c9SJoe Perches } 7818a27f7c9SJoe Perches /* hex u16 without leading 0s */ 782eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 7838a27f7c9SJoe Perches hi = word >> 8; 7848a27f7c9SJoe Perches lo = word & 0xff; 7858a27f7c9SJoe Perches if (hi) { 7868a27f7c9SJoe Perches if (hi > 0x0f) 7878a27f7c9SJoe Perches p = pack_hex_byte(p, hi); 7888a27f7c9SJoe Perches else 7898a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 7908a27f7c9SJoe Perches } 7918a27f7c9SJoe Perches if (hi || lo > 0x0f) 7928a27f7c9SJoe Perches p = pack_hex_byte(p, lo); 7938a27f7c9SJoe Perches else 7948a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 7958a27f7c9SJoe Perches needcolon = true; 7968a27f7c9SJoe Perches } 7978a27f7c9SJoe Perches 7988a27f7c9SJoe Perches if (useIPv4) { 7998a27f7c9SJoe Perches if (needcolon) 8008a27f7c9SJoe Perches *p++ = ':'; 801eb78cd26SJoe Perches p = ip4_string(p, &in6.s6_addr[12], false); 8028a27f7c9SJoe Perches } 8038a27f7c9SJoe Perches 8048a27f7c9SJoe Perches *p = '\0'; 8058a27f7c9SJoe Perches return p; 8068a27f7c9SJoe Perches } 8078a27f7c9SJoe Perches 808eb78cd26SJoe Perches static char *ip6_string(char *p, const char *addr, const char *fmt) 8098a27f7c9SJoe Perches { 8108a27f7c9SJoe Perches int i; 811689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 812eb78cd26SJoe Perches p = pack_hex_byte(p, *addr++); 813eb78cd26SJoe Perches p = pack_hex_byte(p, *addr++); 8148a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 815689afa7dSHarvey Harrison *p++ = ':'; 816689afa7dSHarvey Harrison } 8178a27f7c9SJoe Perches 818689afa7dSHarvey Harrison *p = '\0'; 8198a27f7c9SJoe Perches return p; 8208a27f7c9SJoe Perches } 8218a27f7c9SJoe Perches 8228a27f7c9SJoe Perches static char *ip6_addr_string(char *buf, char *end, const u8 *addr, 8238a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 8248a27f7c9SJoe Perches { 8258a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 8268a27f7c9SJoe Perches 8278a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 828eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 8298a27f7c9SJoe Perches else 830eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 831689afa7dSHarvey Harrison 832fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 833689afa7dSHarvey Harrison } 834689afa7dSHarvey Harrison 8358a27f7c9SJoe Perches static char *ip4_addr_string(char *buf, char *end, const u8 *addr, 8368a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 8374aa99606SHarvey Harrison { 8388a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 8394aa99606SHarvey Harrison 8408a27f7c9SJoe Perches ip4_string(ip4_addr, addr, fmt[0] == 'i'); 8414aa99606SHarvey Harrison 842fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 8434aa99606SHarvey Harrison } 8444aa99606SHarvey Harrison 8454d8a743cSLinus Torvalds /* 8464d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 8474d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 8484d8a743cSLinus Torvalds * specifiers. 8494d8a743cSLinus Torvalds * 850332d2e78SLinus Torvalds * Right now we handle: 8510fe1ef24SLinus Torvalds * 8520c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 8530c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 8540efb4d20SSteven Rostedt * - 'S' For symbolic direct pointers with offset 8550efb4d20SSteven Rostedt * - 's' For symbolic direct pointers without offset 856c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 857c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 858dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 859dd45c9cfSHarvey Harrison * usual colon-separated hex notation 8608a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 8618a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 8628a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 8638a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 8648a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 8658a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 8668a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 8678a27f7c9SJoe Perches * - 'I6c' for IPv6 addresses printed as specified by 8688a27f7c9SJoe Perches * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt 869332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 870332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 871332d2e78SLinus Torvalds * pointer to the real address. 8724d8a743cSLinus Torvalds */ 873fef20d9cSFrederic Weisbecker static char *pointer(const char *fmt, char *buf, char *end, void *ptr, 874fef20d9cSFrederic Weisbecker struct printf_spec spec) 87578a8bf69SLinus Torvalds { 876d97106abSLinus Torvalds if (!ptr) 877fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 878d97106abSLinus Torvalds 8790fe1ef24SLinus Torvalds switch (*fmt) { 8800fe1ef24SLinus Torvalds case 'F': 8810c8b946eSFrederic Weisbecker case 'f': 8820fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 88391adcd2cSSteven Rostedt case 's': 8840fe1ef24SLinus Torvalds /* Fallthrough */ 8850fe1ef24SLinus Torvalds case 'S': 8860c8b946eSFrederic Weisbecker return symbol_string(buf, end, ptr, spec, *fmt); 887332d2e78SLinus Torvalds case 'R': 888c7dabef8SBjorn Helgaas case 'r': 889fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 8908a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 8918a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 8928a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 8938a27f7c9SJoe Perches case 'I': /* Formatted IP supported 8948a27f7c9SJoe Perches * 4: 1.2.3.4 8958a27f7c9SJoe Perches * 6: 0001:0203:...:0708 8968a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 8978a27f7c9SJoe Perches */ 8988a27f7c9SJoe Perches case 'i': /* Contiguous: 8998a27f7c9SJoe Perches * 4: 001.002.003.004 9008a27f7c9SJoe Perches * 6: 000102...0f 9018a27f7c9SJoe Perches */ 9028a27f7c9SJoe Perches switch (fmt[1]) { 9038a27f7c9SJoe Perches case '6': 9048a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 9058a27f7c9SJoe Perches case '4': 9068a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 9078a27f7c9SJoe Perches } 9084aa99606SHarvey Harrison break; 9090fe1ef24SLinus Torvalds } 910fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 911fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 912fef20d9cSFrederic Weisbecker spec.field_width = 2*sizeof(void *); 913fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 91478a8bf69SLinus Torvalds } 915fef20d9cSFrederic Weisbecker spec.base = 16; 916fef20d9cSFrederic Weisbecker 917fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 918fef20d9cSFrederic Weisbecker } 919fef20d9cSFrederic Weisbecker 920fef20d9cSFrederic Weisbecker /* 921fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 922fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 923fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 924fef20d9cSFrederic Weisbecker * to go on the next call). 925fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 926fef20d9cSFrederic Weisbecker * 927fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 928fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 929fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 930fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 931fef20d9cSFrederic Weisbecker * 932fef20d9cSFrederic Weisbecker * @fmt: the format string 933fef20d9cSFrederic Weisbecker * @type of the token returned 934fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 935fef20d9cSFrederic Weisbecker * @field_width: overwritten width 936fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 937fef20d9cSFrederic Weisbecker * @precision: precision of a number 938fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 939fef20d9cSFrederic Weisbecker */ 940fef20d9cSFrederic Weisbecker static int format_decode(const char *fmt, struct printf_spec *spec) 941fef20d9cSFrederic Weisbecker { 942fef20d9cSFrederic Weisbecker const char *start = fmt; 943fef20d9cSFrederic Weisbecker 944fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 945ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 946fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 947fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 948fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 949fef20d9cSFrederic Weisbecker } 950fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 951fef20d9cSFrederic Weisbecker goto precision; 952fef20d9cSFrederic Weisbecker } 953fef20d9cSFrederic Weisbecker 954fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 955fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 956fef20d9cSFrederic Weisbecker if (spec->precision < 0) 957fef20d9cSFrederic Weisbecker spec->precision = 0; 958fef20d9cSFrederic Weisbecker 959fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 960fef20d9cSFrederic Weisbecker goto qualifier; 961fef20d9cSFrederic Weisbecker } 962fef20d9cSFrederic Weisbecker 963fef20d9cSFrederic Weisbecker /* By default */ 964fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 965fef20d9cSFrederic Weisbecker 966fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 967fef20d9cSFrederic Weisbecker if (*fmt == '%') 968fef20d9cSFrederic Weisbecker break; 969fef20d9cSFrederic Weisbecker } 970fef20d9cSFrederic Weisbecker 971fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 972fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 973fef20d9cSFrederic Weisbecker return fmt - start; 974fef20d9cSFrederic Weisbecker 975fef20d9cSFrederic Weisbecker /* Process flags */ 976fef20d9cSFrederic Weisbecker spec->flags = 0; 977fef20d9cSFrederic Weisbecker 978fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 979fef20d9cSFrederic Weisbecker bool found = true; 980fef20d9cSFrederic Weisbecker 981fef20d9cSFrederic Weisbecker ++fmt; 982fef20d9cSFrederic Weisbecker 983fef20d9cSFrederic Weisbecker switch (*fmt) { 984fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 985fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 986fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 987fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 988fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 989fef20d9cSFrederic Weisbecker default: found = false; 990fef20d9cSFrederic Weisbecker } 991fef20d9cSFrederic Weisbecker 992fef20d9cSFrederic Weisbecker if (!found) 993fef20d9cSFrederic Weisbecker break; 994fef20d9cSFrederic Weisbecker } 995fef20d9cSFrederic Weisbecker 996fef20d9cSFrederic Weisbecker /* get field width */ 997fef20d9cSFrederic Weisbecker spec->field_width = -1; 998fef20d9cSFrederic Weisbecker 999fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1000fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1001fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1002fef20d9cSFrederic Weisbecker /* it's the next argument */ 1003ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1004fef20d9cSFrederic Weisbecker return ++fmt - start; 1005fef20d9cSFrederic Weisbecker } 1006fef20d9cSFrederic Weisbecker 1007fef20d9cSFrederic Weisbecker precision: 1008fef20d9cSFrederic Weisbecker /* get the precision */ 1009fef20d9cSFrederic Weisbecker spec->precision = -1; 1010fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1011fef20d9cSFrederic Weisbecker ++fmt; 1012fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1013fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1014fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1015fef20d9cSFrederic Weisbecker spec->precision = 0; 1016fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1017fef20d9cSFrederic Weisbecker /* it's the next argument */ 1018adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1019fef20d9cSFrederic Weisbecker return ++fmt - start; 1020fef20d9cSFrederic Weisbecker } 1021fef20d9cSFrederic Weisbecker } 1022fef20d9cSFrederic Weisbecker 1023fef20d9cSFrederic Weisbecker qualifier: 1024fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1025fef20d9cSFrederic Weisbecker spec->qualifier = -1; 1026fef20d9cSFrederic Weisbecker if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || 1027fef20d9cSFrederic Weisbecker *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { 1028a4e94ef0SZhaolei spec->qualifier = *fmt++; 1029a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 1030a4e94ef0SZhaolei if (spec->qualifier == 'l') { 1031fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 1032fef20d9cSFrederic Weisbecker ++fmt; 1033a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 1034a4e94ef0SZhaolei spec->qualifier = 'H'; 1035a4e94ef0SZhaolei ++fmt; 1036a4e94ef0SZhaolei } 1037fef20d9cSFrederic Weisbecker } 1038fef20d9cSFrederic Weisbecker } 1039fef20d9cSFrederic Weisbecker 1040fef20d9cSFrederic Weisbecker /* default base */ 1041fef20d9cSFrederic Weisbecker spec->base = 10; 1042fef20d9cSFrederic Weisbecker switch (*fmt) { 1043fef20d9cSFrederic Weisbecker case 'c': 1044fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1045fef20d9cSFrederic Weisbecker return ++fmt - start; 1046fef20d9cSFrederic Weisbecker 1047fef20d9cSFrederic Weisbecker case 's': 1048fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1049fef20d9cSFrederic Weisbecker return ++fmt - start; 1050fef20d9cSFrederic Weisbecker 1051fef20d9cSFrederic Weisbecker case 'p': 1052fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1053fef20d9cSFrederic Weisbecker return fmt - start; 1054fef20d9cSFrederic Weisbecker /* skip alnum */ 1055fef20d9cSFrederic Weisbecker 1056fef20d9cSFrederic Weisbecker case 'n': 1057fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NRCHARS; 1058fef20d9cSFrederic Weisbecker return ++fmt - start; 1059fef20d9cSFrederic Weisbecker 1060fef20d9cSFrederic Weisbecker case '%': 1061fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1062fef20d9cSFrederic Weisbecker return ++fmt - start; 1063fef20d9cSFrederic Weisbecker 1064fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1065fef20d9cSFrederic Weisbecker case 'o': 1066fef20d9cSFrederic Weisbecker spec->base = 8; 1067fef20d9cSFrederic Weisbecker break; 1068fef20d9cSFrederic Weisbecker 1069fef20d9cSFrederic Weisbecker case 'x': 1070fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1071fef20d9cSFrederic Weisbecker 1072fef20d9cSFrederic Weisbecker case 'X': 1073fef20d9cSFrederic Weisbecker spec->base = 16; 1074fef20d9cSFrederic Weisbecker break; 1075fef20d9cSFrederic Weisbecker 1076fef20d9cSFrederic Weisbecker case 'd': 1077fef20d9cSFrederic Weisbecker case 'i': 107839e874f8SFrederic Weisbecker spec->flags |= SIGN; 1079fef20d9cSFrederic Weisbecker case 'u': 1080fef20d9cSFrederic Weisbecker break; 1081fef20d9cSFrederic Weisbecker 1082fef20d9cSFrederic Weisbecker default: 1083fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1084fef20d9cSFrederic Weisbecker return fmt - start; 1085fef20d9cSFrederic Weisbecker } 1086fef20d9cSFrederic Weisbecker 1087fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 1088fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1089fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 109039e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1091fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG; 1092fef20d9cSFrederic Weisbecker else 1093fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_ULONG; 1094fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') { 1095fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1096fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 1097fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1098a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 1099a4e94ef0SZhaolei if (spec->flags & SIGN) 1100a4e94ef0SZhaolei spec->type = FORMAT_TYPE_BYTE; 1101a4e94ef0SZhaolei else 1102a4e94ef0SZhaolei spec->type = FORMAT_TYPE_UBYTE; 1103fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 110439e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1105fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SHORT; 1106fef20d9cSFrederic Weisbecker else 1107fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_USHORT; 1108fef20d9cSFrederic Weisbecker } else { 110939e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1110fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INT; 1111fef20d9cSFrederic Weisbecker else 1112fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_UINT; 1113fef20d9cSFrederic Weisbecker } 1114fef20d9cSFrederic Weisbecker 1115fef20d9cSFrederic Weisbecker return ++fmt - start; 111678a8bf69SLinus Torvalds } 111778a8bf69SLinus Torvalds 11181da177e4SLinus Torvalds /** 11191da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 11201da177e4SLinus Torvalds * @buf: The buffer to place the result into 11211da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 11221da177e4SLinus Torvalds * @fmt: The format string to use 11231da177e4SLinus Torvalds * @args: Arguments for the format string 11241da177e4SLinus Torvalds * 112520036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 112691adcd2cSSteven Rostedt * %pS output the name of a text symbol with offset 112791adcd2cSSteven Rostedt * %ps output the name of a text symbol without offset 11280c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 11290c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 1130332d2e78SLinus Torvalds * %pR output the address range in a struct resource 11310efb4d20SSteven Rostedt * %n is ignored 113220036fdcSAndi Kleen * 11331da177e4SLinus Torvalds * The return value is the number of characters which would 11341da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 11351da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 11361da177e4SLinus Torvalds * number of characters written into @buf as return value 113772fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 11381da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 11391da177e4SLinus Torvalds * string is truncated. 11401da177e4SLinus Torvalds * 11411da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 114272fd4a35SRobert P. J. Day * You probably want snprintf() instead. 11431da177e4SLinus Torvalds */ 11441da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 11451da177e4SLinus Torvalds { 11461da177e4SLinus Torvalds unsigned long long num; 11471da177e4SLinus Torvalds char *str, *end, c; 1148fef20d9cSFrederic Weisbecker int read; 1149fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 11501da177e4SLinus Torvalds 1151f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1152f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 11532f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 11541da177e4SLinus Torvalds return 0; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds str = buf; 1157f796937aSJeremy Fitzhardinge end = buf + size; 11581da177e4SLinus Torvalds 1159f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1160f796937aSJeremy Fitzhardinge if (end < buf) { 11611da177e4SLinus Torvalds end = ((void *)-1); 1162f796937aSJeremy Fitzhardinge size = end - buf; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 1165fef20d9cSFrederic Weisbecker while (*fmt) { 1166fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1167fef20d9cSFrederic Weisbecker 1168fef20d9cSFrederic Weisbecker read = format_decode(fmt, &spec); 1169fef20d9cSFrederic Weisbecker 1170fef20d9cSFrederic Weisbecker fmt += read; 1171fef20d9cSFrederic Weisbecker 1172fef20d9cSFrederic Weisbecker switch (spec.type) { 1173fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1174fef20d9cSFrederic Weisbecker int copy = read; 1175fef20d9cSFrederic Weisbecker if (str < end) { 1176fef20d9cSFrederic Weisbecker if (copy > end - str) 1177fef20d9cSFrederic Weisbecker copy = end - str; 1178fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1179fef20d9cSFrederic Weisbecker } 1180fef20d9cSFrederic Weisbecker str += read; 1181fef20d9cSFrederic Weisbecker break; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 1184ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1185fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1186fef20d9cSFrederic Weisbecker break; 11871da177e4SLinus Torvalds 1188fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1189fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1190fef20d9cSFrederic Weisbecker break; 11911da177e4SLinus Torvalds 1192fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 1193fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1194fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1195f796937aSJeremy Fitzhardinge if (str < end) 11961da177e4SLinus Torvalds *str = ' '; 11971da177e4SLinus Torvalds ++str; 1198fef20d9cSFrederic Weisbecker 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1202f796937aSJeremy Fitzhardinge if (str < end) 12031da177e4SLinus Torvalds *str = c; 12041da177e4SLinus Torvalds ++str; 1205fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1206f796937aSJeremy Fitzhardinge if (str < end) 12071da177e4SLinus Torvalds *str = ' '; 12081da177e4SLinus Torvalds ++str; 12091da177e4SLinus Torvalds } 1210fef20d9cSFrederic Weisbecker break; 12111da177e4SLinus Torvalds 1212fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1213fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1214fef20d9cSFrederic Weisbecker break; 12151da177e4SLinus Torvalds 1216fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1217fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, va_arg(args, void *), 1218fef20d9cSFrederic Weisbecker spec); 1219fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 12204d8a743cSLinus Torvalds fmt++; 1221fef20d9cSFrederic Weisbecker break; 12221da177e4SLinus Torvalds 1223fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1224f796937aSJeremy Fitzhardinge if (str < end) 12251da177e4SLinus Torvalds *str = '%'; 12261da177e4SLinus Torvalds ++str; 12271da177e4SLinus Torvalds break; 12281da177e4SLinus Torvalds 1229fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1230f796937aSJeremy Fitzhardinge if (str < end) 12311da177e4SLinus Torvalds *str = '%'; 12321da177e4SLinus Torvalds ++str; 1233fef20d9cSFrederic Weisbecker break; 1234fef20d9cSFrederic Weisbecker 1235fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 1236fef20d9cSFrederic Weisbecker int qualifier = spec.qualifier; 1237fef20d9cSFrederic Weisbecker 1238fef20d9cSFrederic Weisbecker if (qualifier == 'l') { 1239fef20d9cSFrederic Weisbecker long *ip = va_arg(args, long *); 1240fef20d9cSFrederic Weisbecker *ip = (str - buf); 1241fef20d9cSFrederic Weisbecker } else if (qualifier == 'Z' || 1242fef20d9cSFrederic Weisbecker qualifier == 'z') { 1243fef20d9cSFrederic Weisbecker size_t *ip = va_arg(args, size_t *); 1244fef20d9cSFrederic Weisbecker *ip = (str - buf); 12451da177e4SLinus Torvalds } else { 1246fef20d9cSFrederic Weisbecker int *ip = va_arg(args, int *); 1247fef20d9cSFrederic Weisbecker *ip = (str - buf); 1248fef20d9cSFrederic Weisbecker } 1249fef20d9cSFrederic Weisbecker break; 1250fef20d9cSFrederic Weisbecker } 1251fef20d9cSFrederic Weisbecker 1252fef20d9cSFrederic Weisbecker default: 1253fef20d9cSFrederic Weisbecker switch (spec.type) { 1254fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1255fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1256fef20d9cSFrederic Weisbecker break; 1257fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1258fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1259fef20d9cSFrederic Weisbecker break; 1260fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1261fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1262fef20d9cSFrederic Weisbecker break; 1263fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1264fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1265fef20d9cSFrederic Weisbecker break; 1266fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1267fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1268fef20d9cSFrederic Weisbecker break; 1269a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1270a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1271a4e94ef0SZhaolei break; 1272a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1273a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1274a4e94ef0SZhaolei break; 1275fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1276fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1277fef20d9cSFrederic Weisbecker break; 1278fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1279fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1280fef20d9cSFrederic Weisbecker break; 128139e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 128239e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1283fef20d9cSFrederic Weisbecker break; 1284fef20d9cSFrederic Weisbecker default: 1285fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 12861da177e4SLinus Torvalds } 1287fef20d9cSFrederic Weisbecker 1288fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 12891da177e4SLinus Torvalds } 1290fef20d9cSFrederic Weisbecker } 1291fef20d9cSFrederic Weisbecker 1292f796937aSJeremy Fitzhardinge if (size > 0) { 1293f796937aSJeremy Fitzhardinge if (str < end) 12941da177e4SLinus Torvalds *str = '\0'; 1295f796937aSJeremy Fitzhardinge else 12960a6047eeSLinus Torvalds end[-1] = '\0'; 1297f796937aSJeremy Fitzhardinge } 1298fef20d9cSFrederic Weisbecker 1299f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 13001da177e4SLinus Torvalds return str-buf; 1301fef20d9cSFrederic Weisbecker 13021da177e4SLinus Torvalds } 13031da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /** 13061da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 13071da177e4SLinus Torvalds * @buf: The buffer to place the result into 13081da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 13091da177e4SLinus Torvalds * @fmt: The format string to use 13101da177e4SLinus Torvalds * @args: Arguments for the format string 13111da177e4SLinus Torvalds * 13121da177e4SLinus Torvalds * The return value is the number of characters which have been written into 13131da177e4SLinus Torvalds * the @buf not including the trailing '\0'. If @size is <= 0 the function 13141da177e4SLinus Torvalds * returns 0. 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 131772fd4a35SRobert P. J. Day * You probably want scnprintf() instead. 131820036fdcSAndi Kleen * 131920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 13201da177e4SLinus Torvalds */ 13211da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 13221da177e4SLinus Torvalds { 13231da177e4SLinus Torvalds int i; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds i=vsnprintf(buf,size,fmt,args); 13261da177e4SLinus Torvalds return (i >= size) ? (size - 1) : i; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /** 13311da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 13321da177e4SLinus Torvalds * @buf: The buffer to place the result into 13331da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 13341da177e4SLinus Torvalds * @fmt: The format string to use 13351da177e4SLinus Torvalds * @...: Arguments for the format string 13361da177e4SLinus Torvalds * 13371da177e4SLinus Torvalds * The return value is the number of characters which would be 13381da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 13391da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 13401da177e4SLinus Torvalds * @size, the resulting string is truncated. 134120036fdcSAndi Kleen * 134220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 13431da177e4SLinus Torvalds */ 13441da177e4SLinus Torvalds int snprintf(char * buf, size_t size, const char *fmt, ...) 13451da177e4SLinus Torvalds { 13461da177e4SLinus Torvalds va_list args; 13471da177e4SLinus Torvalds int i; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds va_start(args, fmt); 13501da177e4SLinus Torvalds i=vsnprintf(buf,size,fmt,args); 13511da177e4SLinus Torvalds va_end(args); 13521da177e4SLinus Torvalds return i; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds /** 13571da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 13581da177e4SLinus Torvalds * @buf: The buffer to place the result into 13591da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 13601da177e4SLinus Torvalds * @fmt: The format string to use 13611da177e4SLinus Torvalds * @...: Arguments for the format string 13621da177e4SLinus Torvalds * 13631da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 1364ea6f3281SMartin Peschke * the trailing '\0'. If @size is <= 0 the function returns 0. 13651da177e4SLinus Torvalds */ 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds int scnprintf(char * buf, size_t size, const char *fmt, ...) 13681da177e4SLinus Torvalds { 13691da177e4SLinus Torvalds va_list args; 13701da177e4SLinus Torvalds int i; 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds va_start(args, fmt); 13731da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 13741da177e4SLinus Torvalds va_end(args); 13751da177e4SLinus Torvalds return (i >= size) ? (size - 1) : i; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds /** 13801da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 13811da177e4SLinus Torvalds * @buf: The buffer to place the result into 13821da177e4SLinus Torvalds * @fmt: The format string to use 13831da177e4SLinus Torvalds * @args: Arguments for the format string 13841da177e4SLinus Torvalds * 13851da177e4SLinus Torvalds * The function returns the number of characters written 138672fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 13871da177e4SLinus Torvalds * buffer overflows. 13881da177e4SLinus Torvalds * 13891da177e4SLinus Torvalds * Call this function if you are already dealing with a va_list. 139072fd4a35SRobert P. J. Day * You probably want sprintf() instead. 139120036fdcSAndi Kleen * 139220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 13931da177e4SLinus Torvalds */ 13941da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 13951da177e4SLinus Torvalds { 13961da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds /** 14011da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 14021da177e4SLinus Torvalds * @buf: The buffer to place the result into 14031da177e4SLinus Torvalds * @fmt: The format string to use 14041da177e4SLinus Torvalds * @...: Arguments for the format string 14051da177e4SLinus Torvalds * 14061da177e4SLinus Torvalds * The function returns the number of characters written 140772fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 14081da177e4SLinus Torvalds * buffer overflows. 140920036fdcSAndi Kleen * 141020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 14111da177e4SLinus Torvalds */ 14121da177e4SLinus Torvalds int sprintf(char * buf, const char *fmt, ...) 14131da177e4SLinus Torvalds { 14141da177e4SLinus Torvalds va_list args; 14151da177e4SLinus Torvalds int i; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds va_start(args, fmt); 14181da177e4SLinus Torvalds i=vsnprintf(buf, INT_MAX, fmt, args); 14191da177e4SLinus Torvalds va_end(args); 14201da177e4SLinus Torvalds return i; 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 14231da177e4SLinus Torvalds 14244370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 14254370aa4aSLai Jiangshan /* 14264370aa4aSLai Jiangshan * bprintf service: 14274370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 14284370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 14294370aa4aSLai Jiangshan */ 14304370aa4aSLai Jiangshan 14314370aa4aSLai Jiangshan /** 14324370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 14334370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 14344370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 14354370aa4aSLai Jiangshan * @fmt: The format string to use 14364370aa4aSLai Jiangshan * @args: Arguments for the format string 14374370aa4aSLai Jiangshan * 14384370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 14394370aa4aSLai Jiangshan * is skiped. 14404370aa4aSLai Jiangshan * 14414370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 14424370aa4aSLai Jiangshan * the given input. 14434370aa4aSLai Jiangshan * 14444370aa4aSLai Jiangshan * NOTE: 14454370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 14464370aa4aSLai Jiangshan * valid for bstr_printf(). 14474370aa4aSLai Jiangshan */ 14484370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 14494370aa4aSLai Jiangshan { 1450fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 14514370aa4aSLai Jiangshan char *str, *end; 1452fef20d9cSFrederic Weisbecker int read; 14534370aa4aSLai Jiangshan 14544370aa4aSLai Jiangshan str = (char *)bin_buf; 14554370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 14564370aa4aSLai Jiangshan 14574370aa4aSLai Jiangshan #define save_arg(type) \ 14584370aa4aSLai Jiangshan do { \ 14594370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 14604370aa4aSLai Jiangshan unsigned long long value; \ 14614370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 14624370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 14634370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 14644370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 14654370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 14664370aa4aSLai Jiangshan } \ 14674370aa4aSLai Jiangshan } else { \ 14684370aa4aSLai Jiangshan unsigned long value; \ 14694370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 14704370aa4aSLai Jiangshan value = va_arg(args, int); \ 14714370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 14724370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 14734370aa4aSLai Jiangshan } \ 14744370aa4aSLai Jiangshan str += sizeof(type); \ 14754370aa4aSLai Jiangshan } while (0) 14764370aa4aSLai Jiangshan 14774370aa4aSLai Jiangshan 1478fef20d9cSFrederic Weisbecker while (*fmt) { 1479fef20d9cSFrederic Weisbecker read = format_decode(fmt, &spec); 14804370aa4aSLai Jiangshan 1481fef20d9cSFrederic Weisbecker fmt += read; 1482fef20d9cSFrederic Weisbecker 1483fef20d9cSFrederic Weisbecker switch (spec.type) { 1484fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 1485fef20d9cSFrederic Weisbecker break; 1486fef20d9cSFrederic Weisbecker 1487ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1488fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 14894370aa4aSLai Jiangshan save_arg(int); 1490fef20d9cSFrederic Weisbecker break; 14914370aa4aSLai Jiangshan 1492fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 14934370aa4aSLai Jiangshan save_arg(char); 1494fef20d9cSFrederic Weisbecker break; 1495fef20d9cSFrederic Weisbecker 1496fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 14974370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 14984370aa4aSLai Jiangshan size_t len; 14994370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 15004370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 1501*0f4f81dcSAndré Goddard Rosa save_str = "(null)"; 15024370aa4aSLai Jiangshan len = strlen(save_str); 15034370aa4aSLai Jiangshan if (str + len + 1 < end) 15044370aa4aSLai Jiangshan memcpy(str, save_str, len + 1); 15054370aa4aSLai Jiangshan str += len + 1; 1506fef20d9cSFrederic Weisbecker break; 15074370aa4aSLai Jiangshan } 1508fef20d9cSFrederic Weisbecker 1509fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 15104370aa4aSLai Jiangshan save_arg(void *); 15114370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 1512fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 15134370aa4aSLai Jiangshan fmt++; 1514fef20d9cSFrederic Weisbecker break; 1515fef20d9cSFrederic Weisbecker 1516fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1517fef20d9cSFrederic Weisbecker break; 1518fef20d9cSFrederic Weisbecker 1519fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1520fef20d9cSFrederic Weisbecker break; 1521fef20d9cSFrederic Weisbecker 1522fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 15234370aa4aSLai Jiangshan /* skip %n 's argument */ 1524fef20d9cSFrederic Weisbecker int qualifier = spec.qualifier; 15254370aa4aSLai Jiangshan void *skip_arg; 15264370aa4aSLai Jiangshan if (qualifier == 'l') 15274370aa4aSLai Jiangshan skip_arg = va_arg(args, long *); 15284370aa4aSLai Jiangshan else if (qualifier == 'Z' || qualifier == 'z') 15294370aa4aSLai Jiangshan skip_arg = va_arg(args, size_t *); 15304370aa4aSLai Jiangshan else 15314370aa4aSLai Jiangshan skip_arg = va_arg(args, int *); 1532fef20d9cSFrederic Weisbecker break; 15334370aa4aSLai Jiangshan } 15344370aa4aSLai Jiangshan 1535fef20d9cSFrederic Weisbecker default: 1536fef20d9cSFrederic Weisbecker switch (spec.type) { 1537fef20d9cSFrederic Weisbecker 1538fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1539fef20d9cSFrederic Weisbecker save_arg(long long); 1540fef20d9cSFrederic Weisbecker break; 1541fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1542fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1543fef20d9cSFrederic Weisbecker save_arg(unsigned long); 1544fef20d9cSFrederic Weisbecker break; 1545fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1546fef20d9cSFrederic Weisbecker save_arg(size_t); 1547fef20d9cSFrederic Weisbecker break; 1548fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1549fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 1550fef20d9cSFrederic Weisbecker break; 1551a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1552a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1553a4e94ef0SZhaolei save_arg(char); 1554a4e94ef0SZhaolei break; 1555fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1556fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1557fef20d9cSFrederic Weisbecker save_arg(short); 1558fef20d9cSFrederic Weisbecker break; 1559fef20d9cSFrederic Weisbecker default: 1560fef20d9cSFrederic Weisbecker save_arg(int); 1561fef20d9cSFrederic Weisbecker } 1562fef20d9cSFrederic Weisbecker } 1563fef20d9cSFrederic Weisbecker } 15644370aa4aSLai Jiangshan return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 1565fef20d9cSFrederic Weisbecker 1566fef20d9cSFrederic Weisbecker #undef save_arg 15674370aa4aSLai Jiangshan } 15684370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 15694370aa4aSLai Jiangshan 15704370aa4aSLai Jiangshan /** 15714370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 15724370aa4aSLai Jiangshan * @buf: The buffer to place the result into 15734370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 15744370aa4aSLai Jiangshan * @fmt: The format string to use 15754370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 15764370aa4aSLai Jiangshan * 15774370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 15784370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 15794370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 15804370aa4aSLai Jiangshan * 15814370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 15820efb4d20SSteven Rostedt * see vsnprintf comment for details. 15834370aa4aSLai Jiangshan * 15844370aa4aSLai Jiangshan * The return value is the number of characters which would 15854370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 15864370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 15874370aa4aSLai Jiangshan * number of characters written into @buf as return value 15884370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 15894370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 15904370aa4aSLai Jiangshan * string is truncated. 15914370aa4aSLai Jiangshan */ 15924370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 15934370aa4aSLai Jiangshan { 15944370aa4aSLai Jiangshan unsigned long long num; 15954370aa4aSLai Jiangshan char *str, *end, c; 15964370aa4aSLai Jiangshan const char *args = (const char *)bin_buf; 15974370aa4aSLai Jiangshan 1598fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 15994370aa4aSLai Jiangshan 16002f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 16014370aa4aSLai Jiangshan return 0; 16024370aa4aSLai Jiangshan 16034370aa4aSLai Jiangshan str = buf; 16044370aa4aSLai Jiangshan end = buf + size; 16054370aa4aSLai Jiangshan 16064370aa4aSLai Jiangshan #define get_arg(type) \ 16074370aa4aSLai Jiangshan ({ \ 16084370aa4aSLai Jiangshan typeof(type) value; \ 16094370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 16104370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 16114370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 16124370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 16134370aa4aSLai Jiangshan } else { \ 16144370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 16154370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 16164370aa4aSLai Jiangshan } \ 16174370aa4aSLai Jiangshan args += sizeof(type); \ 16184370aa4aSLai Jiangshan value; \ 16194370aa4aSLai Jiangshan }) 16204370aa4aSLai Jiangshan 16214370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 16224370aa4aSLai Jiangshan if (end < buf) { 16234370aa4aSLai Jiangshan end = ((void *)-1); 16244370aa4aSLai Jiangshan size = end - buf; 16254370aa4aSLai Jiangshan } 16264370aa4aSLai Jiangshan 1627fef20d9cSFrederic Weisbecker while (*fmt) { 1628fef20d9cSFrederic Weisbecker int read; 1629fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1630fef20d9cSFrederic Weisbecker 1631fef20d9cSFrederic Weisbecker read = format_decode(fmt, &spec); 1632fef20d9cSFrederic Weisbecker 1633fef20d9cSFrederic Weisbecker fmt += read; 1634fef20d9cSFrederic Weisbecker 1635fef20d9cSFrederic Weisbecker switch (spec.type) { 1636fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1637fef20d9cSFrederic Weisbecker int copy = read; 1638fef20d9cSFrederic Weisbecker if (str < end) { 1639fef20d9cSFrederic Weisbecker if (copy > end - str) 1640fef20d9cSFrederic Weisbecker copy = end - str; 1641fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1642fef20d9cSFrederic Weisbecker } 1643fef20d9cSFrederic Weisbecker str += read; 1644fef20d9cSFrederic Weisbecker break; 16454370aa4aSLai Jiangshan } 16464370aa4aSLai Jiangshan 1647ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1648fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 1649fef20d9cSFrederic Weisbecker break; 16504370aa4aSLai Jiangshan 1651fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1652fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 1653fef20d9cSFrederic Weisbecker break; 16544370aa4aSLai Jiangshan 1655fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 1656fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1657fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 16584370aa4aSLai Jiangshan if (str < end) 16594370aa4aSLai Jiangshan *str = ' '; 16604370aa4aSLai Jiangshan ++str; 16614370aa4aSLai Jiangshan } 16624370aa4aSLai Jiangshan } 16634370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 16644370aa4aSLai Jiangshan if (str < end) 16654370aa4aSLai Jiangshan *str = c; 16664370aa4aSLai Jiangshan ++str; 1667fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 16684370aa4aSLai Jiangshan if (str < end) 16694370aa4aSLai Jiangshan *str = ' '; 16704370aa4aSLai Jiangshan ++str; 16714370aa4aSLai Jiangshan } 1672fef20d9cSFrederic Weisbecker break; 16734370aa4aSLai Jiangshan 1674fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 16754370aa4aSLai Jiangshan const char *str_arg = args; 16764370aa4aSLai Jiangshan size_t len = strlen(str_arg); 16774370aa4aSLai Jiangshan args += len + 1; 1678fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 1679fef20d9cSFrederic Weisbecker break; 16804370aa4aSLai Jiangshan } 16814370aa4aSLai Jiangshan 1682fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1683fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, get_arg(void *), spec); 1684fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 16854370aa4aSLai Jiangshan fmt++; 1686fef20d9cSFrederic Weisbecker break; 16874370aa4aSLai Jiangshan 1688fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 16894370aa4aSLai Jiangshan if (str < end) 16904370aa4aSLai Jiangshan *str = '%'; 16914370aa4aSLai Jiangshan ++str; 16924370aa4aSLai Jiangshan break; 16934370aa4aSLai Jiangshan 1694fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 16954370aa4aSLai Jiangshan if (str < end) 16964370aa4aSLai Jiangshan *str = '%'; 16974370aa4aSLai Jiangshan ++str; 1698fef20d9cSFrederic Weisbecker break; 1699fef20d9cSFrederic Weisbecker 1700fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: 1701fef20d9cSFrederic Weisbecker /* skip */ 1702fef20d9cSFrederic Weisbecker break; 1703fef20d9cSFrederic Weisbecker 1704fef20d9cSFrederic Weisbecker default: 1705fef20d9cSFrederic Weisbecker switch (spec.type) { 1706fef20d9cSFrederic Weisbecker 1707fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 17084370aa4aSLai Jiangshan num = get_arg(long long); 1709fef20d9cSFrederic Weisbecker break; 1710fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 17114370aa4aSLai Jiangshan num = get_arg(unsigned long); 1712fef20d9cSFrederic Weisbecker break; 1713fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1714fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 1715fef20d9cSFrederic Weisbecker break; 1716fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 17174370aa4aSLai Jiangshan num = get_arg(size_t); 1718fef20d9cSFrederic Weisbecker break; 1719fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 17204370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 1721fef20d9cSFrederic Weisbecker break; 1722a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1723a4e94ef0SZhaolei num = get_arg(unsigned char); 1724a4e94ef0SZhaolei break; 1725a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1726a4e94ef0SZhaolei num = get_arg(signed char); 1727a4e94ef0SZhaolei break; 1728fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1729fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 1730fef20d9cSFrederic Weisbecker break; 1731fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1732fef20d9cSFrederic Weisbecker num = get_arg(short); 1733fef20d9cSFrederic Weisbecker break; 1734fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 17354370aa4aSLai Jiangshan num = get_arg(unsigned int); 1736fef20d9cSFrederic Weisbecker break; 1737fef20d9cSFrederic Weisbecker default: 1738fef20d9cSFrederic Weisbecker num = get_arg(int); 17394370aa4aSLai Jiangshan } 1740fef20d9cSFrederic Weisbecker 1741fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 17424370aa4aSLai Jiangshan } 1743fef20d9cSFrederic Weisbecker } 1744fef20d9cSFrederic Weisbecker 17454370aa4aSLai Jiangshan if (size > 0) { 17464370aa4aSLai Jiangshan if (str < end) 17474370aa4aSLai Jiangshan *str = '\0'; 17484370aa4aSLai Jiangshan else 17494370aa4aSLai Jiangshan end[-1] = '\0'; 17504370aa4aSLai Jiangshan } 1751fef20d9cSFrederic Weisbecker 17524370aa4aSLai Jiangshan #undef get_arg 17534370aa4aSLai Jiangshan 17544370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 17554370aa4aSLai Jiangshan return str - buf; 17564370aa4aSLai Jiangshan } 17574370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 17584370aa4aSLai Jiangshan 17594370aa4aSLai Jiangshan /** 17604370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 17614370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 17624370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 17634370aa4aSLai Jiangshan * @fmt: The format string to use 17644370aa4aSLai Jiangshan * @...: Arguments for the format string 17654370aa4aSLai Jiangshan * 17664370aa4aSLai Jiangshan * The function returns the number of words(u32) written 17674370aa4aSLai Jiangshan * into @bin_buf. 17684370aa4aSLai Jiangshan */ 17694370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 17704370aa4aSLai Jiangshan { 17714370aa4aSLai Jiangshan va_list args; 17724370aa4aSLai Jiangshan int ret; 17734370aa4aSLai Jiangshan 17744370aa4aSLai Jiangshan va_start(args, fmt); 17754370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 17764370aa4aSLai Jiangshan va_end(args); 17774370aa4aSLai Jiangshan return ret; 17784370aa4aSLai Jiangshan } 17794370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 17804370aa4aSLai Jiangshan 17814370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 17824370aa4aSLai Jiangshan 17831da177e4SLinus Torvalds /** 17841da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 17851da177e4SLinus Torvalds * @buf: input buffer 17861da177e4SLinus Torvalds * @fmt: format of buffer 17871da177e4SLinus Torvalds * @args: arguments 17881da177e4SLinus Torvalds */ 17891da177e4SLinus Torvalds int vsscanf(const char * buf, const char * fmt, va_list args) 17901da177e4SLinus Torvalds { 17911da177e4SLinus Torvalds const char *str = buf; 17921da177e4SLinus Torvalds char *next; 17931da177e4SLinus Torvalds char digit; 17941da177e4SLinus Torvalds int num = 0; 17951da177e4SLinus Torvalds int qualifier; 17961da177e4SLinus Torvalds int base; 17971da177e4SLinus Torvalds int field_width; 17981da177e4SLinus Torvalds int is_sign = 0; 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds while(*fmt && *str) { 18011da177e4SLinus Torvalds /* skip any white space in format */ 18021da177e4SLinus Torvalds /* white space in format matchs any amount of 18031da177e4SLinus Torvalds * white space, including none, in the input. 18041da177e4SLinus Torvalds */ 18051da177e4SLinus Torvalds if (isspace(*fmt)) { 18061da177e4SLinus Torvalds while (isspace(*fmt)) 18071da177e4SLinus Torvalds ++fmt; 18081da177e4SLinus Torvalds while (isspace(*str)) 18091da177e4SLinus Torvalds ++str; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 18131da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 18141da177e4SLinus Torvalds if (*fmt++ != *str++) 18151da177e4SLinus Torvalds break; 18161da177e4SLinus Torvalds continue; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds if (!*fmt) 18201da177e4SLinus Torvalds break; 18211da177e4SLinus Torvalds ++fmt; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds /* skip this conversion. 18241da177e4SLinus Torvalds * advance both strings to next white space 18251da177e4SLinus Torvalds */ 18261da177e4SLinus Torvalds if (*fmt == '*') { 18278fccae2cSAndy Spencer while (!isspace(*fmt) && *fmt != '%' && *fmt) 18281da177e4SLinus Torvalds fmt++; 18291da177e4SLinus Torvalds while (!isspace(*str) && *str) 18301da177e4SLinus Torvalds str++; 18311da177e4SLinus Torvalds continue; 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds /* get field width */ 18351da177e4SLinus Torvalds field_width = -1; 18361da177e4SLinus Torvalds if (isdigit(*fmt)) 18371da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds /* get conversion qualifier */ 18401da177e4SLinus Torvalds qualifier = -1; 18411da177e4SLinus Torvalds if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || 18421da177e4SLinus Torvalds *fmt == 'Z' || *fmt == 'z') { 18431da177e4SLinus Torvalds qualifier = *fmt++; 18441da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 18451da177e4SLinus Torvalds if (qualifier == 'h') { 18461da177e4SLinus Torvalds qualifier = 'H'; 18471da177e4SLinus Torvalds fmt++; 18481da177e4SLinus Torvalds } else if (qualifier == 'l') { 18491da177e4SLinus Torvalds qualifier = 'L'; 18501da177e4SLinus Torvalds fmt++; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds base = 10; 18551da177e4SLinus Torvalds is_sign = 0; 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds if (!*fmt || !*str) 18581da177e4SLinus Torvalds break; 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds switch(*fmt++) { 18611da177e4SLinus Torvalds case 'c': 18621da177e4SLinus Torvalds { 18631da177e4SLinus Torvalds char *s = (char *) va_arg(args,char*); 18641da177e4SLinus Torvalds if (field_width == -1) 18651da177e4SLinus Torvalds field_width = 1; 18661da177e4SLinus Torvalds do { 18671da177e4SLinus Torvalds *s++ = *str++; 18681da177e4SLinus Torvalds } while (--field_width > 0 && *str); 18691da177e4SLinus Torvalds num++; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds continue; 18721da177e4SLinus Torvalds case 's': 18731da177e4SLinus Torvalds { 18741da177e4SLinus Torvalds char *s = (char *) va_arg(args, char *); 18751da177e4SLinus Torvalds if(field_width == -1) 18761da177e4SLinus Torvalds field_width = INT_MAX; 18771da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 18781da177e4SLinus Torvalds while (isspace(*str)) 18791da177e4SLinus Torvalds str++; 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /* now copy until next white space */ 18821da177e4SLinus Torvalds while (*str && !isspace(*str) && field_width--) { 18831da177e4SLinus Torvalds *s++ = *str++; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds *s = '\0'; 18861da177e4SLinus Torvalds num++; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds continue; 18891da177e4SLinus Torvalds case 'n': 18901da177e4SLinus Torvalds /* return number of characters read so far */ 18911da177e4SLinus Torvalds { 18921da177e4SLinus Torvalds int *i = (int *)va_arg(args,int*); 18931da177e4SLinus Torvalds *i = str - buf; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds continue; 18961da177e4SLinus Torvalds case 'o': 18971da177e4SLinus Torvalds base = 8; 18981da177e4SLinus Torvalds break; 18991da177e4SLinus Torvalds case 'x': 19001da177e4SLinus Torvalds case 'X': 19011da177e4SLinus Torvalds base = 16; 19021da177e4SLinus Torvalds break; 19031da177e4SLinus Torvalds case 'i': 19041da177e4SLinus Torvalds base = 0; 19051da177e4SLinus Torvalds case 'd': 19061da177e4SLinus Torvalds is_sign = 1; 19071da177e4SLinus Torvalds case 'u': 19081da177e4SLinus Torvalds break; 19091da177e4SLinus Torvalds case '%': 19101da177e4SLinus Torvalds /* looking for '%' in str */ 19111da177e4SLinus Torvalds if (*str++ != '%') 19121da177e4SLinus Torvalds return num; 19131da177e4SLinus Torvalds continue; 19141da177e4SLinus Torvalds default: 19151da177e4SLinus Torvalds /* invalid format; stop here */ 19161da177e4SLinus Torvalds return num; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds /* have some sort of integer conversion. 19201da177e4SLinus Torvalds * first, skip white space in buffer. 19211da177e4SLinus Torvalds */ 19221da177e4SLinus Torvalds while (isspace(*str)) 19231da177e4SLinus Torvalds str++; 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds digit = *str; 19261da177e4SLinus Torvalds if (is_sign && digit == '-') 19271da177e4SLinus Torvalds digit = *(str + 1); 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds if (!digit 19301da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 19311da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 19321da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 19331da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 19341da177e4SLinus Torvalds break; 19351da177e4SLinus Torvalds 19361da177e4SLinus Torvalds switch(qualifier) { 19371da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 19381da177e4SLinus Torvalds if (is_sign) { 19391da177e4SLinus Torvalds signed char *s = (signed char *) va_arg(args,signed char *); 19401da177e4SLinus Torvalds *s = (signed char) simple_strtol(str,&next,base); 19411da177e4SLinus Torvalds } else { 19421da177e4SLinus Torvalds unsigned char *s = (unsigned char *) va_arg(args, unsigned char *); 19431da177e4SLinus Torvalds *s = (unsigned char) simple_strtoul(str, &next, base); 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds break; 19461da177e4SLinus Torvalds case 'h': 19471da177e4SLinus Torvalds if (is_sign) { 19481da177e4SLinus Torvalds short *s = (short *) va_arg(args,short *); 19491da177e4SLinus Torvalds *s = (short) simple_strtol(str,&next,base); 19501da177e4SLinus Torvalds } else { 19511da177e4SLinus Torvalds unsigned short *s = (unsigned short *) va_arg(args, unsigned short *); 19521da177e4SLinus Torvalds *s = (unsigned short) simple_strtoul(str, &next, base); 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds break; 19551da177e4SLinus Torvalds case 'l': 19561da177e4SLinus Torvalds if (is_sign) { 19571da177e4SLinus Torvalds long *l = (long *) va_arg(args,long *); 19581da177e4SLinus Torvalds *l = simple_strtol(str,&next,base); 19591da177e4SLinus Torvalds } else { 19601da177e4SLinus Torvalds unsigned long *l = (unsigned long*) va_arg(args,unsigned long*); 19611da177e4SLinus Torvalds *l = simple_strtoul(str,&next,base); 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds break; 19641da177e4SLinus Torvalds case 'L': 19651da177e4SLinus Torvalds if (is_sign) { 19661da177e4SLinus Torvalds long long *l = (long long*) va_arg(args,long long *); 19671da177e4SLinus Torvalds *l = simple_strtoll(str,&next,base); 19681da177e4SLinus Torvalds } else { 19691da177e4SLinus Torvalds unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*); 19701da177e4SLinus Torvalds *l = simple_strtoull(str,&next,base); 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds break; 19731da177e4SLinus Torvalds case 'Z': 19741da177e4SLinus Torvalds case 'z': 19751da177e4SLinus Torvalds { 19761da177e4SLinus Torvalds size_t *s = (size_t*) va_arg(args,size_t*); 19771da177e4SLinus Torvalds *s = (size_t) simple_strtoul(str,&next,base); 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds break; 19801da177e4SLinus Torvalds default: 19811da177e4SLinus Torvalds if (is_sign) { 19821da177e4SLinus Torvalds int *i = (int *) va_arg(args, int*); 19831da177e4SLinus Torvalds *i = (int) simple_strtol(str,&next,base); 19841da177e4SLinus Torvalds } else { 19851da177e4SLinus Torvalds unsigned int *i = (unsigned int*) va_arg(args, unsigned int*); 19861da177e4SLinus Torvalds *i = (unsigned int) simple_strtoul(str,&next,base); 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds break; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds num++; 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds if (!next) 19931da177e4SLinus Torvalds break; 19941da177e4SLinus Torvalds str = next; 19951da177e4SLinus Torvalds } 1996c6b40d16SJohannes Berg 1997c6b40d16SJohannes Berg /* 1998c6b40d16SJohannes Berg * Now we've come all the way through so either the input string or the 1999c6b40d16SJohannes Berg * format ended. In the former case, there can be a %n at the current 2000c6b40d16SJohannes Berg * position in the format that needs to be filled. 2001c6b40d16SJohannes Berg */ 2002c6b40d16SJohannes Berg if (*fmt == '%' && *(fmt + 1) == 'n') { 2003c6b40d16SJohannes Berg int *p = (int *)va_arg(args, int *); 2004c6b40d16SJohannes Berg *p = str - buf; 2005c6b40d16SJohannes Berg } 2006c6b40d16SJohannes Berg 20071da177e4SLinus Torvalds return num; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 20101da177e4SLinus Torvalds 20111da177e4SLinus Torvalds /** 20121da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 20131da177e4SLinus Torvalds * @buf: input buffer 20141da177e4SLinus Torvalds * @fmt: formatting of buffer 20151da177e4SLinus Torvalds * @...: resulting arguments 20161da177e4SLinus Torvalds */ 20171da177e4SLinus Torvalds int sscanf(const char * buf, const char * fmt, ...) 20181da177e4SLinus Torvalds { 20191da177e4SLinus Torvalds va_list args; 20201da177e4SLinus Torvalds int i; 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds va_start(args,fmt); 20231da177e4SLinus Torvalds i = vsscanf(buf,fmt,args); 20241da177e4SLinus Torvalds va_end(args); 20251da177e4SLinus Torvalds return i; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2028