.
[glibc.git] / sysdeps / generic / setenv.c
blobdc2e8b43f3319664eb066b5dc7fd67e5aaaf40e3
1 /* Copyright (C) 1992, 1995 Free Software Foundation, Inc.
2 This file is part of 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include <errno.h>
25 #if _LIBC || HAVE_STDLIB_H
26 #include <stdlib.h>
27 #endif
28 #if _LIBC || HAVE_STRING_H
29 #include <string.h>
30 #endif
31 #if _LIBC || HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 #ifndef HAVE_GNU_LD
36 #define __environ environ
37 #endif
39 int
40 setenv (name, value, replace)
41 const char *name;
42 const char *value;
43 int replace;
45 register char **ep;
46 register size_t size;
47 const size_t namelen = strlen (name);
48 const size_t vallen = strlen (value) + 1;
50 size = 0;
51 for (ep = __environ; *ep != NULL; ++ep)
52 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
53 break;
54 else
55 ++size;
57 if (*ep == NULL)
59 static char **last_environ;
60 char **new_environ;
61 if (__environ == last_environ)
62 /* We allocated this space; we can extend it. */
63 new_environ = (char **) realloc (last_environ,
64 (size + 2) * sizeof (char *));
65 else
66 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
68 if (new_environ == NULL)
69 return -1;
71 new_environ[size] = malloc (namelen + 1 + vallen);
72 if (new_environ[size] == NULL)
74 free ((char *) new_environ);
75 errno = ENOMEM;
76 return -1;
79 if (__environ != last_environ)
80 memcpy ((char *) new_environ, (char *) __environ,
81 size * sizeof (char *));
83 memcpy (new_environ[size], name, namelen);
84 new_environ[size][namelen] = '=';
85 memcpy (&new_environ[size][namelen + 1], value, vallen);
87 new_environ[size + 1] = NULL;
89 last_environ = __environ = new_environ;
91 else if (replace)
93 size_t len = strlen (*ep);
94 if (len + 1 < namelen + 1 + vallen)
96 /* The existing string is too short; malloc a new one. */
97 char *new = malloc (namelen + 1 + vallen);
98 if (new == NULL)
99 return -1;
100 *ep = new;
102 memcpy (*ep, name, namelen);
103 (*ep)[namelen] = '=';
104 memcpy (&(*ep)[namelen + 1], value, vallen);
107 return 0;
110 void
111 unsetenv (const char *name)
113 const size_t len = strlen (name);
114 char **ep;
116 for (ep = __environ; *ep; ++ep)
117 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
119 /* Found it. Remove this pointer by moving later ones back. */
120 char **dp = ep;
122 dp[0] = dp[1];
123 while (*dp++);
124 /* Continue the loop in case NAME appears again. */