home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / GT1800S.ZIP / DRAM2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-11  |  4.5 KB  |  164 lines

  1. #include "define.h"
  2. #include <stdio.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <dos.h>
  7. #include <sys\stat.h>
  8. #include <errno.h>
  9. #include "futil.h"
  10.  
  11. extern INTEGER hash_max;
  12. extern INTEGER hash_crc;
  13. extern INTEGER hash_value;
  14. extern INTEGER delta_hash;
  15. extern INTEGER hash_first;
  16.  
  17. extern INTEGER delta_calc(void);
  18. extern INTEGER next_hash(void);
  19.  
  20. /*****
  21.  
  22.     The function 'prt_error()' is called when an error is returned
  23.     by the I/O library.
  24.  
  25.        fs ..... type of error, OPEN, CLOSE, etc.
  26.  
  27.        ns ..... name of file causing error.
  28.  
  29. *****/
  30. void prt_error(char *fs,char *ns)
  31. {
  32.     char work[82];
  33.  
  34.     strncpy(work,ns,78);
  35.     work[78]=0;
  36. #ifdef UTILITY
  37.     printf(
  38. #else
  39.     wf_printf(
  40. #endif
  41.     "\r\n\nERROR: unable to %s: %s\r\n\n",fs,work
  42.     );
  43.     sleep(1);
  44. }
  45.  
  46. /*****
  47.  
  48.     The function 'createfile()' is used to open files for creation.
  49.     It handles some errors that might be encountered on LANs and
  50.     returns a valid file handle or -1 on error.
  51.  
  52.        name ..... the name of the file to be created.
  53.  
  54. *****/
  55. int createfile(char *name)
  56. {
  57.     int loop, handle;
  58.  
  59.     handle = loop = -1;
  60.     while (handle < 0) {
  61.     if ((handle = _creat(name,0)) < 0) {
  62.         if ((++loop > 30) || (errno == ENOENT) || (errno == EMFILE)) {
  63.         prt_error("create",name);
  64.         return(-1);
  65.         }
  66.         sleep(1);
  67.     }
  68.     }
  69.     return(handle);
  70. }
  71.  
  72. /*****
  73.  
  74.        The function 'closefile()' is used to close files.  Error processing
  75.        is done if the file cannot be closed.
  76.  
  77.       handle ..... the file handle to be closed.
  78.  
  79.       name ....... the name of the file to be closed.
  80.  
  81. *****/
  82. int closefile(int handle,char *name)
  83. {
  84.     int res;
  85.  
  86.     if ((res = close(handle)) < 0)
  87.     prt_error("close",name);
  88.     return(res);
  89. }
  90.  
  91. /*****
  92.  
  93.     The function 're_org()' is used to remove orphaned hash pointers
  94.     from the IDX files.  Pointers in these files become orphaned as
  95.     delete records in the CTL file are re-used.  Thus it is needed to
  96.     re-organize the HASH TABLE from time-to-time (depends on how many
  97.     deleted records you have accumulated).  The fcb must be open prior
  98.     to entry.
  99.  
  100. *****/
  101. #ifdef HOST
  102. int re_org(DRAM far *fcb,int func)
  103. {
  104.     LONGINTEGER odxfpos, odxflen, ndxfpos;
  105.     INTEGER j;
  106.     int ndx_handle;
  107.     char temp1[80], temp2[80];
  108.     STRING odx_file[80];
  109.     STRING ndx_file[80];
  110.     IDX_RECORD ndx_rec;
  111.     IDX_RECORD far *xp;
  112.  
  113.     lock_dat(fcb,0,fcb->dat_recsize);
  114.     xp = fcb->idx_rec;
  115.     dram_slasher(fcb);
  116.     FARstrcpy((char far *)temp1,(char far *)fcb->dram_path);
  117.     FARstrcpy((char far *)temp2,(char far *)fcb->dram_fname);
  118.     sprintf(odx_file,"%s%s.IDX",temp1,temp2);
  119.     sprintf(ndx_file,"%s%s.NDX",temp1,temp2);
  120.     odxflen = filelength(fcb->idx_handle);
  121.     hash_max = ((INTEGER) (odxflen / ((LONGINTEGER) sizeof(IDX_RECORD)))) - 1;
  122.     ndx_handle = createfile(ndx_file);
  123.     FARmemset((char far *) xp,0,sizeof(IDX_RECORD));
  124.     for (j = 0; j <= hash_max; j++)
  125.     writefile(ndx_handle,(STRING far *) xp,sizeof(IDX_RECORD));
  126.     closefile(ndx_handle,ndx_file);
  127.     ndx_handle = _open(ndx_file,o_mode(O_RDWR));
  128.     seekfile(fcb->idx_handle,0L,SEEK_SET);
  129.     readfile(fcb->idx_handle,(STRING far *) xp,sizeof(IDX_RECORD));
  130.     seekfile(ndx_handle,0L,SEEK_SET);
  131.     writefile(ndx_handle,(STRING far *) xp,sizeof(IDX_RECORD));
  132.     for (j = 1; j <= hash_max; j++) {
  133.     odxfpos = (((LONGINTEGER) j) * ((LONGINTEGER) sizeof(IDX_RECORD)));
  134.     seekfile(fcb->idx_handle,odxfpos,SEEK_SET);
  135.     readfile(fcb->idx_handle,(STRING far *) xp,sizeof(IDX_RECORD));
  136.     if (! xp->hash_re_used) {
  137.         hash_crc = xp->hash_code;
  138.         hash_value = hash_crc % hash_max;
  139.         ndxfpos = ((((LONGINTEGER) hash_value) + 1L) * ((LONGINTEGER) sizeof(IDX_RECORD)));
  140.         seekfile(ndx_handle,ndxfpos,SEEK_SET);
  141.         readfile(ndx_handle,(STRING far *) &ndx_rec,sizeof(IDX_RECORD));
  142.         if (ndx_rec.hash_ptr) {
  143.         delta_hash = delta_calc();
  144.         hash_first = hash_value;
  145.         do {
  146.             next_hash();
  147.             ndxfpos = ((((LONGINTEGER) hash_value) + 1L) * ((LONGINTEGER) sizeof(IDX_RECORD)));
  148.             seekfile(ndx_handle,ndxfpos,SEEK_SET);
  149.             readfile(ndx_handle,(STRING far *) &ndx_rec,sizeof(IDX_RECORD));
  150.         } while ((ndx_rec.hash_ptr) && (ndxfpos != hash_first));
  151.         }
  152.         seekfile(ndx_handle,ndxfpos,SEEK_SET);
  153.         writefile(ndx_handle,(STRING far *) xp,sizeof(IDX_RECORD));
  154.     }
  155.     }
  156.     close(ndx_handle);
  157.     close(fcb->idx_handle);
  158.     backup_idx(fcb);
  159.     fcb->idx_handle = _open(odx_file,o_mode(O_RDWR));
  160.     unlock_dat(fcb);
  161.     return(0);
  162. }
  163. #endif
  164.