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.4.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-28  |  3.1 KB  |  144 lines

  1. #include "util.h"
  2. #include <sys/file.h>
  3. #include <fcntl.h>
  4.  
  5. /*
  6.  *    Standardized file-locking package  (4.2BSD)
  7.  *
  8.  *    Feb 88    Added support for "a+", "r+" and "w+". Also, improved logging
  9.  *        and added attention to "sentprotect".
  10.  *            Edward C. Bennett <edward@engr.uky.edu>
  11.  *
  12.  *  This version assumes the existence of the 4.2BSD flock() system call.
  13.  */
  14. extern int errno;               /* simulate system error problems */
  15. extern int sentprotect;        /* default file protection */
  16.  
  17. #ifdef DEBUG
  18. #include "ll_log.h"
  19. extern LLog *logptr;
  20. #endif
  21.  
  22. LOCVAR    char *NIL = "NIL";
  23.  
  24. /* */
  25. lk_open(file, access, lockdir, lockfile, maxtime)
  26. char    *file;            /* file to be locked */
  27. int    access;            /* read-write permissions */
  28. char    *lockdir;        /* --Ignored-- */
  29. char    *lockfile;        /* --Ignored-- */
  30. int    maxtime;        /* maybe break lock after it is this old */
  31. {
  32.     register    int    fd;
  33.  
  34. #ifdef DEBUG
  35.     ll_log(logptr, LLOGBTR, "lk_open(%s,%d,%s,%s,%d)", file, access,
  36.         lockdir ? lockdir : NIL, lockfile ? lockfile : NIL, maxtime);
  37. #endif
  38.  
  39.     if ((fd = open(file, access | O_NDELAY)) < 0
  40.         || flock(fd, LOCK_EX|LOCK_NB) < 0) {
  41. #ifdef DEBUG
  42.         ll_err(logptr, LLOGBTR, "open notok (err %d)", errno);
  43. #endif
  44.         if (fd >= 0)
  45.             (void) close(fd);
  46.  
  47.         return(NOTOK);
  48.     }
  49.     return(fd);
  50. }
  51.  
  52. lk_close(fd, file, lockdir, lockfile)
  53. int    fd;
  54. char    *file;            /* -- Ignored -- */
  55. char    *lockdir;        /* -- Ignored -- */
  56. char    *lockfile;        /* -- Ignored -- */
  57. {
  58.     register    int    retval;
  59.  
  60. #ifdef DEBUG
  61.     ll_log(logptr, LLOGBTR, "lk_close(%d,%s,%s,%s)", fd, file,
  62.         lockdir ? lockdir : NIL, lockfile ? lockfile : NIL);
  63. #endif
  64.     if (fd < 0)
  65.         return(OK);
  66.  
  67.     retval = close(fd);
  68.     return(retval);
  69. }
  70. /* */
  71. FILE *
  72. lk_fopen(file, access, lockdir, lockfile, maxtime)
  73. char    *file;            /* file to be locked */
  74. char    *access;        /* read-write permissions */
  75. char    *lockdir;        /* --Ignored-- */
  76. char    *lockfile;        /* --Ignored-- */
  77. int    maxtime;        /* maybe break lock after it is this old */
  78. {
  79.     register    int    fd, oflag, plus;
  80.     register    FILE    *fp;
  81.  
  82. #ifdef DEBUG
  83.     ll_log(logptr, LLOGBTR, "lk_fopen(%s,%s,%s,%s,%d)", file, access,
  84.         lockdir ? lockdir : NIL, lockfile ? lockfile : NIL, maxtime);
  85. #endif
  86.  
  87.     oflag = O_CREAT | O_NDELAY;
  88.     plus = (access[1] == '+');
  89.     switch (access[0]) {
  90.     case 'a':
  91.         oflag |= (plus ? O_RDWR : O_WRONLY) | O_APPEND;
  92.         break;
  93.  
  94.     case 'r':
  95.         oflag |= (plus ? O_RDWR : O_RDONLY);
  96.         break;
  97.  
  98.     case 'w':
  99.         oflag |= (plus ? O_RDWR : O_WRONLY) | O_TRUNC;
  100.         break;
  101.  
  102.     default:
  103.         ll_err(logptr, LLOGFAT, "lk_fopen(): bad mode '%s'\n", access);
  104.         return(NULL);
  105.     }
  106.  
  107.     if ((fd = open(file, oflag, sentprotect)) < 0 ||
  108.         flock(fd, LOCK_EX|LOCK_NB) < 0) {
  109. #ifdef DEBUG
  110.         ll_err(logptr, LLOGFAT, "open of %s notok (err %d)",
  111.             file, errno);
  112. #endif
  113.         if (fd >= 0)
  114.             (void) close(fd);
  115.  
  116.         return(NULL);
  117.     }
  118.  
  119.     if ((fp = fdopen(fd, access)) == NULL) {
  120.         (void) close(fd);
  121.         return((FILE *)NULL);
  122.     }
  123.     return(fp);
  124. }
  125.  
  126. lk_fclose(fp, file, lockdir, lockfile)
  127. FILE    *fp;
  128. char    *file;            /* --Ignored-- */
  129. char    *lockdir;        /* --Ignored-- */
  130. char    *lockfile;        /* --Ignored-- */
  131. {
  132. #ifdef DEBUG
  133.     ll_log(logptr, LLOGBTR, "lk_fclose(0%o,%s,%s,%s)", fp, file,
  134.         lockdir ? lockdir : NIL, lockfile ? lockfile : NIL);
  135. #endif
  136.     switch ((int)fp) {
  137.     case EOF:
  138.     case NULL:
  139.         return(OK);
  140.     }
  141.  
  142.     return(fclose(fp));
  143. }
  144.