1 | /* Copyright (C) 1999, 2004 Free Software Foundation, Inc.
|
---|
2 | This file is part of the GNU C Library.
|
---|
3 |
|
---|
4 | The GNU C Library is free software; you can redistribute it and/or
|
---|
5 | modify it under the terms of the GNU Lesser General Public
|
---|
6 | License as published by the Free Software Foundation; either
|
---|
7 | version 2.1 of the License, or (at your option) any later version.
|
---|
8 |
|
---|
9 | The GNU C Library is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | Lesser General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU Lesser General Public
|
---|
15 | License along with the GNU C Library; if not, write to the Free
|
---|
16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
17 | 02111-1307 USA. */
|
---|
18 |
|
---|
19 | #include <errno.h>
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <string.h>
|
---|
23 |
|
---|
24 |
|
---|
25 | #define VAR "FOOBAR"
|
---|
26 |
|
---|
27 | char putenv_val[100] = VAR "=some longer value";
|
---|
28 |
|
---|
29 | int
|
---|
30 | main (void)
|
---|
31 | {
|
---|
32 | int result = 0;
|
---|
33 | const char *valp;
|
---|
34 |
|
---|
35 | /* First test: remove entry FOOBAR, whether it exists or not. */
|
---|
36 | unsetenv (VAR);
|
---|
37 |
|
---|
38 | /* Now getting the value should fail. */
|
---|
39 | if (getenv (VAR) != NULL)
|
---|
40 | {
|
---|
41 | printf ("There should be no `%s' value\n", VAR);
|
---|
42 | result = 1;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /* Now add a value, with the replace flag cleared. */
|
---|
46 | if (setenv (VAR, "one", 0) != 0)
|
---|
47 | {
|
---|
48 | printf ("setenv #1 failed: %m\n");
|
---|
49 | result = 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* Getting this value should now be possible. */
|
---|
53 | valp = getenv (VAR);
|
---|
54 | if (valp == NULL || strcmp (valp, "one") != 0)
|
---|
55 | {
|
---|
56 | puts ("getenv #2 failed");
|
---|
57 | result = 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /* Try to replace without the replace flag set. This should fail. */
|
---|
61 | if (setenv (VAR, "two", 0) != 0)
|
---|
62 | {
|
---|
63 | printf ("setenv #2 failed: %m\n");
|
---|
64 | result = 1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* The value shouldn't have changed. */
|
---|
68 | valp = getenv (VAR);
|
---|
69 | if (valp == NULL || strcmp (valp, "one") != 0)
|
---|
70 | {
|
---|
71 | puts ("getenv #3 failed");
|
---|
72 | result = 1;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /* Now replace the value using putenv. */
|
---|
76 | if (putenv (putenv_val) != 0)
|
---|
77 | {
|
---|
78 | printf ("putenv #1 failed: %m\n");
|
---|
79 | result = 1;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* The value should have changed now. */
|
---|
83 | valp = getenv (VAR);
|
---|
84 | if (valp == NULL || strcmp (valp, "some longer value") != 0)
|
---|
85 | {
|
---|
86 | printf ("getenv #4 failed (is \"%s\")\n", valp);
|
---|
87 | result = 1;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /* Now one tricky check: changing the variable passed in putenv should
|
---|
91 | change the environment. */
|
---|
92 | strcpy (&putenv_val[sizeof VAR], "a short one");
|
---|
93 |
|
---|
94 | /* The value should have changed again. */
|
---|
95 | valp = getenv (VAR);
|
---|
96 | if (valp == NULL || strcmp (valp, "a short one") != 0)
|
---|
97 | {
|
---|
98 | puts ("getenv #5 failed");
|
---|
99 | result = 1;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* It should even be possible to rename the variable. */
|
---|
103 | strcpy (putenv_val, "XYZZY=some other value");
|
---|
104 |
|
---|
105 | /* Now a lookup using the old name should fail. */
|
---|
106 | if (getenv (VAR) != NULL)
|
---|
107 | {
|
---|
108 | puts ("getenv #6 failed");
|
---|
109 | result = 1;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* But using the new name it should work. */
|
---|
113 | valp = getenv ("XYZZY");
|
---|
114 | if (valp == NULL || strcmp (valp, "some other value") != 0)
|
---|
115 | {
|
---|
116 | puts ("getenv #7 failed");
|
---|
117 | result = 1;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* Create a new variable with the old name. */
|
---|
121 | if (setenv (VAR, "a new value", 0) != 0)
|
---|
122 | {
|
---|
123 | printf ("setenv #3 failed: %m\n");
|
---|
124 | result = 1;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /* At this point a getenv call must return the new value. */
|
---|
128 | valp = getenv (VAR);
|
---|
129 | if (valp == NULL || strcmp (valp, "a new value") != 0)
|
---|
130 | {
|
---|
131 | puts ("getenv #8 failed");
|
---|
132 | result = 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* Black magic: rename the variable we added using putenv back. */
|
---|
136 | strcpy (putenv_val, VAR "=old name new value");
|
---|
137 |
|
---|
138 | /* This is interesting. We have two variables with the same name.
|
---|
139 | Getting a value should return one of them. */
|
---|
140 | valp = getenv (VAR);
|
---|
141 | if (valp == NULL
|
---|
142 | || (strcmp (valp, "a new value") != 0
|
---|
143 | && strcmp (valp, "old name new value") != 0))
|
---|
144 | {
|
---|
145 | puts ("getenv #9 failed");
|
---|
146 | result = 1;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* More fun ahead: we are now removing the variable. This should remove
|
---|
150 | both values. The cast is ok: this call should never put the string
|
---|
151 | in the environment and it should never modify it. */
|
---|
152 | putenv ((char *) VAR);
|
---|
153 |
|
---|
154 | /* Getting the value should now fail. */
|
---|
155 | if (getenv (VAR) != NULL)
|
---|
156 | {
|
---|
157 | printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR));
|
---|
158 | result = 1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* Now a test with an environment variable that's one character long.
|
---|
162 | This is to test a special case in the getenv implementation. */
|
---|
163 | strcpy (putenv_val, "X=one character test");
|
---|
164 | if (putenv (putenv_val) != 0)
|
---|
165 | {
|
---|
166 | printf ("putenv #2 failed: %m\n");
|
---|
167 | result = 1;
|
---|
168 | }
|
---|
169 |
|
---|
170 | valp = getenv ("X");
|
---|
171 | if (valp == NULL || strcmp (valp, "one character test") != 0)
|
---|
172 | {
|
---|
173 | puts ("getenv #11 failed");
|
---|
174 | result = 1;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
|
---|
178 | or if name contains '=' character. */
|
---|
179 | errno = 0;
|
---|
180 | if (setenv (NULL, "val", 1) >= 0 || errno != EINVAL)
|
---|
181 | {
|
---|
182 | puts ("setenv #4 failed");
|
---|
183 | result = 1;
|
---|
184 | }
|
---|
185 |
|
---|
186 | errno = 0;
|
---|
187 | if (setenv ("", "val", 0) >= 0 || errno != EINVAL)
|
---|
188 | {
|
---|
189 | puts ("setenv #5 failed");
|
---|
190 | result = 1;
|
---|
191 | }
|
---|
192 |
|
---|
193 | errno = 0;
|
---|
194 | if (setenv ("var=val", "val", 1) >= 0 || errno != EINVAL)
|
---|
195 | {
|
---|
196 | puts ("setenv #6 failed");
|
---|
197 | result = 1;
|
---|
198 | }
|
---|
199 |
|
---|
200 | errno = 0;
|
---|
201 | if (unsetenv (NULL) >= 0 || errno != EINVAL)
|
---|
202 | {
|
---|
203 | puts ("unsetenv #1 failed");
|
---|
204 | result = 1;
|
---|
205 | }
|
---|
206 |
|
---|
207 | errno = 0;
|
---|
208 | if (unsetenv ("") >= 0 || errno != EINVAL)
|
---|
209 | {
|
---|
210 | puts ("unsetenv #2 failed");
|
---|
211 | result = 1;
|
---|
212 | }
|
---|
213 |
|
---|
214 | errno = 0;
|
---|
215 | if (unsetenv ("x=y") >= 0 || errno != EINVAL)
|
---|
216 | {
|
---|
217 | puts ("unsetenv #3 failed");
|
---|
218 | result = 1;
|
---|
219 | }
|
---|
220 |
|
---|
221 | return result;
|
---|
222 | }
|
---|