Commit | Line | Data |
---|---|---|
19468eff GH |
1 | /* Copyright (C) 1991 Free Software Foundation, Inc. |
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 JB |
15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
16 | USA */ | |
19468eff | 17 | |
6e8d25a6 GB |
18 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
19 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
20 | ||
19468eff | 21 | #ifdef HAVE_CONFIG_H |
d183c1b9 | 22 | #include "libguile/scmconfig.h" |
19468eff GH |
23 | #endif |
24 | ||
25 | #include <sys/types.h> | |
26 | #include <errno.h> | |
27 | #ifndef errno | |
28 | extern int errno; | |
29 | #endif | |
30 | ||
31 | /* Don't include stdlib.h for non-GNU C libraries because some of them | |
32 | contain conflicting prototypes for getopt. | |
33 | This needs to come after some library #include | |
34 | to get __GNU_LIBRARY__ defined. */ | |
35 | #ifdef __GNU_LIBRARY__ | |
36 | #include <stdlib.h> | |
37 | #else | |
38 | char *malloc (); | |
39 | #endif /* GNU C library. */ | |
40 | ||
41 | #if defined(STDC_HEADERS) || defined(HAVE_STRING_H) | |
42 | #include <string.h> | |
43 | #else | |
44 | #include <strings.h> | |
45 | #ifndef strchr | |
46 | #define strchr index | |
47 | #endif | |
48 | #ifndef memcpy | |
49 | #define memcpy(d, s, n) bcopy((s), (d), (n)) | |
50 | #endif | |
51 | #endif | |
52 | ||
53 | #ifdef HAVE_UNISTD_H | |
54 | #include <unistd.h> | |
55 | #endif | |
56 | ||
57 | #ifndef NULL | |
58 | #define NULL 0 | |
59 | #endif | |
60 | ||
61 | extern char **environ; | |
62 | ||
63 | /* Put STRING, which is of the form "NAME=VALUE", in the environment. */ | |
64 | int | |
6e8d25a6 | 65 | putenv (const char *string) |
19468eff GH |
66 | { |
67 | char *name_end = strchr (string, '='); | |
68 | register size_t size; | |
69 | register char **ep; | |
70 | ||
71 | if (name_end == NULL) | |
72 | { | |
73 | /* Remove the variable from the environment. */ | |
74 | size = strlen (string); | |
75 | for (ep = environ; *ep != NULL; ++ep) | |
76 | if (!strncmp (*ep, string, size) && (*ep)[size] == '=') | |
77 | { | |
78 | while (ep[1] != NULL) | |
79 | { | |
80 | ep[0] = ep[1]; | |
81 | ++ep; | |
82 | } | |
83 | *ep = NULL; | |
84 | return 0; | |
85 | } | |
86 | } | |
87 | ||
88 | size = 0; | |
89 | for (ep = environ; *ep != NULL; ++ep) | |
90 | if (!strncmp (*ep, string, name_end - string) && | |
91 | (*ep)[name_end - string] == '=') | |
92 | break; | |
93 | else | |
94 | ++size; | |
95 | ||
96 | if (*ep == NULL) | |
97 | { | |
98 | static char **last_environ = NULL; | |
99 | char **new_environ = (char **) malloc ((size + 2) * sizeof (char *)); | |
100 | if (new_environ == NULL) | |
101 | return -1; | |
102 | memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *)); | |
103 | new_environ[size] = (char *) string; | |
104 | new_environ[size + 1] = NULL; | |
105 | if (last_environ != NULL) | |
106 | free ((char *) last_environ); | |
107 | last_environ = new_environ; | |
108 | environ = new_environ; | |
109 | } | |
110 | else | |
111 | *ep = (char *) string; | |
112 | ||
113 | return 0; | |
114 | } | |
89e00824 ML |
115 | |
116 | /* | |
117 | Local Variables: | |
118 | c-file-style: "gnu" | |
119 | End: | |
120 | */ |