tdial.c - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tdial.c (879B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 
            4 void
            5 usage(void)
            6 {
            7         fprint(2, "usage: dial [-e] addr\n");
            8         exits("usage");
            9 }
           10 
           11 void
           12 killer(void *x, char *msg)
           13 {
           14         USED(x);
           15         if(strcmp(msg, "kill") == 0)
           16                 exits(0);
           17         noted(NDFLT);
           18 }
           19 
           20 void
           21 main(int argc, char **argv)
           22 {
           23         int fd, pid;
           24         char buf[8192];
           25         int n, waitforeof;
           26 
           27         notify(killer);
           28         waitforeof = 0;
           29         ARGBEGIN{
           30         case 'e':
           31                 waitforeof = 1;
           32                 break;
           33         default:
           34                 usage();
           35         }ARGEND
           36 
           37         if(argc != 1)
           38                 usage();
           39 
           40         if((fd = dial(argv[0], nil, nil, nil)) < 0)
           41                 sysfatal("dial: %r");
           42 
           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         }
           54 
           55         while((n = read(fd, buf, sizeof buf)) > 0)
           56                 if(write(1, buf, n) < 0)
           57                         break;
           58 
           59         postnote(PNPROC, pid, "kill");
           60         waitpid();
           61         exits(0);
           62 }