vsprintf.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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>
  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/uaccess.h>
  24. #include <linux/ioport.h>
  25. #include <net/addrconf.h>
  26. #include <asm/page.h> /* for PAGE_SIZE */
  27. #include <asm/div64.h>
  28. #include <asm/sections.h> /* for dereference_function_descriptor() */
  29. #include "kstrtox.h"
  30. /**
  31. * simple_strtoull - convert a string to an unsigned long long
  32. * @cp: The start of the string
  33. * @endp: A pointer to the end of the parsed string will be placed here
  34. * @base: The number base to use
  35. */
  36. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  37. {
  38. unsigned long long result;
  39. unsigned int rv;
  40. cp = _parse_integer_fixup_radix(cp, &base);
  41. rv = _parse_integer(cp, base, &result);
  42. /* FIXME */
  43. cp += (rv & ~KSTRTOX_OVERFLOW);
  44. if (endp)
  45. *endp = (char *)cp;
  46. return result;
  47. }
  48. EXPORT_SYMBOL(simple_strtoull);
  49. /**
  50. * simple_strtoul - convert a string to an unsigned long
  51. * @cp: The start of the string
  52. * @endp: A pointer to the end of the parsed string will be placed here
  53. * @base: The number base to use
  54. */
  55. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  56. {
  57. return simple_strtoull(cp, endp, base);
  58. }
  59. EXPORT_SYMBOL(simple_strtoul);
  60. /**
  61. * simple_strtol - convert a string to a signed long
  62. * @cp: The start of the string
  63. * @endp: A pointer to the end of the parsed string will be placed here
  64. * @base: The number base to use
  65. */
  66. long simple_strtol(const char *cp, char **endp, unsigned int base)
  67. {
  68. if (*cp == '-')
  69. return -simple_strtoul(cp + 1, endp, base);
  70. return simple_strtoul(cp, endp, base);
  71. }
  72. EXPORT_SYMBOL(simple_strtol);
  73. /**
  74. * simple_strtoll - convert a string to a signed long long
  75. * @cp: The start of the string
  76. * @endp: A pointer to the end of the parsed string will be placed here
  77. * @base: The number base to use
  78. */
  79. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  80. {
  81. if (*cp == '-')
  82. return -simple_strtoull(cp + 1, endp, base);
  83. return simple_strtoull(cp, endp, base);
  84. }
  85. EXPORT_SYMBOL(simple_strtoll);
  86. static noinline_for_stack
  87. int skip_atoi(const char **s)
  88. {
  89. int i = 0;
  90. while (isdigit(**s))
  91. i = i*10 + *((*s)++) - '0';
  92. return i;
  93. }
  94. /* Decimal conversion is by far the most typical, and is used
  95. * for /proc and /sys data. This directly impacts e.g. top performance
  96. * with many processes running. We optimize it for speed
  97. * using code from
  98. * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
  99. * (with permission from the author, Douglas W. Jones). */
  100. /* Formats correctly any integer in [0,99999].
  101. * Outputs from one to five digits depending on input.
  102. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
  103. static noinline_for_stack
  104. char *put_dec_trunc(char *buf, unsigned q)
  105. {
  106. unsigned d3, d2, d1, d0;
  107. d1 = (q>>4) & 0xf;
  108. d2 = (q>>8) & 0xf;
  109. d3 = (q>>12);
  110. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  111. q = (d0 * 0xcd) >> 11;
  112. d0 = d0 - 10*q;
  113. *buf++ = d0 + '0'; /* least significant digit */
  114. d1 = q + 9*d3 + 5*d2 + d1;
  115. if (d1 != 0) {
  116. q = (d1 * 0xcd) >> 11;
  117. d1 = d1 - 10*q;
  118. *buf++ = d1 + '0'; /* next digit */
  119. d2 = q + 2*d2;
  120. if ((d2 != 0) || (d3 != 0)) {
  121. q = (d2 * 0xd) >> 7;
  122. d2 = d2 - 10*q;
  123. *buf++ = d2 + '0'; /* next digit */
  124. d3 = q + 4*d3;
  125. if (d3 != 0) {
  126. q = (d3 * 0xcd) >> 11;
  127. d3 = d3 - 10*q;
  128. *buf++ = d3 + '0'; /* next digit */
  129. if (q != 0)
  130. *buf++ = q + '0'; /* most sign. digit */
  131. }
  132. }
  133. }
  134. return buf;
  135. }
  136. /* Same with if's removed. Always emits five digits */
  137. static noinline_for_stack
  138. char *put_dec_full(char *buf, unsigned q)
  139. {
  140. /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
  141. /* but anyway, gcc produces better code with full-sized ints */
  142. unsigned d3, d2, d1, d0;
  143. d1 = (q>>4) & 0xf;
  144. d2 = (q>>8) & 0xf;
  145. d3 = (q>>12);
  146. /*
  147. * Possible ways to approx. divide by 10
  148. * gcc -O2 replaces multiply with shifts and adds
  149. * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
  150. * (x * 0x67) >> 10: 1100111
  151. * (x * 0x34) >> 9: 110100 - same
  152. * (x * 0x1a) >> 8: 11010 - same
  153. * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
  154. */
  155. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  156. q = (d0 * 0xcd) >> 11;
  157. d0 = d0 - 10*q;
  158. *buf++ = d0 + '0';
  159. d1 = q + 9*d3 + 5*d2 + d1;
  160. q = (d1 * 0xcd) >> 11;
  161. d1 = d1 - 10*q;
  162. *buf++ = d1 + '0';
  163. d2 = q + 2*d2;
  164. q = (d2 * 0xd) >> 7;
  165. d2 = d2 - 10*q;
  166. *buf++ = d2 + '0';
  167. d3 = q + 4*d3;
  168. q = (d3 * 0xcd) >> 11; /* - shorter code */
  169. /* q = (d3 * 0x67) >> 10; - would also work */
  170. d3 = d3 - 10*q;
  171. *buf++ = d3 + '0';
  172. *buf++ = q + '0';
  173. return buf;
  174. }
  175. /* No inlining helps gcc to use registers better */
  176. static noinline_for_stack
  177. char *put_dec(char *buf, unsigned long long num)
  178. {
  179. while (1) {
  180. unsigned rem;
  181. if (num < 100000)
  182. return put_dec_trunc(buf, num);
  183. rem = do_div(num, 100000);
  184. buf = put_dec_full(buf, rem);
  185. }
  186. }
  187. #define ZEROPAD 1 /* pad with zero */
  188. #define SIGN 2 /* unsigned/signed long */
  189. #define PLUS 4 /* show plus */
  190. #define SPACE 8 /* space if plus */
  191. #define LEFT 16 /* left justified */
  192. #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
  193. #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
  194. enum format_type {
  195. FORMAT_TYPE_NONE, /* Just a string part */
  196. FORMAT_TYPE_WIDTH,
  197. FORMAT_TYPE_PRECISION,
  198. FORMAT_TYPE_CHAR,
  199. FORMAT_TYPE_STR,
  200. FORMAT_TYPE_PTR,
  201. FORMAT_TYPE_PERCENT_CHAR,
  202. FORMAT_TYPE_INVALID,
  203. FORMAT_TYPE_LONG_LONG,
  204. FORMAT_TYPE_ULONG,
  205. FORMAT_TYPE_LONG,
  206. FORMAT_TYPE_UBYTE,
  207. FORMAT_TYPE_BYTE,
  208. FORMAT_TYPE_USHORT,
  209. FORMAT_TYPE_SHORT,
  210. FORMAT_TYPE_UINT,
  211. FORMAT_TYPE_INT,
  212. FORMAT_TYPE_NRCHARS,
  213. FORMAT_TYPE_SIZE_T,
  214. FORMAT_TYPE_PTRDIFF
  215. };
  216. struct printf_spec {
  217. u8 type; /* format_type enum */
  218. u8 flags; /* flags to number() */
  219. u8 base; /* number base, 8, 10 or 16 only */
  220. u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
  221. s16 field_width; /* width of output field */
  222. s16 precision; /* # of digits/chars */
  223. };
  224. static noinline_for_stack
  225. char *number(char *buf, char *end, unsigned long long num,
  226. struct printf_spec spec)
  227. {
  228. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  229. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  230. char tmp[66];
  231. char sign;
  232. char locase;
  233. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  234. int i;
  235. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  236. * produces same digits or (maybe lowercased) letters */
  237. locase = (spec.flags & SMALL);
  238. if (spec.flags & LEFT)
  239. spec.flags &= ~ZEROPAD;
  240. sign = 0;
  241. if (spec.flags & SIGN) {
  242. if ((signed long long)num < 0) {
  243. sign = '-';
  244. num = -(signed long long)num;
  245. spec.field_width--;
  246. } else if (spec.flags & PLUS) {
  247. sign = '+';
  248. spec.field_width--;
  249. } else if (spec.flags & SPACE) {
  250. sign = ' ';
  251. spec.field_width--;
  252. }
  253. }
  254. if (need_pfx) {
  255. spec.field_width--;
  256. if (spec.base == 16)
  257. spec.field_width--;
  258. }
  259. /* generate full string in tmp[], in reverse order */
  260. i = 0;
  261. if (num == 0)
  262. tmp[i++] = '0';
  263. /* Generic code, for any base:
  264. else do {
  265. tmp[i++] = (digits[do_div(num,base)] | locase);
  266. } while (num != 0);
  267. */
  268. else if (spec.base != 10) { /* 8 or 16 */
  269. int mask = spec.base - 1;
  270. int shift = 3;
  271. if (spec.base == 16)
  272. shift = 4;
  273. do {
  274. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  275. num >>= shift;
  276. } while (num);
  277. } else { /* base 10 */
  278. i = put_dec(tmp, num) - tmp;
  279. }
  280. /* printing 100 using %2d gives "100", not "00" */
  281. if (i > spec.precision)
  282. spec.precision = i;
  283. /* leading space padding */
  284. spec.field_width -= spec.precision;
  285. if (!(spec.flags & (ZEROPAD+LEFT))) {
  286. while (--spec.field_width >= 0) {
  287. if (buf < end)
  288. *buf = ' ';
  289. ++buf;
  290. }
  291. }
  292. /* sign */
  293. if (sign) {
  294. if (buf < end)
  295. *buf = sign;
  296. ++buf;
  297. }
  298. /* "0x" / "0" prefix */
  299. if (need_pfx) {
  300. if (buf < end)
  301. *buf = '0';
  302. ++buf;
  303. if (spec.base == 16) {
  304. if (buf < end)
  305. *buf = ('X' | locase);
  306. ++buf;
  307. }
  308. }
  309. /* zero or space padding */
  310. if (!(spec.flags & LEFT)) {
  311. char c = (spec.flags & ZEROPAD) ? '0' : ' ';
  312. while (--spec.field_width >= 0) {
  313. if (buf < end)
  314. *buf = c;
  315. ++buf;
  316. }
  317. }
  318. /* hmm even more zero padding? */
  319. while (i <= --spec.precision) {
  320. if (buf < end)
  321. *buf = '0';
  322. ++buf;
  323. }
  324. /* actual digits of result */
  325. while (--i >= 0) {
  326. if (buf < end)
  327. *buf = tmp[i];
  328. ++buf;
  329. }
  330. /* trailing space padding */
  331. while (--spec.field_width >= 0) {
  332. if (buf < end)
  333. *buf = ' ';
  334. ++buf;
  335. }
  336. return buf;
  337. }
  338. static noinline_for_stack
  339. char *string(char *buf, char *end, const char *s, struct printf_spec spec)
  340. {
  341. int len, i;
  342. if ((unsigned long)s < PAGE_SIZE)
  343. s = "(null)";
  344. len = strnlen(s, spec.precision);
  345. if (!(spec.flags & LEFT)) {
  346. while (len < spec.field_width--) {
  347. if (buf < end)
  348. *buf = ' ';
  349. ++buf;
  350. }
  351. }
  352. for (i = 0; i < len; ++i) {
  353. if (buf < end)
  354. *buf = *s;
  355. ++buf; ++s;
  356. }
  357. while (len < spec.field_width--) {
  358. if (buf < end)
  359. *buf = ' ';
  360. ++buf;
  361. }
  362. return buf;
  363. }
  364. static noinline_for_stack
  365. char *symbol_string(char *buf, char *end, void *ptr,
  366. struct printf_spec spec, char ext)
  367. {
  368. unsigned long value = (unsigned long) ptr;
  369. #ifdef CONFIG_KALLSYMS
  370. char sym[KSYM_SYMBOL_LEN];
  371. if (ext == 'B')
  372. sprint_backtrace(sym, value);
  373. else if (ext != 'f' && ext != 's')
  374. sprint_symbol(sym, value);
  375. else
  376. kallsyms_lookup(value, NULL, NULL, NULL, sym);
  377. return string(buf, end, sym, spec);
  378. #else
  379. spec.field_width = 2 * sizeof(void *);
  380. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  381. spec.base = 16;
  382. return number(buf, end, value, spec);
  383. #endif
  384. }
  385. static noinline_for_stack
  386. char *resource_string(char *buf, char *end, struct resource *res,
  387. struct printf_spec spec, const char *fmt)
  388. {
  389. #ifndef IO_RSRC_PRINTK_SIZE
  390. #define IO_RSRC_PRINTK_SIZE 6
  391. #endif
  392. #ifndef MEM_RSRC_PRINTK_SIZE
  393. #define MEM_RSRC_PRINTK_SIZE 10
  394. #endif
  395. static const struct printf_spec io_spec = {
  396. .base = 16,
  397. .field_width = IO_RSRC_PRINTK_SIZE,
  398. .precision = -1,
  399. .flags = SPECIAL | SMALL | ZEROPAD,
  400. };
  401. static const struct printf_spec mem_spec = {
  402. .base = 16,
  403. .field_width = MEM_RSRC_PRINTK_SIZE,
  404. .precision = -1,
  405. .flags = SPECIAL | SMALL | ZEROPAD,
  406. };
  407. static const struct printf_spec bus_spec = {
  408. .base = 16,
  409. .field_width = 2,
  410. .precision = -1,
  411. .flags = SMALL | ZEROPAD,
  412. };
  413. static const struct printf_spec dec_spec = {
  414. .base = 10,
  415. .precision = -1,
  416. .flags = 0,
  417. };
  418. static const struct printf_spec str_spec = {
  419. .field_width = -1,
  420. .precision = 10,
  421. .flags = LEFT,
  422. };
  423. static const struct printf_spec flag_spec = {
  424. .base = 16,
  425. .precision = -1,
  426. .flags = SPECIAL | SMALL,
  427. };
  428. /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
  429. * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
  430. #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
  431. #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
  432. #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
  433. #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
  434. char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
  435. 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
  436. char *p = sym, *pend = sym + sizeof(sym);
  437. int decode = (fmt[0] == 'R') ? 1 : 0;
  438. const struct printf_spec *specp;
  439. *p++ = '[';
  440. if (res->flags & IORESOURCE_IO) {
  441. p = string(p, pend, "io ", str_spec);
  442. specp = &io_spec;
  443. } else if (res->flags & IORESOURCE_MEM) {
  444. p = string(p, pend, "mem ", str_spec);
  445. specp = &mem_spec;
  446. } else if (res->flags & IORESOURCE_IRQ) {
  447. p = string(p, pend, "irq ", str_spec);
  448. specp = &dec_spec;
  449. } else if (res->flags & IORESOURCE_DMA) {
  450. p = string(p, pend, "dma ", str_spec);
  451. specp = &dec_spec;
  452. } else if (res->flags & IORESOURCE_BUS) {
  453. p = string(p, pend, "bus ", str_spec);
  454. specp = &bus_spec;
  455. } else {
  456. p = string(p, pend, "??? ", str_spec);
  457. specp = &mem_spec;
  458. decode = 0;
  459. }
  460. p = number(p, pend, res->start, *specp);
  461. if (res->start != res->end) {
  462. *p++ = '-';
  463. p = number(p, pend, res->end, *specp);
  464. }
  465. if (decode) {
  466. if (res->flags & IORESOURCE_MEM_64)
  467. p = string(p, pend, " 64bit", str_spec);
  468. if (res->flags & IORESOURCE_PREFETCH)
  469. p = string(p, pend, " pref", str_spec);
  470. if (res->flags & IORESOURCE_WINDOW)
  471. p = string(p, pend, " window", str_spec);
  472. if (res->flags & IORESOURCE_DISABLED)
  473. p = string(p, pend, " disabled", str_spec);
  474. } else {
  475. p = string(p, pend, " flags ", str_spec);
  476. p = number(p, pend, res->flags, flag_spec);
  477. }
  478. *p++ = ']';
  479. *p = '\0';
  480. return string(buf, end, sym, spec);
  481. }
  482. static noinline_for_stack
  483. char *mac_address_string(char *buf, char *end, u8 *addr,
  484. struct printf_spec spec, const char *fmt)
  485. {
  486. char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
  487. char *p = mac_addr;
  488. int i;
  489. char separator;
  490. if (fmt[1] == 'F') { /* FDDI canonical format */
  491. separator = '-';
  492. } else {
  493. separator = ':';
  494. }
  495. for (i = 0; i < 6; i++) {
  496. p = hex_byte_pack(p, addr[i]);
  497. if (fmt[0] == 'M' && i != 5)
  498. *p++ = separator;
  499. }
  500. *p = '\0';
  501. return string(buf, end, mac_addr, spec);
  502. }
  503. static noinline_for_stack
  504. char *ip4_string(char *p, const u8 *addr, const char *fmt)
  505. {
  506. int i;
  507. bool leading_zeros = (fmt[0] == 'i');
  508. int index;
  509. int step;
  510. switch (fmt[2]) {
  511. case 'h':
  512. #ifdef __BIG_ENDIAN
  513. index = 0;
  514. step = 1;
  515. #else
  516. index = 3;
  517. step = -1;
  518. #endif
  519. break;
  520. case 'l':
  521. index = 3;
  522. step = -1;
  523. break;
  524. case 'n':
  525. case 'b':
  526. default:
  527. index = 0;
  528. step = 1;
  529. break;
  530. }
  531. for (i = 0; i < 4; i++) {
  532. char temp[3]; /* hold each IP quad in reverse order */
  533. int digits = put_dec_trunc(temp, addr[index]) - temp;
  534. if (leading_zeros) {
  535. if (digits < 3)
  536. *p++ = '0';
  537. if (digits < 2)
  538. *p++ = '0';
  539. }
  540. /* reverse the digits in the quad */
  541. while (digits--)
  542. *p++ = temp[digits];
  543. if (i < 3)
  544. *p++ = '.';
  545. index += step;
  546. }
  547. *p = '\0';
  548. return p;
  549. }
  550. static noinline_for_stack
  551. char *ip6_compressed_string(char *p, const char *addr)
  552. {
  553. int i, j, range;
  554. unsigned char zerolength[8];
  555. int longest = 1;
  556. int colonpos = -1;
  557. u16 word;
  558. u8 hi, lo;
  559. bool needcolon = false;
  560. bool useIPv4;
  561. struct in6_addr in6;
  562. memcpy(&in6, addr, sizeof(struct in6_addr));
  563. useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
  564. memset(zerolength, 0, sizeof(zerolength));
  565. if (useIPv4)
  566. range = 6;
  567. else
  568. range = 8;
  569. /* find position of longest 0 run */
  570. for (i = 0; i < range; i++) {
  571. for (j = i; j < range; j++) {
  572. if (in6.s6_addr16[j] != 0)
  573. break;
  574. zerolength[i]++;
  575. }
  576. }
  577. for (i = 0; i < range; i++) {
  578. if (zerolength[i] > longest) {
  579. longest = zerolength[i];
  580. colonpos = i;
  581. }
  582. }
  583. if (longest == 1) /* don't compress a single 0 */
  584. colonpos = -1;
  585. /* emit address */
  586. for (i = 0; i < range; i++) {
  587. if (i == colonpos) {
  588. if (needcolon || i == 0)
  589. *p++ = ':';
  590. *p++ = ':';
  591. needcolon = false;
  592. i += longest - 1;
  593. continue;
  594. }
  595. if (needcolon) {
  596. *p++ = ':';
  597. needcolon = false;
  598. }
  599. /* hex u16 without leading 0s */
  600. word = ntohs(in6.s6_addr16[i]);
  601. hi = word >> 8;
  602. lo = word & 0xff;
  603. if (hi) {
  604. if (hi > 0x0f)
  605. p = hex_byte_pack(p, hi);
  606. else
  607. *p++ = hex_asc_lo(hi);
  608. p = hex_byte_pack(p, lo);
  609. }
  610. else if (lo > 0x0f)
  611. p = hex_byte_pack(p, lo);
  612. else
  613. *p++ = hex_asc_lo(lo);
  614. needcolon = true;
  615. }
  616. if (useIPv4) {
  617. if (needcolon)
  618. *p++ = ':';
  619. p = ip4_string(p, &in6.s6_addr[12], "I4");
  620. }
  621. *p = '\0';
  622. return p;
  623. }
  624. static noinline_for_stack
  625. char *ip6_string(char *p, const char *addr, const char *fmt)
  626. {
  627. int i;
  628. for (i = 0; i < 8; i++) {
  629. p = hex_byte_pack(p, *addr++);
  630. p = hex_byte_pack(p, *addr++);
  631. if (fmt[0] == 'I' && i != 7)
  632. *p++ = ':';
  633. }
  634. *p = '\0';
  635. return p;
  636. }
  637. static noinline_for_stack
  638. char *ip6_addr_string(char *buf, char *end, const u8 *addr,
  639. struct printf_spec spec, const char *fmt)
  640. {
  641. char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
  642. if (fmt[0] == 'I' && fmt[2] == 'c')
  643. ip6_compressed_string(ip6_addr, addr);
  644. else
  645. ip6_string(ip6_addr, addr, fmt);
  646. return string(buf, end, ip6_addr, spec);
  647. }
  648. static noinline_for_stack
  649. char *ip4_addr_string(char *buf, char *end, const u8 *addr,
  650. struct printf_spec spec, const char *fmt)
  651. {
  652. char ip4_addr[sizeof("255.255.255.255")];
  653. ip4_string(ip4_addr, addr, fmt);
  654. return string(buf, end, ip4_addr, spec);
  655. }
  656. static noinline_for_stack
  657. char *uuid_string(char *buf, char *end, const u8 *addr,
  658. struct printf_spec spec, const char *fmt)
  659. {
  660. char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
  661. char *p = uuid;
  662. int i;
  663. static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  664. static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
  665. const u8 *index = be;
  666. bool uc = false;
  667. switch (*(++fmt)) {
  668. case 'L':
  669. uc = true; /* fall-through */
  670. case 'l':
  671. index = le;
  672. break;
  673. case 'B':
  674. uc = true;
  675. break;
  676. }
  677. for (i = 0; i < 16; i++) {
  678. p = hex_byte_pack(p, addr[index[i]]);
  679. switch (i) {
  680. case 3:
  681. case 5:
  682. case 7:
  683. case 9:
  684. *p++ = '-';
  685. break;
  686. }
  687. }
  688. *p = 0;
  689. if (uc) {
  690. p = uuid;
  691. do {
  692. *p = toupper(*p);
  693. } while (*(++p));
  694. }
  695. return string(buf, end, uuid, spec);
  696. }
  697. int kptr_restrict __read_mostly;
  698. /*
  699. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  700. * by an extra set of alphanumeric characters that are extended format
  701. * specifiers.
  702. *
  703. * Right now we handle:
  704. *
  705. * - 'F' For symbolic function descriptor pointers with offset
  706. * - 'f' For simple symbolic function names without offset
  707. * - 'S' For symbolic direct pointers with offset
  708. * - 's' For symbolic direct pointers without offset
  709. * - 'B' For backtraced symbolic direct pointers with offset
  710. * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  711. * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  712. * - 'M' For a 6-byte MAC address, it prints the address in the
  713. * usual colon-separated hex notation
  714. * - 'm' For a 6-byte MAC address, it prints the hex address without colons
  715. * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  716. * with a dash-separated hex notation
  717. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  718. * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  719. * IPv6 uses colon separated network-order 16 bit hex with leading 0's
  720. * - 'i' [46] for 'raw' IPv4/IPv6 addresses
  721. * IPv6 omits the colons (01020304...0f)
  722. * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  723. * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
  724. * - 'I6c' for IPv6 addresses printed as specified by
  725. * http://tools.ietf.org/html/rfc5952
  726. * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
  727. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  728. * Options for %pU are:
  729. * b big endian lower case hex (default)
  730. * B big endian UPPER case hex
  731. * l little endian lower case hex
  732. * L little endian UPPER case hex
  733. * big endian output byte order is:
  734. * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
  735. * little endian output byte order is:
  736. * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
  737. * - 'V' For a struct va_format which contains a format string * and va_list *,
  738. * call vsnprintf(->format, *->va_list).
  739. * Implements a "recursive vsnprintf".
  740. * Do not use this feature without some mechanism to verify the
  741. * correctness of the format string and va_list arguments.
  742. * - 'K' For a kernel pointer that should be hidden from unprivileged users
  743. *
  744. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  745. * function pointers are really function descriptors, which contain a
  746. * pointer to the real address.
  747. */
  748. static noinline_for_stack
  749. char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  750. struct printf_spec spec)
  751. {
  752. if (!ptr && *fmt != 'K') {
  753. /*
  754. * Print (null) with the same width as a pointer so it makes
  755. * tabular output look nice.
  756. */
  757. if (spec.field_width == -1)
  758. spec.field_width = 2 * sizeof(void *);
  759. return string(buf, end, "(null)", spec);
  760. }
  761. switch (*fmt) {
  762. case 'F':
  763. case 'f':
  764. ptr = dereference_function_descriptor(ptr);
  765. /* Fallthrough */
  766. case 'S':
  767. case 's':
  768. case 'B':
  769. return symbol_string(buf, end, ptr, spec, *fmt);
  770. case 'R':
  771. case 'r':
  772. return resource_string(buf, end, ptr, spec, fmt);
  773. case 'M': /* Colon separated: 00:01:02:03:04:05 */
  774. case 'm': /* Contiguous: 000102030405 */
  775. /* [mM]F (FDDI, bit reversed) */
  776. return mac_address_string(buf, end, ptr, spec, fmt);
  777. case 'I': /* Formatted IP supported
  778. * 4: 1.2.3.4
  779. * 6: 0001:0203:...:0708
  780. * 6c: 1::708 or 1::1.2.3.4
  781. */
  782. case 'i': /* Contiguous:
  783. * 4: 001.002.003.004
  784. * 6: 000102...0f
  785. */
  786. switch (fmt[1]) {
  787. case '6':
  788. return ip6_addr_string(buf, end, ptr, spec, fmt);
  789. case '4':
  790. return ip4_addr_string(buf, end, ptr, spec, fmt);
  791. }
  792. break;
  793. case 'U':
  794. return uuid_string(buf, end, ptr, spec, fmt);
  795. case 'V':
  796. return buf + vsnprintf(buf, end > buf ? end - buf : 0,
  797. ((struct va_format *)ptr)->fmt,
  798. *(((struct va_format *)ptr)->va));
  799. case 'K':
  800. /*
  801. * %pK cannot be used in IRQ context because its test
  802. * for CAP_SYSLOG would be meaningless.
  803. */
  804. if (in_irq() || in_serving_softirq() || in_nmi()) {
  805. if (spec.field_width == -1)
  806. spec.field_width = 2 * sizeof(void *);
  807. return string(buf, end, "pK-error", spec);
  808. }
  809. if (!((kptr_restrict == 0) ||
  810. (kptr_restrict == 1 &&
  811. has_capability_noaudit(current, CAP_SYSLOG))))
  812. ptr = NULL;
  813. break;
  814. }
  815. spec.flags |= SMALL;
  816. if (spec.field_width == -1) {
  817. spec.field_width = 2 * sizeof(void *);
  818. spec.flags |= ZEROPAD;
  819. }
  820. spec.base = 16;
  821. return number(buf, end, (unsigned long) ptr, spec);
  822. }
  823. /*
  824. * Helper function to decode printf style format.
  825. * Each call decode a token from the format and return the
  826. * number of characters read (or likely the delta where it wants
  827. * to go on the next call).
  828. * The decoded token is returned through the parameters
  829. *
  830. * 'h', 'l', or 'L' for integer fields
  831. * 'z' support added 23/7/1999 S.H.
  832. * 'z' changed to 'Z' --davidm 1/25/99
  833. * 't' added for ptrdiff_t
  834. *
  835. * @fmt: the format string
  836. * @type of the token returned
  837. * @flags: various flags such as +, -, # tokens..
  838. * @field_width: overwritten width
  839. * @base: base of the number (octal, hex, ...)
  840. * @precision: precision of a number
  841. * @qualifier: qualifier of a number (long, size_t, ...)
  842. */
  843. static noinline_for_stack
  844. int format_decode(const char *fmt, struct printf_spec *spec)
  845. {
  846. const char *start = fmt;
  847. /* we finished early by reading the field width */
  848. if (spec->type == FORMAT_TYPE_WIDTH) {
  849. if (spec->field_width < 0) {
  850. spec->field_width = -spec->field_width;
  851. spec->flags |= LEFT;
  852. }
  853. spec->type = FORMAT_TYPE_NONE;
  854. goto precision;
  855. }
  856. /* we finished early by reading the precision */
  857. if (spec->type == FORMAT_TYPE_PRECISION) {
  858. if (spec->precision < 0)
  859. spec->precision = 0;
  860. spec->type = FORMAT_TYPE_NONE;
  861. goto qualifier;
  862. }
  863. /* By default */
  864. spec->type = FORMAT_TYPE_NONE;
  865. for (; *fmt ; ++fmt) {
  866. if (*fmt == '%')
  867. break;
  868. }
  869. /* Return the current non-format string */
  870. if (fmt != start || !*fmt)
  871. return fmt - start;
  872. /* Process flags */
  873. spec->flags = 0;
  874. while (1) { /* this also skips first '%' */
  875. bool found = true;
  876. ++fmt;
  877. switch (*fmt) {
  878. case '-': spec->flags |= LEFT; break;
  879. case '+': spec->flags |= PLUS; break;
  880. case ' ': spec->flags |= SPACE; break;
  881. case '#': spec->flags |= SPECIAL; break;
  882. case '0': spec->flags |= ZEROPAD; break;
  883. default: found = false;
  884. }
  885. if (!found)
  886. break;
  887. }
  888. /* get field width */
  889. spec->field_width = -1;
  890. if (isdigit(*fmt))
  891. spec->field_width = skip_atoi(&fmt);
  892. else if (*fmt == '*') {
  893. /* it's the next argument */
  894. spec->type = FORMAT_TYPE_WIDTH;
  895. return ++fmt - start;
  896. }
  897. precision:
  898. /* get the precision */
  899. spec->precision = -1;
  900. if (*fmt == '.') {
  901. ++fmt;
  902. if (isdigit(*fmt)) {
  903. spec->precision = skip_atoi(&fmt);
  904. if (spec->precision < 0)
  905. spec->precision = 0;
  906. } else if (*fmt == '*') {
  907. /* it's the next argument */
  908. spec->type = FORMAT_TYPE_PRECISION;
  909. return ++fmt - start;
  910. }
  911. }
  912. qualifier:
  913. /* get the conversion qualifier */
  914. spec->qualifier = -1;
  915. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  916. _tolower(*fmt) == 'z' || *fmt == 't') {
  917. spec->qualifier = *fmt++;
  918. if (unlikely(spec->qualifier == *fmt)) {
  919. if (spec->qualifier == 'l') {
  920. spec->qualifier = 'L';
  921. ++fmt;
  922. } else if (spec->qualifier == 'h') {
  923. spec->qualifier = 'H';
  924. ++fmt;
  925. }
  926. }
  927. }
  928. /* default base */
  929. spec->base = 10;
  930. switch (*fmt) {
  931. case 'c':
  932. spec->type = FORMAT_TYPE_CHAR;
  933. return ++fmt - start;
  934. case 's':
  935. spec->type = FORMAT_TYPE_STR;
  936. return ++fmt - start;
  937. case 'p':
  938. spec->type = FORMAT_TYPE_PTR;
  939. return fmt - start;
  940. /* skip alnum */
  941. case 'n':
  942. spec->type = FORMAT_TYPE_NRCHARS;
  943. return ++fmt - start;
  944. case '%':
  945. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  946. return ++fmt - start;
  947. /* integer number formats - set up the flags and "break" */
  948. case 'o':
  949. spec->base = 8;
  950. break;
  951. case 'x':
  952. spec->flags |= SMALL;
  953. case 'X':
  954. spec->base = 16;
  955. break;
  956. case 'd':
  957. case 'i':
  958. spec->flags |= SIGN;
  959. case 'u':
  960. break;
  961. default:
  962. spec->type = FORMAT_TYPE_INVALID;
  963. return fmt - start;
  964. }
  965. if (spec->qualifier == 'L')
  966. spec->type = FORMAT_TYPE_LONG_LONG;
  967. else if (spec->qualifier == 'l') {
  968. if (spec->flags & SIGN)
  969. spec->type = FORMAT_TYPE_LONG;
  970. else
  971. spec->type = FORMAT_TYPE_ULONG;
  972. } else if (_tolower(spec->qualifier) == 'z') {
  973. spec->type = FORMAT_TYPE_SIZE_T;
  974. } else if (spec->qualifier == 't') {
  975. spec->type = FORMAT_TYPE_PTRDIFF;
  976. } else if (spec->qualifier == 'H') {
  977. if (spec->flags & SIGN)
  978. spec->type = FORMAT_TYPE_BYTE;
  979. else
  980. spec->type = FORMAT_TYPE_UBYTE;
  981. } else if (spec->qualifier == 'h') {
  982. if (spec->flags & SIGN)
  983. spec->type = FORMAT_TYPE_SHORT;
  984. else
  985. spec->type = FORMAT_TYPE_USHORT;
  986. } else {
  987. if (spec->flags & SIGN)
  988. spec->type = FORMAT_TYPE_INT;
  989. else
  990. spec->type = FORMAT_TYPE_UINT;
  991. }
  992. return ++fmt - start;
  993. }
  994. /**
  995. * vsnprintf - Format a string and place it in a buffer
  996. * @buf: The buffer to place the result into
  997. * @size: The size of the buffer, including the trailing null space
  998. * @fmt: The format string to use
  999. * @args: Arguments for the format string
  1000. *
  1001. * This function follows C99 vsnprintf, but has some extensions:
  1002. * %pS output the name of a text symbol with offset
  1003. * %ps output the name of a text symbol without offset
  1004. * %pF output the name of a function pointer with its offset
  1005. * %pf output the name of a function pointer without its offset
  1006. * %pB output the name of a backtrace symbol with its offset
  1007. * %pR output the address range in a struct resource with decoded flags
  1008. * %pr output the address range in a struct resource with raw flags
  1009. * %pM output a 6-byte MAC address with colons
  1010. * %pm output a 6-byte MAC address without colons
  1011. * %pI4 print an IPv4 address without leading zeros
  1012. * %pi4 print an IPv4 address with leading zeros
  1013. * %pI6 print an IPv6 address with colons
  1014. * %pi6 print an IPv6 address without colons
  1015. * %pI6c print an IPv6 address as specified by RFC 5952
  1016. * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
  1017. * case.
  1018. * %n is ignored
  1019. *
  1020. * The return value is the number of characters which would
  1021. * be generated for the given input, excluding the trailing
  1022. * '\0', as per ISO C99. If you want to have the exact
  1023. * number of characters written into @buf as return value
  1024. * (not including the trailing '\0'), use vscnprintf(). If the
  1025. * return is greater than or equal to @size, the resulting
  1026. * string is truncated.
  1027. *
  1028. * If you're not already dealing with a va_list consider using snprintf().
  1029. */
  1030. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1031. {
  1032. unsigned long long num;
  1033. char *str, *end;
  1034. struct printf_spec spec = {0};
  1035. /* Reject out-of-range values early. Large positive sizes are
  1036. used for unknown buffer sizes. */
  1037. if (WARN_ON_ONCE((int) size < 0))
  1038. return 0;
  1039. str = buf;
  1040. end = buf + size;
  1041. /* Make sure end is always >= buf */
  1042. if (end < buf) {
  1043. end = ((void *)-1);
  1044. size = end - buf;
  1045. }
  1046. while (*fmt) {
  1047. const char *old_fmt = fmt;
  1048. int read = format_decode(fmt, &spec);
  1049. fmt += read;
  1050. switch (spec.type) {
  1051. case FORMAT_TYPE_NONE: {
  1052. int copy = read;
  1053. if (str < end) {
  1054. if (copy > end - str)
  1055. copy = end - str;
  1056. memcpy(str, old_fmt, copy);
  1057. }
  1058. str += read;
  1059. break;
  1060. }
  1061. case FORMAT_TYPE_WIDTH:
  1062. spec.field_width = va_arg(args, int);
  1063. break;
  1064. case FORMAT_TYPE_PRECISION:
  1065. spec.precision = va_arg(args, int);
  1066. break;
  1067. case FORMAT_TYPE_CHAR: {
  1068. char c;
  1069. if (!(spec.flags & LEFT)) {
  1070. while (--spec.field_width > 0) {
  1071. if (str < end)
  1072. *str = ' ';
  1073. ++str;
  1074. }
  1075. }
  1076. c = (unsigned char) va_arg(args, int);
  1077. if (str < end)
  1078. *str = c;
  1079. ++str;
  1080. while (--spec.field_width > 0) {
  1081. if (str < end)
  1082. *str = ' ';
  1083. ++str;
  1084. }
  1085. break;
  1086. }
  1087. case FORMAT_TYPE_STR:
  1088. str = string(str, end, va_arg(args, char *), spec);
  1089. break;
  1090. case FORMAT_TYPE_PTR:
  1091. str = pointer(fmt+1, str, end, va_arg(args, void *),
  1092. spec);
  1093. while (isalnum(*fmt))
  1094. fmt++;
  1095. break;
  1096. case FORMAT_TYPE_PERCENT_CHAR:
  1097. if (str < end)
  1098. *str = '%';
  1099. ++str;
  1100. break;
  1101. case FORMAT_TYPE_INVALID:
  1102. if (str < end)
  1103. *str = '%';
  1104. ++str;
  1105. break;
  1106. case FORMAT_TYPE_NRCHARS: {
  1107. u8 qualifier = spec.qualifier;
  1108. if (qualifier == 'l') {
  1109. long *ip = va_arg(args, long *);
  1110. *ip = (str - buf);
  1111. } else if (_tolower(qualifier) == 'z') {
  1112. size_t *ip = va_arg(args, size_t *);
  1113. *ip = (str - buf);
  1114. } else {
  1115. int *ip = va_arg(args, int *);
  1116. *ip = (str - buf);
  1117. }
  1118. break;
  1119. }
  1120. default:
  1121. switch (spec.type) {
  1122. case FORMAT_TYPE_LONG_LONG:
  1123. num = va_arg(args, long long);
  1124. break;
  1125. case FORMAT_TYPE_ULONG:
  1126. num = va_arg(args, unsigned long);
  1127. break;
  1128. case FORMAT_TYPE_LONG:
  1129. num = va_arg(args, long);
  1130. break;
  1131. case FORMAT_TYPE_SIZE_T:
  1132. num = va_arg(args, size_t);
  1133. break;
  1134. case FORMAT_TYPE_PTRDIFF:
  1135. num = va_arg(args, ptrdiff_t);
  1136. break;
  1137. case FORMAT_TYPE_UBYTE:
  1138. num = (unsigned char) va_arg(args, int);
  1139. break;
  1140. case FORMAT_TYPE_BYTE:
  1141. num = (signed char) va_arg(args, int);
  1142. break;
  1143. case FORMAT_TYPE_USHORT:
  1144. num = (unsigned short) va_arg(args, int);
  1145. break;
  1146. case FORMAT_TYPE_SHORT:
  1147. num = (short) va_arg(args, int);
  1148. break;
  1149. case FORMAT_TYPE_INT:
  1150. num = (int) va_arg(args, int);
  1151. break;
  1152. default:
  1153. num = va_arg(args, unsigned int);
  1154. }
  1155. str = number(str, end, num, spec);
  1156. }
  1157. }
  1158. if (size > 0) {
  1159. if (str < end)
  1160. *str = '\0';
  1161. else
  1162. end[-1] = '\0';
  1163. }
  1164. /* the trailing null byte doesn't count towards the total */
  1165. return str-buf;
  1166. }
  1167. EXPORT_SYMBOL(vsnprintf);
  1168. /**
  1169. * vscnprintf - Format a string and place it in a buffer
  1170. * @buf: The buffer to place the result into
  1171. * @size: The size of the buffer, including the trailing null space
  1172. * @fmt: The format string to use
  1173. * @args: Arguments for the format string
  1174. *
  1175. * The return value is the number of characters which have been written into
  1176. * the @buf not including the trailing '\0'. If @size is == 0 the function
  1177. * returns 0.
  1178. *
  1179. * If you're not already dealing with a va_list consider using scnprintf().
  1180. *
  1181. * See the vsnprintf() documentation for format string extensions over C99.
  1182. */
  1183. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1184. {
  1185. int i;
  1186. i = vsnprintf(buf, size, fmt, args);
  1187. if (likely(i < size))
  1188. return i;
  1189. if (size != 0)
  1190. return size - 1;
  1191. return 0;
  1192. }
  1193. EXPORT_SYMBOL(vscnprintf);
  1194. /**
  1195. * snprintf - Format a string and place it in a buffer
  1196. * @buf: The buffer to place the result into
  1197. * @size: The size of the buffer, including the trailing null space
  1198. * @fmt: The format string to use
  1199. * @...: Arguments for the format string
  1200. *
  1201. * The return value is the number of characters which would be
  1202. * generated for the given input, excluding the trailing null,
  1203. * as per ISO C99. If the return is greater than or equal to
  1204. * @size, the resulting string is truncated.
  1205. *
  1206. * See the vsnprintf() documentation for format string extensions over C99.
  1207. */
  1208. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1209. {
  1210. va_list args;
  1211. int i;
  1212. va_start(args, fmt);
  1213. i = vsnprintf(buf, size, fmt, args);
  1214. va_end(args);
  1215. return i;
  1216. }
  1217. EXPORT_SYMBOL(snprintf);
  1218. /**
  1219. * scnprintf - Format a string and place it in a buffer
  1220. * @buf: The buffer to place the result into
  1221. * @size: The size of the buffer, including the trailing null space
  1222. * @fmt: The format string to use
  1223. * @...: Arguments for the format string
  1224. *
  1225. * The return value is the number of characters written into @buf not including
  1226. * the trailing '\0'. If @size is == 0 the function returns 0.
  1227. */
  1228. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  1229. {
  1230. va_list args;
  1231. int i;
  1232. va_start(args, fmt);
  1233. i = vscnprintf(buf, size, fmt, args);
  1234. va_end(args);
  1235. return i;
  1236. }
  1237. EXPORT_SYMBOL(scnprintf);
  1238. /**
  1239. * vsprintf - Format a string and place it in a buffer
  1240. * @buf: The buffer to place the result into
  1241. * @fmt: The format string to use
  1242. * @args: Arguments for the format string
  1243. *
  1244. * The function returns the number of characters written
  1245. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1246. * buffer overflows.
  1247. *
  1248. * If you're not already dealing with a va_list consider using sprintf().
  1249. *
  1250. * See the vsnprintf() documentation for format string extensions over C99.
  1251. */
  1252. int vsprintf(char *buf, const char *fmt, va_list args)
  1253. {
  1254. return vsnprintf(buf, INT_MAX, fmt, args);
  1255. }
  1256. EXPORT_SYMBOL(vsprintf);
  1257. /**
  1258. * sprintf - Format a string and place it in a buffer
  1259. * @buf: The buffer to place the result into
  1260. * @fmt: The format string to use
  1261. * @...: Arguments for the format string
  1262. *
  1263. * The function returns the number of characters written
  1264. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1265. * buffer overflows.
  1266. *
  1267. * See the vsnprintf() documentation for format string extensions over C99.
  1268. */
  1269. int sprintf(char *buf, const char *fmt, ...)
  1270. {
  1271. va_list args;
  1272. int i;
  1273. va_start(args, fmt);
  1274. i = vsnprintf(buf, INT_MAX, fmt, args);
  1275. va_end(args);
  1276. return i;
  1277. }
  1278. EXPORT_SYMBOL(sprintf);
  1279. #ifdef CONFIG_BINARY_PRINTF
  1280. /*
  1281. * bprintf service:
  1282. * vbin_printf() - VA arguments to binary data
  1283. * bstr_printf() - Binary data to text string
  1284. */
  1285. /**
  1286. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1287. * @bin_buf: The buffer to place args' binary value
  1288. * @size: The size of the buffer(by words(32bits), not characters)
  1289. * @fmt: The format string to use
  1290. * @args: Arguments for the format string
  1291. *
  1292. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1293. * is skiped.
  1294. *
  1295. * The return value is the number of words(32bits) which would be generated for
  1296. * the given input.
  1297. *
  1298. * NOTE:
  1299. * If the return value is greater than @size, the resulting bin_buf is NOT
  1300. * valid for bstr_printf().
  1301. */
  1302. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1303. {
  1304. struct printf_spec spec = {0};
  1305. char *str, *end;
  1306. str = (char *)bin_buf;
  1307. end = (char *)(bin_buf + size);
  1308. #define save_arg(type) \
  1309. do { \
  1310. if (sizeof(type) == 8) { \
  1311. unsigned long long value; \
  1312. str = PTR_ALIGN(str, sizeof(u32)); \
  1313. value = va_arg(args, unsigned long long); \
  1314. if (str + sizeof(type) <= end) { \
  1315. *(u32 *)str = *(u32 *)&value; \
  1316. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1317. } \
  1318. } else { \
  1319. unsigned long value; \
  1320. str = PTR_ALIGN(str, sizeof(type)); \
  1321. value = va_arg(args, int); \
  1322. if (str + sizeof(type) <= end) \
  1323. *(typeof(type) *)str = (type)value; \
  1324. } \
  1325. str += sizeof(type); \
  1326. } while (0)
  1327. while (*fmt) {
  1328. int read = format_decode(fmt, &spec);
  1329. fmt += read;
  1330. switch (spec.type) {
  1331. case FORMAT_TYPE_NONE:
  1332. case FORMAT_TYPE_INVALID:
  1333. case FORMAT_TYPE_PERCENT_CHAR:
  1334. break;
  1335. case FORMAT_TYPE_WIDTH:
  1336. case FORMAT_TYPE_PRECISION:
  1337. save_arg(int);
  1338. break;
  1339. case FORMAT_TYPE_CHAR:
  1340. save_arg(char);
  1341. break;
  1342. case FORMAT_TYPE_STR: {
  1343. const char *save_str = va_arg(args, char *);
  1344. size_t len;
  1345. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1346. || (unsigned long)save_str < PAGE_SIZE)
  1347. save_str = "(null)";
  1348. len = strlen(save_str) + 1;
  1349. if (str + len < end)
  1350. memcpy(str, save_str, len);
  1351. str += len;
  1352. break;
  1353. }
  1354. case FORMAT_TYPE_PTR:
  1355. save_arg(void *);
  1356. /* skip all alphanumeric pointer suffixes */
  1357. while (isalnum(*fmt))
  1358. fmt++;
  1359. break;
  1360. case FORMAT_TYPE_NRCHARS: {
  1361. /* skip %n 's argument */
  1362. u8 qualifier = spec.qualifier;
  1363. void *skip_arg;
  1364. if (qualifier == 'l')
  1365. skip_arg = va_arg(args, long *);
  1366. else if (_tolower(qualifier) == 'z')
  1367. skip_arg = va_arg(args, size_t *);
  1368. else
  1369. skip_arg = va_arg(args, int *);
  1370. break;
  1371. }
  1372. default:
  1373. switch (spec.type) {
  1374. case FORMAT_TYPE_LONG_LONG:
  1375. save_arg(long long);
  1376. break;
  1377. case FORMAT_TYPE_ULONG:
  1378. case FORMAT_TYPE_LONG:
  1379. save_arg(unsigned long);
  1380. break;
  1381. case FORMAT_TYPE_SIZE_T:
  1382. save_arg(size_t);
  1383. break;
  1384. case FORMAT_TYPE_PTRDIFF:
  1385. save_arg(ptrdiff_t);
  1386. break;
  1387. case FORMAT_TYPE_UBYTE:
  1388. case FORMAT_TYPE_BYTE:
  1389. save_arg(char);
  1390. break;
  1391. case FORMAT_TYPE_USHORT:
  1392. case FORMAT_TYPE_SHORT:
  1393. save_arg(short);
  1394. break;
  1395. default:
  1396. save_arg(int);
  1397. }
  1398. }
  1399. }
  1400. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1401. #undef save_arg
  1402. }
  1403. EXPORT_SYMBOL_GPL(vbin_printf);
  1404. /**
  1405. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1406. * @buf: The buffer to place the result into
  1407. * @size: The size of the buffer, including the trailing null space
  1408. * @fmt: The format string to use
  1409. * @bin_buf: Binary arguments for the format string
  1410. *
  1411. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1412. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1413. * a binary buffer that generated by vbin_printf.
  1414. *
  1415. * The format follows C99 vsnprintf, but has some extensions:
  1416. * see vsnprintf comment for details.
  1417. *
  1418. * The return value is the number of characters which would
  1419. * be generated for the given input, excluding the trailing
  1420. * '\0', as per ISO C99. If you want to have the exact
  1421. * number of characters written into @buf as return value
  1422. * (not including the trailing '\0'), use vscnprintf(). If the
  1423. * return is greater than or equal to @size, the resulting
  1424. * string is truncated.
  1425. */
  1426. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1427. {
  1428. struct printf_spec spec = {0};
  1429. char *str, *end;
  1430. const char *args = (const char *)bin_buf;
  1431. if (WARN_ON_ONCE((int) size < 0))
  1432. return 0;
  1433. str = buf;
  1434. end = buf + size;
  1435. #define get_arg(type) \
  1436. ({ \
  1437. typeof(type) value; \
  1438. if (sizeof(type) == 8) { \
  1439. args = PTR_ALIGN(args, sizeof(u32)); \
  1440. *(u32 *)&value = *(u32 *)args; \
  1441. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1442. } else { \
  1443. args = PTR_ALIGN(args, sizeof(type)); \
  1444. value = *(typeof(type) *)args; \
  1445. } \
  1446. args += sizeof(type); \
  1447. value; \
  1448. })
  1449. /* Make sure end is always >= buf */
  1450. if (end < buf) {
  1451. end = ((void *)-1);
  1452. size = end - buf;
  1453. }
  1454. while (*fmt) {
  1455. const char *old_fmt = fmt;
  1456. int read = format_decode(fmt, &spec);
  1457. fmt += read;
  1458. switch (spec.type) {
  1459. case FORMAT_TYPE_NONE: {
  1460. int copy = read;
  1461. if (str < end) {
  1462. if (copy > end - str)
  1463. copy = end - str;
  1464. memcpy(str, old_fmt, copy);
  1465. }
  1466. str += read;
  1467. break;
  1468. }
  1469. case FORMAT_TYPE_WIDTH:
  1470. spec.field_width = get_arg(int);
  1471. break;
  1472. case FORMAT_TYPE_PRECISION:
  1473. spec.precision = get_arg(int);
  1474. break;
  1475. case FORMAT_TYPE_CHAR: {
  1476. char c;
  1477. if (!(spec.flags & LEFT)) {
  1478. while (--spec.field_width > 0) {
  1479. if (str < end)
  1480. *str = ' ';
  1481. ++str;
  1482. }
  1483. }
  1484. c = (unsigned char) get_arg(char);
  1485. if (str < end)
  1486. *str = c;
  1487. ++str;
  1488. while (--spec.field_width > 0) {
  1489. if (str < end)
  1490. *str = ' ';
  1491. ++str;
  1492. }
  1493. break;
  1494. }
  1495. case FORMAT_TYPE_STR: {
  1496. const char *str_arg = args;
  1497. args += strlen(str_arg) + 1;
  1498. str = string(str, end, (char *)str_arg, spec);
  1499. break;
  1500. }
  1501. case FORMAT_TYPE_PTR:
  1502. str = pointer(fmt+1, str, end, get_arg(void *), spec);
  1503. while (isalnum(*fmt))
  1504. fmt++;
  1505. break;
  1506. case FORMAT_TYPE_PERCENT_CHAR:
  1507. case FORMAT_TYPE_INVALID:
  1508. if (str < end)
  1509. *str = '%';
  1510. ++str;
  1511. break;
  1512. case FORMAT_TYPE_NRCHARS:
  1513. /* skip */
  1514. break;
  1515. default: {
  1516. unsigned long long num;
  1517. switch (spec.type) {
  1518. case FORMAT_TYPE_LONG_LONG:
  1519. num = get_arg(long long);
  1520. break;
  1521. case FORMAT_TYPE_ULONG:
  1522. case FORMAT_TYPE_LONG:
  1523. num = get_arg(unsigned long);
  1524. break;
  1525. case FORMAT_TYPE_SIZE_T:
  1526. num = get_arg(size_t);
  1527. break;
  1528. case FORMAT_TYPE_PTRDIFF:
  1529. num = get_arg(ptrdiff_t);
  1530. break;
  1531. case FORMAT_TYPE_UBYTE:
  1532. num = get_arg(unsigned char);
  1533. break;
  1534. case FORMAT_TYPE_BYTE:
  1535. num = get_arg(signed char);
  1536. break;
  1537. case FORMAT_TYPE_USHORT:
  1538. num = get_arg(unsigned short);
  1539. break;
  1540. case FORMAT_TYPE_SHORT:
  1541. num = get_arg(short);
  1542. break;
  1543. case FORMAT_TYPE_UINT:
  1544. num = get_arg(unsigned int);
  1545. break;
  1546. default:
  1547. num = get_arg(int);
  1548. }
  1549. str = number(str, end, num, spec);
  1550. } /* default: */
  1551. } /* switch(spec.type) */
  1552. } /* while(*fmt) */
  1553. if (size > 0) {
  1554. if (str < end)
  1555. *str = '\0';
  1556. else
  1557. end[-1] = '\0';
  1558. }
  1559. #undef get_arg
  1560. /* the trailing null byte doesn't count towards the total */
  1561. return str - buf;
  1562. }
  1563. EXPORT_SYMBOL_GPL(bstr_printf);
  1564. /**
  1565. * bprintf - Parse a format string and place args' binary value in a buffer
  1566. * @bin_buf: The buffer to place args' binary value
  1567. * @size: The size of the buffer(by words(32bits), not characters)
  1568. * @fmt: The format string to use
  1569. * @...: Arguments for the format string
  1570. *
  1571. * The function returns the number of words(u32) written
  1572. * into @bin_buf.
  1573. */
  1574. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  1575. {
  1576. va_list args;
  1577. int ret;
  1578. va_start(args, fmt);
  1579. ret = vbin_printf(bin_buf, size, fmt, args);
  1580. va_end(args);
  1581. return ret;
  1582. }
  1583. EXPORT_SYMBOL_GPL(bprintf);
  1584. #endif /* CONFIG_BINARY_PRINTF */
  1585. /**
  1586. * vsscanf - Unformat a buffer into a list of arguments
  1587. * @buf: input buffer
  1588. * @fmt: format of buffer
  1589. * @args: arguments
  1590. */
  1591. int vsscanf(const char *buf, const char *fmt, va_list args)
  1592. {
  1593. const char *str = buf;
  1594. char *next;
  1595. char digit;
  1596. int num = 0;
  1597. u8 qualifier;
  1598. u8 base;
  1599. s16 field_width;
  1600. bool is_sign;
  1601. while (*fmt && *str) {
  1602. /* skip any white space in format */
  1603. /* white space in format matchs any amount of
  1604. * white space, including none, in the input.
  1605. */
  1606. if (isspace(*fmt)) {
  1607. fmt = skip_spaces(++fmt);
  1608. str = skip_spaces(str);
  1609. }
  1610. /* anything that is not a conversion must match exactly */
  1611. if (*fmt != '%' && *fmt) {
  1612. if (*fmt++ != *str++)
  1613. break;
  1614. continue;
  1615. }
  1616. if (!*fmt)
  1617. break;
  1618. ++fmt;
  1619. /* skip this conversion.
  1620. * advance both strings to next white space
  1621. */
  1622. if (*fmt == '*') {
  1623. while (!isspace(*fmt) && *fmt != '%' && *fmt)
  1624. fmt++;
  1625. while (!isspace(*str) && *str)
  1626. str++;
  1627. continue;
  1628. }
  1629. /* get field width */
  1630. field_width = -1;
  1631. if (isdigit(*fmt))
  1632. field_width = skip_atoi(&fmt);
  1633. /* get conversion qualifier */
  1634. qualifier = -1;
  1635. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  1636. _tolower(*fmt) == 'z') {
  1637. qualifier = *fmt++;
  1638. if (unlikely(qualifier == *fmt)) {
  1639. if (qualifier == 'h') {
  1640. qualifier = 'H';
  1641. fmt++;
  1642. } else if (qualifier == 'l') {
  1643. qualifier = 'L';
  1644. fmt++;
  1645. }
  1646. }
  1647. }
  1648. if (!*fmt || !*str)
  1649. break;
  1650. base = 10;
  1651. is_sign = 0;
  1652. switch (*fmt++) {
  1653. case 'c':
  1654. {
  1655. char *s = (char *)va_arg(args, char*);
  1656. if (field_width == -1)
  1657. field_width = 1;
  1658. do {
  1659. *s++ = *str++;
  1660. } while (--field_width > 0 && *str);
  1661. num++;
  1662. }
  1663. continue;
  1664. case 's':
  1665. {
  1666. char *s = (char *)va_arg(args, char *);
  1667. if (field_width == -1)
  1668. field_width = SHRT_MAX;
  1669. /* first, skip leading white space in buffer */
  1670. str = skip_spaces(str);
  1671. /* now copy until next white space */
  1672. while (*str && !isspace(*str) && field_width--)
  1673. *s++ = *str++;
  1674. *s = '\0';
  1675. num++;
  1676. }
  1677. continue;
  1678. case 'n':
  1679. /* return number of characters read so far */
  1680. {
  1681. int *i = (int *)va_arg(args, int*);
  1682. *i = str - buf;
  1683. }
  1684. continue;
  1685. case 'o':
  1686. base = 8;
  1687. break;
  1688. case 'x':
  1689. case 'X':
  1690. base = 16;
  1691. break;
  1692. case 'i':
  1693. base = 0;
  1694. case 'd':
  1695. is_sign = 1;
  1696. case 'u':
  1697. break;
  1698. case '%':
  1699. /* looking for '%' in str */
  1700. if (*str++ != '%')
  1701. return num;
  1702. continue;
  1703. default:
  1704. /* invalid format; stop here */
  1705. return num;
  1706. }
  1707. /* have some sort of integer conversion.
  1708. * first, skip white space in buffer.
  1709. */
  1710. str = skip_spaces(str);
  1711. digit = *str;
  1712. if (is_sign && digit == '-')
  1713. digit = *(str + 1);
  1714. if (!digit
  1715. || (base == 16 && !isxdigit(digit))
  1716. || (base == 10 && !isdigit(digit))
  1717. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1718. || (base == 0 && !isdigit(digit)))
  1719. break;
  1720. switch (qualifier) {
  1721. case 'H': /* that's 'hh' in format */
  1722. if (is_sign) {
  1723. signed char *s = (signed char *)va_arg(args, signed char *);
  1724. *s = (signed char)simple_strtol(str, &next, base);
  1725. } else {
  1726. unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
  1727. *s = (unsigned char)simple_strtoul(str, &next, base);
  1728. }
  1729. break;
  1730. case 'h':
  1731. if (is_sign) {
  1732. short *s = (short *)va_arg(args, short *);
  1733. *s = (short)simple_strtol(str, &next, base);
  1734. } else {
  1735. unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
  1736. *s = (unsigned short)simple_strtoul(str, &next, base);
  1737. }
  1738. break;
  1739. case 'l':
  1740. if (is_sign) {
  1741. long *l = (long *)va_arg(args, long *);
  1742. *l = simple_strtol(str, &next, base);
  1743. } else {
  1744. unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
  1745. *l = simple_strtoul(str, &next, base);
  1746. }
  1747. break;
  1748. case 'L':
  1749. if (is_sign) {
  1750. long long *l = (long long *)va_arg(args, long long *);
  1751. *l = simple_strtoll(str, &next, base);
  1752. } else {
  1753. unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
  1754. *l = simple_strtoull(str, &next, base);
  1755. }
  1756. break;
  1757. case 'Z':
  1758. case 'z':
  1759. {
  1760. size_t *s = (size_t *)va_arg(args, size_t *);
  1761. *s = (size_t)simple_strtoul(str, &next, base);
  1762. }
  1763. break;
  1764. default:
  1765. if (is_sign) {
  1766. int *i = (int *)va_arg(args, int *);
  1767. *i = (int)simple_strtol(str, &next, base);
  1768. } else {
  1769. unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
  1770. *i = (unsigned int)simple_strtoul(str, &next, base);
  1771. }
  1772. break;
  1773. }
  1774. num++;
  1775. if (!next)
  1776. break;
  1777. str = next;
  1778. }
  1779. /*
  1780. * Now we've come all the way through so either the input string or the
  1781. * format ended. In the former case, there can be a %n at the current
  1782. * position in the format that needs to be filled.
  1783. */
  1784. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1785. int *p = (int *)va_arg(args, int *);
  1786. *p = str - buf;
  1787. }
  1788. return num;
  1789. }
  1790. EXPORT_SYMBOL(vsscanf);
  1791. /**
  1792. * sscanf - Unformat a buffer into a list of arguments
  1793. * @buf: input buffer
  1794. * @fmt: formatting of buffer
  1795. * @...: resulting arguments
  1796. */
  1797. int sscanf(const char *buf, const char *fmt, ...)
  1798. {
  1799. va_list args;
  1800. int i;
  1801. va_start(args, fmt);
  1802. i = vsscanf(buf, fmt, args);
  1803. va_end(args);
  1804. return i;
  1805. }
  1806. EXPORT_SYMBOL(sscanf);