printf.c 6.0 KB

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