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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #define min(a, b) ((a) < (b) ? (a) : (b))
19 #define max(a, b) ((a) > (b) ? (a) : (b))
24 #include "defs.h" /* For strsave(). */
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;
67 for (i = 0; environ[i]; i++) /*EMPTY*/;
71 e->allocated = max (i, e->allocated + 10);
72 e->vector = (char **) xrealloc ((char *)e->vector,
73 (e->allocated + 1) * sizeof (char *));
76 memcpy (e->vector, environ, (i + 1) * sizeof (char *));
80 register int len = strlen (e->vector[i]);
81 register char *new = (char *) xmalloc (len + 1);
82 memcpy (new, e->vector[i], len + 1);
87 /* Return the vector of environment E.
88 This is used to get something to pass to execve. */
97 /* Return the value in environment E of variable VAR. */
100 get_in_environ (e, var)
101 const struct environ *e;
104 register int len = strlen (var);
105 register char **vector = e->vector;
108 for (; (s = *vector) != NULL; vector++)
109 if (STREQN (s, var, len) && s[len] == '=')
115 /* Store the value in E of VAR as VALUE. */
118 set_in_environ (e, var, value)
124 register int len = strlen (var);
125 register char **vector = e->vector;
128 for (i = 0; (s = vector[i]) != NULL; i++)
129 if (STREQN (s, var, len) && s[len] == '=')
134 if (i == e->allocated)
137 vector = (char **) xrealloc ((char *)vector,
138 (e->allocated + 1) * sizeof (char *));
146 s = (char *) xmalloc (len + strlen (value) + 2);
152 /* Certain variables get exported back to the parent (e.g. our)
153 environment, too. FIXME: this is a hideous hack and should not be
154 allowed to live. What if we want to change the environment we pass to
155 the program without affecting GDB's behavior? */
156 if (STREQ(var, "PATH") /* Object file location */
157 || STREQ (var, "G960BASE") /* Intel 960 downloads */
158 || STREQ (var, "G960BIN") /* Intel 960 downloads */
161 putenv (strsave (s));
164 /* This is a compatibility hack, since GDB 4.10 and older didn't have
165 `set gnutarget'. Eventually it should go away, so that (for example)
166 you can debug objdump's handling of GNUTARGET without affecting GDB's
168 if (STREQ (var, "GNUTARGET"))
170 set_gnutarget (value);
175 /* Remove the setting for variable VAR from environment E. */
178 unset_in_environ (e, var)
182 register int len = strlen (var);
183 register char **vector = e->vector;
186 for (; (s = *vector) != NULL; vector++)
188 if (STREQN (s, var, len) && s[len] == '=')
191 /* Walk through the vector, shuffling args down by one, including
192 the NULL terminator. Can't use memcpy() here since the regions
193 overlap, and memmove() might not be available. */
194 while ((vector[0] = vector[1]) != NULL)