-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_setenv.c
54 lines (48 loc) · 1.07 KB
/
shell_setenv.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
51
52
53
54
#include "shell_setenv.h"
#define ll long long
int run_setenv(char** args, char* root) {
ll count = 0, i = 0;
while (args[i] != NULL) {
count++;
i++;
}
if (count >= 4) {
fprintf(stderr, "Too many arguments\n");
}
else if (count <= 1) {
fprintf(stderr, "Too few arguments\n");
}
else if (count == 2) {
if (setenv(args[1], "", 1) == -1) {
perror("setenv error");
return -1;
}
}
else {
if (setenv(args[1], args[2], 1) == -1) {
perror("setenv error");
return -1;
}
}
return 1;
}
int run_unsetenv(char** args, char* root) {
ll count = 0, i = 0;
while (args[i] != NULL) {
count++;
i++;
}
if (count >= 3) {
fprintf(stderr, "Too many arguments\n");
}
else if (count <= 1) {
fprintf(stderr, "Too few arguments\n");
}
else if (count == 2) {
if (unsetenv(args[1]) == -1) {
perror("unsetenv error");
return -1;
}
}
return 1;
}