3 // Collect up to 256 characters into a buffer
4 // and perform ONE system call to print all of them,
5 // in order to make the lines output to the console atomic
6 // and prevent interrupts from causing context switches
7 // in the middle of a console output line and such.
9 int fd
; // file descriptor
10 int idx
; // current buffer index
11 ssize_t result
; // accumulated results from write
12 int error
; // first error that occurred
18 writebuf(struct printbuf
*b
)
21 ssize_t result
= write(b
->fd
, b
->buf
, b
->idx
);
24 if (result
!= b
->idx
) // error, or wrote less than supplied
25 b
->error
= (result
< 0 ? result
: 0);
30 putch(int ch
, void *thunk
)
32 struct printbuf
*b
= (struct printbuf
*) thunk
;
33 b
->buf
[b
->idx
++] = ch
;
41 vfprintf(int fd
, const char *fmt
, va_list ap
)
49 vprintfmt(putch
, &b
, fmt
, ap
);
53 return (b
.result
? b
.result
: b
.error
);
57 fprintf(int fd
, const char *fmt
, ...)
63 cnt
= vfprintf(fd
, fmt
, ap
);
70 printf(const char *fmt
, ...)
76 cnt
= vfprintf(1, fmt
, ap
);