From 0e1d811b25f3719582f9e8619c15c03fc921d565 Mon Sep 17 00:00:00 2001 From: Lieuwe Rooijakkers Date: Mon, 5 Aug 2024 20:44:34 +0200 Subject: omg --- src/omg.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/omg.c 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 +#include +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3-70-g09d2