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 1, or (at your option)
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 extern char *xmalloc ();
25 extern char *xrealloc ();
28 /* Return a new environment object. */
33 register struct environ *e;
35 e = (struct environ *) xmalloc (sizeof (struct environ));
38 e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
43 /* Free an environment and all the strings in it. */
47 register struct environ *e;
49 register char **vector = e->vector;
57 /* Copy the environment given to this process into E.
58 Also copies all the strings in it, so we can be sure
59 that all strings in these environments are safe to free. */
63 register struct environ *e;
65 extern char **environ;
68 for (i = 0; environ[i]; i++) /*EMPTY*/;
72 e->allocated = max (i, e->allocated + 10);
73 e->vector = (char **) xrealloc ((char *)e->vector,
74 (e->allocated + 1) * sizeof (char *));
77 bcopy (environ, e->vector, (i + 1) * sizeof (char *));
81 register int len = strlen (e->vector[i]);
82 register char *new = (char *) xmalloc (len + 1);
83 bcopy (e->vector[i], new, len + 1);
88 /* Return the vector of environment E.
89 This is used to get something to pass to execve. */
98 /* Return the value in environment E of variable VAR. */
101 get_in_environ (e, var)
105 register int len = strlen (var);
106 register char **vector = e->vector;
109 for (; s = *vector; vector++)
110 if (!strncmp (s, var, len)
117 /* Store the value in E of VAR as VALUE. */
120 set_in_environ (e, var, value)
126 register int len = strlen (var);
127 register char **vector = e->vector;
130 for (i = 0; s = vector[i]; i++)
131 if (!strncmp (s, var, 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 /* Certain variables get exported back to the parent (e.g. our)
157 if (!strcmp(var, "PATH") /* Object file location */
158 || !strcmp (var, "G960BASE") /* Intel 960 downloads */
159 || !strcmp (var, "G960BIN") /* Intel 960 downloads */
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; vector++)
178 if (!strncmp (s, var, len)
182 bcopy (vector + 1, vector,
183 (e->allocated - (vector - e->vector)) * sizeof (char *));
184 e->vector[e->allocated - 1] = 0;