3 * access.c - check access to a file or a directory
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
16 #include <proto/dos.h>
17 #include <proto/exec.h>
18 #include <exec/memory.h>
20 #include <bsdsocket.h>
23 * I know, the goto's are ugly, but they make the code smaller and help
24 * to prevent duplicating code.
27 access(const char *name
, int mode
)
29 BPTR lock
, parentLock
;
31 UBYTE bytes
[sizeof(struct FileInfoBlock
) + sizeof(struct InfoData
) + 3];
32 struct FileInfoBlock
*fib
;
33 struct InfoData
*info
;
36 * align the data areas
38 fib
= (struct FileInfoBlock
*) (((ULONG
) bytes
+3) & (0xFFFFFFFF-3));
39 info
= (struct InfoData
*) (ULONG
)(fib
+ 1);
42 * Lock the file (or directory)
44 if ((lock
= Lock((STRPTR
)name
, SHARED_LOCK
)) == NULL
)
47 if (!Examine(lock
, fib
))
50 prot
= fib
->fib_Protection
;
53 * Check each access mode
55 if (mode
& R_OK
&& prot
& FIBF_READ
) {
61 * Check for write protected disks
63 if (!Info(lock
, info
))
66 if (info
->id_DiskState
== ID_WRITE_PROTECTED
) {
72 * not write protected: Check if the lock is to the root of the
73 * disk, if it is, force writing to be allowed.
74 * Check if the lock is a directory before taking ParentDir()
76 if (fib
->fib_DirEntryType
>= 0) { /* lock is a directory */
77 parentLock
= ParentDir(lock
);
78 if (parentLock
!= NULL
)
79 UnLock(parentLock
); /* not the root, prot is valid */
81 prot
&= ~FIBF_WRITE
; /* the root, force writing to be allowed */
83 if (prot
& FIBF_WRITE
) {
88 if (mode
& X_OK
&& prot
& FIBF_EXECUTE
) {
100 errno
= __io2errno(_OSERR
= IoErr());
102 _ug_set_errno(IoErr());