printf.c 5.9 KB

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