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