1 /* environ.c -- library for manipulating environments for GNU.
3 Copyright (C) 1986-2023 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 "common-defs.h"
23 /* See gdbsupport/environ.h. */
26 gdb_environ::operator= (gdb_environ
&&e
)
28 /* Are we self-moving? */
34 m_environ_vector
= std::move (e
.m_environ_vector
);
35 m_user_set_env
= std::move (e
.m_user_set_env
);
36 m_user_unset_env
= std::move (e
.m_user_unset_env
);
37 e
.m_environ_vector
.clear ();
38 e
.m_environ_vector
.push_back (NULL
);
39 e
.m_user_set_env
.clear ();
40 e
.m_user_unset_env
.clear ();
44 /* See gdbsupport/environ.h. */
46 gdb_environ
gdb_environ::from_host_environ ()
48 extern char **environ
;
54 for (int i
= 0; environ
[i
] != NULL
; ++i
)
56 /* Make sure we add the element before the last (NULL). */
57 e
.m_environ_vector
.insert (e
.m_environ_vector
.end () - 1,
58 xstrdup (environ
[i
]));
64 /* See gdbsupport/environ.h. */
69 for (char *v
: m_environ_vector
)
71 m_environ_vector
.clear ();
72 /* Always add the NULL element. */
73 m_environ_vector
.push_back (NULL
);
74 m_user_set_env
.clear ();
75 m_user_unset_env
.clear ();
78 /* Helper function to check if STRING contains an environment variable
79 assignment of VAR, i.e., if STRING starts with 'VAR='. Return true
80 if it contains, false otherwise. */
83 match_var_in_string (const char *string
, const char *var
, size_t var_len
)
85 if (strncmp (string
, var
, var_len
) == 0 && string
[var_len
] == '=')
91 /* See gdbsupport/environ.h. */
94 gdb_environ::get (const char *var
) const
96 size_t len
= strlen (var
);
98 for (char *el
: m_environ_vector
)
99 if (el
!= NULL
&& match_var_in_string (el
, var
, len
))
105 /* See gdbsupport/environ.h. */
108 gdb_environ::set (const char *var
, const char *value
)
110 char *fullvar
= concat (var
, "=", value
, (char *) NULL
);
112 /* We have to unset the variable in the vector if it exists. */
115 /* Insert the element before the last one, which is always NULL. */
116 m_environ_vector
.insert (m_environ_vector
.end () - 1, fullvar
);
118 /* Mark this environment variable as having been set by the user.
119 This will be useful when we deal with setting environment
120 variables on the remote target. */
121 m_user_set_env
.insert (std::string (fullvar
));
123 /* If this environment variable is marked as unset by the user, then
124 remove it from the list, because now the user wants to set
126 m_user_unset_env
.erase (std::string (var
));
129 /* See gdbsupport/environ.h. */
132 gdb_environ::unset (const char *var
, bool update_unset_list
)
134 size_t len
= strlen (var
);
135 std::vector
<char *>::iterator it_env
;
137 /* We iterate until '.end () - 1' because the last element is
139 for (it_env
= m_environ_vector
.begin ();
140 it_env
!= m_environ_vector
.end () - 1;
142 if (match_var_in_string (*it_env
, var
, len
))
145 if (it_env
!= m_environ_vector
.end () - 1)
147 m_user_set_env
.erase (std::string (*it_env
));
150 m_environ_vector
.erase (it_env
);
153 if (update_unset_list
)
154 m_user_unset_env
.insert (std::string (var
));
157 /* See gdbsupport/environ.h. */
160 gdb_environ::unset (const char *var
)
165 /* See gdbsupport/environ.h. */
168 gdb_environ::envp () const
170 return const_cast<char **> (&m_environ_vector
[0]);
173 /* See gdbsupport/environ.h. */
175 const std::set
<std::string
> &
176 gdb_environ::user_set_env () const
178 return m_user_set_env
;
181 const std::set
<std::string
> &
182 gdb_environ::user_unset_env () const
184 return m_user_unset_env
;