home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2MISC / CSDPMI3S.ZIP / SRC / CWSDPMI / EHDRFIX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-24  |  1.3 KB  |  58 lines

  1. /* EXE file header fixer, code taken from Morten Welinder */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5.  
  6. int main(int argc, char **argv)
  7. {
  8.   int f;
  9.   unsigned short us, test;
  10.  
  11.   if(argc != 2) {
  12.     printf("Usage: ehdrfix cwsdpmi.exe\n       Updates heap size in exe header\n");
  13.     exit(1);
  14.   }
  15.  
  16.   f = open(argv[1], O_RDWR | O_BINARY);
  17.   if (f < 0) {
  18.     perror(argv[1]);
  19.     exit(1);
  20.   }
  21.  
  22.   lseek(f, 0x0aL, SEEK_SET);
  23.   read(f, &us, sizeof(us));
  24.   read(f, &test, sizeof(us));
  25.   if(test == 0xffff) {        /* Not set yet */
  26.     int add,add2;
  27.     FILE *fh;
  28.   
  29.     fh = fopen("control.c","r");
  30.     if(fh) {
  31.       char buffer[256];
  32.       add = 2048;        /* Default _heaplen (can be trimmed) */
  33.       add2 = 0;
  34.       while(!fscanf(fh,"extern unsigned _stklen = %dU", &add2)) {
  35.         if(!fgets(buffer,sizeof(buffer),fh)) break;
  36.       }
  37.       fclose(fh);
  38.       if(add && add2) {
  39.         add /= 16;            /* Kb to paragraphs */
  40.         add2 /= 16;
  41.       } else {
  42.         printf("Can't find stack size in control.c!\n");
  43.         exit(2);
  44.       }
  45.     } else {
  46.       printf("Can't find control.c!\n");
  47.       exit(3);
  48.     }
  49.     
  50.     us += add2;
  51.     lseek(f, 0x0aL, SEEK_SET);
  52.     write(f, &us, sizeof(us));    /* Update min memory */
  53.     us += add;
  54.     write(f, &us, sizeof(us));    /* Update max memory */
  55.   }
  56.   return close(f);
  57. }
  58.