GDBserver
environ.c
Go to the documentation of this file.
1 /* environ.c -- library for manipulating environments for GNU.
2 
3  Copyright (C) 1986-2018 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 "common-defs.h"
19 #include "environ.h"
20 #include <algorithm>
21 #include <utility>
22 
23 /* See common/environ.h. */
24 
27 {
28  /* Are we self-moving? */
29  if (&e == this)
30  return *this;
31 
32  m_environ_vector = std::move (e.m_environ_vector);
33  m_user_set_env = std::move (e.m_user_set_env);
34  m_user_unset_env = std::move (e.m_user_unset_env);
35  e.m_environ_vector.clear ();
36  e.m_environ_vector.push_back (NULL);
37  e.m_user_set_env.clear ();
38  e.m_user_unset_env.clear ();
39  return *this;
40 }
41 
42 /* See common/environ.h. */
43 
45 {
46  extern char **environ;
47  gdb_environ e;
48 
49  if (environ == NULL)
50  return e;
51 
52  for (int i = 0; environ[i] != NULL; ++i)
53  {
54  /* Make sure we add the element before the last (NULL). */
55  e.m_environ_vector.insert (e.m_environ_vector.end () - 1,
56  xstrdup (environ[i]));
57  }
58 
59  return e;
60 }
61 
62 /* See common/environ.h. */
63 
64 void
66 {
67  for (char *v : m_environ_vector)
68  xfree (v);
69  m_environ_vector.clear ();
70  /* Always add the NULL element. */
71  m_environ_vector.push_back (NULL);
72  m_user_set_env.clear ();
73  m_user_unset_env.clear ();
74 }
75 
76 /* Helper function to check if STRING contains an environment variable
77  assignment of VAR, i.e., if STRING starts with 'VAR='. Return true
78  if it contains, false otherwise. */
79 
80 static bool
81 match_var_in_string (const char *string, const char *var, size_t var_len)
82 {
83  if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
84  return true;
85 
86  return false;
87 }
88 
89 /* See common/environ.h. */
90 
91 const char *
92 gdb_environ::get (const char *var) const
93 {
94  size_t len = strlen (var);
95 
96  for (char *el : m_environ_vector)
97  if (el != NULL && match_var_in_string (el, var, len))
98  return &el[len + 1];
99 
100  return NULL;
101 }
102 
103 /* See common/environ.h. */
104 
105 void
106 gdb_environ::set (const char *var, const char *value)
107 {
108  char *fullvar = concat (var, "=", value, NULL);
109 
110  /* We have to unset the variable in the vector if it exists. */
111  unset (var, false);
112 
113  /* Insert the element before the last one, which is always NULL. */
114  m_environ_vector.insert (m_environ_vector.end () - 1, fullvar);
115 
116  /* Mark this environment variable as having been set by the user.
117  This will be useful when we deal with setting environment
118  variables on the remote target. */
119  m_user_set_env.insert (std::string (fullvar));
120 
121  /* If this environment variable is marked as unset by the user, then
122  remove it from the list, because now the user wants to set
123  it. */
124  m_user_unset_env.erase (std::string (var));
125 }
126 
127 /* See common/environ.h. */
128 
129 void
130 gdb_environ::unset (const char *var, bool update_unset_list)
131 {
132  size_t len = strlen (var);
133  std::vector<char *>::iterator it_env;
134 
135  /* We iterate until '.end () - 1' because the last element is
136  always NULL. */
137  for (it_env = m_environ_vector.begin ();
138  it_env != m_environ_vector.end () - 1;
139  ++it_env)
140  if (match_var_in_string (*it_env, var, len))
141  break;
142 
143  if (it_env != m_environ_vector.end () - 1)
144  {
145  m_user_set_env.erase (std::string (*it_env));
146  xfree (*it_env);
147 
148  m_environ_vector.erase (it_env);
149  }
150 
151  if (update_unset_list)
152  m_user_unset_env.insert (std::string (var));
153 }
154 
155 /* See common/environ.h. */
156 
157 void
158 gdb_environ::unset (const char *var)
159 {
160  unset (var, true);
161 }
162 
163 /* See common/environ.h. */
164 
165 char **
167 {
168  return const_cast<char **> (&m_environ_vector[0]);
169 }
170 
171 /* See common/environ.h. */
172 
173 const std::set<std::string> &
175 {
176  return m_user_set_env;
177 }
178 
179 const std::set<std::string> &
181 {
182  return m_user_unset_env;
183 }
const char * string
Definition: signals.c:50
const char * get(const char *var) const
Definition: environ.c:92
static void xfree(T *ptr)
Definition: common-utils.h:54
void set(const char *var, const char *value)
Definition: environ.c:106
static gdb_environ from_host_environ()
Definition: environ.c:44
const std::set< std::string > & user_unset_env() const
Definition: environ.c:180
char * xstrdup(const char *s)
Definition: utils.c:44
void clear()
Definition: environ.c:65
std::vector< char * > m_environ_vector
Definition: environ.h:94
static bool match_var_in_string(const char *string, const char *var, size_t var_len)
Definition: environ.c:81
char ** environ
std::set< std::string > m_user_set_env
Definition: environ.h:97
gdb_environ & operator=(gdb_environ &&e)
Definition: environ.c:26
const std::set< std::string > & user_set_env() const
Definition: environ.c:174
char ** envp() const
Definition: environ.c:166
void unset(const char *var)
Definition: environ.c:158
std::set< std::string > m_user_unset_env
Definition: environ.h:100