vsprintf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. #include <stdarg.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/ctype.h>
  14. #include <common.h>
  15. #if !defined (CONFIG_PANIC_HANG)
  16. #include <command.h>
  17. /*cmd_boot.c*/
  18. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  19. #endif
  20. #include <div64.h>
  21. # define NUM_TYPE long long
  22. #define noinline __attribute__((noinline))
  23. const char hex_asc[] = "0123456789abcdef";
  24. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  25. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  26. static inline char *pack_hex_byte(char *buf, u8 byte)
  27. {
  28. *buf++ = hex_asc_hi(byte);
  29. *buf++ = hex_asc_lo(byte);
  30. return buf;
  31. }
  32. unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
  33. {
  34. unsigned long result = 0,value;
  35. if (*cp == '0') {
  36. cp++;
  37. if ((*cp == 'x') && isxdigit(cp[1])) {
  38. base = 16;
  39. cp++;
  40. }
  41. if (!base) {
  42. base = 8;
  43. }
  44. }
  45. if (!base) {
  46. base = 10;
  47. }
  48. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
  49. ? toupper(*cp) : *cp)-'A'+10) < base) {
  50. result = result*base + value;
  51. cp++;
  52. }
  53. if (endp)
  54. *endp = (char *)cp;
  55. return result;
  56. }
  57. long simple_strtol(const char *cp,char **endp,unsigned int base)
  58. {
  59. if(*cp=='-')
  60. return -simple_strtoul(cp+1,endp,base);
  61. return simple_strtoul(cp,endp,base);
  62. }
  63. int ustrtoul(const char *cp, char **endp, unsigned int base)
  64. {
  65. unsigned long result = simple_strtoul(cp, endp, base);
  66. switch (**endp) {
  67. case 'G' :
  68. result *= 1024;
  69. /* fall through */
  70. case 'M':
  71. result *= 1024;
  72. /* fall through */
  73. case 'K':
  74. case 'k':
  75. result *= 1024;
  76. if ((*endp)[1] == 'i') {
  77. if ((*endp)[2] == 'B')
  78. (*endp) += 3;
  79. else
  80. (*endp) += 2;
  81. }
  82. }
  83. return result;
  84. }
  85. unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
  86. {
  87. unsigned long long result = 0, value;
  88. if (*cp == '0') {
  89. cp++;
  90. if ((*cp == 'x') && isxdigit (cp[1])) {
  91. base = 16;
  92. cp++;
  93. }
  94. if (!base) {
  95. base = 8;
  96. }
  97. }
  98. if (!base) {
  99. base = 10;
  100. }
  101. while (isxdigit (*cp) && (value = isdigit (*cp)
  102. ? *cp - '0'
  103. : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
  104. result = result * base + value;
  105. cp++;
  106. }
  107. if (endp)
  108. *endp = (char *) cp;
  109. return result;
  110. }
  111. /* we use this so that we can do without the ctype library */
  112. #define is_digit(c) ((c) >= '0' && (c) <= '9')
  113. static int skip_atoi(const char **s)
  114. {
  115. int i=0;
  116. while (is_digit(**s))
  117. i = i*10 + *((*s)++) - '0';
  118. return i;
  119. }
  120. /* Decimal conversion is by far the most typical, and is used
  121. * for /proc and /sys data. This directly impacts e.g. top performance
  122. * with many processes running. We optimize it for speed
  123. * using code from
  124. * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
  125. * (with permission from the author, Douglas W. Jones). */
  126. /* Formats correctly any integer in [0,99999].
  127. * Outputs from one to five digits depending on input.
  128. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
  129. static char* put_dec_trunc(char *buf, unsigned q)
  130. {
  131. unsigned d3, d2, d1, d0;
  132. d1 = (q>>4) & 0xf;
  133. d2 = (q>>8) & 0xf;
  134. d3 = (q>>12);
  135. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  136. q = (d0 * 0xcd) >> 11;
  137. d0 = d0 - 10*q;
  138. *buf++ = d0 + '0'; /* least significant digit */
  139. d1 = q + 9*d3 + 5*d2 + d1;
  140. if (d1 != 0) {
  141. q = (d1 * 0xcd) >> 11;
  142. d1 = d1 - 10*q;
  143. *buf++ = d1 + '0'; /* next digit */
  144. d2 = q + 2*d2;
  145. if ((d2 != 0) || (d3 != 0)) {
  146. q = (d2 * 0xd) >> 7;
  147. d2 = d2 - 10*q;
  148. *buf++ = d2 + '0'; /* next digit */
  149. d3 = q + 4*d3;
  150. if (d3 != 0) {
  151. q = (d3 * 0xcd) >> 11;
  152. d3 = d3 - 10*q;
  153. *buf++ = d3 + '0'; /* next digit */
  154. if (q != 0)
  155. *buf++ = q + '0'; /* most sign. digit */
  156. }
  157. }
  158. }
  159. return buf;
  160. }
  161. /* Same with if's removed. Always emits five digits */
  162. static char* put_dec_full(char *buf, unsigned q)
  163. {
  164. /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
  165. /* but anyway, gcc produces better code with full-sized ints */
  166. unsigned d3, d2, d1, d0;
  167. d1 = (q>>4) & 0xf;
  168. d2 = (q>>8) & 0xf;
  169. d3 = (q>>12);
  170. /*
  171. * Possible ways to approx. divide by 10
  172. * gcc -O2 replaces multiply with shifts and adds
  173. * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
  174. * (x * 0x67) >> 10: 1100111
  175. * (x * 0x34) >> 9: 110100 - same
  176. * (x * 0x1a) >> 8: 11010 - same
  177. * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
  178. */
  179. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  180. q = (d0 * 0xcd) >> 11;
  181. d0 = d0 - 10*q;
  182. *buf++ = d0 + '0';
  183. d1 = q + 9*d3 + 5*d2 + d1;
  184. q = (d1 * 0xcd) >> 11;
  185. d1 = d1 - 10*q;
  186. *buf++ = d1 + '0';
  187. d2 = q + 2*d2;
  188. q = (d2 * 0xd) >> 7;
  189. d2 = d2 - 10*q;
  190. *buf++ = d2 + '0';
  191. d3 = q + 4*d3;
  192. q = (d3 * 0xcd) >> 11; /* - shorter code */
  193. /* q = (d3 * 0x67) >> 10; - would also work */
  194. d3 = d3 - 10*q;
  195. *buf++ = d3 + '0';
  196. *buf++ = q + '0';
  197. return buf;
  198. }
  199. /* No inlining helps gcc to use registers better */
  200. static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
  201. {
  202. while (1) {
  203. unsigned rem;
  204. if (num < 100000)
  205. return put_dec_trunc(buf, num);
  206. rem = do_div(num, 100000);
  207. buf = put_dec_full(buf, rem);
  208. }
  209. }
  210. #define ZEROPAD 1 /* pad with zero */
  211. #define SIGN 2 /* unsigned/signed long */
  212. #define PLUS 4 /* show plus */
  213. #define SPACE 8 /* space if plus */
  214. #define LEFT 16 /* left justified */
  215. #define SMALL 32 /* Must be 32 == 0x20 */
  216. #define SPECIAL 64 /* 0x */
  217. static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
  218. {
  219. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  220. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  221. char tmp[66];
  222. char sign;
  223. char locase;
  224. int need_pfx = ((type & SPECIAL) && base != 10);
  225. int i;
  226. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  227. * produces same digits or (maybe lowercased) letters */
  228. locase = (type & SMALL);
  229. if (type & LEFT)
  230. type &= ~ZEROPAD;
  231. sign = 0;
  232. if (type & SIGN) {
  233. if ((signed NUM_TYPE) num < 0) {
  234. sign = '-';
  235. num = - (signed NUM_TYPE) num;
  236. size--;
  237. } else if (type & PLUS) {
  238. sign = '+';
  239. size--;
  240. } else if (type & SPACE) {
  241. sign = ' ';
  242. size--;
  243. }
  244. }
  245. if (need_pfx) {
  246. size--;
  247. if (base == 16)
  248. size--;
  249. }
  250. /* generate full string in tmp[], in reverse order */
  251. i = 0;
  252. if (num == 0)
  253. tmp[i++] = '0';
  254. /* Generic code, for any base:
  255. else do {
  256. tmp[i++] = (digits[do_div(num,base)] | locase);
  257. } while (num != 0);
  258. */
  259. else if (base != 10) { /* 8 or 16 */
  260. int mask = base - 1;
  261. int shift = 3;
  262. if (base == 16) shift = 4;
  263. do {
  264. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  265. num >>= shift;
  266. } while (num);
  267. } else { /* base 10 */
  268. i = put_dec(tmp, num) - tmp;
  269. }
  270. /* printing 100 using %2d gives "100", not "00" */
  271. if (i > precision)
  272. precision = i;
  273. /* leading space padding */
  274. size -= precision;
  275. if (!(type & (ZEROPAD+LEFT)))
  276. while(--size >= 0)
  277. *buf++ = ' ';
  278. /* sign */
  279. if (sign)
  280. *buf++ = sign;
  281. /* "0x" / "0" prefix */
  282. if (need_pfx) {
  283. *buf++ = '0';
  284. if (base == 16)
  285. *buf++ = ('X' | locase);
  286. }
  287. /* zero or space padding */
  288. if (!(type & LEFT)) {
  289. char c = (type & ZEROPAD) ? '0' : ' ';
  290. while (--size >= 0)
  291. *buf++ = c;
  292. }
  293. /* hmm even more zero padding? */
  294. while (i <= --precision)
  295. *buf++ = '0';
  296. /* actual digits of result */
  297. while (--i >= 0)
  298. *buf++ = tmp[i];
  299. /* trailing space padding */
  300. while (--size >= 0)
  301. *buf++ = ' ';
  302. return buf;
  303. }
  304. static char *string(char *buf, char *s, int field_width, int precision, int flags)
  305. {
  306. int len, i;
  307. if (s == 0)
  308. s = "<NULL>";
  309. len = strnlen(s, precision);
  310. if (!(flags & LEFT))
  311. while (len < field_width--)
  312. *buf++ = ' ';
  313. for (i = 0; i < len; ++i)
  314. *buf++ = *s++;
  315. while (len < field_width--)
  316. *buf++ = ' ';
  317. return buf;
  318. }
  319. #ifdef CONFIG_CMD_NET
  320. static char *mac_address_string(char *buf, u8 *addr, int field_width,
  321. int precision, int flags)
  322. {
  323. char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
  324. char *p = mac_addr;
  325. int i;
  326. for (i = 0; i < 6; i++) {
  327. p = pack_hex_byte(p, addr[i]);
  328. if (!(flags & SPECIAL) && i != 5)
  329. *p++ = ':';
  330. }
  331. *p = '\0';
  332. return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
  333. }
  334. static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
  335. int precision, int flags)
  336. {
  337. char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
  338. char *p = ip6_addr;
  339. int i;
  340. for (i = 0; i < 8; i++) {
  341. p = pack_hex_byte(p, addr[2 * i]);
  342. p = pack_hex_byte(p, addr[2 * i + 1]);
  343. if (!(flags & SPECIAL) && i != 7)
  344. *p++ = ':';
  345. }
  346. *p = '\0';
  347. return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
  348. }
  349. static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
  350. int precision, int flags)
  351. {
  352. char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
  353. char temp[3]; /* hold each IP quad in reverse order */
  354. char *p = ip4_addr;
  355. int i, digits;
  356. for (i = 0; i < 4; i++) {
  357. digits = put_dec_trunc(temp, addr[i]) - temp;
  358. /* reverse the digits in the quad */
  359. while (digits--)
  360. *p++ = temp[digits];
  361. if (i != 3)
  362. *p++ = '.';
  363. }
  364. *p = '\0';
  365. return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
  366. }
  367. #endif
  368. /*
  369. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  370. * by an extra set of alphanumeric characters that are extended format
  371. * specifiers.
  372. *
  373. * Right now we handle:
  374. *
  375. * - 'M' For a 6-byte MAC address, it prints the address in the
  376. * usual colon-separated hex notation
  377. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  378. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  379. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  380. * currently the same
  381. *
  382. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  383. * function pointers are really function descriptors, which contain a
  384. * pointer to the real address.
  385. */
  386. static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
  387. {
  388. if (!ptr)
  389. return string(buf, "(null)", field_width, precision, flags);
  390. #ifdef CONFIG_CMD_NET
  391. switch (*fmt) {
  392. case 'm':
  393. flags |= SPECIAL;
  394. /* Fallthrough */
  395. case 'M':
  396. return mac_address_string(buf, ptr, field_width, precision, flags);
  397. case 'i':
  398. flags |= SPECIAL;
  399. /* Fallthrough */
  400. case 'I':
  401. if (fmt[1] == '6')
  402. return ip6_addr_string(buf, ptr, field_width, precision, flags);
  403. if (fmt[1] == '4')
  404. return ip4_addr_string(buf, ptr, field_width, precision, flags);
  405. flags &= ~SPECIAL;
  406. break;
  407. }
  408. #endif
  409. flags |= SMALL;
  410. if (field_width == -1) {
  411. field_width = 2*sizeof(void *);
  412. flags |= ZEROPAD;
  413. }
  414. return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
  415. }
  416. /**
  417. * vsprintf - Format a string and place it in a buffer
  418. * @buf: The buffer to place the result into
  419. * @fmt: The format string to use
  420. * @args: Arguments for the format string
  421. *
  422. * This function follows C99 vsprintf, but has some extensions:
  423. * %pS output the name of a text symbol
  424. * %pF output the name of a function pointer
  425. * %pR output the address range in a struct resource
  426. *
  427. * The function returns the number of characters written
  428. * into @buf.
  429. *
  430. * Call this function if you are already dealing with a va_list.
  431. * You probably want sprintf() instead.
  432. */
  433. int vsprintf(char *buf, const char *fmt, va_list args)
  434. {
  435. unsigned NUM_TYPE num;
  436. int base;
  437. char *str;
  438. int flags; /* flags to number() */
  439. int field_width; /* width of output field */
  440. int precision; /* min. # of digits for integers; max
  441. number of chars for from string */
  442. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  443. /* 'z' support added 23/7/1999 S.H. */
  444. /* 'z' changed to 'Z' --davidm 1/25/99 */
  445. /* 't' added for ptrdiff_t */
  446. str = buf;
  447. for (; *fmt ; ++fmt) {
  448. if (*fmt != '%') {
  449. *str++ = *fmt;
  450. continue;
  451. }
  452. /* process flags */
  453. flags = 0;
  454. repeat:
  455. ++fmt; /* this also skips first '%' */
  456. switch (*fmt) {
  457. case '-': flags |= LEFT; goto repeat;
  458. case '+': flags |= PLUS; goto repeat;
  459. case ' ': flags |= SPACE; goto repeat;
  460. case '#': flags |= SPECIAL; goto repeat;
  461. case '0': flags |= ZEROPAD; goto repeat;
  462. }
  463. /* get field width */
  464. field_width = -1;
  465. if (is_digit(*fmt))
  466. field_width = skip_atoi(&fmt);
  467. else if (*fmt == '*') {
  468. ++fmt;
  469. /* it's the next argument */
  470. field_width = va_arg(args, int);
  471. if (field_width < 0) {
  472. field_width = -field_width;
  473. flags |= LEFT;
  474. }
  475. }
  476. /* get the precision */
  477. precision = -1;
  478. if (*fmt == '.') {
  479. ++fmt;
  480. if (is_digit(*fmt))
  481. precision = skip_atoi(&fmt);
  482. else if (*fmt == '*') {
  483. ++fmt;
  484. /* it's the next argument */
  485. precision = va_arg(args, int);
  486. }
  487. if (precision < 0)
  488. precision = 0;
  489. }
  490. /* get the conversion qualifier */
  491. qualifier = -1;
  492. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  493. *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
  494. qualifier = *fmt;
  495. ++fmt;
  496. if (qualifier == 'l' && *fmt == 'l') {
  497. qualifier = 'L';
  498. ++fmt;
  499. }
  500. }
  501. /* default base */
  502. base = 10;
  503. switch (*fmt) {
  504. case 'c':
  505. if (!(flags & LEFT))
  506. while (--field_width > 0)
  507. *str++ = ' ';
  508. *str++ = (unsigned char) va_arg(args, int);
  509. while (--field_width > 0)
  510. *str++ = ' ';
  511. continue;
  512. case 's':
  513. str = string(str, va_arg(args, char *), field_width, precision, flags);
  514. continue;
  515. case 'p':
  516. str = pointer(fmt+1, str,
  517. va_arg(args, void *),
  518. field_width, precision, flags);
  519. /* Skip all alphanumeric pointer suffixes */
  520. while (isalnum(fmt[1]))
  521. fmt++;
  522. continue;
  523. case 'n':
  524. if (qualifier == 'l') {
  525. long * ip = va_arg(args, long *);
  526. *ip = (str - buf);
  527. } else {
  528. int * ip = va_arg(args, int *);
  529. *ip = (str - buf);
  530. }
  531. continue;
  532. case '%':
  533. *str++ = '%';
  534. continue;
  535. /* integer number formats - set up the flags and "break" */
  536. case 'o':
  537. base = 8;
  538. break;
  539. case 'x':
  540. flags |= SMALL;
  541. case 'X':
  542. base = 16;
  543. break;
  544. case 'd':
  545. case 'i':
  546. flags |= SIGN;
  547. case 'u':
  548. break;
  549. default:
  550. *str++ = '%';
  551. if (*fmt)
  552. *str++ = *fmt;
  553. else
  554. --fmt;
  555. continue;
  556. }
  557. if (qualifier == 'L') /* "quad" for 64 bit variables */
  558. num = va_arg(args, unsigned long long);
  559. else if (qualifier == 'l') {
  560. num = va_arg(args, unsigned long);
  561. if (flags & SIGN)
  562. num = (signed long) num;
  563. } else if (qualifier == 'Z' || qualifier == 'z') {
  564. num = va_arg(args, size_t);
  565. } else if (qualifier == 't') {
  566. num = va_arg(args, ptrdiff_t);
  567. } else if (qualifier == 'h') {
  568. num = (unsigned short) va_arg(args, int);
  569. if (flags & SIGN)
  570. num = (signed short) num;
  571. } else {
  572. num = va_arg(args, unsigned int);
  573. if (flags & SIGN)
  574. num = (signed int) num;
  575. }
  576. str = number(str, num, base, field_width, precision, flags);
  577. }
  578. *str = '\0';
  579. return str-buf;
  580. }
  581. /**
  582. * sprintf - Format a string and place it in a buffer
  583. * @buf: The buffer to place the result into
  584. * @fmt: The format string to use
  585. * @...: Arguments for the format string
  586. *
  587. * The function returns the number of characters written
  588. * into @buf.
  589. *
  590. * See the vsprintf() documentation for format string extensions over C99.
  591. */
  592. int sprintf(char * buf, const char *fmt, ...)
  593. {
  594. va_list args;
  595. int i;
  596. va_start(args, fmt);
  597. i=vsprintf(buf,fmt,args);
  598. va_end(args);
  599. return i;
  600. }
  601. void panic(const char *fmt, ...)
  602. {
  603. va_list args;
  604. va_start(args, fmt);
  605. vprintf(fmt, args);
  606. putc('\n');
  607. va_end(args);
  608. #if defined (CONFIG_PANIC_HANG)
  609. hang();
  610. #else
  611. udelay (100000); /* allow messages to go out */
  612. do_reset (NULL, 0, 0, NULL);
  613. #endif
  614. }