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/>. */
22 /* See gdbsupport/environ.h. */
25 gdb_environ::operator= (gdb_environ
&&e
)
27 /* Are we self-moving? */
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 ();
43 /* See gdbsupport/environ.h. */
45 gdb_environ
gdb_environ::from_host_environ ()
47 extern char **environ
;
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
]));
63 /* See gdbsupport/environ.h. */
68 for (char *v
: m_environ_vector
)
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. */
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
] == '=')
90 /* See gdbsupport/environ.h. */
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
))
104 /* See gdbsupport/environ.h. */
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. */
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
125 m_user_unset_env
.erase (std::string (var
));
128 /* See gdbsupport/environ.h. */
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
138 for (it_env
= m_environ_vector
.begin ();
139 it_env
!= m_environ_vector
.end () - 1;
141 if (match_var_in_string (*it_env
, var
, len
))
144 if (it_env
!= m_environ_vector
.end () - 1)
146 m_user_set_env
.erase (std::string (*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. */
159 gdb_environ::unset (const char *var
)
164 /* See gdbsupport/environ.h. */
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
;