vsprintf.c 21 KB

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