aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-02-08 17:27:58 +0100
committerTristan Riehs <tristan.riehs@bordeaux-inp.fr>2024-02-08 17:27:58 +0100
commit9e84fe79e1d6b7c5093f2ea06be745d309952c45 (patch)
treec8af32cb6709ba578b70f0e26a66deec75b2d617
parentf3b3ba220e14b1b5fe39f14d7e0f04c4713e10b5 (diff)
-p option introduced
-rw-r--r--rpt.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/rpt.c b/rpt.c
index b35f492..27ae24c 100644
--- a/rpt.c
+++ b/rpt.c
@@ -43,6 +43,12 @@ FILE *stdin_clone = NULL;
/* File descriptor associated to the clone of stdin. */
int clone_fd;
+/* Number of times the command has been executed. */
+long exec_count = 0;
+
+/* Non-zero if exec_count has to be printed. */
+int do_print_exec_count = 0;
+
void
print_usage(FILE *output)
{
@@ -60,6 +66,7 @@ print_help()
printf(" -h\t\tPrint this message and exit.\n");
printf(" -v\t\tPrint the version number and exit.\n");
printf(" -u\t\tRun COMMAND until it fails.\n");
+ printf(" -p\t\tPrint how many times COMMAND has been executed.\n");
printf("Reapeat is licensed under the GPL3 license.\n");
}
@@ -69,6 +76,13 @@ print_version()
puts(VERSION);
}
+void
+print_exec_count(void)
+{
+ if (do_print_exec_count)
+ printf("Command executed %ld time(s).\n", exec_count);
+}
+
/* Exit whenever a subprocess fails. */
void
exit_on_error(int status)
@@ -82,6 +96,8 @@ exit_on_error(int status)
if (stdin_clone)
fclose(stdin_clone);
+ print_exec_count();
+
exit(status);
}
@@ -133,6 +149,7 @@ wait_child(void (*handle_error_f)(int status))
int wstatus;
wait(&wstatus);
+ exec_count++;
handle_error_f(WEXITSTATUS(wstatus));
}
@@ -213,7 +230,7 @@ main(int argc, char* argv[])
void (*handle_error_f)(int status) = exit_on_error;
- while ((opt = getopt(argc, argv, ":n:fuhV")) != -1)
+ while ((opt = getopt(argc, argv, ":n:fuphV")) != -1)
{
switch(opt)
{
@@ -226,6 +243,9 @@ main(int argc, char* argv[])
case 'u':
count = LONG_MAX;
break;
+ case 'p':
+ do_print_exec_count = 1;
+ break;
case 'h':
print_help();
exit(EXIT_SUCCESS);
@@ -279,5 +299,7 @@ main(int argc, char* argv[])
if (stdin_clone)
fclose(stdin_clone);
+ print_exec_count();
+
return EXIT_SUCCESS;
}