vsprintf.c 62 KB

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