printfmt.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* Implementation of printf console output for user environments.
  2. *
  3. * printf is a debugging statement, not a generic output statement.
  4. * It is very important that it always go to the console, especially when
  5. * debugging file descriptor code! */
  6. #include <stdint.h>
  7. #include <stdarg.h>
  8. #include "api.h"
  9. // Print a number (base <= 16) in reverse order,
  10. // using specified fputch function and associated pointer putdat.
  11. #if !defined(__i386__)
  12. static int
  13. printnum(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  14. unsigned long long num, unsigned base, int width, int padc)
  15. #else
  16. static int
  17. printnum(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  18. unsigned long num, unsigned base, int width, int padc)
  19. #endif
  20. {
  21. // first recursively print all preceding (more significant) digits
  22. if (num >= base) {
  23. if (printnum(_fputch, f, putdat, num / base, base, width - 1, padc) == -1)
  24. return -1;
  25. } else {
  26. // print any needed pad characters before first digit
  27. while (--width > 0)
  28. if ((*_fputch) (f, padc, putdat) == -1)
  29. return -1;
  30. }
  31. // then print this (the least significant) digit
  32. if ((*_fputch) (f, "0123456789abcdef"[num % base], putdat) == -1)
  33. return -1;
  34. return 0;
  35. }
  36. // Get an unsigned int of various possible sizes from a varargs list,
  37. // depending on the lflag parameter.
  38. #if !defined(__i386__)
  39. inline unsigned long long
  40. getuint(va_list *ap, int lflag)
  41. #else
  42. inline unsigned long
  43. getuint(va_list *ap, int lflag)
  44. #endif
  45. {
  46. #if !defined(__i386__)
  47. if (lflag >= 2)
  48. return va_arg(*ap, unsigned long long);
  49. else
  50. #endif
  51. if (lflag)
  52. return va_arg(*ap, unsigned long);
  53. else
  54. return va_arg(*ap, unsigned int);
  55. }
  56. // Same as getuint but signed - can't use getuint
  57. // because of sign extension
  58. #if !defined(__i386__)
  59. inline long long
  60. getint(va_list *ap, int lflag)
  61. #else
  62. inline long
  63. getint(va_list *ap, int lflag)
  64. #endif
  65. {
  66. #if !defined(__i386__)
  67. if (lflag >= 2)
  68. return va_arg(*ap, long long);
  69. else
  70. #endif
  71. if (lflag)
  72. return va_arg(*ap, long);
  73. else
  74. return va_arg(*ap, int);
  75. }
  76. // Main function to format and print a string.
  77. void fprintfmt(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  78. const char * fmt, ...);
  79. void
  80. vfprintfmt(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  81. const char * fmt, va_list *ap)
  82. {
  83. register const char *p;
  84. register int ch;
  85. #if !defined(__i386__)
  86. unsigned long long num;
  87. #else
  88. unsigned long num;
  89. #endif
  90. int base, lflag, width, precision, altflag;
  91. char padc;
  92. while (1) {
  93. while ((ch = *(unsigned char *) (fmt++)) != '%') {
  94. if (ch == '\0')
  95. return;
  96. if ((*_fputch) (f, ch, putdat) < 0)
  97. return;
  98. }
  99. // Process a %-escape sequence
  100. padc = ' ';
  101. width = -1;
  102. precision = -1;
  103. lflag = 0;
  104. altflag = 0;
  105. reswitch:
  106. switch (ch = *(unsigned char *) (fmt++)) {
  107. // flag to pad on the right
  108. case '-':
  109. padc = ' ';
  110. goto reswitch;
  111. // flag to pad with 0's instead of spaces
  112. case '0':
  113. padc = '0';
  114. goto reswitch;
  115. // width field
  116. case '1':
  117. case '2':
  118. case '3':
  119. case '4':
  120. case '5':
  121. case '6':
  122. case '7':
  123. case '8':
  124. case '9':
  125. for (precision = 0; ; ++fmt) {
  126. precision = precision * 10 + ch - '0';
  127. ch = *fmt;
  128. if (ch < '0' || ch > '9')
  129. break;
  130. }
  131. goto process_precision;
  132. case '*':
  133. precision = va_arg(*ap, int);
  134. goto process_precision;
  135. case '.':
  136. if (width < 0)
  137. width = 0;
  138. goto reswitch;
  139. case '#':
  140. altflag = 1;
  141. goto reswitch;
  142. process_precision:
  143. if (width < 0)
  144. width = precision, precision = -1;
  145. goto reswitch;
  146. // long flag (doubled for long long)
  147. case 'l':
  148. lflag++;
  149. goto reswitch;
  150. // character
  151. case 'c':
  152. if ((*_fputch) (f, va_arg(*ap, int), putdat) == -1)
  153. return;
  154. break;
  155. // string
  156. case 's':
  157. if ((p = va_arg(*ap, char *)) == NULL)
  158. p = "(null)";
  159. if (width > 0 && padc != '-')
  160. for (width -= strnlen(p, precision); width > 0; width--)
  161. if ((*_fputch) (f, padc, putdat) == -1)
  162. return;
  163. for (; (ch = *p++) != '\0' && (precision < 0 || --precision >= 0); width--)
  164. if (altflag && (ch < ' ' || ch > '~')) {
  165. if ((*_fputch) (f, '?', putdat) == -1)
  166. return;
  167. } else {
  168. if ((*_fputch) (f, ch, putdat) == -1)
  169. return;
  170. }
  171. for (; width > 0; width--)
  172. if ((*_fputch) (f, ' ', putdat) == -1)
  173. return;
  174. break;
  175. // (signed) decimal
  176. case 'd':
  177. num = getint(ap, lflag);
  178. #if !defined(__i386__)
  179. if ((long long) num < 0) {
  180. if ((*_fputch) (f, '-', putdat) == -1)
  181. return;
  182. num = -(long long) num;
  183. }
  184. #else
  185. if ((long) num < 0) {
  186. if ((*_fputch) (f, '-', putdat) == -1)
  187. return;
  188. num = -(long) num;
  189. }
  190. #endif
  191. base = 10;
  192. goto number;
  193. // unsigned decimal
  194. case 'u':
  195. num = getuint(ap, lflag);
  196. base = 10;
  197. goto number;
  198. // (unsigned) octal
  199. case 'o':
  200. // Replace this with your code.
  201. num = getuint(ap, lflag);
  202. base = 8;
  203. goto number;
  204. // pointer
  205. case 'p':
  206. if ((*_fputch) (f, '0', putdat) == -1)
  207. return;
  208. if ((*_fputch) (f, 'x', putdat) == -1)
  209. return;
  210. #if !defined(__i386__)
  211. num = (unsigned long long)
  212. (uintptr_t) va_arg(*ap, void *);
  213. #else
  214. num = (unsigned long)
  215. (uintptr_t) va_arg(*ap, void *);
  216. #endif
  217. base = 16;
  218. goto number;
  219. // (unsigned) hexadecimal
  220. case 'x':
  221. num = getuint(ap, lflag);
  222. base = 16;
  223. number:
  224. if (printnum(_fputch, f, putdat, num, base, width, padc) == -1)
  225. return;
  226. break;
  227. // escape character
  228. case '^':
  229. if ((*_fputch) (f, 0x1b, putdat) == -1)
  230. return;
  231. break;
  232. // escaped '%' character
  233. case '%':
  234. (*_fputch) (f, ch, putdat);
  235. break;
  236. // unrecognized escape sequence - just print it literally
  237. default:
  238. (*_fputch) (f, '%', putdat);
  239. for (fmt--; fmt[-1] != '%'; fmt--)
  240. /* do nothing */;
  241. break;
  242. }
  243. }
  244. }
  245. void
  246. fprintfmt(int (*_fputch)(void *, int, void *), void * f, void * putdat,
  247. const char * fmt, ...)
  248. {
  249. va_list ap;
  250. va_start(ap, fmt);
  251. vfprintfmt(_fputch, f, putdat, fmt, &ap);
  252. va_end(ap);
  253. }
  254. struct sprintbuf {
  255. int cnt, max;
  256. char * buf;
  257. };
  258. static int
  259. sprintputch(void * f, int ch, struct sprintbuf * b)
  260. {
  261. if (b->cnt >= b->max)
  262. return -1;
  263. b->buf[b->cnt++] = ch;
  264. return 0;
  265. }
  266. static int
  267. vsprintf(char * buf, int n, const char * fmt, va_list *ap)
  268. {
  269. struct sprintbuf b = { 0, n, buf };
  270. if (!buf || n < 1)
  271. return 0;
  272. // print the string to the buffer
  273. vfprintfmt((void *) sprintputch, (void *) 0, &b, fmt, ap);
  274. // null terminate the buffer
  275. if (b.cnt < n)
  276. b.buf[b.cnt] = '\0';
  277. return b.cnt;
  278. }
  279. int
  280. snprintf(char * buf, int n, const char * fmt, ...)
  281. {
  282. va_list ap;
  283. int rc;
  284. va_start(ap, fmt);
  285. rc = vsprintf(buf, n, fmt, &ap);
  286. va_end(ap);
  287. return rc;
  288. }