define __KERNEL_STRICT_NAMES to avoid inclusion of kernel types on systems that carry...
[cake.git] / compiler / clib / access.c
blobb0d0fef3436e8df517f97d07c706124bdfa87f50
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX function access().
6 */
8 #include <errno.h>
9 #include <proto/dos.h>
10 #include <dos/filesystem.h>
11 #include <string.h>
13 #include <aros/debug.h>
15 #include "__errno.h"
16 #include "__upath.h"
18 /*****************************************************************************
20 NAME */
21 #include <unistd.h>
23 int access (
25 /* SYNOPSIS */
26 const char *path,
27 int mode)
29 /* FUNCTION
30 Check access permissions of a file or pathname
32 INPUTS
33 path - the path of the file being checked
34 mode - the bitwise inclusive OR of the access permissions
35 to be checked:
37 W_OK - for write permission
38 R_OK - for readpermissions
39 X_OK - for execute permission
40 F_OK - Just to see whether the file exists
42 RESULT
43 If path cannot be found or if any of the desired access
44 modes would not be granted, then a -1 value is returned;
45 otherwise a 0 value is returned.
47 NOTES
49 EXAMPLE
51 BUGS
53 SEE ALSO
54 open(), ftruncate()
56 INTERNALS
58 ******************************************************************************/
60 BPTR lock = NULL;
61 struct FileInfoBlock *fib = NULL;
62 int result = -1;
63 char vol[32];
64 struct DosList *dl = NULL;
66 if (!path) /* safety check */
68 errno = EFAULT;
69 return -1;
72 if (!strlen(path)) /* empty path */
74 errno = ENOENT;
75 return -1;
78 /* Check if the volume exists. Calling Lock on non-existing volume will bring up System Requester */
79 if (SplitName(__path_u2a(path), ':', vol, 0, sizeof(vol)-1) != -1)
81 if(strcmp(vol, "PROGDIR") != 0)
83 dl = LockDosList(LDF_ALL | LDF_READ);
84 dl = FindDosEntry(dl, vol, LDF_ALL);
85 UnLockDosList(LDF_ALL | LDF_READ);
86 /* Volume / Assign / Device not found */
87 if (dl == NULL)
89 errno = ENOENT;
90 return -1;
95 /* Create a lock and examine a lock */
97 lock = Lock(__path_u2a(path), SHARED_LOCK);
98 if (lock == NULL)
100 errno = IoErr2errno(IoErr());
101 return -1;
104 fib = AllocDosObject(DOS_FIB, NULL);
105 if (!fib)
107 errno = IoErr2errno(IoErr());
108 UnLock(lock);
109 return -1;
112 if (Examine(lock, fib))
114 /* Notice : protection flags are 'low-active' (0 means access is granted) */
115 result = 0;
116 if ((mode & R_OK) && (result == 0) && (fib->fib_Protection & (1 << FIBB_READ)))
118 errno = EACCES;
119 result = -1;
121 if ((mode & W_OK) && (result == 0) && (fib->fib_Protection & (1 << FIBB_WRITE)))
123 errno = EACCES;
124 result = -1;
126 if ((mode & X_OK) && (result == 0) && (fib->fib_Protection & (1 << FIBB_EXECUTE)))
128 errno = EACCES;
129 result = -1;
132 else
134 errno = EBADF;
135 result = -1;
138 FreeDosObject(DOS_FIB, fib);
139 fib = NULL;
141 UnLock(lock);
142 return result;