home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-1.ZIP / SOURCE.ZIP / D4LOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-17  |  2.0 KB  |  90 lines

  1.  
  2. /*
  3.     d4lock( lock_code )   (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  4.  
  5.     - if (lock_code >  0)  the record is locked
  6.     - if (lock_code -1 )   lock the whole file
  7.     - if (lock_code == 0)  lock the record count bytes
  8.  
  9.     rc
  10.        0  -  SUCCESS
  11.       -1  -  Error
  12.       -2  -  Locked by Another Station
  13. */
  14.  
  15.  
  16.  
  17. #include "d4base.h"
  18. #include "u4error.h"
  19.  
  20. extern BASE *v4base ;
  21. extern int   v4cur_base ;
  22.  
  23. extern long  lseek(int, long, int) ;
  24.  
  25.  
  26. int d4lock( lock_code, do_wait )
  27. long  lock_code ;
  28. int   do_wait ;
  29. {
  30.    int     rc ;
  31.    BASE   *base_ptr ;
  32.  
  33.    if ( v4cur_base < 0 ) 
  34.    {
  35.       u4error( E_D_MISSING, (char *) 0 ) ; 
  36.       return( -1 ) ;
  37.    }
  38.  
  39.    base_ptr =  v4base+ v4cur_base ;
  40.    if ( lock_code < -1 )  lock_code = -1 ;
  41.  
  42.    /* if file is already locked, return normally */
  43.    if ( base_ptr->file_lock == 1 )
  44.     return( 0 ) ;
  45.  
  46.    /* if record is already locked, return normally */
  47.    if ( lock_code > 0 ) 
  48.       if ( base_ptr->rec_lock == lock_code)
  49.      return( 0) ;
  50.  
  51.    /* if record count bytes are already locked, return normally */
  52.    if ( lock_code == 0 )
  53.       if ( base_ptr->file_lock == 2 )
  54.      return( 0) ;
  55.  
  56.    /* do any necessary unlocking */
  57.    if ( lock_code == -1 )
  58.       if ( d4unlock( -1L ) < 0 )  return( -1 ) ;
  59.    if ( lock_code > 0 )
  60.       if ( base_ptr->rec_lock > 0 )
  61.      if ( d4unlock( 1L ) < 0 )  return( -1 ) ;
  62.  
  63.    rc       =  0 ;
  64.  
  65.    if (lock_code > 0)
  66.    {
  67.       /* Specified Record Number */
  68.       rc =  u4lock( base_ptr->dos_file, LOCK_START+ lock_code, 1L, do_wait) ;
  69.       if ( rc == 0 )  base_ptr->rec_lock = lock_code ;
  70.    }
  71.    else
  72.    {
  73.       if (lock_code == 0)
  74.       {
  75.      /* Record Count Bytes */
  76.      rc =  u4lock( base_ptr->dos_file, LOCK_START, 1L, do_wait ) ;
  77.      if ( rc == 0 )  base_ptr->file_lock =  2 ;
  78.       }
  79.       else
  80.       {
  81.      /* Whole File */
  82.      rc =  u4lock( base_ptr->dos_file, LOCK_START, LOCK_START, do_wait) ;
  83.      if ( rc == 0 )  base_ptr->file_lock =  1 ;
  84.       }
  85.    }
  86.    return( rc ) ;
  87. }
  88.  
  89.  
  90.