Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/lib/vsprintf.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ |
| 8 | /* |
| 9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
| 10 | */ |
| 11 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 12 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
| 14 | * - changed to provide snprintf and vsnprintf functions |
| 15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> |
| 16 | * - scnprintf and vscnprintf |
| 17 | */ |
| 18 | |
| 19 | #include <stdarg.h> |
Stephen Boyd | 0d1d7a5 | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 20 | #include <linux/clk.h> |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 21 | #include <linux/clk-provider.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 22 | #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/types.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/ctype.h> |
| 26 | #include <linux/kernel.h> |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 27 | #include <linux/kallsyms.h> |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 28 | #include <linux/math64.h> |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 29 | #include <linux/uaccess.h> |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 30 | #include <linux/ioport.h> |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 31 | #include <linux/dcache.h> |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 32 | #include <linux/cred.h> |
Andy Shevchenko | 2b1b0d6 | 2016-05-20 17:01:04 -0700 | [diff] [blame] | 33 | #include <linux/uuid.h> |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 34 | #include <linux/of.h> |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 35 | #include <net/addrconf.h> |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 36 | #ifdef CONFIG_BLOCK |
| 37 | #include <linux/blkdev.h> |
| 38 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 40 | #include "../mm/internal.h" /* For the trace_print_flags arrays */ |
| 41 | |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 42 | #include <asm/page.h> /* for PAGE_SIZE */ |
James Bottomley | deac93d | 2008-09-03 20:43:36 -0500 | [diff] [blame] | 43 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 44 | #include <asm/byteorder.h> /* cpu_to_le16 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 46 | #include <linux/string_helpers.h> |
Alexey Dobriyan | 1dff46d | 2011-10-31 17:12:28 -0700 | [diff] [blame] | 47 | #include "kstrtox.h" |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * simple_strtoull - convert a string to an unsigned long long |
| 51 | * @cp: The start of the string |
| 52 | * @endp: A pointer to the end of the parsed string will be placed here |
| 53 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 54 | * |
| 55 | * This function is obsolete. Please use kstrtoull instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 57 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Alexey Dobriyan | 1dff46d | 2011-10-31 17:12:28 -0700 | [diff] [blame] | 59 | unsigned long long result; |
| 60 | unsigned int rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Alexey Dobriyan | 1dff46d | 2011-10-31 17:12:28 -0700 | [diff] [blame] | 62 | cp = _parse_integer_fixup_radix(cp, &base); |
| 63 | rv = _parse_integer(cp, base, &result); |
| 64 | /* FIXME */ |
| 65 | cp += (rv & ~KSTRTOX_OVERFLOW); |
Harvey Harrison | aa46a63 | 2008-10-16 13:40:34 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (endp) |
| 68 | *endp = (char *)cp; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | return result; |
| 71 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | EXPORT_SYMBOL(simple_strtoull); |
| 73 | |
| 74 | /** |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 75 | * simple_strtoul - convert a string to an unsigned long |
| 76 | * @cp: The start of the string |
| 77 | * @endp: A pointer to the end of the parsed string will be placed here |
| 78 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 79 | * |
| 80 | * This function is obsolete. Please use kstrtoul instead. |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 81 | */ |
| 82 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
| 83 | { |
| 84 | return simple_strtoull(cp, endp, base); |
| 85 | } |
| 86 | EXPORT_SYMBOL(simple_strtoul); |
| 87 | |
| 88 | /** |
| 89 | * simple_strtol - convert a string to a signed long |
| 90 | * @cp: The start of the string |
| 91 | * @endp: A pointer to the end of the parsed string will be placed here |
| 92 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 93 | * |
| 94 | * This function is obsolete. Please use kstrtol instead. |
André Goddard Rosa | 922ac25 | 2009-12-14 18:01:01 -0800 | [diff] [blame] | 95 | */ |
| 96 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
| 97 | { |
| 98 | if (*cp == '-') |
| 99 | return -simple_strtoul(cp + 1, endp, base); |
| 100 | |
| 101 | return simple_strtoul(cp, endp, base); |
| 102 | } |
| 103 | EXPORT_SYMBOL(simple_strtol); |
| 104 | |
| 105 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * simple_strtoll - convert a string to a signed long long |
| 107 | * @cp: The start of the string |
| 108 | * @endp: A pointer to the end of the parsed string will be placed here |
| 109 | * @base: The number base to use |
Eldad Zack | 462e471 | 2012-12-17 16:03:05 -0800 | [diff] [blame] | 110 | * |
| 111 | * This function is obsolete. Please use kstrtoll instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | */ |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 113 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 115 | if (*cp == '-') |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 116 | return -simple_strtoull(cp + 1, endp, base); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 117 | |
Harvey Harrison | 22d2705 | 2008-10-16 13:40:35 -0700 | [diff] [blame] | 118 | return simple_strtoull(cp, endp, base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
Hans Verkuil | 98d5ce0 | 2010-04-23 13:18:04 -0400 | [diff] [blame] | 120 | EXPORT_SYMBOL(simple_strtoll); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 122 | static noinline_for_stack |
| 123 | int skip_atoi(const char **s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 125 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Rasmus Villemoes | 43e5b66 | 2015-02-12 15:01:42 -0800 | [diff] [blame] | 127 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | i = i*10 + *((*s)++) - '0'; |
Rasmus Villemoes | 43e5b66 | 2015-02-12 15:01:42 -0800 | [diff] [blame] | 129 | } while (isdigit(**s)); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | return i; |
| 132 | } |
| 133 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 134 | /* |
| 135 | * Decimal conversion is by far the most typical, and is used for |
| 136 | * /proc and /sys data. This directly impacts e.g. top performance |
| 137 | * with many processes running. We optimize it for speed by emitting |
| 138 | * two characters at a time, using a 200 byte lookup table. This |
| 139 | * roughly halves the number of multiplications compared to computing |
| 140 | * the digits one at a time. Implementation strongly inspired by the |
| 141 | * previous version, which in turn used ideas described at |
| 142 | * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission |
| 143 | * from the author, Douglas W. Jones). |
| 144 | * |
| 145 | * It turns out there is precisely one 26 bit fixed-point |
| 146 | * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 |
| 147 | * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual |
| 148 | * range happens to be somewhat larger (x <= 1073741898), but that's |
| 149 | * irrelevant for our purpose. |
| 150 | * |
| 151 | * For dividing a number in the range [10^4, 10^6-1] by 100, we still |
| 152 | * need a 32x32->64 bit multiply, so we simply use the same constant. |
| 153 | * |
| 154 | * For dividing a number in the range [100, 10^4-1] by 100, there are |
| 155 | * several options. The simplest is (x * 0x147b) >> 19, which is valid |
| 156 | * for all x <= 43698. |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 157 | */ |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 158 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 159 | static const u16 decpair[100] = { |
| 160 | #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) |
| 161 | _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), |
| 162 | _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), |
| 163 | _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), |
| 164 | _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), |
| 165 | _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), |
| 166 | _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), |
| 167 | _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), |
| 168 | _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), |
| 169 | _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), |
| 170 | _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), |
| 171 | #undef _ |
| 172 | }; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 173 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 174 | /* |
| 175 | * This will print a single '0' even if r == 0, since we would |
Rasmus Villemoes | 675cf53 | 2015-04-16 12:43:42 -0700 | [diff] [blame] | 176 | * immediately jump to out_r where two 0s would be written but only |
| 177 | * one of them accounted for in buf. This is needed by ip4_string |
| 178 | * below. All other callers pass a non-zero value of r. |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 179 | */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 180 | static noinline_for_stack |
| 181 | char *put_dec_trunc8(char *buf, unsigned r) |
| 182 | { |
| 183 | unsigned q; |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 184 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 185 | /* 1 <= r < 10^8 */ |
| 186 | if (r < 100) |
| 187 | goto out_r; |
George Spelvin | cb239d0 | 2012-10-04 17:12:30 -0700 | [diff] [blame] | 188 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 189 | /* 100 <= r < 10^8 */ |
| 190 | q = (r * (u64)0x28f5c29) >> 32; |
| 191 | *((u16 *)buf) = decpair[r - 100*q]; |
| 192 | buf += 2; |
| 193 | |
| 194 | /* 1 <= q < 10^6 */ |
| 195 | if (q < 100) |
| 196 | goto out_q; |
| 197 | |
| 198 | /* 100 <= q < 10^6 */ |
| 199 | r = (q * (u64)0x28f5c29) >> 32; |
| 200 | *((u16 *)buf) = decpair[q - 100*r]; |
| 201 | buf += 2; |
| 202 | |
| 203 | /* 1 <= r < 10^4 */ |
| 204 | if (r < 100) |
| 205 | goto out_r; |
| 206 | |
| 207 | /* 100 <= r < 10^4 */ |
| 208 | q = (r * 0x147b) >> 19; |
| 209 | *((u16 *)buf) = decpair[r - 100*q]; |
| 210 | buf += 2; |
| 211 | out_q: |
| 212 | /* 1 <= q < 100 */ |
| 213 | r = q; |
| 214 | out_r: |
| 215 | /* 1 <= r < 100 */ |
| 216 | *((u16 *)buf) = decpair[r]; |
Rasmus Villemoes | 675cf53 | 2015-04-16 12:43:42 -0700 | [diff] [blame] | 217 | buf += r < 10 ? 1 : 2; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 218 | return buf; |
| 219 | } |
| 220 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 221 | #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 |
| 222 | static noinline_for_stack |
| 223 | char *put_dec_full8(char *buf, unsigned r) |
| 224 | { |
| 225 | unsigned q; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 226 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 227 | /* 0 <= r < 10^8 */ |
| 228 | q = (r * (u64)0x28f5c29) >> 32; |
| 229 | *((u16 *)buf) = decpair[r - 100*q]; |
| 230 | buf += 2; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 231 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 232 | /* 0 <= q < 10^6 */ |
| 233 | r = (q * (u64)0x28f5c29) >> 32; |
| 234 | *((u16 *)buf) = decpair[q - 100*r]; |
| 235 | buf += 2; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 236 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 237 | /* 0 <= r < 10^4 */ |
| 238 | q = (r * 0x147b) >> 19; |
| 239 | *((u16 *)buf) = decpair[r - 100*q]; |
| 240 | buf += 2; |
| 241 | |
| 242 | /* 0 <= q < 100 */ |
| 243 | *((u16 *)buf) = decpair[q]; |
| 244 | buf += 2; |
| 245 | return buf; |
| 246 | } |
| 247 | |
| 248 | static noinline_for_stack |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 249 | char *put_dec(char *buf, unsigned long long n) |
| 250 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 251 | if (n >= 100*1000*1000) |
| 252 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); |
| 253 | /* 1 <= n <= 1.6e11 */ |
| 254 | if (n >= 100*1000*1000) |
| 255 | buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); |
| 256 | /* 1 <= n < 1e8 */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 257 | return put_dec_trunc8(buf, n); |
| 258 | } |
| 259 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 260 | #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 261 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 262 | static void |
| 263 | put_dec_full4(char *buf, unsigned r) |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 264 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 265 | unsigned q; |
| 266 | |
| 267 | /* 0 <= r < 10^4 */ |
| 268 | q = (r * 0x147b) >> 19; |
| 269 | *((u16 *)buf) = decpair[r - 100*q]; |
| 270 | buf += 2; |
| 271 | /* 0 <= q < 100 */ |
| 272 | *((u16 *)buf) = decpair[q]; |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Call put_dec_full4 on x % 10000, return x / 10000. |
| 277 | * The approximation x/10000 == (x * 0x346DC5D7) >> 43 |
| 278 | * holds for all x < 1,128,869,999. The largest value this |
| 279 | * helper will ever be asked to convert is 1,125,520,955. |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 280 | * (second call in the put_dec code, assuming n is all-ones). |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 281 | */ |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 282 | static noinline_for_stack |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 283 | unsigned put_dec_helper4(char *buf, unsigned x) |
| 284 | { |
| 285 | uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; |
| 286 | |
| 287 | put_dec_full4(buf, x - q * 10000); |
| 288 | return q; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* Based on code by Douglas W. Jones found at |
| 292 | * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> |
| 293 | * (with permission from the author). |
| 294 | * Performs no 64-bit division and hence should be fast on 32-bit machines. |
| 295 | */ |
| 296 | static |
| 297 | char *put_dec(char *buf, unsigned long long n) |
| 298 | { |
| 299 | uint32_t d3, d2, d1, q, h; |
| 300 | |
| 301 | if (n < 100*1000*1000) |
| 302 | return put_dec_trunc8(buf, n); |
| 303 | |
| 304 | d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ |
| 305 | h = (n >> 32); |
| 306 | d2 = (h ) & 0xffff; |
| 307 | d3 = (h >> 16); /* implicit "& 0xffff" */ |
| 308 | |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 309 | /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 |
| 310 | = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 311 | q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 312 | q = put_dec_helper4(buf, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 313 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 314 | q += 7671 * d3 + 9496 * d2 + 6 * d1; |
| 315 | q = put_dec_helper4(buf+4, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 316 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 317 | q += 4749 * d3 + 42 * d2; |
| 318 | q = put_dec_helper4(buf+8, q); |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 319 | |
George Spelvin | 2359172 | 2012-10-04 17:12:29 -0700 | [diff] [blame] | 320 | q += 281 * d3; |
| 321 | buf += 12; |
| 322 | if (q) |
| 323 | buf = put_dec_trunc8(buf, q); |
| 324 | else while (buf[-1] == '0') |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 325 | --buf; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 326 | |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 327 | return buf; |
| 328 | } |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 329 | |
| 330 | #endif |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 331 | |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 332 | /* |
| 333 | * Convert passed number to decimal string. |
| 334 | * Returns the length of string. On buffer overflow, returns 0. |
| 335 | * |
| 336 | * If speed is not important, use snprintf(). It's easy to read the code. |
| 337 | */ |
| 338 | int num_to_str(char *buf, int size, unsigned long long num) |
| 339 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 340 | /* put_dec requires 2-byte alignment of the buffer. */ |
| 341 | char tmp[sizeof(num) * 3] __aligned(2); |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 342 | int idx, len; |
| 343 | |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 344 | /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ |
| 345 | if (num <= 9) { |
| 346 | tmp[0] = '0' + num; |
| 347 | len = 1; |
| 348 | } else { |
| 349 | len = put_dec(tmp, num) - tmp; |
| 350 | } |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 351 | |
| 352 | if (len > size) |
| 353 | return 0; |
| 354 | for (idx = 0; idx < len; ++idx) |
| 355 | buf[idx] = tmp[len - idx - 1]; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 356 | return len; |
KAMEZAWA Hiroyuki | 1ac101a | 2012-03-23 15:02:54 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 359 | #define SIGN 1 /* unsigned/signed, must be 1 */ |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 360 | #define LEFT 2 /* left justified */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | #define PLUS 4 /* show plus */ |
| 362 | #define SPACE 8 /* space if plus */ |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 363 | #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ |
Bjorn Helgaas | b89dc5d | 2010-03-05 10:47:31 -0700 | [diff] [blame] | 364 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
| 365 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 367 | enum format_type { |
| 368 | FORMAT_TYPE_NONE, /* Just a string part */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 369 | FORMAT_TYPE_WIDTH, |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 370 | FORMAT_TYPE_PRECISION, |
| 371 | FORMAT_TYPE_CHAR, |
| 372 | FORMAT_TYPE_STR, |
| 373 | FORMAT_TYPE_PTR, |
| 374 | FORMAT_TYPE_PERCENT_CHAR, |
| 375 | FORMAT_TYPE_INVALID, |
| 376 | FORMAT_TYPE_LONG_LONG, |
| 377 | FORMAT_TYPE_ULONG, |
| 378 | FORMAT_TYPE_LONG, |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 379 | FORMAT_TYPE_UBYTE, |
| 380 | FORMAT_TYPE_BYTE, |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 381 | FORMAT_TYPE_USHORT, |
| 382 | FORMAT_TYPE_SHORT, |
| 383 | FORMAT_TYPE_UINT, |
| 384 | FORMAT_TYPE_INT, |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 385 | FORMAT_TYPE_SIZE_T, |
| 386 | FORMAT_TYPE_PTRDIFF |
| 387 | }; |
| 388 | |
| 389 | struct printf_spec { |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 390 | unsigned int type:8; /* format_type enum */ |
| 391 | signed int field_width:24; /* width of output field */ |
| 392 | unsigned int flags:8; /* flags to number() */ |
| 393 | unsigned int base:8; /* number base, 8, 10 or 16 only */ |
| 394 | signed int precision:16; /* # of digits/chars */ |
| 395 | } __packed; |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 396 | #define FIELD_WIDTH_MAX ((1 << 23) - 1) |
| 397 | #define PRECISION_MAX ((1 << 15) - 1) |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 398 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 399 | static noinline_for_stack |
| 400 | char *number(char *buf, char *end, unsigned long long num, |
| 401 | struct printf_spec spec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 403 | /* put_dec requires 2-byte alignment of the buffer. */ |
| 404 | char tmp[3 * sizeof(num)] __aligned(2); |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 405 | char sign; |
| 406 | char locase; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 407 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | int i; |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 409 | bool is_zero = num == 0LL; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 410 | int field_width = spec.field_width; |
| 411 | int precision = spec.precision; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 413 | BUILD_BUG_ON(sizeof(struct printf_spec) != 8); |
| 414 | |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 415 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
| 416 | * produces same digits or (maybe lowercased) letters */ |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 417 | locase = (spec.flags & SMALL); |
| 418 | if (spec.flags & LEFT) |
| 419 | spec.flags &= ~ZEROPAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | sign = 0; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 421 | if (spec.flags & SIGN) { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 422 | if ((signed long long)num < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | sign = '-'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 424 | num = -(signed long long)num; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 425 | field_width--; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 426 | } else if (spec.flags & PLUS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | sign = '+'; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 428 | field_width--; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 429 | } else if (spec.flags & SPACE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | sign = ' '; |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 431 | field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 434 | if (need_pfx) { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 435 | if (spec.base == 16) |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 436 | field_width -= 2; |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 437 | else if (!is_zero) |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 438 | field_width--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 440 | |
| 441 | /* generate full string in tmp[], in reverse order */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | i = 0; |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 443 | if (num < spec.base) |
Rasmus Villemoes | 3ea8d44 | 2015-04-15 16:17:08 -0700 | [diff] [blame] | 444 | tmp[i++] = hex_asc_upper[num] | locase; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 445 | else if (spec.base != 10) { /* 8 or 16 */ |
| 446 | int mask = spec.base - 1; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 447 | int shift = 3; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 448 | |
| 449 | if (spec.base == 16) |
| 450 | shift = 4; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 451 | do { |
Rasmus Villemoes | 3ea8d44 | 2015-04-15 16:17:08 -0700 | [diff] [blame] | 452 | tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 453 | num >>= shift; |
| 454 | } while (num); |
Denis Vlasenko | 4277eed | 2007-07-15 23:41:56 -0700 | [diff] [blame] | 455 | } else { /* base 10 */ |
| 456 | i = put_dec(tmp, num) - tmp; |
| 457 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 458 | |
| 459 | /* printing 100 using %2d gives "100", not "00" */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 460 | if (i > precision) |
| 461 | precision = i; |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 462 | /* leading space padding */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 463 | field_width -= precision; |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 464 | if (!(spec.flags & (ZEROPAD | LEFT))) { |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 465 | while (--field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 466 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | *buf = ' '; |
| 468 | ++buf; |
| 469 | } |
| 470 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 471 | /* sign */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (sign) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 473 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | *buf = sign; |
| 475 | ++buf; |
| 476 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 477 | /* "0x" / "0" prefix */ |
| 478 | if (need_pfx) { |
Pierre Carrier | 7c20342 | 2012-05-29 15:07:35 -0700 | [diff] [blame] | 479 | if (spec.base == 16 || !is_zero) { |
| 480 | if (buf < end) |
| 481 | *buf = '0'; |
| 482 | ++buf; |
| 483 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 484 | if (spec.base == 16) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 485 | if (buf < end) |
Denys Vlasenko | 9b706ae | 2008-02-09 23:24:09 +0100 | [diff] [blame] | 486 | *buf = ('X' | locase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | ++buf; |
| 488 | } |
| 489 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 490 | /* zero or space padding */ |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 491 | if (!(spec.flags & LEFT)) { |
Rasmus Villemoes | d1c1b12 | 2015-04-15 16:17:11 -0700 | [diff] [blame] | 492 | char c = ' ' + (spec.flags & ZEROPAD); |
| 493 | BUILD_BUG_ON(' ' + ZEROPAD != '0'); |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 494 | while (--field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 495 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | *buf = c; |
| 497 | ++buf; |
| 498 | } |
| 499 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 500 | /* hmm even more zero padding? */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 501 | while (i <= --precision) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 502 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | *buf = '0'; |
| 504 | ++buf; |
| 505 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 506 | /* actual digits of result */ |
| 507 | while (--i >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 508 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | *buf = tmp[i]; |
| 510 | ++buf; |
| 511 | } |
Denis Vlasenko | b39a734 | 2007-07-15 23:41:54 -0700 | [diff] [blame] | 512 | /* trailing space padding */ |
Rasmus Villemoes | 1c7a8e6 | 2016-01-15 16:58:41 -0800 | [diff] [blame] | 513 | while (--field_width >= 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 514 | if (buf < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | *buf = ' '; |
| 516 | ++buf; |
| 517 | } |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return buf; |
| 520 | } |
| 521 | |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 522 | static noinline_for_stack |
| 523 | char *special_hex_number(char *buf, char *end, unsigned long long num, int size) |
| 524 | { |
| 525 | struct printf_spec spec; |
| 526 | |
| 527 | spec.type = FORMAT_TYPE_PTR; |
| 528 | spec.field_width = 2 + 2 * size; /* 0x + hex */ |
| 529 | spec.flags = SPECIAL | SMALL | ZEROPAD; |
| 530 | spec.base = 16; |
| 531 | spec.precision = -1; |
| 532 | |
| 533 | return number(buf, end, num, spec); |
| 534 | } |
| 535 | |
Rasmus Villemoes | cfccde0 | 2016-01-15 16:58:28 -0800 | [diff] [blame] | 536 | static void move_right(char *buf, char *end, unsigned len, unsigned spaces) |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 537 | { |
| 538 | size_t size; |
| 539 | if (buf >= end) /* nowhere to put anything */ |
| 540 | return; |
| 541 | size = end - buf; |
| 542 | if (size <= spaces) { |
| 543 | memset(buf, ' ', size); |
| 544 | return; |
| 545 | } |
| 546 | if (len) { |
| 547 | if (len > size - spaces) |
| 548 | len = size - spaces; |
| 549 | memmove(buf + spaces, buf, len); |
| 550 | } |
| 551 | memset(buf, ' ', spaces); |
| 552 | } |
| 553 | |
Rasmus Villemoes | cfccde0 | 2016-01-15 16:58:28 -0800 | [diff] [blame] | 554 | /* |
| 555 | * Handle field width padding for a string. |
| 556 | * @buf: current buffer position |
| 557 | * @n: length of string |
| 558 | * @end: end of output buffer |
| 559 | * @spec: for field width and flags |
| 560 | * Returns: new buffer position after padding. |
| 561 | */ |
| 562 | static noinline_for_stack |
| 563 | char *widen_string(char *buf, int n, char *end, struct printf_spec spec) |
| 564 | { |
| 565 | unsigned spaces; |
| 566 | |
| 567 | if (likely(n >= spec.field_width)) |
| 568 | return buf; |
| 569 | /* we want to pad the sucker */ |
| 570 | spaces = spec.field_width - n; |
| 571 | if (!(spec.flags & LEFT)) { |
| 572 | move_right(buf - n, end, n, spaces); |
| 573 | return buf + spaces; |
| 574 | } |
| 575 | while (spaces--) { |
| 576 | if (buf < end) |
| 577 | *buf = ' '; |
| 578 | ++buf; |
| 579 | } |
| 580 | return buf; |
| 581 | } |
| 582 | |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 583 | static noinline_for_stack |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 584 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
| 585 | { |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 586 | int len = 0; |
| 587 | size_t lim = spec.precision; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 588 | |
| 589 | if ((unsigned long)s < PAGE_SIZE) |
| 590 | s = "(null)"; |
| 591 | |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 592 | while (lim--) { |
| 593 | char c = *s++; |
| 594 | if (!c) |
| 595 | break; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 596 | if (buf < end) |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 597 | *buf = c; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 598 | ++buf; |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 599 | ++len; |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 600 | } |
Rasmus Villemoes | 34fc8b9 | 2016-01-15 16:58:34 -0800 | [diff] [blame] | 601 | return widen_string(buf, len, end, spec); |
Rasmus Villemoes | 95508cf | 2016-01-15 16:58:31 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static noinline_for_stack |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 605 | char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, |
| 606 | const char *fmt) |
| 607 | { |
| 608 | const char *array[4], *s; |
| 609 | const struct dentry *p; |
| 610 | int depth; |
| 611 | int i, n; |
| 612 | |
| 613 | switch (fmt[1]) { |
| 614 | case '2': case '3': case '4': |
| 615 | depth = fmt[1] - '0'; |
| 616 | break; |
| 617 | default: |
| 618 | depth = 1; |
| 619 | } |
| 620 | |
| 621 | rcu_read_lock(); |
| 622 | for (i = 0; i < depth; i++, d = p) { |
| 623 | p = ACCESS_ONCE(d->d_parent); |
| 624 | array[i] = ACCESS_ONCE(d->d_name.name); |
| 625 | if (p == d) { |
| 626 | if (i) |
| 627 | array[i] = ""; |
| 628 | i++; |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | s = array[--i]; |
| 633 | for (n = 0; n != spec.precision; n++, buf++) { |
| 634 | char c = *s++; |
| 635 | if (!c) { |
| 636 | if (!i) |
| 637 | break; |
| 638 | c = '/'; |
| 639 | s = array[--i]; |
| 640 | } |
| 641 | if (buf < end) |
| 642 | *buf = c; |
| 643 | } |
| 644 | rcu_read_unlock(); |
Rasmus Villemoes | cfccde0 | 2016-01-15 16:58:28 -0800 | [diff] [blame] | 645 | return widen_string(buf, n, end, spec); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 648 | #ifdef CONFIG_BLOCK |
| 649 | static noinline_for_stack |
| 650 | char *bdev_name(char *buf, char *end, struct block_device *bdev, |
| 651 | struct printf_spec spec, const char *fmt) |
| 652 | { |
| 653 | struct gendisk *hd = bdev->bd_disk; |
| 654 | |
| 655 | buf = string(buf, end, hd->disk_name, spec); |
| 656 | if (bdev->bd_part->partno) { |
| 657 | if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { |
| 658 | if (buf < end) |
| 659 | *buf = 'p'; |
| 660 | buf++; |
| 661 | } |
| 662 | buf = number(buf, end, bdev->bd_part->partno, spec); |
| 663 | } |
| 664 | return buf; |
| 665 | } |
| 666 | #endif |
| 667 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 668 | static noinline_for_stack |
| 669 | char *symbol_string(char *buf, char *end, void *ptr, |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 670 | struct printf_spec spec, const char *fmt) |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 671 | { |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 672 | unsigned long value; |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 673 | #ifdef CONFIG_KALLSYMS |
| 674 | char sym[KSYM_SYMBOL_LEN]; |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 675 | #endif |
| 676 | |
| 677 | if (fmt[1] == 'R') |
| 678 | ptr = __builtin_extract_return_addr(ptr); |
| 679 | value = (unsigned long)ptr; |
| 680 | |
| 681 | #ifdef CONFIG_KALLSYMS |
| 682 | if (*fmt == 'B') |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 683 | sprint_backtrace(sym, value); |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 684 | else if (*fmt != 'f' && *fmt != 's') |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 685 | sprint_symbol(sym, value); |
| 686 | else |
Stephen Boyd | 4796dd2 | 2012-05-29 15:07:33 -0700 | [diff] [blame] | 687 | sprint_symbol_no_offset(sym, value); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 688 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 689 | return string(buf, end, sym, spec); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 690 | #else |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 691 | return special_hex_number(buf, end, value, sizeof(void *)); |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 692 | #endif |
| 693 | } |
| 694 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 695 | static noinline_for_stack |
| 696 | char *resource_string(char *buf, char *end, struct resource *res, |
| 697 | struct printf_spec spec, const char *fmt) |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 698 | { |
| 699 | #ifndef IO_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 700 | #define IO_RSRC_PRINTK_SIZE 6 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 701 | #endif |
| 702 | |
| 703 | #ifndef MEM_RSRC_PRINTK_SIZE |
Bjorn Helgaas | 2840537 | 2009-10-06 15:33:29 -0600 | [diff] [blame] | 704 | #define MEM_RSRC_PRINTK_SIZE 10 |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 705 | #endif |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 706 | static const struct printf_spec io_spec = { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 707 | .base = 16, |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 708 | .field_width = IO_RSRC_PRINTK_SIZE, |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 709 | .precision = -1, |
| 710 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 711 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 712 | static const struct printf_spec mem_spec = { |
| 713 | .base = 16, |
| 714 | .field_width = MEM_RSRC_PRINTK_SIZE, |
| 715 | .precision = -1, |
| 716 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 717 | }; |
Bjorn Helgaas | 0f4050c | 2010-03-05 10:47:42 -0700 | [diff] [blame] | 718 | static const struct printf_spec bus_spec = { |
| 719 | .base = 16, |
| 720 | .field_width = 2, |
| 721 | .precision = -1, |
| 722 | .flags = SMALL | ZEROPAD, |
| 723 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 724 | static const struct printf_spec dec_spec = { |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 725 | .base = 10, |
| 726 | .precision = -1, |
| 727 | .flags = 0, |
| 728 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 729 | static const struct printf_spec str_spec = { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 730 | .field_width = -1, |
| 731 | .precision = 10, |
| 732 | .flags = LEFT, |
| 733 | }; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 734 | static const struct printf_spec flag_spec = { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 735 | .base = 16, |
| 736 | .precision = -1, |
| 737 | .flags = SPECIAL | SMALL, |
| 738 | }; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 739 | |
| 740 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
| 741 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
| 742 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
| 743 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
Bjorn Helgaas | 9d7cca0 | 2010-03-05 10:47:47 -0700 | [diff] [blame] | 744 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 745 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
| 746 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
| 747 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
| 748 | |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 749 | char *p = sym, *pend = sym + sizeof(sym); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 750 | int decode = (fmt[0] == 'R') ? 1 : 0; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 751 | const struct printf_spec *specp; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 752 | |
| 753 | *p++ = '['; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 754 | if (res->flags & IORESOURCE_IO) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 755 | p = string(p, pend, "io ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 756 | specp = &io_spec; |
| 757 | } else if (res->flags & IORESOURCE_MEM) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 758 | p = string(p, pend, "mem ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 759 | specp = &mem_spec; |
| 760 | } else if (res->flags & IORESOURCE_IRQ) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 761 | p = string(p, pend, "irq ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 762 | specp = &dec_spec; |
| 763 | } else if (res->flags & IORESOURCE_DMA) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 764 | p = string(p, pend, "dma ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 765 | specp = &dec_spec; |
Bjorn Helgaas | 0f4050c | 2010-03-05 10:47:42 -0700 | [diff] [blame] | 766 | } else if (res->flags & IORESOURCE_BUS) { |
| 767 | p = string(p, pend, "bus ", str_spec); |
| 768 | specp = &bus_spec; |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 769 | } else { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 770 | p = string(p, pend, "??? ", str_spec); |
Bjorn Helgaas | 4da0b66 | 2010-03-05 10:47:37 -0700 | [diff] [blame] | 771 | specp = &mem_spec; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 772 | decode = 0; |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 773 | } |
Bjorn Helgaas | d19cb80 | 2014-02-26 11:25:56 -0700 | [diff] [blame] | 774 | if (decode && res->flags & IORESOURCE_UNSET) { |
| 775 | p = string(p, pend, "size ", str_spec); |
| 776 | p = number(p, pend, resource_size(res), *specp); |
| 777 | } else { |
| 778 | p = number(p, pend, res->start, *specp); |
| 779 | if (res->start != res->end) { |
| 780 | *p++ = '-'; |
| 781 | p = number(p, pend, res->end, *specp); |
| 782 | } |
Bjorn Helgaas | c91d337 | 2009-10-06 15:33:34 -0600 | [diff] [blame] | 783 | } |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 784 | if (decode) { |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 785 | if (res->flags & IORESOURCE_MEM_64) |
| 786 | p = string(p, pend, " 64bit", str_spec); |
| 787 | if (res->flags & IORESOURCE_PREFETCH) |
| 788 | p = string(p, pend, " pref", str_spec); |
Bjorn Helgaas | 9d7cca0 | 2010-03-05 10:47:47 -0700 | [diff] [blame] | 789 | if (res->flags & IORESOURCE_WINDOW) |
| 790 | p = string(p, pend, " window", str_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 791 | if (res->flags & IORESOURCE_DISABLED) |
| 792 | p = string(p, pend, " disabled", str_spec); |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 793 | } else { |
| 794 | p = string(p, pend, " flags ", str_spec); |
| 795 | p = number(p, pend, res->flags, flag_spec); |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 796 | } |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 797 | *p++ = ']'; |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 798 | *p = '\0'; |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 799 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 800 | return string(buf, end, sym, spec); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 801 | } |
| 802 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 803 | static noinline_for_stack |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 804 | char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
| 805 | const char *fmt) |
| 806 | { |
Steven Rostedt | 360603a | 2013-05-28 15:47:39 -0400 | [diff] [blame] | 807 | int i, len = 1; /* if we pass '%ph[CDN]', field width remains |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 808 | negative value, fallback to the default */ |
| 809 | char separator; |
| 810 | |
| 811 | if (spec.field_width == 0) |
| 812 | /* nothing to print */ |
| 813 | return buf; |
| 814 | |
| 815 | if (ZERO_OR_NULL_PTR(addr)) |
| 816 | /* NULL pointer */ |
| 817 | return string(buf, end, NULL, spec); |
| 818 | |
| 819 | switch (fmt[1]) { |
| 820 | case 'C': |
| 821 | separator = ':'; |
| 822 | break; |
| 823 | case 'D': |
| 824 | separator = '-'; |
| 825 | break; |
| 826 | case 'N': |
| 827 | separator = 0; |
| 828 | break; |
| 829 | default: |
| 830 | separator = ' '; |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | if (spec.field_width > 0) |
| 835 | len = min_t(int, spec.field_width, 64); |
| 836 | |
Rasmus Villemoes | 9c98f23 | 2015-04-15 16:17:23 -0700 | [diff] [blame] | 837 | for (i = 0; i < len; ++i) { |
| 838 | if (buf < end) |
| 839 | *buf = hex_asc_hi(addr[i]); |
| 840 | ++buf; |
| 841 | if (buf < end) |
| 842 | *buf = hex_asc_lo(addr[i]); |
| 843 | ++buf; |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 844 | |
Rasmus Villemoes | 9c98f23 | 2015-04-15 16:17:23 -0700 | [diff] [blame] | 845 | if (separator && i != len - 1) { |
| 846 | if (buf < end) |
| 847 | *buf = separator; |
| 848 | ++buf; |
| 849 | } |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | return buf; |
| 853 | } |
| 854 | |
| 855 | static noinline_for_stack |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 856 | char *bitmap_string(char *buf, char *end, unsigned long *bitmap, |
| 857 | struct printf_spec spec, const char *fmt) |
| 858 | { |
| 859 | const int CHUNKSZ = 32; |
| 860 | int nr_bits = max_t(int, spec.field_width, 0); |
| 861 | int i, chunksz; |
| 862 | bool first = true; |
| 863 | |
| 864 | /* reused to print numbers */ |
| 865 | spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; |
| 866 | |
| 867 | chunksz = nr_bits & (CHUNKSZ - 1); |
| 868 | if (chunksz == 0) |
| 869 | chunksz = CHUNKSZ; |
| 870 | |
| 871 | i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; |
| 872 | for (; i >= 0; i -= CHUNKSZ) { |
| 873 | u32 chunkmask, val; |
| 874 | int word, bit; |
| 875 | |
| 876 | chunkmask = ((1ULL << chunksz) - 1); |
| 877 | word = i / BITS_PER_LONG; |
| 878 | bit = i % BITS_PER_LONG; |
| 879 | val = (bitmap[word] >> bit) & chunkmask; |
| 880 | |
| 881 | if (!first) { |
| 882 | if (buf < end) |
| 883 | *buf = ','; |
| 884 | buf++; |
| 885 | } |
| 886 | first = false; |
| 887 | |
| 888 | spec.field_width = DIV_ROUND_UP(chunksz, 4); |
| 889 | buf = number(buf, end, val, spec); |
| 890 | |
| 891 | chunksz = CHUNKSZ; |
| 892 | } |
| 893 | return buf; |
| 894 | } |
| 895 | |
| 896 | static noinline_for_stack |
| 897 | char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, |
| 898 | struct printf_spec spec, const char *fmt) |
| 899 | { |
| 900 | int nr_bits = max_t(int, spec.field_width, 0); |
| 901 | /* current bit is 'cur', most recently seen range is [rbot, rtop] */ |
| 902 | int cur, rbot, rtop; |
| 903 | bool first = true; |
| 904 | |
| 905 | /* reused to print numbers */ |
| 906 | spec = (struct printf_spec){ .base = 10 }; |
| 907 | |
| 908 | rbot = cur = find_first_bit(bitmap, nr_bits); |
| 909 | while (cur < nr_bits) { |
| 910 | rtop = cur; |
| 911 | cur = find_next_bit(bitmap, nr_bits, cur + 1); |
| 912 | if (cur < nr_bits && cur <= rtop + 1) |
| 913 | continue; |
| 914 | |
| 915 | if (!first) { |
| 916 | if (buf < end) |
| 917 | *buf = ','; |
| 918 | buf++; |
| 919 | } |
| 920 | first = false; |
| 921 | |
| 922 | buf = number(buf, end, rbot, spec); |
| 923 | if (rbot < rtop) { |
| 924 | if (buf < end) |
| 925 | *buf = '-'; |
| 926 | buf++; |
| 927 | |
| 928 | buf = number(buf, end, rtop, spec); |
| 929 | } |
| 930 | |
| 931 | rbot = cur; |
| 932 | } |
| 933 | return buf; |
| 934 | } |
| 935 | |
| 936 | static noinline_for_stack |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 937 | char *mac_address_string(char *buf, char *end, u8 *addr, |
| 938 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 939 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 940 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 941 | char *p = mac_addr; |
| 942 | int i; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 943 | char separator; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 944 | bool reversed = false; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 945 | |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 946 | switch (fmt[1]) { |
| 947 | case 'F': |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 948 | separator = '-'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 949 | break; |
| 950 | |
| 951 | case 'R': |
| 952 | reversed = true; |
| 953 | /* fall through */ |
| 954 | |
| 955 | default: |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 956 | separator = ':'; |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 957 | break; |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 958 | } |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 959 | |
| 960 | for (i = 0; i < 6; i++) { |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 961 | if (reversed) |
| 962 | p = hex_byte_pack(p, addr[5 - i]); |
| 963 | else |
| 964 | p = hex_byte_pack(p, addr[i]); |
| 965 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 966 | if (fmt[0] == 'M' && i != 5) |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 967 | *p++ = separator; |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 968 | } |
| 969 | *p = '\0'; |
| 970 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 971 | return string(buf, end, mac_addr, spec); |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 974 | static noinline_for_stack |
| 975 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 976 | { |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 977 | int i; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 978 | bool leading_zeros = (fmt[0] == 'i'); |
| 979 | int index; |
| 980 | int step; |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 981 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 982 | switch (fmt[2]) { |
| 983 | case 'h': |
| 984 | #ifdef __BIG_ENDIAN |
| 985 | index = 0; |
| 986 | step = 1; |
| 987 | #else |
| 988 | index = 3; |
| 989 | step = -1; |
| 990 | #endif |
| 991 | break; |
| 992 | case 'l': |
| 993 | index = 3; |
| 994 | step = -1; |
| 995 | break; |
| 996 | case 'n': |
| 997 | case 'b': |
| 998 | default: |
| 999 | index = 0; |
| 1000 | step = 1; |
| 1001 | break; |
| 1002 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1003 | for (i = 0; i < 4; i++) { |
Rasmus Villemoes | 7c43d9a | 2015-04-16 12:43:22 -0700 | [diff] [blame] | 1004 | char temp[4] __aligned(2); /* hold each IP quad in reverse order */ |
Denys Vlasenko | 133fd9f | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1005 | int digits = put_dec_trunc8(temp, addr[index]) - temp; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1006 | if (leading_zeros) { |
| 1007 | if (digits < 3) |
| 1008 | *p++ = '0'; |
| 1009 | if (digits < 2) |
| 1010 | *p++ = '0'; |
| 1011 | } |
| 1012 | /* reverse the digits in the quad */ |
| 1013 | while (digits--) |
| 1014 | *p++ = temp[digits]; |
| 1015 | if (i < 3) |
| 1016 | *p++ = '.'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1017 | index += step; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1018 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1019 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1020 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1021 | return p; |
| 1022 | } |
| 1023 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1024 | static noinline_for_stack |
| 1025 | char *ip6_compressed_string(char *p, const char *addr) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1026 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1027 | int i, j, range; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1028 | unsigned char zerolength[8]; |
| 1029 | int longest = 1; |
| 1030 | int colonpos = -1; |
| 1031 | u16 word; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1032 | u8 hi, lo; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1033 | bool needcolon = false; |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1034 | bool useIPv4; |
| 1035 | struct in6_addr in6; |
| 1036 | |
| 1037 | memcpy(&in6, addr, sizeof(struct in6_addr)); |
| 1038 | |
| 1039 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1040 | |
| 1041 | memset(zerolength, 0, sizeof(zerolength)); |
| 1042 | |
| 1043 | if (useIPv4) |
| 1044 | range = 6; |
| 1045 | else |
| 1046 | range = 8; |
| 1047 | |
| 1048 | /* find position of longest 0 run */ |
| 1049 | for (i = 0; i < range; i++) { |
| 1050 | for (j = i; j < range; j++) { |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1051 | if (in6.s6_addr16[j] != 0) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1052 | break; |
| 1053 | zerolength[i]++; |
| 1054 | } |
| 1055 | } |
| 1056 | for (i = 0; i < range; i++) { |
| 1057 | if (zerolength[i] > longest) { |
| 1058 | longest = zerolength[i]; |
| 1059 | colonpos = i; |
| 1060 | } |
| 1061 | } |
Joe Perches | 29cf519 | 2011-06-09 11:23:37 -0700 | [diff] [blame] | 1062 | if (longest == 1) /* don't compress a single 0 */ |
| 1063 | colonpos = -1; |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1064 | |
| 1065 | /* emit address */ |
| 1066 | for (i = 0; i < range; i++) { |
| 1067 | if (i == colonpos) { |
| 1068 | if (needcolon || i == 0) |
| 1069 | *p++ = ':'; |
| 1070 | *p++ = ':'; |
| 1071 | needcolon = false; |
| 1072 | i += longest - 1; |
| 1073 | continue; |
| 1074 | } |
| 1075 | if (needcolon) { |
| 1076 | *p++ = ':'; |
| 1077 | needcolon = false; |
| 1078 | } |
| 1079 | /* hex u16 without leading 0s */ |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1080 | word = ntohs(in6.s6_addr16[i]); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1081 | hi = word >> 8; |
| 1082 | lo = word & 0xff; |
| 1083 | if (hi) { |
| 1084 | if (hi > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1085 | p = hex_byte_pack(p, hi); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1086 | else |
| 1087 | *p++ = hex_asc_lo(hi); |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1088 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1089 | } |
André Goddard Rosa | b5ff992 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 1090 | else if (lo > 0x0f) |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1091 | p = hex_byte_pack(p, lo); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1092 | else |
| 1093 | *p++ = hex_asc_lo(lo); |
| 1094 | needcolon = true; |
| 1095 | } |
| 1096 | |
| 1097 | if (useIPv4) { |
| 1098 | if (needcolon) |
| 1099 | *p++ = ':'; |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1100 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1101 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1102 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1103 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1104 | return p; |
| 1105 | } |
| 1106 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1107 | static noinline_for_stack |
| 1108 | char *ip6_string(char *p, const char *addr, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1109 | { |
| 1110 | int i; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1111 | |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1112 | for (i = 0; i < 8; i++) { |
Andy Shevchenko | 55036ba | 2011-10-31 17:12:41 -0700 | [diff] [blame] | 1113 | p = hex_byte_pack(p, *addr++); |
| 1114 | p = hex_byte_pack(p, *addr++); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1115 | if (fmt[0] == 'I' && i != 7) |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1116 | *p++ = ':'; |
| 1117 | } |
| 1118 | *p = '\0'; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 1119 | |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1120 | return p; |
| 1121 | } |
| 1122 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1123 | static noinline_for_stack |
| 1124 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, |
| 1125 | struct printf_spec spec, const char *fmt) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1126 | { |
| 1127 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
| 1128 | |
| 1129 | if (fmt[0] == 'I' && fmt[2] == 'c') |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1130 | ip6_compressed_string(ip6_addr, addr); |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1131 | else |
Joe Perches | eb78cd2 | 2009-09-18 13:04:06 +0000 | [diff] [blame] | 1132 | ip6_string(ip6_addr, addr, fmt); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1133 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1134 | return string(buf, end, ip6_addr, spec); |
Harvey Harrison | 689afa7 | 2008-10-28 16:04:44 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1137 | static noinline_for_stack |
| 1138 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
| 1139 | struct printf_spec spec, const char *fmt) |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1140 | { |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1141 | char ip4_addr[sizeof("255.255.255.255")]; |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1142 | |
Joe Perches | 0159f24 | 2010-01-13 20:23:30 -0800 | [diff] [blame] | 1143 | ip4_string(ip4_addr, addr, fmt); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1144 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1145 | return string(buf, end, ip4_addr, spec); |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1146 | } |
| 1147 | |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1148 | static noinline_for_stack |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1149 | char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, |
| 1150 | struct printf_spec spec, const char *fmt) |
| 1151 | { |
| 1152 | bool have_p = false, have_s = false, have_f = false, have_c = false; |
| 1153 | char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + |
| 1154 | sizeof(":12345") + sizeof("/123456789") + |
| 1155 | sizeof("%1234567890")]; |
| 1156 | char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); |
| 1157 | const u8 *addr = (const u8 *) &sa->sin6_addr; |
| 1158 | char fmt6[2] = { fmt[0], '6' }; |
| 1159 | u8 off = 0; |
| 1160 | |
| 1161 | fmt++; |
| 1162 | while (isalpha(*++fmt)) { |
| 1163 | switch (*fmt) { |
| 1164 | case 'p': |
| 1165 | have_p = true; |
| 1166 | break; |
| 1167 | case 'f': |
| 1168 | have_f = true; |
| 1169 | break; |
| 1170 | case 's': |
| 1171 | have_s = true; |
| 1172 | break; |
| 1173 | case 'c': |
| 1174 | have_c = true; |
| 1175 | break; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | if (have_p || have_s || have_f) { |
| 1180 | *p = '['; |
| 1181 | off = 1; |
| 1182 | } |
| 1183 | |
| 1184 | if (fmt6[0] == 'I' && have_c) |
| 1185 | p = ip6_compressed_string(ip6_addr + off, addr); |
| 1186 | else |
| 1187 | p = ip6_string(ip6_addr + off, addr, fmt6); |
| 1188 | |
| 1189 | if (have_p || have_s || have_f) |
| 1190 | *p++ = ']'; |
| 1191 | |
| 1192 | if (have_p) { |
| 1193 | *p++ = ':'; |
| 1194 | p = number(p, pend, ntohs(sa->sin6_port), spec); |
| 1195 | } |
| 1196 | if (have_f) { |
| 1197 | *p++ = '/'; |
| 1198 | p = number(p, pend, ntohl(sa->sin6_flowinfo & |
| 1199 | IPV6_FLOWINFO_MASK), spec); |
| 1200 | } |
| 1201 | if (have_s) { |
| 1202 | *p++ = '%'; |
| 1203 | p = number(p, pend, sa->sin6_scope_id, spec); |
| 1204 | } |
| 1205 | *p = '\0'; |
| 1206 | |
| 1207 | return string(buf, end, ip6_addr, spec); |
| 1208 | } |
| 1209 | |
| 1210 | static noinline_for_stack |
| 1211 | char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, |
| 1212 | struct printf_spec spec, const char *fmt) |
| 1213 | { |
| 1214 | bool have_p = false; |
| 1215 | char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; |
| 1216 | char *pend = ip4_addr + sizeof(ip4_addr); |
| 1217 | const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; |
| 1218 | char fmt4[3] = { fmt[0], '4', 0 }; |
| 1219 | |
| 1220 | fmt++; |
| 1221 | while (isalpha(*++fmt)) { |
| 1222 | switch (*fmt) { |
| 1223 | case 'p': |
| 1224 | have_p = true; |
| 1225 | break; |
| 1226 | case 'h': |
| 1227 | case 'l': |
| 1228 | case 'n': |
| 1229 | case 'b': |
| 1230 | fmt4[2] = *fmt; |
| 1231 | break; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | p = ip4_string(ip4_addr, addr, fmt4); |
| 1236 | if (have_p) { |
| 1237 | *p++ = ':'; |
| 1238 | p = number(p, pend, ntohs(sa->sin_port), spec); |
| 1239 | } |
| 1240 | *p = '\0'; |
| 1241 | |
| 1242 | return string(buf, end, ip4_addr, spec); |
| 1243 | } |
| 1244 | |
| 1245 | static noinline_for_stack |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1246 | char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, |
| 1247 | const char *fmt) |
| 1248 | { |
| 1249 | bool found = true; |
| 1250 | int count = 1; |
| 1251 | unsigned int flags = 0; |
| 1252 | int len; |
| 1253 | |
| 1254 | if (spec.field_width == 0) |
| 1255 | return buf; /* nothing to print */ |
| 1256 | |
| 1257 | if (ZERO_OR_NULL_PTR(addr)) |
| 1258 | return string(buf, end, NULL, spec); /* NULL pointer */ |
| 1259 | |
| 1260 | |
| 1261 | do { |
| 1262 | switch (fmt[count++]) { |
| 1263 | case 'a': |
| 1264 | flags |= ESCAPE_ANY; |
| 1265 | break; |
| 1266 | case 'c': |
| 1267 | flags |= ESCAPE_SPECIAL; |
| 1268 | break; |
| 1269 | case 'h': |
| 1270 | flags |= ESCAPE_HEX; |
| 1271 | break; |
| 1272 | case 'n': |
| 1273 | flags |= ESCAPE_NULL; |
| 1274 | break; |
| 1275 | case 'o': |
| 1276 | flags |= ESCAPE_OCTAL; |
| 1277 | break; |
| 1278 | case 'p': |
| 1279 | flags |= ESCAPE_NP; |
| 1280 | break; |
| 1281 | case 's': |
| 1282 | flags |= ESCAPE_SPACE; |
| 1283 | break; |
| 1284 | default: |
| 1285 | found = false; |
| 1286 | break; |
| 1287 | } |
| 1288 | } while (found); |
| 1289 | |
| 1290 | if (!flags) |
| 1291 | flags = ESCAPE_ANY_NP; |
| 1292 | |
| 1293 | len = spec.field_width < 0 ? 1 : spec.field_width; |
| 1294 | |
Rasmus Villemoes | 41416f2 | 2015-04-15 16:17:28 -0700 | [diff] [blame] | 1295 | /* |
| 1296 | * string_escape_mem() writes as many characters as it can to |
| 1297 | * the given buffer, and returns the total size of the output |
| 1298 | * had the buffer been big enough. |
| 1299 | */ |
| 1300 | buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1301 | |
| 1302 | return buf; |
| 1303 | } |
| 1304 | |
| 1305 | static noinline_for_stack |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1306 | char *uuid_string(char *buf, char *end, const u8 *addr, |
| 1307 | struct printf_spec spec, const char *fmt) |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1308 | { |
Andy Shevchenko | 2b1b0d6 | 2016-05-20 17:01:04 -0700 | [diff] [blame] | 1309 | char uuid[UUID_STRING_LEN + 1]; |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1310 | char *p = uuid; |
| 1311 | int i; |
Christoph Hellwig | f9727a1 | 2017-05-17 10:02:48 +0200 | [diff] [blame] | 1312 | const u8 *index = uuid_index; |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1313 | bool uc = false; |
| 1314 | |
| 1315 | switch (*(++fmt)) { |
| 1316 | case 'L': |
| 1317 | uc = true; /* fall-through */ |
| 1318 | case 'l': |
Christoph Hellwig | f9727a1 | 2017-05-17 10:02:48 +0200 | [diff] [blame] | 1319 | index = guid_index; |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1320 | break; |
| 1321 | case 'B': |
| 1322 | uc = true; |
| 1323 | break; |
| 1324 | } |
| 1325 | |
| 1326 | for (i = 0; i < 16; i++) { |
Andy Shevchenko | aa4ea1c | 2016-05-20 17:00:54 -0700 | [diff] [blame] | 1327 | if (uc) |
| 1328 | p = hex_byte_pack_upper(p, addr[index[i]]); |
| 1329 | else |
| 1330 | p = hex_byte_pack(p, addr[index[i]]); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1331 | switch (i) { |
| 1332 | case 3: |
| 1333 | case 5: |
| 1334 | case 7: |
| 1335 | case 9: |
| 1336 | *p++ = '-'; |
| 1337 | break; |
| 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | *p = 0; |
| 1342 | |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1343 | return string(buf, end, uuid, spec); |
| 1344 | } |
| 1345 | |
Andy Shevchenko | 5b17aec | 2016-01-15 16:59:20 -0800 | [diff] [blame] | 1346 | static noinline_for_stack |
| 1347 | char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt) |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1348 | { |
Andy Shevchenko | 5b17aec | 2016-01-15 16:59:20 -0800 | [diff] [blame] | 1349 | unsigned long long num; |
| 1350 | int size; |
| 1351 | |
| 1352 | switch (fmt[1]) { |
| 1353 | case 'F': |
| 1354 | num = *(const netdev_features_t *)addr; |
| 1355 | size = sizeof(netdev_features_t); |
| 1356 | break; |
| 1357 | default: |
| 1358 | num = (unsigned long)addr; |
| 1359 | size = sizeof(unsigned long); |
| 1360 | break; |
| 1361 | } |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1362 | |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1363 | return special_hex_number(buf, end, num, size); |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1366 | static noinline_for_stack |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1367 | char *address_val(char *buf, char *end, const void *addr, const char *fmt) |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1368 | { |
| 1369 | unsigned long long num; |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1370 | int size; |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1371 | |
| 1372 | switch (fmt[1]) { |
| 1373 | case 'd': |
| 1374 | num = *(const dma_addr_t *)addr; |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1375 | size = sizeof(dma_addr_t); |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1376 | break; |
| 1377 | case 'p': |
| 1378 | default: |
| 1379 | num = *(const phys_addr_t *)addr; |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1380 | size = sizeof(phys_addr_t); |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1381 | break; |
| 1382 | } |
| 1383 | |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1384 | return special_hex_number(buf, end, num, size); |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1385 | } |
| 1386 | |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1387 | static noinline_for_stack |
| 1388 | char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, |
| 1389 | const char *fmt) |
| 1390 | { |
| 1391 | if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) |
| 1392 | return string(buf, end, NULL, spec); |
| 1393 | |
| 1394 | switch (fmt[1]) { |
| 1395 | case 'r': |
| 1396 | return number(buf, end, clk_get_rate(clk), spec); |
| 1397 | |
| 1398 | case 'n': |
| 1399 | default: |
| 1400 | #ifdef CONFIG_COMMON_CLK |
| 1401 | return string(buf, end, __clk_get_name(clk), spec); |
| 1402 | #else |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1403 | return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long)); |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1404 | #endif |
| 1405 | } |
| 1406 | } |
| 1407 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1408 | static |
| 1409 | char *format_flags(char *buf, char *end, unsigned long flags, |
| 1410 | const struct trace_print_flags *names) |
| 1411 | { |
| 1412 | unsigned long mask; |
| 1413 | const struct printf_spec strspec = { |
| 1414 | .field_width = -1, |
| 1415 | .precision = -1, |
| 1416 | }; |
| 1417 | const struct printf_spec numspec = { |
| 1418 | .flags = SPECIAL|SMALL, |
| 1419 | .field_width = -1, |
| 1420 | .precision = -1, |
| 1421 | .base = 16, |
| 1422 | }; |
| 1423 | |
| 1424 | for ( ; flags && names->name; names++) { |
| 1425 | mask = names->mask; |
| 1426 | if ((flags & mask) != mask) |
| 1427 | continue; |
| 1428 | |
| 1429 | buf = string(buf, end, names->name, strspec); |
| 1430 | |
| 1431 | flags &= ~mask; |
| 1432 | if (flags) { |
| 1433 | if (buf < end) |
| 1434 | *buf = '|'; |
| 1435 | buf++; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | if (flags) |
| 1440 | buf = number(buf, end, flags, numspec); |
| 1441 | |
| 1442 | return buf; |
| 1443 | } |
| 1444 | |
| 1445 | static noinline_for_stack |
| 1446 | char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt) |
| 1447 | { |
| 1448 | unsigned long flags; |
| 1449 | const struct trace_print_flags *names; |
| 1450 | |
| 1451 | switch (fmt[1]) { |
| 1452 | case 'p': |
| 1453 | flags = *(unsigned long *)flags_ptr; |
| 1454 | /* Remove zone id */ |
| 1455 | flags &= (1UL << NR_PAGEFLAGS) - 1; |
| 1456 | names = pageflag_names; |
| 1457 | break; |
| 1458 | case 'v': |
| 1459 | flags = *(unsigned long *)flags_ptr; |
| 1460 | names = vmaflag_names; |
| 1461 | break; |
| 1462 | case 'g': |
| 1463 | flags = *(gfp_t *)flags_ptr; |
| 1464 | names = gfpflag_names; |
| 1465 | break; |
| 1466 | default: |
| 1467 | WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]); |
| 1468 | return buf; |
| 1469 | } |
| 1470 | |
| 1471 | return format_flags(buf, end, flags, names); |
| 1472 | } |
| 1473 | |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 1474 | static const char *device_node_name_for_depth(const struct device_node *np, int depth) |
| 1475 | { |
| 1476 | for ( ; np && depth; depth--) |
| 1477 | np = np->parent; |
| 1478 | |
| 1479 | return kbasename(np->full_name); |
| 1480 | } |
| 1481 | |
| 1482 | static noinline_for_stack |
| 1483 | char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end) |
| 1484 | { |
| 1485 | int depth; |
| 1486 | const struct device_node *parent = np->parent; |
| 1487 | static const struct printf_spec strspec = { |
| 1488 | .field_width = -1, |
| 1489 | .precision = -1, |
| 1490 | }; |
| 1491 | |
| 1492 | /* special case for root node */ |
| 1493 | if (!parent) |
| 1494 | return string(buf, end, "/", strspec); |
| 1495 | |
| 1496 | for (depth = 0; parent->parent; depth++) |
| 1497 | parent = parent->parent; |
| 1498 | |
| 1499 | for ( ; depth >= 0; depth--) { |
| 1500 | buf = string(buf, end, "/", strspec); |
| 1501 | buf = string(buf, end, device_node_name_for_depth(np, depth), |
| 1502 | strspec); |
| 1503 | } |
| 1504 | return buf; |
| 1505 | } |
| 1506 | |
| 1507 | static noinline_for_stack |
| 1508 | char *device_node_string(char *buf, char *end, struct device_node *dn, |
| 1509 | struct printf_spec spec, const char *fmt) |
| 1510 | { |
| 1511 | char tbuf[sizeof("xxxx") + 1]; |
| 1512 | const char *p; |
| 1513 | int ret; |
| 1514 | char *buf_start = buf; |
| 1515 | struct property *prop; |
| 1516 | bool has_mult, pass; |
| 1517 | static const struct printf_spec num_spec = { |
| 1518 | .flags = SMALL, |
| 1519 | .field_width = -1, |
| 1520 | .precision = -1, |
| 1521 | .base = 10, |
| 1522 | }; |
| 1523 | |
| 1524 | struct printf_spec str_spec = spec; |
| 1525 | str_spec.field_width = -1; |
| 1526 | |
| 1527 | if (!IS_ENABLED(CONFIG_OF)) |
| 1528 | return string(buf, end, "(!OF)", spec); |
| 1529 | |
| 1530 | if ((unsigned long)dn < PAGE_SIZE) |
| 1531 | return string(buf, end, "(null)", spec); |
| 1532 | |
| 1533 | /* simple case without anything any more format specifiers */ |
| 1534 | fmt++; |
| 1535 | if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) |
| 1536 | fmt = "f"; |
| 1537 | |
| 1538 | for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { |
| 1539 | if (pass) { |
| 1540 | if (buf < end) |
| 1541 | *buf = ':'; |
| 1542 | buf++; |
| 1543 | } |
| 1544 | |
| 1545 | switch (*fmt) { |
| 1546 | case 'f': /* full_name */ |
| 1547 | buf = device_node_gen_full_name(dn, buf, end); |
| 1548 | break; |
| 1549 | case 'n': /* name */ |
| 1550 | buf = string(buf, end, dn->name, str_spec); |
| 1551 | break; |
| 1552 | case 'p': /* phandle */ |
| 1553 | buf = number(buf, end, (unsigned int)dn->phandle, num_spec); |
| 1554 | break; |
| 1555 | case 'P': /* path-spec */ |
| 1556 | p = kbasename(of_node_full_name(dn)); |
| 1557 | if (!p[1]) |
| 1558 | p = "/"; |
| 1559 | buf = string(buf, end, p, str_spec); |
| 1560 | break; |
| 1561 | case 'F': /* flags */ |
| 1562 | tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; |
| 1563 | tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; |
| 1564 | tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; |
| 1565 | tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; |
| 1566 | tbuf[4] = 0; |
| 1567 | buf = string(buf, end, tbuf, str_spec); |
| 1568 | break; |
| 1569 | case 'c': /* major compatible string */ |
| 1570 | ret = of_property_read_string(dn, "compatible", &p); |
| 1571 | if (!ret) |
| 1572 | buf = string(buf, end, p, str_spec); |
| 1573 | break; |
| 1574 | case 'C': /* full compatible string */ |
| 1575 | has_mult = false; |
| 1576 | of_property_for_each_string(dn, "compatible", prop, p) { |
| 1577 | if (has_mult) |
| 1578 | buf = string(buf, end, ",", str_spec); |
| 1579 | buf = string(buf, end, "\"", str_spec); |
| 1580 | buf = string(buf, end, p, str_spec); |
| 1581 | buf = string(buf, end, "\"", str_spec); |
| 1582 | |
| 1583 | has_mult = true; |
| 1584 | } |
| 1585 | break; |
| 1586 | default: |
| 1587 | break; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | return widen_string(buf, buf - buf_start, end, spec); |
| 1592 | } |
| 1593 | |
Ingo Molnar | 411f05f | 2011-05-12 23:00:28 +0200 | [diff] [blame] | 1594 | int kptr_restrict __read_mostly; |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1595 | |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 1596 | /* |
| 1597 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 1598 | * by an extra set of alphanumeric characters that are extended format |
| 1599 | * specifiers. |
| 1600 | * |
Joe Perches | 0b52376 | 2017-05-08 15:55:36 -0700 | [diff] [blame] | 1601 | * Please update scripts/checkpatch.pl when adding/removing conversion |
| 1602 | * characters. (Search for "check for vsprintf extension"). |
| 1603 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1604 | * Right now we handle: |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1605 | * |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1606 | * - 'F' For symbolic function descriptor pointers with offset |
| 1607 | * - 'f' For simple symbolic function names without offset |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 1608 | * - 'S' For symbolic direct pointers with offset |
| 1609 | * - 's' For symbolic direct pointers without offset |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 1610 | * - '[FfSs]R' as above with __builtin_extract_return_addr() translation |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1611 | * - 'B' For backtraced symbolic direct pointers with offset |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1612 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
| 1613 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1614 | * - 'b[l]' For a bitmap, the number of bits is determined by the field |
| 1615 | * width which must be explicitly specified either as part of the |
| 1616 | * format string '%32b[l]' or through '%*b[l]', [l] selects |
| 1617 | * range-list format instead of hex format |
Harvey Harrison | dd45c9c | 2008-10-27 15:47:12 -0700 | [diff] [blame] | 1618 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 1619 | * usual colon-separated hex notation |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1620 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
Joe Perches | bc7259a | 2010-01-07 11:43:50 +0000 | [diff] [blame] | 1621 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
Joe Perches | c8e0006 | 2010-01-11 00:44:14 -0800 | [diff] [blame] | 1622 | * with a dash-separated hex notation |
Andy Shevchenko | 7c59154 | 2012-10-04 17:12:33 -0700 | [diff] [blame] | 1623 | * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1624 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
| 1625 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
| 1626 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1627 | * [S][pfs] |
| 1628 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
| 1629 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1630 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
| 1631 | * IPv6 omits the colons (01020304...0f) |
| 1632 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1633 | * [S][pfs] |
| 1634 | * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to |
| 1635 | * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] |
| 1636 | * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order |
| 1637 | * - 'I[6S]c' for IPv6 addresses printed as specified by |
Joe Perches | 29cf519 | 2011-06-09 11:23:37 -0700 | [diff] [blame] | 1638 | * http://tools.ietf.org/html/rfc5952 |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1639 | * - 'E[achnops]' For an escaped buffer, where rules are defined by combination |
| 1640 | * of the following flags (see string_escape_mem() for the |
| 1641 | * details): |
| 1642 | * a - ESCAPE_ANY |
| 1643 | * c - ESCAPE_SPECIAL |
| 1644 | * h - ESCAPE_HEX |
| 1645 | * n - ESCAPE_NULL |
| 1646 | * o - ESCAPE_OCTAL |
| 1647 | * p - ESCAPE_NP |
| 1648 | * s - ESCAPE_SPACE |
| 1649 | * By default ESCAPE_ANY_NP is used. |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1650 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
| 1651 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 1652 | * Options for %pU are: |
| 1653 | * b big endian lower case hex (default) |
| 1654 | * B big endian UPPER case hex |
| 1655 | * l little endian lower case hex |
| 1656 | * L little endian UPPER case hex |
| 1657 | * big endian output byte order is: |
| 1658 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
| 1659 | * little endian output byte order is: |
| 1660 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] |
Joe Perches | 7db6f5f | 2010-06-27 01:02:33 +0000 | [diff] [blame] | 1661 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
| 1662 | * call vsnprintf(->format, *->va_list). |
| 1663 | * Implements a "recursive vsnprintf". |
| 1664 | * Do not use this feature without some mechanism to verify the |
| 1665 | * correctness of the format string and va_list arguments. |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1666 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1667 | * - 'NF' For a netdev_features_t |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1668 | * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with |
| 1669 | * a certain separator (' ' by default): |
| 1670 | * C colon |
| 1671 | * D dash |
| 1672 | * N no separator |
| 1673 | * The maximum supported length is 64 bytes of the input. Consider |
| 1674 | * to use print_hex_dump() for the larger input. |
Joe Perches | aaf0762 | 2014-01-23 15:54:17 -0800 | [diff] [blame] | 1675 | * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives |
| 1676 | * (default assumed to be phys_addr_t, passed by reference) |
Olof Johansson | c0d92a5 | 2013-11-12 15:09:50 -0800 | [diff] [blame] | 1677 | * - 'd[234]' For a dentry name (optionally 2-4 last components) |
| 1678 | * - 'D[234]' Same as 'd' but for a struct file |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 1679 | * - 'g' For block_device name (gendisk + partition number) |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1680 | * - 'C' For a clock, it prints the name (Common Clock Framework) or address |
| 1681 | * (legacy clock framework) of the clock |
| 1682 | * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address |
| 1683 | * (legacy clock framework) of the clock |
| 1684 | * - 'Cr' For a clock, it prints the current rate of the clock |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1685 | * - 'G' For flags to be printed as a collection of symbolic strings that would |
| 1686 | * construct the specific value. Supported flags given by option: |
| 1687 | * p page flags (see struct page) given as pointer to unsigned long |
| 1688 | * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t |
| 1689 | * v vma flags (VM_*) given as pointer to unsigned long |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 1690 | * - 'O' For a kobject based struct. Must be one of the following: |
| 1691 | * - 'OF[fnpPcCF]' For a device tree object |
| 1692 | * Without any optional arguments prints the full_name |
| 1693 | * f device node full_name |
| 1694 | * n device node name |
| 1695 | * p device node phandle |
| 1696 | * P device node path spec (name + @unit) |
| 1697 | * F device node flags |
| 1698 | * c major compatible string |
| 1699 | * C full compatible string |
Martin Kletzander | 5e4ee7b | 2015-11-06 16:30:17 -0800 | [diff] [blame] | 1700 | * |
| 1701 | * ** Please update also Documentation/printk-formats.txt when making changes ** |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1702 | * |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1703 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 1704 | * function pointers are really function descriptors, which contain a |
| 1705 | * pointer to the real address. |
Linus Torvalds | 4d8a743 | 2008-07-06 16:24:57 -0700 | [diff] [blame] | 1706 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1707 | static noinline_for_stack |
| 1708 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 1709 | struct printf_spec spec) |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1710 | { |
Rasmus Villemoes | 80c9eb4 | 2015-11-06 16:30:26 -0800 | [diff] [blame] | 1711 | const int default_width = 2 * sizeof(void *); |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1712 | |
Kees Cook | 9f36e2c | 2011-03-22 16:34:22 -0700 | [diff] [blame] | 1713 | if (!ptr && *fmt != 'K') { |
Joe Perches | 5e05798 | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 1714 | /* |
| 1715 | * Print (null) with the same width as a pointer so it makes |
| 1716 | * tabular output look nice. |
| 1717 | */ |
| 1718 | if (spec.field_width == -1) |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1719 | spec.field_width = default_width; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1720 | return string(buf, end, "(null)", spec); |
Joe Perches | 5e05798 | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 1721 | } |
Linus Torvalds | d97106a | 2009-01-03 11:46:17 -0800 | [diff] [blame] | 1722 | |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1723 | switch (*fmt) { |
| 1724 | case 'F': |
Frederic Weisbecker | 0c8b946 | 2009-04-15 17:48:18 +0200 | [diff] [blame] | 1725 | case 'f': |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1726 | ptr = dereference_function_descriptor(ptr); |
| 1727 | /* Fallthrough */ |
| 1728 | case 'S': |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1729 | case 's': |
Namhyung Kim | 0f77a8d | 2011-03-24 11:42:29 +0900 | [diff] [blame] | 1730 | case 'B': |
Joe Perches | b0d33c2 | 2012-12-12 10:18:50 -0800 | [diff] [blame] | 1731 | return symbol_string(buf, end, ptr, spec, fmt); |
Linus Torvalds | 332d2e7 | 2008-10-20 15:07:34 +1100 | [diff] [blame] | 1732 | case 'R': |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 1733 | case 'r': |
Bjorn Helgaas | fd95541 | 2009-10-06 15:33:39 -0600 | [diff] [blame] | 1734 | return resource_string(buf, end, ptr, spec, fmt); |
Andy Shevchenko | 31550a1 | 2012-07-30 14:40:27 -0700 | [diff] [blame] | 1735 | case 'h': |
| 1736 | return hex_string(buf, end, ptr, spec, fmt); |
Tejun Heo | dbc760b | 2015-02-13 14:36:53 -0800 | [diff] [blame] | 1737 | case 'b': |
| 1738 | switch (fmt[1]) { |
| 1739 | case 'l': |
| 1740 | return bitmap_list_string(buf, end, ptr, spec, fmt); |
| 1741 | default: |
| 1742 | return bitmap_string(buf, end, ptr, spec, fmt); |
| 1743 | } |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1744 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
| 1745 | case 'm': /* Contiguous: 000102030405 */ |
Andrei Emeltchenko | 76597ff9 | 2012-07-30 14:40:23 -0700 | [diff] [blame] | 1746 | /* [mM]F (FDDI) */ |
| 1747 | /* [mM]R (Reverse order; Bluetooth) */ |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1748 | return mac_address_string(buf, end, ptr, spec, fmt); |
| 1749 | case 'I': /* Formatted IP supported |
| 1750 | * 4: 1.2.3.4 |
| 1751 | * 6: 0001:0203:...:0708 |
| 1752 | * 6c: 1::708 or 1::1.2.3.4 |
| 1753 | */ |
| 1754 | case 'i': /* Contiguous: |
| 1755 | * 4: 001.002.003.004 |
| 1756 | * 6: 000102...0f |
| 1757 | */ |
| 1758 | switch (fmt[1]) { |
| 1759 | case '6': |
| 1760 | return ip6_addr_string(buf, end, ptr, spec, fmt); |
| 1761 | case '4': |
| 1762 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
Daniel Borkmann | 1067964 | 2013-06-28 19:49:39 +0200 | [diff] [blame] | 1763 | case 'S': { |
| 1764 | const union { |
| 1765 | struct sockaddr raw; |
| 1766 | struct sockaddr_in v4; |
| 1767 | struct sockaddr_in6 v6; |
| 1768 | } *sa = ptr; |
| 1769 | |
| 1770 | switch (sa->raw.sa_family) { |
| 1771 | case AF_INET: |
| 1772 | return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); |
| 1773 | case AF_INET6: |
| 1774 | return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); |
| 1775 | default: |
| 1776 | return string(buf, end, "(invalid address)", spec); |
| 1777 | }} |
Joe Perches | 8a27f7c | 2009-08-17 12:29:44 +0000 | [diff] [blame] | 1778 | } |
Harvey Harrison | 4aa9960 | 2008-10-29 12:49:58 -0700 | [diff] [blame] | 1779 | break; |
Andy Shevchenko | 71dca95 | 2014-10-13 15:55:18 -0700 | [diff] [blame] | 1780 | case 'E': |
| 1781 | return escaped_string(buf, end, ptr, spec, fmt); |
Joe Perches | 9ac6e44 | 2009-12-14 18:01:09 -0800 | [diff] [blame] | 1782 | case 'U': |
| 1783 | return uuid_string(buf, end, ptr, spec, fmt); |
Joe Perches | 7db6f5f | 2010-06-27 01:02:33 +0000 | [diff] [blame] | 1784 | case 'V': |
Jan Beulich | 5756b76 | 2012-03-05 16:49:24 +0000 | [diff] [blame] | 1785 | { |
| 1786 | va_list va; |
| 1787 | |
| 1788 | va_copy(va, *((struct va_format *)ptr)->va); |
| 1789 | buf += vsnprintf(buf, end > buf ? end - buf : 0, |
| 1790 | ((struct va_format *)ptr)->fmt, va); |
| 1791 | va_end(va); |
| 1792 | return buf; |
| 1793 | } |
Dan Rosenberg | 455cd5a | 2011-01-12 16:59:41 -0800 | [diff] [blame] | 1794 | case 'K': |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1795 | switch (kptr_restrict) { |
| 1796 | case 0: |
| 1797 | /* Always print %pK values */ |
| 1798 | break; |
| 1799 | case 1: { |
Jason A. Donenfeld | 7eb3912 | 2016-02-11 16:13:00 -0800 | [diff] [blame] | 1800 | const struct cred *cred; |
| 1801 | |
| 1802 | /* |
| 1803 | * kptr_restrict==1 cannot be used in IRQ context |
| 1804 | * because its test for CAP_SYSLOG would be meaningless. |
| 1805 | */ |
| 1806 | if (in_irq() || in_serving_softirq() || in_nmi()) { |
| 1807 | if (spec.field_width == -1) |
| 1808 | spec.field_width = default_width; |
| 1809 | return string(buf, end, "pK-error", spec); |
| 1810 | } |
| 1811 | |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1812 | /* |
| 1813 | * Only print the real pointer value if the current |
| 1814 | * process has CAP_SYSLOG and is running with the |
| 1815 | * same credentials it started with. This is because |
| 1816 | * access to files is checked at open() time, but %pK |
| 1817 | * checks permission at read() time. We don't want to |
| 1818 | * leak pointer values if a binary opens a file using |
| 1819 | * %pK and then elevates privileges before reading it. |
| 1820 | */ |
Jason A. Donenfeld | 7eb3912 | 2016-02-11 16:13:00 -0800 | [diff] [blame] | 1821 | cred = current_cred(); |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1822 | if (!has_capability_noaudit(current, CAP_SYSLOG) || |
| 1823 | !uid_eq(cred->euid, cred->uid) || |
| 1824 | !gid_eq(cred->egid, cred->gid)) |
| 1825 | ptr = NULL; |
| 1826 | break; |
| 1827 | } |
| 1828 | case 2: |
| 1829 | default: |
| 1830 | /* Always print 0's for %pK */ |
Joe Perches | 2629760 | 2011-03-22 16:34:19 -0700 | [diff] [blame] | 1831 | ptr = NULL; |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1832 | break; |
| 1833 | } |
Joe Perches | 2629760 | 2011-03-22 16:34:19 -0700 | [diff] [blame] | 1834 | break; |
Ryan Mallon | 312b4e2 | 2013-11-12 15:08:51 -0800 | [diff] [blame] | 1835 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1836 | case 'N': |
Andy Shevchenko | 5b17aec | 2016-01-15 16:59:20 -0800 | [diff] [blame] | 1837 | return netdev_bits(buf, end, ptr, fmt); |
Stepan Moskovchenko | 7d79921 | 2013-02-21 16:43:09 -0800 | [diff] [blame] | 1838 | case 'a': |
Andy Shevchenko | 3cab1e7 | 2016-01-15 16:59:18 -0800 | [diff] [blame] | 1839 | return address_val(buf, end, ptr, fmt); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 1840 | case 'd': |
| 1841 | return dentry_name(buf, end, ptr, spec, fmt); |
Geert Uytterhoeven | 900cca2 | 2015-04-15 16:17:20 -0700 | [diff] [blame] | 1842 | case 'C': |
| 1843 | return clock(buf, end, ptr, spec, fmt); |
Al Viro | 4b6ccca | 2013-09-03 12:00:44 -0400 | [diff] [blame] | 1844 | case 'D': |
| 1845 | return dentry_name(buf, end, |
| 1846 | ((const struct file *)ptr)->f_path.dentry, |
| 1847 | spec, fmt); |
Dmitry Monakhov | 1031bc5 | 2015-04-13 16:31:35 +0400 | [diff] [blame] | 1848 | #ifdef CONFIG_BLOCK |
| 1849 | case 'g': |
| 1850 | return bdev_name(buf, end, ptr, spec, fmt); |
| 1851 | #endif |
| 1852 | |
Vlastimil Babka | edf14cd | 2016-03-15 14:55:56 -0700 | [diff] [blame] | 1853 | case 'G': |
| 1854 | return flags_string(buf, end, ptr, fmt); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 1855 | case 'O': |
| 1856 | switch (fmt[1]) { |
| 1857 | case 'F': |
| 1858 | return device_node_string(buf, end, ptr, spec, fmt + 1); |
| 1859 | } |
Linus Torvalds | 0fe1ef2 | 2008-07-06 16:43:12 -0700 | [diff] [blame] | 1860 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1861 | spec.flags |= SMALL; |
| 1862 | if (spec.field_width == -1) { |
Grant Likely | 725fe00 | 2012-05-31 16:26:08 -0700 | [diff] [blame] | 1863 | spec.field_width = default_width; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1864 | spec.flags |= ZEROPAD; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 1865 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1866 | spec.base = 16; |
| 1867 | |
| 1868 | return number(buf, end, (unsigned long) ptr, spec); |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * Helper function to decode printf style format. |
| 1873 | * Each call decode a token from the format and return the |
| 1874 | * number of characters read (or likely the delta where it wants |
| 1875 | * to go on the next call). |
| 1876 | * The decoded token is returned through the parameters |
| 1877 | * |
| 1878 | * 'h', 'l', or 'L' for integer fields |
| 1879 | * 'z' support added 23/7/1999 S.H. |
| 1880 | * 'z' changed to 'Z' --davidm 1/25/99 |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1881 | * 'Z' changed to 'z' --adobriyan 2017-01-25 |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1882 | * 't' added for ptrdiff_t |
| 1883 | * |
| 1884 | * @fmt: the format string |
| 1885 | * @type of the token returned |
| 1886 | * @flags: various flags such as +, -, # tokens.. |
| 1887 | * @field_width: overwritten width |
| 1888 | * @base: base of the number (octal, hex, ...) |
| 1889 | * @precision: precision of a number |
| 1890 | * @qualifier: qualifier of a number (long, size_t, ...) |
| 1891 | */ |
Joe Perches | cf3b429 | 2010-05-24 14:33:16 -0700 | [diff] [blame] | 1892 | static noinline_for_stack |
| 1893 | int format_decode(const char *fmt, struct printf_spec *spec) |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1894 | { |
| 1895 | const char *start = fmt; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 1896 | char qualifier; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1897 | |
| 1898 | /* we finished early by reading the field width */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1899 | if (spec->type == FORMAT_TYPE_WIDTH) { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1900 | if (spec->field_width < 0) { |
| 1901 | spec->field_width = -spec->field_width; |
| 1902 | spec->flags |= LEFT; |
| 1903 | } |
| 1904 | spec->type = FORMAT_TYPE_NONE; |
| 1905 | goto precision; |
| 1906 | } |
| 1907 | |
| 1908 | /* we finished early by reading the precision */ |
| 1909 | if (spec->type == FORMAT_TYPE_PRECISION) { |
| 1910 | if (spec->precision < 0) |
| 1911 | spec->precision = 0; |
| 1912 | |
| 1913 | spec->type = FORMAT_TYPE_NONE; |
| 1914 | goto qualifier; |
| 1915 | } |
| 1916 | |
| 1917 | /* By default */ |
| 1918 | spec->type = FORMAT_TYPE_NONE; |
| 1919 | |
| 1920 | for (; *fmt ; ++fmt) { |
| 1921 | if (*fmt == '%') |
| 1922 | break; |
| 1923 | } |
| 1924 | |
| 1925 | /* Return the current non-format string */ |
| 1926 | if (fmt != start || !*fmt) |
| 1927 | return fmt - start; |
| 1928 | |
| 1929 | /* Process flags */ |
| 1930 | spec->flags = 0; |
| 1931 | |
| 1932 | while (1) { /* this also skips first '%' */ |
| 1933 | bool found = true; |
| 1934 | |
| 1935 | ++fmt; |
| 1936 | |
| 1937 | switch (*fmt) { |
| 1938 | case '-': spec->flags |= LEFT; break; |
| 1939 | case '+': spec->flags |= PLUS; break; |
| 1940 | case ' ': spec->flags |= SPACE; break; |
| 1941 | case '#': spec->flags |= SPECIAL; break; |
| 1942 | case '0': spec->flags |= ZEROPAD; break; |
| 1943 | default: found = false; |
| 1944 | } |
| 1945 | |
| 1946 | if (!found) |
| 1947 | break; |
| 1948 | } |
| 1949 | |
| 1950 | /* get field width */ |
| 1951 | spec->field_width = -1; |
| 1952 | |
| 1953 | if (isdigit(*fmt)) |
| 1954 | spec->field_width = skip_atoi(&fmt); |
| 1955 | else if (*fmt == '*') { |
| 1956 | /* it's the next argument */ |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1957 | spec->type = FORMAT_TYPE_WIDTH; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1958 | return ++fmt - start; |
| 1959 | } |
| 1960 | |
| 1961 | precision: |
| 1962 | /* get the precision */ |
| 1963 | spec->precision = -1; |
| 1964 | if (*fmt == '.') { |
| 1965 | ++fmt; |
| 1966 | if (isdigit(*fmt)) { |
| 1967 | spec->precision = skip_atoi(&fmt); |
| 1968 | if (spec->precision < 0) |
| 1969 | spec->precision = 0; |
| 1970 | } else if (*fmt == '*') { |
| 1971 | /* it's the next argument */ |
Vegard Nossum | adf26f8 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 1972 | spec->type = FORMAT_TYPE_PRECISION; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1973 | return ++fmt - start; |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | qualifier: |
| 1978 | /* get the conversion qualifier */ |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 1979 | qualifier = 0; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 1980 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 1981 | *fmt == 'z' || *fmt == 't') { |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 1982 | qualifier = *fmt++; |
| 1983 | if (unlikely(qualifier == *fmt)) { |
| 1984 | if (qualifier == 'l') { |
| 1985 | qualifier = 'L'; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1986 | ++fmt; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 1987 | } else if (qualifier == 'h') { |
| 1988 | qualifier = 'H'; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 1989 | ++fmt; |
| 1990 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | /* default base */ |
| 1995 | spec->base = 10; |
| 1996 | switch (*fmt) { |
| 1997 | case 'c': |
| 1998 | spec->type = FORMAT_TYPE_CHAR; |
| 1999 | return ++fmt - start; |
| 2000 | |
| 2001 | case 's': |
| 2002 | spec->type = FORMAT_TYPE_STR; |
| 2003 | return ++fmt - start; |
| 2004 | |
| 2005 | case 'p': |
| 2006 | spec->type = FORMAT_TYPE_PTR; |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 2007 | return ++fmt - start; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2008 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2009 | case '%': |
| 2010 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
| 2011 | return ++fmt - start; |
| 2012 | |
| 2013 | /* integer number formats - set up the flags and "break" */ |
| 2014 | case 'o': |
| 2015 | spec->base = 8; |
| 2016 | break; |
| 2017 | |
| 2018 | case 'x': |
| 2019 | spec->flags |= SMALL; |
| 2020 | |
| 2021 | case 'X': |
| 2022 | spec->base = 16; |
| 2023 | break; |
| 2024 | |
| 2025 | case 'd': |
| 2026 | case 'i': |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 2027 | spec->flags |= SIGN; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2028 | case 'u': |
| 2029 | break; |
| 2030 | |
Ryan Mallon | 708d96fd | 2014-04-03 14:48:37 -0700 | [diff] [blame] | 2031 | case 'n': |
| 2032 | /* |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2033 | * Since %n poses a greater security risk than |
| 2034 | * utility, treat it as any other invalid or |
| 2035 | * unsupported format specifier. |
Ryan Mallon | 708d96fd | 2014-04-03 14:48:37 -0700 | [diff] [blame] | 2036 | */ |
Ryan Mallon | 708d96fd | 2014-04-03 14:48:37 -0700 | [diff] [blame] | 2037 | /* Fall-through */ |
| 2038 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2039 | default: |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2040 | WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2041 | spec->type = FORMAT_TYPE_INVALID; |
| 2042 | return fmt - start; |
| 2043 | } |
| 2044 | |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2045 | if (qualifier == 'L') |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2046 | spec->type = FORMAT_TYPE_LONG_LONG; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2047 | else if (qualifier == 'l') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2048 | BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); |
| 2049 | spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 2050 | } else if (qualifier == 'z') { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2051 | spec->type = FORMAT_TYPE_SIZE_T; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2052 | } else if (qualifier == 't') { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2053 | spec->type = FORMAT_TYPE_PTRDIFF; |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2054 | } else if (qualifier == 'H') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2055 | BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); |
| 2056 | spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); |
Rasmus Villemoes | d048419 | 2016-01-15 16:58:37 -0800 | [diff] [blame] | 2057 | } else if (qualifier == 'h') { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2058 | BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); |
| 2059 | spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2060 | } else { |
Rasmus Villemoes | 51be17d | 2015-04-15 16:17:02 -0700 | [diff] [blame] | 2061 | BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); |
| 2062 | spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | return ++fmt - start; |
Linus Torvalds | 78a8bf6 | 2008-07-06 16:16:15 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2068 | static void |
| 2069 | set_field_width(struct printf_spec *spec, int width) |
| 2070 | { |
| 2071 | spec->field_width = width; |
| 2072 | if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { |
| 2073 | spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | static void |
| 2078 | set_precision(struct printf_spec *spec, int prec) |
| 2079 | { |
| 2080 | spec->precision = prec; |
| 2081 | if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { |
| 2082 | spec->precision = clamp(prec, 0, PRECISION_MAX); |
| 2083 | } |
| 2084 | } |
| 2085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | /** |
| 2087 | * vsnprintf - Format a string and place it in a buffer |
| 2088 | * @buf: The buffer to place the result into |
| 2089 | * @size: The size of the buffer, including the trailing null space |
| 2090 | * @fmt: The format string to use |
| 2091 | * @args: Arguments for the format string |
| 2092 | * |
Rasmus Villemoes | d7ec9a0 | 2015-11-06 16:30:35 -0800 | [diff] [blame] | 2093 | * This function generally follows C99 vsnprintf, but has some |
| 2094 | * extensions and a few limitations: |
| 2095 | * |
mchehab@s-opensource.com | 6cc8913 | 2017-03-30 17:11:33 -0300 | [diff] [blame] | 2096 | * - ``%n`` is unsupported |
| 2097 | * - ``%p*`` is handled by pointer() |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2098 | * |
Martin Kletzander | 5e4ee7b | 2015-11-06 16:30:17 -0800 | [diff] [blame] | 2099 | * See pointer() or Documentation/printk-formats.txt for more |
| 2100 | * extensive description. |
| 2101 | * |
mchehab@s-opensource.com | 6cc8913 | 2017-03-30 17:11:33 -0300 | [diff] [blame] | 2102 | * **Please update the documentation in both places when making changes** |
Andrew Morton | 80f548e | 2012-07-30 14:40:25 -0700 | [diff] [blame] | 2103 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | * The return value is the number of characters which would |
| 2105 | * be generated for the given input, excluding the trailing |
| 2106 | * '\0', as per ISO C99. If you want to have the exact |
| 2107 | * number of characters written into @buf as return value |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2108 | * (not including the trailing '\0'), use vscnprintf(). If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | * return is greater than or equal to @size, the resulting |
| 2110 | * string is truncated. |
| 2111 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2112 | * If you're not already dealing with a va_list consider using snprintf(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | */ |
| 2114 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 2115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | unsigned long long num; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2117 | char *str, *end; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2118 | struct printf_spec spec = {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2120 | /* Reject out-of-range values early. Large positive sizes are |
| 2121 | used for unknown buffer sizes. */ |
Rasmus Villemoes | 2aa2f9e | 2015-02-12 15:01:39 -0800 | [diff] [blame] | 2122 | if (WARN_ON_ONCE(size > INT_MAX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
| 2125 | str = buf; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2126 | end = buf + size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2128 | /* Make sure end is always >= buf */ |
| 2129 | if (end < buf) { |
| 2130 | end = ((void *)-1); |
| 2131 | size = end - buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2134 | while (*fmt) { |
| 2135 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2136 | int read = format_decode(fmt, &spec); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2137 | |
| 2138 | fmt += read; |
| 2139 | |
| 2140 | switch (spec.type) { |
| 2141 | case FORMAT_TYPE_NONE: { |
| 2142 | int copy = read; |
| 2143 | if (str < end) { |
| 2144 | if (copy > end - str) |
| 2145 | copy = end - str; |
| 2146 | memcpy(str, old_fmt, copy); |
| 2147 | } |
| 2148 | str += read; |
| 2149 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2152 | case FORMAT_TYPE_WIDTH: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2153 | set_field_width(&spec, va_arg(args, int)); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2154 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2156 | case FORMAT_TYPE_PRECISION: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2157 | set_precision(&spec, va_arg(args, int)); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2158 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2160 | case FORMAT_TYPE_CHAR: { |
| 2161 | char c; |
| 2162 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2163 | if (!(spec.flags & LEFT)) { |
| 2164 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2165 | if (str < end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | *str = ' '; |
| 2167 | ++str; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2170 | } |
| 2171 | c = (unsigned char) va_arg(args, int); |
| 2172 | if (str < end) |
| 2173 | *str = c; |
| 2174 | ++str; |
| 2175 | while (--spec.field_width > 0) { |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2176 | if (str < end) |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2177 | *str = ' '; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | ++str; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2179 | } |
| 2180 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2183 | case FORMAT_TYPE_STR: |
| 2184 | str = string(str, end, va_arg(args, char *), spec); |
| 2185 | break; |
| 2186 | |
| 2187 | case FORMAT_TYPE_PTR: |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 2188 | str = pointer(fmt, str, end, va_arg(args, void *), |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2189 | spec); |
| 2190 | while (isalnum(*fmt)) |
| 2191 | fmt++; |
| 2192 | break; |
| 2193 | |
| 2194 | case FORMAT_TYPE_PERCENT_CHAR: |
| 2195 | if (str < end) |
| 2196 | *str = '%'; |
| 2197 | ++str; |
| 2198 | break; |
| 2199 | |
| 2200 | case FORMAT_TYPE_INVALID: |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2201 | /* |
| 2202 | * Presumably the arguments passed gcc's type |
| 2203 | * checking, but there is no safe or sane way |
| 2204 | * for us to continue parsing the format and |
| 2205 | * fetching from the va_list; the remaining |
| 2206 | * specifiers and arguments would be out of |
| 2207 | * sync. |
| 2208 | */ |
| 2209 | goto out; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2210 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2211 | default: |
| 2212 | switch (spec.type) { |
| 2213 | case FORMAT_TYPE_LONG_LONG: |
| 2214 | num = va_arg(args, long long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | break; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2216 | case FORMAT_TYPE_ULONG: |
| 2217 | num = va_arg(args, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | break; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2219 | case FORMAT_TYPE_LONG: |
| 2220 | num = va_arg(args, long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | break; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2222 | case FORMAT_TYPE_SIZE_T: |
Jason Gunthorpe | ef12496 | 2012-12-17 15:59:58 -0800 | [diff] [blame] | 2223 | if (spec.flags & SIGN) |
| 2224 | num = va_arg(args, ssize_t); |
| 2225 | else |
| 2226 | num = va_arg(args, size_t); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2227 | break; |
| 2228 | case FORMAT_TYPE_PTRDIFF: |
| 2229 | num = va_arg(args, ptrdiff_t); |
| 2230 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2231 | case FORMAT_TYPE_UBYTE: |
| 2232 | num = (unsigned char) va_arg(args, int); |
| 2233 | break; |
| 2234 | case FORMAT_TYPE_BYTE: |
| 2235 | num = (signed char) va_arg(args, int); |
| 2236 | break; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2237 | case FORMAT_TYPE_USHORT: |
| 2238 | num = (unsigned short) va_arg(args, int); |
| 2239 | break; |
| 2240 | case FORMAT_TYPE_SHORT: |
| 2241 | num = (short) va_arg(args, int); |
| 2242 | break; |
Frederic Weisbecker | 39e874f | 2009-03-09 21:15:04 +0100 | [diff] [blame] | 2243 | case FORMAT_TYPE_INT: |
| 2244 | num = (int) va_arg(args, int); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2245 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | default: |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2247 | num = va_arg(args, unsigned int); |
| 2248 | } |
| 2249 | |
| 2250 | str = number(str, end, num, spec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2253 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2254 | out: |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2255 | if (size > 0) { |
| 2256 | if (str < end) |
| 2257 | *str = '\0'; |
| 2258 | else |
Linus Torvalds | 0a6047e | 2006-06-28 17:09:34 -0700 | [diff] [blame] | 2259 | end[-1] = '\0'; |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2260 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2261 | |
Jeremy Fitzhardinge | f796937 | 2006-06-25 05:49:17 -0700 | [diff] [blame] | 2262 | /* the trailing null byte doesn't count towards the total */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | return str-buf; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | EXPORT_SYMBOL(vsnprintf); |
| 2267 | |
| 2268 | /** |
| 2269 | * vscnprintf - Format a string and place it in a buffer |
| 2270 | * @buf: The buffer to place the result into |
| 2271 | * @size: The size of the buffer, including the trailing null space |
| 2272 | * @fmt: The format string to use |
| 2273 | * @args: Arguments for the format string |
| 2274 | * |
| 2275 | * The return value is the number of characters which have been written into |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2276 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | * returns 0. |
| 2278 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2279 | * If you're not already dealing with a va_list consider using scnprintf(). |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2280 | * |
| 2281 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | */ |
| 2283 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 2284 | { |
| 2285 | int i; |
| 2286 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2287 | i = vsnprintf(buf, size, fmt, args); |
| 2288 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2289 | if (likely(i < size)) |
| 2290 | return i; |
| 2291 | if (size != 0) |
| 2292 | return size - 1; |
| 2293 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | EXPORT_SYMBOL(vscnprintf); |
| 2296 | |
| 2297 | /** |
| 2298 | * snprintf - Format a string and place it in a buffer |
| 2299 | * @buf: The buffer to place the result into |
| 2300 | * @size: The size of the buffer, including the trailing null space |
| 2301 | * @fmt: The format string to use |
| 2302 | * @...: Arguments for the format string |
| 2303 | * |
| 2304 | * The return value is the number of characters which would be |
| 2305 | * generated for the given input, excluding the trailing null, |
| 2306 | * as per ISO C99. If the return is greater than or equal to |
| 2307 | * @size, the resulting string is truncated. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2308 | * |
| 2309 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2311 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | { |
| 2313 | va_list args; |
| 2314 | int i; |
| 2315 | |
| 2316 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2317 | i = vsnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | return i; |
| 2321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | EXPORT_SYMBOL(snprintf); |
| 2323 | |
| 2324 | /** |
| 2325 | * scnprintf - Format a string and place it in a buffer |
| 2326 | * @buf: The buffer to place the result into |
| 2327 | * @size: The size of the buffer, including the trailing null space |
| 2328 | * @fmt: The format string to use |
| 2329 | * @...: Arguments for the format string |
| 2330 | * |
| 2331 | * The return value is the number of characters written into @buf not including |
Changli Gao | b903c0b | 2010-10-26 14:22:50 -0700 | [diff] [blame] | 2332 | * the trailing '\0'. If @size is == 0 the function returns 0. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | */ |
| 2334 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2335 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | { |
| 2337 | va_list args; |
| 2338 | int i; |
| 2339 | |
| 2340 | va_start(args, fmt); |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2341 | i = vscnprintf(buf, size, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2343 | |
Anton Arapov | b921c69 | 2011-01-12 16:59:49 -0800 | [diff] [blame] | 2344 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | } |
| 2346 | EXPORT_SYMBOL(scnprintf); |
| 2347 | |
| 2348 | /** |
| 2349 | * vsprintf - Format a string and place it in a buffer |
| 2350 | * @buf: The buffer to place the result into |
| 2351 | * @fmt: The format string to use |
| 2352 | * @args: Arguments for the format string |
| 2353 | * |
| 2354 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2355 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | * buffer overflows. |
| 2357 | * |
Uwe Kleine-König | ba1835e | 2011-04-06 07:49:04 -0700 | [diff] [blame] | 2358 | * If you're not already dealing with a va_list consider using sprintf(). |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2359 | * |
| 2360 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | */ |
| 2362 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 2363 | { |
| 2364 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 2365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | EXPORT_SYMBOL(vsprintf); |
| 2367 | |
| 2368 | /** |
| 2369 | * sprintf - Format a string and place it in a buffer |
| 2370 | * @buf: The buffer to place the result into |
| 2371 | * @fmt: The format string to use |
| 2372 | * @...: Arguments for the format string |
| 2373 | * |
| 2374 | * The function returns the number of characters written |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 2375 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | * buffer overflows. |
Andi Kleen | 20036fd | 2008-10-15 22:02:02 -0700 | [diff] [blame] | 2377 | * |
| 2378 | * See the vsnprintf() documentation for format string extensions over C99. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2380 | int sprintf(char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | { |
| 2382 | va_list args; |
| 2383 | int i; |
| 2384 | |
| 2385 | va_start(args, fmt); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2386 | i = vsnprintf(buf, INT_MAX, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | return i; |
| 2390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | EXPORT_SYMBOL(sprintf); |
| 2392 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2393 | #ifdef CONFIG_BINARY_PRINTF |
| 2394 | /* |
| 2395 | * bprintf service: |
| 2396 | * vbin_printf() - VA arguments to binary data |
| 2397 | * bstr_printf() - Binary data to text string |
| 2398 | */ |
| 2399 | |
| 2400 | /** |
| 2401 | * vbin_printf - Parse a format string and place args' binary value in a buffer |
| 2402 | * @bin_buf: The buffer to place args' binary value |
| 2403 | * @size: The size of the buffer(by words(32bits), not characters) |
| 2404 | * @fmt: The format string to use |
| 2405 | * @args: Arguments for the format string |
| 2406 | * |
| 2407 | * The format follows C99 vsnprintf, except %n is ignored, and its argument |
Masanari Iida | da3dae5 | 2014-09-09 01:27:23 +0900 | [diff] [blame] | 2408 | * is skipped. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2409 | * |
| 2410 | * The return value is the number of words(32bits) which would be generated for |
| 2411 | * the given input. |
| 2412 | * |
| 2413 | * NOTE: |
| 2414 | * If the return value is greater than @size, the resulting bin_buf is NOT |
| 2415 | * valid for bstr_printf(). |
| 2416 | */ |
| 2417 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) |
| 2418 | { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2419 | struct printf_spec spec = {0}; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2420 | char *str, *end; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2421 | |
| 2422 | str = (char *)bin_buf; |
| 2423 | end = (char *)(bin_buf + size); |
| 2424 | |
| 2425 | #define save_arg(type) \ |
| 2426 | do { \ |
| 2427 | if (sizeof(type) == 8) { \ |
| 2428 | unsigned long long value; \ |
| 2429 | str = PTR_ALIGN(str, sizeof(u32)); \ |
| 2430 | value = va_arg(args, unsigned long long); \ |
| 2431 | if (str + sizeof(type) <= end) { \ |
| 2432 | *(u32 *)str = *(u32 *)&value; \ |
| 2433 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ |
| 2434 | } \ |
| 2435 | } else { \ |
| 2436 | unsigned long value; \ |
| 2437 | str = PTR_ALIGN(str, sizeof(type)); \ |
| 2438 | value = va_arg(args, int); \ |
| 2439 | if (str + sizeof(type) <= end) \ |
| 2440 | *(typeof(type) *)str = (type)value; \ |
| 2441 | } \ |
| 2442 | str += sizeof(type); \ |
| 2443 | } while (0) |
| 2444 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2445 | while (*fmt) { |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2446 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2447 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2448 | fmt += read; |
| 2449 | |
| 2450 | switch (spec.type) { |
| 2451 | case FORMAT_TYPE_NONE: |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2452 | case FORMAT_TYPE_PERCENT_CHAR: |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2453 | break; |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2454 | case FORMAT_TYPE_INVALID: |
| 2455 | goto out; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2456 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2457 | case FORMAT_TYPE_WIDTH: |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2458 | case FORMAT_TYPE_PRECISION: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2459 | save_arg(int); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2460 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2461 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2462 | case FORMAT_TYPE_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2463 | save_arg(char); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2464 | break; |
| 2465 | |
| 2466 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2467 | const char *save_str = va_arg(args, char *); |
| 2468 | size_t len; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 2469 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2470 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
| 2471 | || (unsigned long)save_str < PAGE_SIZE) |
André Goddard Rosa | 0f4f81d | 2009-12-14 18:00:55 -0800 | [diff] [blame] | 2472 | save_str = "(null)"; |
André Goddard Rosa | 6c35663 | 2009-12-14 18:00:56 -0800 | [diff] [blame] | 2473 | len = strlen(save_str) + 1; |
| 2474 | if (str + len < end) |
| 2475 | memcpy(str, save_str, len); |
| 2476 | str += len; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2477 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2478 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2479 | |
| 2480 | case FORMAT_TYPE_PTR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2481 | save_arg(void *); |
| 2482 | /* skip all alphanumeric pointer suffixes */ |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2483 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2484 | fmt++; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2485 | break; |
| 2486 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2487 | default: |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2488 | switch (spec.type) { |
| 2489 | |
| 2490 | case FORMAT_TYPE_LONG_LONG: |
| 2491 | save_arg(long long); |
| 2492 | break; |
| 2493 | case FORMAT_TYPE_ULONG: |
| 2494 | case FORMAT_TYPE_LONG: |
| 2495 | save_arg(unsigned long); |
| 2496 | break; |
| 2497 | case FORMAT_TYPE_SIZE_T: |
| 2498 | save_arg(size_t); |
| 2499 | break; |
| 2500 | case FORMAT_TYPE_PTRDIFF: |
| 2501 | save_arg(ptrdiff_t); |
| 2502 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2503 | case FORMAT_TYPE_UBYTE: |
| 2504 | case FORMAT_TYPE_BYTE: |
| 2505 | save_arg(char); |
| 2506 | break; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2507 | case FORMAT_TYPE_USHORT: |
| 2508 | case FORMAT_TYPE_SHORT: |
| 2509 | save_arg(short); |
| 2510 | break; |
| 2511 | default: |
| 2512 | save_arg(int); |
| 2513 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2514 | } |
| 2515 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2516 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2517 | out: |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2518 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2519 | #undef save_arg |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2520 | } |
| 2521 | EXPORT_SYMBOL_GPL(vbin_printf); |
| 2522 | |
| 2523 | /** |
| 2524 | * bstr_printf - Format a string from binary arguments and place it in a buffer |
| 2525 | * @buf: The buffer to place the result into |
| 2526 | * @size: The size of the buffer, including the trailing null space |
| 2527 | * @fmt: The format string to use |
| 2528 | * @bin_buf: Binary arguments for the format string |
| 2529 | * |
| 2530 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets |
| 2531 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is |
| 2532 | * a binary buffer that generated by vbin_printf. |
| 2533 | * |
| 2534 | * The format follows C99 vsnprintf, but has some extensions: |
Steven Rostedt | 0efb4d2 | 2009-09-17 09:27:29 -0400 | [diff] [blame] | 2535 | * see vsnprintf comment for details. |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2536 | * |
| 2537 | * The return value is the number of characters which would |
| 2538 | * be generated for the given input, excluding the trailing |
| 2539 | * '\0', as per ISO C99. If you want to have the exact |
| 2540 | * number of characters written into @buf as return value |
| 2541 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 2542 | * return is greater than or equal to @size, the resulting |
| 2543 | * string is truncated. |
| 2544 | */ |
| 2545 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
| 2546 | { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2547 | struct printf_spec spec = {0}; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2548 | char *str, *end; |
| 2549 | const char *args = (const char *)bin_buf; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2550 | |
Rasmus Villemoes | 762abb5 | 2015-11-06 16:30:23 -0800 | [diff] [blame] | 2551 | if (WARN_ON_ONCE(size > INT_MAX)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2552 | return 0; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2553 | |
| 2554 | str = buf; |
| 2555 | end = buf + size; |
| 2556 | |
| 2557 | #define get_arg(type) \ |
| 2558 | ({ \ |
| 2559 | typeof(type) value; \ |
| 2560 | if (sizeof(type) == 8) { \ |
| 2561 | args = PTR_ALIGN(args, sizeof(u32)); \ |
| 2562 | *(u32 *)&value = *(u32 *)args; \ |
| 2563 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ |
| 2564 | } else { \ |
| 2565 | args = PTR_ALIGN(args, sizeof(type)); \ |
| 2566 | value = *(typeof(type) *)args; \ |
| 2567 | } \ |
| 2568 | args += sizeof(type); \ |
| 2569 | value; \ |
| 2570 | }) |
| 2571 | |
| 2572 | /* Make sure end is always >= buf */ |
| 2573 | if (end < buf) { |
| 2574 | end = ((void *)-1); |
| 2575 | size = end - buf; |
| 2576 | } |
| 2577 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2578 | while (*fmt) { |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2579 | const char *old_fmt = fmt; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2580 | int read = format_decode(fmt, &spec); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2581 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2582 | fmt += read; |
| 2583 | |
| 2584 | switch (spec.type) { |
| 2585 | case FORMAT_TYPE_NONE: { |
| 2586 | int copy = read; |
| 2587 | if (str < end) { |
| 2588 | if (copy > end - str) |
| 2589 | copy = end - str; |
| 2590 | memcpy(str, old_fmt, copy); |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2591 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2592 | str += read; |
| 2593 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2594 | } |
| 2595 | |
Vegard Nossum | ed681a9 | 2009-03-14 12:08:50 +0100 | [diff] [blame] | 2596 | case FORMAT_TYPE_WIDTH: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2597 | set_field_width(&spec, get_arg(int)); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2598 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2599 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2600 | case FORMAT_TYPE_PRECISION: |
Rasmus Villemoes | 4d72ba0 | 2016-01-15 16:58:44 -0800 | [diff] [blame] | 2601 | set_precision(&spec, get_arg(int)); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2602 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2603 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2604 | case FORMAT_TYPE_CHAR: { |
| 2605 | char c; |
| 2606 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2607 | if (!(spec.flags & LEFT)) { |
| 2608 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2609 | if (str < end) |
| 2610 | *str = ' '; |
| 2611 | ++str; |
| 2612 | } |
| 2613 | } |
| 2614 | c = (unsigned char) get_arg(char); |
| 2615 | if (str < end) |
| 2616 | *str = c; |
| 2617 | ++str; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2618 | while (--spec.field_width > 0) { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2619 | if (str < end) |
| 2620 | *str = ' '; |
| 2621 | ++str; |
| 2622 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2623 | break; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2624 | } |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2625 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2626 | case FORMAT_TYPE_STR: { |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2627 | const char *str_arg = args; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2628 | args += strlen(str_arg) + 1; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2629 | str = string(str, end, (char *)str_arg, spec); |
| 2630 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2633 | case FORMAT_TYPE_PTR: |
Rasmus Villemoes | ffbfed0 | 2015-02-12 15:01:37 -0800 | [diff] [blame] | 2634 | str = pointer(fmt, str, end, get_arg(void *), spec); |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2635 | while (isalnum(*fmt)) |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2636 | fmt++; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2637 | break; |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2638 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2639 | case FORMAT_TYPE_PERCENT_CHAR: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2640 | if (str < end) |
| 2641 | *str = '%'; |
| 2642 | ++str; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2643 | break; |
| 2644 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2645 | case FORMAT_TYPE_INVALID: |
| 2646 | goto out; |
| 2647 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2648 | default: { |
| 2649 | unsigned long long num; |
| 2650 | |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2651 | switch (spec.type) { |
| 2652 | |
| 2653 | case FORMAT_TYPE_LONG_LONG: |
| 2654 | num = get_arg(long long); |
| 2655 | break; |
| 2656 | case FORMAT_TYPE_ULONG: |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2657 | case FORMAT_TYPE_LONG: |
| 2658 | num = get_arg(unsigned long); |
| 2659 | break; |
| 2660 | case FORMAT_TYPE_SIZE_T: |
| 2661 | num = get_arg(size_t); |
| 2662 | break; |
| 2663 | case FORMAT_TYPE_PTRDIFF: |
| 2664 | num = get_arg(ptrdiff_t); |
| 2665 | break; |
Zhaolei | a4e94ef | 2009-03-27 17:07:05 +0800 | [diff] [blame] | 2666 | case FORMAT_TYPE_UBYTE: |
| 2667 | num = get_arg(unsigned char); |
| 2668 | break; |
| 2669 | case FORMAT_TYPE_BYTE: |
| 2670 | num = get_arg(signed char); |
| 2671 | break; |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2672 | case FORMAT_TYPE_USHORT: |
| 2673 | num = get_arg(unsigned short); |
| 2674 | break; |
| 2675 | case FORMAT_TYPE_SHORT: |
| 2676 | num = get_arg(short); |
| 2677 | break; |
| 2678 | case FORMAT_TYPE_UINT: |
| 2679 | num = get_arg(unsigned int); |
| 2680 | break; |
| 2681 | default: |
| 2682 | num = get_arg(int); |
| 2683 | } |
| 2684 | |
| 2685 | str = number(str, end, num, spec); |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2686 | } /* default: */ |
| 2687 | } /* switch(spec.type) */ |
| 2688 | } /* while(*fmt) */ |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2689 | |
Rasmus Villemoes | b006f19 | 2015-11-06 16:30:20 -0800 | [diff] [blame] | 2690 | out: |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2691 | if (size > 0) { |
| 2692 | if (str < end) |
| 2693 | *str = '\0'; |
| 2694 | else |
| 2695 | end[-1] = '\0'; |
| 2696 | } |
Frederic Weisbecker | fef20d9c | 2009-03-06 17:21:50 +0100 | [diff] [blame] | 2697 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2698 | #undef get_arg |
| 2699 | |
| 2700 | /* the trailing null byte doesn't count towards the total */ |
| 2701 | return str - buf; |
| 2702 | } |
| 2703 | EXPORT_SYMBOL_GPL(bstr_printf); |
| 2704 | |
| 2705 | /** |
| 2706 | * bprintf - Parse a format string and place args' binary value in a buffer |
| 2707 | * @bin_buf: The buffer to place args' binary value |
| 2708 | * @size: The size of the buffer(by words(32bits), not characters) |
| 2709 | * @fmt: The format string to use |
| 2710 | * @...: Arguments for the format string |
| 2711 | * |
| 2712 | * The function returns the number of words(u32) written |
| 2713 | * into @bin_buf. |
| 2714 | */ |
| 2715 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) |
| 2716 | { |
| 2717 | va_list args; |
| 2718 | int ret; |
| 2719 | |
| 2720 | va_start(args, fmt); |
| 2721 | ret = vbin_printf(bin_buf, size, fmt, args); |
| 2722 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2723 | |
Lai Jiangshan | 4370aa4 | 2009-03-06 17:21:46 +0100 | [diff] [blame] | 2724 | return ret; |
| 2725 | } |
| 2726 | EXPORT_SYMBOL_GPL(bprintf); |
| 2727 | |
| 2728 | #endif /* CONFIG_BINARY_PRINTF */ |
| 2729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | /** |
| 2731 | * vsscanf - Unformat a buffer into a list of arguments |
| 2732 | * @buf: input buffer |
| 2733 | * @fmt: format of buffer |
| 2734 | * @args: arguments |
| 2735 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2736 | int vsscanf(const char *buf, const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | { |
| 2738 | const char *str = buf; |
| 2739 | char *next; |
| 2740 | char digit; |
| 2741 | int num = 0; |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 2742 | u8 qualifier; |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2743 | unsigned int base; |
| 2744 | union { |
| 2745 | long long s; |
| 2746 | unsigned long long u; |
| 2747 | } val; |
Joe Perches | ef0658f | 2010-03-06 17:10:14 -0800 | [diff] [blame] | 2748 | s16 field_width; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2749 | bool is_sign; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 2751 | while (*fmt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | /* skip any white space in format */ |
| 2753 | /* white space in format matchs any amount of |
| 2754 | * white space, including none, in the input. |
| 2755 | */ |
| 2756 | if (isspace(*fmt)) { |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2757 | fmt = skip_spaces(++fmt); |
| 2758 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
| 2761 | /* anything that is not a conversion must match exactly */ |
| 2762 | if (*fmt != '%' && *fmt) { |
| 2763 | if (*fmt++ != *str++) |
| 2764 | break; |
| 2765 | continue; |
| 2766 | } |
| 2767 | |
| 2768 | if (!*fmt) |
| 2769 | break; |
| 2770 | ++fmt; |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | /* skip this conversion. |
| 2773 | * advance both strings to next white space |
| 2774 | */ |
| 2775 | if (*fmt == '*') { |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 2776 | if (!*str) |
| 2777 | break; |
Jessica Yu | f9310b2 | 2016-03-17 14:23:07 -0700 | [diff] [blame] | 2778 | while (!isspace(*fmt) && *fmt != '%' && *fmt) { |
| 2779 | /* '%*[' not yet supported, invalid format */ |
| 2780 | if (*fmt == '[') |
| 2781 | return num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | fmt++; |
Jessica Yu | f9310b2 | 2016-03-17 14:23:07 -0700 | [diff] [blame] | 2783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | while (!isspace(*str) && *str) |
| 2785 | str++; |
| 2786 | continue; |
| 2787 | } |
| 2788 | |
| 2789 | /* get field width */ |
| 2790 | field_width = -1; |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2791 | if (isdigit(*fmt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | field_width = skip_atoi(&fmt); |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2793 | if (field_width <= 0) |
| 2794 | break; |
| 2795 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | |
| 2797 | /* get conversion qualifier */ |
| 2798 | qualifier = -1; |
Andy Shevchenko | 75fb8f2 | 2011-07-25 17:13:20 -0700 | [diff] [blame] | 2799 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 2800 | *fmt == 'z') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | qualifier = *fmt++; |
| 2802 | if (unlikely(qualifier == *fmt)) { |
| 2803 | if (qualifier == 'h') { |
| 2804 | qualifier = 'H'; |
| 2805 | fmt++; |
| 2806 | } else if (qualifier == 'l') { |
| 2807 | qualifier = 'L'; |
| 2808 | fmt++; |
| 2809 | } |
| 2810 | } |
| 2811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | |
Jan Beulich | da99075 | 2012-10-04 17:13:24 -0700 | [diff] [blame] | 2813 | if (!*fmt) |
| 2814 | break; |
| 2815 | |
| 2816 | if (*fmt == 'n') { |
| 2817 | /* return number of characters read so far */ |
| 2818 | *va_arg(args, int *) = str - buf; |
| 2819 | ++fmt; |
| 2820 | continue; |
| 2821 | } |
| 2822 | |
| 2823 | if (!*str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2826 | base = 10; |
Fabian Frederick | 3f623eb | 2014-06-04 16:11:52 -0700 | [diff] [blame] | 2827 | is_sign = false; |
André Goddard Rosa | d4be151 | 2009-12-14 18:00:59 -0800 | [diff] [blame] | 2828 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2829 | switch (*fmt++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | case 'c': |
| 2831 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2832 | char *s = (char *)va_arg(args, char*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | if (field_width == -1) |
| 2834 | field_width = 1; |
| 2835 | do { |
| 2836 | *s++ = *str++; |
| 2837 | } while (--field_width > 0 && *str); |
| 2838 | num++; |
| 2839 | } |
| 2840 | continue; |
| 2841 | case 's': |
| 2842 | { |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2843 | char *s = (char *)va_arg(args, char *); |
| 2844 | if (field_width == -1) |
Alexey Dobriyan | 4be929b | 2010-05-24 14:33:03 -0700 | [diff] [blame] | 2845 | field_width = SHRT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | /* first, skip leading white space in buffer */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2847 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | |
| 2849 | /* now copy until next white space */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2850 | while (*str && !isspace(*str) && field_width--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | *s++ = *str++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | *s = '\0'; |
| 2853 | num++; |
| 2854 | } |
| 2855 | continue; |
Jessica Yu | f9310b2 | 2016-03-17 14:23:07 -0700 | [diff] [blame] | 2856 | /* |
| 2857 | * Warning: This implementation of the '[' conversion specifier |
| 2858 | * deviates from its glibc counterpart in the following ways: |
| 2859 | * (1) It does NOT support ranges i.e. '-' is NOT a special |
| 2860 | * character |
| 2861 | * (2) It cannot match the closing bracket ']' itself |
| 2862 | * (3) A field width is required |
| 2863 | * (4) '%*[' (discard matching input) is currently not supported |
| 2864 | * |
| 2865 | * Example usage: |
| 2866 | * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", |
| 2867 | * buf1, buf2, buf3); |
| 2868 | * if (ret < 3) |
| 2869 | * // etc.. |
| 2870 | */ |
| 2871 | case '[': |
| 2872 | { |
| 2873 | char *s = (char *)va_arg(args, char *); |
| 2874 | DECLARE_BITMAP(set, 256) = {0}; |
| 2875 | unsigned int len = 0; |
| 2876 | bool negate = (*fmt == '^'); |
| 2877 | |
| 2878 | /* field width is required */ |
| 2879 | if (field_width == -1) |
| 2880 | return num; |
| 2881 | |
| 2882 | if (negate) |
| 2883 | ++fmt; |
| 2884 | |
| 2885 | for ( ; *fmt && *fmt != ']'; ++fmt, ++len) |
| 2886 | set_bit((u8)*fmt, set); |
| 2887 | |
| 2888 | /* no ']' or no character set found */ |
| 2889 | if (!*fmt || !len) |
| 2890 | return num; |
| 2891 | ++fmt; |
| 2892 | |
| 2893 | if (negate) { |
| 2894 | bitmap_complement(set, set, 256); |
| 2895 | /* exclude null '\0' byte */ |
| 2896 | clear_bit(0, set); |
| 2897 | } |
| 2898 | |
| 2899 | /* match must be non-empty */ |
| 2900 | if (!test_bit((u8)*str, set)) |
| 2901 | return num; |
| 2902 | |
| 2903 | while (test_bit((u8)*str, set) && field_width--) |
| 2904 | *s++ = *str++; |
| 2905 | *s = '\0'; |
| 2906 | ++num; |
| 2907 | } |
| 2908 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | case 'o': |
| 2910 | base = 8; |
| 2911 | break; |
| 2912 | case 'x': |
| 2913 | case 'X': |
| 2914 | base = 16; |
| 2915 | break; |
| 2916 | case 'i': |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2917 | base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | case 'd': |
Fabian Frederick | 3f623eb | 2014-06-04 16:11:52 -0700 | [diff] [blame] | 2919 | is_sign = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | case 'u': |
| 2921 | break; |
| 2922 | case '%': |
| 2923 | /* looking for '%' in str */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2924 | if (*str++ != '%') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | return num; |
| 2926 | continue; |
| 2927 | default: |
| 2928 | /* invalid format; stop here */ |
| 2929 | return num; |
| 2930 | } |
| 2931 | |
| 2932 | /* have some sort of integer conversion. |
| 2933 | * first, skip white space in buffer. |
| 2934 | */ |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 2935 | str = skip_spaces(str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | |
| 2937 | digit = *str; |
| 2938 | if (is_sign && digit == '-') |
| 2939 | digit = *(str + 1); |
| 2940 | |
| 2941 | if (!digit |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2942 | || (base == 16 && !isxdigit(digit)) |
| 2943 | || (base == 10 && !isdigit(digit)) |
| 2944 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 2945 | || (base == 0 && !isdigit(digit))) |
| 2946 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2948 | if (is_sign) |
| 2949 | val.s = qualifier != 'L' ? |
| 2950 | simple_strtol(str, &next, base) : |
| 2951 | simple_strtoll(str, &next, base); |
| 2952 | else |
| 2953 | val.u = qualifier != 'L' ? |
| 2954 | simple_strtoul(str, &next, base) : |
| 2955 | simple_strtoull(str, &next, base); |
| 2956 | |
| 2957 | if (field_width > 0 && next - str > field_width) { |
| 2958 | if (base == 0) |
| 2959 | _parse_integer_fixup_radix(str, &base); |
| 2960 | while (next - str > field_width) { |
| 2961 | if (is_sign) |
| 2962 | val.s = div_s64(val.s, base); |
| 2963 | else |
| 2964 | val.u = div_u64(val.u, base); |
| 2965 | --next; |
| 2966 | } |
| 2967 | } |
| 2968 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 2969 | switch (qualifier) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | case 'H': /* that's 'hh' in format */ |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2971 | if (is_sign) |
| 2972 | *va_arg(args, signed char *) = val.s; |
| 2973 | else |
| 2974 | *va_arg(args, unsigned char *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | break; |
| 2976 | case 'h': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2977 | if (is_sign) |
| 2978 | *va_arg(args, short *) = val.s; |
| 2979 | else |
| 2980 | *va_arg(args, unsigned short *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | break; |
| 2982 | case 'l': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2983 | if (is_sign) |
| 2984 | *va_arg(args, long *) = val.s; |
| 2985 | else |
| 2986 | *va_arg(args, unsigned long *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | break; |
| 2988 | case 'L': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2989 | if (is_sign) |
| 2990 | *va_arg(args, long long *) = val.s; |
| 2991 | else |
| 2992 | *va_arg(args, unsigned long long *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | case 'z': |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2995 | *va_arg(args, size_t *) = val.u; |
| 2996 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | default: |
Jan Beulich | 5380975 | 2012-12-17 16:01:31 -0800 | [diff] [blame] | 2998 | if (is_sign) |
| 2999 | *va_arg(args, int *) = val.s; |
| 3000 | else |
| 3001 | *va_arg(args, unsigned int *) = val.u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | break; |
| 3003 | } |
| 3004 | num++; |
| 3005 | |
| 3006 | if (!next) |
| 3007 | break; |
| 3008 | str = next; |
| 3009 | } |
Johannes Berg | c6b40d1 | 2007-05-08 00:27:20 -0700 | [diff] [blame] | 3010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | return num; |
| 3012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | EXPORT_SYMBOL(vsscanf); |
| 3014 | |
| 3015 | /** |
| 3016 | * sscanf - Unformat a buffer into a list of arguments |
| 3017 | * @buf: input buffer |
| 3018 | * @fmt: formatting of buffer |
| 3019 | * @...: resulting arguments |
| 3020 | */ |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3021 | int sscanf(const char *buf, const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | { |
| 3023 | va_list args; |
| 3024 | int i; |
| 3025 | |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3026 | va_start(args, fmt); |
| 3027 | i = vsscanf(buf, fmt, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | va_end(args); |
André Goddard Rosa | 7b9186f | 2009-12-14 18:00:57 -0800 | [diff] [blame] | 3029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | return i; |
| 3031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | EXPORT_SYMBOL(sscanf); |