vsprintf.c 17 KB

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