]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/generic/setenv.c
Mon Apr 1 13:34:55 1996 Roland McGrath <roland@whiz-bang.gnu.ai.mit.edu>
[thirdparty/glibc.git] / sysdeps / generic / setenv.c
CommitLineData
60092701 1/* Copyright (C) 1992, 1995 Free Software Foundation, Inc.
28f540f4
RM
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
196980f5
RM
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#include <errno.h>
24
25#if _LIBC || HAVE_STDLIB_H
28f540f4 26#include <stdlib.h>
196980f5
RM
27#endif
28#if _LIBC || HAVE_STRING_H
28f540f4 29#include <string.h>
196980f5
RM
30#endif
31#if _LIBC || HAVE_UNISTD_H
28f540f4 32#include <unistd.h>
196980f5 33#endif
28f540f4
RM
34
35#ifndef HAVE_GNU_LD
36#define __environ environ
37#endif
38
39int
196980f5
RM
40setenv (name, value, replace)
41 const char *name;
42 const char *value;
43 int replace;
28f540f4
RM
44{
45 register char **ep;
46 register size_t size;
196980f5
RM
47 const size_t namelen = strlen (name);
48 const size_t vallen = strlen (value) + 1;
28f540f4
RM
49
50 size = 0;
51 for (ep = __environ; *ep != NULL; ++ep)
52 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
53 break;
54 else
55 ++size;
196980f5 56
28f540f4
RM
57 if (*ep == NULL)
58 {
196980f5
RM
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 *));
67
28f540f4
RM
68 if (new_environ == NULL)
69 return -1;
28f540f4 70
196980f5 71 new_environ[size] = malloc (namelen + 1 + vallen);
28f540f4
RM
72 if (new_environ[size] == NULL)
73 {
196980f5 74 free ((char *) new_environ);
28f540f4
RM
75 errno = ENOMEM;
76 return -1;
77 }
196980f5
RM
78
79 if (__environ != last_environ)
80 memcpy ((char *) new_environ, (char *) __environ,
81 size * sizeof (char *));
82
28f540f4
RM
83 memcpy (new_environ[size], name, namelen);
84 new_environ[size][namelen] = '=';
196980f5 85 memcpy (&new_environ[size][namelen + 1], value, vallen);
28f540f4
RM
86
87 new_environ[size + 1] = NULL;
88
196980f5 89 last_environ = __environ = new_environ;
28f540f4
RM
90 }
91 else if (replace)
92 {
93 size_t len = strlen (*ep);
196980f5 94 if (len + 1 < namelen + 1 + vallen)
28f540f4 95 {
196980f5 96 /* The existing string is too short; malloc a new one. */
28f540f4
RM
97 char *new = malloc (namelen + 1 + vallen);
98 if (new == NULL)
99 return -1;
100 *ep = new;
101 }
102 memcpy (*ep, name, namelen);
103 (*ep)[namelen] = '=';
196980f5 104 memcpy (&(*ep)[namelen + 1], value, vallen);
28f540f4
RM
105 }
106
107 return 0;
108}
196980f5
RM
109
110void
111unsetenv (const char *name)
112{
113 const size_t len = strlen (name);
114 char **ep;
115
116 for (ep = __environ; *ep; ++ep)
117 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
118 {
119 /* Found it. Remove this pointer by moving later ones back. */
120 char **dp = ep;
121 do
122 dp[0] = dp[1];
123 while (*dp++);
124 /* Continue the loop in case NAME appears again. */
125 }
126}