1 /* environ.c -- library for manipulating environments for GNU.
2 Copyright (C) 1986, 1989 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #define min(a, b) ((a) < (b) ? (a) : (b))
19 #define max(a, b) ((a) > (b) ? (a) : (b))
23 #include "gdb_string.h"
27 /* Return a new environment object. */
32 register struct environ *e;
34 e = (struct environ *) xmalloc (sizeof (struct environ));
37 e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
42 /* Free an environment and all the strings in it. */
46 register struct environ *e;
48 register char **vector = e->vector;
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. */
62 register struct environ *e;
64 extern char **environ;
70 for (i = 0; environ[i]; i++) /*EMPTY*/;
74 e->allocated = max (i, e->allocated + 10);
75 e->vector = (char **) xrealloc ((char *)e->vector,
76 (e->allocated + 1) * sizeof (char *));
79 memcpy (e->vector, environ, (i + 1) * sizeof (char *));
83 register int len = strlen (e->vector[i]);
84 register char *new = (char *) xmalloc (len + 1);
85 memcpy (new, e->vector[i], len + 1);
90 /* Return the vector of environment E.
91 This is used to get something to pass to execve. */
100 /* Return the value in environment E of variable VAR. */
103 get_in_environ (e, var)
104 const struct environ *e;
107 register int len = strlen (var);
108 register char **vector = e->vector;
111 for (; (s = *vector) != NULL; vector++)
112 if (STREQN (s, var, len) && s[len] == '=')
118 /* Store the value in E of VAR as VALUE. */
121 set_in_environ (e, var, value)
127 register int len = strlen (var);
128 register char **vector = e->vector;
131 for (i = 0; (s = vector[i]) != NULL; i++)
132 if (STREQN (s, var, len) && s[len] == '=')
137 if (i == e->allocated)
140 vector = (char **) xrealloc ((char *)vector,
141 (e->allocated + 1) * sizeof (char *));
149 s = (char *) xmalloc (len + strlen (value) + 2);
155 /* This used to handle setting the PATH and GNUTARGET variables
156 specially. The latter has been replaced by "set gnutarget"
157 (which has worked since GDB 4.11). The former affects searching
158 the PATH to find SHELL, and searching the PATH to find the
159 argument of "symbol-file" or "exec-file". Maybe we should have
160 some kind of "set exec-path" for that. But in any event, having
161 "set env" affect anything besides the inferior is a bad idea.
162 What if we want to change the environment we pass to the program
163 without afecting GDB's behavior? */
168 /* Remove the setting for variable VAR from environment E. */
171 unset_in_environ (e, var)
175 register int len = strlen (var);
176 register char **vector = e->vector;
179 for (; (s = *vector) != NULL; vector++)
181 if (STREQN (s, var, len) && s[len] == '=')
184 /* Walk through the vector, shuffling args down by one, including
185 the NULL terminator. Can't use memcpy() here since the regions
186 overlap, and memmove() might not be available. */
187 while ((vector[0] = vector[1]) != NULL)