27aa3aedb8ec8e1b39f685fdff2fe4da4cdca30f
[deliverable/binutils-gdb.git] / gdb / environ.c
1 /* environ.c -- library for manipulating environments for GNU.
2 Copyright (C) 1986, 1989 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #define min(a, b) ((a) < (b) ? (a) : (b))
19 #define max(a, b) ((a) > (b) ? (a) : (b))
20
21 #include "environ.h"
22 \f
23 /* Return a new environment object. */
24
25 struct environ *
26 make_environ ()
27 {
28 register struct environ *e;
29
30 e = (struct environ *) xmalloc (sizeof (struct environ));
31
32 e->allocated = 10;
33 e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
34 e->vector[0] = 0;
35 return e;
36 }
37
38 /* Free an environment and all the strings in it. */
39
40 void
41 free_environ (e)
42 register struct environ *e;
43 {
44 register char **vector = e->vector;
45
46 while (*vector)
47 free (*vector++);
48
49 free (e);
50 }
51
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. */
55
56 void
57 init_environ (e)
58 register struct environ *e;
59 {
60 extern char **environ;
61 register int i;
62
63 for (i = 0; environ[i]; i++);
64
65 if (e->allocated < i)
66 {
67 e->allocated = max (i, e->allocated + 10);
68 e->vector = (char **) xrealloc (e->vector,
69 (e->allocated + 1) * sizeof (char *));
70 }
71
72 bcopy (environ, e->vector, (i + 1) * sizeof (char *));
73
74 while (--i >= 0)
75 {
76 register int len = strlen (e->vector[i]);
77 register char *new = (char *) xmalloc (len + 1);
78 bcopy (e->vector[i], new, len);
79 e->vector[i] = new;
80 }
81 }
82
83 /* Return the vector of environment E.
84 This is used to get something to pass to execve. */
85
86 char **
87 environ_vector (e)
88 struct environ *e;
89 {
90 return e->vector;
91 }
92 \f
93 /* Return the value in environment E of variable VAR. */
94
95 char *
96 get_in_environ (e, var)
97 struct environ *e;
98 char *var;
99 {
100 register int len = strlen (var);
101 register char **vector = e->vector;
102 register char *s;
103
104 for (; s = *vector; vector++)
105 if (!strncmp (s, var, len)
106 && s[len] == '=')
107 return &s[len + 1];
108
109 return 0;
110 }
111
112 /* Store the value in E of VAR as VALUE. */
113
114 void
115 set_in_environ (e, var, value)
116 struct environ *e;
117 char *var;
118 char *value;
119 {
120 register int i;
121 register int len = strlen (var);
122 register char **vector = e->vector;
123 register char *s;
124
125 for (i = 0; s = vector[i]; i++)
126 if (!strncmp (s, var, len)
127 && s[len] == '=')
128 break;
129
130 if (s == 0)
131 {
132 if (i == e->allocated)
133 {
134 e->allocated += 10;
135 vector = (char **) xrealloc (vector,
136 (e->allocated + 1) * sizeof (char *));
137 e->vector = vector;
138 }
139 vector[i + 1] = 0;
140 }
141 else
142 free (s);
143
144 s = (char *) xmalloc (len + strlen (value) + 2);
145 strcpy (s, var);
146 strcat (s, "=");
147 strcat (s, value);
148 vector[i] = s;
149 return;
150 }
151
152 /* Remove the setting for variable VAR from environment E. */
153
154 void
155 unset_in_environ (e, var)
156 struct environ *e;
157 char *var;
158 {
159 register int len = strlen (var);
160 register char **vector = e->vector;
161 register char *s;
162
163 for (; s = *vector; vector++)
164 if (!strncmp (s, var, len)
165 && s[len] == '=')
166 {
167 free (s);
168 bcopy (vector + 1, vector,
169 (e->allocated - (vector - e->vector)) * sizeof (char *));
170 e->vector[e->allocated - 1] = 0;
171 return;
172 }
173 }
This page took 0.032965 seconds and 4 git commands to generate.