2003-06-11 David Carlton <carlton@bactrian.org>
[deliverable/binutils-gdb.git] / gdb / environ.c
1 /* environ.c -- library for manipulating environments for GNU.
2 Copyright 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 2000
3 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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #define min(a, b) ((a) < (b) ? (a) : (b))
21 #define max(a, b) ((a) > (b) ? (a) : (b))
22
23 #include "defs.h"
24 #include "environ.h"
25 #include "gdb_string.h"
26 \f
27
28 /* Return a new environment object. */
29
30 struct environ *
31 make_environ (void)
32 {
33 register struct environ *e;
34
35 e = (struct environ *) xmalloc (sizeof (struct environ));
36
37 e->allocated = 10;
38 e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
39 e->vector[0] = 0;
40 return e;
41 }
42
43 /* Free an environment and all the strings in it. */
44
45 void
46 free_environ (register struct environ *e)
47 {
48 register char **vector = e->vector;
49
50 while (*vector)
51 xfree (*vector++);
52
53 xfree (e);
54 }
55
56 /* Copy the environment given to this process into E.
57 Also copies all the strings in it, so we can be sure
58 that all strings in these environments are safe to free. */
59
60 void
61 init_environ (register struct environ *e)
62 {
63 extern char **environ;
64 register int i;
65
66 if (environ == NULL)
67 return;
68
69 for (i = 0; environ[i]; i++) /*EMPTY */ ;
70
71 if (e->allocated < i)
72 {
73 e->allocated = max (i, e->allocated + 10);
74 e->vector = (char **) xrealloc ((char *) e->vector,
75 (e->allocated + 1) * sizeof (char *));
76 }
77
78 memcpy (e->vector, environ, (i + 1) * sizeof (char *));
79
80 while (--i >= 0)
81 {
82 register int len = strlen (e->vector[i]);
83 register char *new = (char *) xmalloc (len + 1);
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
92 char **
93 environ_vector (struct environ *e)
94 {
95 return e->vector;
96 }
97 \f
98 /* Return the value in environment E of variable VAR. */
99
100 char *
101 get_in_environ (const struct environ *e, const char *var)
102 {
103 register int len = strlen (var);
104 register char **vector = e->vector;
105 register char *s;
106
107 for (; (s = *vector) != NULL; vector++)
108 if (STREQN (s, var, len) && s[len] == '=')
109 return &s[len + 1];
110
111 return 0;
112 }
113
114 /* Store the value in E of VAR as VALUE. */
115
116 void
117 set_in_environ (struct environ *e, const char *var, const char *value)
118 {
119 register int i;
120 register int len = strlen (var);
121 register char **vector = e->vector;
122 register char *s;
123
124 for (i = 0; (s = vector[i]) != NULL; i++)
125 if (STREQN (s, var, len) && s[len] == '=')
126 break;
127
128 if (s == 0)
129 {
130 if (i == e->allocated)
131 {
132 e->allocated += 10;
133 vector = (char **) xrealloc ((char *) vector,
134 (e->allocated + 1) * sizeof (char *));
135 e->vector = vector;
136 }
137 vector[i + 1] = 0;
138 }
139 else
140 xfree (s);
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
163 void
164 unset_in_environ (struct environ *e, char *var)
165 {
166 register int len = strlen (var);
167 register char **vector = e->vector;
168 register char *s;
169
170 for (; (s = *vector) != NULL; vector++)
171 {
172 if (STREQN (s, var, len) && s[len] == '=')
173 {
174 xfree (s);
175 /* Walk through the vector, shuffling args down by one, including
176 the NULL terminator. Can't use memcpy() here since the regions
177 overlap, and memmove() might not be available. */
178 while ((vector[0] = vector[1]) != NULL)
179 {
180 vector++;
181 }
182 break;
183 }
184 }
185 }
This page took 0.042166 seconds and 4 git commands to generate.