home *** CD-ROM | disk | FTP | other *** search
-
- /* u4lock.c (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
-
- Returns
- 0 Normal
- -1 Error
- -2 Locked by other User
- */
-
- #include "p4misc.h"
- #include "d4all.h"
- #include "u4error.h"
-
- #include <errno.h>
-
- #ifdef NO_LOCK
- u4lock( int file_handle, long o_set, long num_bytes, int do_wait )
- {
- return 0 ;
- }
- #endif
-
- #ifdef DO_ERRNO
- extern int errno ;
- #endif
-
- #ifdef LOCK_TRAP
-
- typedef struct lock_info_st
- {
- int next ;
- int prev ;
- int hand ;
- long start_pos ;
- long len ;
- long end_pos ;
- } LOCK_INFO ;
-
- static LOCK_INFO *v4lock_info = 0 ;
- static int v4lock_last = -1 ;
-
- d4lock_find( int h, long p, long l )
- {
- int ref ;
- LOCK_INFO *l_ptr ;
-
- for ( ref = v4lock_last; ref >= 0; ref = v4lock_info[ref].prev )
- {
- l_ptr = v4lock_info+ref ;
- if ( l_ptr->hand == h && l_ptr->start_pos == p && l_ptr->len == l )
- return ref ;
- if ( l_ptr->hand == h )
- {
- /* Check for Overlap. */
- if ( l_ptr->start_pos >= p && l_ptr->start_pos <= p+l-1 ||
- l_ptr->end_pos >= p && l_ptr->end_pos <= p+l-1 ||
- p >= l_ptr->start_pos && p <= l_ptr->end_pos ||
- p+l-1 >= l_ptr->start_pos && p+l-1 <= l_ptr->end_pos )
- u4error( E_INTERNAL, "Locking Overlap Detected", (char *) 0 ) ;
- }
- }
- return -2 ;
- }
-
- d4lock_remove( int h, long p, long l )
- {
- int ref, new_ref ;
-
- ref = d4lock_find( h,p,l ) ;
- if ( ref < 0 ) u4error( E_INTERNAL, "Removing Lock which was never placed.", (char *) 0 ) ;
-
- new_ref = h4free( (char **) &v4lock_info, ref ) ;
- if ( ref == v4lock_last )
- v4lock_last = new_ref ;
-
- return 0 ;
- }
-
- d4lock_save( int h, long p, long l )
- {
- if ( v4lock_info == 0 )
- {
- if ( h4create( (char **) &v4lock_info, 100, sizeof(LOCK_INFO), 50 ) < 0)
- {
- u4error( E_MEMORY, (char *) 0 ) ;
- return -1 ;
- }
- }
-
- if ( d4lock_find(h,p,l) != -2 )
- u4error( E_INTERNAL, "Locking Overlap Detected", (char *) 0 ) ;
-
- if ( (v4lock_last = h4get( (char **) &v4lock_info, v4lock_last)) < 0 )
- {
- u4error( E_MEMORY, (char *) 0 ) ;
- return -1 ;
- }
- v4lock_info[v4lock_last].hand = h ;
- v4lock_info[v4lock_last].start_pos = p ;
- v4lock_info[v4lock_last].len = l ;
- v4lock_info[v4lock_last].end_pos = p+l-1 ;
- }
- #endif
-
- #ifdef DO_LOCK
-
- #include <time.h>
- #include <io.h>
-
- u4lock( int file_handle, long o_set, long num_bytes, int do_wait )
- {
- int rc ;
-
- errno = 0 ;
- rc = lock( file_handle, o_set, num_bytes ) ;
- #ifdef IS_386
- rc &= 0xFFFF ;
- #endif
- if (rc == 0 || errno == EINVAL)
- {
- #ifdef LOCK_TRAP
- d4lock_save( file_handle, o_set, num_bytes ) ;
- #endif
- return( 0) ; /* Single User or Success */
- }
-
- if (errno != EACCES)
- {
- char buf[33] ;
- c4ltoa( (long) errno, buf, 10 ) ;
- buf[10] = '\000' ;
- u4error( E_LOCK, "errno", buf, (char *) 0 ) ;
- return( -1) ;
- }
-
- if ( ! do_wait ) return( -2 ) ;
-
- while (1)
- {
- time_t old_time ;
-
- time( &old_time) ; /* wait a second & try lock again */
- while ( time( (time_t *) 0 ) <= old_time) ;
-
- if ( lock( file_handle, o_set, num_bytes ) == 0 )
- {
- #ifdef LOCK_TRAP
- d4lock_save( file_handle, o_set, num_bytes ) ;
- #endif
- return( 0) ;
- }
- }
- }
- #endif
-
-
- #ifdef DO_LOCKING
-
- #ifdef UNIX
- #include <sys/locking.h>
- #else
- #include <sys\locking.h>
- #include <io.h>
- #endif
-
- u4lock( int file_handle, long o_set, long num_bytes, int do_wait )
- {
- int rc ;
-
- errno = 0 ;
-
- lseek( file_handle, o_set, 0 ) ;
- rc = locking( file_handle, LK_NBLCK, num_bytes) ;
- #ifdef IS_386
- rc &= 0xFFFF ;
- #endif
-
- if (rc == 0 || errno == EINVAL)
- {
- #ifdef LOCK_TRAP
- d4lock_save( file_handle, o_set, num_bytes ) ;
- #endif
- return( 0) ; /* Single User or Success */
- }
-
- #ifndef UNIX
- if (errno != EACCES)
- {
- char buf[33] ;
- c4ltoa( (long) errno, buf, 10 ) ;
- buf[10] = '\000' ;
- u4error( E_LOCK, "errno", buf, (char *) 0 ) ;
- return( -1) ;
- }
- #endif
-
- if ( ! do_wait ) return( -2 ) ;
-
- while (1)
- {
- lseek( file_handle, o_set, 0 ) ;
- if ( locking( file_handle, LK_LOCK, num_bytes) == 0)
- {
- #ifdef LOCK_TRAP
- d4lock_save( file_handle, o_set, num_bytes ) ;
- #endif
- return( 0) ; /* Single User or Success */
- }
- }
- }
- #endif
-