#define __USE_XOPEN #include #include #include #include #include #include #include #include #include #include #include #include #include #include int recint = 0; int recquit; void sendint() { recint = 1; } void quit() { recquit = 1; } extern char *ptsname (int __fd); #define setnonblock(fd, val) \ do { \ int on = val; \ if (ioctl(fd, FIONBIO, &on) == -1) { \ perror("ioctl()"); \ exit(1); \ } \ } while(0); int main(int argc, char *argv[]) { int pfd; int tfd = 0; fd_set set; struct sockaddr_un addr; int max; struct termios saveterm; if (argc < 3 || argc > 3) { fprintf(stderr, "%s pipe tty\n", argv[0]); exit(1); } printf("Opening %s ... ", argv[1]); pfd = socket(AF_UNIX,SOCK_STREAM,0); addr.sun_family=AF_UNIX; strcpy(addr.sun_path, argv[1]); if (connect(pfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) == -1) { perror("connect()"); exit(1); } printf("OK\n"); fflush(stdout); printf("Opening %s ... ", argv[2]); tfd = open(argv[2], O_RDWR); if (tfd == -1) { perror("open()"); exit(1); } if (ptsname(tfd)) { printf("%s\n", ptsname(tfd)); } else { printf("OK\n"); } fflush(stdout); setnonblock(tfd, 1); setnonblock(pfd, 1); max = pfd; if (tfd > max) { max = tfd; } tcgetattr(tfd, &saveterm); cfmakeraw(&saveterm); while (1) { FD_ZERO(&set); FD_SET(pfd, &set); FD_SET(tfd, &set); #ifdef DEBUG printf("Selecting ... "); fflush(stdout); #endif if ((select(max + 1, &set, NULL, NULL, NULL) == -1) && (errno != EAGAIN) && (errno != EINTR)) { perror("select()"); exit(1); } #ifdef DEBUG printf("Selecting ... "); printf("OK\n"); fflush(stdout); #endif if (FD_ISSET(tfd, &set)) { char buf[1024]; int n; errno = 0; n = read(tfd, buf, sizeof(buf)); if (n == -1 && errno != EAGAIN) { fprintf(stderr, "read(tfd): %s [%d]\n", strerror(errno), errno); exit(1); } if (n != -1) { #ifdef DEBUG printf("Writing %d bytes from %s to %s ... ", n, argv[2], argv[1]); fflush(stdout); #endif setnonblock(pfd, 0); if (write(pfd, buf, n) != n) { perror("write()"); exit(0); } setnonblock(pfd, 1); #ifdef DEBUG printf("OK\n"); fflush(stdout); #endif } n = read(tfd, buf, sizeof(buf)); if (n == -1 && errno != EAGAIN) { fprintf(stderr, "read(tfd): %s [%d]\n", strerror(errno), errno); exit(1); } } if (FD_ISSET(pfd, &set)) { char buf[1024]; int n, ret; errno = 0; n = read(pfd, buf, sizeof(buf)); if (n == -1 && errno != EAGAIN) { fprintf(stderr, "read(pipe): %s [%d]\n", strerror(errno), errno); } if (n != -1) { #ifdef DEBUG printf("Writing %d bytes from %s to %s ... ", n, argv[1], argv[2]); fflush(stdout); #endif setnonblock(tfd, 0); if ((ret = write(tfd, buf, n)) != n) { fprintf(stderr, "write(tfd): %d != %d, %d\n", n, ret, errno); fflush(stderr); exit(1); } setnonblock(tfd, 1); #ifdef DEBUG printf("OK\n"); fflush(stdout); #endif } } } done: exit(0); }