Mercurial > octave-antonio
annotate scripts/miscellaneous/dir.m @ 17908:7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
* dir.m, ls.m: Add note about wildcard expansion (bug #40544).
author | Rik <rik@octave.org> |
---|---|
date | Tue, 12 Nov 2013 16:43:42 -0800 |
parents | d63878346099 |
children | 931cc13a6f3b 446c46af4b42 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17513
diff
changeset
|
1 ## Copyright (C) 2004-2013 John W. Eaton |
4694 | 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. | |
4694 | 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/>. | |
4694 | 18 |
19 ## -*- texinfo -*- | |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
20 ## @deftypefn {Function File} {} dir |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
21 ## @deftypefnx {Function File} {} dir (@var{directory}) |
4694 | 22 ## @deftypefnx {Function File} {[@var{list}] =} dir (@var{directory}) |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
23 ## Display file listing for directory @var{directory}. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
24 ## |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
25 ## If @var{directory} is not specified then list the present working directory. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
26 ## |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
27 ## If a return value is requested, return a structure array with the fields |
4694 | 28 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
29 ## @table @asis |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
30 ## @item name |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
31 ## File or directory name. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
32 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
33 ## @item date |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
34 ## Timestamp of file modification (string value). |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
35 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
36 ## @item bytes |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
37 ## File size in bytes. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
38 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
39 ## @item isdir |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
40 ## True if name is a directory. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
41 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
42 ## @item datenum |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
43 ## Timestamp of file modification as serial date number (double). |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
44 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
45 ## @item statinfo |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
46 ## Information structure returned from @code{stat}. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
47 ## @end table |
4694 | 48 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
49 ## If @var{directory} is a filename, rather than a directory, then return |
17908
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
50 ## information about the named file. @var{directory} may also be a list rather |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 ## than a single directory or file. |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
52 ## |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
53 ## @var{directory} is subject to shell expansion if it contains any wildcard |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
54 ## 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
|
55 ## 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
|
56 ## backslash operator @samp{\}. |
6955 | 57 ## |
58 ## Note that for symbolic links, @code{dir} returns information about | |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
59 ## the file that the symbolic link points to rather than the link itself. |
7007 | 60 ## However, if the link points to a nonexistent file, @code{dir} returns |
6955 | 61 ## information about the link. |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
62 ## @seealso{ls, readdir, glob, what, stat} |
4694 | 63 ## @end deftypefn |
64 | |
65 ## Author: jwe | |
66 | |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
67 ## FIXME: This is quite slow for large directories, so perhaps |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
68 ## it should be converted to C++. |
4694 | 69 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
70 function retval = dir (directory) |
4694 | 71 |
5689 | 72 if (nargin == 0) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
73 directory = "."; |
5689 | 74 elseif (nargin > 1) |
6046 | 75 print_usage (); |
4694 | 76 endif |
77 | |
5690 | 78 ## Prep the retval. |
6947 | 79 info = struct (zeros (0, 1), |
10549 | 80 {"name", "date", "bytes", "isdir", "datenum", "statinfo"}); |
5689 | 81 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
82 if (ischar (directory)) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
83 if (strcmp (directory, "*")) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
84 directory = "."; |
5689 | 85 endif |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
86 if (strcmp (directory, ".")) |
5690 | 87 flst = {"."}; |
88 nf = 1; | |
89 else | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
90 flst = glob (directory); |
5690 | 91 nf = length (flst); |
92 endif | |
5689 | 93 |
5690 | 94 ## Determine the file list for the case where a single directory is |
95 ## specified. | |
96 if (nf == 1) | |
97 fn = flst{1}; | |
6955 | 98 [st, err, msg] = stat (fn); |
5689 | 99 if (err < 0) |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14327
diff
changeset
|
100 warning ("dir: 'stat (%s)' failed: %s", fn, msg); |
10549 | 101 nf = 0; |
6955 | 102 elseif (S_ISDIR (st.mode)) |
10549 | 103 flst = readdir (flst{1}); |
104 nf = length (flst); | |
105 for i = 1:nf | |
106 flst{i} = fullfile (fn, flst{i}); | |
107 endfor | |
5689 | 108 endif |
109 endif | |
110 | |
5690 | 111 if (length (flst) > 0) |
5689 | 112 ## Collect results. |
113 for i = nf:-1:1 | |
10549 | 114 fn = flst{i}; |
115 [st, err, msg] = lstat (fn); | |
116 if (err < 0) | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14327
diff
changeset
|
117 warning ("dir: 'lstat (%s)' failed: %s", fn, msg); |
10549 | 118 else |
119 ## If we are looking at a link that points to something, | |
120 ## return info about the target of the link, otherwise, return | |
121 ## info about the link itself. | |
122 if (S_ISLNK (st.mode)) | |
123 [xst, err, msg] = stat (fn); | |
124 if (! err) | |
125 st = xst; | |
126 endif | |
127 endif | |
128 [dummy, fn, ext] = fileparts (fn); | |
16994
333243133364
Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents:
15466
diff
changeset
|
129 fn = [fn ext]; |
10549 | 130 info(i,1).name = fn; |
131 lt = localtime (st.mtime); | |
132 info(i,1).date = strftime ("%d-%b-%Y %T", lt); | |
133 info(i,1).bytes = st.size; | |
134 info(i,1).isdir = S_ISDIR (st.mode); | |
135 info(i,1).datenum = datenum (lt.year + 1900, lt.mon + 1, lt.mday, | |
136 lt.hour, lt.min, lt.sec); | |
137 info(i,1).statinfo = st; | |
138 endif | |
5689 | 139 endfor |
5690 | 140 endif |
5689 | 141 |
142 else | |
143 error ("dir: expecting directory or filename to be a char array"); | |
144 endif | |
145 | |
5690 | 146 ## Return the output arguments. |
5689 | 147 if (nargout > 0) |
5690 | 148 ## Return the requested structure. |
5689 | 149 retval = info; |
5690 | 150 elseif (length (info) > 0) |
151 ## Print the structure to the screen. | |
152 printf ("%s", list_in_columns ({info.name})); | |
5689 | 153 else |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14327
diff
changeset
|
154 warning ("dir: nonexistent directory '%s'", directory); |
5689 | 155 endif |
156 | |
4694 | 157 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
158 |