1 /*
2 * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10
11 int gTestNr;
12
13
14 void
set_env()15 set_env()
16 {
17 gTestNr++;
18 printf("Test %d...\n", gTestNr);
19
20 if (setenv("TEST_VARIABLE", "42", true) != 0)
21 fprintf(stderr, "Test %d: setting variable failed!\n", gTestNr);
22 }
23
24
25 void
test_env()26 test_env()
27 {
28 if (getenv("TEST_VARIABLE") != NULL)
29 fprintf(stderr, "Test %d: not cleared!\n", gTestNr);
30 if (setenv("OTHER_VARIABLE", "test", true) != 0)
31 fprintf(stderr, "Test %d: setting other failed!\n", gTestNr);
32 }
33
34
35 int
main(int argc,char ** argv)36 main(int argc, char** argv)
37 {
38 set_env();
39 environ = NULL;
40 test_env();
41
42 set_env();
43 environ[0] = NULL;
44 test_env();
45
46 static char* emptyEnv[1] = {NULL};
47 set_env();
48 environ = emptyEnv;
49 test_env();
50
51 set_env();
52 environ = (char**)calloc(1, sizeof(*environ));
53 test_env();
54
55 // clearenv() is not part of the POSIX specs
56 #if 1
57 set_env();
58 clearenv();
59 test_env();
60 #endif
61
62 return 0;
63 }
64
65