Mercurial > octave
annotate scripts/miscellaneous/dir.m @ 28683:549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
* dir.m: Add logic to detect wildcard (Matlab only recognizes '*') and disable
special case logic to return directory contents when only one argument to dir
is present. Add BIST test for bug #58976 (this was harder than the fix).
author | Rik <rik@octave.org> |
---|---|
date | Thu, 03 Sep 2020 17:15:01 -0700 |
parents | 9cb04a9e81ec |
children | cf9fe018c9d1 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
3 ## Copyright (C) 2004-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27898
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
4694 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
4694 | 11 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## (at your option) any later version. |
4694 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
18 ## GNU General Public License for more details. |
4694 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
7016 | 21 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23396
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
4694 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20181
diff
changeset
|
27 ## @deftypefn {} {} dir |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20181
diff
changeset
|
28 ## @deftypefnx {} {} dir (@var{directory}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20181
diff
changeset
|
29 ## @deftypefnx {} {[@var{list}] =} dir (@var{directory}) |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
30 ## Display file listing for directory @var{directory}. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
31 ## |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
32 ## 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
|
33 ## |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
34 ## If a return value is requested, return a structure array with the fields |
4694 | 35 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
36 ## @table @asis |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
37 ## @item name |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17908
diff
changeset
|
38 ## File or directory name. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
39 ## |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
40 ## @item folder |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
41 ## Location of file or directory |
23396
945b53af4655
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
23268
diff
changeset
|
42 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
43 ## @item date |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
44 ## Timestamp of file modification (string value). |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
45 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
46 ## @item bytes |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
47 ## File size in bytes. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
48 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
49 ## @item isdir |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17908
diff
changeset
|
50 ## True if name is a directory. |
17513
fedcd3717ebc
doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents:
17397
diff
changeset
|
51 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
52 ## @item datenum |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
53 ## 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
|
54 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
55 ## @item statinfo |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
56 ## Information structure returned from @code{stat}. |
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
57 ## @end table |
4694 | 58 ## |
17397
0bf2fc8562c9
doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents:
17338
diff
changeset
|
59 ## 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
|
60 ## 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
|
61 ## 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
|
62 ## |
7df8f5372ea8
doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 ## @var{directory} is subject to shell expansion if it contains any wildcard |
19198 | 64 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a |
65 ## wildcard character the wildcard must be escaped using the backslash operator | |
66 ## @samp{\}. | |
6955 | 67 ## |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
68 ## Note that for symbolic links, @code{dir} returns information about the |
20181
aa36fb998a4d
maint: Remove unnecessary whitespace at end of lines.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
69 ## file that the symbolic link points to rather than the link itself. |
7007 | 70 ## However, if the link points to a nonexistent file, @code{dir} returns |
6955 | 71 ## information about the link. |
19198 | 72 ## @seealso{ls, readdir, glob, what, stat, lstat} |
4694 | 73 ## @end deftypefn |
74 | |
19198 | 75 ## FIXME: This is quite slow for large directories. |
76 ## Perhaps it should be converted to C++? | |
4694 | 77 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
78 function retval = dir (directory) |
4694 | 79 |
5689 | 80 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
|
81 directory = "."; |
5689 | 82 elseif (nargin > 1) |
6046 | 83 print_usage (); |
4694 | 84 endif |
85 | |
19198 | 86 if (! ischar (directory)) |
87 error ("dir: DIRECTORY argument must be a string"); | |
88 endif | |
89 | |
5690 | 90 ## Prep the retval. |
6947 | 91 info = struct (zeros (0, 1), |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
92 {"name", "folder" "date", "bytes", "isdir", "datenum", "statinfo"}); |
5689 | 93 |
19198 | 94 if (strcmp (directory, "*")) |
95 directory = "."; | |
96 endif | |
97 if (strcmp (directory, ".")) | |
98 flst = {"."}; | |
99 nf = 1; | |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
100 dir_has_wildcard = false; |
19198 | 101 else |
22155
289409b2992d
Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
21634
diff
changeset
|
102 flst = __wglob__ (directory); |
19198 | 103 nf = numel (flst); |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
104 dir_has_wildcard = any (directory == '*'); # See Bug #58976. |
19198 | 105 endif |
5689 | 106 |
19198 | 107 ## Determine the file list for the case where a single directory is specified. |
108 if (nf == 1) | |
109 fn = flst{1}; | |
110 [st, err, msg] = stat (fn); | |
111 if (err < 0) | |
112 warning ("dir: 'stat (%s)' failed: %s", fn, msg); | |
113 nf = 0; | |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
114 elseif (S_ISDIR (st.mode) && ! dir_has_wildcard) |
19198 | 115 flst = readdir (flst{1}); |
116 nf = numel (flst); | |
23248
529c6d0c6684
dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
117 flst = strcat ([fn filesep], flst); |
5690 | 118 endif |
19198 | 119 endif |
5689 | 120 |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
121 if (nf > 0) |
23248
529c6d0c6684
dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
122 |
23256
99ee4dd83ceb
dir.m: Fix regression in 529c6d0c6684.
Rik <rik@octave.org>
parents:
23249
diff
changeset
|
123 fs = regexptranslate ("escape", filesep ("all")); |
23268
efb139519388
dir.m: Do not include trailing filesep in "folder". Fix BIST. (bug #50504).
Markus Mützel <markus.muetzel@gmx.de>
parents:
23265
diff
changeset
|
124 re = sprintf ('(^.+)[%s]([^%s.]*)([.][^%s]*)?$', fs, fs, fs); |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
125 last_dir = last_absdir = ""; |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
126 info(nf,1).name = ""; # pre-declare size of struct array |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
127 |
19198 | 128 ## Collect results. |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
129 cnt = 0; |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
130 for i = 1:nf |
19198 | 131 fn = flst{i}; |
132 [st, err, msg] = lstat (fn); | |
133 if (err < 0) | |
134 warning ("dir: 'lstat (%s)' failed: %s", fn, msg); | |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
135 continue; |
19198 | 136 else |
137 ## If we are looking at a link that points to something, | |
138 ## return info about the target of the link, otherwise, return | |
139 ## info about the link itself. | |
140 if (S_ISLNK (st.mode)) | |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
141 [xst, err] = stat (fn); |
19198 | 142 if (! err) |
143 st = xst; | |
144 endif | |
145 endif | |
27333
2c4759c8239c
dir.m: Tweak code for 10% performance improvement.
Rik <rik@octave.org>
parents:
27329
diff
changeset
|
146 tmpdir = regexprep (fn, re, '$1'); |
27569
29ccef7e5295
New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents:
27333
diff
changeset
|
147 if (is_same_file (fn, tmpdir)) |
27333
2c4759c8239c
dir.m: Tweak code for 10% performance improvement.
Rik <rik@octave.org>
parents:
27329
diff
changeset
|
148 ## regexrep failed to match, no directory component. |
28009
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
149 no_dir = true; |
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
150 else |
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
151 no_dir = false; |
27328
29e52bcc72af
dir.m: Fix occasionally returning incorrect folder field (bug #55448).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
152 endif |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
153 fn = regexprep (fn, re, '$2$3'); |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
154 info(++cnt).name = fn; |
28009
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
155 if (no_dir && fn != ".") |
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
156 tmpdir = "."; |
e3e26f54bfdc
dir.m: Return correct folder for '.' file (bug #57666)
Rik <rik@octave.org>
parents:
27923
diff
changeset
|
157 endif |
27569
29ccef7e5295
New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents:
27333
diff
changeset
|
158 if (! is_same_file (last_dir, tmpdir)) |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
159 ## Caching mechanism to speed up function |
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
160 last_dir = tmpdir; |
28034
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
161 if (ispc () && strncmp (last_dir, '\\', 2)) |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
162 ## Windows UNC network file name is used as is |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
163 last_absdir = last_dir; |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
164 else |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
165 last_absdir = canonicalize_file_name (last_dir); |
673fb7081ebe
Supply folder field for dir() on Windows UNC shares (bug #57664).
Daniel J Sebald <daniel.sebald@ieee.org>
parents:
28010
diff
changeset
|
166 endif |
23265
44d51b2b691c
dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents:
23256
diff
changeset
|
167 endif |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
168 info(cnt).folder = last_absdir; |
19198 | 169 lt = localtime (st.mtime); |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
170 info(cnt).date = strftime ("%d-%b-%Y %T", lt); |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
171 info(cnt).bytes = st.size; |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
172 info(cnt).isdir = S_ISDIR (st.mode); |
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
173 info(cnt).datenum = [lt.year + 1900, lt.mon + 1, lt.mday, ... |
23249
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
174 lt.hour, lt.min, lt.sec]; |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
175 info(cnt).statinfo = st; |
19198 | 176 endif |
177 endfor | |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
178 info((cnt+1):end) = []; # remove any unused entries |
23249
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
179 ## A lot of gymnastics in order to call datenum just once. 2x speed up. |
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
180 dvec = [info.datenum]([[1:6:end]', [2:6:end]', [3:6:end]', ... |
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
181 [4:6:end]', [5:6:end]', [6:6:end]']); |
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
182 dnum = datenum (dvec); |
27241
733431da9742
dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents:
26763
diff
changeset
|
183 ctmp = mat2cell (dnum, ones (cnt,1), 1); |
23249
21fc54e4bb7b
dir.m: Improve performance by another 2X (bug #50416).
Rik <rik@octave.org>
parents:
23248
diff
changeset
|
184 [info.datenum] = ctmp{:}; |
5689 | 185 endif |
186 | |
5690 | 187 ## Return the output arguments. |
5689 | 188 if (nargout > 0) |
5690 | 189 ## Return the requested structure. |
5689 | 190 retval = info; |
19198 | 191 elseif (numel (info) > 0) |
5690 | 192 ## Print the structure to the screen. |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
193 printf ("%s", list_in_columns ({info.name})); |
5689 | 194 else |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14327
diff
changeset
|
195 warning ("dir: nonexistent directory '%s'", directory); |
5689 | 196 endif |
197 | |
4694 | 198 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16994
diff
changeset
|
199 |
19198 | 200 |
201 %!test | |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
202 %! orig_dir = pwd (); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
203 %! tmp_dir = tempname (); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
204 %! unwind_protect |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
205 %! assert (mkdir (tmp_dir)); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
206 %! chdir (tmp_dir); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
207 %! fclose (fopen ("f1", "w")); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
208 %! list = dir (); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
209 %! assert (isstruct (list) && ! isempty (list)); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
210 %! assert (fieldnames (list), |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
211 %! {"name"; "folder"; "date"; "bytes"; "isdir"; "datenum"; "statinfo"}); |
19198 | 212 %! |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
213 %! if (isunix ()) |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
214 %! idx = find (strcmp ({list.name}, "."), 1); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
215 %! assert ({list(idx:idx+1).name}, {".", ".."}); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
216 %! assert ([list(idx:idx+1).isdir], [true true]); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
217 %! endif |
19198 | 218 %! |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
219 %! ## test that specifying a filename works the same as using a directory. |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
220 %! found = find (! [list.isdir], 1); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
221 %! if (! isempty (found)) |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
222 %! list2 = dir (fullfile (list(found).folder, list(found).name)); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
223 %! assert (list(found), list2); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
224 %! endif |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
225 %! unwind_protect_cleanup |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
226 %! chdir (orig_dir); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
227 %! confirm_recursive_rmdir (false, "local"); |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
228 %! if (exist (tmp_dir)) |
28087
9cb04a9e81ec
rmdir: throw an error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
28034
diff
changeset
|
229 %! sts = rmdir (tmp_dir, "s"); |
26763
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
230 %! endif |
12fe42e16841
use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
231 %! end_unwind_protect |
19198 | 232 |
28683
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
233 %!test <*58976> |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
234 %! orig_dir = pwd (); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
235 %! tmp_dir = tempname (); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
236 %! unwind_protect |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
237 %! assert (mkdir (tmp_dir)); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
238 %! assert (mkdir (fullfile (tmp_dir, "dir1"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
239 %! assert (mkdir (fullfile (tmp_dir, "dir2"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
240 %! chdir (fullfile (tmp_dir, "dir1")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
241 %! fclose (fopen ("f1", "w")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
242 %! chdir (tmp_dir); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
243 %! |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
244 %! ## Wildcard with multiple matches lists directories |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
245 %! list = dir (fullfile (tmp_dir, "dir*")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
246 %! keyboard; |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
247 %! assert (numel (list) == 2); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
248 %! assert ({list.name}, {"dir1", "dir2"}); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
249 %! |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
250 %! ## Wildcard with single match lists directories |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
251 %! assert (rmdir (fullfile (tmp_dir, "dir2"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
252 %! list = dir (fullfile (tmp_dir, "dir*")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
253 %! assert (numel (list) == 1); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
254 %! assert ({list.name}, {"dir1"}); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
255 %! |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
256 %! ## No wildcard returns listing of directory contents |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
257 %! list = dir (fullfile (tmp_dir, "dir1")); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
258 %! assert (any (strcmp ({list.name}, "f1"))); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
259 %! unwind_protect_cleanup |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
260 %! chdir (orig_dir); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
261 %! confirm_recursive_rmdir (false, "local"); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
262 %! if (exist (tmp_dir)) |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
263 %! sts = rmdir (tmp_dir, "s"); |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
264 %! endif |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
265 %! end_unwind_protect |
549c10384cc2
dir.m: Return directory, not contents, when wildcard specified (bug #58976)
Rik <rik@octave.org>
parents:
28087
diff
changeset
|
266 |
28010
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
267 %!testif ; isunix () <*57666> |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
268 %! orig_dir = pwd (); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
269 %! tmp_dir = tempname (); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
270 %! unwind_protect |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
271 %! assert (mkdir (tmp_dir)); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
272 %! list = dir (tmp_dir); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
273 %! assert (list(1).name, "."); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
274 %! assert (list(1).folder, canonicalize_file_name (tmp_dir)); |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
275 %! unwind_protect_cleanup |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
276 %! if (exist (tmp_dir)) |
28087
9cb04a9e81ec
rmdir: throw an error if operation fails and nargout == 0 (bug #57830).
Rik <rik@octave.org>
parents:
28034
diff
changeset
|
277 %! sts = rmdir (tmp_dir); |
28010
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
278 %! endif |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
279 %! end_unwind_protect |
4f00900ac621
dir.m: Add regression BIST test (bug #57666).
Mike Miller <mtmiller@octave.org>
parents:
28009
diff
changeset
|
280 |
19198 | 281 ## Test input validation |
282 %!error <DIRECTORY argument must be a string> dir (1) | |
283 %!warning <nonexistent directory> dir ("_%UNLIKELY_DIR_NAME%_"); |