Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <fcall.h>
4 #include <9pclient.h>
6 /* C99 nonsense */
7 #ifdef va_copy
8 # define VA_COPY(a,b) va_copy(a,b)
9 # define VA_END(a) va_end(a)
10 #else
11 # define VA_COPY(a,b) (a) = (b)
12 # define VA_END(a)
13 #endif
15 static int
16 fidflush(Fmt *f)
17 {
18 int n;
20 n = (char*)f->to - (char*)f->start;
21 if(n && fswrite(f->farg, f->start, n) != n)
22 return 0;
23 f->to = f->start;
24 return 1;
25 }
27 static int
28 fsfmtfidinit(Fmt *f, CFid *fid, char *buf, int size)
29 {
30 f->runes = 0;
31 f->start = buf;
32 f->to = buf;
33 f->stop = buf + size;
34 f->flush = fidflush;
35 f->farg = fid;
36 f->nfmt = 0;
37 return 0;
38 }
40 int
41 fsprint(CFid *fd, char *fmt, ...)
42 {
43 int n;
44 va_list args;
46 va_start(args, fmt);
47 n = fsvprint(fd, fmt, args);
48 va_end(args);
49 return n;
50 }
52 int
53 fsvprint(CFid *fd, char *fmt, va_list args)
54 {
55 Fmt f;
56 char buf[256];
57 int n;
59 fsfmtfidinit(&f, fd, buf, sizeof(buf));
60 VA_COPY(f.args,args);
61 n = dofmt(&f, fmt);
62 VA_END(f.args);
63 if(n > 0 && fidflush(&f) == 0)
64 return -1;
65 return n;
66 }