vsprintf.c 18 KB

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