aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rpt.c44
1 files changed, 43 insertions, 1 deletions
diff --git a/rpt.c b/rpt.c
index c515704..82e2c95 100644
--- a/rpt.c
+++ b/rpt.c
@@ -1,14 +1,21 @@
#define _GNU_SOURCE /* getopt_long */
#include <errno.h>
+#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#define VERSION "1.0.0"
-#define TMPFILE "/tmp/repeat-tmp"
+
+/* Size of the buffer used to clone stdin. */
+#define BUFSIZE 8000
+
+/* File descriptor associated to the clone of stdin. */
+int clone_fd;
void
print_usage(FILE *output)
@@ -103,6 +110,39 @@ repeat_cmd(char **argv, long count, void (*handle_error_f)(int status))
invoke_cmd(argv, handle_error_f);
}
+void
+copy_stdin()
+{
+ ssize_t nbytes;
+ char buf[BUFSIZE];
+ FILE *tmp_stream;
+
+ tmp_stream = tmpfile();
+
+ if (!tmp_stream)
+ {
+ perror("rpt");
+ exit(1);
+ }
+
+ clone_fd = fileno(tmp_stream);
+ errno = 0;
+
+ while ((nbytes = read(STDIN_FILENO, buf, BUFSIZE)) > 0)
+ {
+ if (write(clone_fd, buf, nbytes) < 0)
+ break;
+ }
+
+ fclose(tmp_stream);
+
+ if (errno)
+ {
+ perror("rpt");
+ exit(1);
+ }
+}
+
int
main(int argc, char* argv[])
{
@@ -167,7 +207,9 @@ main(int argc, char* argv[])
return 3;
}
+ copy_stdin();
repeat_cmd(argv + optind, count, handle_error_f);
+ close(clone_fd);
return 0;
}