missing/flock.c


DEFINITIONS

This source file includes following functions.
  1. flock
  2. flock
  3. flock


   1  #include "config.h"
   2  
   3  #if defined HAVE_FCNTL && defined HAVE_FCNTL_H
   4  
   5  /* These are the flock() constants.  Since this sytems doesn't have
   6     flock(), the values of the constants are probably not available.
   7  */
   8  # ifndef LOCK_SH
   9  #  define LOCK_SH 1
  10  # endif
  11  # ifndef LOCK_EX
  12  #  define LOCK_EX 2
  13  # endif
  14  # ifndef LOCK_NB
  15  #  define LOCK_NB 4
  16  # endif
  17  # ifndef LOCK_UN
  18  #  define LOCK_UN 8
  19  # endif
  20  
  21  #include <fcntl.h>
  22  #include <unistd.h>
  23  #include <errno.h>
  24  
  25  int
  26  flock(fd, operation)
  27      int fd;
  28      int operation;
  29  {
  30      struct flock lock;
  31   
  32      switch (operation & ~LOCK_NB) {
  33      case LOCK_SH:
  34          lock.l_type = F_RDLCK;
  35          break;
  36      case LOCK_EX:
  37          lock.l_type = F_WRLCK;
  38          break;
  39      case LOCK_UN:
  40          lock.l_type = F_UNLCK;
  41          break;
  42      default:
  43          errno = EINVAL;
  44          return -1;
  45      }
  46      lock.l_whence = SEEK_SET;
  47      lock.l_start = lock.l_len = 0L;
  48   
  49      return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &lock);
  50  }
  51  
  52  #elif defined(HAVE_LOCKF)
  53  
  54  #include <unistd.h>
  55  #include <errno.h>
  56  
  57  /*  Emulate flock() with lockf() or fcntl().  This is just to increase
  58      portability of scripts.  The calls might not be completely
  59      interchangeable.  What's really needed is a good file
  60      locking module.
  61  */
  62  
  63  # ifndef F_ULOCK
  64  #  define F_ULOCK       0       /* Unlock a previously locked region */
  65  # endif
  66  # ifndef F_LOCK
  67  #  define F_LOCK        1       /* Lock a region for exclusive use */
  68  # endif
  69  # ifndef F_TLOCK
  70  #  define F_TLOCK       2       /* Test and lock a region for exclusive use */
  71  # endif
  72  # ifndef F_TEST
  73  #  define F_TEST        3       /* Test a region for other processes locks */
  74  # endif
  75  
  76  /* These are the flock() constants.  Since this sytems doesn't have
  77     flock(), the values of the constants are probably not available.
  78  */
  79  # ifndef LOCK_SH
  80  #  define LOCK_SH 1
  81  # endif
  82  # ifndef LOCK_EX
  83  #  define LOCK_EX 2
  84  # endif
  85  # ifndef LOCK_NB
  86  #  define LOCK_NB 4
  87  # endif
  88  # ifndef LOCK_UN
  89  #  define LOCK_UN 8
  90  # endif
  91  
  92  int
  93  flock(fd, operation)
  94      int fd;
  95      int operation;
  96  {
  97      switch (operation) {
  98  
  99          /* LOCK_SH - get a shared lock */
 100        case LOCK_SH:
 101          rb_notimplement();
 102          return -1;
 103          /* LOCK_EX - get an exclusive lock */
 104        case LOCK_EX:
 105          return lockf (fd, F_LOCK, 0);
 106  
 107          /* LOCK_SH|LOCK_NB - get a non-blocking shared lock */
 108        case LOCK_SH|LOCK_NB:
 109          rb_notimplement();
 110          return -1;
 111          /* LOCK_EX|LOCK_NB - get a non-blocking exclusive lock */
 112        case LOCK_EX|LOCK_NB:
 113          return lockf (fd, F_TLOCK, 0);
 114  
 115          /* LOCK_UN - unlock */
 116        case LOCK_UN:
 117          return lockf (fd, F_ULOCK, 0);
 118  
 119          /* Default - can't decipher operation */
 120        default:
 121          errno = EINVAL;
 122          return -1;
 123      }
 124  }
 125  #elif !defined NT
 126  int
 127  flock(fd, operation)
 128      int fd;
 129      int operation;
 130  {
 131      rb_notimplement();
 132      return -1;
 133  }
 134  #endif