]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* environ.c -- library for manipulating environments for GNU. |
69517000 | 2 | |
61baf725 | 3 | Copyright (C) 1986-2017 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b JM |
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 | |
a9762ec7 | 7 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 8 | (at your option) any later version. |
c906108c | 9 | |
c5aa993b JM |
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. | |
c906108c | 14 | |
c5aa993b | 15 | You should have received a copy of the GNU General Public License |
a9762ec7 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c | 17 | |
1672e0d9 | 18 | #include "common-defs.h" |
c906108c | 19 | #include "environ.h" |
325fac50 | 20 | #include <algorithm> |
9a6c7d9c | 21 | #include <utility> |
c5aa993b | 22 | |
9a6c7d9c | 23 | /* See common/environ.h. */ |
c906108c | 24 | |
9a6c7d9c SDJ |
25 | gdb_environ & |
26 | gdb_environ::operator= (gdb_environ &&e) | |
c906108c | 27 | { |
9a6c7d9c SDJ |
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; | |
c906108c SS |
36 | } |
37 | ||
9a6c7d9c | 38 | /* See common/environ.h. */ |
c906108c | 39 | |
9a6c7d9c | 40 | gdb_environ gdb_environ::from_host_environ () |
c906108c SS |
41 | { |
42 | extern char **environ; | |
9a6c7d9c | 43 | gdb_environ e; |
c906108c SS |
44 | |
45 | if (environ == NULL) | |
9a6c7d9c | 46 | return e; |
c906108c | 47 | |
9a6c7d9c | 48 | for (int i = 0; environ[i] != NULL; ++i) |
c906108c | 49 | { |
9a6c7d9c SDJ |
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])); | |
c906108c SS |
53 | } |
54 | ||
9a6c7d9c SDJ |
55 | return e; |
56 | } | |
c906108c | 57 | |
9a6c7d9c | 58 | /* See common/environ.h. */ |
d7f9d729 | 59 | |
9a6c7d9c SDJ |
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); | |
c906108c SS |
68 | } |
69 | ||
9a6c7d9c SDJ |
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. */ | |
c906108c | 73 | |
9a6c7d9c SDJ |
74 | static bool |
75 | match_var_in_string (char *string, const char *var, size_t var_len) | |
c906108c | 76 | { |
9a6c7d9c SDJ |
77 | if (strncmp (string, var, var_len) == 0 && string[var_len] == '=') |
78 | return true; | |
79 | ||
80 | return false; | |
c906108c | 81 | } |
c906108c | 82 | |
9a6c7d9c SDJ |
83 | /* See common/environ.h. */ |
84 | ||
85 | const char * | |
86 | gdb_environ::get (const char *var) const | |
c906108c | 87 | { |
9a6c7d9c | 88 | size_t len = strlen (var); |
c906108c | 89 | |
9a6c7d9c SDJ |
90 | for (char *el : m_environ_vector) |
91 | if (el != NULL && match_var_in_string (el, var, len)) | |
92 | return &el[len + 1]; | |
c906108c | 93 | |
9a6c7d9c | 94 | return NULL; |
c906108c SS |
95 | } |
96 | ||
9a6c7d9c | 97 | /* See common/environ.h. */ |
c906108c SS |
98 | |
99 | void | |
9a6c7d9c | 100 | gdb_environ::set (const char *var, const char *value) |
c906108c | 101 | { |
9a6c7d9c SDJ |
102 | /* We have to unset the variable in the vector if it exists. */ |
103 | unset (var); | |
c906108c | 104 | |
9a6c7d9c SDJ |
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)); | |
c906108c SS |
108 | } |
109 | ||
9a6c7d9c | 110 | /* See common/environ.h. */ |
c906108c SS |
111 | |
112 | void | |
9a6c7d9c | 113 | gdb_environ::unset (const char *var) |
c906108c | 114 | { |
9a6c7d9c SDJ |
115 | size_t len = strlen (var); |
116 | ||
d4c6ce5b | 117 | /* We iterate until '.end () - 1' because the last element is |
9a6c7d9c | 118 | always NULL. */ |
96160d60 SDJ |
119 | for (std::vector<char *>::iterator el = m_environ_vector.begin (); |
120 | el != m_environ_vector.end () - 1; | |
9a6c7d9c SDJ |
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 | } | |
c906108c | 129 | |
9a6c7d9c SDJ |
130 | /* See common/environ.h. */ |
131 | ||
132 | char ** | |
133 | gdb_environ::envp () const | |
134 | { | |
135 | return const_cast<char **> (&m_environ_vector[0]); | |
c906108c | 136 | } |