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 / I4UNLOCK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-14  |  2.2 KB  |  90 lines

  1.  
  2. /*  i4unlock.c    (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  3.  
  4.     Unlocks an index file or all the index files corresponding to the
  5.     current database.
  6. */
  7.  
  8. #include "d4base.h"
  9. #include "u4error.h"
  10. #include <errno.h>
  11. #include <stdlib.h>
  12.  
  13. #ifndef UNIX
  14.    #include <io.h>
  15. #endif
  16.  
  17. #ifndef TURBO     
  18.    #ifdef UNIX
  19.       #include <sys/locking.h>
  20.    #else
  21.       #include <sys\locking.h>
  22.    #endif
  23. #endif
  24.  
  25. extern   INDEX  *v4index ;
  26. extern   BLOCK  *v4block ;
  27.  
  28.  
  29. int i4unlock( index_ref )
  30. int       index_ref ;
  31. {
  32.    int    rc, block_on, h_size ;
  33.    INDEX *index_ptr ;
  34.  
  35.    if ( index_ref >= 0)
  36.    {
  37.       index_ptr =  v4index + index_ref ;
  38.  
  39.       /* First, flush the buffers to disk. */
  40.       for( block_on = index_ptr->block_ref; block_on >= 0; block_on = v4block[block_on].prev )
  41.          if ( v4block[block_on].wrt )  b4write( index_ref, block_on ) ;
  42.       for( block_on = index_ptr->block_last; block_on >= 0; block_on = v4block[block_on].prev )
  43.          if ( v4block[block_on].wrt )  b4write( index_ref, block_on ) ;
  44.  
  45.       if ( index_ptr->lock )
  46.       {
  47.      if ( index_ptr->version != index_ptr->old_version ) 
  48.      {
  49.         index_ptr->old_version =  index_ptr->version ;
  50.             lseek( index_ptr->dos_file, (long) 0, 0) ;
  51.         #ifdef CLIPPER
  52.                h_size =  (int) ((char *)(v4index+1) - (char *)&v4index->sign) ;
  53.                rc =  write( index_ptr->dos_file, (char *)&index_ptr->sign, h_size ) ;
  54.         #else
  55.                h_size =  (int) ((char *)(v4index+1) - (char *)&v4index->root) ;
  56.                rc =  write( index_ptr->dos_file, (char *)&index_ptr->root, h_size ) ;
  57.         #endif
  58.         if ( rc != (int) h_size ) 
  59.         {
  60.            u4error( E_WRITE, index_ptr->name, (char *) 0 ) ;
  61.            return -1 ;
  62.         }
  63.          }
  64.      rc =  u4unlock( index_ptr->dos_file, LOCK_START, 1L ) ;
  65.      if (rc != 0  &&  errno != EINVAL)
  66.      {
  67.         u4error( E_UNLOCK, index_ptr->name, (char *) 0) ;
  68.         return( -1) ;
  69.      }
  70.  
  71.      index_ptr->lock = 0 ;
  72.       }
  73.    }
  74.    else
  75.    {
  76.       int  index_on ;
  77.  
  78.       index_on =  d4ptr()->index_ref ;
  79.       while ( index_on >= 0)
  80.       {
  81.      if ( i4unlock(index_on) < 0) return( -1) ;
  82.      index_on =  v4index[index_on].prev ;
  83.       }
  84.    }
  85.  
  86.    return( 0) ;
  87. }
  88.  
  89.  
  90.