aboutsummaryrefslogtreecommitdiff
path: root/src/lib9pclient/print.c
blob: d02afb40375c0ffe561a1c7a4894c662f68cb096 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <9pclient.h>

/* C99 nonsense */
#ifdef va_copy
#	define VA_COPY(a,b) va_copy(a,b)
#	define VA_END(a) va_end(a)
#else
#	define VA_COPY(a,b) (a) = (b)
#	define VA_END(a)
#endif

static int
fidflush(Fmt *f)
{
	int n;

	n = (char*)f->to - (char*)f->start;
	if(n && fswrite(f->farg, f->start, n) != n)
		return 0;
	f->to = f->start;
	return 1;
}

static int
fsfmtfidinit(Fmt *f, CFid *fid, char *buf, int size)
{
	f->runes = 0;
	f->start = buf;
	f->to = buf;
	f->stop = buf + size;
	f->flush = fidflush;
	f->farg = fid;
	f->nfmt = 0;
	return 0;
}

int
fsprint(CFid *fd, char *fmt, ...)
{
	int n;
	va_list args;

	va_start(args, fmt);
	n = fsvprint(fd, fmt, args);
	va_end(args);
	return n;
}

int
fsvprint(CFid *fd, char *fmt, va_list args)
{
	Fmt f;
	char buf[256];
	int n;

	fsfmtfidinit(&f, fd, buf, sizeof(buf));
	VA_COPY(f.args,args);
	n = dofmt(&f, fmt);
	VA_END(f.args);
	if(n > 0 && fidflush(&f) == 0)
		return -1;
	return n;
}