C++: dlsym casts in gdb/linux-thread-db.c and gdb/gdbserver/thread-db.c
[deliverable/binutils-gdb.git] / gdb / environ.c
CommitLineData
c906108c 1/* environ.c -- library for manipulating environments for GNU.
69517000 2
32d0add0 3 Copyright (C) 1986-2015 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
SS
17
18#define min(a, b) ((a) < (b) ? (a) : (b))
19#define max(a, b) ((a) > (b) ? (a) : (b))
20
21#include "defs.h"
22#include "environ.h"
c906108c 23\f
c5aa993b 24
c906108c
SS
25/* Return a new environment object. */
26
1bf1958d 27struct gdb_environ *
fba45db2 28make_environ (void)
c906108c 29{
1bf1958d 30 struct gdb_environ *e;
c906108c 31
1bf1958d 32 e = (struct gdb_environ *) xmalloc (sizeof (struct gdb_environ));
c906108c
SS
33
34 e->allocated = 10;
35 e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
36 e->vector[0] = 0;
37 return e;
38}
39
40/* Free an environment and all the strings in it. */
41
42void
1bf1958d 43free_environ (struct gdb_environ *e)
c906108c 44{
52f0bd74 45 char **vector = e->vector;
c906108c
SS
46
47 while (*vector)
b8c9b27d 48 xfree (*vector++);
c906108c 49
9a61c7a6 50 xfree (e->vector);
b8c9b27d 51 xfree (e);
c906108c
SS
52}
53
54/* Copy the environment given to this process into E.
55 Also copies all the strings in it, so we can be sure
56 that all strings in these environments are safe to free. */
57
58void
1bf1958d 59init_environ (struct gdb_environ *e)
c906108c
SS
60{
61 extern char **environ;
52f0bd74 62 int i;
c906108c
SS
63
64 if (environ == NULL)
65 return;
66
c5aa993b 67 for (i = 0; environ[i]; i++) /*EMPTY */ ;
c906108c
SS
68
69 if (e->allocated < i)
70 {
71 e->allocated = max (i, e->allocated + 10);
c5aa993b 72 e->vector = (char **) xrealloc ((char *) e->vector,
c906108c
SS
73 (e->allocated + 1) * sizeof (char *));
74 }
75
76 memcpy (e->vector, environ, (i + 1) * sizeof (char *));
77
78 while (--i >= 0)
79 {
aa1ee363 80 int len = strlen (e->vector[i]);
fe978cb0 81 char *newobj = (char *) xmalloc (len + 1);
d7f9d729 82
fe978cb0
PA
83 memcpy (newobj, e->vector[i], len + 1);
84 e->vector[i] = newobj;
c906108c
SS
85 }
86}
87
88/* Return the vector of environment E.
89 This is used to get something to pass to execve. */
90
91char **
1bf1958d 92environ_vector (struct gdb_environ *e)
c906108c
SS
93{
94 return e->vector;
95}
96\f
97/* Return the value in environment E of variable VAR. */
98
99char *
1bf1958d 100get_in_environ (const struct gdb_environ *e, const char *var)
c906108c 101{
52f0bd74
AC
102 int len = strlen (var);
103 char **vector = e->vector;
104 char *s;
c906108c
SS
105
106 for (; (s = *vector) != NULL; vector++)
bf896cb0 107 if (strncmp (s, var, len) == 0 && s[len] == '=')
c906108c
SS
108 return &s[len + 1];
109
110 return 0;
111}
112
113/* Store the value in E of VAR as VALUE. */
114
115void
1bf1958d 116set_in_environ (struct gdb_environ *e, const char *var, const char *value)
c906108c 117{
52f0bd74
AC
118 int i;
119 int len = strlen (var);
120 char **vector = e->vector;
121 char *s;
c906108c
SS
122
123 for (i = 0; (s = vector[i]) != NULL; i++)
bf896cb0 124 if (strncmp (s, var, len) == 0 && s[len] == '=')
c906108c
SS
125 break;
126
127 if (s == 0)
128 {
129 if (i == e->allocated)
130 {
131 e->allocated += 10;
c5aa993b 132 vector = (char **) xrealloc ((char *) vector,
c906108c
SS
133 (e->allocated + 1) * sizeof (char *));
134 e->vector = vector;
135 }
136 vector[i + 1] = 0;
137 }
138 else
b8c9b27d 139 xfree (s);
c906108c
SS
140
141 s = (char *) xmalloc (len + strlen (value) + 2);
142 strcpy (s, var);
143 strcat (s, "=");
144 strcat (s, value);
145 vector[i] = s;
146
147 /* This used to handle setting the PATH and GNUTARGET variables
148 specially. The latter has been replaced by "set gnutarget"
149 (which has worked since GDB 4.11). The former affects searching
150 the PATH to find SHELL, and searching the PATH to find the
151 argument of "symbol-file" or "exec-file". Maybe we should have
152 some kind of "set exec-path" for that. But in any event, having
153 "set env" affect anything besides the inferior is a bad idea.
154 What if we want to change the environment we pass to the program
155 without afecting GDB's behavior? */
156
157 return;
158}
159
160/* Remove the setting for variable VAR from environment E. */
161
162void
41c77899 163unset_in_environ (struct gdb_environ *e, const char *var)
c906108c 164{
52f0bd74
AC
165 int len = strlen (var);
166 char **vector = e->vector;
167 char *s;
c906108c
SS
168
169 for (; (s = *vector) != NULL; vector++)
170 {
591e78ff 171 if (strncmp (s, var, len) == 0 && s[len] == '=')
c906108c 172 {
b8c9b27d 173 xfree (s);
c906108c
SS
174 /* Walk through the vector, shuffling args down by one, including
175 the NULL terminator. Can't use memcpy() here since the regions
0963b4bd 176 overlap, and memmove() might not be available. */
c906108c
SS
177 while ((vector[0] = vector[1]) != NULL)
178 {
179 vector++;
180 }
181 break;
182 }
183 }
184}
This page took 1.016236 seconds and 4 git commands to generate.