strscan: make it work properly
[tools.git] / tools / writeonlynet.c
1 #include <jh.h>
2 #include <unistd.h>
3
4 int main(int argc, char **argv) {
5   if (argc != 3) xperror("invocation: <host> <port>", 0);
6   int s = netopen(argv[1], argv[2], JH_TCP_HINTS);
7   if (s < 0) {
8     fprintf(stderr, "unable to connect: %s\n", gai_strerror(s));
9     exit(1);
10   }
11   while (1) {
12     char buf[4096];
13     fprintf(stderr, "reading...\n");
14     ssize_t count = fail_on_neg(read(0, buf, sizeof(buf)), "read failed", 1);
15     if (count == 0) return 0;
16     fprintf(stderr, "read %zd chars, writing...\n", count);
17     ssize_t wcount = fail_on_neg(write(s, buf, count), "write failed", 1);
18     if (wcount == 0) {
19       fprintf(stderr, "zero write! ending.\n");
20       return 1;
21     }
22     if (wcount != count) {
23       fprintf(stderr, "wrong write size! ending.\n");
24       return 1;
25     }
26   }
27 }