changelog shortlog tags changeset files revisions annotate raw

scripts/miscellaneous/dir.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (70 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
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
7## the Free Software Foundation; either version 3 of the License, or (at
8## your option) any later version.
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
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
20## @deftypefn {Function File} {} dir (@var{directory})
21## @deftypefnx {Function File} {[@var{list}] =} dir (@var{directory})
22## Display file listing for directory @var{directory}. If a return
23## value is requested, return a structure array with the fields
24##
25## @example
26## @group
27## name
28## bytes
29## date
30## isdir
31## statinfo
32## @end group
33## @end example
34##
35## @noindent
36## in which @code{statinfo} is the structure returned from @code{stat}.
37##
38## If @var{directory} is not a directory, return information about the
39## named @var{filename}. @var{directory} may be a list of directories
40## specified either by name or with wildcard characters (like * and ?)
41## which will be expanded with glob.
42##
43## Note that for symbolic links, @code{dir} returns information about
44## the file that a symbolic link points to instead of the link itself.
45## However, if the link points to a nonexistent file, @code{dir} returns
46## information about the link.
47## @seealso{ls, stat, lstat, readdir, glob, filesep}
48## @end deftypefn
49
50## Author: jwe
51
52## FIXME -- this is quite slow for large directories, so perhaps
53## it should be converted to C++.
54
55function retval = dir (file)
56
57 if (nargin == 0)
58 file = ".";
59 elseif (nargin > 1)
60 print_usage ();
61 endif
62
63 ## Prep the retval.
64 info = struct (zeros (0, 1),
65 {"name", "date", "bytes", "isdir", "datenum", "statinfo"});
66
67 if (ischar (file))
68 if (strcmp (file, "*"))
69 file = ".";
70 endif
71 if (strcmp (file, "."))
72 flst = {"."};
73 nf = 1;
74 else
75 flst = glob (file);
76 nf = length (flst);
77 endif
78
79 ## Determine the file list for the case where a single directory is
80 ## specified.
81 if (nf == 1)
82 fn = flst{1};
83 [st, err, msg] = stat (fn);
84 if (err < 0)
85 warning ("dir: `stat (%s)' failed: %s", fn, msg);
86 nf = 0;
87 elseif (S_ISDIR (st.mode))
88 flst = readdir (flst{1});
89 nf = length (flst);
90 for i = 1:nf
91 flst{i} = fullfile (fn, flst{i});
92 endfor
93 endif
94 endif
95
96 if (length (flst) > 0)
97 ## Collect results.
98 for i = nf:-1:1
99 fn = flst{i};
100 [st, err, msg] = lstat (fn);
101 if (err < 0)
102 warning ("dir: `lstat (%s)' failed: %s", fn, msg);
103 else
104 ## If we are looking at a link that points to something,
105 ## return info about the target of the link, otherwise, return
106 ## info about the link itself.
107 if (S_ISLNK (st.mode))
108 [xst, err, msg] = stat (fn);
109 if (! err)
110 st = xst;
111 endif
112 endif
113 [dummy, fn, ext] = fileparts (fn);
114 fn = cstrcat (fn, ext);
115 info(i,1).name = fn;
116 lt = localtime (st.mtime);
117 info(i,1).date = strftime ("%d-%b-%Y %T", lt);
118 info(i,1).bytes = st.size;
119 info(i,1).isdir = S_ISDIR (st.mode);
120 info(i,1).datenum = datenum (lt.year + 1900, lt.mon + 1, lt.mday,
121 lt.hour, lt.min, lt.sec);
122 info(i,1).statinfo = st;
123 endif
124 endfor
125 endif
126
127 else
128 error ("dir: expecting directory or filename to be a char array");
129 endif
130
131 ## Return the output arguments.
132 if (nargout > 0)
133 ## Return the requested structure.
134 retval = info;
135 elseif (length (info) > 0)
136 ## Print the structure to the screen.
137 printf ("%s", list_in_columns ({info.name}));
138 else
139 warning ("dir: nonexistent file `%s'", file);
140 endif
141
142endfunction