Blob
1 #include <u.h>2 #include <libc.h>4 void5 usage(void)6 {7 fprint(2, "usage: dial [-e] addr\n");8 exits("usage");9 }11 void12 killer(void *x, char *msg)13 {14 USED(x);15 if(strcmp(msg, "kill") == 0)16 exits(0);17 noted(NDFLT);18 }20 void21 main(int argc, char **argv)22 {23 int fd, pid;24 char buf[8192];25 int n, waitforeof;27 notify(killer);28 waitforeof = 0;29 ARGBEGIN{30 case 'e':31 waitforeof = 1;32 break;33 default:34 usage();35 }ARGEND37 if(argc != 1)38 usage();40 if((fd = dial(argv[0], nil, nil, nil)) < 0)41 sysfatal("dial: %r");43 switch(pid = fork()){44 case -1:45 sysfatal("fork: %r");46 case 0:47 while((n = read(0, buf, sizeof buf)) > 0)48 if(write(fd, buf, n) < 0)49 break;50 if(!waitforeof)51 postnote(PNPROC, getppid(), "kill");52 exits(nil);53 }55 while((n = read(fd, buf, sizeof buf)) > 0)56 if(write(1, buf, n) < 0)57 break;59 postnote(PNPROC, pid, "kill");60 waitpid();61 exits(0);62 }