still serv_close bug...
[mit-jos.git] / lib / printf.c
blob59d90c6e0a16931d138196a610ca41d3a7ca411b
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!
8 #include <inc/types.h>
9 #include <inc/stdio.h>
10 #include <inc/stdarg.h>
11 #include <inc/lib.h>
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];
26 static void
27 putch(int ch, struct printbuf *b)
29 b->buf[b->idx++] = ch;
30 if (b->idx == 256-1) {
31 sys_cputs(b->buf, b->idx);
32 b->idx = 0;
34 b->cnt++;
37 int
38 vcprintf(const char *fmt, va_list ap)
40 struct printbuf b;
42 b.idx = 0;
43 b.cnt = 0;
44 vprintfmt((void*)putch, &b, fmt, ap);
45 sys_cputs(b.buf, b.idx);
47 return b.cnt;
50 int
51 cprintf(const char *fmt, ...)
53 va_list ap;
54 int cnt;
56 va_start(ap, fmt);
57 cnt = vcprintf(fmt, ap);
58 va_end(ap);
60 return cnt;