home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / D4LOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-19  |  2.6 KB  |  112 lines

  1. /*
  2.     d4lock( lock_code )   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.     - if (lock_code >  0)  the record is locked
  5.     - if (lock_code -1 )   lock the whole file
  6.     - if (lock_code == 0)  lock the record count bytes
  7.  
  8.     rc
  9.        0  -  SUCCESS
  10.       -1  -  Error
  11.       -2  -  Locked by Another Station
  12. */
  13.  
  14. #include "d4base.h"
  15. #include "u4error.h"
  16.  
  17. #ifndef UNIX
  18. #include <io.h>
  19. #endif
  20.  
  21. extern BASE *v4base ;
  22. extern int   v4cur_base ;
  23.  
  24. #ifdef LOG
  25.    extern void logger( char *, char *) ;
  26. #endif
  27.  
  28.  
  29. int d4lock( long lock_code, int do_wait )
  30. {
  31.    int     rc ;
  32.    BASE   *base_ptr ;
  33.  
  34.    if ( v4cur_base < 0 ) 
  35.    {
  36.       u4error( E_D_MISSING, (char *) 0 ) ; 
  37.       return( -1 ) ;
  38.    }
  39.  
  40.    base_ptr =  v4base+ v4cur_base ;
  41.    if ( lock_code < -1L )  lock_code = -1L ;
  42.  
  43.    /* if file is already locked, return normally */
  44.    if ( base_ptr->file_lock == 1 )
  45.     return( 0 ) ;
  46.  
  47.    /* if record is already locked, return normally */
  48.    if ( lock_code > 0L ) 
  49.       if ( base_ptr->rec_lock == lock_code)
  50.      return 0 ;
  51.  
  52.    /* if record count bytes are already locked, return normally */
  53.    if ( lock_code == 0L )
  54.       if ( base_ptr->file_lock == 0 )
  55.      return( 0) ;
  56.  
  57.    /* do any necessary unlocking */
  58.    if ( lock_code == -1L )
  59.       if ( d4unlock( -1L ) < 0 )  return( -1 ) ;
  60.    if ( lock_code > 0L )
  61.       if ( base_ptr->rec_lock > 0L )
  62.      if ( d4unlock( 1L ) < 0 )  return( -1 ) ;
  63.  
  64.    rc =  0 ;
  65.  
  66.    if (lock_code >= 0L)
  67.    {
  68.       /* Specified Record Number */
  69.       #ifdef LOG
  70.          if ( lock_code == 0L )
  71.             logger( d4name(), "Before Record Count Byte Lock" ) ;
  72.          else
  73.          {
  74.             char buf[20] ;
  75.             c4ltoa( lock_code, buf, 10 ) ;
  76.             buf[10] = '\000' ;
  77.             logger( d4name(), buf ) ;
  78.          }
  79.       #endif
  80.       rc =  u4lock( base_ptr->file_hand, LOCK_START+ lock_code, 1L, do_wait) ;
  81.       #ifdef LOG
  82.          if ( lock_code == 0L )
  83.             logger( d4name(), "After Record Count Byte Lock" ) ;
  84.          else
  85.             logger( d4name(), "After Record Lock" ) ;
  86.       #endif
  87.       if ( rc == 0 )
  88.       {
  89.          if ( lock_code == 0L )
  90.             base_ptr->file_lock =  0 ;
  91.          else
  92.             base_ptr->rec_lock = lock_code ;
  93.       }
  94.    }
  95.    else
  96.    {
  97.       #ifdef LOG
  98.          logger( d4name(), "Before File Lock" ) ;
  99.       #endif
  100.       /* Whole File */
  101.       rc =  u4lock( base_ptr->file_hand, LOCK_START, LOCK_START, do_wait) ;
  102.       if ( rc == 0 )  base_ptr->file_lock =  1 ;
  103.       #ifdef LOG
  104.          logger( d4name(), "After File Lock" ) ;
  105.       #endif
  106.    }
  107.  
  108.    return( rc ) ;
  109. }
  110.  
  111.  
  112.