Debian Codegraph @

gnulib-tests/test-putenv.c

raw
1 /* Test the putenv function.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation, either version 3 of the License,
7 or (at your option) any later version.
8
9 This file 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Collin Funk <[email protected]>, 2024. */
18
19 #include <config.h>
20
21 /* Specification. */
22 #include <stdlib.h>
23
24 #include "signature.h"
25 SIGNATURE_CHECK (putenv, int, (char *));
26
27 #include <string.h>
28
29 #include "macros.h"
30
31 int
32 main (void)
33 {
34 char *ptr;
35
36 /* Verify the environment is clean. */
37 unsetenv ("TEST_VAR");
38 ASSERT (getenv ("TEST_VAR") == NULL);
39
40 /* Use static on variables passed to the environment to pacify
41 -Wanalyzer-putenv-of-auto-var. */
42
43 /* Verify adding an environment variable. */
44 {
45 static char var[] = "TEST_VAR=abc";
46 ASSERT (putenv (var) == 0);
47 ptr = getenv ("TEST_VAR");
48 ASSERT (ptr != NULL);
49 ASSERT (STREQ (ptr, "abc"));
50 }
51
52 /* Verify removing an environment variable. */
53 {
54 static char var[] = "TEST_VAR";
55 ASSERT (putenv (var) == 0);
56 ASSERT (getenv ("TEST_VAR") == NULL);
57 }
58
59 /* Verify the behavior when removing a variable not in the environment. */
60 {
61 static char var[] = "TEST_VAR";
62 ASSERT (putenv (var) == 0);
63 ASSERT (getenv ("TEST_VAR") == NULL);
64 }
65
66 return test_exit_status;
67 }