Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit fbe6bfc

Browse files
kbleeskasal
authored andcommitted
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 <[email protected]>
1 parent 45f09a0 commit fbe6bfc

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

compat/mingw.c

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,53 @@ char *mingw_getcwd(char *pointer, int len)
713713
return pointer;
714714
}
715715

716+
static int env_compare(const void *a, const void *b)
717+
{
718+
char *const *ea = a;
719+
char *const *eb = b;
720+
return strcasecmp(*ea, *eb);
721+
}
722+
723+
static int lookup_env(char **env, const char *name, size_t nmln)
724+
{
725+
int i;
726+
727+
for (i = 0; env[i]; i++) {
728+
if (!strncasecmp(env[i], name, nmln) && '=' == env[i][nmln])
729+
/* matches */
730+
return i;
731+
}
732+
return -1;
733+
}
734+
735+
/*
736+
* If name contains '=', then sets the variable, otherwise it unsets it
737+
*/
738+
static char **env_setenv(char **env, const char *name)
739+
{
740+
char *eq = strchrnul(name, '=');
741+
int i = lookup_env(env, name, eq-name);
742+
743+
if (i < 0) {
744+
if (*eq) {
745+
for (i = 0; env[i]; i++)
746+
;
747+
env = xrealloc(env, (i+2)*sizeof(*env));
748+
env[i] = (char*) name;
749+
env[i+1] = NULL;
750+
}
751+
}
752+
else {
753+
free(env[i]);
754+
if (*eq)
755+
env[i] = (char*) name;
756+
else
757+
for (; env[i]; i++)
758+
env[i] = env[i+1];
759+
}
760+
return env;
761+
}
762+
716763
#undef getenv
717764
char *mingw_getenv(const char *name)
718765
{
@@ -730,6 +777,12 @@ char *mingw_getenv(const char *name)
730777
return result;
731778
}
732779

780+
int mingw_putenv(const char *namevalue)
781+
{
782+
environ = env_setenv(environ, namevalue);
783+
return 0;
784+
}
785+
733786
/*
734787
* See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
735788
* (Parsing C++ Command-Line Arguments)
@@ -912,13 +965,6 @@ static char *path_lookup(const char *cmd, char **path, int exe_only)
912965
return prog;
913966
}
914967

915-
static int env_compare(const void *a, const void *b)
916-
{
917-
char *const *ea = a;
918-
char *const *eb = b;
919-
return strcasecmp(*ea, *eb);
920-
}
921-
922968
struct pinfo_t {
923969
struct pinfo_t *next;
924970
pid_t pid;
@@ -1207,46 +1253,6 @@ void free_environ(char **env)
12071253
free(env);
12081254
}
12091255

1210-
static int lookup_env(char **env, const char *name, size_t nmln)
1211-
{
1212-
int i;
1213-
1214-
for (i = 0; env[i]; i++) {
1215-
if (!strncasecmp(env[i], name, nmln) && '=' == env[i][nmln])
1216-
/* matches */
1217-
return i;
1218-
}
1219-
return -1;
1220-
}
1221-
1222-
/*
1223-
* If name contains '=', then sets the variable, otherwise it unsets it
1224-
*/
1225-
static char **env_setenv(char **env, const char *name)
1226-
{
1227-
char *eq = strchrnul(name, '=');
1228-
int i = lookup_env(env, name, eq-name);
1229-
1230-
if (i < 0) {
1231-
if (*eq) {
1232-
for (i = 0; env[i]; i++)
1233-
;
1234-
env = xrealloc(env, (i+2)*sizeof(*env));
1235-
env[i] = (char*) name;
1236-
env[i+1] = NULL;
1237-
}
1238-
}
1239-
else {
1240-
free(env[i]);
1241-
if (*eq)
1242-
env[i] = (char*) name;
1243-
else
1244-
for (; env[i]; i++)
1245-
env[i] = env[i+1];
1246-
}
1247-
return env;
1248-
}
1249-
12501256
/*
12511257
* Copies global environ and adjusts variables as specified by vars.
12521258
*/
@@ -1261,12 +1267,6 @@ char **make_augmented_environ(const char *const *vars)
12611267
return env;
12621268
}
12631269

1264-
int mingw_putenv(const char *namevalue)
1265-
{
1266-
environ = env_setenv(environ, namevalue);
1267-
return 0;
1268-
}
1269-
12701270
/*
12711271
* Note, this isn't a complete replacement for getaddrinfo. It assumes
12721272
* that service contains a numerical port, or that it is null. It

0 commit comments

Comments
 (0)