diff --git a/apps/env.c b/apps/env.c index 9f02e444..bc3c4d0a 100644 --- a/apps/env.c +++ b/apps/env.c @@ -3,19 +3,46 @@ * of the NCSA / University of Illinois License - see LICENSE.md * Copyright (C) 2013-2018 K. Lange * - * env - Print environment - * - * Prints all the environment values. + * env - Print or set environment */ #include +#include #include +#include +#include + +extern int _environ_size; int main(int argc, char ** argv) { - char ** env = environ; + int start = 1; - while (*env) { - printf("%s\n", *env); - env++; + if (start < argc && !strcmp(argv[start],"-i")) { + for (int i = 0; i < _environ_size; ++i) { + environ[i] = NULL; + } + start++; + } + + for (; start < argc; ++start) { + if (!strchr(argv[start],'=')) { + break; + } else { + putenv(argv[start]); + } + } + + if (start < argc) { + /* Execute command */ + if (execvp(argv[start], &argv[start])) { + fprintf(stderr, "%s: %s: %s\n", argv[0], argv[start], strerror(errno)); + } + } else { + char ** env = environ; + + while (*env) { + printf("%s\n", *env); + env++; + } } return 0;