*** empty log message ***
[bpt/guile.git] / libguile / putenv.c
CommitLineData
e6e2e95a 1/* Copyright (C) 1991, 2000, 2001 Free Software Foundation, Inc.
19468eff
GH
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
82892bed 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
c22adbeb
MV
16 USA
17
18 As a special exception, the Free Software Foundation gives permission
19 for additional uses of the text contained in its release of GUILE.
20
21 The exception is that, if you link the GUILE library with other files
22 to produce an executable, this does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 Your use of that executable is in no way restricted on account of
25 linking the GUILE library code into it.
26
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License.
29
30 This exception applies only to the code released by the
31 Free Software Foundation under the name GUILE. If you copy
32 code from other Free Software Foundation releases into a copy of
33 GUILE, as the General Public License permits, the exception does
34 not apply to the code that you add in this way. To avoid misleading
35 anyone as to the status of such modified files, you must delete
36 this exception notice from them.
37
38 If you write modifications of your own for GUILE, it is your choice
39 whether to permit this exception to apply to your modifications.
40 If you do not wish that, delete this exception notice. */
19468eff 41
6e8d25a6
GB
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
19468eff 45#ifdef HAVE_CONFIG_H
d183c1b9 46#include "libguile/scmconfig.h"
19468eff
GH
47#endif
48
49#include <sys/types.h>
50#include <errno.h>
19468eff
GH
51
52/* Don't include stdlib.h for non-GNU C libraries because some of them
53 contain conflicting prototypes for getopt.
54 This needs to come after some library #include
55 to get __GNU_LIBRARY__ defined. */
56#ifdef __GNU_LIBRARY__
57#include <stdlib.h>
58#else
59char *malloc ();
60#endif /* GNU C library. */
61
62#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
63#include <string.h>
64#else
65#include <strings.h>
66#ifndef strchr
67#define strchr index
68#endif
69#ifndef memcpy
70#define memcpy(d, s, n) bcopy((s), (d), (n))
71#endif
72#endif
73
74#ifdef HAVE_UNISTD_H
75#include <unistd.h>
76#endif
77
78#ifndef NULL
79#define NULL 0
80#endif
81
82extern char **environ;
83
84/* Put STRING, which is of the form "NAME=VALUE", in the environment. */
85int
6e8d25a6 86putenv (const char *string)
19468eff
GH
87{
88 char *name_end = strchr (string, '=');
89 register size_t size;
90 register char **ep;
91
92 if (name_end == NULL)
93 {
94 /* Remove the variable from the environment. */
95 size = strlen (string);
96 for (ep = environ; *ep != NULL; ++ep)
97 if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
98 {
99 while (ep[1] != NULL)
100 {
101 ep[0] = ep[1];
102 ++ep;
103 }
104 *ep = NULL;
105 return 0;
106 }
107 }
108
109 size = 0;
110 for (ep = environ; *ep != NULL; ++ep)
111 if (!strncmp (*ep, string, name_end - string) &&
112 (*ep)[name_end - string] == '=')
113 break;
114 else
115 ++size;
116
117 if (*ep == NULL)
118 {
119 static char **last_environ = NULL;
120 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
121 if (new_environ == NULL)
122 return -1;
123 memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *));
124 new_environ[size] = (char *) string;
125 new_environ[size + 1] = NULL;
126 if (last_environ != NULL)
127 free ((char *) last_environ);
128 last_environ = new_environ;
129 environ = new_environ;
130 }
131 else
132 *ep = (char *) string;
133
134 return 0;
135}
89e00824
ML
136
137/*
138 Local Variables:
139 c-file-style: "gnu"
140 End:
141*/