home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / ovl301.zip / MAPOVL.C < prev    next >
Text File  |  1989-02-02  |  5KB  |  143 lines

  1. /* MAPOVL.C
  2.  
  3.     programmed in the small memory model of Turbo C version 2.0
  4.  
  5.     Show file information for overlays appended to a root .EXE file
  6.  
  7. */
  8.  
  9. #include "stdio.h"
  10. #include "dos.h"
  11. #include "string.h"
  12. #include "io.h"
  13. #include "conio.h"
  14. #include "stdlib.h"
  15.  
  16. FILE *fp;
  17.  
  18. char fname[128];
  19. char f_noext_name[125];    /* file name in fname without extension */
  20. unsigned char header[27];
  21. char buff[8192];    /* file buffer for setvbuf */
  22.  
  23. unsigned int filecount=0;
  24. unsigned long filelen;    /* length of .exe file (overlay or root) */
  25. unsigned long len_no_hdr;    /* length of file without .exe header byte */
  26. unsigned int relocation_count;    /* number of relocation table items */
  27. unsigned long relocation_bytes;    /* byte count of relocation table items */
  28. unsigned int header_size;    /* size of .exe header */
  29. unsigned long ovl_largest=0L;    /* size of largest overlay file */
  30. unsigned int which_ovl=0;    /* overlay file number that is largest */
  31.  
  32. void operate(),report();
  33. int get_info();
  34.  
  35. void main(int argc, char *argv[])
  36. {
  37.     char *period_pos;    /* position of period in fname */
  38.     char temp[180];
  39.  
  40.     if(argc<2){    /* file name was not provided on command line */
  41.         cprintf("\n\rInput filename for report\r\n");
  42.         gets(fname);
  43.     }
  44.     else{    /* command link provided file name */
  45.         strcpy(fname,argv[1]);
  46.     }
  47.     strcpy(f_noext_name,fname);
  48.     if((period_pos=strchr(f_noext_name,'.'))==NULL){    /* need to add default .exe file extension */
  49.         strcat(fname,".exe");    /* add the .exe extension */
  50.     }
  51.     else{
  52.         period_pos[0]=0;    /* kill the extension */
  53.     }
  54.     sprintf(temp,"Report on file %s",fname);
  55.     printf("\n\n%s",temp);    /* print header */
  56.     strset(temp,'=');    /* make a separation just as long as the header line */
  57.     printf("\n%s",temp);    /* print separation line */
  58.     operate();
  59. }
  60.  
  61. void operate()
  62. {
  63.     int breakflag=0;
  64.  
  65.     if((fp=fopen(fname,"rb"))==NULL){    /* error opening file */
  66.         printf("\n\007Error opening file %s\n",fname);
  67.         exit(1);
  68.     }
  69.     setvbuf(fp,buff,_IOFBF,8192);    /* speed up i/o */
  70.     while(!feof(fp)){    /* split out .exe files until end of file or invalid .exe information */
  71.         breakflag=get_info();
  72.         if(breakflag)    /* no more files, or error */
  73.             break;
  74.         report();
  75.         filecount++;    /* increment count of overlay files */
  76.     }
  77.     if(ovl_largest>0){    /* only print if there is an overlay file */
  78.         printf("\n\nOverlay file %d always loads into the standard overlay area.",
  79.             which_ovl);
  80.     }
  81.     fclose(fp);
  82.  
  83.     printf("\n\nReport complete: %d overlay file%s\n",filecount-1,filecount!=2?"s":"");
  84. }
  85.  
  86. int get_info()
  87. {
  88.     int signature,ovl_number;
  89.     fread(header,27,1,fp);    /* read in first 27 .exe header bytes */
  90.     fseek(fp,-27L,SEEK_CUR);    /* position back to start of .exe file */
  91.  
  92.     signature=(header[0]=='M' && header[1]=='Z');    /* check for valid MZ .exe signature */
  93.     ovl_number=header[26];    /* get overlay number */
  94.     if(!signature || ovl_number!=filecount){    /* header bytes have invalid values */
  95.         return(1);    /* not a proper .exe file */
  96.     }
  97.     header[0]=header[1]=0;    /* force invalid signature if no more bytes to read */
  98.  
  99.     filelen=header[5];
  100.     filelen*=256L;
  101.     filelen+=header[4];    /* size of file in 512 byte pages */
  102.     filelen*=512L;    /* get length of .exe file to nearest 512 byte page */
  103.  
  104.     header_size=header[9];
  105.     header_size*=256;
  106.     header_size+=header[8];    /* size of header in paragraphs */
  107.     header_size*=16;    /* size of header in bytes */
  108.  
  109.     len_no_hdr=filelen-header_size;    /* size of file without any .exe header bytes (size of code) */
  110.  
  111.     relocation_count=header[7];
  112.     relocation_count*=256;
  113.     relocation_count+=header[6];    /* get number of relocation table items */
  114.  
  115.     relocation_bytes=(long)relocation_count*4L;    /* get byte count of relocation table items */
  116.  
  117.     fseek(fp,filelen,SEEK_CUR);    /* position to read info of next .exe file */
  118.  
  119.     return(0);    /* successful read of information */
  120. }
  121.  
  122. void report()
  123. {
  124.     if(!filecount){    /* root file */
  125.         printf("\n\nRoot file:  File size %6ld (%lx hex) bytes",len_no_hdr,len_no_hdr);
  126.         printf("\n    Relocation table %5d (%x hex) items, %6ld (%lx hex) bytes",
  127.             relocation_count,relocation_count,relocation_bytes,relocation_bytes);
  128.         printf("\n    Unaffected by /ZBn option");
  129.     }
  130.     else{    /* overlay file */
  131.         printf("\n\nOverlay file %2d:  File size %6ld (%lx hex) bytes",filecount,
  132.             len_no_hdr,len_no_hdr);
  133.         printf("\n    Relocation table %5d (%x hex) items, %6ld (%lx hex) bytes",
  134.             relocation_count,relocation_count,relocation_bytes,relocation_bytes);
  135.         printf("\n    /ZBn option requires 'n' of %d for no table reload",
  136.             relocation_count==0?1:1+(int)((relocation_bytes-1)/1024));
  137.         if(len_no_hdr>ovl_largest){    /* new largest overlay file */
  138.             ovl_largest=len_no_hdr;
  139.             which_ovl=filecount;
  140.         }
  141.     }
  142. }
  143.