home *** CD-ROM | disk | FTP | other *** search
- RCS_ID_C="$Id: access.c,v 1.3 1994/04/04 01:29:49 jraja Exp $"
- /*
- * access.c --- check access to a file or a directory
- *
- * Author: jraja <Jarno.Rajahalme@hut.fi>
- *
- * This file is part of the AmiTCP/IP Network Support Library.
- *
- * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
- * Helsinki University of Technology, Finland.
- * All rights reserved.
- *
- * Created : Mon Mar 28 09:15:14 1994 jraja
- * Last modified: Wed Mar 30 10:10:56 1994 jraja
- *
- * $Log: access.c,v $
- * Revision 1.3 1994/04/04 01:29:49 jraja
- * Included == 0 to the directory range (according to The Amiga Guru Book).
- *
- * Revision 1.2 1994/03/30 07:39:20 jraja
- * Added copyright text.
- *
- * Revision 1.1 1994/03/28 06:24:51 jraja
- * Initial revision
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <dos.h>
- #include <string.h>
- #include <errno.h>
- #include <dos/dos.h>
- #include <proto/dos.h>
- #include <proto/exec.h>
- #include <exec/memory.h>
-
- #include <bsdsocket.h>
-
- /*
- * I know, the goto's are ugly, but they make the code smaller and help
- * to prevent duplicating code.
- */
- int
- access(const char *name, int mode)
- {
- BPTR lock, parentLock;
- LONG prot;
- UBYTE bytes[sizeof(struct FileInfoBlock) + sizeof(struct InfoData) + 3];
- struct FileInfoBlock *fib;
- struct InfoData *info;
-
- /*
- * align the data areas
- */
- fib = (struct FileInfoBlock *) (((ULONG) bytes+3) & (0xFFFFFFFF-3));
- info = (struct InfoData *) (ULONG)(fib + 1);
-
- /*
- * Lock the file (or directory)
- */
- if ((lock = Lock(name, SHARED_LOCK)) == NULL)
- goto osfail;
-
- if (!Examine(lock, fib))
- goto osfail;
-
- prot = fib->fib_Protection;
-
- /*
- * Check each access mode
- */
- if (mode & R_OK && prot & FIBF_READ) {
- errno = EACCES;
- goto fail;
- }
- if (mode & W_OK) {
- /*
- * Check for write protected disks
- */
- if (!Info(lock, info))
- goto osfail;
-
- if (info->id_DiskState == ID_WRITE_PROTECTED) {
- errno = EROFS;
- goto fail;
- }
-
- /*
- * not write protected: Check if the lock is to the root of the
- * disk, if it is, force writing to be allowed.
- * Check if the lock is a directory before taking ParentDir()
- */
- if (fib->fib_DirEntryType >= 0) { /* lock is a directory */
- parentLock = ParentDir(lock);
- if (parentLock != NULL)
- UnLock(parentLock); /* not the root, prot is valid */
- else
- prot &= ~FIBF_WRITE; /* the root, force writing to be allowed */
- }
- if (prot & FIBF_WRITE) {
- errno = EACCES;
- goto fail;
- }
- }
- if (mode & X_OK && prot & FIBF_EXECUTE) {
- errno = EACCES;
- goto fail;
- }
-
- /* F_OK */
-
- UnLock(lock);
- return 0;
-
- osfail:
- #if __SASC
- errno = __io2errno(_OSERR = IoErr());
- #else
- _ug_set_errno(IoErr());
- #endif
-
- fail:
- if (lock != NULL)
- UnLock(lock);
-
- return -1;
- }
-