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))
23 /* Return a new environment object. */
28 register struct environ
*e
;
30 e
= (struct environ
*) xmalloc (sizeof (struct environ
));
33 e
->vector
= (char **) xmalloc ((e
->allocated
+ 1) * sizeof (char *));
38 /* Free an environment and all the strings in it. */
42 register struct environ
*e
;
44 register char **vector
= e
->vector
;
52 /* Copy the environment given to this process into E.
53 Also copies all the strings in it, so we can be sure
54 that all strings in these environments are safe to free. */
58 register struct environ
*e
;
60 extern char **environ
;
63 for (i
= 0; environ
[i
]; i
++);
67 e
->allocated
= max (i
, e
->allocated
+ 10);
68 e
->vector
= (char **) xrealloc (e
->vector
,
69 (e
->allocated
+ 1) * sizeof (char *));
72 bcopy (environ
, e
->vector
, (i
+ 1) * sizeof (char *));
76 register int len
= strlen (e
->vector
[i
]);
77 register char *new = (char *) xmalloc (len
+ 1);
78 bcopy (e
->vector
[i
], new, len
);
83 /* Return the vector of environment E.
84 This is used to get something to pass to execve. */
93 /* Return the value in environment E of variable VAR. */
96 get_in_environ (e
, var
)
100 register int len
= strlen (var
);
101 register char **vector
= e
->vector
;
104 for (; s
= *vector
; vector
++)
105 if (!strncmp (s
, var
, len
)
112 /* Store the value in E of VAR as VALUE. */
115 set_in_environ (e
, var
, value
)
121 register int len
= strlen (var
);
122 register char **vector
= e
->vector
;
125 for (i
= 0; s
= vector
[i
]; i
++)
126 if (!strncmp (s
, var
, len
)
132 if (i
== e
->allocated
)
135 vector
= (char **) xrealloc (vector
,
136 (e
->allocated
+ 1) * sizeof (char *));
144 s
= (char *) xmalloc (len
+ strlen (value
) + 2);
152 /* Remove the setting for variable VAR from environment E. */
155 unset_in_environ (e
, var
)
159 register int len
= strlen (var
);
160 register char **vector
= e
->vector
;
163 for (; s
= *vector
; vector
++)
164 if (!strncmp (s
, var
, len
)
168 bcopy (vector
+ 1, vector
,
169 (e
->allocated
- (vector
- e
->vector
)) * sizeof (char *));
170 e
->vector
[e
->allocated
- 1] = 0;