vsprintf.c 18 KB

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