summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-05 20:44:34 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-05 20:54:26 +0200
commit0e1d811b25f3719582f9e8619c15c03fc921d565 (patch)
tree97fe2a4cd1e2334d0dde597c5346f52aaa501833 /src
parent085da0ac632a60660260303bb7930e698afc0b95 (diff)
omg
Diffstat (limited to 'src')
-rw-r--r--src/omg.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/omg.c b/src/omg.c
new file mode 100644
index 0000000..b289c2c
--- /dev/null
+++ b/src/omg.c
@@ -0,0 +1,84 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "util/versie.h"
+
+static void usage(FILE *f) {
+ fprintf(f,
+ "Gebruik: omg [-hV] [NAAM=WAARDE]... [COMMANDO]...\n"
+ "\n"
+ "TODO \n"
+ "\n"
+ " -h Toon deze hulptekst\n"
+ " -V Toon versienummer\n");
+}
+
+// Returns pointer to argument array containing the variables to set and the
+// command to execute and its arguments
+static char** parse_options(int argc, char **argv) {
+ int opt;
+ while ((opt = getopt(argc, argv, "hV")) != -1) {
+ switch (opt) {
+ case 'h':
+ usage(stdout);
+ exit(0);
+
+ case 'V':
+ drukkedoos_print_versie(stdout);
+ exit(0);
+
+ case '?':
+ fprintf(stderr, "omg: Ongeldige optie: -%c\n", optopt);
+ usage(stderr);
+ exit(1);
+ }
+ }
+
+ return argv + optind;
+}
+
+
+int entry_omg(int argc, char **argv) {
+ char **args = parse_options(argc, argv);
+
+ while (*args != NULL) {
+ if (strchr(*args, '=') == NULL) {
+ break;
+ }
+
+ char *name = strtok(*args, "=");
+ char *value = strtok(NULL, "=");
+
+ if (value == NULL) {
+ fprintf(stderr, "omg: geen waarde gegeven voor sleutel '%s'\n", name);
+ return 1;
+ }
+
+ errno = 0;
+ if (setenv(name, value, true) == -1) {
+ fprintf(stderr, "omg: kon omgeving niet aanpassen: %s\n", strerror(errno));
+ }
+
+ args++;
+ }
+
+ if (*args == NULL) {
+ while (*environ != NULL) {
+ printf("%s\n", *environ);
+ environ++;
+ }
+
+ return 0;
+ }
+
+ errno = 0;
+ execvp(args[0], args);
+ fprintf(stderr, "omg: kon proces niet starten: %s\n", strerror(errno));
+
+ return 1;
+}