2d1395719b719bf408689ca20a4b9b74b7bda6cb
[deliverable/binutils-gdb.git] / gdb / common / environ.c
1 /* environ.c -- library for manipulating environments for GNU.
2
3 Copyright (C) 1986-2017 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
25 gdb_environ &
26 gdb_environ::operator= (gdb_environ &&e)
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 e.m_environ_vector.clear ();
34 e.m_environ_vector.push_back (NULL);
35 return *this;
36 }
37
38 /* See common/environ.h. */
39
40 gdb_environ gdb_environ::from_host_environ ()
41 {
42 extern char **environ;
43 gdb_environ e;
44
45 if (environ == NULL)
46 return e;
47
48 for (int i = 0; environ[i] != NULL; ++i)
49 {
50 /* Make sure we add the element before the last (NULL). */
51 e.m_environ_vector.insert (e.m_environ_vector.end () - 1,
52 xstrdup (environ[i]));
53 }
54
55 return e;
56 }
57
58 /* See common/environ.h. */
59
60 void
61 gdb_environ::clear ()
62 {
63 for (char *v : m_environ_vector)
64 xfree (v);
65 m_environ_vector.clear ();
66 /* Always add the NULL element. */
67 m_environ_vector.push_back (NULL);
68 }
69
70 /* Helper function to check if STRING contains an environment variable
71 assignment of VAR, i.e., if STRING starts with 'VAR='. Return true
72 if it contains, false otherwise. */
73
74 static bool
75 match_var_in_string (char *string, const char *var, size_t var_len)
76 {
77 if (strncmp (string, var, var_len) == 0 && string[var_len] == '=')
78 return true;
79
80 return false;
81 }
82
83 /* See common/environ.h. */
84
85 const char *
86 gdb_environ::get (const char *var) const
87 {
88 size_t len = strlen (var);
89
90 for (char *el : m_environ_vector)
91 if (el != NULL && match_var_in_string (el, var, len))
92 return &el[len + 1];
93
94 return NULL;
95 }
96
97 /* See common/environ.h. */
98
99 void
100 gdb_environ::set (const char *var, const char *value)
101 {
102 /* We have to unset the variable in the vector if it exists. */
103 unset (var);
104
105 /* Insert the element before the last one, which is always NULL. */
106 m_environ_vector.insert (m_environ_vector.end () - 1,
107 concat (var, "=", value, NULL));
108 }
109
110 /* See common/environ.h. */
111
112 void
113 gdb_environ::unset (const char *var)
114 {
115 size_t len = strlen (var);
116
117 /* We iterate until '.cend () - 1' because the last element is
118 always NULL. */
119 for (std::vector<char *>::iterator el = m_environ_vector.begin ();
120 el != m_environ_vector.end () - 1;
121 ++el)
122 if (match_var_in_string (*el, var, len))
123 {
124 xfree (*el);
125 m_environ_vector.erase (el);
126 break;
127 }
128 }
129
130 /* See common/environ.h. */
131
132 char **
133 gdb_environ::envp () const
134 {
135 return const_cast<char **> (&m_environ_vector[0]);
136 }
This page took 0.035199 seconds and 4 git commands to generate.