vsprintf.c 47 KB

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