Fix country_name in nds_NL
[glibc.git] / stdlib / tst-environ.c
blobb2301641f559d46dad1c2f2e7cd3c1f57b6d7701
1 /* Copyright (C) 1999-2017 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/>. */
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <libc-diag.h>
24 #define VAR "FOOBAR"
26 char putenv_val[100] = VAR "=some longer value";
28 static int
29 do_test (void)
31 int result = 0;
32 const char *valp;
34 /* First test: remove entry FOOBAR, whether it exists or not. */
35 unsetenv (VAR);
37 /* Now getting the value should fail. */
38 if (getenv (VAR) != NULL)
40 printf ("There should be no `%s' value\n", VAR);
41 result = 1;
44 /* Now add a value, with the replace flag cleared. */
45 if (setenv (VAR, "one", 0) != 0)
47 printf ("setenv #1 failed: %m\n");
48 result = 1;
51 /* Getting this value should now be possible. */
52 valp = getenv (VAR);
53 if (valp == NULL || strcmp (valp, "one") != 0)
55 puts ("getenv #2 failed");
56 result = 1;
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");
63 result = 1;
66 /* The value shouldn't have changed. */
67 valp = getenv (VAR);
68 if (valp == NULL || strcmp (valp, "one") != 0)
70 puts ("getenv #3 failed");
71 result = 1;
74 /* Now replace the value using putenv. */
75 if (putenv (putenv_val) != 0)
77 printf ("putenv #1 failed: %m\n");
78 result = 1;
81 /* The value should have changed now. */
82 valp = getenv (VAR);
83 if (valp == NULL || strcmp (valp, "some longer value") != 0)
85 printf ("getenv #4 failed (is \"%s\")\n", valp);
86 result = 1;
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. */
94 valp = getenv (VAR);
95 if (valp == NULL || strcmp (valp, "a short one") != 0)
97 puts ("getenv #5 failed");
98 result = 1;
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");
108 result = 1;
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");
116 result = 1;
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");
123 result = 1;
126 /* At this point a getenv call must return the new value. */
127 valp = getenv (VAR);
128 if (valp == NULL || strcmp (valp, "a new value") != 0)
130 puts ("getenv #8 failed");
131 result = 1;
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. */
139 valp = getenv (VAR);
140 if (valp == NULL
141 || (strcmp (valp, "a new value") != 0
142 && strcmp (valp, "old name new value") != 0))
144 puts ("getenv #9 failed");
145 result = 1;
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));
157 result = 1;
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");
166 result = 1;
169 valp = getenv ("X");
170 if (valp == NULL || strcmp (valp, "one character test") != 0)
172 puts ("getenv #11 failed");
173 result = 1;
176 /* Both setenv and unsetenv should return -1/EINVAL for NULL or "" name
177 or if name contains '=' character. */
178 errno = 0;
179 if (setenv (NULL, "val", 1) >= 0 || errno != EINVAL)
181 puts ("setenv #4 failed");
182 result = 1;
185 errno = 0;
186 if (setenv ("", "val", 0) >= 0 || errno != EINVAL)
188 puts ("setenv #5 failed");
189 result = 1;
192 errno = 0;
193 if (setenv ("var=val", "val", 1) >= 0 || errno != EINVAL)
195 puts ("setenv #6 failed");
196 result = 1;
199 /* This deliberately tests supplying a null pointer to a function whose
200 argument is marked __attribute__ ((nonnull)). */
201 DIAG_PUSH_NEEDS_COMMENT;
202 DIAG_IGNORE_NEEDS_COMMENT(5, "-Wnonnull");
203 errno = 0;
204 if (unsetenv (NULL) >= 0 || errno != EINVAL)
206 puts ("unsetenv #1 failed");
207 result = 1;
209 DIAG_POP_NEEDS_COMMENT;
211 errno = 0;
212 if (unsetenv ("") >= 0 || errno != EINVAL)
214 puts ("unsetenv #2 failed");
215 result = 1;
218 errno = 0;
219 if (unsetenv ("x=y") >= 0 || errno != EINVAL)
221 puts ("unsetenv #3 failed");
222 result = 1;
225 return result;
228 #define TEST_FUNCTION do_test ()
229 #include "../test-skeleton.c"