1## Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 John W. Eaton
3## This file is part of Octave.
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.
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.
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/>.
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
36## in which @code{statinfo} is the structure returned from @code{stat}.
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.
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}
52## FIXME -- this is quite slow for large directories, so perhaps
53## it should be converted to C++.
55function retval = dir (file)
64 info = struct (zeros (0, 1),
65 {"name", "date", "bytes", "isdir", "datenum", "statinfo"});
68 if (strcmp (file, "*"))
71 if (strcmp (file, "."))
79 ## Determine the file list for the case where a single directory is
83 [st, err, msg] = stat (fn);
85 warning ("dir: `stat (%s)' failed: %s", fn, msg);
87 elseif (S_ISDIR (st.mode))
88 flst = readdir (flst{1});
91 flst{i} = fullfile (fn, flst{i});
96 if (length (flst) > 0)
100 [st, err, msg] = lstat (fn);
102 warning ("dir: `lstat (%s)' failed: %s", fn, msg);
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);
113 [dummy, fn, ext] = fileparts (fn);
114 fn = cstrcat (fn, ext);
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;
128 error ("dir: expecting directory or filename to be a char array");
131 ## Return the output arguments.
133 ## Return the requested structure.
135 elseif (length (info) > 0)
136 ## Print the structure to the screen.
137 printf ("%s", list_in_columns ({info.name}));
139 warning ("dir: nonexistent file `%s'", file);