bcat.c (748B) download
1#include "bio.h"
2
3#include <fmt.h>
4
5Biobuf bout;
6
7void bcat(Biobuf* b, char* name) {
8 char buf[1000];
9 int n;
10
11 while ((n = Bread(b, buf, sizeof buf)) > 0) {
12 if (Bwrite(&bout, buf, n) < 0)
13 fprint(2, "writing during %s: %r\n", name);
14 }
15 if (n < 0)
16 fprint(2, "reading %s: %r\n", name);
17}
18
19int main(int argc, char** argv) {
20 int i;
21 Biobuf b, *bp;
22 Fmt fmt;
23
24 Binit(&bout, 1, O_WRONLY);
25 Bfmtinit(&fmt, &bout);
26 fmtprint(&fmt, "hello, world\n");
27 Bfmtflush(&fmt);
28
29 if (argc == 1) {
30 Binit(&b, 0, O_RDONLY);
31 bcat(&b, "<stdin>");
32 } else {
33 for (i = 1; i < argc; i++) {
34 if ((bp = Bopen(argv[i], O_RDONLY)) == 0) {
35 fprint(2, "Bopen %s: %r\n", argv[i]);
36 continue;
37 }
38 bcat(bp, argv[i]);
39 Bterm(bp);
40 }
41 }
42 exit(0);
43}