vsprintf.c 17 KB

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