From 75c555bada280559253975349c192f956ab35674 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Fri, 20 Oct 2023 08:11:34 +0200 Subject: piping supported --- rpt.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'rpt.c') diff --git a/rpt.c b/rpt.c index cf466c0..6650f1e 100644 --- a/rpt.c +++ b/rpt.c @@ -14,8 +14,12 @@ /* Size of the buffer used to clone stdin. */ #define BUFSIZE 8000 +/* Clone of stdin. Used to perform lseek(2) calls between each child + process. */ +static FILE *stdin_clone; + /* File descriptor associated to the clone of stdin. */ -int clone_fd; +static int clone_fd; void print_usage(FILE *output) @@ -63,7 +67,7 @@ exec_child(char **argv) { int status; - status = lseek(STDIN_FILENO, 0, L_SET); + status = lseek(clone_fd, 0, L_SET); if (status < 0) { @@ -71,7 +75,7 @@ exec_child(char **argv) exit(4); } - status = dup2(dup(clone_fd), STDIN_FILENO); + status = dup2(clone_fd, STDIN_FILENO); if (status < 0) { @@ -131,17 +135,16 @@ copy_stdin() { ssize_t nbytes; char buf[BUFSIZE]; - FILE *tmp_stream; - tmp_stream = tmpfile(); + stdin_clone = tmpfile(); /* closed at the end of the main */ - if (!tmp_stream) + if (!stdin_clone) { perror("rpt"); exit(1); } - clone_fd = fileno(tmp_stream); + clone_fd = fileno(stdin_clone); errno = 0; while ((nbytes = read(STDIN_FILENO, buf, BUFSIZE)) > 0) @@ -150,10 +153,9 @@ copy_stdin() break; } - fclose(tmp_stream); - if (errno) { + fclose(stdin_clone); perror("rpt"); exit(1); } @@ -226,6 +228,6 @@ main(int argc, char* argv[]) copy_stdin(); repeat_cmd(argv + optind, count, handle_error_f); - close(clone_fd); + fclose(stdin_clone); return 0; } -- cgit v1.2.3