From a6e6ecc31e7a6d54573122f611c9ded18f2b41d6 Mon Sep 17 00:00:00 2001 From: Nathan Wagner Date: Sat, 25 Apr 2015 20:49:56 +0000 Subject: [PATCH] implemented env --- utils/env/env.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 utils/env/env.c diff --git a/utils/env/env.c b/utils/env/env.c new file mode 100644 index 0000000..f3ec296 --- /dev/null +++ b/utils/env/env.c @@ -0,0 +1,61 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include + +/* Public domain by Nathan Wagner */ + +extern char **environ; + +static void printenv() { + char **env; + for (env = environ; *env; env++) { + printf("%s\n", *env); + } +} + +static void clearenv() { + char var[1024]; /* TODO how long can the name be ? */ + char *s; + int i; + + while (*environ) { + i = 0; + s = *environ; + while (*s && *s != '=') { + var[i++] = *s++; + } + var[i] = 0; + unsetenv(var); + } +} + +int main(int ac, char *av[]) { + int arg; + arg = 1; + if (arg < ac && !strcmp(av[arg], "-i")) { + clearenv(); + arg++; + } + while (arg < ac) { + char *eq; + eq = strchr(av[arg], '='); + if (eq) { + *eq = 0; + setenv(av[arg], eq+1, 1); + *eq = '='; + } else { + break; + } + arg++; + } + + if (arg >= ac) { + printenv(); + return 0; + } + + execvp(av[arg], &av[arg]); + return 0; +} -- 2.40.0