comparison scripts/miscellaneous/fileattrib.m @ 28105:ff3b8b21a890

fileattrib.m: Overhaul function. * fileattrib.m: Rewrite docstring. Throw an error if nargout ==0 and operation fails. Otherwise, correctly assign outputs based on internal variable "sts".
author Rik <rik@octave.org>
date Tue, 18 Feb 2020 21:57:14 -0800
parents bd51beb6205e
children 28de41192f3c
comparison
equal deleted inserted replaced
28104:b2e0a2ddfd7d 28105:ff3b8b21a890
22 ## <https://www.gnu.org/licenses/>. 22 ## <https://www.gnu.org/licenses/>.
23 ## 23 ##
24 ######################################################################## 24 ########################################################################
25 25
26 ## -*- texinfo -*- 26 ## -*- texinfo -*-
27 ## @deftypefn {} {} fileattrib (@var{file}) 27 ## @deftypefn {} {} fileattrib ()
28 ## @deftypefnx {} {} fileattrib () 28 ## @deftypefnx {} {} fileattrib (@var{file})
29 ## @deftypefnx {} {[@var{status}, @var{attrib}] =} fileattrib (@dots{})
29 ## @deftypefnx {} {[@var{status}, @var{msg}, @var{msgid}] =} fileattrib (@dots{}) 30 ## @deftypefnx {} {[@var{status}, @var{msg}, @var{msgid}] =} fileattrib (@dots{})
30 ## Return information about @var{file}. 31 ## Report attribute information about @var{file}.
31 ## 32 ##
32 ## If successful, @var{status} is 1 and @var{msg} is a structure with the 33 ## If no file or directory is specified, report information about the present
33 ## following fields: 34 ## working directory.
35 ##
36 ## If successful, the output is a structure with the following fields:
34 ## 37 ##
35 ## @table @code 38 ## @table @code
36 ## @item Name 39 ## @item Name
37 ## Full name of @var{file}. 40 ## Full name of @var{file}.
38 ## 41 ##
62 ## @itemx GroupExecute 65 ## @itemx GroupExecute
63 ## @itemx OtherExecute 66 ## @itemx OtherExecute
64 ## True if the user (group; other users) has execute permission for @var{file}. 67 ## True if the user (group; other users) has execute permission for @var{file}.
65 ## @end table 68 ## @end table
66 ## 69 ##
67 ## If an attribute does not apply (i.e., archive on a Unix system) then the 70 ## If an attribute does not apply (e.g., archive on a Unix system) then the
68 ## field is set to NaN. 71 ## field is set to NaN.
69 ## 72 ##
70 ## If @code{attrib} fails, @var{msg} is a non-empty string containing an 73 ## If @var{file} contains globbing characters, information about all matching
71 ## error message and @var{msg_id} is the non-empty string @qcode{"fileattrib"}. 74 ## files is returned in a structure array.
72 ## 75 ##
73 ## With no input arguments, return information about the current directory. 76 ## If outputs are requested, the first is @var{status} which takes the value 1
74 ## 77 ## when the operation was successful, and 0 otherwise. The second output
75 ## If @var{file} contains globbing characters, return information about all 78 ## contains the structure described above (@var{attrib}) if the operation was
76 ## the matching files. 79 ## successful; otherwise, the second output is a system-dependent error message
77 ## @seealso{glob} 80 ## (@var{msg}). The third output is an empty string ("") when the operation
81 ## was successful, or a unique message identifier (@var{msgid}) in the case of
82 ## failure.
83 ## @seealso{stat, glob}
78 ## @end deftypefn 84 ## @end deftypefn
79 85
80 function [status, msg, msgid] = fileattrib (file = ".") 86 function [status, msg, msgid] = fileattrib (file = ".")
81 87
82 if (nargin > 1) 88 if (nargin > 1)
85 91
86 if (! ischar (file)) 92 if (! ischar (file))
87 error ("fileattrib: FILE must be a string"); 93 error ("fileattrib: FILE must be a string");
88 endif 94 endif
89 95
90 status = true; 96 sts = 1;
91 msg = ""; 97 msg = "";
92 msgid = ""; 98 msgid = "";
93 99
94 files = glob (file); 100 files = glob (file);
95 if (isempty (files)) 101 if (isempty (files))
106 r(i).archive = NaN; 112 r(i).archive = NaN;
107 r(i).system = NaN; 113 r(i).system = NaN;
108 r(i).hidden = NaN; 114 r(i).hidden = NaN;
109 else 115 else
110 [~, attrib] = dos (sprintf ('attrib "%s"', r(i).Name)); 116 [~, attrib] = dos (sprintf ('attrib "%s"', r(i).Name));
111 ## dos never returns error status so have to check it indirectly 117 ## DOS never returns error status so have to check it indirectly
112 if (! isempty (strfind (attrib, " -"))) 118 if (! isempty (strfind (attrib, " -")))
113 status = false; 119 sts = 0;
114 msgid = "fileattrib";
115 break; 120 break;
116 endif 121 endif
117 attrib = regexprep (attrib, '\S+:.*', ""); 122 attrib = regexprep (attrib, '\S+:.*', "");
118 r(i).archive = any (attrib == "A"); 123 r(i).archive = any (attrib == "A");
119 r(i).system = any (attrib == "S"); 124 r(i).system = any (attrib == "S");
140 r(i).OtherRead = NaN; 145 r(i).OtherRead = NaN;
141 r(i).OtherWrite = NaN; 146 r(i).OtherWrite = NaN;
142 r(i).OtherExecute = NaN; 147 r(i).OtherExecute = NaN;
143 endif 148 endif
144 else 149 else
145 status = false; 150 sts = 0;
146 msgid = "fileattrib";
147 break; 151 break;
148 endif 152 endif
149 endfor 153 endfor
150 154
151 if (status) 155 if (nargout == 0)
152 if (nargout == 0) 156 if (! sts)
153 status = r; 157 error ("fileattrib: operation failed");
158 endif
159 status = r;
160 else
161 status = sts;
162 if (! sts)
163 if (isempty (msg))
164 msg = "operation failed";
165 endif
166 msgid = "fileattrib";
154 else 167 else
155 msg = r; 168 msg = r;
156 endif 169 endif
157 endif 170 endif
158 171