vsprintf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. * from hush: simple_itoa() was lifted from boa-0.93.15
  11. */
  12. #include <stdarg.h>
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include <linux/ctype.h>
  16. #include <errno.h>
  17. #include <common.h>
  18. #if !defined(CONFIG_PANIC_HANG)
  19. #include <command.h>
  20. #endif
  21. #include <div64.h>
  22. #define noinline __attribute__((noinline))
  23. /* some reluctance to put this into a new limits.h, so it is here */
  24. #define INT_MAX ((int)(~0U>>1))
  25. unsigned long simple_strtoul(const char *cp, char **endp,
  26. unsigned int base)
  27. {
  28. unsigned long result = 0;
  29. unsigned long value;
  30. if (*cp == '0') {
  31. cp++;
  32. if ((*cp == 'x') && isxdigit(cp[1])) {
  33. base = 16;
  34. cp++;
  35. }
  36. if (!base)
  37. base = 8;
  38. }
  39. if (!base)
  40. base = 10;
  41. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
  42. ? toupper(*cp) : *cp)-'A'+10) < base) {
  43. result = result*base + value;
  44. cp++;
  45. }
  46. if (endp)
  47. *endp = (char *)cp;
  48. return result;
  49. }
  50. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  51. {
  52. char *tail;
  53. unsigned long val;
  54. size_t len;
  55. *res = 0;
  56. len = strlen(cp);
  57. if (len == 0)
  58. return -EINVAL;
  59. val = simple_strtoul(cp, &tail, base);
  60. if (tail == cp)
  61. return -EINVAL;
  62. if ((*tail == '\0') ||
  63. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  64. *res = val;
  65. return 0;
  66. }
  67. return -EINVAL;
  68. }
  69. long simple_strtol(const char *cp, char **endp, unsigned int base)
  70. {
  71. if (*cp == '-')
  72. return -simple_strtoul(cp + 1, endp, base);
  73. return simple_strtoul(cp, endp, base);
  74. }
  75. unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
  76. {
  77. unsigned long result = simple_strtoul(cp, endp, base);
  78. switch (**endp) {
  79. case 'G':
  80. result *= 1024;
  81. /* fall through */
  82. case 'M':
  83. result *= 1024;
  84. /* fall through */
  85. case 'K':
  86. case 'k':
  87. result *= 1024;
  88. if ((*endp)[1] == 'i') {
  89. if ((*endp)[2] == 'B')
  90. (*endp) += 3;
  91. else
  92. (*endp) += 2;
  93. }
  94. }
  95. return result;
  96. }
  97. unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
  98. {
  99. unsigned long long result = simple_strtoull(cp, endp, base);
  100. switch (**endp) {
  101. case 'G':
  102. result *= 1024;
  103. /* fall through */
  104. case 'M':
  105. result *= 1024;
  106. /* fall through */
  107. case 'K':
  108. case 'k':
  109. result *= 1024;
  110. if ((*endp)[1] == 'i') {
  111. if ((*endp)[2] == 'B')
  112. (*endp) += 3;
  113. else
  114. (*endp) += 2;
  115. }
  116. }
  117. return result;
  118. }
  119. unsigned long long simple_strtoull(const char *cp, char **endp,
  120. unsigned int base)
  121. {
  122. unsigned long long result = 0, value;
  123. if (*cp == '0') {
  124. cp++;
  125. if ((*cp == 'x') && isxdigit(cp[1])) {
  126. base = 16;
  127. cp++;
  128. }
  129. if (!base)
  130. base = 8;
  131. }
  132. if (!base)
  133. base = 10;
  134. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
  135. : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
  136. result = result * base + value;
  137. cp++;
  138. }
  139. if (endp)
  140. *endp = (char *) cp;
  141. return result;
  142. }
  143. /* we use this so that we can do without the ctype library */
  144. #define is_digit(c) ((c) >= '0' && (c) <= '9')
  145. static int skip_atoi(const char **s)
  146. {
  147. int i = 0;
  148. while (is_digit(**s))
  149. i = i * 10 + *((*s)++) - '0';
  150. return i;
  151. }
  152. /* Decimal conversion is by far the most typical, and is used
  153. * for /proc and /sys data. This directly impacts e.g. top performance
  154. * with many processes running. We optimize it for speed
  155. * using code from
  156. * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
  157. * (with permission from the author, Douglas W. Jones). */
  158. /* Formats correctly any integer in [0,99999].
  159. * Outputs from one to five digits depending on input.
  160. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
  161. static char *put_dec_trunc(char *buf, unsigned q)
  162. {
  163. unsigned d3, d2, d1, d0;
  164. d1 = (q>>4) & 0xf;
  165. d2 = (q>>8) & 0xf;
  166. d3 = (q>>12);
  167. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  168. q = (d0 * 0xcd) >> 11;
  169. d0 = d0 - 10*q;
  170. *buf++ = d0 + '0'; /* least significant digit */
  171. d1 = q + 9*d3 + 5*d2 + d1;
  172. if (d1 != 0) {
  173. q = (d1 * 0xcd) >> 11;
  174. d1 = d1 - 10*q;
  175. *buf++ = d1 + '0'; /* next digit */
  176. d2 = q + 2*d2;
  177. if ((d2 != 0) || (d3 != 0)) {
  178. q = (d2 * 0xd) >> 7;
  179. d2 = d2 - 10*q;
  180. *buf++ = d2 + '0'; /* next digit */
  181. d3 = q + 4*d3;
  182. if (d3 != 0) {
  183. q = (d3 * 0xcd) >> 11;
  184. d3 = d3 - 10*q;
  185. *buf++ = d3 + '0'; /* next digit */
  186. if (q != 0)
  187. *buf++ = q + '0'; /* most sign. digit */
  188. }
  189. }
  190. }
  191. return buf;
  192. }
  193. /* Same with if's removed. Always emits five digits */
  194. static char *put_dec_full(char *buf, unsigned q)
  195. {
  196. /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
  197. /* but anyway, gcc produces better code with full-sized ints */
  198. unsigned d3, d2, d1, d0;
  199. d1 = (q>>4) & 0xf;
  200. d2 = (q>>8) & 0xf;
  201. d3 = (q>>12);
  202. /*
  203. * Possible ways to approx. divide by 10
  204. * gcc -O2 replaces multiply with shifts and adds
  205. * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
  206. * (x * 0x67) >> 10: 1100111
  207. * (x * 0x34) >> 9: 110100 - same
  208. * (x * 0x1a) >> 8: 11010 - same
  209. * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
  210. */
  211. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  212. q = (d0 * 0xcd) >> 11;
  213. d0 = d0 - 10*q;
  214. *buf++ = d0 + '0';
  215. d1 = q + 9*d3 + 5*d2 + d1;
  216. q = (d1 * 0xcd) >> 11;
  217. d1 = d1 - 10*q;
  218. *buf++ = d1 + '0';
  219. d2 = q + 2*d2;
  220. q = (d2 * 0xd) >> 7;
  221. d2 = d2 - 10*q;
  222. *buf++ = d2 + '0';
  223. d3 = q + 4*d3;
  224. q = (d3 * 0xcd) >> 11; /* - shorter code */
  225. /* q = (d3 * 0x67) >> 10; - would also work */
  226. d3 = d3 - 10*q;
  227. *buf++ = d3 + '0';
  228. *buf++ = q + '0';
  229. return buf;
  230. }
  231. /* No inlining helps gcc to use registers better */
  232. static noinline char *put_dec(char *buf, u64 num)
  233. {
  234. while (1) {
  235. unsigned rem;
  236. if (num < 100000)
  237. return put_dec_trunc(buf, num);
  238. rem = do_div(num, 100000);
  239. buf = put_dec_full(buf, rem);
  240. }
  241. }
  242. #define ZEROPAD 1 /* pad with zero */
  243. #define SIGN 2 /* unsigned/signed long */
  244. #define PLUS 4 /* show plus */
  245. #define SPACE 8 /* space if plus */
  246. #define LEFT 16 /* left justified */
  247. #define SMALL 32 /* Must be 32 == 0x20 */
  248. #define SPECIAL 64 /* 0x */
  249. #ifdef CONFIG_SYS_VSNPRINTF
  250. /*
  251. * Macro to add a new character to our output string, but only if it will
  252. * fit. The macro moves to the next character position in the output string.
  253. */
  254. #define ADDCH(str, ch) do { \
  255. if ((str) < end) \
  256. *(str) = (ch); \
  257. ++str; \
  258. } while (0)
  259. #else
  260. #define ADDCH(str, ch) (*(str)++ = (ch))
  261. #endif
  262. static char *number(char *buf, char *end, u64 num,
  263. int base, int size, int precision, int type)
  264. {
  265. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  266. static const char digits[16] = "0123456789ABCDEF";
  267. char tmp[66];
  268. char sign;
  269. char locase;
  270. int need_pfx = ((type & SPECIAL) && base != 10);
  271. int i;
  272. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  273. * produces same digits or (maybe lowercased) letters */
  274. locase = (type & SMALL);
  275. if (type & LEFT)
  276. type &= ~ZEROPAD;
  277. sign = 0;
  278. if (type & SIGN) {
  279. if ((s64) num < 0) {
  280. sign = '-';
  281. num = -(s64) num;
  282. size--;
  283. } else if (type & PLUS) {
  284. sign = '+';
  285. size--;
  286. } else if (type & SPACE) {
  287. sign = ' ';
  288. size--;
  289. }
  290. }
  291. if (need_pfx) {
  292. size--;
  293. if (base == 16)
  294. size--;
  295. }
  296. /* generate full string in tmp[], in reverse order */
  297. i = 0;
  298. if (num == 0)
  299. tmp[i++] = '0';
  300. /* Generic code, for any base:
  301. else do {
  302. tmp[i++] = (digits[do_div(num,base)] | locase);
  303. } while (num != 0);
  304. */
  305. else if (base != 10) { /* 8 or 16 */
  306. int mask = base - 1;
  307. int shift = 3;
  308. if (base == 16)
  309. shift = 4;
  310. do {
  311. tmp[i++] = (digits[((unsigned char)num) & mask]
  312. | locase);
  313. num >>= shift;
  314. } while (num);
  315. } else { /* base 10 */
  316. i = put_dec(tmp, num) - tmp;
  317. }
  318. /* printing 100 using %2d gives "100", not "00" */
  319. if (i > precision)
  320. precision = i;
  321. /* leading space padding */
  322. size -= precision;
  323. if (!(type & (ZEROPAD + LEFT))) {
  324. while (--size >= 0)
  325. ADDCH(buf, ' ');
  326. }
  327. /* sign */
  328. if (sign)
  329. ADDCH(buf, sign);
  330. /* "0x" / "0" prefix */
  331. if (need_pfx) {
  332. ADDCH(buf, '0');
  333. if (base == 16)
  334. ADDCH(buf, 'X' | locase);
  335. }
  336. /* zero or space padding */
  337. if (!(type & LEFT)) {
  338. char c = (type & ZEROPAD) ? '0' : ' ';
  339. while (--size >= 0)
  340. ADDCH(buf, c);
  341. }
  342. /* hmm even more zero padding? */
  343. while (i <= --precision)
  344. ADDCH(buf, '0');
  345. /* actual digits of result */
  346. while (--i >= 0)
  347. ADDCH(buf, tmp[i]);
  348. /* trailing space padding */
  349. while (--size >= 0)
  350. ADDCH(buf, ' ');
  351. return buf;
  352. }
  353. static char *string(char *buf, char *end, char *s, int field_width,
  354. int precision, int flags)
  355. {
  356. int len, i;
  357. if (s == NULL)
  358. s = "<NULL>";
  359. len = strnlen(s, precision);
  360. if (!(flags & LEFT))
  361. while (len < field_width--)
  362. ADDCH(buf, ' ');
  363. for (i = 0; i < len; ++i)
  364. ADDCH(buf, *s++);
  365. while (len < field_width--)
  366. ADDCH(buf, ' ');
  367. return buf;
  368. }
  369. #ifdef CONFIG_CMD_NET
  370. static const char hex_asc[] = "0123456789abcdef";
  371. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  372. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  373. static inline char *pack_hex_byte(char *buf, u8 byte)
  374. {
  375. *buf++ = hex_asc_hi(byte);
  376. *buf++ = hex_asc_lo(byte);
  377. return buf;
  378. }
  379. static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
  380. int precision, int flags)
  381. {
  382. /* (6 * 2 hex digits), 5 colons and trailing zero */
  383. char mac_addr[6 * 3];
  384. char *p = mac_addr;
  385. int i;
  386. for (i = 0; i < 6; i++) {
  387. p = pack_hex_byte(p, addr[i]);
  388. if (!(flags & SPECIAL) && i != 5)
  389. *p++ = ':';
  390. }
  391. *p = '\0';
  392. return string(buf, end, mac_addr, field_width, precision,
  393. flags & ~SPECIAL);
  394. }
  395. static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
  396. int precision, int flags)
  397. {
  398. /* (8 * 4 hex digits), 7 colons and trailing zero */
  399. char ip6_addr[8 * 5];
  400. char *p = ip6_addr;
  401. int i;
  402. for (i = 0; i < 8; i++) {
  403. p = pack_hex_byte(p, addr[2 * i]);
  404. p = pack_hex_byte(p, addr[2 * i + 1]);
  405. if (!(flags & SPECIAL) && i != 7)
  406. *p++ = ':';
  407. }
  408. *p = '\0';
  409. return string(buf, end, ip6_addr, field_width, precision,
  410. flags & ~SPECIAL);
  411. }
  412. static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
  413. int precision, int flags)
  414. {
  415. /* (4 * 3 decimal digits), 3 dots and trailing zero */
  416. char ip4_addr[4 * 4];
  417. char temp[3]; /* hold each IP quad in reverse order */
  418. char *p = ip4_addr;
  419. int i, digits;
  420. for (i = 0; i < 4; i++) {
  421. digits = put_dec_trunc(temp, addr[i]) - temp;
  422. /* reverse the digits in the quad */
  423. while (digits--)
  424. *p++ = temp[digits];
  425. if (i != 3)
  426. *p++ = '.';
  427. }
  428. *p = '\0';
  429. return string(buf, end, ip4_addr, field_width, precision,
  430. flags & ~SPECIAL);
  431. }
  432. #endif
  433. /*
  434. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  435. * by an extra set of alphanumeric characters that are extended format
  436. * specifiers.
  437. *
  438. * Right now we handle:
  439. *
  440. * - 'M' For a 6-byte MAC address, it prints the address in the
  441. * usual colon-separated hex notation
  442. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  443. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  444. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  445. * currently the same
  446. *
  447. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  448. * function pointers are really function descriptors, which contain a
  449. * pointer to the real address.
  450. */
  451. static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  452. int field_width, int precision, int flags)
  453. {
  454. /*
  455. * Being a boot loader, we explicitly allow pointers to
  456. * (physical) address null.
  457. */
  458. #if 0
  459. if (!ptr)
  460. return string(buf, end, "(null)", field_width, precision,
  461. flags);
  462. #endif
  463. #ifdef CONFIG_CMD_NET
  464. switch (*fmt) {
  465. case 'm':
  466. flags |= SPECIAL;
  467. /* Fallthrough */
  468. case 'M':
  469. return mac_address_string(buf, end, ptr, field_width,
  470. precision, flags);
  471. case 'i':
  472. flags |= SPECIAL;
  473. /* Fallthrough */
  474. case 'I':
  475. if (fmt[1] == '6')
  476. return ip6_addr_string(buf, end, ptr, field_width,
  477. precision, flags);
  478. if (fmt[1] == '4')
  479. return ip4_addr_string(buf, end, ptr, field_width,
  480. precision, flags);
  481. flags &= ~SPECIAL;
  482. break;
  483. }
  484. #endif
  485. flags |= SMALL;
  486. if (field_width == -1) {
  487. field_width = 2*sizeof(void *);
  488. flags |= ZEROPAD;
  489. }
  490. return number(buf, end, (unsigned long)ptr, 16, field_width,
  491. precision, flags);
  492. }
  493. static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
  494. va_list args)
  495. {
  496. u64 num;
  497. int base;
  498. char *str;
  499. int flags; /* flags to number() */
  500. int field_width; /* width of output field */
  501. int precision; /* min. # of digits for integers; max
  502. number of chars for from string */
  503. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  504. /* 'z' support added 23/7/1999 S.H. */
  505. /* 'z' changed to 'Z' --davidm 1/25/99 */
  506. /* 't' added for ptrdiff_t */
  507. char *end = buf + size;
  508. #ifdef CONFIG_SYS_VSNPRINTF
  509. /* Make sure end is always >= buf - do we want this in U-Boot? */
  510. if (end < buf) {
  511. end = ((void *)-1);
  512. size = end - buf;
  513. }
  514. #endif
  515. str = buf;
  516. for (; *fmt ; ++fmt) {
  517. if (*fmt != '%') {
  518. ADDCH(str, *fmt);
  519. continue;
  520. }
  521. /* process flags */
  522. flags = 0;
  523. repeat:
  524. ++fmt; /* this also skips first '%' */
  525. switch (*fmt) {
  526. case '-':
  527. flags |= LEFT;
  528. goto repeat;
  529. case '+':
  530. flags |= PLUS;
  531. goto repeat;
  532. case ' ':
  533. flags |= SPACE;
  534. goto repeat;
  535. case '#':
  536. flags |= SPECIAL;
  537. goto repeat;
  538. case '0':
  539. flags |= ZEROPAD;
  540. goto repeat;
  541. }
  542. /* get field width */
  543. field_width = -1;
  544. if (is_digit(*fmt))
  545. field_width = skip_atoi(&fmt);
  546. else if (*fmt == '*') {
  547. ++fmt;
  548. /* it's the next argument */
  549. field_width = va_arg(args, int);
  550. if (field_width < 0) {
  551. field_width = -field_width;
  552. flags |= LEFT;
  553. }
  554. }
  555. /* get the precision */
  556. precision = -1;
  557. if (*fmt == '.') {
  558. ++fmt;
  559. if (is_digit(*fmt))
  560. precision = skip_atoi(&fmt);
  561. else if (*fmt == '*') {
  562. ++fmt;
  563. /* it's the next argument */
  564. precision = va_arg(args, int);
  565. }
  566. if (precision < 0)
  567. precision = 0;
  568. }
  569. /* get the conversion qualifier */
  570. qualifier = -1;
  571. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  572. *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
  573. qualifier = *fmt;
  574. ++fmt;
  575. if (qualifier == 'l' && *fmt == 'l') {
  576. qualifier = 'L';
  577. ++fmt;
  578. }
  579. }
  580. /* default base */
  581. base = 10;
  582. switch (*fmt) {
  583. case 'c':
  584. if (!(flags & LEFT)) {
  585. while (--field_width > 0)
  586. ADDCH(str, ' ');
  587. }
  588. ADDCH(str, (unsigned char) va_arg(args, int));
  589. while (--field_width > 0)
  590. ADDCH(str, ' ');
  591. continue;
  592. case 's':
  593. str = string(str, end, va_arg(args, char *),
  594. field_width, precision, flags);
  595. continue;
  596. case 'p':
  597. str = pointer(fmt + 1, str, end,
  598. va_arg(args, void *),
  599. field_width, precision, flags);
  600. /* Skip all alphanumeric pointer suffixes */
  601. while (isalnum(fmt[1]))
  602. fmt++;
  603. continue;
  604. case 'n':
  605. if (qualifier == 'l') {
  606. long *ip = va_arg(args, long *);
  607. *ip = (str - buf);
  608. } else {
  609. int *ip = va_arg(args, int *);
  610. *ip = (str - buf);
  611. }
  612. continue;
  613. case '%':
  614. ADDCH(str, '%');
  615. continue;
  616. /* integer number formats - set up the flags and "break" */
  617. case 'o':
  618. base = 8;
  619. break;
  620. case 'x':
  621. flags |= SMALL;
  622. case 'X':
  623. base = 16;
  624. break;
  625. case 'd':
  626. case 'i':
  627. flags |= SIGN;
  628. case 'u':
  629. break;
  630. default:
  631. ADDCH(str, '%');
  632. if (*fmt)
  633. ADDCH(str, *fmt);
  634. else
  635. --fmt;
  636. continue;
  637. }
  638. if (qualifier == 'L') /* "quad" for 64 bit variables */
  639. num = va_arg(args, unsigned long long);
  640. else if (qualifier == 'l') {
  641. num = va_arg(args, unsigned long);
  642. if (flags & SIGN)
  643. num = (signed long) num;
  644. } else if (qualifier == 'Z' || qualifier == 'z') {
  645. num = va_arg(args, size_t);
  646. } else if (qualifier == 't') {
  647. num = va_arg(args, ptrdiff_t);
  648. } else if (qualifier == 'h') {
  649. num = (unsigned short) va_arg(args, int);
  650. if (flags & SIGN)
  651. num = (signed short) num;
  652. } else {
  653. num = va_arg(args, unsigned int);
  654. if (flags & SIGN)
  655. num = (signed int) num;
  656. }
  657. str = number(str, end, num, base, field_width, precision,
  658. flags);
  659. }
  660. #ifdef CONFIG_SYS_VSNPRINTF
  661. if (size > 0) {
  662. ADDCH(str, '\0');
  663. if (str > end)
  664. end[-1] = '\0';
  665. --str;
  666. }
  667. #else
  668. *str = '\0';
  669. #endif
  670. /* the trailing null byte doesn't count towards the total */
  671. return str - buf;
  672. }
  673. #ifdef CONFIG_SYS_VSNPRINTF
  674. int vsnprintf(char *buf, size_t size, const char *fmt,
  675. va_list args)
  676. {
  677. return vsnprintf_internal(buf, size, fmt, args);
  678. }
  679. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  680. {
  681. int i;
  682. i = vsnprintf(buf, size, fmt, args);
  683. if (likely(i < size))
  684. return i;
  685. if (size != 0)
  686. return size - 1;
  687. return 0;
  688. }
  689. int snprintf(char *buf, size_t size, const char *fmt, ...)
  690. {
  691. va_list args;
  692. int i;
  693. va_start(args, fmt);
  694. i = vsnprintf(buf, size, fmt, args);
  695. va_end(args);
  696. return i;
  697. }
  698. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  699. {
  700. va_list args;
  701. int i;
  702. va_start(args, fmt);
  703. i = vscnprintf(buf, size, fmt, args);
  704. va_end(args);
  705. return i;
  706. }
  707. #endif /* CONFIG_SYS_VSNPRINT */
  708. /**
  709. * Format a string and place it in a buffer (va_list version)
  710. *
  711. * @param buf The buffer to place the result into
  712. * @param fmt The format string to use
  713. * @param args Arguments for the format string
  714. *
  715. * The function returns the number of characters written
  716. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  717. * buffer overflows.
  718. *
  719. * If you're not already dealing with a va_list consider using sprintf().
  720. */
  721. int vsprintf(char *buf, const char *fmt, va_list args)
  722. {
  723. return vsnprintf_internal(buf, INT_MAX, fmt, args);
  724. }
  725. int sprintf(char *buf, const char *fmt, ...)
  726. {
  727. va_list args;
  728. int i;
  729. va_start(args, fmt);
  730. i = vsprintf(buf, fmt, args);
  731. va_end(args);
  732. return i;
  733. }
  734. void panic(const char *fmt, ...)
  735. {
  736. va_list args;
  737. va_start(args, fmt);
  738. vprintf(fmt, args);
  739. putc('\n');
  740. va_end(args);
  741. #if defined(CONFIG_PANIC_HANG)
  742. hang();
  743. #else
  744. udelay(100000); /* allow messages to go out */
  745. do_reset(NULL, 0, 0, NULL);
  746. #endif
  747. while (1)
  748. ;
  749. }
  750. void __assert_fail(const char *assertion, const char *file, unsigned line,
  751. const char *function)
  752. {
  753. /* This will not return */
  754. panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
  755. assertion);
  756. }
  757. char *simple_itoa(ulong i)
  758. {
  759. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  760. static char local[22];
  761. char *p = &local[21];
  762. *p-- = '\0';
  763. do {
  764. *p-- = '0' + i % 10;
  765. i /= 10;
  766. } while (i > 0);
  767. return p + 1;
  768. }
  769. /* We don't seem to have %'d in U-Boot */
  770. void print_grouped_ull(unsigned long long int_val, int digits)
  771. {
  772. char str[21], *s;
  773. int grab = 3;
  774. digits = (digits + 2) / 3;
  775. sprintf(str, "%*llu", digits * 3, int_val);
  776. for (s = str; *s; s += grab) {
  777. if (s != str)
  778. putc(s[-1] != ' ' ? ',' : ' ');
  779. printf("%.*s", grab, s);
  780. grab = 3;
  781. }
  782. }