Skip to content

Commit 9ff15b0

Browse files
committed
Merge branch 'rs/mingw-path-lookup-simplify'
Code simplification. * rs/mingw-path-lookup-simplify: mingw: simplify PATH handling
2 parents ce079b9 + e0ca1ca commit 9ff15b0

File tree

1 file changed

+23
-70
lines changed

1 file changed

+23
-70
lines changed

compat/mingw.c

Lines changed: 23 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -940,67 +940,15 @@ static const char *parse_interpreter(const char *cmd)
940940
return p+1;
941941
}
942942

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-
996943
/*
997944
* exe_only means that we only want to detect .exe files, but not scripts
998945
* (which do not have an extension)
999946
*/
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)
1001949
{
1002950
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);
1004952

1005953
if (!isexe && access(path, F_OK) == 0)
1006954
return xstrdup(path);
@@ -1015,17 +963,29 @@ static char *lookup_prog(const char *dir, const char *cmd, int isexe, int exe_on
1015963
* Determines the absolute path of cmd using the split path in path.
1016964
* If cmd contains a slash or backslash, no lookup is performed.
1017965
*/
1018-
static char *path_lookup(const char *cmd, char **path, int exe_only)
966+
static char *path_lookup(const char *cmd, int exe_only)
1019967
{
968+
const char *path;
1020969
char *prog = NULL;
1021970
int len = strlen(cmd);
1022971
int isexe = len >= 4 && !strcasecmp(cmd+len-4, ".exe");
1023972

1024973
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;
1026979

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+
}
1029989

1030990
return prog;
1031991
}
@@ -1192,8 +1152,7 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
11921152
int fhin, int fhout, int fherr)
11931153
{
11941154
pid_t pid;
1195-
char **path = get_path_split();
1196-
char *prog = path_lookup(cmd, path, 0);
1155+
char *prog = path_lookup(cmd, 0);
11971156

11981157
if (!prog) {
11991158
errno = ENOENT;
@@ -1204,7 +1163,7 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
12041163

12051164
if (interpr) {
12061165
const char *argv0 = argv[0];
1207-
char *iprog = path_lookup(interpr, path, 1);
1166+
char *iprog = path_lookup(interpr, 1);
12081167
argv[0] = prog;
12091168
if (!iprog) {
12101169
errno = ENOENT;
@@ -1222,21 +1181,18 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
12221181
fhin, fhout, fherr);
12231182
free(prog);
12241183
}
1225-
free_path_split(path);
12261184
return pid;
12271185
}
12281186

12291187
static int try_shell_exec(const char *cmd, char *const *argv)
12301188
{
12311189
const char *interpr = parse_interpreter(cmd);
1232-
char **path;
12331190
char *prog;
12341191
int pid = 0;
12351192

12361193
if (!interpr)
12371194
return 0;
1238-
path = get_path_split();
1239-
prog = path_lookup(interpr, path, 1);
1195+
prog = path_lookup(interpr, 1);
12401196
if (prog) {
12411197
int argc = 0;
12421198
const char **argv2;
@@ -1255,7 +1211,6 @@ static int try_shell_exec(const char *cmd, char *const *argv)
12551211
free(prog);
12561212
free(argv2);
12571213
}
1258-
free_path_split(path);
12591214
return pid;
12601215
}
12611216

@@ -1277,16 +1232,14 @@ int mingw_execv(const char *cmd, char *const *argv)
12771232

12781233
int mingw_execvp(const char *cmd, char *const *argv)
12791234
{
1280-
char **path = get_path_split();
1281-
char *prog = path_lookup(cmd, path, 0);
1235+
char *prog = path_lookup(cmd, 0);
12821236

12831237
if (prog) {
12841238
mingw_execv(prog, argv);
12851239
free(prog);
12861240
} else
12871241
errno = ENOENT;
12881242

1289-
free_path_split(path);
12901243
return -1;
12911244
}
12921245

0 commit comments

Comments
 (0)