vsprintf.c 62 KB

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