xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/environ.cc (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
1 /* environ.c -- library for manipulating environments for GNU.
2 
3    Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 
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.
9 
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.
14 
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/>.  */
17 
18 #include "environ.h"
19 #include <algorithm>
20 #include <utility>
21 
22 /* See gdbsupport/environ.h.  */
23 
24 gdb_environ &
25 gdb_environ::operator= (gdb_environ &&e)
26 {
27   /* Are we self-moving?  */
28   if (&e == this)
29     return *this;
30 
31   this->clear ();
32 
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;
41 }
42 
43 /* See gdbsupport/environ.h.  */
44 
45 gdb_environ gdb_environ::from_host_environ ()
46 {
47   extern char **environ;
48   gdb_environ e;
49 
50   if (environ == NULL)
51     return e;
52 
53   for (int i = 0; environ[i] != NULL; ++i)
54     {
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]));
58     }
59 
60   return e;
61 }
62 
63 /* See gdbsupport/environ.h.  */
64 
65 void
66 gdb_environ::clear ()
67 {
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 ();
75 }
76 
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.  */
80 
81 static bool
82 match_var_in_string (const char *string, const char *var, size_t var_len)
83 {
84   if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
85     return true;
86 
87   return false;
88 }
89 
90 /* See gdbsupport/environ.h.  */
91 
92 const char *
93 gdb_environ::get (const char *var) const
94 {
95   size_t len = strlen (var);
96 
97   for (char *el : m_environ_vector)
98     if (el != NULL && match_var_in_string (el, var, len))
99       return &el[len + 1];
100 
101   return NULL;
102 }
103 
104 /* See gdbsupport/environ.h.  */
105 
106 void
107 gdb_environ::set (const char *var, const char *value)
108 {
109   char *fullvar = concat (var, "=", value, (char *) NULL);
110 
111   /* We have to unset the variable in the vector if it exists.  */
112   unset (var, false);
113 
114   /* Insert the element before the last one, which is always NULL.  */
115   m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
116 
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));
121 
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));
126 }
127 
128 /* See gdbsupport/environ.h.  */
129 
130 void
131 gdb_environ::unset (const char *var, bool update_unset_list)
132 {
133   size_t len = strlen (var);
134   std::vector<char *>::iterator it_env;
135 
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;
143 
144   if (it_env != m_environ_vector.end () - 1)
145     {
146       m_user_set_env.erase (std::string (*it_env));
147       xfree (*it_env);
148 
149       m_environ_vector.erase (it_env);
150     }
151 
152   if (update_unset_list)
153     m_user_unset_env.insert (std::string (var));
154 }
155 
156 /* See gdbsupport/environ.h.  */
157 
158 void
159 gdb_environ::unset (const char *var)
160 {
161   unset (var, true);
162 }
163 
164 /* See gdbsupport/environ.h.  */
165 
166 char **
167 gdb_environ::envp () const
168 {
169   return const_cast<char **> (&m_environ_vector[0]);
170 }
171 
172 /* See gdbsupport/environ.h.  */
173 
174 const std::set<std::string> &
175 gdb_environ::user_set_env () const
176 {
177   return m_user_set_env;
178 }
179 
180 const std::set<std::string> &
181 gdb_environ::user_unset_env () const
182 {
183   return m_user_unset_env;
184 }
185