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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} dir (@var{directory}) |
|
22 ## @deftypefnx {Function File} {[@var{list}] =} dir (@var{directory}) |
|
23 ## Display file listing for directory @var{directory}. If a return |
|
24 ## value is requested, return a structure array with the fields |
|
25 ## |
|
26 ## @example |
|
27 ## @group |
|
28 ## name |
|
29 ## bytes |
|
30 ## date |
|
31 ## isdir |
|
32 ## statinfo |
|
33 ## @end group |
|
34 ## @end example |
|
35 ## |
|
36 ## @noindent |
|
37 ## in which @code{statinfo} is the structure returned from @code{stat}. |
|
38 ## |
|
39 ## If @var{directory} is not a directory, return information about the |
|
40 ## named file. |
|
41 ## @var{filename}. |
|
42 ## @end deftypefn |
|
43 ## @seealso{stat} |
|
44 |
|
45 ## Author: jwe |
|
46 |
|
47 ## XXX FIXME XXX -- this is quite slow for large directories, so perhaps |
|
48 ## it should be converted to C++. |
|
49 |
|
50 function retval = dir (file) |
|
51 |
|
52 if (nargin == 1) |
|
53 if (isstr (file)) |
|
54 flst = glob (file); |
|
55 nf = length (flst); |
|
56 if (nf == 0) |
|
57 if (nargout > 0) |
|
58 ## XXX FIXME XXX -- need a way to create an empty (0x1) |
|
59 ## structure array. |
|
60 retval = []; |
|
61 else |
|
62 warning ("dir: nonexistent file \"%s\"", file); |
|
63 endif |
|
64 else |
|
65 nt = 0; |
|
66 len = zeros (nf, 1); |
|
67 finfo = cell (nf, 1); |
|
68 ## Collect results. |
|
69 for i = nf:-1:1 |
|
70 fn = flst{i}; |
|
71 if (isstr (fn)) |
|
72 [st, err, msg] = lstat (fn); |
|
73 if (err < 0) |
|
74 warning ("dir: nonexistent file \"%s\"", fn); |
|
75 else |
|
76 if (st.modestr(1) == "d") |
|
77 lst = readdir (fn); |
|
78 n = length (lst); |
|
79 for j = n:-1:1 |
|
80 tfn = lst{j}; |
|
81 ## The lstat call seems to be the bottleneck for large |
|
82 ## directories. |
|
83 [st, err, msg] = lstat (strcat (fn, "/", tfn)); |
|
84 if (err < 0) |
|
85 warning ("dir: stat failed for %s (%s)", tfn, msg); |
|
86 else |
|
87 info(j).name = tfn; |
|
88 ## Generating the pretty time string also takes a |
|
89 ## significant amount of time for large directories. |
|
90 info(j).date = strftime ("%d-%b-%Y %T", |
|
91 localtime (st.mtime)); |
|
92 info(j).bytes = st.size; |
|
93 info(j).isdir = st.modestr(1) == "d"; |
|
94 info(j).statinfo = st; |
|
95 endif |
|
96 endfor |
|
97 else |
|
98 info.name = fn; |
|
99 info.date = strftime ("%d-%b-%Y %T", localtime (st.mtime)); |
|
100 info.bytes = st.size; |
|
101 info.isdir = st.modestr(1) == "d"; |
|
102 info.statinfo = st; |
|
103 endif |
|
104 nt += (len(i) = length (info)); |
|
105 finfo{i} = info; |
|
106 endif |
|
107 else |
|
108 error ("dir: expecting filename argument to be a string"); |
|
109 endif |
|
110 endfor |
|
111 ## Condense results to a single struct array. |
|
112 ## XXX FIXME XXX -- need a way to create an empty (0x) struct |
|
113 ## array in case the file is nonexistent. |
|
114 if (nt == 0) |
|
115 file_list = []; |
|
116 else |
|
117 off = 1; |
|
118 for i = 1:nf |
|
119 tlen = len(i); |
|
120 file_list(off:off+tlen-1) = finfo{i}; |
|
121 off += tlen; |
|
122 endfor |
|
123 endif |
|
124 if (nargout > 0) |
|
125 if (length (file_list) > 0) |
|
126 retval = file_list; |
|
127 else |
|
128 retval = []; |
|
129 endif |
|
130 else |
|
131 ## XXX FIXME XXX -- need a way to neatly list these in columns. |
|
132 for i = 1:nt |
|
133 printf (" %s\n", file_list(i).name); |
|
134 endfor |
|
135 endif |
|
136 endif |
|
137 else |
|
138 error ("dir: expecting directory or filename to be a string"); |
|
139 endif |
|
140 else |
|
141 usage ("dir (file)"); |
|
142 endif |
|
143 |
|
144 endfunction |