home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / ovl301.zip / SPLIT.C < prev    next >
Text File  |  1989-01-12  |  3KB  |  115 lines

  1. /* SPLIT.C
  2.  
  3.     programmed in the small memory model of Turbo C version 2.0
  4.  
  5.     splits root file appended overlay files into separate files with
  6.     the extension of .001, .002, etc.
  7.  
  8. */
  9.  
  10. #include "stdio.h"
  11. #include "dos.h"
  12. #include "string.h"
  13. #include "io.h"
  14.  
  15. FILE *fpin,*fpout;
  16.  
  17. char fname[128];
  18. char f_noext_name[125];    /* file name in fname without extension */
  19. unsigned char header[27];
  20. char inbuff[4096];    /* file inbuffer for setvbuf */
  21. char outbuff[4096];    /* file outbuffer for setvbuf */
  22. char transbuff[512];    /* file bytes transfer buffer */
  23.  
  24. int filecount=0;
  25.  
  26. void operate();
  27. int split(char *workname);
  28.  
  29. void main(int argc, char *argv[])
  30. {
  31.     char *period_pos;    /* position of period in fname */
  32.  
  33.     if(argc<2){    /* file name was not provided on command line */
  34.         cprintf("\n\rInput overlay file to split\r\n");
  35.         gets(fname);
  36.     }
  37.     else{    /* command link provided file name */
  38.         strcpy(fname,argv[1]);
  39.     }
  40.     strcpy(f_noext_name,fname);
  41.     if((period_pos=strchr(f_noext_name,'.'))==NULL){    /* need to add default .exe file extension */
  42.         strcat(fname,".exe");    /* add the .exe extension */
  43.     }
  44.     else{
  45.         period_pos[0]=0;    /* kill the extension */
  46.     }
  47.     operate();
  48. }
  49.  
  50. void operate()
  51. {
  52.     char workname[128];
  53.     char file_ext[5];    /* file extension; .$$$, .001, .002, etc. */
  54.  
  55.     if((fpin=fopen(fname,"rb"))==NULL){    /* error opening file */
  56.         printf("\n\007Error opening file %s",fname);
  57.         exit(1);
  58.     }
  59.     setvbuf(fpin,inbuff,_IOFBF,4096);    /* speed up i/o */
  60.     while(!feof(fpin)){    /* split out .exe files until end of file or invalid .EXE information */
  61.         strcpy(workname,f_noext_name);
  62.         /* get appropriate extension for work file */
  63.         sprintf(file_ext,!filecount?".$$$":".%03d",filecount);
  64.         strcat(workname,file_ext);    /* add extension to the work file name */
  65.         if(split(workname)){
  66.             break;    /* all done splitting out files */
  67.         }
  68.         filecount++;    /* increment count of files split */
  69.     }
  70.     fclose(fpin);
  71.     /* no errors, kill old file, rename temporary .$$$ file to old name */
  72.     strcpy(workname,f_noext_name);
  73.     strcat(workname,".$$$");
  74.     unlink(fname);
  75.     rename(workname,fname);
  76.     printf("\nSplit complete:  %d overlay file%s",filecount-1,filecount!=2?"s":"");
  77. }
  78.  
  79. int split(char *workname)
  80. {
  81.     int signature,ovl_number;
  82.     unsigned long loop;
  83.     unsigned long filelen;    /* length of .exe file (overlay or root) */
  84.  
  85.     fread(header,27,1,fpin);    /* read in first 27 .exe header bytes */
  86.     fseek(fpin,-27L,SEEK_CUR);    /* position back to start of .exe file */
  87.  
  88.     signature=(header[0]=='M' && header[1]=='Z');    /* check for valid MZ .exe signature */
  89.     ovl_number=header[26];    /* get overlay number */
  90.     if(!signature || ovl_number!=filecount){    /* header bytes have invalid values */
  91.         return(1);    /* not a proper .EXE file to split */
  92.     }
  93.  
  94.     if((fpout=fopen(workname,"wb"))==NULL){    /* error opening file */
  95.         printf("\n\007Error opening file %s",workname);
  96.         exit(1);
  97.     }
  98.     setvbuf(fpout,outbuff,_IOFBF,4096);    /* speed up i/o */
  99.  
  100.     filelen=header[5];
  101.     filelen*=256L;
  102.     filelen+=header[4];
  103.     filelen*=512L;    /* get length of .exe file to nearest 512 byte page */
  104.     for(loop=0L;loop<=filelen-512L;loop+=512L){    /* transfer in 512 byte chunks */
  105.         fread(transbuff,1,512,fpin);
  106.         fwrite(transbuff,1,512,fpout);
  107.     }
  108.     /* transfer leftover bytes */
  109.     fread(transbuff,1,filelen%512,fpin);
  110.     fwrite(transbuff,1,filelen%512,fpout);
  111.  
  112.     fclose(fpout);
  113.     return(0);    /* successful file split */
  114. }
  115.