vsprintf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <common.h>
  15. #if !defined (CONFIG_PANIC_HANG)
  16. #include <command.h>
  17. /*cmd_boot.c*/
  18. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  19. #endif
  20. unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
  21. {
  22. unsigned long result = 0,value;
  23. if (*cp == '0') {
  24. cp++;
  25. if ((*cp == 'x') && isxdigit(cp[1])) {
  26. base = 16;
  27. cp++;
  28. }
  29. if (!base) {
  30. base = 8;
  31. }
  32. }
  33. if (!base) {
  34. base = 10;
  35. }
  36. while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
  37. ? toupper(*cp) : *cp)-'A'+10) < base) {
  38. result = result*base + value;
  39. cp++;
  40. }
  41. if (endp)
  42. *endp = (char *)cp;
  43. return result;
  44. }
  45. long simple_strtol(const char *cp,char **endp,unsigned int base)
  46. {
  47. if(*cp=='-')
  48. return -simple_strtoul(cp+1,endp,base);
  49. return simple_strtoul(cp,endp,base);
  50. }
  51. #if CFG_64BIT_STRTOUL
  52. unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
  53. {
  54. unsigned long long result = 0, value;
  55. if (*cp == '0') {
  56. cp++;
  57. if ((*cp == 'x') && isxdigit (cp[1])) {
  58. base = 16;
  59. cp++;
  60. }
  61. if (!base) {
  62. base = 8;
  63. }
  64. }
  65. if (!base) {
  66. base = 10;
  67. }
  68. while (isxdigit (*cp) && (value = isdigit (*cp)
  69. ? *cp - '0'
  70. : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
  71. result = result * base + value;
  72. cp++;
  73. }
  74. if (endp)
  75. *endp = (char *) cp;
  76. return result;
  77. }
  78. #endif /* CFG_64BIT_STRTOUL */
  79. /* we use this so that we can do without the ctype library */
  80. #define is_digit(c) ((c) >= '0' && (c) <= '9')
  81. static int skip_atoi(const char **s)
  82. {
  83. int i=0;
  84. while (is_digit(**s))
  85. i = i*10 + *((*s)++) - '0';
  86. return i;
  87. }
  88. #define ZEROPAD 1 /* pad with zero */
  89. #define SIGN 2 /* unsigned/signed long */
  90. #define PLUS 4 /* show plus */
  91. #define SPACE 8 /* space if plus */
  92. #define LEFT 16 /* left justified */
  93. #define SPECIAL 32 /* 0x */
  94. #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
  95. #define do_div(n,base) ({ \
  96. int __res; \
  97. __res = ((unsigned long) n) % (unsigned) base; \
  98. n = ((unsigned long) n) / (unsigned) base; \
  99. __res; \
  100. })
  101. #if CFG_64BIT_VSPRINTF
  102. static char * number(char * str, long long num, int base, int size, int precision ,int type)
  103. #else
  104. static char * number(char * str, long num, int base, int size, int precision ,int type)
  105. #endif
  106. {
  107. char c,sign,tmp[66];
  108. const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
  109. int i;
  110. if (type & LARGE)
  111. digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  112. if (type & LEFT)
  113. type &= ~ZEROPAD;
  114. if (base < 2 || base > 36)
  115. return 0;
  116. c = (type & ZEROPAD) ? '0' : ' ';
  117. sign = 0;
  118. if (type & SIGN) {
  119. if (num < 0) {
  120. sign = '-';
  121. num = -num;
  122. size--;
  123. } else if (type & PLUS) {
  124. sign = '+';
  125. size--;
  126. } else if (type & SPACE) {
  127. sign = ' ';
  128. size--;
  129. }
  130. }
  131. if (type & SPECIAL) {
  132. if (base == 16)
  133. size -= 2;
  134. else if (base == 8)
  135. size--;
  136. }
  137. i = 0;
  138. if (num == 0)
  139. tmp[i++]='0';
  140. else while (num != 0)
  141. tmp[i++] = digits[do_div(num,base)];
  142. if (i > precision)
  143. precision = i;
  144. size -= precision;
  145. if (!(type&(ZEROPAD+LEFT)))
  146. while(size-->0)
  147. *str++ = ' ';
  148. if (sign)
  149. *str++ = sign;
  150. if (type & SPECIAL) {
  151. if (base==8)
  152. *str++ = '0';
  153. else if (base==16) {
  154. *str++ = '0';
  155. *str++ = digits[33];
  156. }
  157. }
  158. if (!(type & LEFT))
  159. while (size-- > 0)
  160. *str++ = c;
  161. while (i < precision--)
  162. *str++ = '0';
  163. while (i-- > 0)
  164. *str++ = tmp[i];
  165. while (size-- > 0)
  166. *str++ = ' ';
  167. return str;
  168. }
  169. /* Forward decl. needed for IP address printing stuff... */
  170. int sprintf(char * buf, const char *fmt, ...);
  171. int vsprintf(char *buf, const char *fmt, va_list args)
  172. {
  173. int len;
  174. #if CFG_64BIT_VSPRINTF
  175. unsigned long long num;
  176. #else
  177. unsigned long num;
  178. #endif
  179. int i, base;
  180. char * str;
  181. const char *s;
  182. int flags; /* flags to number() */
  183. int field_width; /* width of output field */
  184. int precision; /* min. # of digits for integers; max
  185. number of chars for from string */
  186. int qualifier; /* 'h', 'l', or 'q' for integer fields */
  187. for (str=buf ; *fmt ; ++fmt) {
  188. if (*fmt != '%') {
  189. *str++ = *fmt;
  190. continue;
  191. }
  192. /* process flags */
  193. flags = 0;
  194. repeat:
  195. ++fmt; /* this also skips first '%' */
  196. switch (*fmt) {
  197. case '-': flags |= LEFT; goto repeat;
  198. case '+': flags |= PLUS; goto repeat;
  199. case ' ': flags |= SPACE; goto repeat;
  200. case '#': flags |= SPECIAL; goto repeat;
  201. case '0': flags |= ZEROPAD; goto repeat;
  202. }
  203. /* get field width */
  204. field_width = -1;
  205. if (is_digit(*fmt))
  206. field_width = skip_atoi(&fmt);
  207. else if (*fmt == '*') {
  208. ++fmt;
  209. /* it's the next argument */
  210. field_width = va_arg(args, int);
  211. if (field_width < 0) {
  212. field_width = -field_width;
  213. flags |= LEFT;
  214. }
  215. }
  216. /* get the precision */
  217. precision = -1;
  218. if (*fmt == '.') {
  219. ++fmt;
  220. if (is_digit(*fmt))
  221. precision = skip_atoi(&fmt);
  222. else if (*fmt == '*') {
  223. ++fmt;
  224. /* it's the next argument */
  225. precision = va_arg(args, int);
  226. }
  227. if (precision < 0)
  228. precision = 0;
  229. }
  230. /* get the conversion qualifier */
  231. qualifier = -1;
  232. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'q') {
  233. qualifier = *fmt;
  234. ++fmt;
  235. }
  236. /* default base */
  237. base = 10;
  238. switch (*fmt) {
  239. case 'c':
  240. if (!(flags & LEFT))
  241. while (--field_width > 0)
  242. *str++ = ' ';
  243. *str++ = (unsigned char) va_arg(args, int);
  244. while (--field_width > 0)
  245. *str++ = ' ';
  246. continue;
  247. case 's':
  248. s = va_arg(args, char *);
  249. if (!s)
  250. s = "<NULL>";
  251. len = strnlen(s, precision);
  252. if (!(flags & LEFT))
  253. while (len < field_width--)
  254. *str++ = ' ';
  255. for (i = 0; i < len; ++i)
  256. *str++ = *s++;
  257. while (len < field_width--)
  258. *str++ = ' ';
  259. continue;
  260. case 'p':
  261. if (field_width == -1) {
  262. field_width = 2*sizeof(void *);
  263. flags |= ZEROPAD;
  264. }
  265. str = number(str,
  266. (unsigned long) va_arg(args, void *), 16,
  267. field_width, precision, flags);
  268. continue;
  269. case 'n':
  270. if (qualifier == 'l') {
  271. long * ip = va_arg(args, long *);
  272. *ip = (str - buf);
  273. } else {
  274. int * ip = va_arg(args, int *);
  275. *ip = (str - buf);
  276. }
  277. continue;
  278. case '%':
  279. *str++ = '%';
  280. continue;
  281. /* integer number formats - set up the flags and "break" */
  282. case 'o':
  283. base = 8;
  284. break;
  285. case 'X':
  286. flags |= LARGE;
  287. case 'x':
  288. base = 16;
  289. break;
  290. case 'd':
  291. case 'i':
  292. flags |= SIGN;
  293. case 'u':
  294. break;
  295. default:
  296. *str++ = '%';
  297. if (*fmt)
  298. *str++ = *fmt;
  299. else
  300. --fmt;
  301. continue;
  302. }
  303. #if CFG_64BIT_VSPRINTF
  304. if (qualifier == 'q') /* "quad" for 64 bit variables */
  305. num = va_arg(args, unsigned long long);
  306. else
  307. #endif
  308. if (qualifier == 'l')
  309. num = va_arg(args, unsigned long);
  310. else if (qualifier == 'h') {
  311. num = (unsigned short) va_arg(args, int);
  312. if (flags & SIGN)
  313. num = (short) num;
  314. } else if (flags & SIGN)
  315. num = va_arg(args, int);
  316. else
  317. num = va_arg(args, unsigned int);
  318. str = number(str, num, base, field_width, precision, flags);
  319. }
  320. *str = '\0';
  321. return str-buf;
  322. }
  323. int sprintf(char * buf, const char *fmt, ...)
  324. {
  325. va_list args;
  326. int i;
  327. va_start(args, fmt);
  328. i=vsprintf(buf,fmt,args);
  329. va_end(args);
  330. return i;
  331. }
  332. void panic(const char *fmt, ...)
  333. {
  334. va_list args;
  335. va_start(args, fmt);
  336. vprintf(fmt, args);
  337. putc('\n');
  338. va_end(args);
  339. #if defined (CONFIG_PANIC_HANG)
  340. hang();
  341. #else
  342. udelay (100000); /* allow messages to go out */
  343. do_reset (NULL, 0, 0, NULL);
  344. #endif
  345. }