Win32: move environment functions

Move environment helper functions up so that they can be reused by
mingw_getenv and mingw_spawnve_fd in subsequent patches.

Signed-off-by: Karsten Blees <blees@dcon.de>
This commit is contained in:
Karsten Blees
2012-01-15 00:05:04 +01:00
committed by Johannes Schindelin
parent 5fd9cab8ff
commit 17bc7bc5f6

View File

@@ -737,6 +737,53 @@ char *mingw_getcwd(char *pointer, int len)
return pointer;
}
static int env_compare(const void *a, const void *b)
{
char *const *ea = a;
char *const *eb = b;
return strcasecmp(*ea, *eb);
}
static int lookup_env(char **env, const char *name, size_t nmln)
{
int i;
for (i = 0; env[i]; i++) {
if (!strncasecmp(env[i], name, nmln) && '=' == env[i][nmln])
/* matches */
return i;
}
return -1;
}
/*
* If name contains '=', then sets the variable, otherwise it unsets it
*/
static char **env_setenv(char **env, const char *name)
{
char *eq = strchrnul(name, '=');
int i = lookup_env(env, name, eq-name);
if (i < 0) {
if (*eq) {
for (i = 0; env[i]; i++)
;
env = xrealloc(env, (i+2)*sizeof(*env));
env[i] = (char*) name;
env[i+1] = NULL;
}
}
else {
free(env[i]);
if (*eq)
env[i] = (char*) name;
else
for (; env[i]; i++)
env[i] = env[i+1];
}
return env;
}
#undef getenv
char *mingw_getenv(const char *name)
{
@@ -754,6 +801,12 @@ char *mingw_getenv(const char *name)
return result;
}
int mingw_putenv(const char *namevalue)
{
environ = env_setenv(environ, namevalue);
return 0;
}
/*
* See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
* (Parsing C++ Command-Line Arguments)
@@ -936,13 +989,6 @@ static char *path_lookup(const char *cmd, char **path, int exe_only)
return prog;
}
static int env_compare(const void *a, const void *b)
{
char *const *ea = a;
char *const *eb = b;
return strcasecmp(*ea, *eb);
}
struct pinfo_t {
struct pinfo_t *next;
pid_t pid;
@@ -1223,46 +1269,6 @@ void free_environ(char **env)
free(env);
}
static int lookup_env(char **env, const char *name, size_t nmln)
{
int i;
for (i = 0; env[i]; i++) {
if (!strncasecmp(env[i], name, nmln) && '=' == env[i][nmln])
/* matches */
return i;
}
return -1;
}
/*
* If name contains '=', then sets the variable, otherwise it unsets it
*/
static char **env_setenv(char **env, const char *name)
{
char *eq = strchrnul(name, '=');
int i = lookup_env(env, name, eq-name);
if (i < 0) {
if (*eq) {
for (i = 0; env[i]; i++)
;
env = xrealloc(env, (i+2)*sizeof(*env));
env[i] = (char*) name;
env[i+1] = NULL;
}
}
else {
free(env[i]);
if (*eq)
env[i] = (char*) name;
else
for (; env[i]; i++)
env[i] = env[i+1];
}
return env;
}
/*
* Copies global environ and adjusts variables as specified by vars.
*/
@@ -1277,12 +1283,6 @@ char **make_augmented_environ(const char *const *vars)
return env;
}
int mingw_putenv(const char *namevalue)
{
environ = env_setenv(environ, namevalue);
return 0;
}
/*
* Note, this isn't a complete replacement for getaddrinfo. It assumes
* that service contains a numerical port, or that it is null. It