toaruos/apps/env.c

51 lines
940 B
C
Raw Normal View History

/**
* @brief env - Print or set environment
*
* @copyright
2018-08-15 04:07:33 +03:00
* This file is part of ToaruOS and is released under the terms
2018-02-26 07:21:56 +03:00
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2018 K. Lange
2018-02-26 07:21:56 +03:00
*/
#include <stdio.h>
2018-10-02 15:52:51 +03:00
#include <stdlib.h>
2018-08-15 04:07:33 +03:00
#include <unistd.h>
2018-10-02 15:52:51 +03:00
#include <string.h>
#include <errno.h>
extern int _environ_size;
2018-02-26 07:21:56 +03:00
int main(int argc, char ** argv) {
2018-10-02 15:52:51 +03:00
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;
2018-08-15 04:07:33 +03:00
2018-10-02 15:52:51 +03:00
while (*env) {
printf("%s\n", *env);
env++;
}
2018-02-26 07:21:56 +03:00
}
2018-08-15 04:07:33 +03:00
return 0;
2018-02-26 07:21:56 +03:00
}