diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/omg.c | 84 | 
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; +}  | 
