1 /* environ.c -- library for manipulating environments for GNU.
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #define min(a, b) ((a) < (b) ? (a) : (b))
19 #define max(a, b) ((a) > (b) ? (a) : (b))
23 #include "gdb_string.h"
26 /* Return a new environment object. */
31 struct gdb_environ
*e
;
33 e
= (struct gdb_environ
*) xmalloc (sizeof (struct gdb_environ
));
36 e
->vector
= (char **) xmalloc ((e
->allocated
+ 1) * sizeof (char *));
41 /* Free an environment and all the strings in it. */
44 free_environ (struct gdb_environ
*e
)
46 char **vector
= e
->vector
;
55 /* Copy the environment given to this process into E.
56 Also copies all the strings in it, so we can be sure
57 that all strings in these environments are safe to free. */
60 init_environ (struct gdb_environ
*e
)
62 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 memcpy (e
->vector
, environ
, (i
+ 1) * sizeof (char *));
81 int len
= strlen (e
->vector
[i
]);
82 char *new = (char *) xmalloc (len
+ 1);
84 memcpy (new, e
->vector
[i
], len
+ 1);
89 /* Return the vector of environment E.
90 This is used to get something to pass to execve. */
93 environ_vector (struct gdb_environ
*e
)
98 /* Return the value in environment E of variable VAR. */
101 get_in_environ (const struct gdb_environ
*e
, const char *var
)
103 int len
= strlen (var
);
104 char **vector
= e
->vector
;
107 for (; (s
= *vector
) != NULL
; vector
++)
108 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
114 /* Store the value in E of VAR as VALUE. */
117 set_in_environ (struct gdb_environ
*e
, const char *var
, const char *value
)
120 int len
= strlen (var
);
121 char **vector
= e
->vector
;
124 for (i
= 0; (s
= vector
[i
]) != NULL
; i
++)
125 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
130 if (i
== e
->allocated
)
133 vector
= (char **) xrealloc ((char *) vector
,
134 (e
->allocated
+ 1) * sizeof (char *));
142 s
= (char *) xmalloc (len
+ strlen (value
) + 2);
148 /* This used to handle setting the PATH and GNUTARGET variables
149 specially. The latter has been replaced by "set gnutarget"
150 (which has worked since GDB 4.11). The former affects searching
151 the PATH to find SHELL, and searching the PATH to find the
152 argument of "symbol-file" or "exec-file". Maybe we should have
153 some kind of "set exec-path" for that. But in any event, having
154 "set env" affect anything besides the inferior is a bad idea.
155 What if we want to change the environment we pass to the program
156 without afecting GDB's behavior? */
161 /* Remove the setting for variable VAR from environment E. */
164 unset_in_environ (struct gdb_environ
*e
, char *var
)
166 int len
= strlen (var
);
167 char **vector
= e
->vector
;
170 for (; (s
= *vector
) != NULL
; vector
++)
172 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
175 /* Walk through the vector, shuffling args down by one, including
176 the NULL terminator. Can't use memcpy() here since the regions
177 overlap, and memmove() might not be available. */
178 while ((vector
[0] = vector
[1]) != NULL
)
This page took 0.035036 seconds and 4 git commands to generate.