aboutsummaryrefslogtreecommitdiff
path: root/src/rpt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rpt.c')
-rw-r--r--src/rpt.c49
1 files changed, 43 insertions, 6 deletions
diff --git a/src/rpt.c b/src/rpt.c
index aae0b73..1a9a256 100644
--- a/src/rpt.c
+++ b/src/rpt.c
@@ -35,7 +35,7 @@ print_version()
void
exit_on_error(int status)
{
- if (!status)
+ if (WIFEXITED(status))
return;
fprintf(stderr, "Process exited with status %d, aborting.\n", status);
@@ -49,6 +49,40 @@ continue_on_error(int status)
(void) status;
}
+/* Invoke the executable *ARGV COUNT times, passing it the rest of ARG as
+ arguments. Call HANDLE_ERROR_F between each process. */
+void
+repeat_cmd(char **argv, long count, void (*handle_error_f)(int))
+{
+ pid_t pid;
+ int status;
+
+ for(long i = 0; i < count; ++i)
+ {
+ pid = fork();
+
+ if (pid == 0)
+ {
+ /* child */
+ status = execvp(argv[0], argv);
+
+ if (status < 0)
+ exit(1);
+
+ exit(2);
+ }
+ else
+ {
+ /* parent */
+ int exit_status = 0;
+
+ waitpid(pid, &exit_status, 0);
+
+ handle_error_f(exit_status);
+ }
+ }
+}
+
int
main(int argc, char* argv[])
{
@@ -66,7 +100,7 @@ main(int argc, char* argv[])
char *strcount = NULL;
long count = 1;
- void (*handle_exit_f)(int status) = exit_on_error;
+ void (*handle_error_f)(int status) = exit_on_error;
while ((opt = getopt_long(argc, argv, "+:n:fVh", opts, NULL)) >= 0)
{
@@ -76,7 +110,7 @@ main(int argc, char* argv[])
strcount = optarg;
break;
case 'f':
- handle_exit_f = continue_on_error;
+ handle_error_f = continue_on_error;
break;
case 'h':
print_help();
@@ -87,10 +121,10 @@ main(int argc, char* argv[])
case ':':
fprintf(stderr, "COUNT value missing.\n");
print_usage(stderr);
- return 1;
+ return 1; /* invalid option */
default:
print_usage(stderr);
- return 1;
+ return 1; /* invalid option */
}
}
@@ -103,11 +137,14 @@ main(int argc, char* argv[])
{
fprintf(stderr, "Failed to read COUNT : '%s'.\n",
strcount);
- return 2;
+ return 2; /* error while reading COUNT */
}
}
+ if (optind >= argc)
+ return 3; /* COMMAND not provided */
+ repeat_cmd(argv + optind, count, handle_error_f);
return 0;
}