function varargout = ls(varargin)
%LS List folder contents.
%   LS displays the results of the 'ls' command on UNIX. On UNIX,
%   LS returns a character row vector of filenames separated
%   by tab and space characters. On Windows, LS returns an m-by-n
%   character array of filenames, where m is the number of filenames
%   and n is the number of characters in the longest filename found.
%   Filenames shorter than n characters are padded with space characters.
%
%   You can pass any flags to LS as well that your operating system supports.
%
%   See also DIR, MKDIR, RMDIR, FILEATTRIB, COPYFILE, MOVEFILE, DELETE.

%   Copyright 1984-2020 The MathWorks, Inc.

% check output arguments
if nargout > 1
    error(message('MATLAB:ls:TooManyOutputArguments'));
end

if nargin > 0
    [varargin{:}] = convertStringsToChars(varargin{:});
    if ~iscellstr(varargin)
        error(message('MATLAB:ls:InputsMustBeStrings'));
    end
    
    try
        isURL = any(matlab.io.internal.vfs.validators.isIRI(varargin));
    catch
        isURL = false;
    end
    
    if isURL
        error(message('MATLAB:ls:URLNotAllowed'));
    end
end

% perform platform specific directory listing
try
    nOut = nargout;
    if isunix
        [varargout{1:nOut}] = unixImpl(nOut, varargin{:});
    else
        [varargout{1:nOut}] = winImpl(nOut,varargin{:});
    end
catch ME
    throw(ME);
end
end

% ======================================================
function varargout = unixImpl(nOut,varargin)
import matlab.oss.internal.quoteUnixCmdArg;
pd = pwd;


args = quoteUnixCmdArg(varargin{:});


if isfolder(pd)
    % if pwd has a    $, `, \ ,
    % in unix shell those characters can get expanded (if contained by double qoutes)
    % and have unusual effects. So they must be escaped in that case.
    thePatternToEscpae = characterListPattern("$\`");
    pd = insertBefore(pd, thePatternToEscpae, "\"); %handle pathname with either of '$ \ `
    cmd = ['cd "' pd '" < /dev/null && ls' args];
else
    cmd = ['ls' args];
end
[s,listing] = matlab.system.internal.executeCommand(cmd);

if s ~= 0
    error(message('MATLAB:ls:OSError',listing));
end

if nOut == 0
    disp(listing);
else
    varargout{1} = listing;
end

end

% ======================================================
function listing = winImpl(nOut,varargin)
if numel(varargin) > 1
    error(message('MATLAB:ls:TooManyInputArguments'));
end
% Display output of dir in wide format.
% dir; prints out info.
% d = dir; does not.
if nOut == 0
    dir(varargin{:});
else
    d = dir(varargin{:});
    listing = char(d.name);
end
end
