Commit | Line | Data |
---|---|---|
e6e2e95a | 1 | /* Copyright (C) 1991, 2000, 2001 Free Software Foundation, Inc. |
19468eff | 2 | |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library 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 GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
19468eff | 17 | |
6e8d25a6 | 18 | |
6c320737 RB |
19 | #if HAVE_CONFIG_H |
20 | # include <config.h> | |
19468eff GH |
21 | #endif |
22 | ||
6c320737 RB |
23 | #include "libguile/scmconfig.h" |
24 | ||
19468eff GH |
25 | #include <sys/types.h> |
26 | #include <errno.h> | |
19468eff GH |
27 | |
28 | /* Don't include stdlib.h for non-GNU C libraries because some of them | |
29 | contain conflicting prototypes for getopt. | |
30 | This needs to come after some library #include | |
31 | to get __GNU_LIBRARY__ defined. */ | |
32 | #ifdef __GNU_LIBRARY__ | |
33 | #include <stdlib.h> | |
34 | #else | |
35 | char *malloc (); | |
36 | #endif /* GNU C library. */ | |
37 | ||
38 | #if defined(STDC_HEADERS) || defined(HAVE_STRING_H) | |
39 | #include <string.h> | |
40 | #else | |
41 | #include <strings.h> | |
42 | #ifndef strchr | |
43 | #define strchr index | |
44 | #endif | |
45 | #ifndef memcpy | |
46 | #define memcpy(d, s, n) bcopy((s), (d), (n)) | |
47 | #endif | |
48 | #endif | |
49 | ||
50 | #ifdef HAVE_UNISTD_H | |
51 | #include <unistd.h> | |
52 | #endif | |
53 | ||
54 | #ifndef NULL | |
55 | #define NULL 0 | |
56 | #endif | |
57 | ||
58 | extern char **environ; | |
59 | ||
60 | /* Put STRING, which is of the form "NAME=VALUE", in the environment. */ | |
61 | int | |
6e8d25a6 | 62 | putenv (const char *string) |
19468eff GH |
63 | { |
64 | char *name_end = strchr (string, '='); | |
65 | register size_t size; | |
66 | register char **ep; | |
67 | ||
68 | if (name_end == NULL) | |
69 | { | |
70 | /* Remove the variable from the environment. */ | |
71 | size = strlen (string); | |
72 | for (ep = environ; *ep != NULL; ++ep) | |
73 | if (!strncmp (*ep, string, size) && (*ep)[size] == '=') | |
74 | { | |
75 | while (ep[1] != NULL) | |
76 | { | |
77 | ep[0] = ep[1]; | |
78 | ++ep; | |
79 | } | |
80 | *ep = NULL; | |
81 | return 0; | |
82 | } | |
83 | } | |
84 | ||
85 | size = 0; | |
86 | for (ep = environ; *ep != NULL; ++ep) | |
87 | if (!strncmp (*ep, string, name_end - string) && | |
88 | (*ep)[name_end - string] == '=') | |
89 | break; | |
90 | else | |
91 | ++size; | |
92 | ||
93 | if (*ep == NULL) | |
94 | { | |
95 | static char **last_environ = NULL; | |
67329a9e | 96 | char **new_environ = (char **) scm_malloc ((size + 2) * sizeof (char *)); |
19468eff GH |
97 | if (new_environ == NULL) |
98 | return -1; | |
99 | memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *)); | |
100 | new_environ[size] = (char *) string; | |
101 | new_environ[size + 1] = NULL; | |
102 | if (last_environ != NULL) | |
103 | free ((char *) last_environ); | |
104 | last_environ = new_environ; | |
105 | environ = new_environ; | |
106 | } | |
107 | else | |
108 | *ep = (char *) string; | |
109 | ||
110 | return 0; | |
111 | } | |
89e00824 ML |
112 | |
113 | /* | |
114 | Local Variables: | |
115 | c-file-style: "gnu" | |
116 | End: | |
117 | */ |