vsprintf.c 6.6 KB

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