From b975b7392606050c1ed83a8cce016f872589ad38 Mon Sep 17 00:00:00 2001 From: Tristan Riehs Date: Thu, 28 Sep 2023 08:08:38 +0200 Subject: source moved --- src/rpt.c | 150 -------------------------------------------------------------- 1 file changed, 150 deletions(-) delete mode 100644 src/rpt.c (limited to 'src') diff --git a/src/rpt.c b/src/rpt.c deleted file mode 100644 index 1a9a256..0000000 --- a/src/rpt.c +++ /dev/null @@ -1,150 +0,0 @@ -#define _GNU_SOURCE /* getopt_long */ - -#include -#include -#include -#include -#include - -#define VERSION "1.0.0" - -void -print_usage(FILE *output) -{ - fprintf(output, "Usage:\n"); - fprintf(output, "rpt [-n | --count COUNT] [-f | --force] COMMAND\n"); - fprintf(output, "rpt [-h | --help]\n"); - fprintf(output, "rpt [-V | --version]\n"); -} - -void -print_help() -{ - puts("Repeat : repeat a shell command"); - puts(""); - print_usage(stdout); -} - -void -print_version() -{ - puts(VERSION); -} - -/* Exit whenever a subprocess fails. */ -void -exit_on_error(int status) -{ - if (WIFEXITED(status)) - return; - - fprintf(stderr, "Process exited with status %d, aborting.\n", status); - exit(1); -} - -/* Ignore subprocesses' errors. */ -void -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[]) -{ - struct option const opts[] = { - {"count", required_argument, NULL, 'n'}, - {"force", no_argument, NULL, 'f'}, - {"version", no_argument, NULL, 'V'}, - {"help", no_argument, NULL, 'h'}, - {NULL, 0, NULL, 0} - }; - - opterr = 0; /* dismiss getopt error message */ - int opt; - - char *strcount = NULL; - long count = 1; - - void (*handle_error_f)(int status) = exit_on_error; - - while ((opt = getopt_long(argc, argv, "+:n:fVh", opts, NULL)) >= 0) - { - switch(opt) - { - case 'n': - strcount = optarg; - break; - case 'f': - handle_error_f = continue_on_error; - break; - case 'h': - print_help(); - return 0; - case 'V': - print_version(); - return 0; - case ':': - fprintf(stderr, "COUNT value missing.\n"); - print_usage(stderr); - return 1; /* invalid option */ - default: - print_usage(stderr); - return 1; /* invalid option */ - } - } - - if (strcount) - { - char *err = NULL; - count = strtol(strcount, &err, 10); - - if (err && (*err != '\0')) - { - fprintf(stderr, "Failed to read COUNT : '%s'.\n", - strcount); - return 2; /* error while reading COUNT */ - } - } - - if (optind >= argc) - return 3; /* COMMAND not provided */ - - repeat_cmd(argv + optind, count, handle_error_f); - - return 0; -} -- cgit v1.2.3