|
1 |
| -function exe = which(name, fpath) |
| 1 | +function exe = which(filename, fpath) |
2 | 2 | % fileio.which Find executable with name under path
|
3 | 3 | % like Python shutil.which, may return relative or absolute path
|
4 | 4 |
|
5 | 5 | arguments
|
6 |
| - name (1,1) string {mustBeNonzeroLengthText} |
| 6 | + filename (1,1) string {mustBeNonzeroLengthText} |
7 | 7 | fpath (1,:) string = getenv('PATH')
|
8 | 8 | end
|
9 | 9 |
|
10 | 10 | import stdlib.fileio.is_exe
|
11 | 11 | import stdlib.fileio.expanduser
|
12 | 12 |
|
| 13 | +names = filename; |
| 14 | + |
13 | 15 | 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"; |
16 | 21 | end
|
17 | 22 | end
|
18 | 23 |
|
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 |
24 | 33 | end
|
25 |
| -end |
| 34 | + |
| 35 | +end % for name |
| 36 | + |
| 37 | +%% path given |
26 | 38 |
|
27 | 39 | if isscalar(fpath)
|
28 | 40 | fpath = split(expanduser(fpath), pathsep).';
|
|
34 | 46 | fpath = [pwd, fpath];
|
35 | 47 | end
|
36 | 48 |
|
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 |
41 | 56 | end
|
42 |
| -end |
| 57 | + |
| 58 | +end % for name |
43 | 59 |
|
44 | 60 | exe = string.empty;
|
45 | 61 |
|
|
0 commit comments