home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / LSEQ.ZIP / LSLOCK.C < prev    next >
Text File  |  1989-08-30  |  4KB  |  187 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "lslock.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. #include "lseq_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      lslock - lseq lock
  13.  
  14. SYNOPSIS
  15.      #include <lseq.h>
  16.  
  17.      int lslock(lsp, l_type)
  18.      lseq_t *lsp;
  19.      int l_type;
  20.  
  21. DESCRIPTION
  22.      The lslock function controls the lock status of an lseq.  The lsp
  23.      argument is an open lseq.  l_type indicates the target status of
  24.      the lock on the lseq.
  25.  
  26.      The lock types available are:
  27.  
  28.           LS_RDLCK - lock lseq for reading
  29.           LS_WRLCK - lock lseq for reading and writing
  30.           LS_RDLKW - lock lseq for reading (wait)
  31.           LS_WRLKW - lock lseq for reading and writing (wait)
  32.           LS_UNLCK - unlock lseq
  33.  
  34.      For the lock types which wait, lslock will not return until the
  35.      lock is available.  For the lock types which do not wait, if the
  36.      lock is unavailable because of a lock held by another process  a
  37.      value of -1 is returned and errno set to EAGAIN.
  38.  
  39.      lslock will fail if one or more of the following is true:
  40.  
  41.      [EAGAIN]       l_type is LS_RDLCK and lsp is already
  42.                     write locked by another process, or l_type
  43.                     is LS_WRLCK and lsp is already read
  44.                     or write locked by another process.
  45.      [EINVAL]       lsp is is not a valid lseq pointer.
  46.      [EINVAL]       l_type is not one of the valid lock types.
  47.      [LSECORRUPT]   lsp is corrupt.
  48.      [LSENOPEN]     lsp is not open.
  49.      [LSENOPEN]     l_type is LS_RDLCK or LS_RDLKW and lsp
  50.                     is not opened for reading or l_type is LS_WRLCK
  51.                     or LS_WRLKW and lsp is not open for writing.
  52.  
  53. SEE ALSO
  54.      lsgetlck.
  55.  
  56. DIAGNOSTICS
  57.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  58.      value of -1 is returned, and errno set to indicate the error.
  59.  
  60. ------------------------------------------------------------------------------*/
  61. int lslock(lsp, l_type)
  62. lseq_t * lsp;
  63. int       l_type;
  64. {
  65.     int    rs    = 0;
  66.     int    bl_type    = 0;
  67.  
  68.     errno = 0;
  69.  
  70.     /* validate arguments */
  71.     if (!ls_valid(lsp)) {
  72.         errno = EINVAL;
  73.         return -1;
  74.     }
  75.  
  76.     /* check if lseq not open */
  77.     if (!(lsp->flags & LSOPEN)) {
  78.         errno = LSENOPEN;
  79.         return -1;
  80.     }
  81.  
  82.     /* check if file is open */
  83.     switch (l_type) {
  84.     case LS_RDLCK:
  85.         if (!(lsp->flags & LSREAD)) {
  86.             errno = LSENOPEN;
  87.             return -1;
  88.         }
  89.         bl_type = B_RDLCK;
  90.         break;
  91.     case LS_RDLKW:
  92.         if (!(lsp->flags & LSREAD)) {
  93.             errno = LSENOPEN;
  94.             return -1;
  95.         }
  96.         bl_type = B_RDLKW;
  97.         break;
  98.     case LS_WRLCK:
  99.         if (!(lsp->flags & LSWRITE)) {
  100.             errno = LSENOPEN;
  101.             return -1;
  102.         }
  103.         bl_type = B_WRLCK;
  104.         break;
  105.     case LS_WRLKW:
  106.         if (!(lsp->flags & LSWRITE)) {
  107.             errno = LSENOPEN;
  108.             return -1;
  109.         }
  110.         bl_type = B_WRLKW;
  111.         break;
  112.     case LS_UNLCK:
  113.         /* flush buffers */
  114.         rs = bflush(lsp->bp);
  115.         if (rs == -1) {
  116.             LSEPRINT;
  117.             return -1;
  118.         }
  119.         bl_type = B_UNLCK;
  120.         break;
  121.     default:
  122.         errno = EINVAL;
  123.         return -1;
  124.         break;
  125.     }
  126.  
  127.     /* lock lseq file */
  128.     rs = lockb(lsp->bp, bl_type, (bpos_t)0, (size_t)0);
  129.     if (rs == -1) {
  130.         if (errno != EAGAIN) LSEPRINT;
  131.         return -1;
  132.     }
  133.  
  134.     /* set status bits in lseq control structure */
  135.     switch (l_type) {
  136.     case LS_UNLCK:
  137.         if (lsgetlck(lsp) == LS_WRLCK) {    /* update header */
  138.             rs = bputh(lsp->bp, (void *)&lsp->lshdr);
  139.             if (rs == -1) {
  140.                 LSEPRINT;
  141.                 return -1;
  142.             }
  143.         }
  144.         lsp->flags &= ~LSLOCKS;
  145.         break;    /* case LS_UNLCK: */
  146.     case LS_RDLCK:
  147.     case LS_RDLKW:
  148.         if (lsgetlck(lsp) == LS_UNLCK) {
  149.             rs = bgeth(lsp->bp, (void *)&lsp->lshdr);
  150.             if (rs == -1) {
  151.                 LSEPRINT;
  152.                 return -1;
  153.             }
  154.             if (lsp->lshdr.flags & LSHMOD) {
  155.                 errno = LSECORRUPT;
  156.                 return -1;
  157.             }
  158.         }
  159.         lsp->flags |= LSRDLCK;
  160.         lsp->flags &= ~LSWRLCK;
  161.         break;    /* case LS_RDLCK: */
  162.     case LS_WRLCK:
  163.     case LS_WRLKW:
  164.         if (lsgetlck(lsp) == LS_UNLCK) {
  165.             rs = bgeth(lsp->bp, (void *)&lsp->lshdr);
  166.             if (rs == -1) {
  167.                 LSEPRINT;
  168.                 return -1;
  169.             }
  170.             if (lsp->lshdr.flags & LSHMOD) {
  171.                 errno = LSECORRUPT;
  172.                 return -1;
  173.             }
  174.         }
  175.         lsp->flags |= (LSRDLCK | LSWRLCK);
  176.         break;    /* case LS_WRLCK: */
  177.     default:
  178.         LSEPRINT;
  179.         errno = LSEPANIC;
  180.         return -1;
  181.         break;    /* default: */
  182.     }
  183.  
  184.     errno = 0;
  185.     return 0;
  186. }
  187.