vsprintf.c 49 KB

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