OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [gdb/] [environ.c] - Blame information for rev 850

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 330 jeremybenn
/* environ.c -- library for manipulating environments for GNU.
2
 
3
   Copyright (C) 1986, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 2000, 2005
4
   2003, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
 
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 3 of the License, or
9
   (at your option) any later version.
10
 
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.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
 
19
#define min(a, b) ((a) < (b) ? (a) : (b))
20
#define max(a, b) ((a) > (b) ? (a) : (b))
21
 
22
#include "defs.h"
23
#include "environ.h"
24
#include "gdb_string.h"
25
 
26
 
27
/* Return a new environment object.  */
28
 
29
struct gdb_environ *
30
make_environ (void)
31
{
32
  struct gdb_environ *e;
33
 
34
  e = (struct gdb_environ *) xmalloc (sizeof (struct gdb_environ));
35
 
36
  e->allocated = 10;
37
  e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
38
  e->vector[0] = 0;
39
  return e;
40
}
41
 
42
/* Free an environment and all the strings in it.  */
43
 
44
void
45
free_environ (struct gdb_environ *e)
46
{
47
  char **vector = e->vector;
48
 
49
  while (*vector)
50
    xfree (*vector++);
51
 
52
  xfree (e->vector);
53
  xfree (e);
54
}
55
 
56
/* Copy the environment given to this process into E.
57
   Also copies all the strings in it, so we can be sure
58
   that all strings in these environments are safe to free.  */
59
 
60
void
61
init_environ (struct gdb_environ *e)
62
{
63
  extern char **environ;
64
  int i;
65
 
66
  if (environ == NULL)
67
    return;
68
 
69
  for (i = 0; environ[i]; i++) /*EMPTY */ ;
70
 
71
  if (e->allocated < i)
72
    {
73
      e->allocated = max (i, e->allocated + 10);
74
      e->vector = (char **) xrealloc ((char *) e->vector,
75
                                      (e->allocated + 1) * sizeof (char *));
76
    }
77
 
78
  memcpy (e->vector, environ, (i + 1) * sizeof (char *));
79
 
80
  while (--i >= 0)
81
    {
82
      int len = strlen (e->vector[i]);
83
      char *new = (char *) xmalloc (len + 1);
84
 
85
      memcpy (new, e->vector[i], len + 1);
86
      e->vector[i] = new;
87
    }
88
}
89
 
90
/* Return the vector of environment E.
91
   This is used to get something to pass to execve.  */
92
 
93
char **
94
environ_vector (struct gdb_environ *e)
95
{
96
  return e->vector;
97
}
98
 
99
/* Return the value in environment E of variable VAR.  */
100
 
101
char *
102
get_in_environ (const struct gdb_environ *e, const char *var)
103
{
104
  int len = strlen (var);
105
  char **vector = e->vector;
106
  char *s;
107
 
108
  for (; (s = *vector) != NULL; vector++)
109
    if (strncmp (s, var, len) == 0 && s[len] == '=')
110
      return &s[len + 1];
111
 
112
  return 0;
113
}
114
 
115
/* Store the value in E of VAR as VALUE.  */
116
 
117
void
118
set_in_environ (struct gdb_environ *e, const char *var, const char *value)
119
{
120
  int i;
121
  int len = strlen (var);
122
  char **vector = e->vector;
123
  char *s;
124
 
125
  for (i = 0; (s = vector[i]) != NULL; i++)
126
    if (strncmp (s, var, len) == 0 && s[len] == '=')
127
      break;
128
 
129
  if (s == 0)
130
    {
131
      if (i == e->allocated)
132
        {
133
          e->allocated += 10;
134
          vector = (char **) xrealloc ((char *) vector,
135
                                       (e->allocated + 1) * sizeof (char *));
136
          e->vector = vector;
137
        }
138
      vector[i + 1] = 0;
139
    }
140
  else
141
    xfree (s);
142
 
143
  s = (char *) xmalloc (len + strlen (value) + 2);
144
  strcpy (s, var);
145
  strcat (s, "=");
146
  strcat (s, value);
147
  vector[i] = s;
148
 
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?  */
158
 
159
  return;
160
}
161
 
162
/* Remove the setting for variable VAR from environment E.  */
163
 
164
void
165
unset_in_environ (struct gdb_environ *e, char *var)
166
{
167
  int len = strlen (var);
168
  char **vector = e->vector;
169
  char *s;
170
 
171
  for (; (s = *vector) != NULL; vector++)
172
    {
173
      if (strncmp (s, var, len) == 0 && s[len] == '=')
174
        {
175
          xfree (s);
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)
180
            {
181
              vector++;
182
            }
183
          break;
184
        }
185
    }
186
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.