vsprintf.c 18 KB

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