vsprintf.c 29 KB

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