home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / comm / tcp / amitcp / src / netlib / access.c < prev    next >
C/C++ Source or Header  |  1994-04-04  |  3KB  |  129 lines

  1. RCS_ID_C="$Id: access.c,v 1.3 1994/04/04 01:29:49 jraja Exp $"
  2. /*
  3.  * access.c --- check access to a file or a directory
  4.  *
  5.  * Author: jraja <Jarno.Rajahalme@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP Network Support Library.
  8.  *
  9.  * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *                  All rights reserved.
  12.  *
  13.  * Created      : Mon Mar 28 09:15:14 1994 jraja
  14.  * Last modified: Wed Mar 30 10:10:56 1994 jraja
  15.  *
  16.  * $Log: access.c,v $
  17.  * Revision 1.3  1994/04/04  01:29:49  jraja
  18.  * Included == 0 to the directory range (according to The Amiga Guru Book).
  19.  *
  20.  * Revision 1.2  1994/03/30  07:39:20  jraja
  21.  * Added copyright text.
  22.  *
  23.  * Revision 1.1  1994/03/28  06:24:51  jraja
  24.  * Initial revision
  25.  *
  26.  */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <dos.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <dos/dos.h>
  34. #include <proto/dos.h>
  35. #include <proto/exec.h>
  36. #include <exec/memory.h>
  37.  
  38. #include <bsdsocket.h>
  39.  
  40. /*
  41.  * I know, the goto's are ugly, but they make the code smaller and help
  42.  * to prevent duplicating code.
  43.  */
  44. int
  45. access(const char *name, int mode)
  46. {
  47.   BPTR    lock, parentLock;
  48.   LONG    prot;
  49.   UBYTE   bytes[sizeof(struct FileInfoBlock) + sizeof(struct InfoData) + 3];
  50.   struct FileInfoBlock *fib;
  51.   struct InfoData      *info;
  52.  
  53.   /*
  54.    * align the data areas
  55.    */
  56.   fib = (struct FileInfoBlock *) (((ULONG) bytes+3) & (0xFFFFFFFF-3));
  57.   info = (struct InfoData *) (ULONG)(fib + 1);
  58.  
  59.   /*
  60.    * Lock the file (or directory)
  61.    */
  62.   if ((lock = Lock(name, SHARED_LOCK)) == NULL)
  63.     goto osfail;
  64.   
  65.   if (!Examine(lock, fib))
  66.     goto osfail;
  67.   
  68.   prot = fib->fib_Protection;
  69.  
  70.   /*
  71.    * Check each access mode
  72.    */
  73.   if (mode & R_OK && prot & FIBF_READ) {
  74.     errno = EACCES;
  75.     goto fail;
  76.   }
  77.   if (mode & W_OK) {
  78.     /*
  79.      * Check for write protected disks
  80.      */
  81.     if (!Info(lock, info))
  82.       goto osfail;
  83.  
  84.     if (info->id_DiskState == ID_WRITE_PROTECTED) {
  85.       errno = EROFS;
  86.       goto fail;
  87.     }
  88.  
  89.     /*
  90.      * not write protected: Check if the lock is to the root of the 
  91.      * disk, if it is, force writing to be allowed.
  92.      * Check if the lock is a directory before taking ParentDir()
  93.      */
  94.     if (fib->fib_DirEntryType >= 0) { /* lock is a directory */
  95.       parentLock = ParentDir(lock);
  96.       if (parentLock != NULL)
  97.     UnLock(parentLock); /* not the root, prot is valid */
  98.       else
  99.     prot &= ~FIBF_WRITE; /* the root, force writing to be allowed */
  100.     }
  101.     if (prot & FIBF_WRITE) {
  102.       errno = EACCES;
  103.       goto fail;
  104.     }
  105.   }
  106.   if (mode & X_OK && prot & FIBF_EXECUTE) {
  107.     errno = EACCES;
  108.     goto fail;
  109.   }
  110.   
  111.   /* F_OK */
  112.  
  113.   UnLock(lock);
  114.   return 0;
  115.   
  116.  osfail:
  117. #if __SASC
  118.   errno = __io2errno(_OSERR = IoErr());
  119. #else
  120.   _ug_set_errno(IoErr());
  121. #endif
  122.  
  123.  fail:
  124.   if (lock != NULL)
  125.     UnLock(lock);
  126.  
  127.   return -1;
  128. }
  129.