vsprintf.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  1. /*
  2. * linux/lib/vsprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
  7. /*
  8. * Wirzenius wrote this portably, Torvalds fucked it up :-)
  9. */
  10. /*
  11. * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  12. * - changed to provide snprintf and vsnprintf functions
  13. * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
  14. * - scnprintf and vscnprintf
  15. */
  16. #include <stdarg.h>
  17. #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/ctype.h>
  21. #include <linux/kernel.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/math64.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/ioport.h>
  26. #include <linux/dcache.h>
  27. #include <linux/cred.h>
  28. #include <net/addrconf.h>
  29. #include <asm/page.h> /* for PAGE_SIZE */
  30. #include <asm/sections.h> /* for dereference_function_descriptor() */
  31. #include <linux/string_helpers.h>
  32. #include "kstrtox.h"
  33. /**
  34. * simple_strtoull - convert a string to an unsigned long long
  35. * @cp: The start of the string
  36. * @endp: A pointer to the end of the parsed string will be placed here
  37. * @base: The number base to use
  38. *
  39. * This function is obsolete. Please use kstrtoull instead.
  40. */
  41. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  42. {
  43. unsigned long long result;
  44. unsigned int rv;
  45. cp = _parse_integer_fixup_radix(cp, &base);
  46. rv = _parse_integer(cp, base, &result);
  47. /* FIXME */
  48. cp += (rv & ~KSTRTOX_OVERFLOW);
  49. if (endp)
  50. *endp = (char *)cp;
  51. return result;
  52. }
  53. EXPORT_SYMBOL(simple_strtoull);
  54. /**
  55. * simple_strtoul - convert a string to an unsigned long
  56. * @cp: The start of the string
  57. * @endp: A pointer to the end of the parsed string will be placed here
  58. * @base: The number base to use
  59. *
  60. * This function is obsolete. Please use kstrtoul instead.
  61. */
  62. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  63. {
  64. return simple_strtoull(cp, endp, base);
  65. }
  66. EXPORT_SYMBOL(simple_strtoul);
  67. /**
  68. * simple_strtol - convert a string to a signed long
  69. * @cp: The start of the string
  70. * @endp: A pointer to the end of the parsed string will be placed here
  71. * @base: The number base to use
  72. *
  73. * This function is obsolete. Please use kstrtol instead.
  74. */
  75. long simple_strtol(const char *cp, char **endp, unsigned int base)
  76. {
  77. if (*cp == '-')
  78. return -simple_strtoul(cp + 1, endp, base);
  79. return simple_strtoul(cp, endp, base);
  80. }
  81. EXPORT_SYMBOL(simple_strtol);
  82. /**
  83. * simple_strtoll - convert a string to a signed long long
  84. * @cp: The start of the string
  85. * @endp: A pointer to the end of the parsed string will be placed here
  86. * @base: The number base to use
  87. *
  88. * This function is obsolete. Please use kstrtoll instead.
  89. */
  90. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  91. {
  92. if (*cp == '-')
  93. return -simple_strtoull(cp + 1, endp, base);
  94. return simple_strtoull(cp, endp, base);
  95. }
  96. EXPORT_SYMBOL(simple_strtoll);
  97. static noinline_for_stack
  98. int skip_atoi(const char **s)
  99. {
  100. int i = 0;
  101. do {
  102. i = i*10 + *((*s)++) - '0';
  103. } while (isdigit(**s));
  104. return i;
  105. }
  106. /* Decimal conversion is by far the most typical, and is used
  107. * for /proc and /sys data. This directly impacts e.g. top performance
  108. * with many processes running. We optimize it for speed
  109. * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
  110. * (with permission from the author, Douglas W. Jones).
  111. */
  112. #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
  113. /* Formats correctly any integer in [0, 999999999] */
  114. static noinline_for_stack
  115. char *put_dec_full9(char *buf, unsigned q)
  116. {
  117. unsigned r;
  118. /*
  119. * Possible ways to approx. divide by 10
  120. * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
  121. * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
  122. * (x * 0x6667) >> 18 x < 43699
  123. * (x * 0x3334) >> 17 x < 16389
  124. * (x * 0x199a) >> 16 x < 16389
  125. * (x * 0x0ccd) >> 15 x < 16389
  126. * (x * 0x0667) >> 14 x < 2739
  127. * (x * 0x0334) >> 13 x < 1029
  128. * (x * 0x019a) >> 12 x < 1029
  129. * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
  130. * (x * 0x0067) >> 10 x < 179
  131. * (x * 0x0034) >> 9 x < 69 same
  132. * (x * 0x001a) >> 8 x < 69 same
  133. * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
  134. * (x * 0x0007) >> 6 x < 19
  135. * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
  136. */
  137. r = (q * (uint64_t)0x1999999a) >> 32;
  138. *buf++ = (q - 10 * r) + '0'; /* 1 */
  139. q = (r * (uint64_t)0x1999999a) >> 32;
  140. *buf++ = (r - 10 * q) + '0'; /* 2 */
  141. r = (q * (uint64_t)0x1999999a) >> 32;
  142. *buf++ = (q - 10 * r) + '0'; /* 3 */
  143. q = (r * (uint64_t)0x1999999a) >> 32;
  144. *buf++ = (r - 10 * q) + '0'; /* 4 */
  145. r = (q * (uint64_t)0x1999999a) >> 32;
  146. *buf++ = (q - 10 * r) + '0'; /* 5 */
  147. /* Now value is under 10000, can avoid 64-bit multiply */
  148. q = (r * 0x199a) >> 16;
  149. *buf++ = (r - 10 * q) + '0'; /* 6 */
  150. r = (q * 0xcd) >> 11;
  151. *buf++ = (q - 10 * r) + '0'; /* 7 */
  152. q = (r * 0xcd) >> 11;
  153. *buf++ = (r - 10 * q) + '0'; /* 8 */
  154. *buf++ = q + '0'; /* 9 */
  155. return buf;
  156. }
  157. #endif
  158. /* Similar to above but do not pad with zeros.
  159. * Code can be easily arranged to print 9 digits too, but our callers
  160. * always call put_dec_full9() instead when the number has 9 decimal digits.
  161. */
  162. static noinline_for_stack
  163. char *put_dec_trunc8(char *buf, unsigned r)
  164. {
  165. unsigned q;
  166. /* Copy of previous function's body with added early returns */
  167. while (r >= 10000) {
  168. q = r + '0';
  169. r = (r * (uint64_t)0x1999999a) >> 32;
  170. *buf++ = q - 10*r;
  171. }
  172. q = (r * 0x199a) >> 16; /* r <= 9999 */
  173. *buf++ = (r - 10 * q) + '0';
  174. if (q == 0)
  175. return buf;
  176. r = (q * 0xcd) >> 11; /* q <= 999 */
  177. *buf++ = (q - 10 * r) + '0';
  178. if (r == 0)
  179. return buf;
  180. q = (r * 0xcd) >> 11; /* r <= 99 */
  181. *buf++ = (r - 10 * q) + '0';
  182. if (q == 0)
  183. return buf;
  184. *buf++ = q + '0'; /* q <= 9 */
  185. return buf;
  186. }
  187. /* There are two algorithms to print larger numbers.
  188. * One is generic: divide by 1000000000 and repeatedly print
  189. * groups of (up to) 9 digits. It's conceptually simple,
  190. * but requires a (unsigned long long) / 1000000000 division.
  191. *
  192. * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
  193. * manipulates them cleverly and generates groups of 4 decimal digits.
  194. * It so happens that it does NOT require long long division.
  195. *
  196. * If long is > 32 bits, division of 64-bit values is relatively easy,
  197. * and we will use the first algorithm.
  198. * If long long is > 64 bits (strange architecture with VERY large long long),
  199. * second algorithm can't be used, and we again use the first one.
  200. *
  201. * Else (if long is 32 bits and long long is 64 bits) we use second one.
  202. */
  203. #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
  204. /* First algorithm: generic */
  205. static
  206. char *put_dec(char *buf, unsigned long long n)
  207. {
  208. if (n >= 100*1000*1000) {
  209. while (n >= 1000*1000*1000)
  210. buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
  211. if (n >= 100*1000*1000)
  212. return put_dec_full9(buf, n);
  213. }
  214. return put_dec_trunc8(buf, n);
  215. }
  216. #else
  217. /* Second algorithm: valid only for 64-bit long longs */
  218. /* See comment in put_dec_full9 for choice of constants */
  219. static noinline_for_stack
  220. void put_dec_full4(char *buf, unsigned q)
  221. {
  222. unsigned r;
  223. r = (q * 0xccd) >> 15;
  224. buf[0] = (q - 10 * r) + '0';
  225. q = (r * 0xcd) >> 11;
  226. buf[1] = (r - 10 * q) + '0';
  227. r = (q * 0xcd) >> 11;
  228. buf[2] = (q - 10 * r) + '0';
  229. buf[3] = r + '0';
  230. }
  231. /*
  232. * Call put_dec_full4 on x % 10000, return x / 10000.
  233. * The approximation x/10000 == (x * 0x346DC5D7) >> 43
  234. * holds for all x < 1,128,869,999. The largest value this
  235. * helper will ever be asked to convert is 1,125,520,955.
  236. * (d1 in the put_dec code, assuming n is all-ones).
  237. */
  238. static
  239. unsigned put_dec_helper4(char *buf, unsigned x)
  240. {
  241. uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
  242. put_dec_full4(buf, x - q * 10000);
  243. return q;
  244. }
  245. /* Based on code by Douglas W. Jones found at
  246. * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
  247. * (with permission from the author).
  248. * Performs no 64-bit division and hence should be fast on 32-bit machines.
  249. */
  250. static
  251. char *put_dec(char *buf, unsigned long long n)
  252. {
  253. uint32_t d3, d2, d1, q, h;
  254. if (n < 100*1000*1000)
  255. return put_dec_trunc8(buf, n);
  256. d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
  257. h = (n >> 32);
  258. d2 = (h ) & 0xffff;
  259. d3 = (h >> 16); /* implicit "& 0xffff" */
  260. q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
  261. q = put_dec_helper4(buf, q);
  262. q += 7671 * d3 + 9496 * d2 + 6 * d1;
  263. q = put_dec_helper4(buf+4, q);
  264. q += 4749 * d3 + 42 * d2;
  265. q = put_dec_helper4(buf+8, q);
  266. q += 281 * d3;
  267. buf += 12;
  268. if (q)
  269. buf = put_dec_trunc8(buf, q);
  270. else while (buf[-1] == '0')
  271. --buf;
  272. return buf;
  273. }
  274. #endif
  275. /*
  276. * Convert passed number to decimal string.
  277. * Returns the length of string. On buffer overflow, returns 0.
  278. *
  279. * If speed is not important, use snprintf(). It's easy to read the code.
  280. */
  281. int num_to_str(char *buf, int size, unsigned long long num)
  282. {
  283. char tmp[sizeof(num) * 3];
  284. int idx, len;
  285. /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
  286. if (num <= 9) {
  287. tmp[0] = '0' + num;
  288. len = 1;
  289. } else {
  290. len = put_dec(tmp, num) - tmp;
  291. }
  292. if (len > size)
  293. return 0;
  294. for (idx = 0; idx < len; ++idx)
  295. buf[idx] = tmp[len - idx - 1];
  296. return len;
  297. }
  298. #define SIGN 1 /* unsigned/signed, must be 1 */
  299. #define ZEROPAD 2 /* pad with zero */
  300. #define PLUS 4 /* show plus */
  301. #define SPACE 8 /* space if plus */
  302. #define LEFT 16 /* left justified */
  303. #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
  304. #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
  305. enum format_type {
  306. FORMAT_TYPE_NONE, /* Just a string part */
  307. FORMAT_TYPE_WIDTH,
  308. FORMAT_TYPE_PRECISION,
  309. FORMAT_TYPE_CHAR,
  310. FORMAT_TYPE_STR,
  311. FORMAT_TYPE_PTR,
  312. FORMAT_TYPE_PERCENT_CHAR,
  313. FORMAT_TYPE_INVALID,
  314. FORMAT_TYPE_LONG_LONG,
  315. FORMAT_TYPE_ULONG,
  316. FORMAT_TYPE_LONG,
  317. FORMAT_TYPE_UBYTE,
  318. FORMAT_TYPE_BYTE,
  319. FORMAT_TYPE_USHORT,
  320. FORMAT_TYPE_SHORT,
  321. FORMAT_TYPE_UINT,
  322. FORMAT_TYPE_INT,
  323. FORMAT_TYPE_SIZE_T,
  324. FORMAT_TYPE_PTRDIFF
  325. };
  326. struct printf_spec {
  327. u8 type; /* format_type enum */
  328. u8 flags; /* flags to number() */
  329. u8 base; /* number base, 8, 10 or 16 only */
  330. u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
  331. s16 field_width; /* width of output field */
  332. s16 precision; /* # of digits/chars */
  333. };
  334. static noinline_for_stack
  335. char *number(char *buf, char *end, unsigned long long num,
  336. struct printf_spec spec)
  337. {
  338. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  339. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  340. char tmp[3 * sizeof(num)];
  341. char sign;
  342. char locase;
  343. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  344. int i;
  345. bool is_zero = num == 0LL;
  346. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  347. * produces same digits or (maybe lowercased) letters */
  348. locase = (spec.flags & SMALL);
  349. if (spec.flags & LEFT)
  350. spec.flags &= ~ZEROPAD;
  351. sign = 0;
  352. if (spec.flags & SIGN) {
  353. if ((signed long long)num < 0) {
  354. sign = '-';
  355. num = -(signed long long)num;
  356. spec.field_width--;
  357. } else if (spec.flags & PLUS) {
  358. sign = '+';
  359. spec.field_width--;
  360. } else if (spec.flags & SPACE) {
  361. sign = ' ';
  362. spec.field_width--;
  363. }
  364. }
  365. if (need_pfx) {
  366. if (spec.base == 16)
  367. spec.field_width -= 2;
  368. else if (!is_zero)
  369. spec.field_width--;
  370. }
  371. /* generate full string in tmp[], in reverse order */
  372. i = 0;
  373. if (num < spec.base)
  374. tmp[i++] = digits[num] | locase;
  375. else if (spec.base != 10) { /* 8 or 16 */
  376. int mask = spec.base - 1;
  377. int shift = 3;
  378. if (spec.base == 16)
  379. shift = 4;
  380. do {
  381. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  382. num >>= shift;
  383. } while (num);
  384. } else { /* base 10 */
  385. i = put_dec(tmp, num) - tmp;
  386. }
  387. /* printing 100 using %2d gives "100", not "00" */
  388. if (i > spec.precision)
  389. spec.precision = i;
  390. /* leading space padding */
  391. spec.field_width -= spec.precision;
  392. if (!(spec.flags & (ZEROPAD | LEFT))) {
  393. while (--spec.field_width >= 0) {
  394. if (buf < end)
  395. *buf = ' ';
  396. ++buf;
  397. }
  398. }
  399. /* sign */
  400. if (sign) {
  401. if (buf < end)
  402. *buf = sign;
  403. ++buf;
  404. }
  405. /* "0x" / "0" prefix */
  406. if (need_pfx) {
  407. if (spec.base == 16 || !is_zero) {
  408. if (buf < end)
  409. *buf = '0';
  410. ++buf;
  411. }
  412. if (spec.base == 16) {
  413. if (buf < end)
  414. *buf = ('X' | locase);
  415. ++buf;
  416. }
  417. }
  418. /* zero or space padding */
  419. if (!(spec.flags & LEFT)) {
  420. char c = (spec.flags & ZEROPAD) ? '0' : ' ';
  421. while (--spec.field_width >= 0) {
  422. if (buf < end)
  423. *buf = c;
  424. ++buf;
  425. }
  426. }
  427. /* hmm even more zero padding? */
  428. while (i <= --spec.precision) {
  429. if (buf < end)
  430. *buf = '0';
  431. ++buf;
  432. }
  433. /* actual digits of result */
  434. while (--i >= 0) {
  435. if (buf < end)
  436. *buf = tmp[i];
  437. ++buf;
  438. }
  439. /* trailing space padding */
  440. while (--spec.field_width >= 0) {
  441. if (buf < end)
  442. *buf = ' ';
  443. ++buf;
  444. }
  445. return buf;
  446. }
  447. static noinline_for_stack
  448. char *string(char *buf, char *end, const char *s, struct printf_spec spec)
  449. {
  450. int len, i;
  451. if ((unsigned long)s < PAGE_SIZE)
  452. s = "(null)";
  453. len = strnlen(s, spec.precision);
  454. if (!(spec.flags & LEFT)) {
  455. while (len < spec.field_width--) {
  456. if (buf < end)
  457. *buf = ' ';
  458. ++buf;
  459. }
  460. }
  461. for (i = 0; i < len; ++i) {
  462. if (buf < end)
  463. *buf = *s;
  464. ++buf; ++s;
  465. }
  466. while (len < spec.field_width--) {
  467. if (buf < end)
  468. *buf = ' ';
  469. ++buf;
  470. }
  471. return buf;
  472. }
  473. static void widen(char *buf, char *end, unsigned len, unsigned spaces)
  474. {
  475. size_t size;
  476. if (buf >= end) /* nowhere to put anything */
  477. return;
  478. size = end - buf;
  479. if (size <= spaces) {
  480. memset(buf, ' ', size);
  481. return;
  482. }
  483. if (len) {
  484. if (len > size - spaces)
  485. len = size - spaces;
  486. memmove(buf + spaces, buf, len);
  487. }
  488. memset(buf, ' ', spaces);
  489. }
  490. static noinline_for_stack
  491. char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
  492. const char *fmt)
  493. {
  494. const char *array[4], *s;
  495. const struct dentry *p;
  496. int depth;
  497. int i, n;
  498. switch (fmt[1]) {
  499. case '2': case '3': case '4':
  500. depth = fmt[1] - '0';
  501. break;
  502. default:
  503. depth = 1;
  504. }
  505. rcu_read_lock();
  506. for (i = 0; i < depth; i++, d = p) {
  507. p = ACCESS_ONCE(d->d_parent);
  508. array[i] = ACCESS_ONCE(d->d_name.name);
  509. if (p == d) {
  510. if (i)
  511. array[i] = "";
  512. i++;
  513. break;
  514. }
  515. }
  516. s = array[--i];
  517. for (n = 0; n != spec.precision; n++, buf++) {
  518. char c = *s++;
  519. if (!c) {
  520. if (!i)
  521. break;
  522. c = '/';
  523. s = array[--i];
  524. }
  525. if (buf < end)
  526. *buf = c;
  527. }
  528. rcu_read_unlock();
  529. if (n < spec.field_width) {
  530. /* we want to pad the sucker */
  531. unsigned spaces = spec.field_width - n;
  532. if (!(spec.flags & LEFT)) {
  533. widen(buf - n, end, n, spaces);
  534. return buf + spaces;
  535. }
  536. while (spaces--) {
  537. if (buf < end)
  538. *buf = ' ';
  539. ++buf;
  540. }
  541. }
  542. return buf;
  543. }
  544. static noinline_for_stack
  545. char *symbol_string(char *buf, char *end, void *ptr,
  546. struct printf_spec spec, const char *fmt)
  547. {
  548. unsigned long value;
  549. #ifdef CONFIG_KALLSYMS
  550. char sym[KSYM_SYMBOL_LEN];
  551. #endif
  552. if (fmt[1] == 'R')
  553. ptr = __builtin_extract_return_addr(ptr);
  554. value = (unsigned long)ptr;
  555. #ifdef CONFIG_KALLSYMS
  556. if (*fmt == 'B')
  557. sprint_backtrace(sym, value);
  558. else if (*fmt != 'f' && *fmt != 's')
  559. sprint_symbol(sym, value);
  560. else
  561. sprint_symbol_no_offset(sym, value);
  562. return string(buf, end, sym, spec);
  563. #else
  564. spec.field_width = 2 * sizeof(void *);
  565. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  566. spec.base = 16;
  567. return number(buf, end, value, spec);
  568. #endif
  569. }
  570. static noinline_for_stack
  571. char *resource_string(char *buf, char *end, struct resource *res,
  572. struct printf_spec spec, const char *fmt)
  573. {
  574. #ifndef IO_RSRC_PRINTK_SIZE
  575. #define IO_RSRC_PRINTK_SIZE 6
  576. #endif
  577. #ifndef MEM_RSRC_PRINTK_SIZE
  578. #define MEM_RSRC_PRINTK_SIZE 10
  579. #endif
  580. static const struct printf_spec io_spec = {
  581. .base = 16,
  582. .field_width = IO_RSRC_PRINTK_SIZE,
  583. .precision = -1,
  584. .flags = SPECIAL | SMALL | ZEROPAD,
  585. };
  586. static const struct printf_spec mem_spec = {
  587. .base = 16,
  588. .field_width = MEM_RSRC_PRINTK_SIZE,
  589. .precision = -1,
  590. .flags = SPECIAL | SMALL | ZEROPAD,
  591. };
  592. static const struct printf_spec bus_spec = {
  593. .base = 16,
  594. .field_width = 2,
  595. .precision = -1,
  596. .flags = SMALL | ZEROPAD,
  597. };
  598. static const struct printf_spec dec_spec = {
  599. .base = 10,
  600. .precision = -1,
  601. .flags = 0,
  602. };
  603. static const struct printf_spec str_spec = {
  604. .field_width = -1,
  605. .precision = 10,
  606. .flags = LEFT,
  607. };
  608. static const struct printf_spec flag_spec = {
  609. .base = 16,
  610. .precision = -1,
  611. .flags = SPECIAL | SMALL,
  612. };
  613. /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
  614. * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
  615. #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
  616. #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
  617. #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
  618. #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
  619. char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
  620. 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
  621. char *p = sym, *pend = sym + sizeof(sym);
  622. int decode = (fmt[0] == 'R') ? 1 : 0;
  623. const struct printf_spec *specp;
  624. *p++ = '[';
  625. if (res->flags & IORESOURCE_IO) {
  626. p = string(p, pend, "io ", str_spec);
  627. specp = &io_spec;
  628. } else if (res->flags & IORESOURCE_MEM) {
  629. p = string(p, pend, "mem ", str_spec);
  630. specp = &mem_spec;
  631. } else if (res->flags & IORESOURCE_IRQ) {
  632. p = string(p, pend, "irq ", str_spec);
  633. specp = &dec_spec;
  634. } else if (res->flags & IORESOURCE_DMA) {
  635. p = string(p, pend, "dma ", str_spec);
  636. specp = &dec_spec;
  637. } else if (res->flags & IORESOURCE_BUS) {
  638. p = string(p, pend, "bus ", str_spec);
  639. specp = &bus_spec;
  640. } else {
  641. p = string(p, pend, "??? ", str_spec);
  642. specp = &mem_spec;
  643. decode = 0;
  644. }
  645. if (decode && res->flags & IORESOURCE_UNSET) {
  646. p = string(p, pend, "size ", str_spec);
  647. p = number(p, pend, resource_size(res), *specp);
  648. } else {
  649. p = number(p, pend, res->start, *specp);
  650. if (res->start != res->end) {
  651. *p++ = '-';
  652. p = number(p, pend, res->end, *specp);
  653. }
  654. }
  655. if (decode) {
  656. if (res->flags & IORESOURCE_MEM_64)
  657. p = string(p, pend, " 64bit", str_spec);
  658. if (res->flags & IORESOURCE_PREFETCH)
  659. p = string(p, pend, " pref", str_spec);
  660. if (res->flags & IORESOURCE_WINDOW)
  661. p = string(p, pend, " window", str_spec);
  662. if (res->flags & IORESOURCE_DISABLED)
  663. p = string(p, pend, " disabled", str_spec);
  664. } else {
  665. p = string(p, pend, " flags ", str_spec);
  666. p = number(p, pend, res->flags, flag_spec);
  667. }
  668. *p++ = ']';
  669. *p = '\0';
  670. return string(buf, end, sym, spec);
  671. }
  672. static noinline_for_stack
  673. char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  674. const char *fmt)
  675. {
  676. int i, len = 1; /* if we pass '%ph[CDN]', field width remains
  677. negative value, fallback to the default */
  678. char separator;
  679. if (spec.field_width == 0)
  680. /* nothing to print */
  681. return buf;
  682. if (ZERO_OR_NULL_PTR(addr))
  683. /* NULL pointer */
  684. return string(buf, end, NULL, spec);
  685. switch (fmt[1]) {
  686. case 'C':
  687. separator = ':';
  688. break;
  689. case 'D':
  690. separator = '-';
  691. break;
  692. case 'N':
  693. separator = 0;
  694. break;
  695. default:
  696. separator = ' ';
  697. break;
  698. }
  699. if (spec.field_width > 0)
  700. len = min_t(int, spec.field_width, 64);
  701. for (i = 0; i < len && buf < end - 1; i++) {
  702. buf = hex_byte_pack(buf, addr[i]);
  703. if (buf < end && separator && i != len - 1)
  704. *buf++ = separator;
  705. }
  706. return buf;
  707. }
  708. static noinline_for_stack
  709. char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
  710. struct printf_spec spec, const char *fmt)
  711. {
  712. const int CHUNKSZ = 32;
  713. int nr_bits = max_t(int, spec.field_width, 0);
  714. int i, chunksz;
  715. bool first = true;
  716. /* reused to print numbers */
  717. spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
  718. chunksz = nr_bits & (CHUNKSZ - 1);
  719. if (chunksz == 0)
  720. chunksz = CHUNKSZ;
  721. i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
  722. for (; i >= 0; i -= CHUNKSZ) {
  723. u32 chunkmask, val;
  724. int word, bit;
  725. chunkmask = ((1ULL << chunksz) - 1);
  726. word = i / BITS_PER_LONG;
  727. bit = i % BITS_PER_LONG;
  728. val = (bitmap[word] >> bit) & chunkmask;
  729. if (!first) {
  730. if (buf < end)
  731. *buf = ',';
  732. buf++;
  733. }
  734. first = false;
  735. spec.field_width = DIV_ROUND_UP(chunksz, 4);
  736. buf = number(buf, end, val, spec);
  737. chunksz = CHUNKSZ;
  738. }
  739. return buf;
  740. }
  741. static noinline_for_stack
  742. char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
  743. struct printf_spec spec, const char *fmt)
  744. {
  745. int nr_bits = max_t(int, spec.field_width, 0);
  746. /* current bit is 'cur', most recently seen range is [rbot, rtop] */
  747. int cur, rbot, rtop;
  748. bool first = true;
  749. /* reused to print numbers */
  750. spec = (struct printf_spec){ .base = 10 };
  751. rbot = cur = find_first_bit(bitmap, nr_bits);
  752. while (cur < nr_bits) {
  753. rtop = cur;
  754. cur = find_next_bit(bitmap, nr_bits, cur + 1);
  755. if (cur < nr_bits && cur <= rtop + 1)
  756. continue;
  757. if (!first) {
  758. if (buf < end)
  759. *buf = ',';
  760. buf++;
  761. }
  762. first = false;
  763. buf = number(buf, end, rbot, spec);
  764. if (rbot < rtop) {
  765. if (buf < end)
  766. *buf = '-';
  767. buf++;
  768. buf = number(buf, end, rtop, spec);
  769. }
  770. rbot = cur;
  771. }
  772. return buf;
  773. }
  774. static noinline_for_stack
  775. char *mac_address_string(char *buf, char *end, u8 *addr,
  776. struct printf_spec spec, const char *fmt)
  777. {
  778. char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
  779. char *p = mac_addr;
  780. int i;
  781. char separator;
  782. bool reversed = false;
  783. switch (fmt[1]) {
  784. case 'F':
  785. separator = '-';
  786. break;
  787. case 'R':
  788. reversed = true;
  789. /* fall through */
  790. default:
  791. separator = ':';
  792. break;
  793. }
  794. for (i = 0; i < 6; i++) {
  795. if (reversed)
  796. p = hex_byte_pack(p, addr[5 - i]);
  797. else
  798. p = hex_byte_pack(p, addr[i]);
  799. if (fmt[0] == 'M' && i != 5)
  800. *p++ = separator;
  801. }
  802. *p = '\0';
  803. return string(buf, end, mac_addr, spec);
  804. }
  805. static noinline_for_stack
  806. char *ip4_string(char *p, const u8 *addr, const char *fmt)
  807. {
  808. int i;
  809. bool leading_zeros = (fmt[0] == 'i');
  810. int index;
  811. int step;
  812. switch (fmt[2]) {
  813. case 'h':
  814. #ifdef __BIG_ENDIAN
  815. index = 0;
  816. step = 1;
  817. #else
  818. index = 3;
  819. step = -1;
  820. #endif
  821. break;
  822. case 'l':
  823. index = 3;
  824. step = -1;
  825. break;
  826. case 'n':
  827. case 'b':
  828. default:
  829. index = 0;
  830. step = 1;
  831. break;
  832. }
  833. for (i = 0; i < 4; i++) {
  834. char temp[3]; /* hold each IP quad in reverse order */
  835. int digits = put_dec_trunc8(temp, addr[index]) - temp;
  836. if (leading_zeros) {
  837. if (digits < 3)
  838. *p++ = '0';
  839. if (digits < 2)
  840. *p++ = '0';
  841. }
  842. /* reverse the digits in the quad */
  843. while (digits--)
  844. *p++ = temp[digits];
  845. if (i < 3)
  846. *p++ = '.';
  847. index += step;
  848. }
  849. *p = '\0';
  850. return p;
  851. }
  852. static noinline_for_stack
  853. char *ip6_compressed_string(char *p, const char *addr)
  854. {
  855. int i, j, range;
  856. unsigned char zerolength[8];
  857. int longest = 1;
  858. int colonpos = -1;
  859. u16 word;
  860. u8 hi, lo;
  861. bool needcolon = false;
  862. bool useIPv4;
  863. struct in6_addr in6;
  864. memcpy(&in6, addr, sizeof(struct in6_addr));
  865. useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
  866. memset(zerolength, 0, sizeof(zerolength));
  867. if (useIPv4)
  868. range = 6;
  869. else
  870. range = 8;
  871. /* find position of longest 0 run */
  872. for (i = 0; i < range; i++) {
  873. for (j = i; j < range; j++) {
  874. if (in6.s6_addr16[j] != 0)
  875. break;
  876. zerolength[i]++;
  877. }
  878. }
  879. for (i = 0; i < range; i++) {
  880. if (zerolength[i] > longest) {
  881. longest = zerolength[i];
  882. colonpos = i;
  883. }
  884. }
  885. if (longest == 1) /* don't compress a single 0 */
  886. colonpos = -1;
  887. /* emit address */
  888. for (i = 0; i < range; i++) {
  889. if (i == colonpos) {
  890. if (needcolon || i == 0)
  891. *p++ = ':';
  892. *p++ = ':';
  893. needcolon = false;
  894. i += longest - 1;
  895. continue;
  896. }
  897. if (needcolon) {
  898. *p++ = ':';
  899. needcolon = false;
  900. }
  901. /* hex u16 without leading 0s */
  902. word = ntohs(in6.s6_addr16[i]);
  903. hi = word >> 8;
  904. lo = word & 0xff;
  905. if (hi) {
  906. if (hi > 0x0f)
  907. p = hex_byte_pack(p, hi);
  908. else
  909. *p++ = hex_asc_lo(hi);
  910. p = hex_byte_pack(p, lo);
  911. }
  912. else if (lo > 0x0f)
  913. p = hex_byte_pack(p, lo);
  914. else
  915. *p++ = hex_asc_lo(lo);
  916. needcolon = true;
  917. }
  918. if (useIPv4) {
  919. if (needcolon)
  920. *p++ = ':';
  921. p = ip4_string(p, &in6.s6_addr[12], "I4");
  922. }
  923. *p = '\0';
  924. return p;
  925. }
  926. static noinline_for_stack
  927. char *ip6_string(char *p, const char *addr, const char *fmt)
  928. {
  929. int i;
  930. for (i = 0; i < 8; i++) {
  931. p = hex_byte_pack(p, *addr++);
  932. p = hex_byte_pack(p, *addr++);
  933. if (fmt[0] == 'I' && i != 7)
  934. *p++ = ':';
  935. }
  936. *p = '\0';
  937. return p;
  938. }
  939. static noinline_for_stack
  940. char *ip6_addr_string(char *buf, char *end, const u8 *addr,
  941. struct printf_spec spec, const char *fmt)
  942. {
  943. char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
  944. if (fmt[0] == 'I' && fmt[2] == 'c')
  945. ip6_compressed_string(ip6_addr, addr);
  946. else
  947. ip6_string(ip6_addr, addr, fmt);
  948. return string(buf, end, ip6_addr, spec);
  949. }
  950. static noinline_for_stack
  951. char *ip4_addr_string(char *buf, char *end, const u8 *addr,
  952. struct printf_spec spec, const char *fmt)
  953. {
  954. char ip4_addr[sizeof("255.255.255.255")];
  955. ip4_string(ip4_addr, addr, fmt);
  956. return string(buf, end, ip4_addr, spec);
  957. }
  958. static noinline_for_stack
  959. char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
  960. struct printf_spec spec, const char *fmt)
  961. {
  962. bool have_p = false, have_s = false, have_f = false, have_c = false;
  963. char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
  964. sizeof(":12345") + sizeof("/123456789") +
  965. sizeof("%1234567890")];
  966. char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
  967. const u8 *addr = (const u8 *) &sa->sin6_addr;
  968. char fmt6[2] = { fmt[0], '6' };
  969. u8 off = 0;
  970. fmt++;
  971. while (isalpha(*++fmt)) {
  972. switch (*fmt) {
  973. case 'p':
  974. have_p = true;
  975. break;
  976. case 'f':
  977. have_f = true;
  978. break;
  979. case 's':
  980. have_s = true;
  981. break;
  982. case 'c':
  983. have_c = true;
  984. break;
  985. }
  986. }
  987. if (have_p || have_s || have_f) {
  988. *p = '[';
  989. off = 1;
  990. }
  991. if (fmt6[0] == 'I' && have_c)
  992. p = ip6_compressed_string(ip6_addr + off, addr);
  993. else
  994. p = ip6_string(ip6_addr + off, addr, fmt6);
  995. if (have_p || have_s || have_f)
  996. *p++ = ']';
  997. if (have_p) {
  998. *p++ = ':';
  999. p = number(p, pend, ntohs(sa->sin6_port), spec);
  1000. }
  1001. if (have_f) {
  1002. *p++ = '/';
  1003. p = number(p, pend, ntohl(sa->sin6_flowinfo &
  1004. IPV6_FLOWINFO_MASK), spec);
  1005. }
  1006. if (have_s) {
  1007. *p++ = '%';
  1008. p = number(p, pend, sa->sin6_scope_id, spec);
  1009. }
  1010. *p = '\0';
  1011. return string(buf, end, ip6_addr, spec);
  1012. }
  1013. static noinline_for_stack
  1014. char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
  1015. struct printf_spec spec, const char *fmt)
  1016. {
  1017. bool have_p = false;
  1018. char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
  1019. char *pend = ip4_addr + sizeof(ip4_addr);
  1020. const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
  1021. char fmt4[3] = { fmt[0], '4', 0 };
  1022. fmt++;
  1023. while (isalpha(*++fmt)) {
  1024. switch (*fmt) {
  1025. case 'p':
  1026. have_p = true;
  1027. break;
  1028. case 'h':
  1029. case 'l':
  1030. case 'n':
  1031. case 'b':
  1032. fmt4[2] = *fmt;
  1033. break;
  1034. }
  1035. }
  1036. p = ip4_string(ip4_addr, addr, fmt4);
  1037. if (have_p) {
  1038. *p++ = ':';
  1039. p = number(p, pend, ntohs(sa->sin_port), spec);
  1040. }
  1041. *p = '\0';
  1042. return string(buf, end, ip4_addr, spec);
  1043. }
  1044. static noinline_for_stack
  1045. char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  1046. const char *fmt)
  1047. {
  1048. bool found = true;
  1049. int count = 1;
  1050. unsigned int flags = 0;
  1051. int len;
  1052. if (spec.field_width == 0)
  1053. return buf; /* nothing to print */
  1054. if (ZERO_OR_NULL_PTR(addr))
  1055. return string(buf, end, NULL, spec); /* NULL pointer */
  1056. do {
  1057. switch (fmt[count++]) {
  1058. case 'a':
  1059. flags |= ESCAPE_ANY;
  1060. break;
  1061. case 'c':
  1062. flags |= ESCAPE_SPECIAL;
  1063. break;
  1064. case 'h':
  1065. flags |= ESCAPE_HEX;
  1066. break;
  1067. case 'n':
  1068. flags |= ESCAPE_NULL;
  1069. break;
  1070. case 'o':
  1071. flags |= ESCAPE_OCTAL;
  1072. break;
  1073. case 'p':
  1074. flags |= ESCAPE_NP;
  1075. break;
  1076. case 's':
  1077. flags |= ESCAPE_SPACE;
  1078. break;
  1079. default:
  1080. found = false;
  1081. break;
  1082. }
  1083. } while (found);
  1084. if (!flags)
  1085. flags = ESCAPE_ANY_NP;
  1086. len = spec.field_width < 0 ? 1 : spec.field_width;
  1087. /* Ignore the error. We print as many characters as we can */
  1088. string_escape_mem(addr, len, &buf, end - buf, flags, NULL);
  1089. return buf;
  1090. }
  1091. static noinline_for_stack
  1092. char *uuid_string(char *buf, char *end, const u8 *addr,
  1093. struct printf_spec spec, const char *fmt)
  1094. {
  1095. char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
  1096. char *p = uuid;
  1097. int i;
  1098. static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  1099. static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
  1100. const u8 *index = be;
  1101. bool uc = false;
  1102. switch (*(++fmt)) {
  1103. case 'L':
  1104. uc = true; /* fall-through */
  1105. case 'l':
  1106. index = le;
  1107. break;
  1108. case 'B':
  1109. uc = true;
  1110. break;
  1111. }
  1112. for (i = 0; i < 16; i++) {
  1113. p = hex_byte_pack(p, addr[index[i]]);
  1114. switch (i) {
  1115. case 3:
  1116. case 5:
  1117. case 7:
  1118. case 9:
  1119. *p++ = '-';
  1120. break;
  1121. }
  1122. }
  1123. *p = 0;
  1124. if (uc) {
  1125. p = uuid;
  1126. do {
  1127. *p = toupper(*p);
  1128. } while (*(++p));
  1129. }
  1130. return string(buf, end, uuid, spec);
  1131. }
  1132. static
  1133. char *netdev_feature_string(char *buf, char *end, const u8 *addr,
  1134. struct printf_spec spec)
  1135. {
  1136. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  1137. if (spec.field_width == -1)
  1138. spec.field_width = 2 + 2 * sizeof(netdev_features_t);
  1139. spec.base = 16;
  1140. return number(buf, end, *(const netdev_features_t *)addr, spec);
  1141. }
  1142. static noinline_for_stack
  1143. char *address_val(char *buf, char *end, const void *addr,
  1144. struct printf_spec spec, const char *fmt)
  1145. {
  1146. unsigned long long num;
  1147. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  1148. spec.base = 16;
  1149. switch (fmt[1]) {
  1150. case 'd':
  1151. num = *(const dma_addr_t *)addr;
  1152. spec.field_width = sizeof(dma_addr_t) * 2 + 2;
  1153. break;
  1154. case 'p':
  1155. default:
  1156. num = *(const phys_addr_t *)addr;
  1157. spec.field_width = sizeof(phys_addr_t) * 2 + 2;
  1158. break;
  1159. }
  1160. return number(buf, end, num, spec);
  1161. }
  1162. int kptr_restrict __read_mostly;
  1163. /*
  1164. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  1165. * by an extra set of alphanumeric characters that are extended format
  1166. * specifiers.
  1167. *
  1168. * Right now we handle:
  1169. *
  1170. * - 'F' For symbolic function descriptor pointers with offset
  1171. * - 'f' For simple symbolic function names without offset
  1172. * - 'S' For symbolic direct pointers with offset
  1173. * - 's' For symbolic direct pointers without offset
  1174. * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
  1175. * - 'B' For backtraced symbolic direct pointers with offset
  1176. * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  1177. * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  1178. * - 'b[l]' For a bitmap, the number of bits is determined by the field
  1179. * width which must be explicitly specified either as part of the
  1180. * format string '%32b[l]' or through '%*b[l]', [l] selects
  1181. * range-list format instead of hex format
  1182. * - 'M' For a 6-byte MAC address, it prints the address in the
  1183. * usual colon-separated hex notation
  1184. * - 'm' For a 6-byte MAC address, it prints the hex address without colons
  1185. * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  1186. * with a dash-separated hex notation
  1187. * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
  1188. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  1189. * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  1190. * IPv6 uses colon separated network-order 16 bit hex with leading 0's
  1191. * [S][pfs]
  1192. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1193. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1194. * - 'i' [46] for 'raw' IPv4/IPv6 addresses
  1195. * IPv6 omits the colons (01020304...0f)
  1196. * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  1197. * [S][pfs]
  1198. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1199. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1200. * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
  1201. * - 'I[6S]c' for IPv6 addresses printed as specified by
  1202. * http://tools.ietf.org/html/rfc5952
  1203. * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
  1204. * of the following flags (see string_escape_mem() for the
  1205. * details):
  1206. * a - ESCAPE_ANY
  1207. * c - ESCAPE_SPECIAL
  1208. * h - ESCAPE_HEX
  1209. * n - ESCAPE_NULL
  1210. * o - ESCAPE_OCTAL
  1211. * p - ESCAPE_NP
  1212. * s - ESCAPE_SPACE
  1213. * By default ESCAPE_ANY_NP is used.
  1214. * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
  1215. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  1216. * Options for %pU are:
  1217. * b big endian lower case hex (default)
  1218. * B big endian UPPER case hex
  1219. * l little endian lower case hex
  1220. * L little endian UPPER case hex
  1221. * big endian output byte order is:
  1222. * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
  1223. * little endian output byte order is:
  1224. * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
  1225. * - 'V' For a struct va_format which contains a format string * and va_list *,
  1226. * call vsnprintf(->format, *->va_list).
  1227. * Implements a "recursive vsnprintf".
  1228. * Do not use this feature without some mechanism to verify the
  1229. * correctness of the format string and va_list arguments.
  1230. * - 'K' For a kernel pointer that should be hidden from unprivileged users
  1231. * - 'NF' For a netdev_features_t
  1232. * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
  1233. * a certain separator (' ' by default):
  1234. * C colon
  1235. * D dash
  1236. * N no separator
  1237. * The maximum supported length is 64 bytes of the input. Consider
  1238. * to use print_hex_dump() for the larger input.
  1239. * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  1240. * (default assumed to be phys_addr_t, passed by reference)
  1241. * - 'd[234]' For a dentry name (optionally 2-4 last components)
  1242. * - 'D[234]' Same as 'd' but for a struct file
  1243. *
  1244. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  1245. * function pointers are really function descriptors, which contain a
  1246. * pointer to the real address.
  1247. */
  1248. static noinline_for_stack
  1249. char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  1250. struct printf_spec spec)
  1251. {
  1252. int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
  1253. if (!ptr && *fmt != 'K') {
  1254. /*
  1255. * Print (null) with the same width as a pointer so it makes
  1256. * tabular output look nice.
  1257. */
  1258. if (spec.field_width == -1)
  1259. spec.field_width = default_width;
  1260. return string(buf, end, "(null)", spec);
  1261. }
  1262. switch (*fmt) {
  1263. case 'F':
  1264. case 'f':
  1265. ptr = dereference_function_descriptor(ptr);
  1266. /* Fallthrough */
  1267. case 'S':
  1268. case 's':
  1269. case 'B':
  1270. return symbol_string(buf, end, ptr, spec, fmt);
  1271. case 'R':
  1272. case 'r':
  1273. return resource_string(buf, end, ptr, spec, fmt);
  1274. case 'h':
  1275. return hex_string(buf, end, ptr, spec, fmt);
  1276. case 'b':
  1277. switch (fmt[1]) {
  1278. case 'l':
  1279. return bitmap_list_string(buf, end, ptr, spec, fmt);
  1280. default:
  1281. return bitmap_string(buf, end, ptr, spec, fmt);
  1282. }
  1283. case 'M': /* Colon separated: 00:01:02:03:04:05 */
  1284. case 'm': /* Contiguous: 000102030405 */
  1285. /* [mM]F (FDDI) */
  1286. /* [mM]R (Reverse order; Bluetooth) */
  1287. return mac_address_string(buf, end, ptr, spec, fmt);
  1288. case 'I': /* Formatted IP supported
  1289. * 4: 1.2.3.4
  1290. * 6: 0001:0203:...:0708
  1291. * 6c: 1::708 or 1::1.2.3.4
  1292. */
  1293. case 'i': /* Contiguous:
  1294. * 4: 001.002.003.004
  1295. * 6: 000102...0f
  1296. */
  1297. switch (fmt[1]) {
  1298. case '6':
  1299. return ip6_addr_string(buf, end, ptr, spec, fmt);
  1300. case '4':
  1301. return ip4_addr_string(buf, end, ptr, spec, fmt);
  1302. case 'S': {
  1303. const union {
  1304. struct sockaddr raw;
  1305. struct sockaddr_in v4;
  1306. struct sockaddr_in6 v6;
  1307. } *sa = ptr;
  1308. switch (sa->raw.sa_family) {
  1309. case AF_INET:
  1310. return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
  1311. case AF_INET6:
  1312. return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
  1313. default:
  1314. return string(buf, end, "(invalid address)", spec);
  1315. }}
  1316. }
  1317. break;
  1318. case 'E':
  1319. return escaped_string(buf, end, ptr, spec, fmt);
  1320. case 'U':
  1321. return uuid_string(buf, end, ptr, spec, fmt);
  1322. case 'V':
  1323. {
  1324. va_list va;
  1325. va_copy(va, *((struct va_format *)ptr)->va);
  1326. buf += vsnprintf(buf, end > buf ? end - buf : 0,
  1327. ((struct va_format *)ptr)->fmt, va);
  1328. va_end(va);
  1329. return buf;
  1330. }
  1331. case 'K':
  1332. /*
  1333. * %pK cannot be used in IRQ context because its test
  1334. * for CAP_SYSLOG would be meaningless.
  1335. */
  1336. if (kptr_restrict && (in_irq() || in_serving_softirq() ||
  1337. in_nmi())) {
  1338. if (spec.field_width == -1)
  1339. spec.field_width = default_width;
  1340. return string(buf, end, "pK-error", spec);
  1341. }
  1342. switch (kptr_restrict) {
  1343. case 0:
  1344. /* Always print %pK values */
  1345. break;
  1346. case 1: {
  1347. /*
  1348. * Only print the real pointer value if the current
  1349. * process has CAP_SYSLOG and is running with the
  1350. * same credentials it started with. This is because
  1351. * access to files is checked at open() time, but %pK
  1352. * checks permission at read() time. We don't want to
  1353. * leak pointer values if a binary opens a file using
  1354. * %pK and then elevates privileges before reading it.
  1355. */
  1356. const struct cred *cred = current_cred();
  1357. if (!has_capability_noaudit(current, CAP_SYSLOG) ||
  1358. !uid_eq(cred->euid, cred->uid) ||
  1359. !gid_eq(cred->egid, cred->gid))
  1360. ptr = NULL;
  1361. break;
  1362. }
  1363. case 2:
  1364. default:
  1365. /* Always print 0's for %pK */
  1366. ptr = NULL;
  1367. break;
  1368. }
  1369. break;
  1370. case 'N':
  1371. switch (fmt[1]) {
  1372. case 'F':
  1373. return netdev_feature_string(buf, end, ptr, spec);
  1374. }
  1375. break;
  1376. case 'a':
  1377. return address_val(buf, end, ptr, spec, fmt);
  1378. case 'd':
  1379. return dentry_name(buf, end, ptr, spec, fmt);
  1380. case 'D':
  1381. return dentry_name(buf, end,
  1382. ((const struct file *)ptr)->f_path.dentry,
  1383. spec, fmt);
  1384. }
  1385. spec.flags |= SMALL;
  1386. if (spec.field_width == -1) {
  1387. spec.field_width = default_width;
  1388. spec.flags |= ZEROPAD;
  1389. }
  1390. spec.base = 16;
  1391. return number(buf, end, (unsigned long) ptr, spec);
  1392. }
  1393. /*
  1394. * Helper function to decode printf style format.
  1395. * Each call decode a token from the format and return the
  1396. * number of characters read (or likely the delta where it wants
  1397. * to go on the next call).
  1398. * The decoded token is returned through the parameters
  1399. *
  1400. * 'h', 'l', or 'L' for integer fields
  1401. * 'z' support added 23/7/1999 S.H.
  1402. * 'z' changed to 'Z' --davidm 1/25/99
  1403. * 't' added for ptrdiff_t
  1404. *
  1405. * @fmt: the format string
  1406. * @type of the token returned
  1407. * @flags: various flags such as +, -, # tokens..
  1408. * @field_width: overwritten width
  1409. * @base: base of the number (octal, hex, ...)
  1410. * @precision: precision of a number
  1411. * @qualifier: qualifier of a number (long, size_t, ...)
  1412. */
  1413. static noinline_for_stack
  1414. int format_decode(const char *fmt, struct printf_spec *spec)
  1415. {
  1416. const char *start = fmt;
  1417. /* we finished early by reading the field width */
  1418. if (spec->type == FORMAT_TYPE_WIDTH) {
  1419. if (spec->field_width < 0) {
  1420. spec->field_width = -spec->field_width;
  1421. spec->flags |= LEFT;
  1422. }
  1423. spec->type = FORMAT_TYPE_NONE;
  1424. goto precision;
  1425. }
  1426. /* we finished early by reading the precision */
  1427. if (spec->type == FORMAT_TYPE_PRECISION) {
  1428. if (spec->precision < 0)
  1429. spec->precision = 0;
  1430. spec->type = FORMAT_TYPE_NONE;
  1431. goto qualifier;
  1432. }
  1433. /* By default */
  1434. spec->type = FORMAT_TYPE_NONE;
  1435. for (; *fmt ; ++fmt) {
  1436. if (*fmt == '%')
  1437. break;
  1438. }
  1439. /* Return the current non-format string */
  1440. if (fmt != start || !*fmt)
  1441. return fmt - start;
  1442. /* Process flags */
  1443. spec->flags = 0;
  1444. while (1) { /* this also skips first '%' */
  1445. bool found = true;
  1446. ++fmt;
  1447. switch (*fmt) {
  1448. case '-': spec->flags |= LEFT; break;
  1449. case '+': spec->flags |= PLUS; break;
  1450. case ' ': spec->flags |= SPACE; break;
  1451. case '#': spec->flags |= SPECIAL; break;
  1452. case '0': spec->flags |= ZEROPAD; break;
  1453. default: found = false;
  1454. }
  1455. if (!found)
  1456. break;
  1457. }
  1458. /* get field width */
  1459. spec->field_width = -1;
  1460. if (isdigit(*fmt))
  1461. spec->field_width = skip_atoi(&fmt);
  1462. else if (*fmt == '*') {
  1463. /* it's the next argument */
  1464. spec->type = FORMAT_TYPE_WIDTH;
  1465. return ++fmt - start;
  1466. }
  1467. precision:
  1468. /* get the precision */
  1469. spec->precision = -1;
  1470. if (*fmt == '.') {
  1471. ++fmt;
  1472. if (isdigit(*fmt)) {
  1473. spec->precision = skip_atoi(&fmt);
  1474. if (spec->precision < 0)
  1475. spec->precision = 0;
  1476. } else if (*fmt == '*') {
  1477. /* it's the next argument */
  1478. spec->type = FORMAT_TYPE_PRECISION;
  1479. return ++fmt - start;
  1480. }
  1481. }
  1482. qualifier:
  1483. /* get the conversion qualifier */
  1484. spec->qualifier = -1;
  1485. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  1486. _tolower(*fmt) == 'z' || *fmt == 't') {
  1487. spec->qualifier = *fmt++;
  1488. if (unlikely(spec->qualifier == *fmt)) {
  1489. if (spec->qualifier == 'l') {
  1490. spec->qualifier = 'L';
  1491. ++fmt;
  1492. } else if (spec->qualifier == 'h') {
  1493. spec->qualifier = 'H';
  1494. ++fmt;
  1495. }
  1496. }
  1497. }
  1498. /* default base */
  1499. spec->base = 10;
  1500. switch (*fmt) {
  1501. case 'c':
  1502. spec->type = FORMAT_TYPE_CHAR;
  1503. return ++fmt - start;
  1504. case 's':
  1505. spec->type = FORMAT_TYPE_STR;
  1506. return ++fmt - start;
  1507. case 'p':
  1508. spec->type = FORMAT_TYPE_PTR;
  1509. return ++fmt - start;
  1510. case '%':
  1511. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  1512. return ++fmt - start;
  1513. /* integer number formats - set up the flags and "break" */
  1514. case 'o':
  1515. spec->base = 8;
  1516. break;
  1517. case 'x':
  1518. spec->flags |= SMALL;
  1519. case 'X':
  1520. spec->base = 16;
  1521. break;
  1522. case 'd':
  1523. case 'i':
  1524. spec->flags |= SIGN;
  1525. case 'u':
  1526. break;
  1527. case 'n':
  1528. /*
  1529. * Since %n poses a greater security risk than utility, treat
  1530. * it as an invalid format specifier. Warn about its use so
  1531. * that new instances don't get added.
  1532. */
  1533. WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
  1534. /* Fall-through */
  1535. default:
  1536. spec->type = FORMAT_TYPE_INVALID;
  1537. return fmt - start;
  1538. }
  1539. if (spec->qualifier == 'L')
  1540. spec->type = FORMAT_TYPE_LONG_LONG;
  1541. else if (spec->qualifier == 'l') {
  1542. BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
  1543. spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
  1544. } else if (_tolower(spec->qualifier) == 'z') {
  1545. spec->type = FORMAT_TYPE_SIZE_T;
  1546. } else if (spec->qualifier == 't') {
  1547. spec->type = FORMAT_TYPE_PTRDIFF;
  1548. } else if (spec->qualifier == 'H') {
  1549. BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
  1550. spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
  1551. } else if (spec->qualifier == 'h') {
  1552. BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
  1553. spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
  1554. } else {
  1555. BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
  1556. spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
  1557. }
  1558. return ++fmt - start;
  1559. }
  1560. /**
  1561. * vsnprintf - Format a string and place it in a buffer
  1562. * @buf: The buffer to place the result into
  1563. * @size: The size of the buffer, including the trailing null space
  1564. * @fmt: The format string to use
  1565. * @args: Arguments for the format string
  1566. *
  1567. * This function follows C99 vsnprintf, but has some extensions:
  1568. * %pS output the name of a text symbol with offset
  1569. * %ps output the name of a text symbol without offset
  1570. * %pF output the name of a function pointer with its offset
  1571. * %pf output the name of a function pointer without its offset
  1572. * %pB output the name of a backtrace symbol with its offset
  1573. * %pR output the address range in a struct resource with decoded flags
  1574. * %pr output the address range in a struct resource with raw flags
  1575. * %pb output the bitmap with field width as the number of bits
  1576. * %pbl output the bitmap as range list with field width as the number of bits
  1577. * %pM output a 6-byte MAC address with colons
  1578. * %pMR output a 6-byte MAC address with colons in reversed order
  1579. * %pMF output a 6-byte MAC address with dashes
  1580. * %pm output a 6-byte MAC address without colons
  1581. * %pmR output a 6-byte MAC address without colons in reversed order
  1582. * %pI4 print an IPv4 address without leading zeros
  1583. * %pi4 print an IPv4 address with leading zeros
  1584. * %pI6 print an IPv6 address with colons
  1585. * %pi6 print an IPv6 address without colons
  1586. * %pI6c print an IPv6 address as specified by RFC 5952
  1587. * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
  1588. * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
  1589. * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
  1590. * case.
  1591. * %*pE[achnops] print an escaped buffer
  1592. * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
  1593. * bytes of the input)
  1594. * %n is ignored
  1595. *
  1596. * ** Please update Documentation/printk-formats.txt when making changes **
  1597. *
  1598. * The return value is the number of characters which would
  1599. * be generated for the given input, excluding the trailing
  1600. * '\0', as per ISO C99. If you want to have the exact
  1601. * number of characters written into @buf as return value
  1602. * (not including the trailing '\0'), use vscnprintf(). If the
  1603. * return is greater than or equal to @size, the resulting
  1604. * string is truncated.
  1605. *
  1606. * If you're not already dealing with a va_list consider using snprintf().
  1607. */
  1608. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1609. {
  1610. unsigned long long num;
  1611. char *str, *end;
  1612. struct printf_spec spec = {0};
  1613. /* Reject out-of-range values early. Large positive sizes are
  1614. used for unknown buffer sizes. */
  1615. if (WARN_ON_ONCE(size > INT_MAX))
  1616. return 0;
  1617. str = buf;
  1618. end = buf + size;
  1619. /* Make sure end is always >= buf */
  1620. if (end < buf) {
  1621. end = ((void *)-1);
  1622. size = end - buf;
  1623. }
  1624. while (*fmt) {
  1625. const char *old_fmt = fmt;
  1626. int read = format_decode(fmt, &spec);
  1627. fmt += read;
  1628. switch (spec.type) {
  1629. case FORMAT_TYPE_NONE: {
  1630. int copy = read;
  1631. if (str < end) {
  1632. if (copy > end - str)
  1633. copy = end - str;
  1634. memcpy(str, old_fmt, copy);
  1635. }
  1636. str += read;
  1637. break;
  1638. }
  1639. case FORMAT_TYPE_WIDTH:
  1640. spec.field_width = va_arg(args, int);
  1641. break;
  1642. case FORMAT_TYPE_PRECISION:
  1643. spec.precision = va_arg(args, int);
  1644. break;
  1645. case FORMAT_TYPE_CHAR: {
  1646. char c;
  1647. if (!(spec.flags & LEFT)) {
  1648. while (--spec.field_width > 0) {
  1649. if (str < end)
  1650. *str = ' ';
  1651. ++str;
  1652. }
  1653. }
  1654. c = (unsigned char) va_arg(args, int);
  1655. if (str < end)
  1656. *str = c;
  1657. ++str;
  1658. while (--spec.field_width > 0) {
  1659. if (str < end)
  1660. *str = ' ';
  1661. ++str;
  1662. }
  1663. break;
  1664. }
  1665. case FORMAT_TYPE_STR:
  1666. str = string(str, end, va_arg(args, char *), spec);
  1667. break;
  1668. case FORMAT_TYPE_PTR:
  1669. str = pointer(fmt, str, end, va_arg(args, void *),
  1670. spec);
  1671. while (isalnum(*fmt))
  1672. fmt++;
  1673. break;
  1674. case FORMAT_TYPE_PERCENT_CHAR:
  1675. if (str < end)
  1676. *str = '%';
  1677. ++str;
  1678. break;
  1679. case FORMAT_TYPE_INVALID:
  1680. if (str < end)
  1681. *str = '%';
  1682. ++str;
  1683. break;
  1684. default:
  1685. switch (spec.type) {
  1686. case FORMAT_TYPE_LONG_LONG:
  1687. num = va_arg(args, long long);
  1688. break;
  1689. case FORMAT_TYPE_ULONG:
  1690. num = va_arg(args, unsigned long);
  1691. break;
  1692. case FORMAT_TYPE_LONG:
  1693. num = va_arg(args, long);
  1694. break;
  1695. case FORMAT_TYPE_SIZE_T:
  1696. if (spec.flags & SIGN)
  1697. num = va_arg(args, ssize_t);
  1698. else
  1699. num = va_arg(args, size_t);
  1700. break;
  1701. case FORMAT_TYPE_PTRDIFF:
  1702. num = va_arg(args, ptrdiff_t);
  1703. break;
  1704. case FORMAT_TYPE_UBYTE:
  1705. num = (unsigned char) va_arg(args, int);
  1706. break;
  1707. case FORMAT_TYPE_BYTE:
  1708. num = (signed char) va_arg(args, int);
  1709. break;
  1710. case FORMAT_TYPE_USHORT:
  1711. num = (unsigned short) va_arg(args, int);
  1712. break;
  1713. case FORMAT_TYPE_SHORT:
  1714. num = (short) va_arg(args, int);
  1715. break;
  1716. case FORMAT_TYPE_INT:
  1717. num = (int) va_arg(args, int);
  1718. break;
  1719. default:
  1720. num = va_arg(args, unsigned int);
  1721. }
  1722. str = number(str, end, num, spec);
  1723. }
  1724. }
  1725. if (size > 0) {
  1726. if (str < end)
  1727. *str = '\0';
  1728. else
  1729. end[-1] = '\0';
  1730. }
  1731. /* the trailing null byte doesn't count towards the total */
  1732. return str-buf;
  1733. }
  1734. EXPORT_SYMBOL(vsnprintf);
  1735. /**
  1736. * vscnprintf - Format a string and place it in a buffer
  1737. * @buf: The buffer to place the result into
  1738. * @size: The size of the buffer, including the trailing null space
  1739. * @fmt: The format string to use
  1740. * @args: Arguments for the format string
  1741. *
  1742. * The return value is the number of characters which have been written into
  1743. * the @buf not including the trailing '\0'. If @size is == 0 the function
  1744. * returns 0.
  1745. *
  1746. * If you're not already dealing with a va_list consider using scnprintf().
  1747. *
  1748. * See the vsnprintf() documentation for format string extensions over C99.
  1749. */
  1750. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1751. {
  1752. int i;
  1753. i = vsnprintf(buf, size, fmt, args);
  1754. if (likely(i < size))
  1755. return i;
  1756. if (size != 0)
  1757. return size - 1;
  1758. return 0;
  1759. }
  1760. EXPORT_SYMBOL(vscnprintf);
  1761. /**
  1762. * snprintf - Format a string and place it in a buffer
  1763. * @buf: The buffer to place the result into
  1764. * @size: The size of the buffer, including the trailing null space
  1765. * @fmt: The format string to use
  1766. * @...: Arguments for the format string
  1767. *
  1768. * The return value is the number of characters which would be
  1769. * generated for the given input, excluding the trailing null,
  1770. * as per ISO C99. If the return is greater than or equal to
  1771. * @size, the resulting string is truncated.
  1772. *
  1773. * See the vsnprintf() documentation for format string extensions over C99.
  1774. */
  1775. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1776. {
  1777. va_list args;
  1778. int i;
  1779. va_start(args, fmt);
  1780. i = vsnprintf(buf, size, fmt, args);
  1781. va_end(args);
  1782. return i;
  1783. }
  1784. EXPORT_SYMBOL(snprintf);
  1785. /**
  1786. * scnprintf - Format a string and place it in a buffer
  1787. * @buf: The buffer to place the result into
  1788. * @size: The size of the buffer, including the trailing null space
  1789. * @fmt: The format string to use
  1790. * @...: Arguments for the format string
  1791. *
  1792. * The return value is the number of characters written into @buf not including
  1793. * the trailing '\0'. If @size is == 0 the function returns 0.
  1794. */
  1795. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  1796. {
  1797. va_list args;
  1798. int i;
  1799. va_start(args, fmt);
  1800. i = vscnprintf(buf, size, fmt, args);
  1801. va_end(args);
  1802. return i;
  1803. }
  1804. EXPORT_SYMBOL(scnprintf);
  1805. /**
  1806. * vsprintf - Format a string and place it in a buffer
  1807. * @buf: The buffer to place the result into
  1808. * @fmt: The format string to use
  1809. * @args: Arguments for the format string
  1810. *
  1811. * The function returns the number of characters written
  1812. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1813. * buffer overflows.
  1814. *
  1815. * If you're not already dealing with a va_list consider using sprintf().
  1816. *
  1817. * See the vsnprintf() documentation for format string extensions over C99.
  1818. */
  1819. int vsprintf(char *buf, const char *fmt, va_list args)
  1820. {
  1821. return vsnprintf(buf, INT_MAX, fmt, args);
  1822. }
  1823. EXPORT_SYMBOL(vsprintf);
  1824. /**
  1825. * sprintf - Format a string and place it in a buffer
  1826. * @buf: The buffer to place the result into
  1827. * @fmt: The format string to use
  1828. * @...: Arguments for the format string
  1829. *
  1830. * The function returns the number of characters written
  1831. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1832. * buffer overflows.
  1833. *
  1834. * See the vsnprintf() documentation for format string extensions over C99.
  1835. */
  1836. int sprintf(char *buf, const char *fmt, ...)
  1837. {
  1838. va_list args;
  1839. int i;
  1840. va_start(args, fmt);
  1841. i = vsnprintf(buf, INT_MAX, fmt, args);
  1842. va_end(args);
  1843. return i;
  1844. }
  1845. EXPORT_SYMBOL(sprintf);
  1846. #ifdef CONFIG_BINARY_PRINTF
  1847. /*
  1848. * bprintf service:
  1849. * vbin_printf() - VA arguments to binary data
  1850. * bstr_printf() - Binary data to text string
  1851. */
  1852. /**
  1853. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1854. * @bin_buf: The buffer to place args' binary value
  1855. * @size: The size of the buffer(by words(32bits), not characters)
  1856. * @fmt: The format string to use
  1857. * @args: Arguments for the format string
  1858. *
  1859. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1860. * is skipped.
  1861. *
  1862. * The return value is the number of words(32bits) which would be generated for
  1863. * the given input.
  1864. *
  1865. * NOTE:
  1866. * If the return value is greater than @size, the resulting bin_buf is NOT
  1867. * valid for bstr_printf().
  1868. */
  1869. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1870. {
  1871. struct printf_spec spec = {0};
  1872. char *str, *end;
  1873. str = (char *)bin_buf;
  1874. end = (char *)(bin_buf + size);
  1875. #define save_arg(type) \
  1876. do { \
  1877. if (sizeof(type) == 8) { \
  1878. unsigned long long value; \
  1879. str = PTR_ALIGN(str, sizeof(u32)); \
  1880. value = va_arg(args, unsigned long long); \
  1881. if (str + sizeof(type) <= end) { \
  1882. *(u32 *)str = *(u32 *)&value; \
  1883. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1884. } \
  1885. } else { \
  1886. unsigned long value; \
  1887. str = PTR_ALIGN(str, sizeof(type)); \
  1888. value = va_arg(args, int); \
  1889. if (str + sizeof(type) <= end) \
  1890. *(typeof(type) *)str = (type)value; \
  1891. } \
  1892. str += sizeof(type); \
  1893. } while (0)
  1894. while (*fmt) {
  1895. int read = format_decode(fmt, &spec);
  1896. fmt += read;
  1897. switch (spec.type) {
  1898. case FORMAT_TYPE_NONE:
  1899. case FORMAT_TYPE_INVALID:
  1900. case FORMAT_TYPE_PERCENT_CHAR:
  1901. break;
  1902. case FORMAT_TYPE_WIDTH:
  1903. case FORMAT_TYPE_PRECISION:
  1904. save_arg(int);
  1905. break;
  1906. case FORMAT_TYPE_CHAR:
  1907. save_arg(char);
  1908. break;
  1909. case FORMAT_TYPE_STR: {
  1910. const char *save_str = va_arg(args, char *);
  1911. size_t len;
  1912. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1913. || (unsigned long)save_str < PAGE_SIZE)
  1914. save_str = "(null)";
  1915. len = strlen(save_str) + 1;
  1916. if (str + len < end)
  1917. memcpy(str, save_str, len);
  1918. str += len;
  1919. break;
  1920. }
  1921. case FORMAT_TYPE_PTR:
  1922. save_arg(void *);
  1923. /* skip all alphanumeric pointer suffixes */
  1924. while (isalnum(*fmt))
  1925. fmt++;
  1926. break;
  1927. default:
  1928. switch (spec.type) {
  1929. case FORMAT_TYPE_LONG_LONG:
  1930. save_arg(long long);
  1931. break;
  1932. case FORMAT_TYPE_ULONG:
  1933. case FORMAT_TYPE_LONG:
  1934. save_arg(unsigned long);
  1935. break;
  1936. case FORMAT_TYPE_SIZE_T:
  1937. save_arg(size_t);
  1938. break;
  1939. case FORMAT_TYPE_PTRDIFF:
  1940. save_arg(ptrdiff_t);
  1941. break;
  1942. case FORMAT_TYPE_UBYTE:
  1943. case FORMAT_TYPE_BYTE:
  1944. save_arg(char);
  1945. break;
  1946. case FORMAT_TYPE_USHORT:
  1947. case FORMAT_TYPE_SHORT:
  1948. save_arg(short);
  1949. break;
  1950. default:
  1951. save_arg(int);
  1952. }
  1953. }
  1954. }
  1955. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1956. #undef save_arg
  1957. }
  1958. EXPORT_SYMBOL_GPL(vbin_printf);
  1959. /**
  1960. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1961. * @buf: The buffer to place the result into
  1962. * @size: The size of the buffer, including the trailing null space
  1963. * @fmt: The format string to use
  1964. * @bin_buf: Binary arguments for the format string
  1965. *
  1966. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1967. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1968. * a binary buffer that generated by vbin_printf.
  1969. *
  1970. * The format follows C99 vsnprintf, but has some extensions:
  1971. * see vsnprintf comment for details.
  1972. *
  1973. * The return value is the number of characters which would
  1974. * be generated for the given input, excluding the trailing
  1975. * '\0', as per ISO C99. If you want to have the exact
  1976. * number of characters written into @buf as return value
  1977. * (not including the trailing '\0'), use vscnprintf(). If the
  1978. * return is greater than or equal to @size, the resulting
  1979. * string is truncated.
  1980. */
  1981. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1982. {
  1983. struct printf_spec spec = {0};
  1984. char *str, *end;
  1985. const char *args = (const char *)bin_buf;
  1986. if (WARN_ON_ONCE((int) size < 0))
  1987. return 0;
  1988. str = buf;
  1989. end = buf + size;
  1990. #define get_arg(type) \
  1991. ({ \
  1992. typeof(type) value; \
  1993. if (sizeof(type) == 8) { \
  1994. args = PTR_ALIGN(args, sizeof(u32)); \
  1995. *(u32 *)&value = *(u32 *)args; \
  1996. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1997. } else { \
  1998. args = PTR_ALIGN(args, sizeof(type)); \
  1999. value = *(typeof(type) *)args; \
  2000. } \
  2001. args += sizeof(type); \
  2002. value; \
  2003. })
  2004. /* Make sure end is always >= buf */
  2005. if (end < buf) {
  2006. end = ((void *)-1);
  2007. size = end - buf;
  2008. }
  2009. while (*fmt) {
  2010. const char *old_fmt = fmt;
  2011. int read = format_decode(fmt, &spec);
  2012. fmt += read;
  2013. switch (spec.type) {
  2014. case FORMAT_TYPE_NONE: {
  2015. int copy = read;
  2016. if (str < end) {
  2017. if (copy > end - str)
  2018. copy = end - str;
  2019. memcpy(str, old_fmt, copy);
  2020. }
  2021. str += read;
  2022. break;
  2023. }
  2024. case FORMAT_TYPE_WIDTH:
  2025. spec.field_width = get_arg(int);
  2026. break;
  2027. case FORMAT_TYPE_PRECISION:
  2028. spec.precision = get_arg(int);
  2029. break;
  2030. case FORMAT_TYPE_CHAR: {
  2031. char c;
  2032. if (!(spec.flags & LEFT)) {
  2033. while (--spec.field_width > 0) {
  2034. if (str < end)
  2035. *str = ' ';
  2036. ++str;
  2037. }
  2038. }
  2039. c = (unsigned char) get_arg(char);
  2040. if (str < end)
  2041. *str = c;
  2042. ++str;
  2043. while (--spec.field_width > 0) {
  2044. if (str < end)
  2045. *str = ' ';
  2046. ++str;
  2047. }
  2048. break;
  2049. }
  2050. case FORMAT_TYPE_STR: {
  2051. const char *str_arg = args;
  2052. args += strlen(str_arg) + 1;
  2053. str = string(str, end, (char *)str_arg, spec);
  2054. break;
  2055. }
  2056. case FORMAT_TYPE_PTR:
  2057. str = pointer(fmt, str, end, get_arg(void *), spec);
  2058. while (isalnum(*fmt))
  2059. fmt++;
  2060. break;
  2061. case FORMAT_TYPE_PERCENT_CHAR:
  2062. case FORMAT_TYPE_INVALID:
  2063. if (str < end)
  2064. *str = '%';
  2065. ++str;
  2066. break;
  2067. default: {
  2068. unsigned long long num;
  2069. switch (spec.type) {
  2070. case FORMAT_TYPE_LONG_LONG:
  2071. num = get_arg(long long);
  2072. break;
  2073. case FORMAT_TYPE_ULONG:
  2074. case FORMAT_TYPE_LONG:
  2075. num = get_arg(unsigned long);
  2076. break;
  2077. case FORMAT_TYPE_SIZE_T:
  2078. num = get_arg(size_t);
  2079. break;
  2080. case FORMAT_TYPE_PTRDIFF:
  2081. num = get_arg(ptrdiff_t);
  2082. break;
  2083. case FORMAT_TYPE_UBYTE:
  2084. num = get_arg(unsigned char);
  2085. break;
  2086. case FORMAT_TYPE_BYTE:
  2087. num = get_arg(signed char);
  2088. break;
  2089. case FORMAT_TYPE_USHORT:
  2090. num = get_arg(unsigned short);
  2091. break;
  2092. case FORMAT_TYPE_SHORT:
  2093. num = get_arg(short);
  2094. break;
  2095. case FORMAT_TYPE_UINT:
  2096. num = get_arg(unsigned int);
  2097. break;
  2098. default:
  2099. num = get_arg(int);
  2100. }
  2101. str = number(str, end, num, spec);
  2102. } /* default: */
  2103. } /* switch(spec.type) */
  2104. } /* while(*fmt) */
  2105. if (size > 0) {
  2106. if (str < end)
  2107. *str = '\0';
  2108. else
  2109. end[-1] = '\0';
  2110. }
  2111. #undef get_arg
  2112. /* the trailing null byte doesn't count towards the total */
  2113. return str - buf;
  2114. }
  2115. EXPORT_SYMBOL_GPL(bstr_printf);
  2116. /**
  2117. * bprintf - Parse a format string and place args' binary value in a buffer
  2118. * @bin_buf: The buffer to place args' binary value
  2119. * @size: The size of the buffer(by words(32bits), not characters)
  2120. * @fmt: The format string to use
  2121. * @...: Arguments for the format string
  2122. *
  2123. * The function returns the number of words(u32) written
  2124. * into @bin_buf.
  2125. */
  2126. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  2127. {
  2128. va_list args;
  2129. int ret;
  2130. va_start(args, fmt);
  2131. ret = vbin_printf(bin_buf, size, fmt, args);
  2132. va_end(args);
  2133. return ret;
  2134. }
  2135. EXPORT_SYMBOL_GPL(bprintf);
  2136. #endif /* CONFIG_BINARY_PRINTF */
  2137. /**
  2138. * vsscanf - Unformat a buffer into a list of arguments
  2139. * @buf: input buffer
  2140. * @fmt: format of buffer
  2141. * @args: arguments
  2142. */
  2143. int vsscanf(const char *buf, const char *fmt, va_list args)
  2144. {
  2145. const char *str = buf;
  2146. char *next;
  2147. char digit;
  2148. int num = 0;
  2149. u8 qualifier;
  2150. unsigned int base;
  2151. union {
  2152. long long s;
  2153. unsigned long long u;
  2154. } val;
  2155. s16 field_width;
  2156. bool is_sign;
  2157. while (*fmt) {
  2158. /* skip any white space in format */
  2159. /* white space in format matchs any amount of
  2160. * white space, including none, in the input.
  2161. */
  2162. if (isspace(*fmt)) {
  2163. fmt = skip_spaces(++fmt);
  2164. str = skip_spaces(str);
  2165. }
  2166. /* anything that is not a conversion must match exactly */
  2167. if (*fmt != '%' && *fmt) {
  2168. if (*fmt++ != *str++)
  2169. break;
  2170. continue;
  2171. }
  2172. if (!*fmt)
  2173. break;
  2174. ++fmt;
  2175. /* skip this conversion.
  2176. * advance both strings to next white space
  2177. */
  2178. if (*fmt == '*') {
  2179. if (!*str)
  2180. break;
  2181. while (!isspace(*fmt) && *fmt != '%' && *fmt)
  2182. fmt++;
  2183. while (!isspace(*str) && *str)
  2184. str++;
  2185. continue;
  2186. }
  2187. /* get field width */
  2188. field_width = -1;
  2189. if (isdigit(*fmt)) {
  2190. field_width = skip_atoi(&fmt);
  2191. if (field_width <= 0)
  2192. break;
  2193. }
  2194. /* get conversion qualifier */
  2195. qualifier = -1;
  2196. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  2197. _tolower(*fmt) == 'z') {
  2198. qualifier = *fmt++;
  2199. if (unlikely(qualifier == *fmt)) {
  2200. if (qualifier == 'h') {
  2201. qualifier = 'H';
  2202. fmt++;
  2203. } else if (qualifier == 'l') {
  2204. qualifier = 'L';
  2205. fmt++;
  2206. }
  2207. }
  2208. }
  2209. if (!*fmt)
  2210. break;
  2211. if (*fmt == 'n') {
  2212. /* return number of characters read so far */
  2213. *va_arg(args, int *) = str - buf;
  2214. ++fmt;
  2215. continue;
  2216. }
  2217. if (!*str)
  2218. break;
  2219. base = 10;
  2220. is_sign = false;
  2221. switch (*fmt++) {
  2222. case 'c':
  2223. {
  2224. char *s = (char *)va_arg(args, char*);
  2225. if (field_width == -1)
  2226. field_width = 1;
  2227. do {
  2228. *s++ = *str++;
  2229. } while (--field_width > 0 && *str);
  2230. num++;
  2231. }
  2232. continue;
  2233. case 's':
  2234. {
  2235. char *s = (char *)va_arg(args, char *);
  2236. if (field_width == -1)
  2237. field_width = SHRT_MAX;
  2238. /* first, skip leading white space in buffer */
  2239. str = skip_spaces(str);
  2240. /* now copy until next white space */
  2241. while (*str && !isspace(*str) && field_width--)
  2242. *s++ = *str++;
  2243. *s = '\0';
  2244. num++;
  2245. }
  2246. continue;
  2247. case 'o':
  2248. base = 8;
  2249. break;
  2250. case 'x':
  2251. case 'X':
  2252. base = 16;
  2253. break;
  2254. case 'i':
  2255. base = 0;
  2256. case 'd':
  2257. is_sign = true;
  2258. case 'u':
  2259. break;
  2260. case '%':
  2261. /* looking for '%' in str */
  2262. if (*str++ != '%')
  2263. return num;
  2264. continue;
  2265. default:
  2266. /* invalid format; stop here */
  2267. return num;
  2268. }
  2269. /* have some sort of integer conversion.
  2270. * first, skip white space in buffer.
  2271. */
  2272. str = skip_spaces(str);
  2273. digit = *str;
  2274. if (is_sign && digit == '-')
  2275. digit = *(str + 1);
  2276. if (!digit
  2277. || (base == 16 && !isxdigit(digit))
  2278. || (base == 10 && !isdigit(digit))
  2279. || (base == 8 && (!isdigit(digit) || digit > '7'))
  2280. || (base == 0 && !isdigit(digit)))
  2281. break;
  2282. if (is_sign)
  2283. val.s = qualifier != 'L' ?
  2284. simple_strtol(str, &next, base) :
  2285. simple_strtoll(str, &next, base);
  2286. else
  2287. val.u = qualifier != 'L' ?
  2288. simple_strtoul(str, &next, base) :
  2289. simple_strtoull(str, &next, base);
  2290. if (field_width > 0 && next - str > field_width) {
  2291. if (base == 0)
  2292. _parse_integer_fixup_radix(str, &base);
  2293. while (next - str > field_width) {
  2294. if (is_sign)
  2295. val.s = div_s64(val.s, base);
  2296. else
  2297. val.u = div_u64(val.u, base);
  2298. --next;
  2299. }
  2300. }
  2301. switch (qualifier) {
  2302. case 'H': /* that's 'hh' in format */
  2303. if (is_sign)
  2304. *va_arg(args, signed char *) = val.s;
  2305. else
  2306. *va_arg(args, unsigned char *) = val.u;
  2307. break;
  2308. case 'h':
  2309. if (is_sign)
  2310. *va_arg(args, short *) = val.s;
  2311. else
  2312. *va_arg(args, unsigned short *) = val.u;
  2313. break;
  2314. case 'l':
  2315. if (is_sign)
  2316. *va_arg(args, long *) = val.s;
  2317. else
  2318. *va_arg(args, unsigned long *) = val.u;
  2319. break;
  2320. case 'L':
  2321. if (is_sign)
  2322. *va_arg(args, long long *) = val.s;
  2323. else
  2324. *va_arg(args, unsigned long long *) = val.u;
  2325. break;
  2326. case 'Z':
  2327. case 'z':
  2328. *va_arg(args, size_t *) = val.u;
  2329. break;
  2330. default:
  2331. if (is_sign)
  2332. *va_arg(args, int *) = val.s;
  2333. else
  2334. *va_arg(args, unsigned int *) = val.u;
  2335. break;
  2336. }
  2337. num++;
  2338. if (!next)
  2339. break;
  2340. str = next;
  2341. }
  2342. return num;
  2343. }
  2344. EXPORT_SYMBOL(vsscanf);
  2345. /**
  2346. * sscanf - Unformat a buffer into a list of arguments
  2347. * @buf: input buffer
  2348. * @fmt: formatting of buffer
  2349. * @...: resulting arguments
  2350. */
  2351. int sscanf(const char *buf, const char *fmt, ...)
  2352. {
  2353. va_list args;
  2354. int i;
  2355. va_start(args, fmt);
  2356. i = vsscanf(buf, fmt, args);
  2357. va_end(args);
  2358. return i;
  2359. }
  2360. EXPORT_SYMBOL(sscanf);