annotate scripts/miscellaneous/ls.m @ 17744:d63878346099

maint: Update copyright notices for release.
author John W. Eaton <jwe@octave.org>
date Wed, 23 Oct 2013 22:09:27 -0400
parents 0bf2fc8562c9
children 7df8f5372ea8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17397
diff changeset
1 ## Copyright (C) 2006-2013 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 -*-
11547
e1851653d59c Eliminate @deffn macros.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
20 ## @deftypefn {Command} {} ls options
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
21 ## List directory contents. For example:
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11547
diff changeset
22 ##
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
23 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
24 ## @group
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
25 ## ls -l
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
26 ## @print{} total 12
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
27 ## @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
28 ## @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
29 ## @end group
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
30 ## @end example
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11547
diff changeset
31 ##
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
32 ## The @code{dir} and @code{ls} commands are implemented by calling your
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
33 ## system's directory listing command, so the available options may vary
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
34 ## from system to system.
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 16711
diff changeset
35 ## @seealso{dir, readdir, glob, what, stat, filesep, ls_command}
11547
e1851653d59c Eliminate @deffn macros.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
36 ## @end deftypefn
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
37
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
38 ## Author: jwe
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
39
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
40 function retval = ls (varargin)
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
41
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
42 global __ls_command__;
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
43
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
44 if (isempty (__ls_command__) || ! ischar (__ls_command__))
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
45 ## Initialize value for __ls_command__.
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
46 ls_command ();
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
47 endif
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
48
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
49 if (! iscellstr (varargin))
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
50 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
51 endif
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
52
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
53 if (nargin > 0)
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
54 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
55 if (ispc () && ! isunix ())
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
56 ## shell (cmd.exe) on MinGW uses '^' as escape character
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
57 args = regexprep (args, '([^\w.*? -])', '^$1');
13900
5859cf1d57a6 quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents: 13899
diff changeset
58 else
15700
5d6a5bc65ef1 Fix regexp escaping of arguments to ls
Mike Miller <mtmiller@ieee.org>
parents: 14363
diff changeset
59 args = regexprep (args, '([^\w.*? -])', '\\$1');
13900
5859cf1d57a6 quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents: 13899
diff changeset
60 endif
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
61 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
62 else
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
63 args = "";
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
64 endif
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
65
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
66 cmd = sprintf ("%s %s", __ls_command__, args);
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
67
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
68 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
69 [status, output] = system (cmd);
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
70
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
71 if (status != 0)
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
72 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
73 elseif (nargout == 0)
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
74 puts (output);
16711
6b00c6bff60c ls.m: fix error when listing an empty directory
Mike Miller <mtmiller@ieee.org>
parents: 15700
diff changeset
75 elseif (isempty (output))
6b00c6bff60c ls.m: fix error when listing an empty directory
Mike Miller <mtmiller@ieee.org>
parents: 15700
diff changeset
76 retval = "";
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
77 else
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
78 retval = strvcat (regexp (output, '\S+', 'match'){:});
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
79 endif
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
80 else
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
81 ## 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
82 ## 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
83 ## 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
84 system (cmd);
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
85 endif
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
86
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents:
diff changeset
87 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
88
14007
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
89
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
90 %!test
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
91 %! list = ls ();
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
92 %! assert (ischar (list));
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
93 %! assert (! isempty (list));
caa7439203f2 ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents: 14000
diff changeset
94
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
95 %!error ls (1)
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
96