vsprintf.c 31 KB

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