]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/tst-environ.c
Update.
[thirdparty/glibc.git] / stdlib / tst-environ.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4
5 #define VAR "FOOBAR"
6
7 char putenv_val[100] = VAR "=some longer value";
8
9 int
10 main (void)
11 {
12 int result = 0;
13 const char *valp;
14
15 /* First test: remove entry FOOBAR, whether it exists or not. */
16 unsetenv (VAR);
17
18 /* Now getting the value should fail. */
19 if (getenv (VAR) != NULL)
20 {
21 printf ("There should be no `%s' value\n", VAR);
22 result = 1;
23 }
24
25 /* Now add a value, with the replace flag cleared. */
26 if (setenv (VAR, "one", 0) != 0)
27 {
28 printf ("setenv #1 failed: %m\n");
29 result = 1;
30 }
31
32 /* Getting this value should now be possible. */
33 valp = getenv (VAR);
34 if (valp == NULL || strcmp (valp, "one") != 0)
35 {
36 puts ("getenv #2 failed");
37 result = 1;
38 }
39
40 /* Try to replace without the replace flag set. This should fail. */
41 if (setenv (VAR, "two", 0) != 0)
42 {
43 printf ("setenv #2 failed: %m\n");
44 result = 1;
45 }
46
47 /* The value shouldn't have changed. */
48 valp = getenv (VAR);
49 if (valp == NULL || strcmp (valp, "one") != 0)
50 {
51 puts ("getenv #3 failed");
52 result = 1;
53 }
54
55 /* Now replace the value using putenv. */
56 if (putenv (putenv_val) != 0)
57 {
58 printf ("putenv #1 failed: %m\n");
59 result = 1;
60 }
61
62 /* The value should have changed now. */
63 valp = getenv (VAR);
64 if (valp == NULL || strcmp (valp, "some longer value") != 0)
65 {
66 printf ("getenv #4 failed (is \"%s\")\n", valp);
67 result = 1;
68 }
69
70 /* Now one tricky check: changing the variable passed in putenv should
71 change the environment. */
72 strcpy (&putenv_val[sizeof VAR], "a short one");
73
74 /* The value should have changed again. */
75 valp = getenv (VAR);
76 if (valp == NULL || strcmp (valp, "a short one") != 0)
77 {
78 puts ("getenv #5 failed");
79 result = 1;
80 }
81
82 /* It should even be possible to rename the variable. */
83 strcpy (putenv_val, "XYZZY=some other value");
84
85 /* Now a lookup using the old name should fail. */
86 if (getenv (VAR) != NULL)
87 {
88 puts ("getenv #6 failed");
89 result = 1;
90 }
91
92 /* But using the new name it should work. */
93 valp = getenv ("XYZZY");
94 if (valp == NULL || strcmp (valp, "some other value") != 0)
95 {
96 puts ("getenv #7 failed");
97 result = 1;
98 }
99
100 /* Create a new variable with the old name. */
101 if (setenv (VAR, "a new value", 0) != 0)
102 {
103 printf ("setenv #3 failed: %m\n");
104 result = 1;
105 }
106
107 /* At this point a getenv call must return the new value. */
108 valp = getenv (VAR);
109 if (valp == NULL || strcmp (valp, "a new value") != 0)
110 {
111 puts ("getenv #8 failed");
112 result = 1;
113 }
114
115 /* Black magic: rename the variable we added using putenv back. */
116 strcpy (putenv_val, VAR "=old name new value");
117
118 /* This is interesting. We have two variables with the same name.
119 Getting a value should return one of them. */
120 valp = getenv (VAR);
121 if (valp == NULL
122 || (strcmp (valp, "a new value") != 0
123 && strcmp (valp, "old name new value") != 0))
124 {
125 puts ("getenv #9 failed");
126 result = 1;
127 }
128
129 /* More fun ahead: we are now removing the variable. This should remove
130 both values. The cast is ok: this call should never put the string
131 in the environment and it should never modify it. */
132 putenv ((char *) VAR);
133
134 /* Getting the value should now fail. */
135 if (getenv (VAR) != NULL)
136 {
137 printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR));
138 result = 1;
139 }
140
141 /* Now a test with an environment variable that's one character long.
142 This is to test a special case in the getenv implementation. */
143 strcpy (putenv_val, "X=one character test");
144 if (putenv (putenv_val) != 0)
145 {
146 printf ("putenv #2 failed: %m\n");
147 result = 1;
148 }
149
150 valp = getenv ("X");
151 if (valp == NULL || strcmp (valp, "one character test") != 0)
152 {
153 puts ("getenv #11 failed");
154 result = 1;
155 }
156
157 return result;
158 }