forked from wenjun1055/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environ.c
executable file
·47 lines (43 loc) · 1.12 KB
/
environ.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *var, *value;
if (argc == 1 || argc > 3) {
fprintf(stderr, "usage: environ var [value]\n");
exit(1);
}
var = argv[1];
value = getenv(var);
if (value) {
printf("Variable %s has value %s\n", var, value);
} else {
printf("Variable %s has no value\n", var);
}
if (argc == 3) {
char *string;
value = argv[2];
string = malloc(strlen(var) + strlen(value) + 2);
if (!string) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
strcpy(string, var);
strcat(string, "=");
strcat(string, value);
printf("Calling putenv with: %s\n", string);
if (putenv(string) != 0) {
fprintf(stderr, "putenv failed\n");
free(string);
exit(1);
}
value = getenv(var);
if (value) {
printf("New value of %s is %s\n", var, value);
} else {
printf("New value of %s is null??\n", var);
}
}
exit(0);
}