1 /* Copyright (C) 1999-2013 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, see
16 <http://www.gnu.org/licenses/>. */
26 char putenv_val
[100] = VAR
"=some longer value";
34 /* First test: remove entry FOOBAR, whether it exists or not. */
37 /* Now getting the value should fail. */
38 if (getenv (VAR
) != NULL
)
40 printf ("There should be no `%s' value\n", VAR
);
44 /* Now add a value, with the replace flag cleared. */
45 if (setenv (VAR
, "one", 0) != 0)
47 printf ("setenv #1 failed: %m\n");
51 /* Getting this value should now be possible. */
53 if (valp
== NULL
|| strcmp (valp
, "one") != 0)
55 puts ("getenv #2 failed");
59 /* Try to replace without the replace flag set. This should fail. */
60 if (setenv (VAR
, "two", 0) != 0)
62 printf ("setenv #2 failed: %m\n");
66 /* The value shouldn't have changed. */
68 if (valp
== NULL
|| strcmp (valp
, "one") != 0)
70 puts ("getenv #3 failed");
74 /* Now replace the value using putenv. */
75 if (putenv (putenv_val
) != 0)
77 printf ("putenv #1 failed: %m\n");
81 /* The value should have changed now. */
83 if (valp
== NULL
|| strcmp (valp
, "some longer value") != 0)
85 printf ("getenv #4 failed (is \"%s\")\n", valp
);
89 /* Now one tricky check: changing the variable passed in putenv should
90 change the environment. */
91 strcpy (&putenv_val
[sizeof VAR
], "a short one");
93 /* The value should have changed again. */
95 if (valp
== NULL
|| strcmp (valp
, "a short one") != 0)
97 puts ("getenv #5 failed");
101 /* It should even be possible to rename the variable. */
102 strcpy (putenv_val
, "XYZZY=some other value");
104 /* Now a lookup using the old name should fail. */
105 if (getenv (VAR
) != NULL
)
107 puts ("getenv #6 failed");
111 /* But using the new name it should work. */
112 valp
= getenv ("XYZZY");
113 if (valp
== NULL
|| strcmp (valp
, "some other value") != 0)
115 puts ("getenv #7 failed");
119 /* Create a new variable with the old name. */
120 if (setenv (VAR
, "a new value", 0) != 0)
122 printf ("setenv #3 failed: %m\n");
126 /* At this point a getenv call must return the new value. */
128 if (valp
== NULL
|| strcmp (valp
, "a new value") != 0)
130 puts ("getenv #8 failed");
134 /* Black magic: rename the variable we added using putenv back. */
135 strcpy (putenv_val
, VAR
"=old name new value");
137 /* This is interesting. We have two variables with the same name.
138 Getting a value should return one of them. */
141 || (strcmp (valp
, "a new value") != 0
142 && strcmp (valp
, "old name new value") != 0))
144 puts ("getenv #9 failed");
148 /* More fun ahead: we are now removing the variable. This should remove
149 both values. The cast is ok: this call should never put the string
150 in the environment and it should never modify it. */
151 putenv ((char *) VAR
);
153 /* Getting the value should now fail. */
154 if (getenv (VAR
) != NULL
)
156 printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR
));
160 /* Now a test with an environment variable that's one character long.
161 This is to test a special case in the getenv implementation. */
162 strcpy (putenv_val
, "X=one character test");
163 if (putenv (putenv_val
) != 0)
165 printf ("putenv #2 failed: %m\n");
170 if (valp
== NULL
|| strcmp (valp
, "one character test") != 0)
172 puts ("getenv #11 failed");
176 /* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
177 or if name contains '=' character. */
179 if (setenv (NULL
, "val", 1) >= 0 || errno
!= EINVAL
)
181 puts ("setenv #4 failed");
186 if (setenv ("", "val", 0) >= 0 || errno
!= EINVAL
)
188 puts ("setenv #5 failed");
193 if (setenv ("var=val", "val", 1) >= 0 || errno
!= EINVAL
)
195 puts ("setenv #6 failed");
200 if (unsetenv (NULL
) >= 0 || errno
!= EINVAL
)
202 puts ("unsetenv #1 failed");
207 if (unsetenv ("") >= 0 || errno
!= EINVAL
)
209 puts ("unsetenv #2 failed");
214 if (unsetenv ("x=y") >= 0 || errno
!= EINVAL
)
216 puts ("unsetenv #3 failed");