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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
4694
|
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 |
5689
|
40 ## named @var{filename}. @var{directory} may be a list of directories |
|
41 ## specified either by name or with wildcard characters (like * and ?) |
|
42 ## which will be expanded with glob. |
5597
|
43 ## @seealso{ls, stat, readdir, glob, filesep} |
4694
|
44 ## @end deftypefn |
|
45 |
|
46 ## Author: jwe |
|
47 |
|
48 ## XXX FIXME XXX -- this is quite slow for large directories, so perhaps |
|
49 ## it should be converted to C++. |
|
50 |
|
51 function retval = dir (file) |
|
52 |
5689
|
53 if (nargin == 0) |
5690
|
54 file = "."; |
5689
|
55 elseif (nargin > 1) |
4694
|
56 usage ("dir (file)"); |
|
57 endif |
|
58 |
5690
|
59 ## Prep the retval. |
|
60 info = struct (zeros (0, 1)); |
5689
|
61 |
|
62 if (ischar (file)) |
5690
|
63 if (strcmp (file, "*")) |
|
64 file = "."; |
5689
|
65 endif |
5690
|
66 if (strcmp (file, ".")) |
|
67 flst = {"."}; |
|
68 nf = 1; |
|
69 else |
|
70 flst = glob (file); |
|
71 nf = length (flst); |
|
72 endif |
5689
|
73 |
5690
|
74 ## Determine the file list for the case where a single directory is |
|
75 ## specified. |
|
76 if (nf == 1) |
|
77 fn = flst{1}; |
|
78 [st, err, msg] = lstat (fn); |
5689
|
79 if (err < 0) |
5690
|
80 warning ("dir: `lstat (%s)' failed: %s", fn, msg); |
5689
|
81 nf = 0; |
|
82 elseif (st.modestr(1) == "d") |
5690
|
83 flst = readdir (flst{1}); |
|
84 nf = length (flst); |
|
85 for i = 1:nf |
|
86 flst{i} = fullfile (fn, flst{i}); |
|
87 endfor |
5689
|
88 endif |
|
89 endif |
|
90 |
5690
|
91 if (length (flst) > 0) |
5689
|
92 ## Collect results. |
|
93 for i = nf:-1:1 |
|
94 fn = flst{i}; |
|
95 [st, err, msg] = lstat (fn); |
|
96 if (err < 0) |
5690
|
97 warning ("dir: `lstat (%s)' failed: %s", fn, msg); |
5689
|
98 else |
|
99 [dummy, fn, ext] = fileparts (fn); |
|
100 fn = strcat (fn, ext); |
|
101 info(i).name = fn; |
|
102 info(i).date = strftime ("%d-%b-%Y %T", localtime (st.mtime)); |
|
103 info(i).bytes = st.size; |
|
104 info(i).isdir = st.modestr(1) == "d"; |
|
105 info(i).statinfo = st; |
|
106 endif |
|
107 endfor |
5690
|
108 endif |
5689
|
109 |
|
110 else |
|
111 error ("dir: expecting directory or filename to be a char array"); |
|
112 endif |
|
113 |
5690
|
114 ## Return the output arguments. |
5689
|
115 if (nargout > 0) |
5690
|
116 ## Return the requested structure. |
5689
|
117 retval = info; |
5690
|
118 elseif (length (info) > 0) |
|
119 ## Print the structure to the screen. |
|
120 printf ("%s", list_in_columns ({info.name})); |
5689
|
121 else |
5690
|
122 warning ("dir: nonexistent file `%s'", file); |
5689
|
123 endif |
|
124 |
4694
|
125 endfunction |