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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cblock.c    1.1 - 89/08/31" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. #include <lseq.h>
  9. #include "cbase_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      cblock - lock cbase
  14.  
  15. SYNOPSIS
  16.      #include <cbase.h>
  17.  
  18.      int cblock(cbp, l_type)
  19.      cbase_t *cbp;
  20.      int l_type;
  21.  
  22. DESCRIPTION
  23.      The cblock function controls the lock status of a cbase.  The cbp
  24.      argument is an open cbase.  l_type indicates the target status of
  25.      the lock on the cbase.
  26.  
  27.      The lock types available are:
  28.  
  29.           CB_RDLCK - lock cbase for reading
  30.           CB_WRLCK - lock cbase for reading and writing
  31.           CB_RDLKW - lock cbase for reading (wait)
  32.           CB_WRLKW - lock cbase for reading and writing (wait)
  33.           CB_UNLCK - unlock cbase
  34.  
  35.      For the lock types which wait, cblock will not return until the
  36.      lock is available.  For the lock types which do not wait, if the
  37.      lock is unavailable because of a lock held by another process  a
  38.      value of -1 is returned and errno set to EAGAIN.
  39.  
  40.      cblock will fail if one or more of the following is true:
  41.  
  42.      [EAGAIN]       l_type is CB_RDLCK and the cbase is already
  43.                     write locked by another process, or l_type
  44.                     is CB_WRLCK and the cbase is already read
  45.                     or write locked by another process.
  46.      [EINVAL]       cbp is is not a valid cbase.
  47.      [EINVAL]       l_type is not one of the valid lock types.
  48.      [CBECORRUPT]   Either the record file or one of the key
  49.                     files of cbp is corrupt.
  50.      [CBENOPEN]     cbp is not open.
  51.      [CBENOPEN]     l_type is CB_RDLCK or CB_RDLKW and cbp
  52.                     is not opened for reading or l_type is
  53.                     CB_WRLCK or CB_WRLKW and cbp not open for
  54.                     writing.
  55.  
  56. SEE ALSO
  57.      cbgetlck.
  58.  
  59. DIAGNOSTICS
  60.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  61.      value of -1 is returned, and errno set to indicate the error.
  62.  
  63. ------------------------------------------------------------------------------*/
  64. int cblock(cbp, l_type)
  65. cbase_t * cbp;
  66. int       l_type;
  67. {
  68.     int    rs        = 0;
  69.     int    lsl_type    = 0;
  70.     int    btl_type    = 0;
  71.     int    i        = 0;
  72.  
  73.     errno = 0;
  74.  
  75.     /* validate arguments */
  76.     if (!cb_valid(cbp)) {
  77.         errno = EINVAL;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if cbase not open */
  82.     if (!(cbp->flags & CBOPEN)) {
  83.         errno = CBENOPEN;
  84.         return -1;
  85.     }
  86.  
  87.     /* check open flags and set lsl_type and btl_type */
  88.     switch (l_type) {
  89.     case CB_RDLCK:
  90.         if (!(cbp->flags & CBREAD)) {
  91.             errno = CBENOPEN;
  92.             return -1;
  93.         }
  94.         lsl_type = LS_RDLCK;
  95.         btl_type = BT_RDLCK;
  96.         break;
  97.     case CB_RDLKW:
  98.         if (!(cbp->flags & CBREAD)) {
  99.             errno = CBENOPEN;
  100.             return -1;
  101.         }
  102.         lsl_type = LS_RDLKW;
  103.         btl_type = BT_RDLCK;
  104.         break;
  105.     case CB_WRLCK:
  106.         if (!(cbp->flags & CBWRITE)) {
  107.             errno = CBENOPEN;
  108.             return -1;
  109.         }
  110.         lsl_type = LS_WRLCK;
  111.         btl_type = BT_WRLCK;
  112.         break;
  113.     case CB_WRLKW:
  114.         if (!(cbp->flags & CBWRITE)) {
  115.             errno = CBENOPEN;
  116.             return -1;
  117.         }
  118.         lsl_type = LS_WRLKW;
  119.         btl_type = BT_WRLCK;
  120.         break;
  121.     case CB_UNLCK:
  122.         lsl_type = LS_UNLCK;
  123.         btl_type = BT_UNLCK;
  124.         break;
  125.     default:
  126.         errno = EINVAL;
  127.         return -1;
  128.         break;
  129.     }
  130.  
  131.     /* lock record file */
  132.     rs = lslock(cbp->lsp, lsl_type);
  133.     if (rs == -1) {
  134.         if (errno == EAGAIN) {
  135.             return -1;
  136.         }
  137.         if (errno == LSECORRUPT) {
  138.             errno = CBECORRUPT;
  139.             return -1;
  140.         }
  141.         CBEPRINT;
  142.         return -1;
  143.     }
  144.  
  145.     /* lock key files */
  146.     for (i = 0; i < cbp->fldcnt; i++) {
  147.         if (cbp->fields[i].flags & CBFKEY) {
  148.             rs = btlock(cbp->btp[i], btl_type);
  149.             if (rs == -1) {
  150.                 CBEPRINT;
  151.                 return -1;
  152.             }
  153.         }
  154.     }
  155.  
  156.     /* set lock bits */
  157.     switch (l_type) {
  158.     case CB_RDLCK:
  159.     case CB_RDLKW:
  160.         cbp->flags |= CBRDLCK;
  161.         cbp->flags &= ~CBWRLCK;
  162.         break;
  163.     case CB_WRLCK:
  164.     case CB_WRLKW:
  165.         cbp->flags |= (CBRDLCK | CBWRLCK);
  166.         break;
  167.     case CB_UNLCK:
  168.         cbp->flags &= ~CBLOCKS;
  169.         break;
  170.     default:
  171.         CBEPRINT;
  172.         errno = CBEPANIC;
  173.         return -1;
  174.         break;
  175.     }
  176.  
  177.     errno = 0;
  178.     return 0;
  179. }
  180.