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

  1.  
  2. /* m4check.c   (c)Copyright Sequiter Software Inc., 1987-1990.  All rights reserved. */
  3.  
  4. #include "p4misc.h"
  5. #include "d4all.h"
  6. #include "u4error.h"
  7. #include "m4.h"
  8.  
  9. #include <string.h>
  10. #ifndef UNIX
  11. #include <io.h>
  12. #endif
  13.  
  14. extern  int  m4to_disk( FREE_AREA *, M *) ;
  15. extern  int  m4skip_area( M *) ;
  16. extern  BASE  *v4base ;
  17.  
  18. long   data[5] ;
  19.  
  20. #define  NO_ENTRIES    data[0]  /* The number of free chain entries */
  21. #define  FREE_BLOCKS   data[1]  /* The number of free blocks */
  22. #define  ADJACENT      data[2]  /* The number of adjacent free entries */
  23. #define  LOST_BLOCKS   data[3]  /* The number of lost blocks */
  24. #define  USED_BLOCKS   data[4]  /* The number of used blocks */
  25.  
  26.  
  27. long  *m4check( long field_ref )
  28. {
  29.    M     m ;
  30.    MEMO_BLOCK  memo_block ;
  31.    char  *ptr ;
  32.    long  on_rec, memo_num, tot_len, tot_blocks, len_read ;
  33.    int   rc ;
  34.  
  35.    memset( (char *) &m, (int) 0, sizeof(m) ) ;
  36.    m.base_ref   =  (int) (field_ref >> 16) ;
  37.  
  38.    if ( m4open(m.base_ref) < 0 )   return (long *) 0 ;
  39.  
  40.    m.block_size =  v4base[m.base_ref].memo_size ;
  41.    if ( m.block_size <= 0 )  m.block_size = 0x200 ;
  42.    m.hand  =  v4base[m.base_ref].memo_file ;
  43.  
  44.    memset( (char *) data, 0, sizeof(data) ) ;
  45.  
  46.    if ( (ptr =  h4alloc( 0x7FFF )) == (char *) 0 )  return( (long *) 0 ) ;
  47.  
  48.    for( rc = m4skip_area(&m); rc == 0  &&  m.cur.num < 0xFFFFFFL; rc = m4skip_area(&m) )
  49.    {
  50.       NO_ENTRIES++ ;
  51.       FREE_BLOCKS +=  m.cur.num ;
  52.  
  53.       if ( m.cur.block_no == m.prev.block_no + m.prev.num  &&  m.cur.block_no != 0L )
  54.      ADJACENT++ ;
  55.    }
  56.    if ( rc < 0 )
  57.    {
  58.       h4free_memory(ptr) ;
  59.       return (long *) 0 ;
  60.    }
  61.  
  62.    for ( on_rec = 1L ; on_rec <= d4reccount(); on_rec++ )
  63.    {
  64.       if ( d4go( on_rec )  != 0 )  
  65.       {
  66.      h4free_memory(ptr) ;
  67.      return (long *) 0 ;
  68.       }
  69.  
  70.       memo_num =  (long) c4atol(f4ptr(field_ref), f4width(field_ref)) ;
  71.       if ( memo_num  == 0L )  continue ;
  72.  
  73.       lseek( m.hand, memo_num * m.block_size, 0 ) ;
  74.       rc = read( m.hand, (char *) &memo_block, sizeof(MEMO_BLOCK) ) ;
  75.       if ( rc < 0 )
  76.       {
  77.      u4error( E_READ, "Memo File:", m4name(m.base_ref), (char *) 0 ) ;
  78.      h4free_memory( ptr ) ;
  79.      return (long *) 0 ;
  80.       }
  81.  
  82.       if ( memo_block.minus_one == -1 )
  83.      len_read =  memo_block.num_chars ;
  84.       else
  85.       {
  86.      len_read =  (int) m4read( field_ref, on_rec, (char *) ptr, 0x7FFF) ;
  87.      if ( len_read < 0 )
  88.      {
  89.         h4free_memory(ptr) ;
  90.         return (long *) 0 ;
  91.      }
  92.       }
  93.  
  94.       USED_BLOCKS += (len_read+ m.block_size-1) / m.block_size ;
  95.    }
  96.  
  97.    NO_ENTRIES-- ;
  98.    h4free_memory(ptr) ;
  99.  
  100.    if ( rc < 0 )
  101.       return (long *) 0 ;
  102.  
  103.    /* Compute the number of lost blocks */
  104.    if ( (tot_len =  filelength(m.hand)) < 0L)
  105.       return (long *) 0 ;
  106.  
  107.    tot_blocks =  (tot_len + m.block_size -1) / m.block_size ;
  108.    LOST_BLOCKS =  tot_blocks - USED_BLOCKS - FREE_BLOCKS - 1 ;
  109.  
  110.    return  data ;
  111. }
  112.