vsprintf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. /*
  538. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  539. * by an extra set of alphanumeric characters that are extended format
  540. * specifiers.
  541. *
  542. * Right now we handle:
  543. *
  544. * - 'F' For symbolic function descriptor pointers
  545. * - 'S' For symbolic direct pointers
  546. * - 'R' For a struct resource pointer, it prints the range of
  547. * addresses (not the name nor the flags)
  548. * - 'M' For a 6-byte MAC address, it prints the address in the
  549. * usual colon-separated hex notation
  550. *
  551. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  552. * function pointers are really function descriptors, which contain a
  553. * pointer to the real address.
  554. */
  555. static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
  556. {
  557. switch (*fmt) {
  558. case 'F':
  559. ptr = dereference_function_descriptor(ptr);
  560. /* Fallthrough */
  561. case 'S':
  562. return symbol_string(buf, end, ptr, field_width, precision, flags);
  563. case 'R':
  564. return resource_string(buf, end, ptr, field_width, precision, flags);
  565. case 'M':
  566. return mac_address_string(buf, end, ptr, field_width, precision, flags);
  567. }
  568. flags |= SMALL;
  569. if (field_width == -1) {
  570. field_width = 2*sizeof(void *);
  571. flags |= ZEROPAD;
  572. }
  573. return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
  574. }
  575. /**
  576. * vsnprintf - Format a string and place it in a buffer
  577. * @buf: The buffer to place the result into
  578. * @size: The size of the buffer, including the trailing null space
  579. * @fmt: The format string to use
  580. * @args: Arguments for the format string
  581. *
  582. * This function follows C99 vsnprintf, but has some extensions:
  583. * %pS output the name of a text symbol
  584. * %pF output the name of a function pointer
  585. * %pR output the address range in a struct resource
  586. *
  587. * The return value is the number of characters which would
  588. * be generated for the given input, excluding the trailing
  589. * '\0', as per ISO C99. If you want to have the exact
  590. * number of characters written into @buf as return value
  591. * (not including the trailing '\0'), use vscnprintf(). If the
  592. * return is greater than or equal to @size, the resulting
  593. * string is truncated.
  594. *
  595. * Call this function if you are already dealing with a va_list.
  596. * You probably want snprintf() instead.
  597. */
  598. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  599. {
  600. unsigned long long num;
  601. int base;
  602. char *str, *end, c;
  603. int flags; /* flags to number() */
  604. int field_width; /* width of output field */
  605. int precision; /* min. # of digits for integers; max
  606. number of chars for from string */
  607. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  608. /* 'z' support added 23/7/1999 S.H. */
  609. /* 'z' changed to 'Z' --davidm 1/25/99 */
  610. /* 't' added for ptrdiff_t */
  611. /* Reject out-of-range values early. Large positive sizes are
  612. used for unknown buffer sizes. */
  613. if (unlikely((int) size < 0)) {
  614. /* There can be only one.. */
  615. static char warn = 1;
  616. WARN_ON(warn);
  617. warn = 0;
  618. return 0;
  619. }
  620. str = buf;
  621. end = buf + size;
  622. /* Make sure end is always >= buf */
  623. if (end < buf) {
  624. end = ((void *)-1);
  625. size = end - buf;
  626. }
  627. for (; *fmt ; ++fmt) {
  628. if (*fmt != '%') {
  629. if (str < end)
  630. *str = *fmt;
  631. ++str;
  632. continue;
  633. }
  634. /* process flags */
  635. flags = 0;
  636. repeat:
  637. ++fmt; /* this also skips first '%' */
  638. switch (*fmt) {
  639. case '-': flags |= LEFT; goto repeat;
  640. case '+': flags |= PLUS; goto repeat;
  641. case ' ': flags |= SPACE; goto repeat;
  642. case '#': flags |= SPECIAL; goto repeat;
  643. case '0': flags |= ZEROPAD; goto repeat;
  644. }
  645. /* get field width */
  646. field_width = -1;
  647. if (isdigit(*fmt))
  648. field_width = skip_atoi(&fmt);
  649. else if (*fmt == '*') {
  650. ++fmt;
  651. /* it's the next argument */
  652. field_width = va_arg(args, int);
  653. if (field_width < 0) {
  654. field_width = -field_width;
  655. flags |= LEFT;
  656. }
  657. }
  658. /* get the precision */
  659. precision = -1;
  660. if (*fmt == '.') {
  661. ++fmt;
  662. if (isdigit(*fmt))
  663. precision = skip_atoi(&fmt);
  664. else if (*fmt == '*') {
  665. ++fmt;
  666. /* it's the next argument */
  667. precision = va_arg(args, int);
  668. }
  669. if (precision < 0)
  670. precision = 0;
  671. }
  672. /* get the conversion qualifier */
  673. qualifier = -1;
  674. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  675. *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
  676. qualifier = *fmt;
  677. ++fmt;
  678. if (qualifier == 'l' && *fmt == 'l') {
  679. qualifier = 'L';
  680. ++fmt;
  681. }
  682. }
  683. /* default base */
  684. base = 10;
  685. switch (*fmt) {
  686. case 'c':
  687. if (!(flags & LEFT)) {
  688. while (--field_width > 0) {
  689. if (str < end)
  690. *str = ' ';
  691. ++str;
  692. }
  693. }
  694. c = (unsigned char) va_arg(args, int);
  695. if (str < end)
  696. *str = c;
  697. ++str;
  698. while (--field_width > 0) {
  699. if (str < end)
  700. *str = ' ';
  701. ++str;
  702. }
  703. continue;
  704. case 's':
  705. str = string(str, end, va_arg(args, char *), field_width, precision, flags);
  706. continue;
  707. case 'p':
  708. str = pointer(fmt+1, str, end,
  709. va_arg(args, void *),
  710. field_width, precision, flags);
  711. /* Skip all alphanumeric pointer suffixes */
  712. while (isalnum(fmt[1]))
  713. fmt++;
  714. continue;
  715. case 'n':
  716. /* FIXME:
  717. * What does C99 say about the overflow case here? */
  718. if (qualifier == 'l') {
  719. long * ip = va_arg(args, long *);
  720. *ip = (str - buf);
  721. } else if (qualifier == 'Z' || qualifier == 'z') {
  722. size_t * ip = va_arg(args, size_t *);
  723. *ip = (str - buf);
  724. } else {
  725. int * ip = va_arg(args, int *);
  726. *ip = (str - buf);
  727. }
  728. continue;
  729. case '%':
  730. if (str < end)
  731. *str = '%';
  732. ++str;
  733. continue;
  734. /* integer number formats - set up the flags and "break" */
  735. case 'o':
  736. base = 8;
  737. break;
  738. case 'x':
  739. flags |= SMALL;
  740. case 'X':
  741. base = 16;
  742. break;
  743. case 'd':
  744. case 'i':
  745. flags |= SIGN;
  746. case 'u':
  747. break;
  748. default:
  749. if (str < end)
  750. *str = '%';
  751. ++str;
  752. if (*fmt) {
  753. if (str < end)
  754. *str = *fmt;
  755. ++str;
  756. } else {
  757. --fmt;
  758. }
  759. continue;
  760. }
  761. if (qualifier == 'L')
  762. num = va_arg(args, long long);
  763. else if (qualifier == 'l') {
  764. num = va_arg(args, unsigned long);
  765. if (flags & SIGN)
  766. num = (signed long) num;
  767. } else if (qualifier == 'Z' || qualifier == 'z') {
  768. num = va_arg(args, size_t);
  769. } else if (qualifier == 't') {
  770. num = va_arg(args, ptrdiff_t);
  771. } else if (qualifier == 'h') {
  772. num = (unsigned short) va_arg(args, int);
  773. if (flags & SIGN)
  774. num = (signed short) num;
  775. } else {
  776. num = va_arg(args, unsigned int);
  777. if (flags & SIGN)
  778. num = (signed int) num;
  779. }
  780. str = number(str, end, num, base,
  781. field_width, precision, flags);
  782. }
  783. if (size > 0) {
  784. if (str < end)
  785. *str = '\0';
  786. else
  787. end[-1] = '\0';
  788. }
  789. /* the trailing null byte doesn't count towards the total */
  790. return str-buf;
  791. }
  792. EXPORT_SYMBOL(vsnprintf);
  793. /**
  794. * vscnprintf - Format a string and place it in a buffer
  795. * @buf: The buffer to place the result into
  796. * @size: The size of the buffer, including the trailing null space
  797. * @fmt: The format string to use
  798. * @args: Arguments for the format string
  799. *
  800. * The return value is the number of characters which have been written into
  801. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  802. * returns 0.
  803. *
  804. * Call this function if you are already dealing with a va_list.
  805. * You probably want scnprintf() instead.
  806. *
  807. * See the vsnprintf() documentation for format string extensions over C99.
  808. */
  809. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  810. {
  811. int i;
  812. i=vsnprintf(buf,size,fmt,args);
  813. return (i >= size) ? (size - 1) : i;
  814. }
  815. EXPORT_SYMBOL(vscnprintf);
  816. /**
  817. * snprintf - Format a string and place it in a buffer
  818. * @buf: The buffer to place the result into
  819. * @size: The size of the buffer, including the trailing null space
  820. * @fmt: The format string to use
  821. * @...: Arguments for the format string
  822. *
  823. * The return value is the number of characters which would be
  824. * generated for the given input, excluding the trailing null,
  825. * as per ISO C99. If the return is greater than or equal to
  826. * @size, the resulting string is truncated.
  827. *
  828. * See the vsnprintf() documentation for format string extensions over C99.
  829. */
  830. int snprintf(char * buf, size_t size, const char *fmt, ...)
  831. {
  832. va_list args;
  833. int i;
  834. va_start(args, fmt);
  835. i=vsnprintf(buf,size,fmt,args);
  836. va_end(args);
  837. return i;
  838. }
  839. EXPORT_SYMBOL(snprintf);
  840. /**
  841. * scnprintf - Format a string and place it in a buffer
  842. * @buf: The buffer to place the result into
  843. * @size: The size of the buffer, including the trailing null space
  844. * @fmt: The format string to use
  845. * @...: Arguments for the format string
  846. *
  847. * The return value is the number of characters written into @buf not including
  848. * the trailing '\0'. If @size is <= 0 the function returns 0.
  849. */
  850. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  851. {
  852. va_list args;
  853. int i;
  854. va_start(args, fmt);
  855. i = vsnprintf(buf, size, fmt, args);
  856. va_end(args);
  857. return (i >= size) ? (size - 1) : i;
  858. }
  859. EXPORT_SYMBOL(scnprintf);
  860. /**
  861. * vsprintf - Format a string and place it in a buffer
  862. * @buf: The buffer to place the result into
  863. * @fmt: The format string to use
  864. * @args: Arguments for the format string
  865. *
  866. * The function returns the number of characters written
  867. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  868. * buffer overflows.
  869. *
  870. * Call this function if you are already dealing with a va_list.
  871. * You probably want sprintf() instead.
  872. *
  873. * See the vsnprintf() documentation for format string extensions over C99.
  874. */
  875. int vsprintf(char *buf, const char *fmt, va_list args)
  876. {
  877. return vsnprintf(buf, INT_MAX, fmt, args);
  878. }
  879. EXPORT_SYMBOL(vsprintf);
  880. /**
  881. * sprintf - Format a string and place it in a buffer
  882. * @buf: The buffer to place the result into
  883. * @fmt: The format string to use
  884. * @...: Arguments for the format string
  885. *
  886. * The function returns the number of characters written
  887. * into @buf. Use snprintf() or scnprintf() in order to avoid
  888. * buffer overflows.
  889. *
  890. * See the vsnprintf() documentation for format string extensions over C99.
  891. */
  892. int sprintf(char * buf, const char *fmt, ...)
  893. {
  894. va_list args;
  895. int i;
  896. va_start(args, fmt);
  897. i=vsnprintf(buf, INT_MAX, fmt, args);
  898. va_end(args);
  899. return i;
  900. }
  901. EXPORT_SYMBOL(sprintf);
  902. /**
  903. * vsscanf - Unformat a buffer into a list of arguments
  904. * @buf: input buffer
  905. * @fmt: format of buffer
  906. * @args: arguments
  907. */
  908. int vsscanf(const char * buf, const char * fmt, va_list args)
  909. {
  910. const char *str = buf;
  911. char *next;
  912. char digit;
  913. int num = 0;
  914. int qualifier;
  915. int base;
  916. int field_width;
  917. int is_sign = 0;
  918. while(*fmt && *str) {
  919. /* skip any white space in format */
  920. /* white space in format matchs any amount of
  921. * white space, including none, in the input.
  922. */
  923. if (isspace(*fmt)) {
  924. while (isspace(*fmt))
  925. ++fmt;
  926. while (isspace(*str))
  927. ++str;
  928. }
  929. /* anything that is not a conversion must match exactly */
  930. if (*fmt != '%' && *fmt) {
  931. if (*fmt++ != *str++)
  932. break;
  933. continue;
  934. }
  935. if (!*fmt)
  936. break;
  937. ++fmt;
  938. /* skip this conversion.
  939. * advance both strings to next white space
  940. */
  941. if (*fmt == '*') {
  942. while (!isspace(*fmt) && *fmt)
  943. fmt++;
  944. while (!isspace(*str) && *str)
  945. str++;
  946. continue;
  947. }
  948. /* get field width */
  949. field_width = -1;
  950. if (isdigit(*fmt))
  951. field_width = skip_atoi(&fmt);
  952. /* get conversion qualifier */
  953. qualifier = -1;
  954. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  955. *fmt == 'Z' || *fmt == 'z') {
  956. qualifier = *fmt++;
  957. if (unlikely(qualifier == *fmt)) {
  958. if (qualifier == 'h') {
  959. qualifier = 'H';
  960. fmt++;
  961. } else if (qualifier == 'l') {
  962. qualifier = 'L';
  963. fmt++;
  964. }
  965. }
  966. }
  967. base = 10;
  968. is_sign = 0;
  969. if (!*fmt || !*str)
  970. break;
  971. switch(*fmt++) {
  972. case 'c':
  973. {
  974. char *s = (char *) va_arg(args,char*);
  975. if (field_width == -1)
  976. field_width = 1;
  977. do {
  978. *s++ = *str++;
  979. } while (--field_width > 0 && *str);
  980. num++;
  981. }
  982. continue;
  983. case 's':
  984. {
  985. char *s = (char *) va_arg(args, char *);
  986. if(field_width == -1)
  987. field_width = INT_MAX;
  988. /* first, skip leading white space in buffer */
  989. while (isspace(*str))
  990. str++;
  991. /* now copy until next white space */
  992. while (*str && !isspace(*str) && field_width--) {
  993. *s++ = *str++;
  994. }
  995. *s = '\0';
  996. num++;
  997. }
  998. continue;
  999. case 'n':
  1000. /* return number of characters read so far */
  1001. {
  1002. int *i = (int *)va_arg(args,int*);
  1003. *i = str - buf;
  1004. }
  1005. continue;
  1006. case 'o':
  1007. base = 8;
  1008. break;
  1009. case 'x':
  1010. case 'X':
  1011. base = 16;
  1012. break;
  1013. case 'i':
  1014. base = 0;
  1015. case 'd':
  1016. is_sign = 1;
  1017. case 'u':
  1018. break;
  1019. case '%':
  1020. /* looking for '%' in str */
  1021. if (*str++ != '%')
  1022. return num;
  1023. continue;
  1024. default:
  1025. /* invalid format; stop here */
  1026. return num;
  1027. }
  1028. /* have some sort of integer conversion.
  1029. * first, skip white space in buffer.
  1030. */
  1031. while (isspace(*str))
  1032. str++;
  1033. digit = *str;
  1034. if (is_sign && digit == '-')
  1035. digit = *(str + 1);
  1036. if (!digit
  1037. || (base == 16 && !isxdigit(digit))
  1038. || (base == 10 && !isdigit(digit))
  1039. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1040. || (base == 0 && !isdigit(digit)))
  1041. break;
  1042. switch(qualifier) {
  1043. case 'H': /* that's 'hh' in format */
  1044. if (is_sign) {
  1045. signed char *s = (signed char *) va_arg(args,signed char *);
  1046. *s = (signed char) simple_strtol(str,&next,base);
  1047. } else {
  1048. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  1049. *s = (unsigned char) simple_strtoul(str, &next, base);
  1050. }
  1051. break;
  1052. case 'h':
  1053. if (is_sign) {
  1054. short *s = (short *) va_arg(args,short *);
  1055. *s = (short) simple_strtol(str,&next,base);
  1056. } else {
  1057. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  1058. *s = (unsigned short) simple_strtoul(str, &next, base);
  1059. }
  1060. break;
  1061. case 'l':
  1062. if (is_sign) {
  1063. long *l = (long *) va_arg(args,long *);
  1064. *l = simple_strtol(str,&next,base);
  1065. } else {
  1066. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1067. *l = simple_strtoul(str,&next,base);
  1068. }
  1069. break;
  1070. case 'L':
  1071. if (is_sign) {
  1072. long long *l = (long long*) va_arg(args,long long *);
  1073. *l = simple_strtoll(str,&next,base);
  1074. } else {
  1075. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1076. *l = simple_strtoull(str,&next,base);
  1077. }
  1078. break;
  1079. case 'Z':
  1080. case 'z':
  1081. {
  1082. size_t *s = (size_t*) va_arg(args,size_t*);
  1083. *s = (size_t) simple_strtoul(str,&next,base);
  1084. }
  1085. break;
  1086. default:
  1087. if (is_sign) {
  1088. int *i = (int *) va_arg(args, int*);
  1089. *i = (int) simple_strtol(str,&next,base);
  1090. } else {
  1091. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1092. *i = (unsigned int) simple_strtoul(str,&next,base);
  1093. }
  1094. break;
  1095. }
  1096. num++;
  1097. if (!next)
  1098. break;
  1099. str = next;
  1100. }
  1101. /*
  1102. * Now we've come all the way through so either the input string or the
  1103. * format ended. In the former case, there can be a %n at the current
  1104. * position in the format that needs to be filled.
  1105. */
  1106. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1107. int *p = (int *)va_arg(args, int *);
  1108. *p = str - buf;
  1109. }
  1110. return num;
  1111. }
  1112. EXPORT_SYMBOL(vsscanf);
  1113. /**
  1114. * sscanf - Unformat a buffer into a list of arguments
  1115. * @buf: input buffer
  1116. * @fmt: formatting of buffer
  1117. * @...: resulting arguments
  1118. */
  1119. int sscanf(const char * buf, const char * fmt, ...)
  1120. {
  1121. va_list args;
  1122. int i;
  1123. va_start(args,fmt);
  1124. i = vsscanf(buf,fmt,args);
  1125. va_end(args);
  1126. return i;
  1127. }
  1128. EXPORT_SYMBOL(sscanf);