OSDN Git Service

* Makefile.in (CFILES): Add putenv.c and setenv.c.
[pf3gnuchains/gcc-fork.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 <errno.h>
24 #if !defined(errno) && !defined(HAVE_ERRNO_DECL)
25 extern int errno;
26 #endif
27 #define __set_errno(ev) ((errno) = (ev))
28
29 #if HAVE_STDLIB_H
30 # include <stdlib.h>
31 #endif
32 #if HAVE_STRING_H
33 # include <string.h>
34 #endif
35 #if HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38
39 #define __environ       environ
40 #ifndef HAVE_ENVIRON_DECL
41 extern char **environ;
42 #endif
43
44 /* LOCK and UNLOCK are defined as no-ops.  This makes the libiberty
45  * implementation MT-Unsafe. */
46 #define LOCK
47 #define UNLOCK
48
49 /* Below this point, it's verbatim code from the glibc-2.0 implementation */
50
51 /* If this variable is not a null pointer we allocated the current
52    environment.  */
53 static char **last_environ;
54
55
56 int
57 setenv (name, value, replace)
58      const char *name;
59      const char *value;
60      int replace;
61 {
62   register char **ep;
63   register size_t size;
64   const size_t namelen = strlen (name);
65   const size_t vallen = strlen (value) + 1;
66
67   LOCK;
68
69   size = 0;
70   if (__environ != NULL)
71     for (ep = __environ; *ep != NULL; ++ep)
72       if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
73         break;
74       else
75         ++size;
76
77   if (__environ == NULL || *ep == NULL)
78     {
79       char **new_environ;
80       if (__environ == last_environ && __environ != NULL)
81         /* We allocated this space; we can extend it.  */
82         new_environ = (char **) realloc (last_environ,
83                                          (size + 2) * sizeof (char *));
84       else
85         new_environ = (char **) malloc ((size + 2) * sizeof (char *));
86
87       if (new_environ == NULL)
88         {
89           UNLOCK;
90           return -1;
91         }
92
93       new_environ[size] = malloc (namelen + 1 + vallen);
94       if (new_environ[size] == NULL)
95         {
96           free ((char *) new_environ);
97           __set_errno (ENOMEM);
98           UNLOCK;
99           return -1;
100         }
101
102       if (__environ != last_environ)
103         memcpy ((char *) new_environ, (char *) __environ,
104                 size * sizeof (char *));
105
106       memcpy (new_environ[size], name, namelen);
107       new_environ[size][namelen] = '=';
108       memcpy (&new_environ[size][namelen + 1], value, vallen);
109
110       new_environ[size + 1] = NULL;
111
112       last_environ = __environ = new_environ;
113     }
114   else if (replace)
115     {
116       size_t len = strlen (*ep);
117       if (len + 1 < namelen + 1 + vallen)
118         {
119           /* The existing string is too short; malloc a new one.  */
120           char *new = malloc (namelen + 1 + vallen);
121           if (new == NULL)
122             {
123               UNLOCK;
124               return -1;
125             }
126           *ep = new;
127         }
128       memcpy (*ep, name, namelen);
129       (*ep)[namelen] = '=';
130       memcpy (&(*ep)[namelen + 1], value, vallen);
131     }
132
133   UNLOCK;
134
135   return 0;
136 }
137
138 void
139 unsetenv (name)
140      const char *name;
141 {
142   const size_t len = strlen (name);
143   char **ep;
144
145   LOCK;
146
147   for (ep = __environ; *ep; ++ep)
148     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
149       {
150         /* Found it.  Remove this pointer by moving later ones back.  */
151         char **dp = ep;
152         do
153           dp[0] = dp[1];
154         while (*dp++);
155         /* Continue the loop in case NAME appears again.  */
156       }
157
158   UNLOCK;
159 }