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