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

  1.  
  2. /* u4lock.c    (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.    Returns
  5.       0   Normal
  6.      -1   Error
  7.      -2   Locked by other User
  8. */
  9.  
  10. #include "p4misc.h"
  11. #include "d4all.h"
  12. #include "u4error.h"
  13.  
  14. #include <errno.h>
  15.  
  16. #ifdef NO_LOCK
  17. u4lock( int file_handle, long o_set, long num_bytes, int do_wait )
  18. {
  19.    return 0 ;
  20. }
  21. #endif
  22.  
  23. #ifdef DO_ERRNO
  24.    extern int  errno ;
  25. #endif
  26.  
  27. #ifdef LOCK_TRAP
  28.  
  29. typedef struct lock_info_st
  30. {
  31.    int     next ;   
  32.    int     prev ;   
  33.    int     hand ;
  34.    long    start_pos ;
  35.    long    len ;
  36.    long    end_pos ;
  37. }  LOCK_INFO ;
  38.  
  39. static LOCK_INFO *v4lock_info = 0 ;
  40. static int        v4lock_last = -1 ;
  41.  
  42. d4lock_find( int h, long p, long l )
  43. {
  44.    int ref ;
  45.    LOCK_INFO *l_ptr ;
  46.  
  47.    for ( ref = v4lock_last; ref >= 0; ref =  v4lock_info[ref].prev )
  48.    {
  49.       l_ptr =  v4lock_info+ref ;
  50.       if ( l_ptr->hand == h  && l_ptr->start_pos == p && l_ptr->len == l )
  51.          return  ref ;
  52.       if ( l_ptr->hand == h )
  53.       {
  54.      /* Check for Overlap. */
  55.      if ( l_ptr->start_pos >= p && l_ptr->start_pos <= p+l-1  ||
  56.           l_ptr->end_pos >= p   && l_ptr->end_pos   <= p+l-1  ||
  57.           p >= l_ptr->start_pos && p <= l_ptr->end_pos        ||
  58.           p+l-1 >= l_ptr->start_pos && p+l-1 <= l_ptr->end_pos )
  59.         u4error( E_INTERNAL, "Locking Overlap Detected", (char *) 0 ) ;
  60.       }
  61.    }
  62.    return -2 ;
  63. }
  64.  
  65. d4lock_remove( int h, long p, long l )
  66. {
  67.    int  ref, new_ref ;
  68.  
  69.    ref =  d4lock_find( h,p,l ) ;
  70.    if ( ref < 0 )   u4error( E_INTERNAL, "Removing Lock which was never placed.", (char *) 0 ) ;
  71.  
  72.    new_ref =  h4free( (char **) &v4lock_info, ref ) ;
  73.    if ( ref == v4lock_last )
  74.       v4lock_last =  new_ref ;
  75.  
  76.    return 0 ;
  77. }
  78.  
  79. d4lock_save( int h, long p, long l )
  80. {
  81.    if ( v4lock_info == 0 )
  82.    {
  83.       if ( h4create( (char **) &v4lock_info, 100, sizeof(LOCK_INFO), 50 ) < 0)
  84.       {
  85.      u4error( E_MEMORY, (char *) 0 ) ;
  86.      return -1 ;
  87.       }
  88.    }
  89.  
  90.    if ( d4lock_find(h,p,l) != -2 )
  91.       u4error( E_INTERNAL, "Locking Overlap Detected", (char *) 0 ) ;
  92.  
  93.    if ( (v4lock_last = h4get( (char **) &v4lock_info, v4lock_last)) < 0 )
  94.    {
  95.       u4error( E_MEMORY, (char *) 0 ) ;
  96.       return -1 ;
  97.    }
  98.    v4lock_info[v4lock_last].hand = h ;
  99.    v4lock_info[v4lock_last].start_pos = p ;
  100.    v4lock_info[v4lock_last].len  = l ;
  101.    v4lock_info[v4lock_last].end_pos =  p+l-1 ;
  102. }
  103. #endif
  104.  
  105. #ifdef DO_LOCK
  106.  
  107. #include <time.h>
  108. #include <io.h>
  109.  
  110. u4lock( int file_handle, long o_set, long num_bytes, int do_wait )
  111. {
  112.    int    rc ;
  113.  
  114.    errno =  0 ;
  115.    rc =  lock( file_handle, o_set, num_bytes ) ;
  116.    #ifdef IS_386
  117.       rc &= 0xFFFF ;
  118.    #endif
  119.    if (rc == 0 ||  errno == EINVAL)
  120.    {
  121.       #ifdef LOCK_TRAP
  122.          d4lock_save( file_handle, o_set, num_bytes ) ;
  123.       #endif
  124.       return( 0) ;  /* Single User or Success */
  125.    }
  126.  
  127.    if (errno != EACCES)
  128.    {
  129.       char  buf[33] ;
  130.       c4ltoa( (long) errno, buf, 10 ) ;
  131.       buf[10] = '\000' ;
  132.       u4error( E_LOCK, "errno", buf, (char *) 0 ) ;
  133.       return( -1) ;
  134.    }
  135.  
  136.    if ( ! do_wait )  return( -2 ) ;
  137.  
  138.    while (1)
  139.    {
  140.       time_t  old_time ;
  141.  
  142.       time( &old_time) ;   /* wait a second & try lock again */
  143.       while (  time( (time_t *) 0 ) <=  old_time)    ;
  144.  
  145.       if ( lock( file_handle, o_set, num_bytes ) == 0 )
  146.       {
  147.          #ifdef LOCK_TRAP
  148.             d4lock_save( file_handle, o_set, num_bytes ) ;
  149.          #endif
  150.          return( 0) ;
  151.       }
  152.    }
  153. }
  154. #endif
  155.  
  156.  
  157. #ifdef DO_LOCKING
  158.  
  159. #ifdef UNIX
  160. #include <sys/locking.h>
  161. #else
  162. #include <sys\locking.h>
  163. #include <io.h>
  164. #endif
  165.  
  166. u4lock( int file_handle, long o_set, long num_bytes, int do_wait )
  167. {
  168.    int    rc ;
  169.  
  170.    errno =  0 ;
  171.  
  172.    lseek( file_handle, o_set, 0 ) ;
  173.    rc =  locking( file_handle, LK_NBLCK, num_bytes) ;
  174.    #ifdef IS_386
  175.       rc &= 0xFFFF ;
  176.    #endif
  177.  
  178.    if (rc == 0 ||  errno == EINVAL)
  179.    {
  180.       #ifdef LOCK_TRAP
  181.          d4lock_save( file_handle, o_set, num_bytes ) ;
  182.       #endif
  183.       return( 0) ;  /* Single User or Success */
  184.    }
  185.  
  186.    #ifndef UNIX
  187.       if (errno != EACCES)
  188.       {
  189.          char  buf[33] ;
  190.      c4ltoa( (long) errno, buf, 10 ) ;
  191.      buf[10] = '\000' ;
  192.          u4error( E_LOCK, "errno", buf, (char *) 0 ) ;
  193.          return( -1) ;
  194.       }
  195.    #endif
  196.  
  197.    if ( ! do_wait )  return( -2 ) ;
  198.  
  199.    while (1)
  200.    {
  201.       lseek( file_handle, o_set, 0 ) ;
  202.       if ( locking( file_handle, LK_LOCK, num_bytes) == 0)
  203.       {
  204.          #ifdef LOCK_TRAP
  205.             d4lock_save( file_handle, o_set, num_bytes ) ;
  206.          #endif
  207.          return( 0) ;  /* Single User or Success */
  208.       }
  209.    }
  210. }
  211. #endif
  212.