vsprintf.c 47 KB

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