diff options
| author | rsc <devnull@localhost> | 2005-08-31 02:18:29 +0000 |
|---|---|---|
| committer | rsc <devnull@localhost> | 2005-08-31 02:18:29 +0000 |
| commit | 262ecfed9f7e39811f34517d82e848b8ec20f863 (patch) | |
| tree | b9947441be3e7b6a3d7c5e944854591e1b027bfb /src/cmd/lp/ipcopen.c | |
| parent | 2863ba101f0c9fec34756948e263cd534a3634ee (diff) | |
| download | plan9port-262ecfed9f7e39811f34517d82e848b8ec20f863.tar.gz plan9port-262ecfed9f7e39811f34517d82e848b8ec20f863.zip | |
Initial lp.
Diffstat (limited to 'src/cmd/lp/ipcopen.c')
| -rw-r--r-- | src/cmd/lp/ipcopen.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/cmd/lp/ipcopen.c b/src/cmd/lp/ipcopen.c new file mode 100644 index 00000000..666f64c4 --- /dev/null +++ b/src/cmd/lp/ipcopen.c @@ -0,0 +1,92 @@ +#include <u.h> +#include <libc.h> + +int ppid; + +/* + * predefined + */ +void pass(int from, int to); + + +/* + * Connect to given datakit port + */ +main(int argc, char *argv[]) +{ + int fd0, fd1; + int cpid; + char c; + char *cp, *devdir, *buf; + + if (argc != 4) { + fprint(2, "usage: %s destination network service\n", argv[0]); + exits("incorrect number of arguments"); + } + if(!(cp = malloc((long)(strlen(argv[1])+strlen(argv[2])+strlen(argv[3])+8)))) { + perror("malloc"); + exits("malloc failed"); + } + sprint(cp, "%s!%s!%s", argv[2], argv[1], argv[3]); + if (dial(cp, &devdir, 0) < 0) { + fprint(2, "dialing %s\n", cp); + perror("dial"); + exits("can't dial"); + } + + /* + * Initialize the input fd, and copy bytes. + */ + + if(!(buf = malloc((long)(strlen(devdir)+6)))) { + perror("malloc"); + exits("malloc failed"); + } + sprint(buf, "%s/data", devdir); + fd0=open(buf, OREAD); + fd1=open(buf, OWRITE); + if(fd0<0 || fd1<0) { + print("can't open", buf); + exits("can't open port"); + } + ppid = getpid(); + switch(cpid = fork()){ + case -1: + perror("fork failed"); + exits("fork failed"); + case 0: + close(0); + close(fd1); + pass(fd0, 1); /* from remote */ + hangup(fd0); + close(1); + close(fd0); + exits(""); + default: + close(1); + close(fd0); + pass(0, fd1); /* to remote */ + hangup(fd1); + close(0); + close(fd1); + exits(""); + } +} + +void +pass(int from, int to) +{ + char buf[1024]; + int ppid, cpid; + int n, tot = 0; + + while ((n=read(from, buf, sizeof(buf))) > 0) { + if (n==1 && tot==0 && *buf=='\0') + break; + tot += n; + if (write(to, buf, n)!=n) { + perror("pass write error"); + exits("pass write error"); + } + } +} |
