vsprintf.c 17 KB

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