From a5f24bbc84d543d816e5fadee11bd8e7e6d5ef10 Mon Sep 17 00:00:00 2001
From: Tristan Riehs <tristan.riehs@bordeaux-inp.fr>
Date: Wed, 27 Sep 2023 19:46:48 +0200
Subject: option parsing + reading of COUNT

---
 src/rpt.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 60 insertions(+), 3 deletions(-)

(limited to 'src')

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;
 }
-- 
cgit v1.2.3