home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / SOURCE / D4CLOSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  2.2 KB  |  95 lines

  1.  
  2. /* d4close.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved.
  3.  
  4.    Closes a database file.
  5. */
  6.  
  7. #include "d4all.h"
  8. #include "u4error.h"
  9.  
  10. #include <time.h>
  11. #ifndef UNIX
  12. #include <io.h>
  13. #endif
  14.  
  15. extern  BASE   *v4base  ;
  16. extern  int     v4cur_base  ;
  17. extern  int     v4last_base ;
  18.  
  19. d4close()
  20. {
  21.    int   file_hand, rc, close_return ;
  22.    BASE *base_ptr ;
  23.  
  24.    long time_val ;   /* For Update Time */
  25.    struct tm  *tm_ptr ;
  26.  
  27.    x4filter_reset() ;
  28.    x4relate_reset() ;
  29.  
  30.    base_ptr =  v4base + v4cur_base ;
  31.    if ( v4cur_base < 0 )
  32.    {
  33.       u4error( E_D_MISSING, (char *) 0 ) ;
  34.       return( -1 ) ;
  35.    }
  36.  
  37.    close_return =  0 ;
  38.  
  39.    if ( d4flush(v4cur_base) < 0 )  close_return =  -1 ;
  40.    h4free_memory( base_ptr->old_buf ) ;
  41.    h4free_memory( (char *) base_ptr->fields  ) ;
  42.  
  43.    if ( d4buf_total( 0L, 0, 1) < 0 )
  44.       close_return =  -1 ;
  45.  
  46.    #ifndef SMALL
  47.       /* Close the Index Files */
  48.       while ( base_ptr->index_ref >= 0 )
  49.      if ( i4close( base_ptr->index_ref) < 0 )  close_return = -1 ;
  50.    #endif
  51.  
  52.    /* Close the Memo Files */
  53.    if ( base_ptr->memo_file >= 0 )
  54.       close( base_ptr->memo_file ) ;
  55.  
  56.    file_hand =  base_ptr->file_hand ;
  57.  
  58.    /* Lock the header, update the header, and close the file */
  59.    if ( d4lock( 0L,1 ) == -1 )  close_return =  -1 ;
  60.    d4reccount() ;  /* Fills in the 'header 'num_recs' */
  61.  
  62.    /* Update the last Update Time */
  63.    time ( (time_t *) &time_val) ;
  64.    tm_ptr =  localtime( (time_t *) &time_val) ;
  65.  
  66.    base_ptr->yy =  (char) tm_ptr->tm_year  ;
  67.    base_ptr->mm =  (char) tm_ptr->tm_mon+ (char) 1  ;
  68.    base_ptr->dd =  (char) tm_ptr->tm_mday  ;
  69.  
  70.    lseek( file_hand, 1L, 0 ) ;
  71.    if( write( file_hand, (char *) &base_ptr->yy, 7) != 7)
  72.    {
  73.       u4error( E_WRITE, base_ptr->name, (char *) 0) ;
  74.       close_return =  -1 ;
  75.    }
  76.  
  77.    if ( d4unlock( -1L ) < 0 )  close_return =  -1 ;
  78.  
  79.    rc = h4free( (char **) &v4base, v4cur_base ) ;
  80.    if ( v4cur_base == v4last_base )
  81.       v4last_base =  rc ;
  82.    v4cur_base =  v4last_base ;
  83.  
  84.    close( file_hand) ;
  85.  
  86.    return( close_return ) ;
  87. }
  88.  
  89.  
  90. d4close_all()
  91. {
  92.    while( v4last_base >= 0 )    if ( d4close() < 0 )  return( -1 ) ;
  93.    return( 0 ) ;
  94. }
  95.