home *** CD-ROM | disk | FTP | other *** search
/ The UNIX CD Bookshelf / OREILLY_TUCB_UNIX_CD.iso / upt / examples / SOURCES / TWIN / TWIN. / imode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-24  |  1.3 KB  |  53 lines

  1. /* imode.c */
  2. /********************************************************************
  3. *    Function      : give file name and permission; return true or false
  4. *    Author        : Istvan Mohos, 1987 --- in the Public Domain
  5. *********************************************************************/
  6.  
  7. #include "i.h"
  8.  
  9. int
  10. imode(fname, perm)
  11. char *fname, *perm;
  12. {
  13.     struct stat sbuf;
  14.     int realmode;
  15.     int pval = 0;
  16.     int mult = 1;
  17.     register char *pp;
  18.  
  19.     if (BADCHARP(fname))
  20.         return(ierror("imode: invalid file name"));
  21.     if (BADCHARP(perm))
  22.         return(ierror("imode: invalid permission string"));
  23.  
  24.     for (pp = perm; *pp++;);
  25.     for (--pp; --pp >= perm; mult <<= 3) {
  26.         switch(*pp) {
  27.         default:
  28.             return(ierror("imode: invalid permission string"));
  29.         case 48:
  30.             break;
  31.         case 49:
  32.         case 50:
  33.         case 51:
  34.         case 52:
  35.         case 53:
  36.         case 54:
  37.         case 55:
  38.             pval += (*pp - '0') * mult;
  39.             break;
  40.         }
  41.     }
  42.     if (!pval | pval > 0xffff)
  43.         return(ierror("imode: invalid permission value"));
  44.     if (stat(fname, &sbuf) == -1)
  45.         return(ierror("imode: stat"));
  46.  
  47.     realmode = (int)sbuf.st_mode;
  48.     if ((realmode & pval) == pval)
  49.         return(1);  /* TRUE; perm OK */
  50.  
  51.     return(0); /* FALSE; perm mismatch */
  52. }
  53.