printf.c 5.8 KB

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