]> git.ipfire.org Git - thirdparty/gcc.git/blob - libiberty/setenv.c
setenv.c (sys/types.h, stdio.h): Files included.
[thirdparty/gcc.git] / libiberty / setenv.c
1 /* Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file based on setenv.c in the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include "ansidecl.h"
24
25 #include <errno.h>
26 #if !defined(errno) && !defined(HAVE_ERRNO_DECL)
27 extern int errno;
28 #endif
29 #define __set_errno(ev) ((errno) = (ev))
30
31 #if HAVE_STDLIB_H
32 # include <stdlib.h>
33 #else
34 #include <sys/types.h> /* For `size_t' */
35 #include <stdio.h> /* For `NULL' */
36 #endif
37 #if HAVE_STRING_H
38 # include <string.h>
39 #endif
40 #if HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
43
44 #define __environ environ
45 #ifndef HAVE_ENVIRON_DECL
46 extern char **environ;
47 #endif
48
49 /* LOCK and UNLOCK are defined as no-ops. This makes the libiberty
50 * implementation MT-Unsafe. */
51 #define LOCK
52 #define UNLOCK
53
54 /* Below this point, it's verbatim code from the glibc-2.0 implementation */
55
56 /* If this variable is not a null pointer we allocated the current
57 environment. */
58 static char **last_environ;
59
60
61 int
62 setenv (name, value, replace)
63 const char *name;
64 const char *value;
65 int replace;
66 {
67 register char **ep;
68 register size_t size;
69 const size_t namelen = strlen (name);
70 const size_t vallen = strlen (value) + 1;
71
72 LOCK;
73
74 size = 0;
75 if (__environ != NULL)
76 for (ep = __environ; *ep != NULL; ++ep)
77 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
78 break;
79 else
80 ++size;
81
82 if (__environ == NULL || *ep == NULL)
83 {
84 char **new_environ;
85 if (__environ == last_environ && __environ != NULL)
86 /* We allocated this space; we can extend it. */
87 new_environ = (char **) realloc (last_environ,
88 (size + 2) * sizeof (char *));
89 else
90 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
91
92 if (new_environ == NULL)
93 {
94 UNLOCK;
95 return -1;
96 }
97
98 new_environ[size] = malloc (namelen + 1 + vallen);
99 if (new_environ[size] == NULL)
100 {
101 free ((char *) new_environ);
102 __set_errno (ENOMEM);
103 UNLOCK;
104 return -1;
105 }
106
107 if (__environ != last_environ)
108 memcpy ((char *) new_environ, (char *) __environ,
109 size * sizeof (char *));
110
111 memcpy (new_environ[size], name, namelen);
112 new_environ[size][namelen] = '=';
113 memcpy (&new_environ[size][namelen + 1], value, vallen);
114
115 new_environ[size + 1] = NULL;
116
117 last_environ = __environ = new_environ;
118 }
119 else if (replace)
120 {
121 size_t len = strlen (*ep);
122 if (len + 1 < namelen + 1 + vallen)
123 {
124 /* The existing string is too short; malloc a new one. */
125 char *new = malloc (namelen + 1 + vallen);
126 if (new == NULL)
127 {
128 UNLOCK;
129 return -1;
130 }
131 *ep = new;
132 }
133 memcpy (*ep, name, namelen);
134 (*ep)[namelen] = '=';
135 memcpy (&(*ep)[namelen + 1], value, vallen);
136 }
137
138 UNLOCK;
139
140 return 0;
141 }
142
143 void
144 unsetenv (name)
145 const char *name;
146 {
147 const size_t len = strlen (name);
148 char **ep;
149
150 LOCK;
151
152 for (ep = __environ; *ep; ++ep)
153 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
154 {
155 /* Found it. Remove this pointer by moving later ones back. */
156 char **dp = ep;
157 do
158 dp[0] = dp[1];
159 while (*dp++);
160 /* Continue the loop in case NAME appears again. */
161 }
162
163 UNLOCK;
164 }