Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / clib / access.c
blobca71739c0f541f43cbeb991d58b62b761b54f8a1
1 /*
2 Copyright © 1995-2008, 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 /* Check if the volume exists. Calling Lock on non-existing volume will bring up System Requester */
73 if (SplitName(__path_u2a(path), ':', vol, 0, sizeof(vol)-1) != -1)
75 if(strcmp(vol, "PROGDIR") != 0)
77 dl = LockDosList(LDF_ALL | LDF_READ);
78 dl = FindDosEntry(dl, vol, LDF_ALL);
79 UnLockDosList(LDF_ALL | LDF_READ);
80 /* Volume / Assign / Device not found */
81 if (dl == NULL)
83 errno = ENOENT;
84 return -1;
89 /* Create a lock and examine a lock */
91 lock = Lock(__path_u2a(path), SHARED_LOCK);
92 if (lock == NULL)
94 errno = IoErr2errno(IoErr());
95 return -1;
98 fib = AllocDosObject(DOS_FIB, NULL);
99 if (!fib)
101 errno = IoErr2errno(IoErr());
102 UnLock(lock);
103 return -1;
106 if (Examine(lock, fib))
108 /* Notice : protection flags are 'low-active' (0 means access is granted) */
109 result = 0;
110 if ((mode & R_OK) && (result == 0) && (fib->fib_Protection & (1 << FIBB_READ)))
112 errno = EACCES;
113 result = -1;
115 if ((mode & W_OK) && (result == 0) && (fib->fib_Protection & (1 << FIBB_WRITE)))
117 errno = EACCES;
118 result = -1;
120 if ((mode & X_OK) && (result == 0) && (fib->fib_Protection & (1 << FIBB_EXECUTE)))
122 errno = EACCES;
123 result = -1;
126 else
128 errno = EBADF;
129 result = -1;
132 FreeDosObject(DOS_FIB, fib);
133 fib = NULL;
135 UnLock(lock);
136 return result;