1999-04-16 09:35:26 +08:00
|
|
|
|
/* environ.c -- library for manipulating environments for GNU.
|
2003-09-15 06:35:33 +08:00
|
|
|
|
|
2005-12-18 06:34:03 +08:00
|
|
|
|
Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 2000, 2005
|
2007-01-10 01:59:20 +08:00
|
|
|
|
2003, 2007 Free Software Foundation, Inc.
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
1999-07-08 04:19:36 +08:00
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-08-24 02:08:50 +08:00
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
1999-07-08 04:19:36 +08:00
|
|
|
|
(at your option) any later version.
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
1999-07-08 04:19:36 +08:00
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
1999-07-08 04:19:36 +08:00
|
|
|
|
You should have received a copy of the GNU General Public License
|
2007-08-24 02:08:50 +08:00
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
#define min(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
|
#define max(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
|
#include "environ.h"
|
|
|
|
|
#include "gdb_string.h"
|
|
|
|
|
|
1999-07-08 04:19:36 +08:00
|
|
|
|
|
1999-04-16 09:35:26 +08:00
|
|
|
|
/* Return a new environment object. */
|
|
|
|
|
|
2005-03-09 06:17:34 +08:00
|
|
|
|
struct gdb_environ *
|
2000-07-30 09:48:28 +08:00
|
|
|
|
make_environ (void)
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
2005-03-09 06:17:34 +08:00
|
|
|
|
struct gdb_environ *e;
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
2005-03-09 06:17:34 +08:00
|
|
|
|
e = (struct gdb_environ *) xmalloc (sizeof (struct gdb_environ));
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
e->allocated = 10;
|
|
|
|
|
e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
|
|
|
|
|
e->vector[0] = 0;
|
|
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Free an environment and all the strings in it. */
|
|
|
|
|
|
|
|
|
|
void
|
2005-03-09 06:17:34 +08:00
|
|
|
|
free_environ (struct gdb_environ *e)
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
2003-09-14 Andrew Cagney <cagney@redhat.com>
* alpha-nat.c: Remove some occurances of "register".
* alpha-tdep.c, arm-tdep.c, blockframe.c, breakpoint.c: Ditto.
* buildsym.c, c-typeprint.c, c-valprint.c, coffread.c: Ditto.
* corefile.c, cp-support.c, cp-valprint.c, cris-tdep.c: Ditto.
* dbxread.c, dcache.c, dwarf2read.c, elfread.c: Ditto.
* environ.c, eval.c, event-top.c, f-typeprint.c: Ditto.
* f-valprint.c, findvar.c, frame.c, gdbtypes.c: Ditto.
* h8300-tdep.c, hppa-tdep.c, hppab-nat.c, hppah-nat.c: Ditto.
* hppam3-nat.c, hpread.c, ia64-aix-nat.c, ia64-linux-nat.c: Ditto.
* infcall.c, infcmd.c, inflow.c, infptrace.c, infrun.c: Ditto.
* infttrace.c, irix5-nat.c, jv-typeprint.c: Ditto.
* jv-valprint.c, m68k-tdep.c, m68klinux-nat.c, main.c: Ditto.
* mdebugread.c, minsyms.c, mips-linux-tdep.c: Ditto.
* mips-nat.c, mips-tdep.c, mipsread.c, mipsv4-nat.c: Ditto.
* ns32k-tdep.c, objfiles.c, p-typeprint.c: Ditto.
* p-valprint.c, ppc-linux-nat.c, printcmd.c: Ditto.
* remote-mips.c, remote-vx.c, rs6000-nat.c: Ditto.
* rs6000-tdep.c, scm-exp.c, sh-tdep.c, sh64-tdep.c: Ditto.
* solib.c, somread.c, source.c, sparc-tdep.c: Ditto.
* stabsread.c, stack.c, standalone.c, symfile.c: Ditto.
* symmisc.c, symtab.c, top.c, tracepoint.c: Ditto.
* typeprint.c, utils.c, valarith.c, valops.c: Ditto.
* values.c, vax-tdep.c, xcoffread.c: Ditto.
2003-09-15 00:32:14 +08:00
|
|
|
|
char **vector = e->vector;
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
while (*vector)
|
2000-12-15 09:01:51 +08:00
|
|
|
|
xfree (*vector++);
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
2000-12-15 09:01:51 +08:00
|
|
|
|
xfree (e);
|
1999-04-16 09:35:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Copy the environment given to this process into E.
|
|
|
|
|
Also copies all the strings in it, so we can be sure
|
|
|
|
|
that all strings in these environments are safe to free. */
|
|
|
|
|
|
|
|
|
|
void
|
2005-03-09 06:17:34 +08:00
|
|
|
|
init_environ (struct gdb_environ *e)
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
|
|
|
|
extern char **environ;
|
2003-09-14 Andrew Cagney <cagney@redhat.com>
* alpha-nat.c: Remove some occurances of "register".
* alpha-tdep.c, arm-tdep.c, blockframe.c, breakpoint.c: Ditto.
* buildsym.c, c-typeprint.c, c-valprint.c, coffread.c: Ditto.
* corefile.c, cp-support.c, cp-valprint.c, cris-tdep.c: Ditto.
* dbxread.c, dcache.c, dwarf2read.c, elfread.c: Ditto.
* environ.c, eval.c, event-top.c, f-typeprint.c: Ditto.
* f-valprint.c, findvar.c, frame.c, gdbtypes.c: Ditto.
* h8300-tdep.c, hppa-tdep.c, hppab-nat.c, hppah-nat.c: Ditto.
* hppam3-nat.c, hpread.c, ia64-aix-nat.c, ia64-linux-nat.c: Ditto.
* infcall.c, infcmd.c, inflow.c, infptrace.c, infrun.c: Ditto.
* infttrace.c, irix5-nat.c, jv-typeprint.c: Ditto.
* jv-valprint.c, m68k-tdep.c, m68klinux-nat.c, main.c: Ditto.
* mdebugread.c, minsyms.c, mips-linux-tdep.c: Ditto.
* mips-nat.c, mips-tdep.c, mipsread.c, mipsv4-nat.c: Ditto.
* ns32k-tdep.c, objfiles.c, p-typeprint.c: Ditto.
* p-valprint.c, ppc-linux-nat.c, printcmd.c: Ditto.
* remote-mips.c, remote-vx.c, rs6000-nat.c: Ditto.
* rs6000-tdep.c, scm-exp.c, sh-tdep.c, sh64-tdep.c: Ditto.
* solib.c, somread.c, source.c, sparc-tdep.c: Ditto.
* stabsread.c, stack.c, standalone.c, symfile.c: Ditto.
* symmisc.c, symtab.c, top.c, tracepoint.c: Ditto.
* typeprint.c, utils.c, valarith.c, valops.c: Ditto.
* values.c, vax-tdep.c, xcoffread.c: Ditto.
2003-09-15 00:32:14 +08:00
|
|
|
|
int i;
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
if (environ == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
1999-07-08 04:19:36 +08:00
|
|
|
|
for (i = 0; environ[i]; i++) /*EMPTY */ ;
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
if (e->allocated < i)
|
|
|
|
|
{
|
|
|
|
|
e->allocated = max (i, e->allocated + 10);
|
1999-07-08 04:19:36 +08:00
|
|
|
|
e->vector = (char **) xrealloc ((char *) e->vector,
|
1999-04-16 09:35:26 +08:00
|
|
|
|
(e->allocated + 1) * sizeof (char *));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy (e->vector, environ, (i + 1) * sizeof (char *));
|
|
|
|
|
|
|
|
|
|
while (--i >= 0)
|
|
|
|
|
{
|
2003-09-16 Andrew Cagney <cagney@redhat.com>
* buildsym.c: Remove more occurances of "register".
* coffread.c, dbxread.c, dcache.c, dwarf2read.c: Ditto.
* environ.c, eval.c, f-valprint.c, findvar.c: Ditto.
* gdbtypes.c, gnu-v2-abi.c, h8300-tdep.c, hppa-tdep.c: Ditto.
* infcmd.c, mdebugread.c, minsyms.c, mips-tdep.c: Ditto.
* printcmd.c, remote-vx.c, sh-stub.c, sh-tdep.c: Ditto.
* sh64-tdep.c, source.c, stabsread.c, stack.c: Ditto.
* standalone.c, symfile.c, symmisc.c, symtab.c: Ditto.
* utils.c, valops.c, values.c, xcoffread.c: Ditto.
2003-09-17 02:56:35 +08:00
|
|
|
|
int len = strlen (e->vector[i]);
|
|
|
|
|
char *new = (char *) xmalloc (len + 1);
|
1999-04-16 09:35:26 +08:00
|
|
|
|
memcpy (new, e->vector[i], len + 1);
|
|
|
|
|
e->vector[i] = new;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the vector of environment E.
|
|
|
|
|
This is used to get something to pass to execve. */
|
|
|
|
|
|
|
|
|
|
char **
|
2005-03-09 06:17:34 +08:00
|
|
|
|
environ_vector (struct gdb_environ *e)
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
|
|
|
|
return e->vector;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the value in environment E of variable VAR. */
|
|
|
|
|
|
|
|
|
|
char *
|
2005-03-09 06:17:34 +08:00
|
|
|
|
get_in_environ (const struct gdb_environ *e, const char *var)
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
2003-09-14 Andrew Cagney <cagney@redhat.com>
* alpha-nat.c: Remove some occurances of "register".
* alpha-tdep.c, arm-tdep.c, blockframe.c, breakpoint.c: Ditto.
* buildsym.c, c-typeprint.c, c-valprint.c, coffread.c: Ditto.
* corefile.c, cp-support.c, cp-valprint.c, cris-tdep.c: Ditto.
* dbxread.c, dcache.c, dwarf2read.c, elfread.c: Ditto.
* environ.c, eval.c, event-top.c, f-typeprint.c: Ditto.
* f-valprint.c, findvar.c, frame.c, gdbtypes.c: Ditto.
* h8300-tdep.c, hppa-tdep.c, hppab-nat.c, hppah-nat.c: Ditto.
* hppam3-nat.c, hpread.c, ia64-aix-nat.c, ia64-linux-nat.c: Ditto.
* infcall.c, infcmd.c, inflow.c, infptrace.c, infrun.c: Ditto.
* infttrace.c, irix5-nat.c, jv-typeprint.c: Ditto.
* jv-valprint.c, m68k-tdep.c, m68klinux-nat.c, main.c: Ditto.
* mdebugread.c, minsyms.c, mips-linux-tdep.c: Ditto.
* mips-nat.c, mips-tdep.c, mipsread.c, mipsv4-nat.c: Ditto.
* ns32k-tdep.c, objfiles.c, p-typeprint.c: Ditto.
* p-valprint.c, ppc-linux-nat.c, printcmd.c: Ditto.
* remote-mips.c, remote-vx.c, rs6000-nat.c: Ditto.
* rs6000-tdep.c, scm-exp.c, sh-tdep.c, sh64-tdep.c: Ditto.
* solib.c, somread.c, source.c, sparc-tdep.c: Ditto.
* stabsread.c, stack.c, standalone.c, symfile.c: Ditto.
* symmisc.c, symtab.c, top.c, tracepoint.c: Ditto.
* typeprint.c, utils.c, valarith.c, valops.c: Ditto.
* values.c, vax-tdep.c, xcoffread.c: Ditto.
2003-09-15 00:32:14 +08:00
|
|
|
|
int len = strlen (var);
|
|
|
|
|
char **vector = e->vector;
|
|
|
|
|
char *s;
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
for (; (s = *vector) != NULL; vector++)
|
2003-11-07 06:54:02 +08:00
|
|
|
|
if (strncmp (s, var, len) == 0 && s[len] == '=')
|
1999-04-16 09:35:26 +08:00
|
|
|
|
return &s[len + 1];
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Store the value in E of VAR as VALUE. */
|
|
|
|
|
|
|
|
|
|
void
|
2005-03-09 06:17:34 +08:00
|
|
|
|
set_in_environ (struct gdb_environ *e, const char *var, const char *value)
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
2003-09-14 Andrew Cagney <cagney@redhat.com>
* alpha-nat.c: Remove some occurances of "register".
* alpha-tdep.c, arm-tdep.c, blockframe.c, breakpoint.c: Ditto.
* buildsym.c, c-typeprint.c, c-valprint.c, coffread.c: Ditto.
* corefile.c, cp-support.c, cp-valprint.c, cris-tdep.c: Ditto.
* dbxread.c, dcache.c, dwarf2read.c, elfread.c: Ditto.
* environ.c, eval.c, event-top.c, f-typeprint.c: Ditto.
* f-valprint.c, findvar.c, frame.c, gdbtypes.c: Ditto.
* h8300-tdep.c, hppa-tdep.c, hppab-nat.c, hppah-nat.c: Ditto.
* hppam3-nat.c, hpread.c, ia64-aix-nat.c, ia64-linux-nat.c: Ditto.
* infcall.c, infcmd.c, inflow.c, infptrace.c, infrun.c: Ditto.
* infttrace.c, irix5-nat.c, jv-typeprint.c: Ditto.
* jv-valprint.c, m68k-tdep.c, m68klinux-nat.c, main.c: Ditto.
* mdebugread.c, minsyms.c, mips-linux-tdep.c: Ditto.
* mips-nat.c, mips-tdep.c, mipsread.c, mipsv4-nat.c: Ditto.
* ns32k-tdep.c, objfiles.c, p-typeprint.c: Ditto.
* p-valprint.c, ppc-linux-nat.c, printcmd.c: Ditto.
* remote-mips.c, remote-vx.c, rs6000-nat.c: Ditto.
* rs6000-tdep.c, scm-exp.c, sh-tdep.c, sh64-tdep.c: Ditto.
* solib.c, somread.c, source.c, sparc-tdep.c: Ditto.
* stabsread.c, stack.c, standalone.c, symfile.c: Ditto.
* symmisc.c, symtab.c, top.c, tracepoint.c: Ditto.
* typeprint.c, utils.c, valarith.c, valops.c: Ditto.
* values.c, vax-tdep.c, xcoffread.c: Ditto.
2003-09-15 00:32:14 +08:00
|
|
|
|
int i;
|
|
|
|
|
int len = strlen (var);
|
|
|
|
|
char **vector = e->vector;
|
|
|
|
|
char *s;
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
for (i = 0; (s = vector[i]) != NULL; i++)
|
2003-11-07 06:54:02 +08:00
|
|
|
|
if (strncmp (s, var, len) == 0 && s[len] == '=')
|
1999-04-16 09:35:26 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (s == 0)
|
|
|
|
|
{
|
|
|
|
|
if (i == e->allocated)
|
|
|
|
|
{
|
|
|
|
|
e->allocated += 10;
|
1999-07-08 04:19:36 +08:00
|
|
|
|
vector = (char **) xrealloc ((char *) vector,
|
1999-04-16 09:35:26 +08:00
|
|
|
|
(e->allocated + 1) * sizeof (char *));
|
|
|
|
|
e->vector = vector;
|
|
|
|
|
}
|
|
|
|
|
vector[i + 1] = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
2000-12-15 09:01:51 +08:00
|
|
|
|
xfree (s);
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
s = (char *) xmalloc (len + strlen (value) + 2);
|
|
|
|
|
strcpy (s, var);
|
|
|
|
|
strcat (s, "=");
|
|
|
|
|
strcat (s, value);
|
|
|
|
|
vector[i] = s;
|
|
|
|
|
|
|
|
|
|
/* This used to handle setting the PATH and GNUTARGET variables
|
|
|
|
|
specially. The latter has been replaced by "set gnutarget"
|
|
|
|
|
(which has worked since GDB 4.11). The former affects searching
|
|
|
|
|
the PATH to find SHELL, and searching the PATH to find the
|
|
|
|
|
argument of "symbol-file" or "exec-file". Maybe we should have
|
|
|
|
|
some kind of "set exec-path" for that. But in any event, having
|
|
|
|
|
"set env" affect anything besides the inferior is a bad idea.
|
|
|
|
|
What if we want to change the environment we pass to the program
|
|
|
|
|
without afecting GDB's behavior? */
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Remove the setting for variable VAR from environment E. */
|
|
|
|
|
|
|
|
|
|
void
|
2005-03-09 06:17:34 +08:00
|
|
|
|
unset_in_environ (struct gdb_environ *e, char *var)
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
2003-09-14 Andrew Cagney <cagney@redhat.com>
* alpha-nat.c: Remove some occurances of "register".
* alpha-tdep.c, arm-tdep.c, blockframe.c, breakpoint.c: Ditto.
* buildsym.c, c-typeprint.c, c-valprint.c, coffread.c: Ditto.
* corefile.c, cp-support.c, cp-valprint.c, cris-tdep.c: Ditto.
* dbxread.c, dcache.c, dwarf2read.c, elfread.c: Ditto.
* environ.c, eval.c, event-top.c, f-typeprint.c: Ditto.
* f-valprint.c, findvar.c, frame.c, gdbtypes.c: Ditto.
* h8300-tdep.c, hppa-tdep.c, hppab-nat.c, hppah-nat.c: Ditto.
* hppam3-nat.c, hpread.c, ia64-aix-nat.c, ia64-linux-nat.c: Ditto.
* infcall.c, infcmd.c, inflow.c, infptrace.c, infrun.c: Ditto.
* infttrace.c, irix5-nat.c, jv-typeprint.c: Ditto.
* jv-valprint.c, m68k-tdep.c, m68klinux-nat.c, main.c: Ditto.
* mdebugread.c, minsyms.c, mips-linux-tdep.c: Ditto.
* mips-nat.c, mips-tdep.c, mipsread.c, mipsv4-nat.c: Ditto.
* ns32k-tdep.c, objfiles.c, p-typeprint.c: Ditto.
* p-valprint.c, ppc-linux-nat.c, printcmd.c: Ditto.
* remote-mips.c, remote-vx.c, rs6000-nat.c: Ditto.
* rs6000-tdep.c, scm-exp.c, sh-tdep.c, sh64-tdep.c: Ditto.
* solib.c, somread.c, source.c, sparc-tdep.c: Ditto.
* stabsread.c, stack.c, standalone.c, symfile.c: Ditto.
* symmisc.c, symtab.c, top.c, tracepoint.c: Ditto.
* typeprint.c, utils.c, valarith.c, valops.c: Ditto.
* values.c, vax-tdep.c, xcoffread.c: Ditto.
2003-09-15 00:32:14 +08:00
|
|
|
|
int len = strlen (var);
|
|
|
|
|
char **vector = e->vector;
|
|
|
|
|
char *s;
|
1999-04-16 09:35:26 +08:00
|
|
|
|
|
|
|
|
|
for (; (s = *vector) != NULL; vector++)
|
|
|
|
|
{
|
2005-08-29 20:57:49 +08:00
|
|
|
|
if (strncmp (s, var, len) == 0 && s[len] == '=')
|
1999-04-16 09:35:26 +08:00
|
|
|
|
{
|
2000-12-15 09:01:51 +08:00
|
|
|
|
xfree (s);
|
1999-04-16 09:35:26 +08:00
|
|
|
|
/* Walk through the vector, shuffling args down by one, including
|
|
|
|
|
the NULL terminator. Can't use memcpy() here since the regions
|
|
|
|
|
overlap, and memmove() might not be available. */
|
|
|
|
|
while ((vector[0] = vector[1]) != NULL)
|
|
|
|
|
{
|
|
|
|
|
vector++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|