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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
2 ##
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
4 ##
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6943
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6943
diff changeset
8 ## your option) any later version.
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
9 ##
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
14 ##
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6943
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6943
diff changeset
17 ## <http://www.gnu.org/licenses/>.
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
18
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
21 ## @deftypefnx {Command} {} ls @var{filenames}
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
22 ## @deftypefnx {Command} {} ls @var{options}
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
23 ## @deftypefnx {Command} {} ls @var{options} @var{filenames}
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
24 ## @deftypefnx {Function File} {@var{list} =} ls (@dots{})
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
25 ##
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
26 ## List directory contents.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
27 ##
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
28 ## The @code{ls} command is implemented by calling the native operating
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
29 ## system's directory listing command---available @var{options} will vary from
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
30 ## system to system.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
31 ##
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
32 ## Filenames are subject to shell expansion if they contain any wildcard
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
33 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
34 ## wildcard character the wildcard must be escaped using the backslash operator
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
35 ## @samp{\}.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
36 ##
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
37 ## If the optional output @var{list} is requested then @code{ls} returns a
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
38 ## character array with one row for each file/directory name.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
39 ##
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
42 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
43 ## @group
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
44 ## ls -l
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
45 ## @print{} total 12
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
46 ## @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
e1851653d59c Eliminate @deffn macros.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
52 ## @end deftypefn
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
53
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
54 ## Author: jwe
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
55
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
56 function retval = ls (varargin)
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
57
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
58 global __ls_command__;
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
59
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
60 if (isempty (__ls_command__) || ! ischar (__ls_command__))
19232
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
61 ls_command (); # Initialize global value for __ls_command__.
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
62 endif
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
104 endif
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
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
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
110 endif
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
111
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
112 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
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
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
131 %!error <all arguments must be character strings> ls (1)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
132 ## Test below is valid, but produces confusing output on screen
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 19024
diff changeset
133 %!#error <command exited abnormally> ls ("-!")
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
134