File: environment.c

package info (click to toggle)
glib2.0 2.66.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, bullseye-backports
  • size: 49,488 kB
  • sloc: ansic: 482,057; xml: 17,293; python: 7,705; sh: 1,310; perl: 1,140; makefile: 194; cpp: 9
file content (169 lines) | stat: -rw-r--r-- 3,882 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <glib.h>

static void
test_listenv (void)
{
  GHashTable *table;
  gchar **list;
  gint i;

  table = g_hash_table_new_full (g_str_hash, g_str_equal,
                                 g_free, g_free);

  list = g_get_environ ();
  for (i = 0; list[i]; i++)
    {
      gchar **parts;

      parts = g_strsplit (list[i], "=", 2);
      g_assert (g_hash_table_lookup (table, parts[0]) == NULL);
      if (g_strcmp0 (parts[0], ""))
        g_hash_table_insert (table, parts[0], parts[1]);
      g_free (parts);
    }
  g_strfreev (list);

  g_assert_cmpint (g_hash_table_size (table), >, 0);

  list = g_listenv ();
  for (i = 0; list[i]; i++)
    {
      const gchar *expected;
      const gchar *value;

      expected = g_hash_table_lookup (table, list[i]);
      value = g_getenv (list[i]);
      g_assert_cmpstr (value, ==, expected);
      g_hash_table_remove (table, list[i]);
    }
  g_assert_cmpint (g_hash_table_size (table), ==, 0);
  g_hash_table_unref (table);
  g_strfreev (list);
}

static void
test_setenv (void)
{
  const gchar *var, *value;

  var = "NOSUCHENVVAR";
  value = "value1";

  g_assert (g_getenv (var) == NULL);
  g_setenv (var, value, FALSE);
  g_assert_cmpstr (g_getenv (var), ==, value);
  g_assert (g_setenv (var, "value2", FALSE));
  g_assert_cmpstr (g_getenv (var), ==, value);
  g_assert (g_setenv (var, "value2", TRUE));
  g_assert_cmpstr (g_getenv (var), ==, "value2");
  g_unsetenv (var);
  g_assert (g_getenv (var) == NULL);
}

static void
test_environ_array (void)
{
  gchar **env;
  const gchar *value;

  env = g_new (gchar *, 1);
  env[0] = NULL;

  value = g_environ_getenv (env, "foo");
  g_assert (value == NULL);

  env = g_environ_setenv (env, "foo", "bar", TRUE);
  value = g_environ_getenv (env, "foo");
  g_assert_cmpstr (value, ==, "bar");

  env = g_environ_setenv (env, "foo2", "bar2", FALSE);
  value = g_environ_getenv (env, "foo");
  g_assert_cmpstr (value, ==, "bar");
  value = g_environ_getenv (env, "foo2");
  g_assert_cmpstr (value, ==, "bar2");

  env = g_environ_setenv (env, "foo", "x", FALSE);
  value = g_environ_getenv (env, "foo");
  g_assert_cmpstr (value, ==, "bar");

  env = g_environ_setenv (env, "foo", "x", TRUE);
  value = g_environ_getenv (env, "foo");
  g_assert_cmpstr (value, ==, "x");

  env = g_environ_unsetenv (env, "foo2");
  value = g_environ_getenv (env, "foo2");
  g_assert (value == NULL);

  g_strfreev (env);
}

static void
test_environ_null (void)
{
  gchar **env;
  const gchar *value;

  env = NULL;

  value = g_environ_getenv (env, "foo");
  g_assert (value == NULL);

  env = g_environ_setenv (NULL, "foo", "bar", TRUE);
  g_assert (env != NULL);
  g_strfreev (env);

  env = g_environ_unsetenv (NULL, "foo");
  g_assert (env == NULL);
}

static void
test_environ_case (void)
{
  gchar **env;
  const gchar *value;

  env = NULL;

  env = g_environ_setenv (env, "foo", "bar", TRUE);
  value = g_environ_getenv (env, "foo");
  g_assert_cmpstr (value, ==, "bar");

  value = g_environ_getenv (env, "Foo");
#ifdef G_OS_WIN32
  g_assert_cmpstr (value, ==, "bar");
#else
  g_assert (value == NULL);
#endif

  env = g_environ_setenv (env, "FOO", "x", TRUE);
  value = g_environ_getenv (env, "foo");
#ifdef G_OS_WIN32
  g_assert_cmpstr (value, ==, "x");
#else
  g_assert_cmpstr (value, ==, "bar");
#endif

  env = g_environ_unsetenv (env, "Foo");
  value = g_environ_getenv (env, "foo");
#ifdef G_OS_WIN32
  g_assert (value == NULL);
#else
  g_assert_cmpstr (value, ==, "bar");
#endif

  g_strfreev (env);
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/environ/listenv", test_listenv);
  g_test_add_func ("/environ/setenv", test_setenv);
  g_test_add_func ("/environ/array", test_environ_array);
  g_test_add_func ("/environ/null", test_environ_null);
  g_test_add_func ("/environ/case", test_environ_case);

  return g_test_run ();
}