vsprintf.c 6.5 KB

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