annotate scripts/miscellaneous/dir.m @ 7017:a1dbe9d80eee

[project @ 2007-10-12 21:27:11 by jwe]
author jwe
date Fri, 12 Oct 2007 21:27:37 +0000
parents 93c65f2a5668
children 3422f39573b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2004, 2005, 2006, 2007 John W. Eaton
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
2 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
4 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7007
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7007
diff changeset
8 ## your option) any later version.
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
9 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
10 ## 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
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
14 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
15 ## 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
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7007
diff changeset
17 ## <http://www.gnu.org/licenses/>.
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 ## -*- texinfo -*-
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {} dir (@var{directory})
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
21 ## @deftypefnx {Function File} {[@var{list}] =} dir (@var{directory})
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
22 ## Display file listing for directory @var{directory}. If a return
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
23 ## value is requested, return a structure array with the fields
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
24 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
25 ## @example
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
26 ## @group
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
27 ## name
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
28 ## bytes
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
29 ## date
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
30 ## isdir
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
31 ## statinfo
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
32 ## @end group
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
33 ## @end example
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
34 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
35 ## @noindent
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
36 ## in which @code{statinfo} is the structure returned from @code{stat}.
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
37 ##
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
38 ## If @var{directory} is not a directory, return information about the
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
39 ## named @var{filename}. @var{directory} may be a list of directories
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
40 ## specified either by name or with wildcard characters (like * and ?)
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
41 ## which will be expanded with glob.
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
42 ##
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
43 ## Note that for symbolic links, @code{dir} returns information about
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
44 ## the file that a symbolic link points to instead of the link itself.
7007
6304d9ea0a30 [project @ 2007-10-11 16:26:36 by jwe]
jwe
parents: 6955
diff changeset
45 ## 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
46 ## information about the link.
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
47 ## @seealso{ls, stat, lstat, readdir, glob, filesep}
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
48 ## @end deftypefn
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
49
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
50 ## Author: jwe
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
51
6115
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents: 6046
diff changeset
52 ## PKG_ADD: mark_as_command dir
bade9ff1814b [project @ 2006-10-27 17:58:06 by jwe]
jwe
parents: 6046
diff changeset
53
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5750
diff changeset
54 ## FIXME -- this is quite slow for large directories, so perhaps
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
55 ## it should be converted to C++.
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
56
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
57 function retval = dir (file)
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
58
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
59 if (nargin == 0)
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
60 file = ".";
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
61 elseif (nargin > 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5775
diff changeset
62 print_usage ();
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
63 endif
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
64
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
65 ## Prep the retval.
6947
4b41514e5ef0 [project @ 2007-10-03 14:47:32 by jwe]
jwe
parents: 6115
diff changeset
66 info = struct (zeros (0, 1),
4b41514e5ef0 [project @ 2007-10-03 14:47:32 by jwe]
jwe
parents: 6115
diff changeset
67 {"name", "date", "bytes", "isdir", "datenum", "statinfo"});
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
68
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
69 if (ischar (file))
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
70 if (strcmp (file, "*"))
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
71 file = ".";
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
72 endif
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
73 if (strcmp (file, "."))
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
74 flst = {"."};
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
75 nf = 1;
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
76 else
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
77 flst = glob (file);
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
78 nf = length (flst);
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
79 endif
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
80
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
81 ## Determine the file list for the case where a single directory is
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
82 ## specified.
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
83 if (nf == 1)
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
84 fn = flst{1};
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
85 [st, err, msg] = stat (fn);
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
86 if (err < 0)
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
87 warning ("dir: `stat (%s)' failed: %s", fn, msg);
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
88 nf = 0;
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
89 elseif (S_ISDIR (st.mode))
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
90 flst = readdir (flst{1});
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
91 nf = length (flst);
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
92 for i = 1:nf
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
93 flst{i} = fullfile (fn, flst{i});
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
94 endfor
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
95 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
96 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
97
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
98 if (length (flst) > 0)
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
99 ## Collect results.
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
100 for i = nf:-1:1
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
101 fn = flst{i};
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
102 [st, err, msg] = lstat (fn);
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
103 if (err < 0)
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
104 warning ("dir: `lstat (%s)' failed: %s", fn, msg);
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
105 else
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
106 ## If we are looking at a link that points to something,
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
107 ## return info about the target of the link, otherwise, return
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
108 ## info about the link itself.
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
109 if (S_ISLNK (st.mode))
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
110 [xst, err, msg] = stat (fn);
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
111 if (! err)
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
112 st = xst;
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
113 endif
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
114 endif
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
115 [dummy, fn, ext] = fileparts (fn);
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
116 fn = strcat (fn, ext);
5750
bd9cd65d51e4 [project @ 2006-04-11 18:43:25 by jwe]
jwe
parents: 5690
diff changeset
117 info(i,1).name = fn;
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
118 lt = localtime (st.mtime);
6947
4b41514e5ef0 [project @ 2007-10-03 14:47:32 by jwe]
jwe
parents: 6115
diff changeset
119 info(i,1).date = strftime ("%d-%b-%Y %T", lt);
5750
bd9cd65d51e4 [project @ 2006-04-11 18:43:25 by jwe]
jwe
parents: 5690
diff changeset
120 info(i,1).bytes = st.size;
6955
4aef2ca14cf9 [project @ 2007-10-04 16:38:11 by jwe]
jwe
parents: 6947
diff changeset
121 info(i,1).isdir = S_ISDIR (st.mode);
6947
4b41514e5ef0 [project @ 2007-10-03 14:47:32 by jwe]
jwe
parents: 6115
diff changeset
122 info(i,1).datenum = datenum (lt.year + 1900, lt.mon, lt.mday,
4b41514e5ef0 [project @ 2007-10-03 14:47:32 by jwe]
jwe
parents: 6115
diff changeset
123 lt.hour, lt.min, lt.sec);
5750
bd9cd65d51e4 [project @ 2006-04-11 18:43:25 by jwe]
jwe
parents: 5690
diff changeset
124 info(i,1).statinfo = st;
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
125 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
126 endfor
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
127 endif
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
128
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
129 else
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
130 error ("dir: expecting directory or filename to be a char array");
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
131 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
132
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
133 ## Return the output arguments.
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
134 if (nargout > 0)
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
135 ## Return the requested structure.
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
136 retval = info;
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
137 elseif (length (info) > 0)
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
138 ## Print the structure to the screen.
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
139 printf ("%s", list_in_columns ({info.name}));
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
140 else
5690
161ebd1f3410 [project @ 2006-03-17 06:17:24 by jwe]
jwe
parents: 5689
diff changeset
141 warning ("dir: nonexistent file `%s'", file);
5689
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
142 endif
9a16df2b1916 [project @ 2006-03-17 05:00:57 by jwe]
jwe
parents: 5642
diff changeset
143
4694
dd8d08c41c0c [project @ 2004-01-11 18:20:51 by jwe]
jwe
parents:
diff changeset
144 endfunction