printf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* $OpenBSD: printf.c,v 1.27 2015/06/14 10:55:50 miod Exp $ */
  2. /* $NetBSD: printf.c,v 1.10 1996/11/30 04:19:21 gwr Exp $ */
  3. /*-
  4. * Copyright (c) 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)printf.c 8.1 (Berkeley) 6/11/93
  32. */
  33. /*
  34. * Scaled down version of printf(3).
  35. *
  36. * One additional format:
  37. *
  38. * The format %b is supported to decode error registers.
  39. * Its usage is:
  40. *
  41. * printf("reg=%b\n", regval, "<base><arg>*");
  42. *
  43. * where <base> is the output base expressed as a control character, e.g.
  44. * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
  45. * the first of which gives the bit number to be inspected (origin 1), and
  46. * the next characters (up to a control character, i.e. a character <= 32),
  47. * give the name of the register. Thus:
  48. *
  49. * printf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
  50. *
  51. * would produce output:
  52. *
  53. * reg=3<BITTWO,BITONE>
  54. */
  55. #include <sys/types.h>
  56. #include <sys/stdarg.h>
  57. #include "stand.h"
  58. void kprintn(void (*)(int), u_long, int);
  59. #ifdef LIBSA_LONGLONG_PRINTF
  60. void kprintn64(void (*)(int), u_int64_t, int);
  61. #endif
  62. void kdoprnt(void (*)(int), const char *, va_list);
  63. const char hexdig[] = "0123456789abcdef";
  64. void
  65. printf(const char *fmt, ...)
  66. {
  67. va_list ap;
  68. va_start(ap, fmt);
  69. kdoprnt(putchar, fmt, ap);
  70. va_end(ap);
  71. }
  72. void
  73. vprintf(const char *fmt, va_list ap)
  74. {
  75. kdoprnt(putchar, fmt, ap);
  76. }
  77. void
  78. kdoprnt(void (*put)(int), const char *fmt, va_list ap)
  79. {
  80. #ifdef LIBSA_LONGLONG_PRINTF
  81. u_int64_t ull;
  82. #endif
  83. unsigned long ul;
  84. int ch, lflag;
  85. char *p;
  86. for (;;) {
  87. while ((ch = *fmt++) != '%') {
  88. if (ch == '\0')
  89. return;
  90. put(ch);
  91. }
  92. lflag = 0;
  93. reswitch: switch (ch = *fmt++) {
  94. case 'l':
  95. lflag++;
  96. goto reswitch;
  97. #ifndef STRIPPED
  98. case 'b':
  99. {
  100. int set, n;
  101. ul = va_arg(ap, int);
  102. p = va_arg(ap, char *);
  103. kprintn(put, ul, *p++);
  104. if (!ul)
  105. break;
  106. for (set = 0; (n = *p++);) {
  107. if (ul & (1 << (n - 1))) {
  108. put(set ? ',' : '<');
  109. for (; (n = *p) > ' '; ++p)
  110. put(n);
  111. set = 1;
  112. } else
  113. for (; *p > ' '; ++p)
  114. ;
  115. }
  116. if (set)
  117. put('>');
  118. }
  119. break;
  120. #endif
  121. case 'c':
  122. ch = va_arg(ap, int);
  123. put(ch & 0x7f);
  124. break;
  125. case 's':
  126. p = va_arg(ap, char *);
  127. while ((ch = *p++))
  128. put(ch);
  129. break;
  130. case 'd':
  131. #ifdef LIBSA_LONGLONG_PRINTF
  132. if (lflag > 1) {
  133. ull = va_arg(ap, int64_t);
  134. if ((int64_t)ull < 0) {
  135. put('-');
  136. ull = -(int64_t)ull;
  137. }
  138. kprintn64(put, ull, 10);
  139. break;
  140. }
  141. #endif
  142. ul = lflag ?
  143. va_arg(ap, long) : va_arg(ap, int);
  144. if ((long)ul < 0) {
  145. put('-');
  146. ul = -(long)ul;
  147. }
  148. kprintn(put, ul, 10);
  149. break;
  150. case 'o':
  151. #ifdef LIBSA_LONGLONG_PRINTF
  152. if (lflag > 1) {
  153. ull = va_arg(ap, u_int64_t);
  154. kprintn64(put, ull, 8);
  155. break;
  156. }
  157. #endif
  158. ul = lflag ?
  159. va_arg(ap, u_long) : va_arg(ap, u_int);
  160. kprintn(put, ul, 8);
  161. break;
  162. case 'u':
  163. #ifdef LIBSA_LONGLONG_PRINTF
  164. if (lflag > 1) {
  165. ull = va_arg(ap, u_int64_t);
  166. kprintn64(put, ull, 10);
  167. break;
  168. }
  169. #endif
  170. ul = lflag ?
  171. va_arg(ap, u_long) : va_arg(ap, u_int);
  172. kprintn(put, ul, 10);
  173. break;
  174. case 'p':
  175. put('0');
  176. put('x');
  177. lflag += sizeof(void *)==sizeof(u_long)? 1 : 0;
  178. case 'x':
  179. #ifdef LIBSA_LONGLONG_PRINTF
  180. if (lflag > 1) {
  181. ull = va_arg(ap, u_int64_t);
  182. kprintn64(put, ull, 16);
  183. break;
  184. }
  185. #else
  186. if (lflag > 1) {
  187. /* hold an int64_t in base 16 */
  188. char *p, buf[(sizeof(u_int64_t) * NBBY / 4) + 1];
  189. u_int64_t ull;
  190. ull = va_arg(ap, u_int64_t);
  191. p = buf;
  192. do {
  193. *p++ = hexdig[ull & 15];
  194. } while (ull >>= 4);
  195. do {
  196. put(*--p);
  197. } while (p > buf);
  198. break;
  199. }
  200. #endif
  201. ul = lflag ?
  202. va_arg(ap, u_long) : va_arg(ap, u_int);
  203. kprintn(put, ul, 16);
  204. break;
  205. default:
  206. put('%');
  207. #ifdef LIBSA_LONGLONG_PRINTF
  208. while (--lflag)
  209. #else
  210. if (lflag)
  211. #endif
  212. put('l');
  213. put(ch);
  214. }
  215. }
  216. }
  217. void
  218. kprintn(void (*put)(int), unsigned long ul, int base)
  219. {
  220. /* hold a long in base 8 */
  221. char *p, buf[(sizeof(long) * NBBY / 3) + 1];
  222. p = buf;
  223. do {
  224. *p++ = hexdig[ul % base];
  225. } while (ul /= base);
  226. do {
  227. put(*--p);
  228. } while (p > buf);
  229. }
  230. #ifdef LIBSA_LONGLONG_PRINTF
  231. void
  232. kprintn64(void (*put)(int), u_int64_t ull, int base)
  233. {
  234. /* hold an int64_t in base 8 */
  235. char *p, buf[(sizeof(u_int64_t) * NBBY / 3) + 1];
  236. p = buf;
  237. do {
  238. *p++ = hexdig[ull % base];
  239. } while (ull /= base);
  240. do {
  241. put(*--p);
  242. } while (p > buf);
  243. }
  244. #endif
  245. int donottwiddle = 0;
  246. void
  247. twiddle(void)
  248. {
  249. static int pos;
  250. if (!donottwiddle) {
  251. putchar("|/-\\"[pos++ & 3]);
  252. putchar('\b');
  253. }
  254. }