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 (void) 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 (void) 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 (!strncmp (s, var, len)
116 /* Store the value in E of VAR as VALUE. */
119 set_in_environ (e, var, value)
125 register int len = strlen (var);
126 register char **vector = e->vector;
129 for (i = 0; (s = vector[i]) != NULL; i++)
130 if (!strncmp (s, var, len)
136 if (i == e->allocated)
139 vector = (char **) xrealloc ((char *)vector,
140 (e->allocated + 1) * sizeof (char *));
148 s = (char *) xmalloc (len + strlen (value) + 2);
154 /* Certain variables get exported back to the parent (e.g. our)
156 if (!strcmp(var, "PATH") /* Object file location */
157 || !strcmp (var, "G960BASE") /* Intel 960 downloads */
158 || !strcmp (var, "G960BIN") /* Intel 960 downloads */
159 || !strcmp (var, "GNUTARGET") /* BFD object file type */
161 putenv (strsave (s));
166 /* Remove the setting for variable VAR from environment E. */
169 unset_in_environ (e, var)
173 register int len = strlen (var);
174 register char **vector = e->vector;
177 for (; (s = *vector) != NULL; vector++)
178 if (!strncmp (s, var, len)
182 (void) memcpy (vector, vector + 1,
183 (e->allocated - (vector - e->vector)) * sizeof (char *));
184 e->vector[e->allocated - 1] = 0;