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