-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsetenv.c
51 lines (40 loc) · 835 Bytes
/
unsetenv.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
/* gcc -g -Wall -Werror -ansi -pedantic unsetenv.c -o unsetenv */
#include "hdr.h"
char extern **environ;
void printenv() {
char **ep;
for(ep = environ; *ep != NULL; ep++){
printf("%s\n", *ep);
}
}
int index2(char *s){
int ch;
for(ch = 0; s[ch] != '\0';ch++){
if(s[ch] == '='){
return ch;
}
}
return -1;
}
int main(int argc, char *argv[]) {
char **ep, *token, buf[100];
const char sep[2] = "=";
if(argc < 2) {
fprintf(stderr, "usage error\n");
return -1;
}
printenv();
for(ep = environ;*ep != NULL; ep++){
token = strtok(*ep, sep);
if(strcmp(token, argv[1]) != 0) {
continue;
}
memset(buf, '\0', sizeof(buf));
if(strcpy(buf, *ep) == NULL) {
fprintf(stderr, "strcpy failed, errno=%d\n", errno);
return -1;
}
printf("matched: %s, %s\n", buf, getenv(buf));
}
printenv();
}