# HG changeset patch # User Rik # Date 1323281410 28800 # Node ID caa7439203f2e3ae6d3dfe9761897c92232136c7 # Parent 54f76558c41a8974290931bb80923cb83f83e811 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950) * ls.m: pass cellstr to regexprep, rather than comma-separated list. Add %!test block. diff -r 54f76558c41a -r caa7439203f2 scripts/miscellaneous/ls.m --- a/scripts/miscellaneous/ls.m Wed Dec 07 06:55:12 2011 -0500 +++ b/scripts/miscellaneous/ls.m Wed Dec 07 10:10:10 2011 -0800 @@ -46,49 +46,49 @@ ls_command (); endif - if (iscellstr (varargin)) - - args = tilde_expand (varargin); + if (! iscellstr (varargin)) + error ("ls: all arguments must be character strings"); + endif - if (nargin > 0) - if (ispc () && ! isunix ()) - ## shell (cmd.exe) on MinGW uses '^' as escape character - args = regexprep (args{:}, '([^\w.*?])', '^$1'); - else - args = regexprep (args{:}, '([^\w.*?])', '\$1'); - endif + if (nargin > 0) + args = tilde_expand (varargin); + if (ispc () && ! isunix ()) + ## shell (cmd.exe) on MinGW uses '^' as escape character + args = regexprep (args, '([^\w.*? -])', '^$1'); else - args = ""; + args = regexprep (args, '([^\w.*? -])', '\$1'); endif - - cmd = sprintf ("%s %s", __ls_command__, args); - - if (page_screen_output () || nargout > 0) + args = sprintf ("%s ", args{:}); + else + args = ""; + endif - [status, output] = system (cmd); + cmd = sprintf ("%s %s", __ls_command__, args); + + if (page_screen_output () || nargout > 0) + [status, output] = system (cmd); - if (status == 0) - if (nargout == 0) - puts (output); - else - retval = strvcat (regexp (output, '\S+', 'match'){:}); - endif - else - error ("ls: command exited abnormally with status %d\n", status); - endif - + if (status != 0) + error ("ls: command exited abnormally with status %d\n", status); + elseif (nargout == 0) + puts (output); else - ## Just let the output flow if the pager is off. That way the - ## output from things like "ls -R /" will show up immediately and - ## we won't have to buffer all the output. - system (cmd); + retval = strvcat (regexp (output, '\S+', 'match'){:}); endif - else - error ("ls: expecting all arguments to be character strings"); + ## Just let the output flow if the pager is off. That way the + ## output from things like "ls -R /" will show up immediately and + ## we won't have to buffer all the output. + system (cmd); endif endfunction + +%!test +%! list = ls (); +%! assert (ischar (list)); +%! assert (! isempty (list)); + %!error ls (1);