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

  1.  
  2. /* d4close.c   (c)Copyright Sequiter Software Inc., 1987, 1988, 1989.  All rights reserved.
  3.  
  4.    Closes a database file.
  5. */
  6.  
  7. #include "d4base.h"
  8. #ifndef UNIX
  9.    #include <io.h>
  10. #endif
  11. #include <string.h>
  12.  
  13. #include "u4error.h"
  14.  
  15. extern  BASE   *v4base  ;
  16. extern  int     v4cur_base  ;
  17. extern  int     v4last_base ;
  18.  
  19. d4close( )
  20. {
  21.    int   dos_file, rc, close_return ;
  22.    long  disk_time, header_time ;
  23.    BASE *base_ptr ;
  24.    struct
  25.    {
  26.       char yy ;
  27.       char mm ;
  28.       char dd ;
  29.    }  disk_update, header_update ;
  30.  
  31.    base_ptr =  v4base + v4cur_base ;
  32.    if ( v4cur_base < 0 ) 
  33.    {
  34.       u4error( E_D_MISSING, (char *) 0 ) ; 
  35.       return( -1 ) ;
  36.    }
  37.  
  38.    close_return =  0 ;
  39.  
  40.    h4free_memory( base_ptr->old_buf ) ;
  41.    h4free_memory( (char *) base_ptr->fields  ) ;
  42.  
  43.    #ifndef SMALL
  44.       /* Close the Index Files */
  45.       while ( base_ptr->index_ref >= 0 )
  46.      i4close( base_ptr->index_ref ) ;
  47.    #endif
  48.  
  49.    /* Close the Memo Files */
  50.    if ( base_ptr->memo_file >= 0 ) 
  51.       close( base_ptr->memo_file ) ;
  52.  
  53.    memcpy( (char *) &header_update, &base_ptr->yy, 3) ;
  54.    dos_file =  base_ptr->dos_file ;
  55.  
  56.    /* Lock the header update time & close the file */
  57.    d4lock( 0L,1 ) ;
  58.  
  59.    lseek( dos_file, 1L, 0 ) ;
  60.    if ( read( dos_file, (char *) &disk_update, 3) != 3)
  61.    {
  62.       u4error( E_READ, base_ptr->name, (char *) 0) ;
  63.       close_return =  -1 ;
  64.    }
  65.    else
  66.    {
  67.       disk_time  =  ((long)disk_update.yy<<16)  + ((long)disk_update.mm<<8) +
  68.              (long)disk_update.dd ;
  69.       header_time=  ((long)header_update.yy<<16)+ ((long)header_update.mm<<8)+
  70.              (long)header_update.dd ;
  71.       if ( disk_time < header_time )
  72.       {
  73.      /* Compare as Another User Might Have Modified More Recently */
  74.      lseek( dos_file, 1L, 0) ;
  75.      if( write( dos_file, (char *) &header_update, 3) != 3)
  76.      {
  77.         u4error( E_WRITE, base_ptr->name, (char *) 0) ;
  78.         close_return =  -1 ;
  79.      }
  80.       }
  81.    }
  82.  
  83.    if ( d4unlock( -1L ) < 0 )  close_return =  -1 ;
  84.  
  85.    rc = h4free( (char **) &v4base, v4cur_base ) ;
  86.    if ( v4cur_base == v4last_base )
  87.       v4last_base =  rc ;
  88.    v4cur_base =  v4last_base ;
  89.  
  90.    close( dos_file) ;
  91.  
  92.    return( close_return ) ;
  93. }
  94.  
  95.  
  96. d4close_all()
  97. {
  98.    while( v4last_base >= 0 )    if ( d4close() < 0 )  return( -1 ) ;
  99.    return( 0 ) ;
  100. }
  101.