Mercurial > octave-dspies
annotate scripts/miscellaneous/ls.m @ 18990:b517b08d2e24
maint: Merge gui-release to default.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 05 Aug 2014 17:53:36 -0700 |
parents | 12d1297d3a38 0b657f4e7780 |
children |
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 | 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 |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
21 ## @deftypefnx {Command} {} ls filenames |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
22 ## @deftypefnx {Command} {} ls options |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## @deftypefnx {Command} {} ls options filenames |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
24 ## 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
|
25 ## |
6115 | 26 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## @group |
6115 | 28 ## ls -l |
29 ## @print{} total 12 | |
30 ## @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m | |
31 ## @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
|
32 ## @end group |
6115 | 33 ## @end example |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11547
diff
changeset
|
34 ## |
6115 | 35 ## The @code{dir} and @code{ls} commands are implemented by calling your |
17908
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## system's directory listing command, so the available options will vary |
6115 | 37 ## from system to system. |
17908
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
38 ## |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
39 ## Filenames are subject to shell expansion if they contain any wildcard |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 ## characters @samp{*}, @samp{?}, @samp{[]}. If you want to find a |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
41 ## literal example of a wildcard character you must escape it using the |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 ## backslash operator @samp{\}. |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
16711
diff
changeset
|
43 ## @seealso{dir, readdir, glob, what, stat, filesep, ls_command} |
11547 | 44 ## @end deftypefn |
6115 | 45 |
46 ## Author: jwe | |
47 | |
48 function retval = ls (varargin) | |
49 | |
50 global __ls_command__; | |
51 | |
52 if (isempty (__ls_command__) || ! ischar (__ls_command__)) | |
53 ## Initialize value for __ls_command__. | |
54 ls_command (); | |
55 endif | |
56 | |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
57 if (! iscellstr (varargin)) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
58 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
|
59 endif |
6115 | 60 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
61 if (nargin > 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
62 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
|
63 if (ispc () && ! isunix ()) |
18241
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18183
diff
changeset
|
64 idx = ! strncmp (args, '/', 1); |
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18183
diff
changeset
|
65 ## Enclose paths, potentially having spaces, in double quotes: |
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18183
diff
changeset
|
66 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
|
67 ## shell (cmd.exe) on MinGW uses '^' as escape character |
18241
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18183
diff
changeset
|
68 args = regexprep (args, '([^\w.*?])', '^$1'); |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
69 else |
18241
3d22b24863b9
ls.m: Enclose paths with spaces in double quotes on Windows (bug #40989).
Rik <rik@octave.org>
parents:
18183
diff
changeset
|
70 ## 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
|
71 args = regexprep (args, '([^][\w.*?-])', '\\$1'); |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
72 endif |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
73 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
|
74 else |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
75 args = ""; |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
76 endif |
6115 | 77 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
78 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
|
79 |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
80 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
|
81 [status, output] = system (cmd); |
6115 | 82 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
83 if (status != 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
84 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
|
85 elseif (nargout == 0) |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
86 puts (output); |
16711
6b00c6bff60c
ls.m: fix error when listing an empty directory
Mike Miller <mtmiller@ieee.org>
parents:
15700
diff
changeset
|
87 elseif (isempty (output)) |
6b00c6bff60c
ls.m: fix error when listing an empty directory
Mike Miller <mtmiller@ieee.org>
parents:
15700
diff
changeset
|
88 retval = ""; |
6115 | 89 else |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
90 retval = strvcat (regexp (output, '\S+', 'match'){:}); |
6115 | 91 endif |
92 else | |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
93 ## 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
|
94 ## 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
|
95 ## 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
|
96 system (cmd); |
6115 | 97 endif |
98 | |
99 endfunction | |
7411 | 100 |
14007
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
101 |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
102 %!test |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
103 %! list = ls (); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
104 %! assert (ischar (list)); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
105 %! assert (! isempty (list)); |
caa7439203f2
ls.m: Fix handling of arguments with dashes (-l) (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
14000
diff
changeset
|
106 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
107 %!error ls (1) |
7411 | 108 |