Automatic date update in version.in
[binutils-gdb.git] / gdbsupport / environ.cc
blob1b6ca61ee8049a06dc03253f9fa057a1a169751b
1 /* environ.c -- library for manipulating environments for GNU.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "environ.h"
19 #include <algorithm>
20 #include <utility>
22 /* See gdbsupport/environ.h. */
24 gdb_environ &
25 gdb_environ::operator= (gdb_environ &&e)
27 /* Are we self-moving? */
28 if (&e == this)
29 return *this;
31 this->clear ();
33 m_environ_vector = std::move (e.m_environ_vector);
34 m_user_set_env = std::move (e.m_user_set_env);
35 m_user_unset_env = std::move (e.m_user_unset_env);
36 e.m_environ_vector.clear ();
37 e.m_environ_vector.push_back (NULL);
38 e.m_user_set_env.clear ();
39 e.m_user_unset_env.clear ();
40 return *this;
43 /* See gdbsupport/environ.h. */
45 gdb_environ gdb_environ::from_host_environ ()
47 extern char **environ;
48 gdb_environ e;
50 if (environ == NULL)
51 return e;
53 for (int i = 0; environ[i] != NULL; ++i)
55 /* Make sure we add the element before the last (NULL). */
56 e.m_environ_vector.insert (e.m_environ_vector.end () - 1,
57 xstrdup (environ[i]));
60 return e;
63 /* See gdbsupport/environ.h. */
65 void
66 gdb_environ::clear ()
68 for (char *v : m_environ_vector)
69 xfree (v);
70 m_environ_vector.clear ();
71 /* Always add the NULL element. */
72 m_environ_vector.push_back (NULL);
73 m_user_set_env.clear ();
74 m_user_unset_env.clear ();
77 /* Helper function to check if STRING contains an environment variable
78 assignment of VAR, i.e., if STRING starts with 'VAR='. Return true
79 if it contains, false otherwise. */
81 static bool
82 match_var_in_string (const char *string, const char *var, size_t var_len)
84 if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
85 return true;
87 return false;
90 /* See gdbsupport/environ.h. */
92 const char *
93 gdb_environ::get (const char *var) const
95 size_t len = strlen (var);
97 for (char *el : m_environ_vector)
98 if (el != NULL && match_var_in_string (el, var, len))
99 return &el[len + 1];
101 return NULL;
104 /* See gdbsupport/environ.h. */
106 void
107 gdb_environ::set (const char *var, const char *value)
109 char *fullvar = concat (var, "=", value, (char *) NULL);
111 /* We have to unset the variable in the vector if it exists. */
112 unset (var, false);
114 /* Insert the element before the last one, which is always NULL. */
115 m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
117 /* Mark this environment variable as having been set by the user.
118 This will be useful when we deal with setting environment
119 variables on the remote target. */
120 m_user_set_env.insert (std::string (fullvar));
122 /* If this environment variable is marked as unset by the user, then
123 remove it from the list, because now the user wants to set
124 it. */
125 m_user_unset_env.erase (std::string (var));
128 /* See gdbsupport/environ.h. */
130 void
131 gdb_environ::unset (const char *var, bool update_unset_list)
133 size_t len = strlen (var);
134 std::vector<char *>::iterator it_env;
136 /* We iterate until '.end () - 1' because the last element is
137 always NULL. */
138 for (it_env = m_environ_vector.begin ();
139 it_env != m_environ_vector.end () - 1;
140 ++it_env)
141 if (match_var_in_string (*it_env, var, len))
142 break;
144 if (it_env != m_environ_vector.end () - 1)
146 m_user_set_env.erase (std::string (*it_env));
147 xfree (*it_env);
149 m_environ_vector.erase (it_env);
152 if (update_unset_list)
153 m_user_unset_env.insert (std::string (var));
156 /* See gdbsupport/environ.h. */
158 void
159 gdb_environ::unset (const char *var)
161 unset (var, true);
164 /* See gdbsupport/environ.h. */
166 char **
167 gdb_environ::envp () const
169 return const_cast<char **> (&m_environ_vector[0]);
172 /* See gdbsupport/environ.h. */
174 const std::set<std::string> &
175 gdb_environ::user_set_env () const
177 return m_user_set_env;
180 const std::set<std::string> &
181 gdb_environ::user_unset_env () const
183 return m_user_unset_env;