Mercurial > octave-nkf
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 |
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 -*- | |
11547 | 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 | 23 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## @group |
6115 | 25 ## ls -l |
26 ## @print{} total 12 | |
27 ## @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m | |
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 | 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 | 32 ## The @code{dir} and @code{ls} commands are implemented by calling your |
33 ## system's directory listing command, so the available options may vary | |
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 | 36 ## @end deftypefn |
6115 | 37 |
38 ## Author: jwe | |
39 | |
40 function retval = ls (varargin) | |
41 | |
42 global __ls_command__; | |
43 | |
44 if (isempty (__ls_command__) || ! ischar (__ls_command__)) | |
45 ## Initialize value for __ls_command__. | |
46 ls_command (); | |
47 endif | |
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 | 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 | 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 | 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 | 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 | 79 endif |
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 | 85 endif |
86 | |
87 endfunction | |
7411 | 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 | 96 |