1 /* environ.c -- library for manipulating environments for GNU.
3 Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 2000, 2005
4 2003 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
21 #define min(a, b) ((a) < (b) ? (a) : (b))
22 #define max(a, b) ((a) > (b) ? (a) : (b))
26 #include "gdb_string.h"
29 /* Return a new environment object. */
34 struct gdb_environ
*e
;
36 e
= (struct gdb_environ
*) xmalloc (sizeof (struct gdb_environ
));
39 e
->vector
= (char **) xmalloc ((e
->allocated
+ 1) * sizeof (char *));
44 /* Free an environment and all the strings in it. */
47 free_environ (struct gdb_environ
*e
)
49 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. */
62 init_environ (struct gdb_environ
*e
)
64 extern char **environ
;
70 for (i
= 0; environ
[i
]; i
++) /*EMPTY */ ;
74 e
->allocated
= max (i
, e
->allocated
+ 10);
75 e
->vector
= (char **) xrealloc ((char *) e
->vector
,
76 (e
->allocated
+ 1) * sizeof (char *));
79 memcpy (e
->vector
, environ
, (i
+ 1) * sizeof (char *));
83 int len
= strlen (e
->vector
[i
]);
84 char *new = (char *) xmalloc (len
+ 1);
85 memcpy (new, e
->vector
[i
], len
+ 1);
90 /* Return the vector of environment E.
91 This is used to get something to pass to execve. */
94 environ_vector (struct gdb_environ
*e
)
99 /* Return the value in environment E of variable VAR. */
102 get_in_environ (const struct gdb_environ
*e
, const char *var
)
104 int len
= strlen (var
);
105 char **vector
= e
->vector
;
108 for (; (s
= *vector
) != NULL
; vector
++)
109 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
115 /* Store the value in E of VAR as VALUE. */
118 set_in_environ (struct gdb_environ
*e
, const char *var
, const char *value
)
121 int len
= strlen (var
);
122 char **vector
= e
->vector
;
125 for (i
= 0; (s
= vector
[i
]) != NULL
; i
++)
126 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
131 if (i
== e
->allocated
)
134 vector
= (char **) xrealloc ((char *) vector
,
135 (e
->allocated
+ 1) * sizeof (char *));
143 s
= (char *) xmalloc (len
+ strlen (value
) + 2);
149 /* This used to handle setting the PATH and GNUTARGET variables
150 specially. The latter has been replaced by "set gnutarget"
151 (which has worked since GDB 4.11). The former affects searching
152 the PATH to find SHELL, and searching the PATH to find the
153 argument of "symbol-file" or "exec-file". Maybe we should have
154 some kind of "set exec-path" for that. But in any event, having
155 "set env" affect anything besides the inferior is a bad idea.
156 What if we want to change the environment we pass to the program
157 without afecting GDB's behavior? */
162 /* Remove the setting for variable VAR from environment E. */
165 unset_in_environ (struct gdb_environ
*e
, char *var
)
167 int len
= strlen (var
);
168 char **vector
= e
->vector
;
171 for (; (s
= *vector
) != NULL
; vector
++)
173 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
176 /* Walk through the vector, shuffling args down by one, including
177 the NULL terminator. Can't use memcpy() here since the regions
178 overlap, and memmove() might not be available. */
179 while ((vector
[0] = vector
[1]) != NULL
)
This page took 0.034664 seconds and 4 git commands to generate.