Skip to content

Commit da680b0

Browse files
committed
fileio.which: Windows: also work with WSL from native Windows
1 parent 32afe70 commit da680b0

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

+stdlib/+fileio/which.m

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
function exe = which(name, fpath)
1+
function exe = which(filename, fpath)
22
% fileio.which Find executable with name under path
33
% like Python shutil.which, may return relative or absolute path
44

55
arguments
6-
name (1,1) string {mustBeNonzeroLengthText}
6+
filename (1,1) string {mustBeNonzeroLengthText}
77
fpath (1,:) string = getenv('PATH')
88
end
99

1010
import stdlib.fileio.is_exe
1111
import stdlib.fileio.expanduser
1212

13+
names = filename;
14+
1315
if ispc
14-
if ~endsWith(name, ".exe")
15-
name = name + ".exe";
16+
% Windows executable filename doesn't necessarily need .exe,
17+
% particularly for WSL executables that is_exe() will detect from
18+
% native Windows Matlab.
19+
if ~endsWith(filename, ".exe")
20+
names(2) = filename + ".exe";
1621
end
1722
end
1823

19-
if strlength(fileparts(name)) > 0
20-
% has directory part
21-
if is_exe(name)
22-
exe = name;
23-
return
24+
%% directory/filename given
25+
for name = names
26+
27+
if strlength(fileparts(name)) > 0
28+
% has directory part
29+
if is_exe(name)
30+
exe = name;
31+
return
32+
end
2433
end
25-
end
34+
35+
end % for name
36+
37+
%% path given
2638

2739
if isscalar(fpath)
2840
fpath = split(expanduser(fpath), pathsep).';
@@ -34,12 +46,16 @@
3446
fpath = [pwd, fpath];
3547
end
3648

37-
for p = fpath
38-
exe = fullfile(p, name);
39-
if is_exe(exe)
40-
return
49+
for name = names
50+
51+
for p = fpath
52+
exe = fullfile(p, name);
53+
if is_exe(exe)
54+
return
55+
end
4156
end
42-
end
57+
58+
end % for name
4359

4460
exe = string.empty;
4561

buildfile.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
plan = buildplan(localfunctions);
33
end
44

5+
function lintTask(~)
6+
assertSuccess(runtests('stdlib.TestLint'))
7+
end
8+
59
function testTask(~)
610
assertSuccess(runtests('stdlib'))
711
end

0 commit comments

Comments
 (0)