/* log file cmd args... */ #include #include #include #include #include int main(int argc, char* argv[]) { int p[2]; pipe(p); char c; if(fork()) { /* père */ int fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644); if(fd < 0) exit(EXIT_FAILURE); close(p[1]); /* ici, sinon read() bloquera à la fin ! */ while(read(p[0], &c, 1) > 0) { write(1, &c, 1); write(fd, &c, 1); } close(fd); close(p[0]); } else { /* fils */ close(p[0]); dup2(p[1], 1); close(p[1]); execvp(argv[2], argv+2); exit(EXIT_FAILURE); } return EXIT_SUCCESS; }