comparison scripts/miscellaneous/ls.m @ 30926:5044c6918fdf stable

Fix "ls" with glob patterns on Windows (bug #62282). * scripts/miscellaneous/ls.m: Exclude glob patterns from quoted part of string with "ls" on Windows.
author Markus Mützel <markus.muetzel@gmx.de>
date Mon, 11 Apr 2022 15:58:27 +0200
parents 796f54d4ddbf
children b78e2d064eff 7d99816e9709
comparison
equal deleted inserted replaced
30924:e219aacda1e5 30926:5044c6918fdf
30 ## @deftypefnx {} {} ls @var{options} @var{filenames} 30 ## @deftypefnx {} {} ls @var{options} @var{filenames}
31 ## @deftypefnx {} {@var{list} =} ls (@dots{}) 31 ## @deftypefnx {} {@var{list} =} ls (@dots{})
32 ## 32 ##
33 ## List directory contents. 33 ## List directory contents.
34 ## 34 ##
35 ## The @code{ls} command is implemented by calling the native operating 35 ## The @code{ls} function forwards to the @code{ls} command if it is available.
36 ## system's directory listing command---available @var{options} will vary from 36 ## It falls back to calling the native operating system's directory listing
37 ## system to system. 37 ## command. Available @var{options} may vary from system to system.
38 ## 38 ##
39 ## Filenames are subject to shell expansion if they contain any wildcard 39 ## Filenames are subject to shell expansion if they contain any wildcard
40 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a 40 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a
41 ## wildcard character the wildcard must be escaped using the backslash operator 41 ## wildcard character the wildcard must be escaped using the backslash operator
42 ## @samp{\}. 42 ## @samp{\}.
62 62
63 if (! iscellstr (varargin)) 63 if (! iscellstr (varargin))
64 error ("ls: all arguments must be character strings"); 64 error ("ls: all arguments must be character strings");
65 endif 65 endif
66 66
67 ls_cmd = ls_command ();
68
67 if (nargin > 0) 69 if (nargin > 0)
68 args = tilde_expand (varargin); 70 args = tilde_expand (varargin);
71
69 if (ispc () && ! isunix ()) 72 if (ispc () && ! isunix ())
70 idx = ! strncmp (args, '/', 1); 73 if (strncmp (ls_cmd, "ls", 2))
71 ## Enclose paths, potentially having spaces, in double quotes: 74 ## Replace backslashes with forward slashes (unless they escape a glob
72 args(idx) = strcat ('"', args(idx), '"'); 75 ## pattern)
73 ## shell (cmd.exe) on MinGW uses '^' as escape character 76 args = regexprep (args, '\\(?![\*\?\[\]])', '/');
74 args = regexprep (args, '([^\w.*?])', '^$1'); 77 ## Enclose paths, potentially having spaces, in double quotes:
78 args = strcat ('"', args, '"');
79 ## Exclude glob patterns from quoted part of FILENAMES string
80 args = regexprep (args, '(?<!\\)([\*\?])', '"$1"');
81 args = regexprep (args, '(?<!\\)\[', '"[');
82 args = regexprep (args, '(?<!\\)\[', ']"');
83 else
84 idx = ! strncmp (args, '/', 1);
85 ## Enclose paths, potentially having spaces, in double quotes:
86 args(idx) = strcat ('"', args(idx), '"');
87 ## shell (cmd.exe) on MinGW uses '^' as escape character
88 args = regexprep (args, '([^\w.*?])', '^$1');
89 endif
75 else 90 else
76 ## Escape any special characters in filename 91 ## Escape any special characters in filename
77 args = regexprep (args, '([^][\w.*?-])', '\\$1'); 92 args = regexprep (args, '([^][\w.*?-])', '\\$1');
78 ## Undo escaped spaces following command args 93 ## Undo escaped spaces following command args
79 ## Only used for command form where single str contains many args. 94 ## Only used for command form where single str contains many args.
80 ## Example: list = ls ("-l /usr/bin") 95 ## Example: list = ls ("-l /usr/bin")
81 args = regexprep (args, '(-\w+)(?:\\ )+', '$1 '); 96 args = regexprep (args, '(-\w+)(?:\\ )+', '$1 ');
82 endif 97 endif
98
83 args = sprintf ("%s ", args{:}); 99 args = sprintf ("%s ", args{:});
84 else 100 else
85 args = ""; 101 args = "";
86 endif 102 endif
87 103
88 ls_cmd = ls_command ();
89 if (nargout > 0 && strncmp (ls_cmd, "ls", 2)) 104 if (nargout > 0 && strncmp (ls_cmd, "ls", 2))
90 args = ["-1 ", args]; 105 args = ["-1 ", args];
91 endif 106 endif
92 107
93 cmd = [ls_cmd, " ", args]; 108 cmd = [ls_cmd, " ", args];