vsprintf.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  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://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
  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
  1053. * %n is ignored
  1054. *
  1055. * The return value is the number of characters which would
  1056. * be generated for the given input, excluding the trailing
  1057. * '\0', as per ISO C99. If you want to have the exact
  1058. * number of characters written into @buf as return value
  1059. * (not including the trailing '\0'), use vscnprintf(). If the
  1060. * return is greater than or equal to @size, the resulting
  1061. * string is truncated.
  1062. *
  1063. * Call this function if you are already dealing with a va_list.
  1064. * You probably want snprintf() instead.
  1065. */
  1066. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1067. {
  1068. unsigned long long num;
  1069. char *str, *end;
  1070. struct printf_spec spec = {0};
  1071. /* Reject out-of-range values early. Large positive sizes are
  1072. used for unknown buffer sizes. */
  1073. if (WARN_ON_ONCE((int) size < 0))
  1074. return 0;
  1075. str = buf;
  1076. end = buf + size;
  1077. /* Make sure end is always >= buf */
  1078. if (end < buf) {
  1079. end = ((void *)-1);
  1080. size = end - buf;
  1081. }
  1082. while (*fmt) {
  1083. const char *old_fmt = fmt;
  1084. int read = format_decode(fmt, &spec);
  1085. fmt += read;
  1086. switch (spec.type) {
  1087. case FORMAT_TYPE_NONE: {
  1088. int copy = read;
  1089. if (str < end) {
  1090. if (copy > end - str)
  1091. copy = end - str;
  1092. memcpy(str, old_fmt, copy);
  1093. }
  1094. str += read;
  1095. break;
  1096. }
  1097. case FORMAT_TYPE_WIDTH:
  1098. spec.field_width = va_arg(args, int);
  1099. break;
  1100. case FORMAT_TYPE_PRECISION:
  1101. spec.precision = va_arg(args, int);
  1102. break;
  1103. case FORMAT_TYPE_CHAR: {
  1104. char c;
  1105. if (!(spec.flags & LEFT)) {
  1106. while (--spec.field_width > 0) {
  1107. if (str < end)
  1108. *str = ' ';
  1109. ++str;
  1110. }
  1111. }
  1112. c = (unsigned char) va_arg(args, int);
  1113. if (str < end)
  1114. *str = c;
  1115. ++str;
  1116. while (--spec.field_width > 0) {
  1117. if (str < end)
  1118. *str = ' ';
  1119. ++str;
  1120. }
  1121. break;
  1122. }
  1123. case FORMAT_TYPE_STR:
  1124. str = string(str, end, va_arg(args, char *), spec);
  1125. break;
  1126. case FORMAT_TYPE_PTR:
  1127. str = pointer(fmt+1, str, end, va_arg(args, void *),
  1128. spec);
  1129. while (isalnum(*fmt))
  1130. fmt++;
  1131. break;
  1132. case FORMAT_TYPE_PERCENT_CHAR:
  1133. if (str < end)
  1134. *str = '%';
  1135. ++str;
  1136. break;
  1137. case FORMAT_TYPE_INVALID:
  1138. if (str < end)
  1139. *str = '%';
  1140. ++str;
  1141. break;
  1142. case FORMAT_TYPE_NRCHARS: {
  1143. int qualifier = spec.qualifier;
  1144. if (qualifier == 'l') {
  1145. long *ip = va_arg(args, long *);
  1146. *ip = (str - buf);
  1147. } else if (TOLOWER(qualifier) == 'z') {
  1148. size_t *ip = va_arg(args, size_t *);
  1149. *ip = (str - buf);
  1150. } else {
  1151. int *ip = va_arg(args, int *);
  1152. *ip = (str - buf);
  1153. }
  1154. break;
  1155. }
  1156. default:
  1157. switch (spec.type) {
  1158. case FORMAT_TYPE_LONG_LONG:
  1159. num = va_arg(args, long long);
  1160. break;
  1161. case FORMAT_TYPE_ULONG:
  1162. num = va_arg(args, unsigned long);
  1163. break;
  1164. case FORMAT_TYPE_LONG:
  1165. num = va_arg(args, long);
  1166. break;
  1167. case FORMAT_TYPE_SIZE_T:
  1168. num = va_arg(args, size_t);
  1169. break;
  1170. case FORMAT_TYPE_PTRDIFF:
  1171. num = va_arg(args, ptrdiff_t);
  1172. break;
  1173. case FORMAT_TYPE_UBYTE:
  1174. num = (unsigned char) va_arg(args, int);
  1175. break;
  1176. case FORMAT_TYPE_BYTE:
  1177. num = (signed char) va_arg(args, int);
  1178. break;
  1179. case FORMAT_TYPE_USHORT:
  1180. num = (unsigned short) va_arg(args, int);
  1181. break;
  1182. case FORMAT_TYPE_SHORT:
  1183. num = (short) va_arg(args, int);
  1184. break;
  1185. case FORMAT_TYPE_INT:
  1186. num = (int) va_arg(args, int);
  1187. break;
  1188. default:
  1189. num = va_arg(args, unsigned int);
  1190. }
  1191. str = number(str, end, num, spec);
  1192. }
  1193. }
  1194. if (size > 0) {
  1195. if (str < end)
  1196. *str = '\0';
  1197. else
  1198. end[-1] = '\0';
  1199. }
  1200. /* the trailing null byte doesn't count towards the total */
  1201. return str-buf;
  1202. }
  1203. EXPORT_SYMBOL(vsnprintf);
  1204. /**
  1205. * vscnprintf - Format a string and place it in a buffer
  1206. * @buf: The buffer to place the result into
  1207. * @size: The size of the buffer, including the trailing null space
  1208. * @fmt: The format string to use
  1209. * @args: Arguments for the format string
  1210. *
  1211. * The return value is the number of characters which have been written into
  1212. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  1213. * returns 0.
  1214. *
  1215. * Call this function if you are already dealing with a va_list.
  1216. * You probably want scnprintf() instead.
  1217. *
  1218. * See the vsnprintf() documentation for format string extensions over C99.
  1219. */
  1220. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1221. {
  1222. int i;
  1223. i = vsnprintf(buf, size, fmt, args);
  1224. return (i >= size) ? (size - 1) : i;
  1225. }
  1226. EXPORT_SYMBOL(vscnprintf);
  1227. /**
  1228. * snprintf - Format a string and place it in a buffer
  1229. * @buf: The buffer to place the result into
  1230. * @size: The size of the buffer, including the trailing null space
  1231. * @fmt: The format string to use
  1232. * @...: Arguments for the format string
  1233. *
  1234. * The return value is the number of characters which would be
  1235. * generated for the given input, excluding the trailing null,
  1236. * as per ISO C99. If the return is greater than or equal to
  1237. * @size, the resulting string is truncated.
  1238. *
  1239. * See the vsnprintf() documentation for format string extensions over C99.
  1240. */
  1241. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1242. {
  1243. va_list args;
  1244. int i;
  1245. va_start(args, fmt);
  1246. i = vsnprintf(buf, size, fmt, args);
  1247. va_end(args);
  1248. return i;
  1249. }
  1250. EXPORT_SYMBOL(snprintf);
  1251. /**
  1252. * scnprintf - Format a string and place it in a buffer
  1253. * @buf: The buffer to place the result into
  1254. * @size: The size of the buffer, including the trailing null space
  1255. * @fmt: The format string to use
  1256. * @...: Arguments for the format string
  1257. *
  1258. * The return value is the number of characters written into @buf not including
  1259. * the trailing '\0'. If @size is <= 0 the function returns 0.
  1260. */
  1261. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  1262. {
  1263. va_list args;
  1264. int i;
  1265. va_start(args, fmt);
  1266. i = vsnprintf(buf, size, fmt, args);
  1267. va_end(args);
  1268. return (i >= size) ? (size - 1) : i;
  1269. }
  1270. EXPORT_SYMBOL(scnprintf);
  1271. /**
  1272. * vsprintf - Format a string and place it in a buffer
  1273. * @buf: The buffer to place the result into
  1274. * @fmt: The format string to use
  1275. * @args: Arguments for the format string
  1276. *
  1277. * The function returns the number of characters written
  1278. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1279. * buffer overflows.
  1280. *
  1281. * Call this function if you are already dealing with a va_list.
  1282. * You probably want sprintf() instead.
  1283. *
  1284. * See the vsnprintf() documentation for format string extensions over C99.
  1285. */
  1286. int vsprintf(char *buf, const char *fmt, va_list args)
  1287. {
  1288. return vsnprintf(buf, INT_MAX, fmt, args);
  1289. }
  1290. EXPORT_SYMBOL(vsprintf);
  1291. /**
  1292. * sprintf - Format a string and place it in a buffer
  1293. * @buf: The buffer to place the result into
  1294. * @fmt: The format string to use
  1295. * @...: Arguments for the format string
  1296. *
  1297. * The function returns the number of characters written
  1298. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1299. * buffer overflows.
  1300. *
  1301. * See the vsnprintf() documentation for format string extensions over C99.
  1302. */
  1303. int sprintf(char *buf, const char *fmt, ...)
  1304. {
  1305. va_list args;
  1306. int i;
  1307. va_start(args, fmt);
  1308. i = vsnprintf(buf, INT_MAX, fmt, args);
  1309. va_end(args);
  1310. return i;
  1311. }
  1312. EXPORT_SYMBOL(sprintf);
  1313. #ifdef CONFIG_BINARY_PRINTF
  1314. /*
  1315. * bprintf service:
  1316. * vbin_printf() - VA arguments to binary data
  1317. * bstr_printf() - Binary data to text string
  1318. */
  1319. /**
  1320. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1321. * @bin_buf: The buffer to place args' binary value
  1322. * @size: The size of the buffer(by words(32bits), not characters)
  1323. * @fmt: The format string to use
  1324. * @args: Arguments for the format string
  1325. *
  1326. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1327. * is skiped.
  1328. *
  1329. * The return value is the number of words(32bits) which would be generated for
  1330. * the given input.
  1331. *
  1332. * NOTE:
  1333. * If the return value is greater than @size, the resulting bin_buf is NOT
  1334. * valid for bstr_printf().
  1335. */
  1336. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1337. {
  1338. struct printf_spec spec = {0};
  1339. char *str, *end;
  1340. str = (char *)bin_buf;
  1341. end = (char *)(bin_buf + size);
  1342. #define save_arg(type) \
  1343. do { \
  1344. if (sizeof(type) == 8) { \
  1345. unsigned long long value; \
  1346. str = PTR_ALIGN(str, sizeof(u32)); \
  1347. value = va_arg(args, unsigned long long); \
  1348. if (str + sizeof(type) <= end) { \
  1349. *(u32 *)str = *(u32 *)&value; \
  1350. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1351. } \
  1352. } else { \
  1353. unsigned long value; \
  1354. str = PTR_ALIGN(str, sizeof(type)); \
  1355. value = va_arg(args, int); \
  1356. if (str + sizeof(type) <= end) \
  1357. *(typeof(type) *)str = (type)value; \
  1358. } \
  1359. str += sizeof(type); \
  1360. } while (0)
  1361. while (*fmt) {
  1362. int read = format_decode(fmt, &spec);
  1363. fmt += read;
  1364. switch (spec.type) {
  1365. case FORMAT_TYPE_NONE:
  1366. case FORMAT_TYPE_INVALID:
  1367. case FORMAT_TYPE_PERCENT_CHAR:
  1368. break;
  1369. case FORMAT_TYPE_WIDTH:
  1370. case FORMAT_TYPE_PRECISION:
  1371. save_arg(int);
  1372. break;
  1373. case FORMAT_TYPE_CHAR:
  1374. save_arg(char);
  1375. break;
  1376. case FORMAT_TYPE_STR: {
  1377. const char *save_str = va_arg(args, char *);
  1378. size_t len;
  1379. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1380. || (unsigned long)save_str < PAGE_SIZE)
  1381. save_str = "(null)";
  1382. len = strlen(save_str) + 1;
  1383. if (str + len < end)
  1384. memcpy(str, save_str, len);
  1385. str += len;
  1386. break;
  1387. }
  1388. case FORMAT_TYPE_PTR:
  1389. save_arg(void *);
  1390. /* skip all alphanumeric pointer suffixes */
  1391. while (isalnum(*fmt))
  1392. fmt++;
  1393. break;
  1394. case FORMAT_TYPE_NRCHARS: {
  1395. /* skip %n 's argument */
  1396. int qualifier = spec.qualifier;
  1397. void *skip_arg;
  1398. if (qualifier == 'l')
  1399. skip_arg = va_arg(args, long *);
  1400. else if (TOLOWER(qualifier) == 'z')
  1401. skip_arg = va_arg(args, size_t *);
  1402. else
  1403. skip_arg = va_arg(args, int *);
  1404. break;
  1405. }
  1406. default:
  1407. switch (spec.type) {
  1408. case FORMAT_TYPE_LONG_LONG:
  1409. save_arg(long long);
  1410. break;
  1411. case FORMAT_TYPE_ULONG:
  1412. case FORMAT_TYPE_LONG:
  1413. save_arg(unsigned long);
  1414. break;
  1415. case FORMAT_TYPE_SIZE_T:
  1416. save_arg(size_t);
  1417. break;
  1418. case FORMAT_TYPE_PTRDIFF:
  1419. save_arg(ptrdiff_t);
  1420. break;
  1421. case FORMAT_TYPE_UBYTE:
  1422. case FORMAT_TYPE_BYTE:
  1423. save_arg(char);
  1424. break;
  1425. case FORMAT_TYPE_USHORT:
  1426. case FORMAT_TYPE_SHORT:
  1427. save_arg(short);
  1428. break;
  1429. default:
  1430. save_arg(int);
  1431. }
  1432. }
  1433. }
  1434. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1435. #undef save_arg
  1436. }
  1437. EXPORT_SYMBOL_GPL(vbin_printf);
  1438. /**
  1439. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1440. * @buf: The buffer to place the result into
  1441. * @size: The size of the buffer, including the trailing null space
  1442. * @fmt: The format string to use
  1443. * @bin_buf: Binary arguments for the format string
  1444. *
  1445. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1446. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1447. * a binary buffer that generated by vbin_printf.
  1448. *
  1449. * The format follows C99 vsnprintf, but has some extensions:
  1450. * see vsnprintf comment for details.
  1451. *
  1452. * The return value is the number of characters which would
  1453. * be generated for the given input, excluding the trailing
  1454. * '\0', as per ISO C99. If you want to have the exact
  1455. * number of characters written into @buf as return value
  1456. * (not including the trailing '\0'), use vscnprintf(). If the
  1457. * return is greater than or equal to @size, the resulting
  1458. * string is truncated.
  1459. */
  1460. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1461. {
  1462. struct printf_spec spec = {0};
  1463. char *str, *end;
  1464. const char *args = (const char *)bin_buf;
  1465. if (WARN_ON_ONCE((int) size < 0))
  1466. return 0;
  1467. str = buf;
  1468. end = buf + size;
  1469. #define get_arg(type) \
  1470. ({ \
  1471. typeof(type) value; \
  1472. if (sizeof(type) == 8) { \
  1473. args = PTR_ALIGN(args, sizeof(u32)); \
  1474. *(u32 *)&value = *(u32 *)args; \
  1475. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1476. } else { \
  1477. args = PTR_ALIGN(args, sizeof(type)); \
  1478. value = *(typeof(type) *)args; \
  1479. } \
  1480. args += sizeof(type); \
  1481. value; \
  1482. })
  1483. /* Make sure end is always >= buf */
  1484. if (end < buf) {
  1485. end = ((void *)-1);
  1486. size = end - buf;
  1487. }
  1488. while (*fmt) {
  1489. const char *old_fmt = fmt;
  1490. int read = format_decode(fmt, &spec);
  1491. fmt += read;
  1492. switch (spec.type) {
  1493. case FORMAT_TYPE_NONE: {
  1494. int copy = read;
  1495. if (str < end) {
  1496. if (copy > end - str)
  1497. copy = end - str;
  1498. memcpy(str, old_fmt, copy);
  1499. }
  1500. str += read;
  1501. break;
  1502. }
  1503. case FORMAT_TYPE_WIDTH:
  1504. spec.field_width = get_arg(int);
  1505. break;
  1506. case FORMAT_TYPE_PRECISION:
  1507. spec.precision = get_arg(int);
  1508. break;
  1509. case FORMAT_TYPE_CHAR: {
  1510. char c;
  1511. if (!(spec.flags & LEFT)) {
  1512. while (--spec.field_width > 0) {
  1513. if (str < end)
  1514. *str = ' ';
  1515. ++str;
  1516. }
  1517. }
  1518. c = (unsigned char) get_arg(char);
  1519. if (str < end)
  1520. *str = c;
  1521. ++str;
  1522. while (--spec.field_width > 0) {
  1523. if (str < end)
  1524. *str = ' ';
  1525. ++str;
  1526. }
  1527. break;
  1528. }
  1529. case FORMAT_TYPE_STR: {
  1530. const char *str_arg = args;
  1531. args += strlen(str_arg) + 1;
  1532. str = string(str, end, (char *)str_arg, spec);
  1533. break;
  1534. }
  1535. case FORMAT_TYPE_PTR:
  1536. str = pointer(fmt+1, str, end, get_arg(void *), spec);
  1537. while (isalnum(*fmt))
  1538. fmt++;
  1539. break;
  1540. case FORMAT_TYPE_PERCENT_CHAR:
  1541. case FORMAT_TYPE_INVALID:
  1542. if (str < end)
  1543. *str = '%';
  1544. ++str;
  1545. break;
  1546. case FORMAT_TYPE_NRCHARS:
  1547. /* skip */
  1548. break;
  1549. default: {
  1550. unsigned long long num;
  1551. switch (spec.type) {
  1552. case FORMAT_TYPE_LONG_LONG:
  1553. num = get_arg(long long);
  1554. break;
  1555. case FORMAT_TYPE_ULONG:
  1556. case FORMAT_TYPE_LONG:
  1557. num = get_arg(unsigned long);
  1558. break;
  1559. case FORMAT_TYPE_SIZE_T:
  1560. num = get_arg(size_t);
  1561. break;
  1562. case FORMAT_TYPE_PTRDIFF:
  1563. num = get_arg(ptrdiff_t);
  1564. break;
  1565. case FORMAT_TYPE_UBYTE:
  1566. num = get_arg(unsigned char);
  1567. break;
  1568. case FORMAT_TYPE_BYTE:
  1569. num = get_arg(signed char);
  1570. break;
  1571. case FORMAT_TYPE_USHORT:
  1572. num = get_arg(unsigned short);
  1573. break;
  1574. case FORMAT_TYPE_SHORT:
  1575. num = get_arg(short);
  1576. break;
  1577. case FORMAT_TYPE_UINT:
  1578. num = get_arg(unsigned int);
  1579. break;
  1580. default:
  1581. num = get_arg(int);
  1582. }
  1583. str = number(str, end, num, spec);
  1584. } /* default: */
  1585. } /* switch(spec.type) */
  1586. } /* while(*fmt) */
  1587. if (size > 0) {
  1588. if (str < end)
  1589. *str = '\0';
  1590. else
  1591. end[-1] = '\0';
  1592. }
  1593. #undef get_arg
  1594. /* the trailing null byte doesn't count towards the total */
  1595. return str - buf;
  1596. }
  1597. EXPORT_SYMBOL_GPL(bstr_printf);
  1598. /**
  1599. * bprintf - Parse a format string and place args' binary value in a buffer
  1600. * @bin_buf: The buffer to place args' binary value
  1601. * @size: The size of the buffer(by words(32bits), not characters)
  1602. * @fmt: The format string to use
  1603. * @...: Arguments for the format string
  1604. *
  1605. * The function returns the number of words(u32) written
  1606. * into @bin_buf.
  1607. */
  1608. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  1609. {
  1610. va_list args;
  1611. int ret;
  1612. va_start(args, fmt);
  1613. ret = vbin_printf(bin_buf, size, fmt, args);
  1614. va_end(args);
  1615. return ret;
  1616. }
  1617. EXPORT_SYMBOL_GPL(bprintf);
  1618. #endif /* CONFIG_BINARY_PRINTF */
  1619. /**
  1620. * vsscanf - Unformat a buffer into a list of arguments
  1621. * @buf: input buffer
  1622. * @fmt: format of buffer
  1623. * @args: arguments
  1624. */
  1625. int vsscanf(const char *buf, const char *fmt, va_list args)
  1626. {
  1627. const char *str = buf;
  1628. char *next;
  1629. char digit;
  1630. int num = 0;
  1631. int qualifier, base, field_width;
  1632. bool is_sign;
  1633. while (*fmt && *str) {
  1634. /* skip any white space in format */
  1635. /* white space in format matchs any amount of
  1636. * white space, including none, in the input.
  1637. */
  1638. if (isspace(*fmt)) {
  1639. fmt = skip_spaces(++fmt);
  1640. str = skip_spaces(str);
  1641. }
  1642. /* anything that is not a conversion must match exactly */
  1643. if (*fmt != '%' && *fmt) {
  1644. if (*fmt++ != *str++)
  1645. break;
  1646. continue;
  1647. }
  1648. if (!*fmt)
  1649. break;
  1650. ++fmt;
  1651. /* skip this conversion.
  1652. * advance both strings to next white space
  1653. */
  1654. if (*fmt == '*') {
  1655. while (!isspace(*fmt) && *fmt != '%' && *fmt)
  1656. fmt++;
  1657. while (!isspace(*str) && *str)
  1658. str++;
  1659. continue;
  1660. }
  1661. /* get field width */
  1662. field_width = -1;
  1663. if (isdigit(*fmt))
  1664. field_width = skip_atoi(&fmt);
  1665. /* get conversion qualifier */
  1666. qualifier = -1;
  1667. if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
  1668. TOLOWER(*fmt) == 'z') {
  1669. qualifier = *fmt++;
  1670. if (unlikely(qualifier == *fmt)) {
  1671. if (qualifier == 'h') {
  1672. qualifier = 'H';
  1673. fmt++;
  1674. } else if (qualifier == 'l') {
  1675. qualifier = 'L';
  1676. fmt++;
  1677. }
  1678. }
  1679. }
  1680. if (!*fmt || !*str)
  1681. break;
  1682. base = 10;
  1683. is_sign = 0;
  1684. switch (*fmt++) {
  1685. case 'c':
  1686. {
  1687. char *s = (char *)va_arg(args, char*);
  1688. if (field_width == -1)
  1689. field_width = 1;
  1690. do {
  1691. *s++ = *str++;
  1692. } while (--field_width > 0 && *str);
  1693. num++;
  1694. }
  1695. continue;
  1696. case 's':
  1697. {
  1698. char *s = (char *)va_arg(args, char *);
  1699. if (field_width == -1)
  1700. field_width = INT_MAX;
  1701. /* first, skip leading white space in buffer */
  1702. str = skip_spaces(str);
  1703. /* now copy until next white space */
  1704. while (*str && !isspace(*str) && field_width--)
  1705. *s++ = *str++;
  1706. *s = '\0';
  1707. num++;
  1708. }
  1709. continue;
  1710. case 'n':
  1711. /* return number of characters read so far */
  1712. {
  1713. int *i = (int *)va_arg(args, int*);
  1714. *i = str - buf;
  1715. }
  1716. continue;
  1717. case 'o':
  1718. base = 8;
  1719. break;
  1720. case 'x':
  1721. case 'X':
  1722. base = 16;
  1723. break;
  1724. case 'i':
  1725. base = 0;
  1726. case 'd':
  1727. is_sign = 1;
  1728. case 'u':
  1729. break;
  1730. case '%':
  1731. /* looking for '%' in str */
  1732. if (*str++ != '%')
  1733. return num;
  1734. continue;
  1735. default:
  1736. /* invalid format; stop here */
  1737. return num;
  1738. }
  1739. /* have some sort of integer conversion.
  1740. * first, skip white space in buffer.
  1741. */
  1742. str = skip_spaces(str);
  1743. digit = *str;
  1744. if (is_sign && digit == '-')
  1745. digit = *(str + 1);
  1746. if (!digit
  1747. || (base == 16 && !isxdigit(digit))
  1748. || (base == 10 && !isdigit(digit))
  1749. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1750. || (base == 0 && !isdigit(digit)))
  1751. break;
  1752. switch (qualifier) {
  1753. case 'H': /* that's 'hh' in format */
  1754. if (is_sign) {
  1755. signed char *s = (signed char *)va_arg(args, signed char *);
  1756. *s = (signed char)simple_strtol(str, &next, base);
  1757. } else {
  1758. unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
  1759. *s = (unsigned char)simple_strtoul(str, &next, base);
  1760. }
  1761. break;
  1762. case 'h':
  1763. if (is_sign) {
  1764. short *s = (short *)va_arg(args, short *);
  1765. *s = (short)simple_strtol(str, &next, base);
  1766. } else {
  1767. unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
  1768. *s = (unsigned short)simple_strtoul(str, &next, base);
  1769. }
  1770. break;
  1771. case 'l':
  1772. if (is_sign) {
  1773. long *l = (long *)va_arg(args, long *);
  1774. *l = simple_strtol(str, &next, base);
  1775. } else {
  1776. unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
  1777. *l = simple_strtoul(str, &next, base);
  1778. }
  1779. break;
  1780. case 'L':
  1781. if (is_sign) {
  1782. long long *l = (long long *)va_arg(args, long long *);
  1783. *l = simple_strtoll(str, &next, base);
  1784. } else {
  1785. unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
  1786. *l = simple_strtoull(str, &next, base);
  1787. }
  1788. break;
  1789. case 'Z':
  1790. case 'z':
  1791. {
  1792. size_t *s = (size_t *)va_arg(args, size_t *);
  1793. *s = (size_t)simple_strtoul(str, &next, base);
  1794. }
  1795. break;
  1796. default:
  1797. if (is_sign) {
  1798. int *i = (int *)va_arg(args, int *);
  1799. *i = (int)simple_strtol(str, &next, base);
  1800. } else {
  1801. unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
  1802. *i = (unsigned int)simple_strtoul(str, &next, base);
  1803. }
  1804. break;
  1805. }
  1806. num++;
  1807. if (!next)
  1808. break;
  1809. str = next;
  1810. }
  1811. /*
  1812. * Now we've come all the way through so either the input string or the
  1813. * format ended. In the former case, there can be a %n at the current
  1814. * position in the format that needs to be filled.
  1815. */
  1816. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1817. int *p = (int *)va_arg(args, int *);
  1818. *p = str - buf;
  1819. }
  1820. return num;
  1821. }
  1822. EXPORT_SYMBOL(vsscanf);
  1823. /**
  1824. * sscanf - Unformat a buffer into a list of arguments
  1825. * @buf: input buffer
  1826. * @fmt: formatting of buffer
  1827. * @...: resulting arguments
  1828. */
  1829. int sscanf(const char *buf, const char *fmt, ...)
  1830. {
  1831. va_list args;
  1832. int i;
  1833. va_start(args, fmt);
  1834. i = vsscanf(buf, fmt, args);
  1835. va_end(args);
  1836. return i;
  1837. }
  1838. EXPORT_SYMBOL(sscanf);