.
[coreutils.git] / lib / setenv.c
blob429f5f8fe88442b27d6a2592b1d178b29be18076
1 /* Copyright (C) 1992, 1995 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C Library.
4 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 USA. */
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <errno.h>
27 #if _LIBC || HAVE_STDLIB_H
28 # include <stdlib.h>
29 #endif
30 #if _LIBC || HAVE_STRING_H
31 # include <string.h>
32 #endif
33 #if _LIBC || HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
37 #ifndef HAVE_GNU_LD
38 # define __environ environ
39 #endif
41 int
42 setenv (name, value, replace)
43 const char *name;
44 const char *value;
45 int replace;
47 register char **ep;
48 register size_t size;
49 const size_t namelen = strlen (name);
50 const size_t vallen = strlen (value) + 1;
52 size = 0;
53 for (ep = __environ; *ep != NULL; ++ep)
54 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
55 break;
56 else
57 ++size;
59 if (*ep == NULL)
61 static char **last_environ;
62 char **new_environ;
63 if (__environ == last_environ)
64 /* We allocated this space; we can extend it. */
65 new_environ = (char **) realloc (last_environ,
66 (size + 2) * sizeof (char *));
67 else
68 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
70 if (new_environ == NULL)
71 return -1;
73 new_environ[size] = malloc (namelen + 1 + vallen);
74 if (new_environ[size] == NULL)
76 free ((char *) new_environ);
77 errno = ENOMEM;
78 return -1;
81 if (__environ != last_environ)
82 memcpy ((char *) new_environ, (char *) __environ,
83 size * sizeof (char *));
85 memcpy (new_environ[size], name, namelen);
86 new_environ[size][namelen] = '=';
87 memcpy (&new_environ[size][namelen + 1], value, vallen);
89 new_environ[size + 1] = NULL;
91 last_environ = __environ = new_environ;
93 else if (replace)
95 size_t len = strlen (*ep);
96 if (len + 1 < namelen + 1 + vallen)
98 /* The existing string is too short; malloc a new one. */
99 char *new = malloc (namelen + 1 + vallen);
100 if (new == NULL)
101 return -1;
102 *ep = new;
104 memcpy (*ep, name, namelen);
105 (*ep)[namelen] = '=';
106 memcpy (&(*ep)[namelen + 1], value, vallen);
109 return 0;
112 void
113 unsetenv (name)
114 const char *name;
116 const size_t len = strlen (name);
117 char **ep;
119 for (ep = __environ; *ep; ++ep)
120 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
122 /* Found it. Remove this pointer by moving later ones back. */
123 char **dp = ep;
125 dp[0] = dp[1];
126 while (*dp++);
127 /* Continue the loop in case NAME appears again. */