vsprintf.c 19 KB

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