home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / util / lk_lock.5.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-02  |  3.8 KB  |  171 lines

  1. #include "util.h"
  2. #include <fcntl.h>
  3.  
  4. /*
  5.  *    Standardized file-locking package (System 5 Rel 2)
  6.  *
  7.  * Apr 88    Added use of sentprotect in open() calls.
  8.  *        Removed use of O_CREAT in lk_open().
  9.  *            Edward C. Bennett <edward@engr.uky.edu>
  10.  *
  11.  * Dec 86    Rewritten to directly use fcntl() rather than lockf().
  12.  *        Added support for "a", "a+", "r+" and "w+".
  13.  *        Adapted by Edward C. Bennett <edward@engr.uky.edu>
  14.  *
  15.  * This version assumes the existence of the System 5 lockf() system call
  16.  * Adapted by Mark Vasoll <vasoll@a.cs.okstate.edu> from the 4.2 BSD version
  17.  */
  18. extern int errno;        /* simulate system error problems */
  19. extern int sentprotect;        /* default file protection */
  20.  
  21. #ifdef DEBUG
  22. #include "ll_log.h"
  23. extern LLog *logptr;
  24. #endif
  25.  
  26. LOCVAR    char *NIL = "NIL";
  27.  
  28. /* */
  29.  
  30. lk_open (file, access, lockdir, lockfile, maxtime)
  31. char    *file;            /* file to be locked */
  32. int    access;            /* read-write permissions */
  33. char    *lockdir;        /* --Ignored-- */
  34. char    *lockfile;        /* --Ignored-- */
  35. int    maxtime;        /* maybe break lock after it is this old */
  36. {
  37.     register    int    fd;
  38.     struct        flock    l;
  39.  
  40. #ifdef DEBUG
  41.     ll_log (logptr, LLOGBTR, "lk_open (%s,%d,%s,%s,%d)", file, access,
  42.         lockdir ? lockdir : NIL, lockfile ? lockfile : NIL, maxtime);
  43. #endif
  44.  
  45.     l.l_len = 0L;
  46.     l.l_start = 0L;
  47.     l.l_whence = 1;        /* lockf.c did this and it doesn't seem to */
  48.                 /* matter since start and len are both zero */
  49.  
  50.     if (access == O_RDONLY)
  51.         l.l_type = F_RDLCK;
  52.     else
  53.         l.l_type = F_WRLCK;
  54.  
  55.     if ((fd = open (file, access, sentprotect)) < 0
  56.         || fcntl (fd, F_SETLK, &l) < 0)
  57.     {
  58. #ifdef DEBUG
  59.         ll_err (logptr, LLOGFAT, "open of %s notok (err %d)",
  60.             file, errno);
  61. #endif
  62.         if (fd >= 0)
  63.             (void) close (fd);
  64.         return (NOTOK);
  65.     }
  66.     return (fd);
  67. }
  68.  
  69. lk_close (fd, file, lockdir, lockfile)
  70. int    fd;
  71. char    *file;            /* file to be locked */
  72. char    *lockdir;        /* directory to put parallel file into */
  73. char    *lockfile;        /* file to lock against */
  74. {
  75.     register    int    retval;
  76.  
  77. #ifdef DEBUG
  78.     ll_log (logptr, LLOGBTR, "lk_close (%d,%s,%s,%s)", fd,
  79.         file ? file : NIL, lockdir ? lockdir : NIL,
  80.         lockfile ? lockfile : NIL);
  81. #endif
  82.  
  83.     if (fd < 0)
  84.         return (OK);
  85.     retval = close (fd);
  86.     return (retval);
  87. }
  88. /* */
  89. FILE *
  90. lk_fopen (file, access, lockdir, lockfile, maxtime)
  91. char    *file;            /* file to be locked */
  92. char    *access;        /* read-write permissions */
  93. char    *lockdir;        /* --Ignored-- */
  94. char    *lockfile;        /* --Ignored-- */
  95. int    maxtime;        /* maybe break lock after it is this old */
  96. {
  97.     register    int    fd, oflag, plus;
  98.     register    FILE    *fp;
  99.     struct        flock    l;
  100.  
  101. #ifdef DEBUG
  102.     ll_log (logptr, LLOGBTR, "lk_fopen (%s,%s,%s,%s,%d)", file, access,
  103.         lockdir ? lockdir : NIL, lockfile ? lockfile : NIL, maxtime);
  104. #endif
  105.  
  106.     l.l_len = 0L;
  107.     l.l_start = 0L;
  108.     l.l_whence = 1;
  109.  
  110.     plus = (access[1] == '+');
  111.     oflag = O_CREAT;    /* fopen() creates a non-existent file */
  112.     switch (access[0]) {
  113.     case 'a':
  114.         oflag |= (plus ? O_RDWR : O_WRONLY) | O_APPEND;
  115.         l.l_type = F_WRLCK;
  116.         break;
  117.  
  118.     case 'r':
  119.         oflag |= (plus ? O_RDWR : O_RDONLY);
  120.         l.l_type = F_RDLCK;
  121.         break;
  122.  
  123.     case 'w':
  124.         oflag |= (plus ? O_RDWR : O_WRONLY) | O_TRUNC;
  125.         l.l_type = F_WRLCK;
  126.         break;
  127.  
  128.     default:
  129.         ll_err (logptr, LLOGFAT, "lk_fopen(): bad mode '%s'\n", access);
  130.         return(NULL);
  131.     }
  132.  
  133.     if ((fd = open(file, oflag, sentprotect)) < 0 || fcntl(fd, F_SETLK, &l) < 0) {
  134. #ifdef DEBUG
  135.         ll_err (logptr, LLOGFAT, "open of %s notok (err %d)",
  136.             file, errno);
  137. #endif
  138.         if (fd >= 0)
  139.             (void) close (fd);
  140.         return (NULL);
  141.     }
  142.  
  143.     if ((fp = fdopen (fd, access)) == NULL) {
  144.         (void) close (fd);
  145.         return ((FILE *) NULL);
  146.     }
  147.     return (fp);
  148. }
  149.  
  150. lk_fclose (fp, file, lockdir, lockfile)
  151. FILE    *fp;
  152. char    *file;            /* --Ignored-- */
  153. char    *lockdir;        /* --Ignored-- */
  154. char    *lockfile;        /* --Ignored-- */
  155. {
  156.     register    int    retval;
  157.  
  158. #ifdef DEBUG
  159.     ll_log (logptr, LLOGBTR, "lk_fclose (%s,%s,%s)", file ? file : NIL,
  160.         lockdir ? lockdir : NIL, lockfile ? lockfile : NIL);
  161. #endif
  162.  
  163.     switch (fp) {
  164.     case EOF:
  165.     case NULL:
  166.         return (OK);
  167.     }
  168.     retval = fclose (fp);
  169.     return (retval);
  170. }
  171.