Mercurial > octave
annotate 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 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30267
diff
changeset
|
3 ## Copyright (C) 2006-2022 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27898
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
6115 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
6115 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
6115 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
6115 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
6115 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20592
diff
changeset
|
27 ## @deftypefn {} {} ls |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20592
diff
changeset
|
28 ## @deftypefnx {} {} ls @var{filenames} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20592
diff
changeset
|
29 ## @deftypefnx {} {} ls @var{options} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20592
diff
changeset
|
30 ## @deftypefnx {} {} ls @var{options} @var{filenames} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20592
diff
changeset
|
31 ## @deftypefnx {} {@var{list} =} ls (@dots{}) |
19198 | 32 ## |
33 ## List directory contents. | |
34 ## | |
30926
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
35 ## The @code{ls} function forwards to the @code{ls} command if it is available. |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
36 ## It falls back to calling the native operating system's directory listing |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
37 ## command. Available @var{options} may vary from system to system. |
19198 | 38 ## |
39 ## Filenames are subject to shell expansion if they contain any wildcard | |
40 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a | |
41 ## wildcard character the wildcard must be escaped using the backslash operator | |
42 ## @samp{\}. | |
43 ## | |
44 ## If the optional output @var{list} is requested then @code{ls} returns a | |
45 ## character array with one row for each file/directory name. | |
46 ## | |
47 ## Example usage on a UNIX-like system: | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11547
diff
changeset
|
48 ## |
6115 | 49 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
50 ## @group |
6115 | 51 ## ls -l |
52 ## @print{} total 12 | |
53 ## @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m | |
54 ## @print{} -rw-r--r-- 1 jwe users 1315 Aug 17 23:14 bar.m | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
55 ## @end group |
6115 | 56 ## @end example |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11547
diff
changeset
|
57 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
16711
diff
changeset
|
58 ## @seealso{dir, readdir, glob, what, stat, filesep, ls_command} |
11547 | 59 ## @end deftypefn |
6115 | 60 |
61 function retval = ls (varargin) | |
62 | |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
63 if (! iscellstr (varargin)) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
64 error ("ls: all arguments must be character strings"); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
65 endif |
6115 | 66 |
30926
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
67 ls_cmd = ls_command (); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
68 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
69 if (nargin > 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
70 args = tilde_expand (varargin); |
30926
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
71 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
72 if (ispc () && ! isunix ()) |
30926
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
73 if (strncmp (ls_cmd, "ls", 2)) |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
74 ## Replace backslashes with forward slashes (unless they escape a glob |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
75 ## pattern) |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
76 args = regexprep (args, '\\(?![\*\?\[\]])', '/'); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
77 ## Enclose paths, potentially having spaces, in double quotes: |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
78 args = strcat ('"', args, '"'); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
79 ## Exclude glob patterns from quoted part of FILENAMES string |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
80 args = regexprep (args, '(?<!\\)([\*\?])', '"$1"'); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
81 args = regexprep (args, '(?<!\\)\[', '"['); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
82 args = regexprep (args, '(?<!\\)\[', ']"'); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
83 else |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
84 idx = ! strncmp (args, '/', 1); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
85 ## Enclose paths, potentially having spaces, in double quotes: |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
86 args(idx) = strcat ('"', args(idx), '"'); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
87 ## shell (cmd.exe) on MinGW uses '^' as escape character |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
88 args = regexprep (args, '([^\w.*?])', '^$1'); |
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
89 endif |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
90 else |
18241
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18183
diff
changeset
|
91 ## Escape any special characters in filename |
18988
12d1297d3a38
ls.m: Fix bug listing directories with spaces in name (bug #42920)
Rik <rik@octave.org>
parents:
18241
diff
changeset
|
92 args = regexprep (args, '([^][\w.*?-])', '\\$1'); |
19211
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
93 ## Undo escaped spaces following command args |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
94 ## Only used for command form where single str contains many args. |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
95 ## Example: list = ls ("-l /usr/bin") |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
96 args = regexprep (args, '(-\w+)(?:\\ )+', '$1 '); |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
97 endif |
30926
5044c6918fdf
Fix "ls" with glob patterns on Windows (bug #62282).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30564
diff
changeset
|
98 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
99 args = sprintf ("%s ", args{:}); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
100 else |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
101 args = ""; |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
102 endif |
6115 | 103 |
30267
535f97bb5f86
ls: Use "ls" command also on Windows if available.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
104 if (nargout > 0 && strncmp (ls_cmd, "ls", 2)) |
20592
56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
19697
diff
changeset
|
105 args = ["-1 ", args]; |
56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
19697
diff
changeset
|
106 endif |
56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
19697
diff
changeset
|
107 |
30267
535f97bb5f86
ls: Use "ls" command also on Windows if available.
Markus Mützel <markus.muetzel@gmx.de>
parents:
29358
diff
changeset
|
108 cmd = [ls_cmd, " ", args]; |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
109 |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
110 if (page_screen_output () || nargout > 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
111 [status, output] = system (cmd); |
6115 | 112 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
113 if (status != 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
114 error ("ls: command exited abnormally with status %d\n", status); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
115 elseif (nargout == 0) |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
116 puts (output); |
6115 | 117 else |
19211
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
118 retval = strvcat (regexp (output, "[\r\n]+", "split"){:}); |
6115 | 119 endif |
120 else | |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
121 ## Just let the output flow if the pager is off. That way the |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
122 ## output from things like "ls -R /" will show up immediately and |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
123 ## we won't have to buffer all the output. |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
124 system (cmd); |
6115 | 125 endif |
126 | |
127 endfunction | |
7411 | 128 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
129 |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
130 %!test |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
131 %! list = ls (); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
132 %! assert (ischar (list)); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
133 %! assert (! isempty (list)); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
134 |
19211
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
135 %!test |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
136 %! if (isunix ()) |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
137 %! list = ls ("/"); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
138 %! list = (list')(:)'; # transform to a single row vector |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
139 %! assert (! isempty (strfind (list, "sbin"))); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
140 %! list2 = ls ("-l /"); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
141 %! list2 = (list2')(:)'; # transform to a single row vector |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
142 %! assert (! isempty (strfind (list2, "sbin"))); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
143 %! assert (rows (list) == rows (list2)); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19211
diff
changeset
|
144 %! endif |
19211
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19202
diff
changeset
|
145 |
19198 | 146 %!error <all arguments must be character strings> ls (1) |
147 ## Test below is valid, but produces confusing output on screen | |
148 %!#error <command exited abnormally> ls ("-!") |