Do not configure or build ld for AIX native systems.
[official-gcc.git] / libiberty / setenv.c
blob8a039d18fa8d9899d0a53c524616650fefc805c7
1 /* Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file based on setenv.c in the GNU C Library.
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.
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.
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. */
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include "ansidecl.h"
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))
31 #if HAVE_STDLIB_H
32 # include <stdlib.h>
33 #endif
34 #if HAVE_STRING_H
35 # include <string.h>
36 #endif
37 #if HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
41 #define __environ environ
42 #ifndef HAVE_ENVIRON_DECL
43 extern char **environ;
44 #endif
46 /* LOCK and UNLOCK are defined as no-ops. This makes the libiberty
47 * implementation MT-Unsafe. */
48 #define LOCK
49 #define UNLOCK
51 /* Below this point, it's verbatim code from the glibc-2.0 implementation */
53 /* If this variable is not a null pointer we allocated the current
54 environment. */
55 static char **last_environ;
58 int
59 setenv (name, value, replace)
60 const char *name;
61 const char *value;
62 int replace;
64 register char **ep;
65 register size_t size;
66 const size_t namelen = strlen (name);
67 const size_t vallen = strlen (value) + 1;
69 LOCK;
71 size = 0;
72 if (__environ != NULL)
73 for (ep = __environ; *ep != NULL; ++ep)
74 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
75 break;
76 else
77 ++size;
79 if (__environ == NULL || *ep == NULL)
81 char **new_environ;
82 if (__environ == last_environ && __environ != NULL)
83 /* We allocated this space; we can extend it. */
84 new_environ = (char **) realloc (last_environ,
85 (size + 2) * sizeof (char *));
86 else
87 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
89 if (new_environ == NULL)
91 UNLOCK;
92 return -1;
95 new_environ[size] = malloc (namelen + 1 + vallen);
96 if (new_environ[size] == NULL)
98 free ((char *) new_environ);
99 __set_errno (ENOMEM);
100 UNLOCK;
101 return -1;
104 if (__environ != last_environ)
105 memcpy ((char *) new_environ, (char *) __environ,
106 size * sizeof (char *));
108 memcpy (new_environ[size], name, namelen);
109 new_environ[size][namelen] = '=';
110 memcpy (&new_environ[size][namelen + 1], value, vallen);
112 new_environ[size + 1] = NULL;
114 last_environ = __environ = new_environ;
116 else if (replace)
118 size_t len = strlen (*ep);
119 if (len + 1 < namelen + 1 + vallen)
121 /* The existing string is too short; malloc a new one. */
122 char *new = malloc (namelen + 1 + vallen);
123 if (new == NULL)
125 UNLOCK;
126 return -1;
128 *ep = new;
130 memcpy (*ep, name, namelen);
131 (*ep)[namelen] = '=';
132 memcpy (&(*ep)[namelen + 1], value, vallen);
135 UNLOCK;
137 return 0;
140 void
141 unsetenv (name)
142 const char *name;
144 const size_t len = strlen (name);
145 char **ep;
147 LOCK;
149 for (ep = __environ; *ep; ++ep)
150 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
152 /* Found it. Remove this pointer by moving later ones back. */
153 char **dp = ep;
155 dp[0] = dp[1];
156 while (*dp++);
157 /* Continue the loop in case NAME appears again. */
160 UNLOCK;