5559
|
1 ## Copyright (C) 2005 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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {[@var{status}, @var{msg}, @var{msgid}] =} fileattrib (@var{file}) |
|
22 ## Return information about @var{file}. |
|
23 ## |
|
24 ## If successful, @var{status} is 1, with @var{result} containing a |
|
25 ## structure with the following fields: |
|
26 ## |
|
27 ## @table @code |
|
28 ## @item Name |
|
29 ## Full name of @var{file}. |
|
30 ## @item archive |
|
31 ## True if @var{file} is an archive (Windows). |
|
32 ## @item system |
|
33 ## True if @var{file} is a system file (Windows). |
|
34 ## @item hidden |
|
35 ## True if @var{file} is a hidden file (Windows). |
|
36 ## @item directory |
|
37 ## True if @var{file} is a directory. |
|
38 ## @item UserRead |
|
39 ## @itemx GroupRead |
|
40 ## @itemx OtherRead |
|
41 ## True if the user (group; other users) has read permission for |
|
42 ## @var{file}. |
|
43 ## @item UserWrite |
|
44 ## @itemx GroupWrite |
|
45 ## @itemx OtherWrite |
|
46 ## True if the user (group; other users) has write permission for |
|
47 ## @var{file}. |
|
48 ## @item UserExecute |
|
49 ## @itemx GroupExecute |
|
50 ## @itemx OtherExecute |
|
51 ## True if the user (group; other users) has execute permission for |
|
52 ## @var{file}. |
|
53 ## @end table |
|
54 ## If an attribute does not apply (i.e., archive on a Unix system) then |
|
55 ## the field is set to NaN. |
|
56 ## |
|
57 ## With no input arguments, return information about the current |
|
58 ## directory. |
|
59 ## |
|
60 ## If @var{file} contains globbing characters, return information about |
|
61 ## all the matching files. |
|
62 ## @seealso{glob} |
|
63 ## @end deftypefn |
|
64 |
|
65 function [status, msg, msgid] = fileattrib (file) |
|
66 |
|
67 status = true; |
|
68 msg = ""; |
|
69 msgid = ""; |
|
70 |
|
71 if (nargin == 0) |
|
72 file = "."; |
|
73 endif |
|
74 |
|
75 if (ischar (file)) |
|
76 files = glob (file); |
|
77 if (isempty (files)) |
|
78 files = {file}; |
|
79 nfiles = 1; |
|
80 else |
|
81 nfiles = length (files); |
|
82 endif |
|
83 else |
|
84 error ("fileattrib: expecting first argument to be a character string"); |
|
85 endif |
|
86 |
|
87 if (nargin == 0 || nargin == 1) |
|
88 |
|
89 r_n = r_a = r_s = r_h = r_d ... |
|
90 = r_u_r = r_u_w = r_u_x ... |
|
91 = r_g_r = r_g_w = r_g_x ... |
|
92 = r_o_r = r_o_w = r_o_x = cell (nfiles, 1); |
|
93 |
|
94 curr_dir = pwd (); |
|
95 |
|
96 for i = 1:nfiles |
|
97 [info, err, msg] = stat (files{i}); |
|
98 if (! err) |
|
99 r_n{i} = canonicalize_file_name (files{i}); |
|
100 r_a{i} = NaN; |
|
101 r_s{i} = NaN; |
|
102 r_h{i} = NaN; |
|
103 r_d{i} = S_ISDIR (info.mode); |
5775
|
104 ## FIXME -- maybe we should have S_IRUSR etc. masks? |
5559
|
105 modestr = info.modestr; |
|
106 r_u_r{i} = modestr(2) == "r"; |
|
107 r_u_w{i} = modestr(3) == "w"; |
|
108 r_u_x{i} = modestr(4) == "x"; |
|
109 r_g_r{i} = modestr(5) == "r"; |
|
110 r_g_w{i} = modestr(6) == "w"; |
|
111 r_g_x{i} = modestr(7) == "x"; |
|
112 r_o_r{i} = modestr(8) == "r"; |
|
113 r_o_w{i} = modestr(9) == "w"; |
|
114 r_o_x{i} = modestr(10) == "x"; |
|
115 else |
|
116 status = false; |
|
117 msgid = "fileattrib"; |
|
118 break; |
|
119 endif |
|
120 endfor |
|
121 if (status) |
|
122 r = struct ("Name", r_n, "archive", r_a, "system", r_s, |
|
123 "hidden", r_s, "directory", r_d, "UserRead", r_u_r, |
|
124 "UserWrite", r_u_w, "UserExecute", r_u_x, |
|
125 "GroupRead", r_g_r, "GroupWrite", r_g_w, |
|
126 "GroupExecute", r_g_x, "OtherRead", r_o_r, |
|
127 "OtherWrite", r_o_w, "OtherExecute", r_o_x); |
|
128 if (nargout == 0) |
|
129 status = r; |
|
130 else |
|
131 msg = r; |
|
132 endif |
|
133 endif |
|
134 else |
6046
|
135 print_usage (); |
5559
|
136 endif |
|
137 |
|
138 endfunction |