vsprintf.c 47 KB

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