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., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #define min(a, b) ((a) < (b) ? (a) : (b))
20 #define max(a, b) ((a) > (b) ? (a) : (b))
24 #include "gdb_string.h"
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
;
71 for (i
= 0; environ
[i
]; i
++) /*EMPTY */ ;
75 e
->allocated
= max (i
, e
->allocated
+ 10);
76 e
->vector
= (char **) xrealloc ((char *) e
->vector
,
77 (e
->allocated
+ 1) * sizeof (char *));
80 memcpy (e
->vector
, environ
, (i
+ 1) * sizeof (char *));
84 register int len
= strlen (e
->vector
[i
]);
85 register char *new = (char *) xmalloc (len
+ 1);
86 memcpy (new, e
->vector
[i
], len
+ 1);
91 /* Return the vector of environment E.
92 This is used to get something to pass to execve. */
101 /* Return the value in environment E of variable VAR. */
104 get_in_environ (e
, var
)
105 const struct environ
*e
;
108 register int len
= strlen (var
);
109 register char **vector
= e
->vector
;
112 for (; (s
= *vector
) != NULL
; vector
++)
113 if (STREQN (s
, var
, len
) && s
[len
] == '=')
119 /* Store the value in E of VAR as VALUE. */
122 set_in_environ (e
, var
, value
)
128 register int len
= strlen (var
);
129 register char **vector
= e
->vector
;
132 for (i
= 0; (s
= vector
[i
]) != NULL
; i
++)
133 if (STREQN (s
, var
, len
) && s
[len
] == '=')
138 if (i
== e
->allocated
)
141 vector
= (char **) xrealloc ((char *) vector
,
142 (e
->allocated
+ 1) * sizeof (char *));
150 s
= (char *) xmalloc (len
+ strlen (value
) + 2);
156 /* This used to handle setting the PATH and GNUTARGET variables
157 specially. The latter has been replaced by "set gnutarget"
158 (which has worked since GDB 4.11). The former affects searching
159 the PATH to find SHELL, and searching the PATH to find the
160 argument of "symbol-file" or "exec-file". Maybe we should have
161 some kind of "set exec-path" for that. But in any event, having
162 "set env" affect anything besides the inferior is a bad idea.
163 What if we want to change the environment we pass to the program
164 without afecting GDB's behavior? */
169 /* Remove the setting for variable VAR from environment E. */
172 unset_in_environ (e
, var
)
176 register int len
= strlen (var
);
177 register char **vector
= e
->vector
;
180 for (; (s
= *vector
) != NULL
; vector
++)
182 if (STREQN (s
, var
, len
) && s
[len
] == '=')
185 /* Walk through the vector, shuffling args down by one, including
186 the NULL terminator. Can't use memcpy() here since the regions
187 overlap, and memmove() might not be available. */
188 while ((vector
[0] = vector
[1]) != NULL
)