vsprintf.c 18 KB

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