vsprintf.c 48 KB

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