xref: /plan9/sys/src/cmd/unix/drawterm/libc/vfprint.c (revision 8ccd4a6360d974db7bd7bbd4f37e7018419ea908)
1 #include <u.h>
2 #include <libc.h>
3 #include "fmtdef.h"
4 
5 /*
6  * generic routine for flushing a formatting buffer
7  * to a file descriptor
8  */
9 int
10 _fmtFdFlush(Fmt *f)
11 {
12 	int n;
13 
14 	n = (char*)f->to - (char*)f->start;
15 	if(n && write((int)f->farg, f->start, n) != n)
16 		return 0;
17 	f->to = f->start;
18 	return 1;
19 }
20 
21 int
22 vfprint(int fd, char *fmt, va_list args)
23 {
24 	Fmt f;
25 	char buf[256];
26 	int n;
27 
28 	fmtfdinit(&f, fd, buf, sizeof(buf));
29 	f.args = args;
30 	n = dofmt(&f, fmt);
31 	if(n > 0 && _fmtFdFlush(&f) == 0)
32 		return -1;
33 	return n;
34 }
35