diff options
-rw-r--r-- | include/mingw.h | 4 | ||||
-rw-r--r-- | win32/env.c | 57 |
2 files changed, 49 insertions, 12 deletions
diff --git a/include/mingw.h b/include/mingw.h index 7caedd063..654ca3ee6 100644 --- a/include/mingw.h +++ b/include/mingw.h | |||
@@ -180,13 +180,15 @@ int mingw_system(const char *cmd); | |||
180 | 180 | ||
181 | int clearenv(void); | 181 | int clearenv(void); |
182 | char *mingw_getenv(const char *name); | 182 | char *mingw_getenv(const char *name); |
183 | int mingw_putenv(const char *env); | ||
183 | char *mingw_mktemp(char *template); | 184 | char *mingw_mktemp(char *template); |
184 | int mkstemp(char *template); | 185 | int mkstemp(char *template); |
185 | char *realpath(const char *path, char *resolved_path); | 186 | char *realpath(const char *path, char *resolved_path); |
186 | int setenv(const char *name, const char *value, int replace); | 187 | int setenv(const char *name, const char *value, int replace); |
187 | void unsetenv(const char *env); | 188 | int unsetenv(const char *env); |
188 | 189 | ||
189 | #define getenv mingw_getenv | 190 | #define getenv mingw_getenv |
191 | #define putenv mingw_putenv | ||
190 | #define mktemp mingw_mktemp | 192 | #define mktemp mingw_mktemp |
191 | 193 | ||
192 | /* | 194 | /* |
diff --git a/win32/env.c b/win32/env.c index f8d231a8f..eb2e1f004 100644 --- a/win32/env.c +++ b/win32/env.c | |||
@@ -104,27 +104,62 @@ char **env_setenv(char **env, const char *name) | |||
104 | else { | 104 | else { |
105 | for (; env[i]; i++) | 105 | for (; env[i]; i++) |
106 | env[i] = env[i+1]; | 106 | env[i] = env[i+1]; |
107 | SetEnvironmentVariable(name, NULL); | ||
108 | } | 107 | } |
109 | } | 108 | } |
110 | return env; | 109 | return env; |
111 | } | 110 | } |
112 | 111 | ||
113 | void unsetenv(const char *env) | 112 | /* |
113 | * Removing an environment variable with WIN32 putenv requires an argument | ||
114 | * like "NAME="; glibc omits the '='. The implementations of unsetenv and | ||
115 | * clearenv allow for this. | ||
116 | * | ||
117 | * It isn't possible to create an environment variable with an empty value | ||
118 | * using WIN32 putenv. | ||
119 | */ | ||
120 | #undef putenv | ||
121 | int unsetenv(const char *env) | ||
114 | { | 122 | { |
115 | env_setenv(environ, env); | 123 | char *name; |
124 | int ret; | ||
125 | |||
126 | name = xmalloc(strlen(env)+2); | ||
127 | strcat(strcpy(name, env), "="); | ||
128 | ret = putenv(name); | ||
129 | free(name); | ||
130 | |||
131 | return ret; | ||
116 | } | 132 | } |
117 | 133 | ||
118 | int clearenv(void) | 134 | int clearenv(void) |
119 | { | 135 | { |
120 | char **env = environ; | 136 | char *name, *s; |
121 | if (!env) | 137 | |
122 | return 0; | 138 | while ( environ && *environ ) { |
123 | while (*env) { | 139 | if ( (s=strchr(*environ, '=')) != NULL ) { |
124 | free(*env); | 140 | name = xstrndup(*environ, s-*environ+1); |
125 | env++; | 141 | putenv(name); |
142 | free(name); | ||
143 | } | ||
144 | else { | ||
145 | return -1; | ||
146 | } | ||
126 | } | 147 | } |
127 | free(env); | 148 | return 0; |
128 | environ = NULL; | 149 | } |
150 | |||
151 | int mingw_putenv(const char *env) | ||
152 | { | ||
153 | char *s; | ||
154 | |||
155 | if ( (s=strchr(env, '=')) == NULL ) { | ||
156 | return unsetenv(env); | ||
157 | } | ||
158 | |||
159 | if ( s[1] != '\0' ) { | ||
160 | return putenv(env); | ||
161 | } | ||
162 | |||
163 | /* can't set empty value */ | ||
129 | return 0; | 164 | return 0; |
130 | } | 165 | } |