@@ -940,67 +940,15 @@ static const char *parse_interpreter(const char *cmd)
940
940
return p + 1 ;
941
941
}
942
942
943
- /*
944
- * Splits the PATH into parts.
945
- */
946
- static char * * get_path_split (void )
947
- {
948
- char * p , * * path , * envpath = mingw_getenv ("PATH" );
949
- int i , n = 0 ;
950
-
951
- if (!envpath || !* envpath )
952
- return NULL ;
953
-
954
- envpath = xstrdup (envpath );
955
- p = envpath ;
956
- while (p ) {
957
- char * dir = p ;
958
- p = strchr (p , ';' );
959
- if (p ) * p ++ = '\0' ;
960
- if (* dir ) { /* not earlier, catches series of ; */
961
- ++ n ;
962
- }
963
- }
964
- if (!n ) {
965
- free (envpath );
966
- return NULL ;
967
- }
968
-
969
- ALLOC_ARRAY (path , n + 1 );
970
- p = envpath ;
971
- i = 0 ;
972
- do {
973
- if (* p )
974
- path [i ++ ] = xstrdup (p );
975
- p = p + strlen (p )+ 1 ;
976
- } while (i < n );
977
- path [i ] = NULL ;
978
-
979
- free (envpath );
980
-
981
- return path ;
982
- }
983
-
984
- static void free_path_split (char * * path )
985
- {
986
- char * * p = path ;
987
-
988
- if (!path )
989
- return ;
990
-
991
- while (* p )
992
- free (* p ++ );
993
- free (path );
994
- }
995
-
996
943
/*
997
944
* exe_only means that we only want to detect .exe files, but not scripts
998
945
* (which do not have an extension)
999
946
*/
1000
- static char * lookup_prog (const char * dir , const char * cmd , int isexe , int exe_only )
947
+ static char * lookup_prog (const char * dir , int dirlen , const char * cmd ,
948
+ int isexe , int exe_only )
1001
949
{
1002
950
char path [MAX_PATH ];
1003
- snprintf (path , sizeof (path ), "%s/ %s.exe" , dir , cmd );
951
+ snprintf (path , sizeof (path ), "%.*s\\ %s.exe" , dirlen , dir , cmd );
1004
952
1005
953
if (!isexe && access (path , F_OK ) == 0 )
1006
954
return xstrdup (path );
@@ -1015,17 +963,29 @@ static char *lookup_prog(const char *dir, const char *cmd, int isexe, int exe_on
1015
963
* Determines the absolute path of cmd using the split path in path.
1016
964
* If cmd contains a slash or backslash, no lookup is performed.
1017
965
*/
1018
- static char * path_lookup (const char * cmd , char * * path , int exe_only )
966
+ static char * path_lookup (const char * cmd , int exe_only )
1019
967
{
968
+ const char * path ;
1020
969
char * prog = NULL ;
1021
970
int len = strlen (cmd );
1022
971
int isexe = len >= 4 && !strcasecmp (cmd + len - 4 , ".exe" );
1023
972
1024
973
if (strchr (cmd , '/' ) || strchr (cmd , '\\' ))
1025
- prog = xstrdup (cmd );
974
+ return xstrdup (cmd );
975
+
976
+ path = mingw_getenv ("PATH" );
977
+ if (!path )
978
+ return NULL ;
1026
979
1027
- while (!prog && * path )
1028
- prog = lookup_prog (* path ++ , cmd , isexe , exe_only );
980
+ while (!prog ) {
981
+ const char * sep = strchrnul (path , ';' );
982
+ int dirlen = sep - path ;
983
+ if (dirlen )
984
+ prog = lookup_prog (path , dirlen , cmd , isexe , exe_only );
985
+ if (!* sep )
986
+ break ;
987
+ path = sep + 1 ;
988
+ }
1029
989
1030
990
return prog ;
1031
991
}
@@ -1192,8 +1152,7 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
1192
1152
int fhin , int fhout , int fherr )
1193
1153
{
1194
1154
pid_t pid ;
1195
- char * * path = get_path_split ();
1196
- char * prog = path_lookup (cmd , path , 0 );
1155
+ char * prog = path_lookup (cmd , 0 );
1197
1156
1198
1157
if (!prog ) {
1199
1158
errno = ENOENT ;
@@ -1204,7 +1163,7 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
1204
1163
1205
1164
if (interpr ) {
1206
1165
const char * argv0 = argv [0 ];
1207
- char * iprog = path_lookup (interpr , path , 1 );
1166
+ char * iprog = path_lookup (interpr , 1 );
1208
1167
argv [0 ] = prog ;
1209
1168
if (!iprog ) {
1210
1169
errno = ENOENT ;
@@ -1222,21 +1181,18 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
1222
1181
fhin , fhout , fherr );
1223
1182
free (prog );
1224
1183
}
1225
- free_path_split (path );
1226
1184
return pid ;
1227
1185
}
1228
1186
1229
1187
static int try_shell_exec (const char * cmd , char * const * argv )
1230
1188
{
1231
1189
const char * interpr = parse_interpreter (cmd );
1232
- char * * path ;
1233
1190
char * prog ;
1234
1191
int pid = 0 ;
1235
1192
1236
1193
if (!interpr )
1237
1194
return 0 ;
1238
- path = get_path_split ();
1239
- prog = path_lookup (interpr , path , 1 );
1195
+ prog = path_lookup (interpr , 1 );
1240
1196
if (prog ) {
1241
1197
int argc = 0 ;
1242
1198
const char * * argv2 ;
@@ -1255,7 +1211,6 @@ static int try_shell_exec(const char *cmd, char *const *argv)
1255
1211
free (prog );
1256
1212
free (argv2 );
1257
1213
}
1258
- free_path_split (path );
1259
1214
return pid ;
1260
1215
}
1261
1216
@@ -1277,16 +1232,14 @@ int mingw_execv(const char *cmd, char *const *argv)
1277
1232
1278
1233
int mingw_execvp (const char * cmd , char * const * argv )
1279
1234
{
1280
- char * * path = get_path_split ();
1281
- char * prog = path_lookup (cmd , path , 0 );
1235
+ char * prog = path_lookup (cmd , 0 );
1282
1236
1283
1237
if (prog ) {
1284
1238
mingw_execv (prog , argv );
1285
1239
free (prog );
1286
1240
} else
1287
1241
errno = ENOENT ;
1288
1242
1289
- free_path_split (path );
1290
1243
return -1 ;
1291
1244
}
1292
1245
0 commit comments