printf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * Oh, it's a waste of space, but oh-so-yummy for debugging. This
  12. * version of printf() does not include 64-bit support. "Live with
  13. * it."
  14. *
  15. */
  16. #include "boot.h"
  17. static int skip_atoi(const char **s)
  18. {
  19. int i = 0;
  20. while (isdigit(**s))
  21. i = i * 10 + *((*s)++) - '0';
  22. return i;
  23. }
  24. #define ZEROPAD 1 /* pad with zero */
  25. #define SIGN 2 /* unsigned/signed long */
  26. #define PLUS 4 /* show plus */
  27. #define SPACE 8 /* space if plus */
  28. #define LEFT 16 /* left justified */
  29. #define SMALL 32 /* Must be 32 == 0x20 */
  30. #define SPECIAL 64 /* 0x */
  31. #define __do_div(n, base) ({ \
  32. int __res; \
  33. __res = ((unsigned long) n) % (unsigned) base; \
  34. n = ((unsigned long) n) / (unsigned) base; \
  35. __res; })
  36. static char *number(char *str, long num, int base, int size, int precision,
  37. int type)
  38. {
  39. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  40. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  41. char tmp[66];
  42. char c, sign, locase;
  43. int i;
  44. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  45. * produces same digits or (maybe lowercased) letters */
  46. locase = (type & SMALL);
  47. if (type & LEFT)
  48. type &= ~ZEROPAD;
  49. if (base < 2 || base > 16)
  50. return NULL;
  51. c = (type & ZEROPAD) ? '0' : ' ';
  52. sign = 0;
  53. if (type & SIGN) {
  54. if (num < 0) {
  55. sign = '-';
  56. num = -num;
  57. size--;
  58. } else if (type & PLUS) {
  59. sign = '+';
  60. size--;
  61. } else if (type & SPACE) {
  62. sign = ' ';
  63. size--;
  64. }
  65. }
  66. if (type & SPECIAL) {
  67. if (base == 16)
  68. size -= 2;
  69. else if (base == 8)
  70. size--;
  71. }
  72. i = 0;
  73. if (num == 0)
  74. tmp[i++] = '0';
  75. else
  76. while (num != 0)
  77. tmp[i++] = (digits[__do_div(num, base)] | locase);
  78. if (i > precision)
  79. precision = i;
  80. size -= precision;
  81. if (!(type & (ZEROPAD + LEFT)))
  82. while (size-- > 0)
  83. *str++ = ' ';
  84. if (sign)
  85. *str++ = sign;
  86. if (type & SPECIAL) {
  87. if (base == 8)
  88. *str++ = '0';
  89. else if (base == 16) {
  90. *str++ = '0';
  91. *str++ = ('X' | locase);
  92. }
  93. }
  94. if (!(type & LEFT))
  95. while (size-- > 0)
  96. *str++ = c;
  97. while (i < precision--)
  98. *str++ = '0';
  99. while (i-- > 0)
  100. *str++ = tmp[i];
  101. while (size-- > 0)
  102. *str++ = ' ';
  103. return str;
  104. }
  105. int vsprintf(char *buf, const char *fmt, va_list args)
  106. {
  107. int len;
  108. unsigned long num;
  109. int i, base;
  110. char *str;
  111. const char *s;
  112. int flags; /* flags to number() */
  113. int field_width; /* width of output field */
  114. int precision; /* min. # of digits for integers; max
  115. number of chars for from string */
  116. int qualifier; /* 'h', 'l', or 'L' for integer fields */
  117. for (str = buf; *fmt; ++fmt) {
  118. if (*fmt != '%') {
  119. *str++ = *fmt;
  120. continue;
  121. }
  122. /* process flags */
  123. flags = 0;
  124. repeat:
  125. ++fmt; /* this also skips first '%' */
  126. switch (*fmt) {
  127. case '-':
  128. flags |= LEFT;
  129. goto repeat;
  130. case '+':
  131. flags |= PLUS;
  132. goto repeat;
  133. case ' ':
  134. flags |= SPACE;
  135. goto repeat;
  136. case '#':
  137. flags |= SPECIAL;
  138. goto repeat;
  139. case '0':
  140. flags |= ZEROPAD;
  141. goto repeat;
  142. }
  143. /* get field width */
  144. field_width = -1;
  145. if (isdigit(*fmt))
  146. field_width = skip_atoi(&fmt);
  147. else if (*fmt == '*') {
  148. ++fmt;
  149. /* it's the next argument */
  150. field_width = va_arg(args, int);
  151. if (field_width < 0) {
  152. field_width = -field_width;
  153. flags |= LEFT;
  154. }
  155. }
  156. /* get the precision */
  157. precision = -1;
  158. if (*fmt == '.') {
  159. ++fmt;
  160. if (isdigit(*fmt))
  161. precision = skip_atoi(&fmt);
  162. else if (*fmt == '*') {
  163. ++fmt;
  164. /* it's the next argument */
  165. precision = va_arg(args, int);
  166. }
  167. if (precision < 0)
  168. precision = 0;
  169. }
  170. /* get the conversion qualifier */
  171. qualifier = -1;
  172. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
  173. qualifier = *fmt;
  174. ++fmt;
  175. }
  176. /* default base */
  177. base = 10;
  178. switch (*fmt) {
  179. case 'c':
  180. if (!(flags & LEFT))
  181. while (--field_width > 0)
  182. *str++ = ' ';
  183. *str++ = (unsigned char)va_arg(args, int);
  184. while (--field_width > 0)
  185. *str++ = ' ';
  186. continue;
  187. case 's':
  188. s = va_arg(args, char *);
  189. len = strnlen(s, precision);
  190. if (!(flags & LEFT))
  191. while (len < field_width--)
  192. *str++ = ' ';
  193. for (i = 0; i < len; ++i)
  194. *str++ = *s++;
  195. while (len < field_width--)
  196. *str++ = ' ';
  197. continue;
  198. case 'p':
  199. if (field_width == -1) {
  200. field_width = 2 * sizeof(void *);
  201. flags |= ZEROPAD;
  202. }
  203. str = number(str,
  204. (unsigned long)va_arg(args, void *), 16,
  205. field_width, precision, flags);
  206. continue;
  207. case 'n':
  208. if (qualifier == 'l') {
  209. long *ip = va_arg(args, long *);
  210. *ip = (str - buf);
  211. } else {
  212. int *ip = va_arg(args, int *);
  213. *ip = (str - buf);
  214. }
  215. continue;
  216. case '%':
  217. *str++ = '%';
  218. continue;
  219. /* integer number formats - set up the flags and "break" */
  220. case 'o':
  221. base = 8;
  222. break;
  223. case 'x':
  224. flags |= SMALL;
  225. case 'X':
  226. base = 16;
  227. break;
  228. case 'd':
  229. case 'i':
  230. flags |= SIGN;
  231. case 'u':
  232. break;
  233. default:
  234. *str++ = '%';
  235. if (*fmt)
  236. *str++ = *fmt;
  237. else
  238. --fmt;
  239. continue;
  240. }
  241. if (qualifier == 'l')
  242. num = va_arg(args, unsigned long);
  243. else if (qualifier == 'h') {
  244. num = (unsigned short)va_arg(args, int);
  245. if (flags & SIGN)
  246. num = (short)num;
  247. } else if (flags & SIGN)
  248. num = va_arg(args, int);
  249. else
  250. num = va_arg(args, unsigned int);
  251. str = number(str, num, base, field_width, precision, flags);
  252. }
  253. *str = '\0';
  254. return str - buf;
  255. }
  256. int sprintf(char *buf, const char *fmt, ...)
  257. {
  258. va_list args;
  259. int i;
  260. va_start(args, fmt);
  261. i = vsprintf(buf, fmt, args);
  262. va_end(args);
  263. return i;
  264. }
  265. int printf(const char *fmt, ...)
  266. {
  267. char printf_buf[1024];
  268. va_list args;
  269. int printed;
  270. va_start(args, fmt);
  271. printed = vsprintf(printf_buf, fmt, args);
  272. va_end(args);
  273. puts(printf_buf);
  274. return printed;
  275. }