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