vsprintf.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 <asm/page.h> /* for PAGE_SIZE */
  26. #include <asm/div64.h>
  27. #include <asm/sections.h> /* for dereference_function_descriptor() */
  28. /* Works only for digits and letters, but small and fast */
  29. #define TOLOWER(x) ((x) | 0x20)
  30. static unsigned int simple_guess_base(const char *cp)
  31. {
  32. if (cp[0] == '0') {
  33. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  34. return 16;
  35. else
  36. return 8;
  37. } else {
  38. return 10;
  39. }
  40. }
  41. /**
  42. * simple_strtoul - convert a string to an unsigned long
  43. * @cp: The start of the string
  44. * @endp: A pointer to the end of the parsed string will be placed here
  45. * @base: The number base to use
  46. */
  47. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  48. {
  49. unsigned long result = 0;
  50. if (!base)
  51. base = simple_guess_base(cp);
  52. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  53. cp += 2;
  54. while (isxdigit(*cp)) {
  55. unsigned int value;
  56. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  57. if (value >= base)
  58. break;
  59. result = result * base + value;
  60. cp++;
  61. }
  62. if (endp)
  63. *endp = (char *)cp;
  64. return result;
  65. }
  66. EXPORT_SYMBOL(simple_strtoul);
  67. /**
  68. * simple_strtol - convert a string to a signed long
  69. * @cp: The start of the string
  70. * @endp: A pointer to the end of the parsed string will be placed here
  71. * @base: The number base to use
  72. */
  73. long simple_strtol(const char *cp, char **endp, unsigned int base)
  74. {
  75. if(*cp == '-')
  76. return -simple_strtoul(cp + 1, endp, base);
  77. return simple_strtoul(cp, endp, base);
  78. }
  79. EXPORT_SYMBOL(simple_strtol);
  80. /**
  81. * simple_strtoull - convert a string to an unsigned long 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. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  87. {
  88. unsigned long long result = 0;
  89. if (!base)
  90. base = simple_guess_base(cp);
  91. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  92. cp += 2;
  93. while (isxdigit(*cp)) {
  94. unsigned int value;
  95. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  96. if (value >= base)
  97. break;
  98. result = result * base + value;
  99. cp++;
  100. }
  101. if (endp)
  102. *endp = (char *)cp;
  103. return result;
  104. }
  105. EXPORT_SYMBOL(simple_strtoull);
  106. /**
  107. * simple_strtoll - convert a string to a signed long long
  108. * @cp: The start of the string
  109. * @endp: A pointer to the end of the parsed string will be placed here
  110. * @base: The number base to use
  111. */
  112. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  113. {
  114. if(*cp=='-')
  115. return -simple_strtoull(cp + 1, endp, base);
  116. return simple_strtoull(cp, endp, base);
  117. }
  118. /**
  119. * strict_strtoul - convert a string to an unsigned long strictly
  120. * @cp: The string to be converted
  121. * @base: The number base to use
  122. * @res: The converted result value
  123. *
  124. * strict_strtoul converts a string to an unsigned long only if the
  125. * string is really an unsigned long string, any string containing
  126. * any invalid char at the tail will be rejected and -EINVAL is returned,
  127. * only a newline char at the tail is acceptible because people generally
  128. * change a module parameter in the following way:
  129. *
  130. * echo 1024 > /sys/module/e1000/parameters/copybreak
  131. *
  132. * echo will append a newline to the tail.
  133. *
  134. * It returns 0 if conversion is successful and *res is set to the converted
  135. * value, otherwise it returns -EINVAL and *res is set to 0.
  136. *
  137. * simple_strtoul just ignores the successive invalid characters and
  138. * return the converted value of prefix part of the string.
  139. */
  140. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  141. {
  142. char *tail;
  143. unsigned long val;
  144. size_t len;
  145. *res = 0;
  146. len = strlen(cp);
  147. if (len == 0)
  148. return -EINVAL;
  149. val = simple_strtoul(cp, &tail, base);
  150. if ((*tail == '\0') ||
  151. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  152. *res = val;
  153. return 0;
  154. }
  155. return -EINVAL;
  156. }
  157. EXPORT_SYMBOL(strict_strtoul);
  158. /**
  159. * strict_strtol - convert a string to a long strictly
  160. * @cp: The string to be converted
  161. * @base: The number base to use
  162. * @res: The converted result value
  163. *
  164. * strict_strtol is similiar to strict_strtoul, but it allows the first
  165. * character of a string is '-'.
  166. *
  167. * It returns 0 if conversion is successful and *res is set to the converted
  168. * value, otherwise it returns -EINVAL and *res is set to 0.
  169. */
  170. int strict_strtol(const char *cp, unsigned int base, long *res)
  171. {
  172. int ret;
  173. if (*cp == '-') {
  174. ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
  175. if (!ret)
  176. *res = -(*res);
  177. } else {
  178. ret = strict_strtoul(cp, base, (unsigned long *)res);
  179. }
  180. return ret;
  181. }
  182. EXPORT_SYMBOL(strict_strtol);
  183. /**
  184. * strict_strtoull - convert a string to an unsigned long long strictly
  185. * @cp: The string to be converted
  186. * @base: The number base to use
  187. * @res: The converted result value
  188. *
  189. * strict_strtoull converts a string to an unsigned long long only if the
  190. * string is really an unsigned long long string, any string containing
  191. * any invalid char at the tail will be rejected and -EINVAL is returned,
  192. * only a newline char at the tail is acceptible because people generally
  193. * change a module parameter in the following way:
  194. *
  195. * echo 1024 > /sys/module/e1000/parameters/copybreak
  196. *
  197. * echo will append a newline to the tail of the string.
  198. *
  199. * It returns 0 if conversion is successful and *res is set to the converted
  200. * value, otherwise it returns -EINVAL and *res is set to 0.
  201. *
  202. * simple_strtoull just ignores the successive invalid characters and
  203. * return the converted value of prefix part of the string.
  204. */
  205. int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
  206. {
  207. char *tail;
  208. unsigned long long val;
  209. size_t len;
  210. *res = 0;
  211. len = strlen(cp);
  212. if (len == 0)
  213. return -EINVAL;
  214. val = simple_strtoull(cp, &tail, base);
  215. if ((*tail == '\0') ||
  216. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  217. *res = val;
  218. return 0;
  219. }
  220. return -EINVAL;
  221. }
  222. EXPORT_SYMBOL(strict_strtoull);
  223. /**
  224. * strict_strtoll - convert a string to a long long strictly
  225. * @cp: The string to be converted
  226. * @base: The number base to use
  227. * @res: The converted result value
  228. *
  229. * strict_strtoll is similiar to strict_strtoull, but it allows the first
  230. * character of a string is '-'.
  231. *
  232. * It returns 0 if conversion is successful and *res is set to the converted
  233. * value, otherwise it returns -EINVAL and *res is set to 0.
  234. */
  235. int strict_strtoll(const char *cp, unsigned int base, long long *res)
  236. {
  237. int ret;
  238. if (*cp == '-') {
  239. ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
  240. if (!ret)
  241. *res = -(*res);
  242. } else {
  243. ret = strict_strtoull(cp, base, (unsigned long long *)res);
  244. }
  245. return ret;
  246. }
  247. EXPORT_SYMBOL(strict_strtoll);
  248. static int skip_atoi(const char **s)
  249. {
  250. int i=0;
  251. while (isdigit(**s))
  252. i = i*10 + *((*s)++) - '0';
  253. return i;
  254. }
  255. /* Decimal conversion is by far the most typical, and is used
  256. * for /proc and /sys data. This directly impacts e.g. top performance
  257. * with many processes running. We optimize it for speed
  258. * using code from
  259. * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
  260. * (with permission from the author, Douglas W. Jones). */
  261. /* Formats correctly any integer in [0,99999].
  262. * Outputs from one to five digits depending on input.
  263. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
  264. static char* put_dec_trunc(char *buf, unsigned q)
  265. {
  266. unsigned d3, d2, d1, d0;
  267. d1 = (q>>4) & 0xf;
  268. d2 = (q>>8) & 0xf;
  269. d3 = (q>>12);
  270. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  271. q = (d0 * 0xcd) >> 11;
  272. d0 = d0 - 10*q;
  273. *buf++ = d0 + '0'; /* least significant digit */
  274. d1 = q + 9*d3 + 5*d2 + d1;
  275. if (d1 != 0) {
  276. q = (d1 * 0xcd) >> 11;
  277. d1 = d1 - 10*q;
  278. *buf++ = d1 + '0'; /* next digit */
  279. d2 = q + 2*d2;
  280. if ((d2 != 0) || (d3 != 0)) {
  281. q = (d2 * 0xd) >> 7;
  282. d2 = d2 - 10*q;
  283. *buf++ = d2 + '0'; /* next digit */
  284. d3 = q + 4*d3;
  285. if (d3 != 0) {
  286. q = (d3 * 0xcd) >> 11;
  287. d3 = d3 - 10*q;
  288. *buf++ = d3 + '0'; /* next digit */
  289. if (q != 0)
  290. *buf++ = q + '0'; /* most sign. digit */
  291. }
  292. }
  293. }
  294. return buf;
  295. }
  296. /* Same with if's removed. Always emits five digits */
  297. static char* put_dec_full(char *buf, unsigned q)
  298. {
  299. /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
  300. /* but anyway, gcc produces better code with full-sized ints */
  301. unsigned d3, d2, d1, d0;
  302. d1 = (q>>4) & 0xf;
  303. d2 = (q>>8) & 0xf;
  304. d3 = (q>>12);
  305. /* Possible ways to approx. divide by 10 */
  306. /* gcc -O2 replaces multiply with shifts and adds */
  307. // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
  308. // (x * 0x67) >> 10: 1100111
  309. // (x * 0x34) >> 9: 110100 - same
  310. // (x * 0x1a) >> 8: 11010 - same
  311. // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
  312. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  313. q = (d0 * 0xcd) >> 11;
  314. d0 = d0 - 10*q;
  315. *buf++ = d0 + '0';
  316. d1 = q + 9*d3 + 5*d2 + d1;
  317. q = (d1 * 0xcd) >> 11;
  318. d1 = d1 - 10*q;
  319. *buf++ = d1 + '0';
  320. d2 = q + 2*d2;
  321. q = (d2 * 0xd) >> 7;
  322. d2 = d2 - 10*q;
  323. *buf++ = d2 + '0';
  324. d3 = q + 4*d3;
  325. q = (d3 * 0xcd) >> 11; /* - shorter code */
  326. /* q = (d3 * 0x67) >> 10; - would also work */
  327. d3 = d3 - 10*q;
  328. *buf++ = d3 + '0';
  329. *buf++ = q + '0';
  330. return buf;
  331. }
  332. /* No inlining helps gcc to use registers better */
  333. static noinline char* put_dec(char *buf, unsigned long long num)
  334. {
  335. while (1) {
  336. unsigned rem;
  337. if (num < 100000)
  338. return put_dec_trunc(buf, num);
  339. rem = do_div(num, 100000);
  340. buf = put_dec_full(buf, rem);
  341. }
  342. }
  343. #define ZEROPAD 1 /* pad with zero */
  344. #define SIGN 2 /* unsigned/signed long */
  345. #define PLUS 4 /* show plus */
  346. #define SPACE 8 /* space if plus */
  347. #define LEFT 16 /* left justified */
  348. #define SMALL 32 /* Must be 32 == 0x20 */
  349. #define SPECIAL 64 /* 0x */
  350. static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
  351. {
  352. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  353. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  354. char tmp[66];
  355. char sign;
  356. char locase;
  357. int need_pfx = ((type & SPECIAL) && base != 10);
  358. int i;
  359. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  360. * produces same digits or (maybe lowercased) letters */
  361. locase = (type & SMALL);
  362. if (type & LEFT)
  363. type &= ~ZEROPAD;
  364. sign = 0;
  365. if (type & SIGN) {
  366. if ((signed long long) num < 0) {
  367. sign = '-';
  368. num = - (signed long long) num;
  369. size--;
  370. } else if (type & PLUS) {
  371. sign = '+';
  372. size--;
  373. } else if (type & SPACE) {
  374. sign = ' ';
  375. size--;
  376. }
  377. }
  378. if (need_pfx) {
  379. size--;
  380. if (base == 16)
  381. size--;
  382. }
  383. /* generate full string in tmp[], in reverse order */
  384. i = 0;
  385. if (num == 0)
  386. tmp[i++] = '0';
  387. /* Generic code, for any base:
  388. else do {
  389. tmp[i++] = (digits[do_div(num,base)] | locase);
  390. } while (num != 0);
  391. */
  392. else if (base != 10) { /* 8 or 16 */
  393. int mask = base - 1;
  394. int shift = 3;
  395. if (base == 16) shift = 4;
  396. do {
  397. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  398. num >>= shift;
  399. } while (num);
  400. } else { /* base 10 */
  401. i = put_dec(tmp, num) - tmp;
  402. }
  403. /* printing 100 using %2d gives "100", not "00" */
  404. if (i > precision)
  405. precision = i;
  406. /* leading space padding */
  407. size -= precision;
  408. if (!(type & (ZEROPAD+LEFT))) {
  409. while(--size >= 0) {
  410. if (buf < end)
  411. *buf = ' ';
  412. ++buf;
  413. }
  414. }
  415. /* sign */
  416. if (sign) {
  417. if (buf < end)
  418. *buf = sign;
  419. ++buf;
  420. }
  421. /* "0x" / "0" prefix */
  422. if (need_pfx) {
  423. if (buf < end)
  424. *buf = '0';
  425. ++buf;
  426. if (base == 16) {
  427. if (buf < end)
  428. *buf = ('X' | locase);
  429. ++buf;
  430. }
  431. }
  432. /* zero or space padding */
  433. if (!(type & LEFT)) {
  434. char c = (type & ZEROPAD) ? '0' : ' ';
  435. while (--size >= 0) {
  436. if (buf < end)
  437. *buf = c;
  438. ++buf;
  439. }
  440. }
  441. /* hmm even more zero padding? */
  442. while (i <= --precision) {
  443. if (buf < end)
  444. *buf = '0';
  445. ++buf;
  446. }
  447. /* actual digits of result */
  448. while (--i >= 0) {
  449. if (buf < end)
  450. *buf = tmp[i];
  451. ++buf;
  452. }
  453. /* trailing space padding */
  454. while (--size >= 0) {
  455. if (buf < end)
  456. *buf = ' ';
  457. ++buf;
  458. }
  459. return buf;
  460. }
  461. static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
  462. {
  463. int len, i;
  464. if ((unsigned long)s < PAGE_SIZE)
  465. s = "<NULL>";
  466. len = strnlen(s, precision);
  467. if (!(flags & LEFT)) {
  468. while (len < field_width--) {
  469. if (buf < end)
  470. *buf = ' ';
  471. ++buf;
  472. }
  473. }
  474. for (i = 0; i < len; ++i) {
  475. if (buf < end)
  476. *buf = *s;
  477. ++buf; ++s;
  478. }
  479. while (len < field_width--) {
  480. if (buf < end)
  481. *buf = ' ';
  482. ++buf;
  483. }
  484. return buf;
  485. }
  486. static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  487. {
  488. unsigned long value = (unsigned long) ptr;
  489. #ifdef CONFIG_KALLSYMS
  490. char sym[KSYM_SYMBOL_LEN];
  491. sprint_symbol(sym, value);
  492. return string(buf, end, sym, field_width, precision, flags);
  493. #else
  494. field_width = 2*sizeof(void *);
  495. flags |= SPECIAL | SMALL | ZEROPAD;
  496. return number(buf, end, value, 16, field_width, precision, flags);
  497. #endif
  498. }
  499. static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
  500. {
  501. #ifndef IO_RSRC_PRINTK_SIZE
  502. #define IO_RSRC_PRINTK_SIZE 4
  503. #endif
  504. #ifndef MEM_RSRC_PRINTK_SIZE
  505. #define MEM_RSRC_PRINTK_SIZE 8
  506. #endif
  507. /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
  508. char sym[4*sizeof(resource_size_t) + 8];
  509. char *p = sym, *pend = sym + sizeof(sym);
  510. int size = -1;
  511. if (res->flags & IORESOURCE_IO)
  512. size = IO_RSRC_PRINTK_SIZE;
  513. else if (res->flags & IORESOURCE_MEM)
  514. size = MEM_RSRC_PRINTK_SIZE;
  515. *p++ = '[';
  516. p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
  517. *p++ = '-';
  518. p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
  519. *p++ = ']';
  520. *p = 0;
  521. return string(buf, end, sym, field_width, precision, flags);
  522. }
  523. static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
  524. int precision, int flags)
  525. {
  526. char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
  527. char *p = mac_addr;
  528. int i;
  529. for (i = 0; i < 6; i++) {
  530. p = pack_hex_byte(p, addr[i]);
  531. if (!(flags & SPECIAL) && i != 5)
  532. *p++ = ':';
  533. }
  534. *p = '\0';
  535. return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
  536. }
  537. static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
  538. int precision, int flags)
  539. {
  540. char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
  541. char *p = ip6_addr;
  542. int i;
  543. for (i = 0; i < 8; i++) {
  544. p = pack_hex_byte(p, addr[2 * i]);
  545. p = pack_hex_byte(p, addr[2 * i + 1]);
  546. if (!(flags & SPECIAL) && i != 7)
  547. *p++ = ':';
  548. }
  549. *p = '\0';
  550. return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
  551. }
  552. static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
  553. int precision, int flags)
  554. {
  555. char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
  556. char *p = ip4_addr;
  557. int i;
  558. for (i = 0; i < 4; i++) {
  559. p = put_dec_trunc(p, addr[i]);
  560. if (i != 3)
  561. *p++ = '.';
  562. }
  563. *p = '\0';
  564. return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
  565. }
  566. /*
  567. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  568. * by an extra set of alphanumeric characters that are extended format
  569. * specifiers.
  570. *
  571. * Right now we handle:
  572. *
  573. * - 'F' For symbolic function descriptor pointers
  574. * - 'S' For symbolic direct pointers
  575. * - 'R' For a struct resource pointer, it prints the range of
  576. * addresses (not the name nor the flags)
  577. * - 'M' For a 6-byte MAC address, it prints the address in the
  578. * usual colon-separated hex notation
  579. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  580. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  581. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  582. * currently the same
  583. *
  584. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  585. * function pointers are really function descriptors, which contain a
  586. * pointer to the real address.
  587. */
  588. static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  589. {
  590. switch (*fmt) {
  591. case 'F':
  592. ptr = dereference_function_descriptor(ptr);
  593. /* Fallthrough */
  594. case 'S':
  595. return symbol_string(buf, end, ptr, field_width, precision, flags);
  596. case 'R':
  597. return resource_string(buf, end, ptr, field_width, precision, flags);
  598. case 'M':
  599. return mac_address_string(buf, end, ptr, field_width, precision, flags);
  600. case 'i':
  601. flags |= SPECIAL;
  602. /* Fallthrough */
  603. case 'I':
  604. if (fmt[1] == '6')
  605. return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
  606. if (fmt[1] == '4')
  607. return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
  608. flags &= ~SPECIAL;
  609. break;
  610. }
  611. flags |= SMALL;
  612. if (field_width == -1) {
  613. field_width = 2*sizeof(void *);
  614. flags |= ZEROPAD;
  615. }
  616. return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
  617. }
  618. /**
  619. * vsnprintf - Format a string and place it in a buffer
  620. * @buf: The buffer to place the result into
  621. * @size: The size of the buffer, including the trailing null space
  622. * @fmt: The format string to use
  623. * @args: Arguments for the format string
  624. *
  625. * This function follows C99 vsnprintf, but has some extensions:
  626. * %pS output the name of a text symbol
  627. * %pF output the name of a function pointer
  628. * %pR output the address range in a struct resource
  629. *
  630. * The return value is the number of characters which would
  631. * be generated for the given input, excluding the trailing
  632. * '\0', as per ISO C99. If you want to have the exact
  633. * number of characters written into @buf as return value
  634. * (not including the trailing '\0'), use vscnprintf(). If the
  635. * return is greater than or equal to @size, the resulting
  636. * string is truncated.
  637. *
  638. * Call this function if you are already dealing with a va_list.
  639. * You probably want snprintf() instead.
  640. */
  641. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  642. {
  643. unsigned long long num;
  644. int base;
  645. char *str, *end, c;
  646. int flags; /* flags to number() */
  647. int field_width; /* width of output field */
  648. int precision; /* min. # of digits for integers; max
  649. number of chars for from string */
  650. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  651. /* 'z' support added 23/7/1999 S.H. */
  652. /* 'z' changed to 'Z' --davidm 1/25/99 */
  653. /* 't' added for ptrdiff_t */
  654. /* Reject out-of-range values early. Large positive sizes are
  655. used for unknown buffer sizes. */
  656. if (unlikely((int) size < 0)) {
  657. /* There can be only one.. */
  658. static char warn = 1;
  659. WARN_ON(warn);
  660. warn = 0;
  661. return 0;
  662. }
  663. str = buf;
  664. end = buf + size;
  665. /* Make sure end is always >= buf */
  666. if (end < buf) {
  667. end = ((void *)-1);
  668. size = end - buf;
  669. }
  670. for (; *fmt ; ++fmt) {
  671. if (*fmt != '%') {
  672. if (str < end)
  673. *str = *fmt;
  674. ++str;
  675. continue;
  676. }
  677. /* process flags */
  678. flags = 0;
  679. repeat:
  680. ++fmt; /* this also skips first '%' */
  681. switch (*fmt) {
  682. case '-': flags |= LEFT; goto repeat;
  683. case '+': flags |= PLUS; goto repeat;
  684. case ' ': flags |= SPACE; goto repeat;
  685. case '#': flags |= SPECIAL; goto repeat;
  686. case '0': flags |= ZEROPAD; goto repeat;
  687. }
  688. /* get field width */
  689. field_width = -1;
  690. if (isdigit(*fmt))
  691. field_width = skip_atoi(&fmt);
  692. else if (*fmt == '*') {
  693. ++fmt;
  694. /* it's the next argument */
  695. field_width = va_arg(args, int);
  696. if (field_width < 0) {
  697. field_width = -field_width;
  698. flags |= LEFT;
  699. }
  700. }
  701. /* get the precision */
  702. precision = -1;
  703. if (*fmt == '.') {
  704. ++fmt;
  705. if (isdigit(*fmt))
  706. precision = skip_atoi(&fmt);
  707. else if (*fmt == '*') {
  708. ++fmt;
  709. /* it's the next argument */
  710. precision = va_arg(args, int);
  711. }
  712. if (precision < 0)
  713. precision = 0;
  714. }
  715. /* get the conversion qualifier */
  716. qualifier = -1;
  717. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  718. *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
  719. qualifier = *fmt;
  720. ++fmt;
  721. if (qualifier == 'l' && *fmt == 'l') {
  722. qualifier = 'L';
  723. ++fmt;
  724. }
  725. }
  726. /* default base */
  727. base = 10;
  728. switch (*fmt) {
  729. case 'c':
  730. if (!(flags & LEFT)) {
  731. while (--field_width > 0) {
  732. if (str < end)
  733. *str = ' ';
  734. ++str;
  735. }
  736. }
  737. c = (unsigned char) va_arg(args, int);
  738. if (str < end)
  739. *str = c;
  740. ++str;
  741. while (--field_width > 0) {
  742. if (str < end)
  743. *str = ' ';
  744. ++str;
  745. }
  746. continue;
  747. case 's':
  748. str = string(str, end, va_arg(args, char *), field_width, precision, flags);
  749. continue;
  750. case 'p':
  751. str = pointer(fmt+1, str, end,
  752. va_arg(args, void *),
  753. field_width, precision, flags);
  754. /* Skip all alphanumeric pointer suffixes */
  755. while (isalnum(fmt[1]))
  756. fmt++;
  757. continue;
  758. case 'n':
  759. /* FIXME:
  760. * What does C99 say about the overflow case here? */
  761. if (qualifier == 'l') {
  762. long * ip = va_arg(args, long *);
  763. *ip = (str - buf);
  764. } else if (qualifier == 'Z' || qualifier == 'z') {
  765. size_t * ip = va_arg(args, size_t *);
  766. *ip = (str - buf);
  767. } else {
  768. int * ip = va_arg(args, int *);
  769. *ip = (str - buf);
  770. }
  771. continue;
  772. case '%':
  773. if (str < end)
  774. *str = '%';
  775. ++str;
  776. continue;
  777. /* integer number formats - set up the flags and "break" */
  778. case 'o':
  779. base = 8;
  780. break;
  781. case 'x':
  782. flags |= SMALL;
  783. case 'X':
  784. base = 16;
  785. break;
  786. case 'd':
  787. case 'i':
  788. flags |= SIGN;
  789. case 'u':
  790. break;
  791. default:
  792. if (str < end)
  793. *str = '%';
  794. ++str;
  795. if (*fmt) {
  796. if (str < end)
  797. *str = *fmt;
  798. ++str;
  799. } else {
  800. --fmt;
  801. }
  802. continue;
  803. }
  804. if (qualifier == 'L')
  805. num = va_arg(args, long long);
  806. else if (qualifier == 'l') {
  807. num = va_arg(args, unsigned long);
  808. if (flags & SIGN)
  809. num = (signed long) num;
  810. } else if (qualifier == 'Z' || qualifier == 'z') {
  811. num = va_arg(args, size_t);
  812. } else if (qualifier == 't') {
  813. num = va_arg(args, ptrdiff_t);
  814. } else if (qualifier == 'h') {
  815. num = (unsigned short) va_arg(args, int);
  816. if (flags & SIGN)
  817. num = (signed short) num;
  818. } else {
  819. num = va_arg(args, unsigned int);
  820. if (flags & SIGN)
  821. num = (signed int) num;
  822. }
  823. str = number(str, end, num, base,
  824. field_width, precision, flags);
  825. }
  826. if (size > 0) {
  827. if (str < end)
  828. *str = '\0';
  829. else
  830. end[-1] = '\0';
  831. }
  832. /* the trailing null byte doesn't count towards the total */
  833. return str-buf;
  834. }
  835. EXPORT_SYMBOL(vsnprintf);
  836. /**
  837. * vscnprintf - Format a string and place it in a buffer
  838. * @buf: The buffer to place the result into
  839. * @size: The size of the buffer, including the trailing null space
  840. * @fmt: The format string to use
  841. * @args: Arguments for the format string
  842. *
  843. * The return value is the number of characters which have been written into
  844. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  845. * returns 0.
  846. *
  847. * Call this function if you are already dealing with a va_list.
  848. * You probably want scnprintf() instead.
  849. *
  850. * See the vsnprintf() documentation for format string extensions over C99.
  851. */
  852. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  853. {
  854. int i;
  855. i=vsnprintf(buf,size,fmt,args);
  856. return (i >= size) ? (size - 1) : i;
  857. }
  858. EXPORT_SYMBOL(vscnprintf);
  859. /**
  860. * snprintf - Format a string and place it in a buffer
  861. * @buf: The buffer to place the result into
  862. * @size: The size of the buffer, including the trailing null space
  863. * @fmt: The format string to use
  864. * @...: Arguments for the format string
  865. *
  866. * The return value is the number of characters which would be
  867. * generated for the given input, excluding the trailing null,
  868. * as per ISO C99. If the return is greater than or equal to
  869. * @size, the resulting string is truncated.
  870. *
  871. * See the vsnprintf() documentation for format string extensions over C99.
  872. */
  873. int snprintf(char * buf, size_t size, const char *fmt, ...)
  874. {
  875. va_list args;
  876. int i;
  877. va_start(args, fmt);
  878. i=vsnprintf(buf,size,fmt,args);
  879. va_end(args);
  880. return i;
  881. }
  882. EXPORT_SYMBOL(snprintf);
  883. /**
  884. * scnprintf - Format a string and place it in a buffer
  885. * @buf: The buffer to place the result into
  886. * @size: The size of the buffer, including the trailing null space
  887. * @fmt: The format string to use
  888. * @...: Arguments for the format string
  889. *
  890. * The return value is the number of characters written into @buf not including
  891. * the trailing '\0'. If @size is <= 0 the function returns 0.
  892. */
  893. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  894. {
  895. va_list args;
  896. int i;
  897. va_start(args, fmt);
  898. i = vsnprintf(buf, size, fmt, args);
  899. va_end(args);
  900. return (i >= size) ? (size - 1) : i;
  901. }
  902. EXPORT_SYMBOL(scnprintf);
  903. /**
  904. * vsprintf - Format a string and place it in a buffer
  905. * @buf: The buffer to place the result into
  906. * @fmt: The format string to use
  907. * @args: Arguments for the format string
  908. *
  909. * The function returns the number of characters written
  910. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  911. * buffer overflows.
  912. *
  913. * Call this function if you are already dealing with a va_list.
  914. * You probably want sprintf() instead.
  915. *
  916. * See the vsnprintf() documentation for format string extensions over C99.
  917. */
  918. int vsprintf(char *buf, const char *fmt, va_list args)
  919. {
  920. return vsnprintf(buf, INT_MAX, fmt, args);
  921. }
  922. EXPORT_SYMBOL(vsprintf);
  923. /**
  924. * sprintf - Format a string and place it in a buffer
  925. * @buf: The buffer to place the result into
  926. * @fmt: The format string to use
  927. * @...: Arguments for the format string
  928. *
  929. * The function returns the number of characters written
  930. * into @buf. Use snprintf() or scnprintf() in order to avoid
  931. * buffer overflows.
  932. *
  933. * See the vsnprintf() documentation for format string extensions over C99.
  934. */
  935. int sprintf(char * buf, const char *fmt, ...)
  936. {
  937. va_list args;
  938. int i;
  939. va_start(args, fmt);
  940. i=vsnprintf(buf, INT_MAX, fmt, args);
  941. va_end(args);
  942. return i;
  943. }
  944. EXPORT_SYMBOL(sprintf);
  945. /**
  946. * vsscanf - Unformat a buffer into a list of arguments
  947. * @buf: input buffer
  948. * @fmt: format of buffer
  949. * @args: arguments
  950. */
  951. int vsscanf(const char * buf, const char * fmt, va_list args)
  952. {
  953. const char *str = buf;
  954. char *next;
  955. char digit;
  956. int num = 0;
  957. int qualifier;
  958. int base;
  959. int field_width;
  960. int is_sign = 0;
  961. while(*fmt && *str) {
  962. /* skip any white space in format */
  963. /* white space in format matchs any amount of
  964. * white space, including none, in the input.
  965. */
  966. if (isspace(*fmt)) {
  967. while (isspace(*fmt))
  968. ++fmt;
  969. while (isspace(*str))
  970. ++str;
  971. }
  972. /* anything that is not a conversion must match exactly */
  973. if (*fmt != '%' && *fmt) {
  974. if (*fmt++ != *str++)
  975. break;
  976. continue;
  977. }
  978. if (!*fmt)
  979. break;
  980. ++fmt;
  981. /* skip this conversion.
  982. * advance both strings to next white space
  983. */
  984. if (*fmt == '*') {
  985. while (!isspace(*fmt) && *fmt)
  986. fmt++;
  987. while (!isspace(*str) && *str)
  988. str++;
  989. continue;
  990. }
  991. /* get field width */
  992. field_width = -1;
  993. if (isdigit(*fmt))
  994. field_width = skip_atoi(&fmt);
  995. /* get conversion qualifier */
  996. qualifier = -1;
  997. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  998. *fmt == 'Z' || *fmt == 'z') {
  999. qualifier = *fmt++;
  1000. if (unlikely(qualifier == *fmt)) {
  1001. if (qualifier == 'h') {
  1002. qualifier = 'H';
  1003. fmt++;
  1004. } else if (qualifier == 'l') {
  1005. qualifier = 'L';
  1006. fmt++;
  1007. }
  1008. }
  1009. }
  1010. base = 10;
  1011. is_sign = 0;
  1012. if (!*fmt || !*str)
  1013. break;
  1014. switch(*fmt++) {
  1015. case 'c':
  1016. {
  1017. char *s = (char *) va_arg(args,char*);
  1018. if (field_width == -1)
  1019. field_width = 1;
  1020. do {
  1021. *s++ = *str++;
  1022. } while (--field_width > 0 && *str);
  1023. num++;
  1024. }
  1025. continue;
  1026. case 's':
  1027. {
  1028. char *s = (char *) va_arg(args, char *);
  1029. if(field_width == -1)
  1030. field_width = INT_MAX;
  1031. /* first, skip leading white space in buffer */
  1032. while (isspace(*str))
  1033. str++;
  1034. /* now copy until next white space */
  1035. while (*str && !isspace(*str) && field_width--) {
  1036. *s++ = *str++;
  1037. }
  1038. *s = '\0';
  1039. num++;
  1040. }
  1041. continue;
  1042. case 'n':
  1043. /* return number of characters read so far */
  1044. {
  1045. int *i = (int *)va_arg(args,int*);
  1046. *i = str - buf;
  1047. }
  1048. continue;
  1049. case 'o':
  1050. base = 8;
  1051. break;
  1052. case 'x':
  1053. case 'X':
  1054. base = 16;
  1055. break;
  1056. case 'i':
  1057. base = 0;
  1058. case 'd':
  1059. is_sign = 1;
  1060. case 'u':
  1061. break;
  1062. case '%':
  1063. /* looking for '%' in str */
  1064. if (*str++ != '%')
  1065. return num;
  1066. continue;
  1067. default:
  1068. /* invalid format; stop here */
  1069. return num;
  1070. }
  1071. /* have some sort of integer conversion.
  1072. * first, skip white space in buffer.
  1073. */
  1074. while (isspace(*str))
  1075. str++;
  1076. digit = *str;
  1077. if (is_sign && digit == '-')
  1078. digit = *(str + 1);
  1079. if (!digit
  1080. || (base == 16 && !isxdigit(digit))
  1081. || (base == 10 && !isdigit(digit))
  1082. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1083. || (base == 0 && !isdigit(digit)))
  1084. break;
  1085. switch(qualifier) {
  1086. case 'H': /* that's 'hh' in format */
  1087. if (is_sign) {
  1088. signed char *s = (signed char *) va_arg(args,signed char *);
  1089. *s = (signed char) simple_strtol(str,&next,base);
  1090. } else {
  1091. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  1092. *s = (unsigned char) simple_strtoul(str, &next, base);
  1093. }
  1094. break;
  1095. case 'h':
  1096. if (is_sign) {
  1097. short *s = (short *) va_arg(args,short *);
  1098. *s = (short) simple_strtol(str,&next,base);
  1099. } else {
  1100. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  1101. *s = (unsigned short) simple_strtoul(str, &next, base);
  1102. }
  1103. break;
  1104. case 'l':
  1105. if (is_sign) {
  1106. long *l = (long *) va_arg(args,long *);
  1107. *l = simple_strtol(str,&next,base);
  1108. } else {
  1109. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1110. *l = simple_strtoul(str,&next,base);
  1111. }
  1112. break;
  1113. case 'L':
  1114. if (is_sign) {
  1115. long long *l = (long long*) va_arg(args,long long *);
  1116. *l = simple_strtoll(str,&next,base);
  1117. } else {
  1118. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1119. *l = simple_strtoull(str,&next,base);
  1120. }
  1121. break;
  1122. case 'Z':
  1123. case 'z':
  1124. {
  1125. size_t *s = (size_t*) va_arg(args,size_t*);
  1126. *s = (size_t) simple_strtoul(str,&next,base);
  1127. }
  1128. break;
  1129. default:
  1130. if (is_sign) {
  1131. int *i = (int *) va_arg(args, int*);
  1132. *i = (int) simple_strtol(str,&next,base);
  1133. } else {
  1134. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1135. *i = (unsigned int) simple_strtoul(str,&next,base);
  1136. }
  1137. break;
  1138. }
  1139. num++;
  1140. if (!next)
  1141. break;
  1142. str = next;
  1143. }
  1144. /*
  1145. * Now we've come all the way through so either the input string or the
  1146. * format ended. In the former case, there can be a %n at the current
  1147. * position in the format that needs to be filled.
  1148. */
  1149. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1150. int *p = (int *)va_arg(args, int *);
  1151. *p = str - buf;
  1152. }
  1153. return num;
  1154. }
  1155. EXPORT_SYMBOL(vsscanf);
  1156. /**
  1157. * sscanf - Unformat a buffer into a list of arguments
  1158. * @buf: input buffer
  1159. * @fmt: formatting of buffer
  1160. * @...: resulting arguments
  1161. */
  1162. int sscanf(const char * buf, const char * fmt, ...)
  1163. {
  1164. va_list args;
  1165. int i;
  1166. va_start(args,fmt);
  1167. i = vsscanf(buf,fmt,args);
  1168. va_end(args);
  1169. return i;
  1170. }
  1171. EXPORT_SYMBOL(sscanf);