-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv.c
50 lines (42 loc) · 1.06 KB
/
env.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "shell.h"
#include "utils.h"
#include "env.h"
void handle_setenv(int argc, char** argv) {
if (argc > 0 && strcmp(argv[argc-1], "&") == 0) {
argc--;
}
if (argc < 1 || argc > 2) {
fprintf(stderr, "Could not set environment variable: Invalid arguments.\n");
return;
}
if (argc == 1) {
set_env(argv[0], "");
} else {
set_env(argv[0], argv[1]);
}
}
bool set_env(char* name, char* val) {
if (setenv(name, val, 1) < 0) {
// setenv copied those strings
perror("Could not set environment variable");
return false;
}
return true;
}
void handle_unsetenv(int argc, char** argv) {
if (argc > 0 && strcmp(argv[argc-1], "&") == 0) {
argc--;
}
if (argc != 1) {
fprintf(stderr, "Could not unset environment variable: Invalid arguments.\n");
return;
}
unset_env(argv[0]);
}
bool unset_env(char* name) {
if (unsetenv(name) < 0) {
perror("Could not unset environment variable");
return false;
}
return true;
}