Make env more normal
This commit is contained in:
parent
719d27166d
commit
a5a186290a
33
apps/env.c
33
apps/env.c
|
@ -3,20 +3,47 @@
|
||||||
* of the NCSA / University of Illinois License - see LICENSE.md
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
||||||
* Copyright (C) 2013-2018 K. Lange
|
* Copyright (C) 2013-2018 K. Lange
|
||||||
*
|
*
|
||||||
* env - Print environment
|
* env - Print or set environment
|
||||||
*
|
|
||||||
* Prints all the environment values.
|
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
extern int _environ_size;
|
||||||
|
|
||||||
int main(int argc, char ** argv) {
|
int main(int argc, char ** argv) {
|
||||||
|
int start = 1;
|
||||||
|
|
||||||
|
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;
|
char ** env = environ;
|
||||||
|
|
||||||
while (*env) {
|
while (*env) {
|
||||||
printf("%s\n", *env);
|
printf("%s\n", *env);
|
||||||
env++;
|
env++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue