1 ## Copyright (C) 2008 Jaroslav Hajek <highegg@gmail.com>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn{Function File} ex = fexist (file, tspec, aspec)
18 ## Checks whether a file exists.
19 ## @var{file} is the queried file path.
20 ## @var{tspec} is a combination of letters f,d,p,S, corresponding
23 ## @item f: regular file
25 ## @item p: named pipe (FIFO special file)
29 ## The query is true if the actual file type matches any of
30 ## the specified options.
32 ## @var{aspec} is a combination of letters r,w,x, corresponding
33 ## to queried access privileges to the file. The query is true
34 ## if the current user has all the spefied types of access, either
35 ## through "user", "group" or "other" specs.
37 ## @seealso{stat, lstat}
40 function ex = fexist (file, tspec, aspec)
46 if (nargin >= 2 && ! isempty (tspec))
51 ft |= S_ISREG (s.mode);
53 ft |= S_ISDIR (s.mode);
55 ft |= S_ISFIFO (s.mode);
57 ft |= S_ISSOCK (s.mode);
59 error ("invalid file type spec: %s", c)
64 if (ex && nargin >= 3 && ! isempty (aspec))
66 mypid = (s.uid == getuid ());
67 mygid = (s.gid == getgid ());
68 mstr = s.modestr(2:end);
72 at &= (mypid && mstr(1) == c) || (mygid && mstr(4) == c) || mstr(7) == c;
74 at &= (mypid && mstr(2) == c) || (mygid && mstr(5) == c) || mstr(8) == c;
76 at &= (mypid && mstr(3) == c) || (mygid && mstr(6) == c) || mstr(9) == c;
78 error ("invalid access type spec: %s", c)