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 */
160 || !strcmp (var
, "GNUTARGET") /* BFD object file type */
162 putenv (strsave (s
));
167 /* Remove the setting for variable VAR from environment E. */
170 unset_in_environ (e
, var
)
174 register int len
= strlen (var
);
175 register char **vector
= e
->vector
;
178 for (; s
= *vector
; vector
++)
179 if (!strncmp (s
, var
, len
)
183 bcopy (vector
+ 1, vector
,
184 (e
->allocated
- (vector
- e
->vector
)) * sizeof (char *));
185 e
->vector
[e
->allocated
- 1] = 0;
This page took 0.032788 seconds and 4 git commands to generate.