diff options
-rw-r--r-- | rpt.c | 24 |
1 files changed, 23 insertions, 1 deletions
@@ -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; } |