Mercurial > octave-nkf
annotate scripts/miscellaneous/ls.m @ 20627:56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Leave message empty to abort commit.
HG: --
HG: user: Lachlan Andrew <lachlanbis@gmail.com>
HG: branch 'default'
* ls.m: Add '-1' to arguments on non-PC platforms so that file listing is one
per line.
author | Lachlan Andrew <lachlanbis@gmail.com> |
---|---|
date | Fri, 09 Oct 2015 19:29:28 -0700 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19731
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19631
diff
changeset
|
1 ## Copyright (C) 2006-2015 John W. Eaton |
6115 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6115 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6115 | 18 |
19 ## -*- texinfo -*- | |
17908
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
20 ## @deftypefn {Command} {} ls |
19232 | 21 ## @deftypefnx {Command} {} ls @var{filenames} |
22 ## @deftypefnx {Command} {} ls @var{options} | |
23 ## @deftypefnx {Command} {} ls @var{options} @var{filenames} | |
24 ## @deftypefnx {Function File} {@var{list} =} ls (@dots{}) | |
25 ## | |
26 ## List directory contents. | |
27 ## | |
28 ## The @code{ls} command is implemented by calling the native operating | |
29 ## system's directory listing command---available @var{options} will vary from | |
30 ## system to system. | |
31 ## | |
32 ## Filenames are subject to shell expansion if they contain any wildcard | |
33 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a | |
34 ## wildcard character the wildcard must be escaped using the backslash operator | |
35 ## @samp{\}. | |
36 ## | |
37 ## If the optional output @var{list} is requested then @code{ls} returns a | |
38 ## character array with one row for each file/directory name. | |
39 ## | |
40 ## 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
|
41 ## |
6115 | 42 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
43 ## @group |
6115 | 44 ## ls -l |
45 ## @print{} total 12 | |
46 ## @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m | |
47 ## @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
|
48 ## @end group |
6115 | 49 ## @end example |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11547
diff
changeset
|
50 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
16711
diff
changeset
|
51 ## @seealso{dir, readdir, glob, what, stat, filesep, ls_command} |
11547 | 52 ## @end deftypefn |
6115 | 53 |
54 ## Author: jwe | |
55 | |
56 function retval = ls (varargin) | |
57 | |
58 global __ls_command__; | |
59 | |
60 if (isempty (__ls_command__) || ! ischar (__ls_command__)) | |
19232 | 61 ls_command (); # Initialize global value for __ls_command__. |
6115 | 62 endif |
63 | |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
64 if (! iscellstr (varargin)) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
65 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
|
66 endif |
6115 | 67 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
68 if (nargin > 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
69 args = tilde_expand (varargin); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
70 if (ispc () && ! isunix ()) |
18240
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18182
diff
changeset
|
71 idx = ! strncmp (args, '/', 1); |
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18182
diff
changeset
|
72 ## Enclose paths, potentially having spaces, in double quotes: |
19631
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19245
diff
changeset
|
73 args(idx) = strcat ('"', args(idx), '"'); |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
74 ## shell (cmd.exe) on MinGW uses '^' as escape character |
18240
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18182
diff
changeset
|
75 args = regexprep (args, '([^\w.*?])', '^$1'); |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
76 else |
18240
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18182
diff
changeset
|
77 ## Escape any special characters in filename |
19022
12d1297d3a38
ls.m: Fix bug listing directories with spaces in name (bug #42920)
Rik <rik@octave.org>
parents:
18240
diff
changeset
|
78 args = regexprep (args, '([^][\w.*?-])', '\\$1'); |
19245
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
79 ## Undo escaped spaces following command args |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
80 ## 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:
19236
diff
changeset
|
81 ## Example: list = ls ("-l /usr/bin") |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
82 args = regexprep (args, '(-\w+)(?:\\ )+', '$1 '); |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
83 endif |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
84 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
|
85 else |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
86 args = ""; |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
87 endif |
6115 | 88 |
20627
56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
19731
diff
changeset
|
89 if (nargout > 0 && (isunix () || ismac ())) |
56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
19731
diff
changeset
|
90 args = ["-1 ", args]; |
56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
19731
diff
changeset
|
91 endif |
56333f6df823
ls.m: Produce listing with 1 file per line if nargout > 0 (bug #46002).
Lachlan Andrew <lachlanbis@gmail.com>
parents:
19731
diff
changeset
|
92 |
19236
4871d0ecbfa7
ls.m: Restore missing space between command and arguments
Mike Miller <mtmiller@ieee.org>
parents:
19232
diff
changeset
|
93 cmd = [__ls_command__ " " args]; |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
94 |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
95 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
|
96 [status, output] = system (cmd); |
6115 | 97 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
98 if (status != 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
99 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
|
100 elseif (nargout == 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
101 puts (output); |
6115 | 102 else |
19245
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
103 retval = strvcat (regexp (output, "[\r\n]+", "split"){:}); |
6115 | 104 endif |
105 else | |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
106 ## 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
|
107 ## 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
|
108 ## 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
|
109 system (cmd); |
6115 | 110 endif |
111 | |
112 endfunction | |
7411 | 113 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
114 |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
115 %!test |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
116 %! list = ls (); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
117 %! assert (ischar (list)); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
118 %! assert (! isempty (list)); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
119 |
19245
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
120 %!test |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
121 %! if (isunix ()) |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
122 %! list = ls ("/"); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
123 %! 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:
19236
diff
changeset
|
124 %! assert (! isempty (strfind (list, "sbin"))); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
125 %! list2 = ls ("-l /"); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
126 %! 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:
19236
diff
changeset
|
127 %! assert (! isempty (strfind (list2, "sbin"))); |
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
128 %! assert (rows (list) == rows (list2)); |
19631
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19245
diff
changeset
|
129 %! endif |
19245
bbb1fbd900d4
ls.m: Return Matlab compatible output when called with functional form.
Rik <rik@octave.org>
parents:
19236
diff
changeset
|
130 |
19232 | 131 %!error <all arguments must be character strings> ls (1) |
132 ## Test below is valid, but produces confusing output on screen | |
133 %!#error <command exited abnormally> ls ("-!") | |
7411 | 134 |