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
27 char putenv_val
[100] = VAR
"=some longer value";
35 /* First test: remove entry FOOBAR, whether it exists or not. */
38 /* Now getting the value should fail. */
39 if (getenv (VAR
) != NULL
)
41 printf ("There should be no `%s' value\n", VAR
);
45 /* Now add a value, with the replace flag cleared. */
46 if (setenv (VAR
, "one", 0) != 0)
48 printf ("setenv #1 failed: %m\n");
52 /* Getting this value should now be possible. */
54 if (valp
== NULL
|| strcmp (valp
, "one") != 0)
56 puts ("getenv #2 failed");
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");
67 /* The value shouldn't have changed. */
69 if (valp
== NULL
|| strcmp (valp
, "one") != 0)
71 puts ("getenv #3 failed");
75 /* Now replace the value using putenv. */
76 if (putenv (putenv_val
) != 0)
78 printf ("putenv #1 failed: %m\n");
82 /* The value should have changed now. */
84 if (valp
== NULL
|| strcmp (valp
, "some longer value") != 0)
86 printf ("getenv #4 failed (is \"%s\")\n", valp
);
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. */
96 if (valp
== NULL
|| strcmp (valp
, "a short one") != 0)
98 puts ("getenv #5 failed");
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");
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");
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");
127 /* At this point a getenv call must return the new value. */
129 if (valp
== NULL
|| strcmp (valp
, "a new value") != 0)
131 puts ("getenv #8 failed");
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. */
142 || (strcmp (valp
, "a new value") != 0
143 && strcmp (valp
, "old name new value") != 0))
145 puts ("getenv #9 failed");
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
));
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");
171 if (valp
== NULL
|| strcmp (valp
, "one character test") != 0)
173 puts ("getenv #11 failed");
177 /* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
178 or if name contains '=' character. */
180 if (setenv (NULL
, "val", 1) >= 0 || errno
!= EINVAL
)
182 puts ("setenv #4 failed");
187 if (setenv ("", "val", 0) >= 0 || errno
!= EINVAL
)
189 puts ("setenv #5 failed");
194 if (setenv ("var=val", "val", 1) >= 0 || errno
!= EINVAL
)
196 puts ("setenv #6 failed");
201 if (unsetenv (NULL
) >= 0 || errno
!= EINVAL
)
203 puts ("unsetenv #1 failed");
208 if (unsetenv ("") >= 0 || errno
!= EINVAL
)
210 puts ("unsetenv #2 failed");
215 if (unsetenv ("x=y") >= 0 || errno
!= EINVAL
)
217 puts ("unsetenv #3 failed");