aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@bordeaux-inp.fr>2023-09-27 19:46:48 +0200
committerTristan Riehs <tristan.riehs@bordeaux-inp.fr>2023-09-27 19:46:48 +0200
commita5f24bbc84d543d816e5fadee11bd8e7e6d5ef10 (patch)
tree9c85136e9a7e6d41d0d44ae8cb17c4158a40cc87
parent606facf2d89340653be66ca44a199e0a0d0482fb (diff)
option parsing + reading of COUNT
-rw-r--r--src/rpt.c63
1 files changed, 60 insertions, 3 deletions
diff --git a/src/rpt.c b/src/rpt.c
index f0a9472..aae0b73 100644
--- a/src/rpt.c
+++ b/src/rpt.c
@@ -2,9 +2,12 @@
#include <getopt.h>
#include <stdio.h>
+#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
+#define VERSION "1.0.0"
+
void
print_usage(FILE *output)
{
@@ -14,6 +17,38 @@ print_usage(FILE *output)
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 (!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;
+}
+
int
main(int argc, char* argv[])
{
@@ -29,9 +64,9 @@ main(int argc, char* argv[])
int opt;
char *strcount = NULL;
- long count;
+ long count = 1;
- void (*handle_exit_f)(int status);
+ void (*handle_exit_f)(int status) = exit_on_error;
while ((opt = getopt_long(argc, argv, "+:n:fVh", opts, NULL)) >= 0)
{
@@ -41,7 +76,14 @@ main(int argc, char* argv[])
strcount = optarg;
break;
case 'f':
-
+ handle_exit_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);
@@ -52,5 +94,20 @@ main(int argc, char* argv[])
}
}
+ 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;
+ }
+ }
+
+
+
return 0;
}