]>
git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/tst-environ.c
7 char putenv_val
[100] = VAR
"=some longer value";
15 /* First test: remove entry FOOBAR, whether it exists or not. */
18 /* Now getting the value should fail. */
19 if (getenv (VAR
) != NULL
)
21 printf ("There should be no `%s' value\n", VAR
);
25 /* Now add a value, with the replace flag cleared. */
26 if (setenv (VAR
, "one", 0) != 0)
28 printf ("setenv #1 failed: %m\n");
32 /* Getting this value should now be possible. */
34 if (valp
== NULL
|| strcmp (valp
, "one") != 0)
36 puts ("getenv #2 failed");
40 /* Try to replace without the replace flag set. This should fail. */
41 if (setenv (VAR
, "two", 0) != 0)
43 printf ("setenv #2 failed: %m\n");
47 /* The value shouldn't have changed. */
49 if (valp
== NULL
|| strcmp (valp
, "one") != 0)
51 puts ("getenv #3 failed");
55 /* Now replace the value using putenv. */
56 if (putenv (putenv_val
) != 0)
58 printf ("putenv #1 failed: %m\n");
62 /* The value should have changed now. */
64 if (valp
== NULL
|| strcmp (valp
, "some longer value") != 0)
66 printf ("getenv #4 failed (is \"%s\")\n", valp
);
70 /* Now one tricky check: changing the variable passed in putenv should
71 change the environment. */
72 strcpy (&putenv_val
[sizeof VAR
], "a short one");
74 /* The value should have changed again. */
76 if (valp
== NULL
|| strcmp (valp
, "a short one") != 0)
78 puts ("getenv #5 failed");
82 /* It should even be possible to rename the variable. */
83 strcpy (putenv_val
, "XYZZY=some other value");
85 /* Now a lookup using the old name should fail. */
86 if (getenv (VAR
) != NULL
)
88 puts ("getenv #6 failed");
92 /* But using the new name it should work. */
93 valp
= getenv ("XYZZY");
94 if (valp
== NULL
|| strcmp (valp
, "some other value") != 0)
96 puts ("getenv #7 failed");
100 /* Create a new variable with the old name. */
101 if (setenv (VAR
, "a new value", 0) != 0)
103 printf ("setenv #3 failed: %m\n");
107 /* At this point a getenv call must return the new value. */
109 if (valp
== NULL
|| strcmp (valp
, "a new value") != 0)
111 puts ("getenv #8 failed");
115 /* Black magic: rename the variable we added using putenv back. */
116 strcpy (putenv_val
, VAR
"=old name new value");
118 /* This is interesting. We have two variables with the same name.
119 Getting a value should return one of them. */
122 || (strcmp (valp
, "a new value") != 0
123 && strcmp (valp
, "old name new value") != 0))
125 puts ("getenv #9 failed");
129 /* More fun ahead: we are now removing the variable. This should remove
130 both values. The cast is ok: this call should never put the string
131 in the environment and it should never modify it. */
132 putenv ((char *) VAR
);
134 /* Getting the value should now fail. */
135 if (getenv (VAR
) != NULL
)
137 printf ("getenv #10 failed (\"%s\" found)\n", getenv (VAR
));
141 /* Now a test with an environment variable that's one character long.
142 This is to test a special case in the getenv implementation. */
143 strcpy (putenv_val
, "X=one character test");
144 if (putenv (putenv_val
) != 0)
146 printf ("putenv #2 failed: %m\n");
151 if (valp
== NULL
|| strcmp (valp
, "one character test") != 0)
153 puts ("getenv #11 failed");