Advertisement
Mysakure

printf.c

Nov 19th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. // Implementation of cprintf console output for user environments,
  2. // based on printfmt() and the sys_cputs() system call.
  3. //
  4. // cprintf is a debugging statement, not a generic output statement.
  5. // It is very important that it always go to the console, especially when
  6. // debugging file descriptor code!
  7.  
  8. #include <inc/types.h>
  9. #include <inc/stdio.h>
  10. #include <inc/stdarg.h>
  11. #include <inc/lib.h>
  12.  
  13.  
  14. // Collect up to 256 characters into a buffer
  15. // and perform ONE system call to print all of them,
  16. // in order to make the lines output to the console atomic
  17. // and prevent interrupts from causing context switches
  18. // in the middle of a console output line and such.
  19. struct printbuf {
  20.     int idx;    // current buffer index
  21.     int cnt;    // total bytes printed so far
  22.     char buf[256];
  23. };
  24.  
  25.  
  26. static void
  27. putch(int ch, struct printbuf *b)
  28. {
  29.     b->buf[b->idx++] = ch;
  30.     if (b->idx == 256-1) {
  31.         sys_cputs(b->buf, b->idx);
  32.         b->idx = 0;
  33.     }
  34.     b->cnt++;
  35. }
  36.  
  37. int
  38. vcprintf(const char *fmt, va_list ap)
  39. {
  40.     struct printbuf b;
  41.  
  42.     b.idx = 0;
  43.     b.cnt = 0;
  44.     vprintfmt((void*)putch, &b, fmt, ap);
  45.     sys_cputs(b.buf, b.idx);
  46.  
  47.     return b.cnt;
  48. }
  49.  
  50. int
  51. cprintf(const char *fmt, ...)
  52. {
  53.     va_list ap;
  54.     int cnt;
  55.  
  56.     va_start(ap, fmt);
  57.     cnt = vcprintf(fmt, ap);
  58.     va_end(ap);
  59.  
  60.     return cnt;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement