vsprintf.c 15 KB

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