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