vsprintf.c 19 KB

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