@@ -759,7 +759,7 @@ static int lookupenv(char **env, const char *name, size_t nmln)
759
759
/*
760
760
* If name contains '=', then sets the variable, otherwise it unsets it
761
761
*/
762
- static char * * do_putenv (char * * env , const char * name )
762
+ static char * * do_putenv (char * * env , const char * name , int free_old )
763
763
{
764
764
char * eq = strchrnul (name , '=' );
765
765
int i = lookupenv (env , name , eq - name );
@@ -774,7 +774,8 @@ static char **do_putenv(char **env, const char *name)
774
774
}
775
775
}
776
776
else {
777
- free (env [i ]);
777
+ if (free_old )
778
+ free (env [i ]);
778
779
if (* eq )
779
780
env [i ] = (char * ) name ;
780
781
else
@@ -803,7 +804,7 @@ char *mingw_getenv(const char *name)
803
804
804
805
int mingw_putenv (const char * namevalue )
805
806
{
806
- environ = do_putenv (environ , namevalue );
807
+ environ = do_putenv (environ , namevalue , 1 );
807
808
return 0 ;
808
809
}
809
810
@@ -996,21 +997,30 @@ static char *path_lookup(const char *cmd, char **path, int exe_only)
996
997
}
997
998
998
999
/*
999
- * Create environment block suitable for CreateProcess.
1000
+ * Create environment block suitable for CreateProcess. Merges current
1001
+ * process environment and the supplied environment changes.
1000
1002
*/
1001
- static wchar_t * make_environment_block (char * * env )
1003
+ static wchar_t * make_environment_block (char * * deltaenv )
1002
1004
{
1003
1005
wchar_t * wenvblk = NULL ;
1004
1006
int count = 0 ;
1005
1007
char * * e , * * tmpenv ;
1006
1008
int size = 0 , wenvsz = 0 , wenvpos = 0 ;
1007
1009
1008
- for ( e = env ; * e ; e ++ )
1010
+ while ( environ [ count ] )
1009
1011
count ++ ;
1010
1012
1011
- /* environment must be sorted */
1013
+ /* copy the environment */
1012
1014
tmpenv = xmalloc (sizeof (* tmpenv ) * (count + 1 ));
1013
- memcpy (tmpenv , env , sizeof (* tmpenv ) * (count + 1 ));
1015
+ memcpy (tmpenv , environ , sizeof (* tmpenv ) * (count + 1 ));
1016
+
1017
+ /* merge supplied environment changes into the temporary environment */
1018
+ for (e = deltaenv ; e && * e ; e ++ )
1019
+ tmpenv = do_putenv (tmpenv , * e , 0 );
1020
+
1021
+ /* environment must be sorted */
1022
+ for (count = 0 ; tmpenv [count ]; )
1023
+ count ++ ;
1014
1024
qsort (tmpenv , count , sizeof (* tmpenv ), compareenv );
1015
1025
1016
1026
/* create environment block from temporary environment */
@@ -1033,7 +1043,7 @@ struct pinfo_t {
1033
1043
struct pinfo_t * pinfo = NULL ;
1034
1044
CRITICAL_SECTION pinfo_cs ;
1035
1045
1036
- static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * env ,
1046
+ static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
1037
1047
const char * dir ,
1038
1048
int prepend_cmd , int fhin , int fhout , int fherr )
1039
1049
{
@@ -1101,8 +1111,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
1101
1111
xutftowcs (wargs , args .buf , 2 * args .len + 1 );
1102
1112
strbuf_release (& args );
1103
1113
1104
- if (env )
1105
- wenvblk = make_environment_block (env );
1114
+ wenvblk = make_environment_block (deltaenv );
1106
1115
1107
1116
memset (& pi , 0 , sizeof (pi ));
1108
1117
ret = CreateProcessW (wcmd , wargs , NULL , NULL , TRUE, flags ,
@@ -1140,10 +1149,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
1140
1149
1141
1150
static pid_t mingw_spawnv (const char * cmd , const char * * argv , int prepend_cmd )
1142
1151
{
1143
- return mingw_spawnve_fd (cmd , argv , environ , NULL , prepend_cmd , 0 , 1 , 2 );
1152
+ return mingw_spawnve_fd (cmd , argv , NULL , NULL , prepend_cmd , 0 , 1 , 2 );
1144
1153
}
1145
1154
1146
- pid_t mingw_spawnvpe (const char * cmd , const char * * argv , char * * env ,
1155
+ pid_t mingw_spawnvpe (const char * cmd , const char * * argv , char * * deltaenv ,
1147
1156
const char * dir ,
1148
1157
int fhin , int fhout , int fherr )
1149
1158
{
@@ -1167,14 +1176,14 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
1167
1176
pid = -1 ;
1168
1177
}
1169
1178
else {
1170
- pid = mingw_spawnve_fd (iprog , argv , env , dir , 1 ,
1179
+ pid = mingw_spawnve_fd (iprog , argv , deltaenv , dir , 1 ,
1171
1180
fhin , fhout , fherr );
1172
1181
free (iprog );
1173
1182
}
1174
1183
argv [0 ] = argv0 ;
1175
1184
}
1176
1185
else
1177
- pid = mingw_spawnve_fd (prog , argv , env , dir , 0 ,
1186
+ pid = mingw_spawnve_fd (prog , argv , deltaenv , dir , 0 ,
1178
1187
fhin , fhout , fherr );
1179
1188
free (prog );
1180
1189
}
@@ -1265,41 +1274,6 @@ int mingw_kill(pid_t pid, int sig)
1265
1274
return -1 ;
1266
1275
}
1267
1276
1268
- static char * * copy_environ (void )
1269
- {
1270
- char * * env ;
1271
- int i = 0 ;
1272
- while (environ [i ])
1273
- i ++ ;
1274
- env = xmalloc ((i + 1 )* sizeof (* env ));
1275
- for (i = 0 ; environ [i ]; i ++ )
1276
- env [i ] = xstrdup (environ [i ]);
1277
- env [i ] = NULL ;
1278
- return env ;
1279
- }
1280
-
1281
- void free_environ (char * * env )
1282
- {
1283
- int i ;
1284
- for (i = 0 ; env [i ]; i ++ )
1285
- free (env [i ]);
1286
- free (env );
1287
- }
1288
-
1289
- /*
1290
- * Copies global environ and adjusts variables as specified by vars.
1291
- */
1292
- char * * make_augmented_environ (const char * const * vars )
1293
- {
1294
- char * * env = copy_environ ();
1295
-
1296
- while (* vars ) {
1297
- const char * v = * vars ++ ;
1298
- env = do_putenv (env , strchr (v , '=' ) ? xstrdup (v ) : v );
1299
- }
1300
- return env ;
1301
- }
1302
-
1303
1277
/*
1304
1278
* Note, this isn't a complete replacement for getaddrinfo. It assumes
1305
1279
* that service contains a numerical port, or that it is null. It
0 commit comments