home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cl5sr386.zip / GO32 / EXE2AOUT.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  1KB  |  72 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/stat.h>
  4. #include <string.h>
  5.  
  6. main(int argc, char **argv)
  7. {
  8.   int i;
  9.   for (i=1; i<argc; i++)
  10.     exe2aout(argv[i]);
  11. }
  12.  
  13.  
  14. exe2aout(char *fname)
  15. {
  16.   unsigned short header[3];
  17.   int ifile;
  18.   int ofile;
  19.   char buf[4096];
  20.   int rbytes;
  21.   ifile = open(fname, O_RDONLY|O_BINARY);
  22.   if (ifile < 0)
  23.   {
  24.     perror(fname);
  25.     return;
  26.   }
  27.   read(ifile, header, sizeof(header));
  28.   if (header[0] == 0x5a4d)
  29.   {
  30.     long header_offset = (long)header[1] + (long)header[2]*512L - 512L;
  31.     lseek(ifile, header_offset, 0);
  32.     header[0] = 0;
  33.     read(ifile, header, sizeof(header));
  34.     if (header[0] != 0x010b)
  35.     {
  36.       fprintf(stderr, "%s does not have an a.out file appended to it\n", fname);
  37.       exit(1);
  38.     }
  39.     lseek(ifile, header_offset, 0);
  40.   }
  41.   else
  42.   {
  43.     fprintf(stderr, "%s is not an .EXE file\n", fname);
  44.     exit(1);
  45.   }
  46.   
  47.   *strrchr(fname, '.') = 0;
  48.   ofile = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
  49.   if (ofile < 0)
  50.   {
  51.     perror(fname);
  52.     return;
  53.   }
  54.   
  55.   while ((rbytes=read(ifile, buf, 4096)) > 0)
  56.   {
  57.     int wb = write(ofile, buf, rbytes);
  58.     if (wb < 0)
  59.     {
  60.       perror(fname);
  61.       break;
  62.     }
  63.     if (wb < rbytes)
  64.     {
  65.       fprintf(stderr, "%s: disk full\n", fname);
  66.       exit(1);
  67.     }
  68.   }
  69.   close(ifile);
  70.   close(ofile);
  71. }
  72.