Contents

WHICH find executable in fpath or env var PATH

like Python shutil.which, find executable in fpath or env var PATH does not resolve path. That is, can return relative path if executable is in: * (Windows) in cwd * (all) fpath or Path contains relative paths

find_all option finds all executables specified under PATH, instead of only the first

function exe = which(cmd, fpath, find_all)
% arguments
%   cmd (1,1) string
%   fpath (1,:) string = string.empty
%   find_all (1,1) logical = false
% end
if nargin < 2
  fpath = string.empty;
end
if nargin < 3
  find_all = false;
end

exe = string.empty;

on Windows, append .exe if not suffix is given

if ispc() && stdlib.strempty(stdlib.suffix(cmd))
  cmd = stdlib.append(cmd, '.exe');
end

full filename was given

if stdlib.is_exe(cmd)
  % is_exe implies isfile
  exe = cmd;
  return
end

%  relative directory component, but path was not an executable file
if ~strcmp(stdlib.filename(cmd), cmd)
  return
end

% path given
if stdlib.strempty(fpath)
  fpath = getenv('PATH');
end

fpath = string(fpath);

if isscalar(fpath)
  fpath = split(fpath, pathsep);
end

if iscolumn(fpath)
  fpath = fpath.';
end

for p = fpath
  if stdlib.strempty(p), continue, end

  if endsWith(p, ["/", filesep])
    e = stdlib.append(p, cmd);
  else
    e = stdlib.append(p, '/', cmd);
  end
  if stdlib.is_exe(e)
    if find_all
      exe(end+1) = e; %#ok<AGROW>
    else
      exe = e;
      return
    end
  end
end
end