4694
|
1 ## Copyright (C) 2004 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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
4694
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
4694
|
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 |
5689
|
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. |
6955
|
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. |
7007
|
45 ## However, if the link points to a nonexistent file, @code{dir} returns |
6955
|
46 ## information about the link. |
|
47 ## @seealso{ls, stat, lstat, readdir, glob, filesep} |
4694
|
48 ## @end deftypefn |
|
49 |
|
50 ## Author: jwe |
|
51 |
6115
|
52 ## PKG_ADD: mark_as_command dir |
|
53 |
5775
|
54 ## FIXME -- this is quite slow for large directories, so perhaps |
4694
|
55 ## it should be converted to C++. |
|
56 |
|
57 function retval = dir (file) |
|
58 |
5689
|
59 if (nargin == 0) |
5690
|
60 file = "."; |
5689
|
61 elseif (nargin > 1) |
6046
|
62 print_usage (); |
4694
|
63 endif |
|
64 |
5690
|
65 ## Prep the retval. |
6947
|
66 info = struct (zeros (0, 1), |
|
67 {"name", "date", "bytes", "isdir", "datenum", "statinfo"}); |
5689
|
68 |
|
69 if (ischar (file)) |
5690
|
70 if (strcmp (file, "*")) |
|
71 file = "."; |
5689
|
72 endif |
5690
|
73 if (strcmp (file, ".")) |
|
74 flst = {"."}; |
|
75 nf = 1; |
|
76 else |
|
77 flst = glob (file); |
|
78 nf = length (flst); |
|
79 endif |
5689
|
80 |
5690
|
81 ## Determine the file list for the case where a single directory is |
|
82 ## specified. |
|
83 if (nf == 1) |
|
84 fn = flst{1}; |
6955
|
85 [st, err, msg] = stat (fn); |
5689
|
86 if (err < 0) |
6955
|
87 warning ("dir: `stat (%s)' failed: %s", fn, msg); |
5689
|
88 nf = 0; |
6955
|
89 elseif (S_ISDIR (st.mode)) |
5690
|
90 flst = readdir (flst{1}); |
|
91 nf = length (flst); |
|
92 for i = 1:nf |
|
93 flst{i} = fullfile (fn, flst{i}); |
|
94 endfor |
5689
|
95 endif |
|
96 endif |
|
97 |
5690
|
98 if (length (flst) > 0) |
5689
|
99 ## Collect results. |
|
100 for i = nf:-1:1 |
|
101 fn = flst{i}; |
|
102 [st, err, msg] = lstat (fn); |
|
103 if (err < 0) |
5690
|
104 warning ("dir: `lstat (%s)' failed: %s", fn, msg); |
5689
|
105 else |
6955
|
106 ## If we are looking at a link that points to something, |
|
107 ## return info about the target of the link, otherwise, return |
|
108 ## info about the link itself. |
|
109 if (S_ISLNK (st.mode)) |
|
110 [xst, err, msg] = stat (fn); |
|
111 if (! err) |
|
112 st = xst; |
|
113 endif |
|
114 endif |
5689
|
115 [dummy, fn, ext] = fileparts (fn); |
|
116 fn = strcat (fn, ext); |
5750
|
117 info(i,1).name = fn; |
6955
|
118 lt = localtime (st.mtime); |
6947
|
119 info(i,1).date = strftime ("%d-%b-%Y %T", lt); |
5750
|
120 info(i,1).bytes = st.size; |
6955
|
121 info(i,1).isdir = S_ISDIR (st.mode); |
6947
|
122 info(i,1).datenum = datenum (lt.year + 1900, lt.mon, lt.mday, |
|
123 lt.hour, lt.min, lt.sec); |
5750
|
124 info(i,1).statinfo = st; |
5689
|
125 endif |
|
126 endfor |
5690
|
127 endif |
5689
|
128 |
|
129 else |
|
130 error ("dir: expecting directory or filename to be a char array"); |
|
131 endif |
|
132 |
5690
|
133 ## Return the output arguments. |
5689
|
134 if (nargout > 0) |
5690
|
135 ## Return the requested structure. |
5689
|
136 retval = info; |
5690
|
137 elseif (length (info) > 0) |
|
138 ## Print the structure to the screen. |
|
139 printf ("%s", list_in_columns ({info.name})); |
5689
|
140 else |
5690
|
141 warning ("dir: nonexistent file `%s'", file); |
5689
|
142 endif |
|
143 |
4694
|
144 endfunction |