home *** CD-ROM | disk | FTP | other *** search
- /*
- d4lock( lock_code ) (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
-
- - if (lock_code > 0) the record is locked
- - if (lock_code -1 ) lock the whole file
- - if (lock_code == 0) lock the record count bytes
-
- rc
- 0 - SUCCESS
- -1 - Error
- -2 - Locked by Another Station
- */
-
- #include "d4base.h"
- #include "u4error.h"
-
- #ifndef UNIX
- #include <io.h>
- #endif
-
- extern BASE *v4base ;
- extern int v4cur_base ;
-
- #ifdef LOG
- extern void logger( char *, char *) ;
- #endif
-
-
- int d4lock( long lock_code, int do_wait )
- {
- int rc ;
- BASE *base_ptr ;
-
- if ( v4cur_base < 0 )
- {
- u4error( E_D_MISSING, (char *) 0 ) ;
- return( -1 ) ;
- }
-
- base_ptr = v4base+ v4cur_base ;
- if ( lock_code < -1L ) lock_code = -1L ;
-
- /* if file is already locked, return normally */
- if ( base_ptr->file_lock == 1 )
- return( 0 ) ;
-
- /* if record is already locked, return normally */
- if ( lock_code > 0L )
- if ( base_ptr->rec_lock == lock_code)
- return 0 ;
-
- /* if record count bytes are already locked, return normally */
- if ( lock_code == 0L )
- if ( base_ptr->file_lock == 0 )
- return( 0) ;
-
- /* do any necessary unlocking */
- if ( lock_code == -1L )
- if ( d4unlock( -1L ) < 0 ) return( -1 ) ;
- if ( lock_code > 0L )
- if ( base_ptr->rec_lock > 0L )
- if ( d4unlock( 1L ) < 0 ) return( -1 ) ;
-
- rc = 0 ;
-
- if (lock_code >= 0L)
- {
- /* Specified Record Number */
- #ifdef LOG
- if ( lock_code == 0L )
- logger( d4name(), "Before Record Count Byte Lock" ) ;
- else
- {
- char buf[20] ;
- c4ltoa( lock_code, buf, 10 ) ;
- buf[10] = '\000' ;
- logger( d4name(), buf ) ;
- }
- #endif
- rc = u4lock( base_ptr->file_hand, LOCK_START+ lock_code, 1L, do_wait) ;
- #ifdef LOG
- if ( lock_code == 0L )
- logger( d4name(), "After Record Count Byte Lock" ) ;
- else
- logger( d4name(), "After Record Lock" ) ;
- #endif
- if ( rc == 0 )
- {
- if ( lock_code == 0L )
- base_ptr->file_lock = 0 ;
- else
- base_ptr->rec_lock = lock_code ;
- }
- }
- else
- {
- #ifdef LOG
- logger( d4name(), "Before File Lock" ) ;
- #endif
- /* Whole File */
- rc = u4lock( base_ptr->file_hand, LOCK_START, LOCK_START, do_wait) ;
- if ( rc == 0 ) base_ptr->file_lock = 1 ;
- #ifdef LOG
- logger( d4name(), "After File Lock" ) ;
- #endif
- }
-
- return( rc ) ;
- }
-
-
-