--- #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/select.h> int main(int argc, char **argv) { int i, j, n, w, eof; fd_set rs; char buf[BUFSIZ]; for (i = 1; i < argc; i++) { // close any "accidently" open file descriptors close((i+2)+(argc-1)); if ((n = open(argv[i], O_RDONLY)) == -1) { fprintf(stderr, "pselect: %s\n", strerror(errno)); return 1; } } for (eof = 0; !eof;) { FD_ZERO(&rs); for (i = 3; i < argc+2; i++) { FD_SET(i+(argc-1), &rs); } if ((n = select(i+argc, &rs, NULL, NULL, NULL)) == -1) { fprintf(stderr, "pselect: %s\n", strerror(errno)); return 1; } for (i = 3; i < argc+2; i++) { if (!FD_ISSET(i+(argc-1), &rs)) { continue; } if ((n = read(i+(argc-1), &buf[0], sizeof(buf))) == -1) { fprintf(stderr, "failed reading from fd %d: %s\n", i+(argc-1), strerror(errno)); return 1; } else if (n == 0) { eof = 1; } for (j = 0; j < n; j += w) { if ((w = write(i, &buf[j], n-j)) == -1 || w == 0) { fprintf(stderr, "failed: %s\n", strerror(errno)); return -1; } } } } }Received on Mon Nov 04 2024 - 09:06:17 CET
This archive was generated by hypermail 2.3.0 : Mon Nov 04 2024 - 09:12:10 CET