Merge devo/bfd with GDB's bfd.
[external/binutils.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 #include <string.h>
23
24 extern char *xmalloc ();
25 extern char *xrealloc ();
26 extern void free ();
27 \f
28 /* Return a new environment object.  */
29
30 struct environ *
31 make_environ ()
32 {
33   register struct environ *e;
34
35   e = (struct environ *) xmalloc (sizeof (struct environ));
36
37   e->allocated = 10;
38   e->vector = (char **) xmalloc ((e->allocated + 1) * sizeof (char *));
39   e->vector[0] = 0;
40   return e;
41 }
42
43 /* Free an environment and all the strings in it.  */
44
45 void
46 free_environ (e)
47      register struct environ *e;
48 {
49   register char **vector = e->vector;
50
51   while (*vector)
52     free (*vector++);
53
54   free (e);
55 }
56
57 /* Copy the environment given to this process into E.
58    Also copies all the strings in it, so we can be sure
59    that all strings in these environments are safe to free.  */
60
61 void
62 init_environ (e)
63      register struct environ *e;
64 {
65   extern char **environ;
66   register int i;
67
68   for (i = 0; environ[i]; i++) /*EMPTY*/;
69
70   if (e->allocated < i)
71     {
72       e->allocated = max (i, e->allocated + 10);
73       e->vector = (char **) xrealloc ((char *)e->vector,
74                                       (e->allocated + 1) * sizeof (char *));
75     }
76
77   bcopy (environ, e->vector, (i + 1) * sizeof (char *));
78
79   while (--i >= 0)
80     {
81       register int len = strlen (e->vector[i]);
82       register char *new = (char *) xmalloc (len + 1);
83       bcopy (e->vector[i], new, len + 1);
84       e->vector[i] = new;
85     }
86 }
87
88 /* Return the vector of environment E.
89    This is used to get something to pass to execve.  */
90
91 char **
92 environ_vector (e)
93      struct environ *e;
94 {
95   return e->vector;
96 }
97 \f
98 /* Return the value in environment E of variable VAR.  */
99
100 char *
101 get_in_environ (e, var)
102      struct environ *e;
103      char *var;
104 {
105   register int len = strlen (var);
106   register char **vector = e->vector;
107   register char *s;
108
109   for (; s = *vector; vector++)
110     if (!strncmp (s, var, len)
111         && s[len] == '=')
112       return &s[len + 1];
113
114   return 0;
115 }
116
117 /* Store the value in E of VAR as VALUE.  */
118
119 void
120 set_in_environ (e, var, value)
121      struct environ *e;
122      char *var;
123      char *value;
124 {
125   register int i;
126   register int len = strlen (var);
127   register char **vector = e->vector;
128   register char *s;
129
130   for (i = 0; s = vector[i]; i++)
131     if (!strncmp (s, var, len)
132         && s[len] == '=')
133       break;
134
135   if (s == 0)
136     {
137       if (i == e->allocated)
138         {
139           e->allocated += 10;
140           vector = (char **) xrealloc ((char *)vector,
141                                        (e->allocated + 1) * sizeof (char *));
142           e->vector = vector;
143         }
144       vector[i + 1] = 0;
145     }
146   else
147     free (s);
148
149   s = (char *) xmalloc (len + strlen (value) + 2);
150   strcpy (s, var);
151   strcat (s, "=");
152   strcat (s, value);
153   vector[i] = s;
154
155   /* Certain variables get exported back to the parent (e.g. our) 
156      environment, too.  */
157   if (!strcmp(var, "PATH")                      /* Object file location */
158    || !strcmp (var, "G960BASE")                 /* Intel 960 downloads */
159    || !strcmp (var, "G960BIN")                  /* Intel 960 downloads */
160    || !strcmp (var, "GNUTARGET")                /* BFD object file type */
161                                 ) {
162     putenv (strsave (s));
163   }
164   return;
165 }
166
167 /* Remove the setting for variable VAR from environment E.  */
168
169 void
170 unset_in_environ (e, var)
171      struct environ *e;
172      char *var;
173 {
174   register int len = strlen (var);
175   register char **vector = e->vector;
176   register char *s;
177
178   for (; s = *vector; vector++)
179     if (!strncmp (s, var, len)
180         && s[len] == '=')
181       {
182         free (s);
183         bcopy (vector + 1, vector,
184                (e->allocated - (vector - e->vector)) * sizeof (char *));
185         e->vector[e->allocated - 1] = 0;
186         return;
187       }
188 }