@@ -899,6 +899,8 @@ static char *path_lookup(const char *cmd, char **path, int exe_only)
899
899
return prog ;
900
900
}
901
901
902
+ static char * * do_putenv (char * * env , const char * name , int free_old );
903
+
902
904
static int compareenv (const void * a , const void * b )
903
905
{
904
906
char * const * ea = a ;
@@ -907,21 +909,30 @@ static int compareenv(const void *a, const void *b)
907
909
}
908
910
909
911
/*
910
- * Create environment block suitable for CreateProcess.
912
+ * Create environment block suitable for CreateProcess. Merges current
913
+ * process environment and the supplied environment changes.
911
914
*/
912
- static wchar_t * make_environment_block (char * * env )
915
+ static wchar_t * make_environment_block (char * * deltaenv )
913
916
{
914
917
wchar_t * wenvblk = NULL ;
915
918
int count = 0 ;
916
919
char * * e , * * tmpenv ;
917
920
int size = 0 , wenvsz = 0 , wenvpos = 0 ;
918
921
919
- for ( e = env ; * e ; e ++ )
922
+ while ( environ [ count ] )
920
923
count ++ ;
921
924
922
- /* environment must be sorted */
925
+ /* copy the environment */
923
926
tmpenv = xmalloc (sizeof (* tmpenv ) * (count + 1 ));
924
- memcpy (tmpenv , env , sizeof (* tmpenv ) * (count + 1 ));
927
+ memcpy (tmpenv , environ , sizeof (* tmpenv ) * (count + 1 ));
928
+
929
+ /* merge supplied environment changes into the temporary environment */
930
+ for (e = deltaenv ; e && * e ; e ++ )
931
+ tmpenv = do_putenv (tmpenv , * e , 0 );
932
+
933
+ /* environment must be sorted */
934
+ for (count = 0 ; tmpenv [count ]; )
935
+ count ++ ;
925
936
qsort (tmpenv , count , sizeof (* tmpenv ), compareenv );
926
937
927
938
/* create environment block from temporary environment */
@@ -944,7 +955,7 @@ struct pinfo_t {
944
955
static struct pinfo_t * pinfo = NULL ;
945
956
CRITICAL_SECTION pinfo_cs ;
946
957
947
- static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * env ,
958
+ static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
948
959
const char * dir ,
949
960
int prepend_cmd , int fhin , int fhout , int fherr )
950
961
{
@@ -1012,8 +1023,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
1012
1023
xutftowcs (wargs , args .buf , 2 * args .len + 1 );
1013
1024
strbuf_release (& args );
1014
1025
1015
- if (env )
1016
- wenvblk = make_environment_block (env );
1026
+ wenvblk = make_environment_block (deltaenv );
1017
1027
1018
1028
memset (& pi , 0 , sizeof (pi ));
1019
1029
ret = CreateProcessW (wcmd , wargs , NULL , NULL , TRUE, flags ,
@@ -1051,10 +1061,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
1051
1061
1052
1062
static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
1053
1063
{
1054
- return mingw_spawnve_fd (cmd , argv , environ , NULL , prepend_cmd , 0 , 1 , 2 );
1064
+ return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
1055
1065
}
1056
1066
1057
- pid_t mingw_spawnvpe (const char * cmd , const char * * argv , char * * env ,
1067
+ pid_t mingw_spawnvpe (const char * cmd , const char * * argv , char * * deltaenv ,
1058
1068
const char * dir ,
1059
1069
int fhin , int fhout , int fherr )
1060
1070
{
@@ -1078,14 +1088,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
1078
1088
pid = -1 ;
1079
1089
}
1080
1090
else {
1081
- pid = mingw_spawnve_fd (iprog , argv , env , dir , 1 ,
1091
+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
1082
1092
fhin , fhout , fherr );
1083
1093
free (iprog );
1084
1094
}
1085
1095
argv [0 ] = argv0 ;
1086
1096
}
1087
1097
else
1088
- pid = mingw_spawnve_fd (prog , argv , env , dir , 0 ,
1098
+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
1089
1099
fhin , fhout , fherr );
1090
1100
free (prog );
1091
1101
}
@@ -1182,27 +1192,6 @@ int mingw_kill(pid_t pid, int sig)
1182
1192
return -1 ;
1183
1193
}
1184
1194
1185
- static char * * copy_environ (void )
1186
- {
1187
- char * * env ;
1188
- int i = 0 ;
1189
- while (environ [i ])
1190
- i ++ ;
1191
- env = xmalloc ((i + 1 )* sizeof (* env ));
1192
- for (i = 0 ; environ [i ]; i ++ )
1193
- env [i ] = xstrdup (environ [i ]);
1194
- env [i ] = NULL ;
1195
- return env ;
1196
- }
1197
-
1198
- void free_environ (char * * env )
1199
- {
1200
- int i ;
1201
- for (i = 0 ; env [i ]; i ++ )
1202
- free (env [i ]);
1203
- free (env );
1204
- }
1205
-
1206
1195
static int lookupenv (char * * env , const char * name , size_t nmln )
1207
1196
{
1208
1197
int i ;
@@ -1218,7 +1207,7 @@ static int lookupenv(char **env, const char *name, size_t nmln)
1218
1207
/*
1219
1208
* If name contains '=', then sets the variable, otherwise it unsets it
1220
1209
*/
1221
- static char * * do_putenv (char * * env , const char * name )
1210
+ static char * * do_putenv (char * * env , const char * name , int free_old )
1222
1211
{
1223
1212
char * eq = strchrnul (name , '=' );
1224
1213
int i = lookupenv (env , name , eq - name );
@@ -1233,7 +1222,8 @@ static char **do_putenv(char **env, const char *name)
1233
1222
}
1234
1223
}
1235
1224
else {
1236
- free (env [i ]);
1225
+ if (free_old )
1226
+ free (env [i ]);
1237
1227
if (* eq )
1238
1228
env [i ] = (char * ) name ;
1239
1229
else
@@ -1243,20 +1233,6 @@ static char **do_putenv(char **env, const char *name)
1243
1233
return env ;
1244
1234
}
1245
1235
1246
- /*
1247
- * Copies global environ and adjusts variables as specified by vars.
1248
- */
1249
- char * * make_augmented_environ (const char * const * vars )
1250
- {
1251
- char * * env = copy_environ ();
1252
-
1253
- while (* vars ) {
1254
- const char * v = * vars ++ ;
1255
- env = do_putenv (env , strchr (v , '=' ) ? xstrdup (v ) : v );
1256
- }
1257
- return env ;
1258
- }
1259
-
1260
1236
#undef getenv
1261
1237
char * mingw_getenv (const char * name )
1262
1238
{
@@ -1272,7 +1248,7 @@ char *mingw_getenv(const char *name)
1272
1248
1273
1249
int mingw_putenv (const char * namevalue )
1274
1250
{
1275
- environ = do_putenv (environ , namevalue );
1251
+ environ = do_putenv (environ , namevalue , 1 );
1276
1252
return 0 ;
1277
1253
}
1278
1254
0 commit comments