[ARM] Add ARMv8.3 command line option and feature flag
[external/binutils.git] / gdb / environ.c
1 /* environ.c -- library for manipulating environments for GNU.
2
3    Copyright (C) 1986-2016 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include "defs.h"
19 #include "environ.h"
20 #include <algorithm>
21 \f
22
23 /* Return a new environment object.  */
24
25 struct gdb_environ *
26 make_environ (void)
27 {
28   struct gdb_environ *e;
29
30   e = XNEW (struct gdb_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 (struct gdb_environ *e)
42 {
43   char **vector = e->vector;
44
45   while (*vector)
46     xfree (*vector++);
47
48   xfree (e->vector);
49   xfree (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 (struct gdb_environ *e)
58 {
59   extern char **environ;
60   int i;
61
62   if (environ == NULL)
63     return;
64
65   for (i = 0; environ[i]; i++) /*EMPTY */ ;
66
67   if (e->allocated < i)
68     {
69       e->allocated = std::max (i, e->allocated + 10);
70       e->vector = (char **) xrealloc ((char *) e->vector,
71                                       (e->allocated + 1) * sizeof (char *));
72     }
73
74   memcpy (e->vector, environ, (i + 1) * sizeof (char *));
75
76   while (--i >= 0)
77     {
78       int len = strlen (e->vector[i]);
79       char *newobj = (char *) xmalloc (len + 1);
80
81       memcpy (newobj, e->vector[i], len + 1);
82       e->vector[i] = newobj;
83     }
84 }
85
86 /* Return the vector of environment E.
87    This is used to get something to pass to execve.  */
88
89 char **
90 environ_vector (struct gdb_environ *e)
91 {
92   return e->vector;
93 }
94 \f
95 /* Return the value in environment E of variable VAR.  */
96
97 char *
98 get_in_environ (const struct gdb_environ *e, const char *var)
99 {
100   int len = strlen (var);
101   char **vector = e->vector;
102   char *s;
103
104   for (; (s = *vector) != NULL; vector++)
105     if (strncmp (s, var, len) == 0 && s[len] == '=')
106       return &s[len + 1];
107
108   return 0;
109 }
110
111 /* Store the value in E of VAR as VALUE.  */
112
113 void
114 set_in_environ (struct gdb_environ *e, const char *var, const char *value)
115 {
116   int i;
117   int len = strlen (var);
118   char **vector = e->vector;
119   char *s;
120
121   for (i = 0; (s = vector[i]) != NULL; i++)
122     if (strncmp (s, var, len) == 0 && s[len] == '=')
123       break;
124
125   if (s == 0)
126     {
127       if (i == e->allocated)
128         {
129           e->allocated += 10;
130           vector = (char **) xrealloc ((char *) vector,
131                                        (e->allocated + 1) * sizeof (char *));
132           e->vector = vector;
133         }
134       vector[i + 1] = 0;
135     }
136   else
137     xfree (s);
138
139   s = (char *) xmalloc (len + strlen (value) + 2);
140   strcpy (s, var);
141   strcat (s, "=");
142   strcat (s, value);
143   vector[i] = s;
144
145   /* This used to handle setting the PATH and GNUTARGET variables
146      specially.  The latter has been replaced by "set gnutarget"
147      (which has worked since GDB 4.11).  The former affects searching
148      the PATH to find SHELL, and searching the PATH to find the
149      argument of "symbol-file" or "exec-file".  Maybe we should have
150      some kind of "set exec-path" for that.  But in any event, having
151      "set env" affect anything besides the inferior is a bad idea.
152      What if we want to change the environment we pass to the program
153      without afecting GDB's behavior?  */
154
155   return;
156 }
157
158 /* Remove the setting for variable VAR from environment E.  */
159
160 void
161 unset_in_environ (struct gdb_environ *e, const char *var)
162 {
163   int len = strlen (var);
164   char **vector = e->vector;
165   char *s;
166
167   for (; (s = *vector) != NULL; vector++)
168     {
169       if (strncmp (s, var, len) == 0 && s[len] == '=')
170         {
171           xfree (s);
172           /* Walk through the vector, shuffling args down by one, including
173              the NULL terminator.  Can't use memcpy() here since the regions
174              overlap, and memmove() might not be available.  */
175           while ((vector[0] = vector[1]) != NULL)
176             {
177               vector++;
178             }
179           break;
180         }
181     }
182 }