1 /* environ.c -- library for manipulating environments for GNU.
3 Copyright (C) 1986-2017 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/>. */
23 /* Return a new environment object. */
28 struct gdb_environ
*e
;
30 e
= XNEW (struct gdb_environ
);
33 e
->vector
= (char **) xmalloc ((e
->allocated
+ 1) * sizeof (char *));
38 /* Free an environment and all the strings in it. */
41 free_environ (struct gdb_environ
*e
)
43 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. */
57 init_environ (struct gdb_environ
*e
)
59 extern char **environ
;
65 for (i
= 0; environ
[i
]; i
++) /*EMPTY */ ;
69 e
->allocated
= std::max (i
, e
->allocated
+ 10);
70 e
->vector
= (char **) xrealloc ((char *) e
->vector
,
71 (e
->allocated
+ 1) * sizeof (char *));
74 memcpy (e
->vector
, environ
, (i
+ 1) * sizeof (char *));
78 int len
= strlen (e
->vector
[i
]);
79 char *newobj
= (char *) xmalloc (len
+ 1);
81 memcpy (newobj
, e
->vector
[i
], len
+ 1);
82 e
->vector
[i
] = newobj
;
86 /* Return the vector of environment E.
87 This is used to get something to pass to execve. */
90 environ_vector (struct gdb_environ
*e
)
95 /* Return the value in environment E of variable VAR. */
98 get_in_environ (const struct gdb_environ
*e
, const char *var
)
100 int len
= strlen (var
);
101 char **vector
= e
->vector
;
104 for (; (s
= *vector
) != NULL
; vector
++)
105 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
111 /* Store the value in E of VAR as VALUE. */
114 set_in_environ (struct gdb_environ
*e
, const char *var
, const char *value
)
117 int len
= strlen (var
);
118 char **vector
= e
->vector
;
121 for (i
= 0; (s
= vector
[i
]) != NULL
; i
++)
122 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
127 if (i
== e
->allocated
)
130 vector
= (char **) xrealloc ((char *) vector
,
131 (e
->allocated
+ 1) * sizeof (char *));
139 s
= (char *) xmalloc (len
+ strlen (value
) + 2);
145 /* This used to handle setting the PATH and GNUTARGET variables
146 specially. The latter has been replaced by "set gnutarget"
147 (which has worked since GDB 4.11). The former affects searching
148 the PATH to find SHELL, and searching the PATH to find the
149 argument of "symbol-file" or "exec-file". Maybe we should have
150 some kind of "set exec-path" for that. But in any event, having
151 "set env" affect anything besides the inferior is a bad idea.
152 What if we want to change the environment we pass to the program
153 without afecting GDB's behavior? */
158 /* Remove the setting for variable VAR from environment E. */
161 unset_in_environ (struct gdb_environ
*e
, const char *var
)
163 int len
= strlen (var
);
164 char **vector
= e
->vector
;
167 for (; (s
= *vector
) != NULL
; vector
++)
169 if (strncmp (s
, var
, len
) == 0 && s
[len
] == '=')
172 /* Walk through the vector, shuffling args down by one, including
173 the NULL terminator. Can't use memcpy() here since the regions
174 overlap, and memmove() might not be available. */
175 while ((vector
[0] = vector
[1]) != NULL
)
This page took 0.033033 seconds and 4 git commands to generate.