annotate scripts/miscellaneous/dir.m @ 27918:b442ec6dda5c

use centralized file for copyright info for individual contributors * COPYRIGHT.md: New file. * In most other files, use "Copyright (C) YYYY-YYYY The Octave Project Developers" instead of tracking individual names in separate source files. The motivation is to reduce the effort required to update the notices each year. Until now, the Octave source files contained copyright notices that list individual contributors. I adopted these file-scope copyright notices because that is what everyone was doing 30 years ago in the days before distributed version control systems. But now, with many contributors and modern version control systems, having these file-scope copyright notices causes trouble when we update copyright years or refactor code. Over time, the file-scope copyright notices may become outdated as new contributions are made or code is moved from one file to another. Sometimes people contribute significant patches but do not add a line claiming copyright. Other times, people add a copyright notice for their contribution but then a later refactoring moves part or all of their contribution to another file and the notice is not moved with the code. As a practical matter, moving such notices is difficult -- determining what parts are due to a particular contributor requires a time-consuming search through the project history. Even managing the yearly update of copyright years is problematic. We have some contributors who are no longer living. Should we update the copyright dates for their contributions when we release new versions? Probably not, but we do still want to claim copyright for the project as a whole. To minimize the difficulty of maintaining the copyright notices, I would like to change Octave's sources to use what is described here: https://softwarefreedom.org/resources/2012/ManagingCopyrightInformation.html in the section "Maintaining centralized copyright notices": The centralized notice approach consolidates all copyright notices in a single location, usually a top-level file. This file should contain all of the copyright notices provided project contributors, unless the contribution was clearly insignificant. It may also credit -- without a copyright notice -- anyone who helped with the project but did not contribute code or other copyrighted material. This approach captures less information about contributions within individual files, recognizing that the DVCS is better equipped to record those details. As we mentioned before, it does have one disadvantage as compared to the file-scope approach: if a single file is separated from the distribution, the recipient won't see the contributors' copyright notices. But this can be easily remedied by including a single copyright notice in each file's header, pointing to the top-level file: Copyright YYYY-YYYY The Octave Project Developers See the COPYRIGHT file at the top-level directory of this distribution or at https://octave.org/COPYRIGHT.html. followed by the usual GPL copyright statement. For more background, see the discussion here: https://lists.gnu.org/archive/html/octave-maintainers/2020-01/msg00009.html Most files in the following directories have been skipped intentinally in this changeset: doc libgui/qterminal liboctave/external m4
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 15:38:17 -0500
parents 4d6d21839dfd
children 1891570abac8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
1 ## Copyright (C) 2004-2019 The Octave Project Developers
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 27898
diff changeset
5 ##
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
6 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
7 ## This file is part of Octave.
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23396
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
10 ## 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
11 ## 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
12 ## (at your option) any later version.
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
13 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## GNU General Public License for more details.
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
18 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
19 ## 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: 7007
diff changeset
20 ## 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
21 ## <https://www.gnu.org/licenses/>.
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
22
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
24 ## @deftypefn {} {} dir
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20181
diff changeset
25 ## @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
26 ## @deftypefnx {} {[@var{list}] =} dir (@var{directory})
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
27 ## Display file listing for directory @var{directory}.
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
28 ##
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
29 ## 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
30 ##
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
31 ## If a return value is requested, return a structure array with the fields
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
32 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
33 ## @table @asis
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
34 ## @item name
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17908
diff changeset
35 ## File or directory name.
17513
fedcd3717ebc doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents: 17397
diff changeset
36 ##
23265
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
37 ## @item folder
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
38 ## Location of file or directory
23396
945b53af4655 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 23268
diff changeset
39 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
40 ## @item date
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
41 ## Timestamp of file modification (string value).
17513
fedcd3717ebc doc: grammarcheck of documentation before 3.8 release.
Rik <rik@octave.org>
parents: 17397
diff changeset
42 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
43 ## @item bytes
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
44 ## File size in bytes.
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 isdir
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17908
diff changeset
47 ## True if name is a directory.
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 datenum
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
50 ## 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
51 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
52 ## @item statinfo
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
53 ## Information structure returned from @code{stat}.
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
54 ## @end table
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
55 ##
17397
0bf2fc8562c9 doc: Update documentation for file and directory functions.
Rik <rik@octave.org>
parents: 17338
diff changeset
56 ## 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
57 ## 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
58 ## 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
59 ##
7df8f5372ea8 doc: Add note about wildcard expansion in ls and dir functions (bug #40544).
Rik <rik@octave.org>
parents: 17744
diff changeset
60 ## @var{directory} is subject to shell expansion if it contains any wildcard
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
61 ## characters @samp{*}, @samp{?}, @samp{[]}. To find a literal example of a
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
62 ## wildcard character the wildcard must be escaped using the backslash operator
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
63 ## @samp{\}.
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
64 ##
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
65 ## 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
66 ## file that the symbolic link points to rather than the link itself.
7007
6304d9ea0a30 [project @ 2007-10-11 16:26:36 by jwe]
jwe
parents: 6955
diff changeset
67 ## However, if the link points to a nonexistent file, @code{dir} returns
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
68 ## information about the link.
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
69 ## @seealso{ls, readdir, glob, what, stat, lstat}
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
70 ## @end deftypefn
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
71
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
72 ## FIXME: This is quite slow for large directories.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
73 ## Perhaps it should be converted to C++?
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
74
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
75 function retval = dir (directory)
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
76
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
77 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
78 directory = ".";
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
79 elseif (nargin > 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5775
diff changeset
80 print_usage ();
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
81 endif
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
82
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
83 if (! ischar (directory))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
84 error ("dir: DIRECTORY argument must be a string");
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
85 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
86
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
87 ## Prep the retval.
6947
4b41514e5ef0 [project @ 2007-10-03 14:47:32 by jwe]
jwe
parents: 6115
diff changeset
88 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
89 {"name", "folder" "date", "bytes", "isdir", "datenum", "statinfo"});
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
90
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
91 if (strcmp (directory, "*"))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
92 directory = ".";
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
93 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
94 if (strcmp (directory, "."))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
95 flst = {"."};
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
96 nf = 1;
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
97 else
22155
289409b2992d Allow dir to accept [ and ] in arguments. (bug #47950)
Lachlan Andrew <lachlanbis@gmail.com>
parents: 21634
diff changeset
98 flst = __wglob__ (directory);
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
99 nf = numel (flst);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
100 endif
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
101
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
102 ## Determine the file list for the case where a single directory is specified.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
103 if (nf == 1)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
104 fn = flst{1};
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
105 [st, err, msg] = stat (fn);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
106 if (err < 0)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
107 warning ("dir: 'stat (%s)' failed: %s", fn, msg);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
108 nf = 0;
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
109 elseif (S_ISDIR (st.mode))
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
110 flst = readdir (flst{1});
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
111 nf = numel (flst);
23248
529c6d0c6684 dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents: 23220
diff changeset
112 flst = strcat ([fn filesep], flst);
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
113 endif
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
114 endif
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
115
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
116 if (nf > 0)
23248
529c6d0c6684 dir.m: Improve performance by 2.3X (bug #50416).
Rik <rik@octave.org>
parents: 23220
diff changeset
117
23256
99ee4dd83ceb dir.m: Fix regression in 529c6d0c6684.
Rik <rik@octave.org>
parents: 23249
diff changeset
118 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
119 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
120 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
121 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
122
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
123 ## Collect results.
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
124 cnt = 0;
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
125 for i = 1:nf
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
126 fn = flst{i};
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
127 [st, err, msg] = lstat (fn);
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
128 if (err < 0)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
129 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
130 continue;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
131 else
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
132 ## If we are looking at a link that points to something,
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
133 ## return info about the target of the link, otherwise, return
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
134 ## info about the link itself.
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
135 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
136 [xst, err] = stat (fn);
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
137 if (! err)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
138 st = xst;
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
139 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
140 endif
27333
2c4759c8239c dir.m: Tweak code for 10% performance improvement.
Rik <rik@octave.org>
parents: 27329
diff changeset
141 tmpdir = regexprep (fn, re, '$1');
27569
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27333
diff changeset
142 if (is_same_file (fn, tmpdir))
27333
2c4759c8239c dir.m: Tweak code for 10% performance improvement.
Rik <rik@octave.org>
parents: 27329
diff changeset
143 ## regexrep failed to match, no directory component.
27328
29e52bcc72af dir.m: Fix occasionally returning incorrect folder field (bug #55448).
Rik <rik@octave.org>
parents: 26376
diff changeset
144 tmpdir = ".";
29e52bcc72af dir.m: Fix occasionally returning incorrect folder field (bug #55448).
Rik <rik@octave.org>
parents: 26376
diff changeset
145 endif
23265
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
146 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
147 info(++cnt).name = fn;
27569
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27333
diff changeset
148 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
149 ## 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
150 last_dir = tmpdir;
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
151 last_absdir = canonicalize_file_name (last_dir);
23265
44d51b2b691c dir.m: Add folder entry to output struct for Matlab compatibility (bug #50504).
Rik <rik@octave.org>
parents: 23256
diff changeset
152 endif
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
153 info(cnt).folder = last_absdir;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
154 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
155 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
156 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
157 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
158 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
159 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
160 info(cnt).statinfo = st;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
161 endif
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
162 endfor
27241
733431da9742 dir.m: Gracefully handle race conditions when file is deleted (bug #56618).
Rik <rik@octave.org>
parents: 26763
diff changeset
163 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
164 ## 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
165 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
166 [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
167 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
168 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
169 [info.datenum] = ctmp{:};
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
170 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
171
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
172 ## Return the output arguments.
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
173 if (nargout > 0)
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
174 ## Return the requested structure.
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
175 retval = info;
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
176 elseif (numel (info) > 0)
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
177 ## Print the structure to the screen.
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
178 printf ("%s", list_in_columns ({info.name}));
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
179 else
15466
d174210ce1ec use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents: 14327
diff changeset
180 warning ("dir: nonexistent directory '%s'", directory);
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
181 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
182
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
183 endfunction
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 16994
diff changeset
184
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
185
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
186 %!test
26763
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
187 %! orig_dir = pwd ();
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
188 %! tmp_dir = tempname ();
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
189 %! unwind_protect
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
190 %! assert (mkdir (tmp_dir));
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
191 %! chdir (tmp_dir);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
192 %! fclose (fopen ("f1", "w"));
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
193 %! list = dir ();
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
194 %! assert (isstruct (list) && ! isempty (list));
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
195 %! assert (fieldnames (list),
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
196 %! {"name"; "folder"; "date"; "bytes"; "isdir"; "datenum"; "statinfo"});
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
197 %!
26763
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
198 %! if (isunix ())
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
199 %! idx = find (strcmp ({list.name}, "."), 1);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
200 %! assert ({list(idx:idx+1).name}, {".", ".."});
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
201 %! 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
202 %! endif
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
203 %!
26763
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
204 %! ## 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
205 %! found = find (! [list.isdir], 1);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
206 %! if (! isempty (found))
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
207 %! 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
208 %! assert (list(found), list2);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
209 %! endif
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
210 %! unwind_protect_cleanup
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
211 %! chdir (orig_dir);
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
212 %! confirm_recursive_rmdir (false, "local");
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
213 %! if (exist (tmp_dir))
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
214 %! rmdir (tmp_dir, "s");
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
215 %! endif
12fe42e16841 use temporary directory for testing dir function
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
216 %! end_unwind_protect
19198
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
217
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
218 ## Test input validation
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
219 %!error <DIRECTORY argument must be a string> dir (1)
931cc13a6f3b Rework ls.m and dir.m
Rik <rik@octave.org>
parents: 17908
diff changeset
220 %!warning <nonexistent directory> dir ("_%UNLIKELY_DIR_NAME%_");