[BZ #3458]
[glibc.git] / stdlib / tst-environ.c
blob6dd9a40527a7f5f00c42dd33b20feee557914edb
1 /* Copyright (C) 1999, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
25 #define VAR "FOOBAR"
27 char putenv_val[100] = VAR "=some longer value";
29 int
30 main (void)
32 int result = 0;
33 const char *valp;
35 /* First test: remove entry FOOBAR, whether it exists or not. */
36 unsetenv (VAR);
38 /* Now getting the value should fail. */
39 if (getenv (VAR) != NULL)
41 printf ("There should be no `%s' value\n", VAR);
42 result = 1;
45 /* Now add a value, with the replace flag cleared. */
46 if (setenv (VAR, "one", 0) != 0)
48 printf ("setenv #1 failed: %m\n");
49 result = 1;
52 /* Getting this value should now be possible. */
53 valp = getenv (VAR);
54 if (valp == NULL || strcmp (valp, "one") != 0)
56 puts ("getenv #2 failed");
57 result = 1;
60 /* Try to replace without the replace flag set. This should fail. */
61 if (setenv (VAR, "two", 0) != 0)
63 printf ("setenv #2 failed: %m\n");
64 result = 1;
67 /* The value shouldn't have changed. */
68 valp = getenv (VAR);
69 if (valp == NULL || strcmp (valp, "one") != 0)
71 puts ("getenv #3 failed");
72 result = 1;
75 /* Now replace the value using putenv. */
76 if (putenv (putenv_val) != 0)
78 printf ("putenv #1 failed: %m\n");
79 result = 1;
82 /* The value should have changed now. */
83 valp = getenv (VAR);
84 if (valp == NULL || strcmp (valp, "some longer value") != 0)
86 printf ("getenv #4 failed (is \"%s\")\n", valp);
87 result = 1;
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");
94 /* The value should have changed again. */
95 valp = getenv (VAR);
96 if (valp == NULL || strcmp (valp, "a short one") != 0)
98 puts ("getenv #5 failed");
99 result = 1;
102 /* It should even be possible to rename the variable. */
103 strcpy (putenv_val, "XYZZY=some other value");
105 /* Now a lookup using the old name should fail. */
106 if (getenv (VAR) != NULL)
108 puts ("getenv #6 failed");
109 result = 1;
112 /* But using the new name it should work. */
113 valp = getenv ("XYZZY");
114 if (valp == NULL || strcmp (valp, "some other value") != 0)
116 puts ("getenv #7 failed");
117 result = 1;
120 /* Create a new variable with the old name. */
121 if (setenv (VAR, "a new value", 0) != 0)
123 printf ("setenv #3 failed: %m\n");
124 result = 1;
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)
131 puts ("getenv #8 failed");
132 result = 1;
135 /* Black magic: rename the variable we added using putenv back. */
136 strcpy (putenv_val, VAR "=old name new value");
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))
145 puts ("getenv #9 failed");
146 result = 1;
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);
154 /* Getting the value should now fail. */
155 if (getenv (VAR) != NULL)
157 printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR));
158 result = 1;
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)
166 printf ("putenv #2 failed: %m\n");
167 result = 1;
170 valp = getenv ("X");
171 if (valp == NULL || strcmp (valp, "one character test") != 0)
173 puts ("getenv #11 failed");
174 result = 1;
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)
182 puts ("setenv #4 failed");
183 result = 1;
186 errno = 0;
187 if (setenv ("", "val", 0) >= 0 || errno != EINVAL)
189 puts ("setenv #5 failed");
190 result = 1;
193 errno = 0;
194 if (setenv ("var=val", "val", 1) >= 0 || errno != EINVAL)
196 puts ("setenv #6 failed");
197 result = 1;
200 errno = 0;
201 if (unsetenv (NULL) >= 0 || errno != EINVAL)
203 puts ("unsetenv #1 failed");
204 result = 1;
207 errno = 0;
208 if (unsetenv ("") >= 0 || errno != EINVAL)
210 puts ("unsetenv #2 failed");
211 result = 1;
214 errno = 0;
215 if (unsetenv ("x=y") >= 0 || errno != EINVAL)
217 puts ("unsetenv #3 failed");
218 result = 1;
221 return result;