ftp.nice.ch/pub/next/developer/languages/c/djgpp-NS.s.tar.gz#/djgpp/go32/exe2coff.c

This is exe2coff.c in view mode; [Download] [Up]

/* This is file EXE2AOUT.C */
/*
** Copyright (C) 1993 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
**
** This file is distributed under the terms listed in the document
** "copying.dj", available from DJ Delorie at the address above.
** A copy of "copying.dj" should accompany this file; if not, a copy
** should be available from where this file was obtained.  This file
** may not be distributed without a verbatim copy of "copying.dj".
**
** This file is distributed WITHOUT ANY WARRANTY; without even the implied
** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <io.h>


void exe2aout(char *fname)
{
  unsigned short header[3];
  int ifile;
  int ofile;
  char buf[4096];
  int rbytes;
  ifile = open(fname, O_RDONLY|O_BINARY);
  if (ifile < 0)
  {
    perror(fname);
    return;
  }
  read(ifile, header, sizeof(header));
  if (header[0] == 0x5a4d)
  {
    long header_offset = (long)header[2]*512L;
    if (header[1])
      header_offset += (long)header[1] - 512L;
    lseek(ifile, header_offset, 0);
    header[0] = 0;
    read(ifile, header, sizeof(header));
    if ((header[0] != 0x010b) && (header[0] != 0x014c))
    {
      fprintf(stderr, "%s does not have an a.out file appended to it\n", fname);
      exit(1);
    }
    lseek(ifile, header_offset, 0);
  }
  else
  {
    fprintf(stderr, "%s is not an .EXE file\n", fname);
    exit(1);
  }
  
  *strrchr(fname, '.') = 0;
  ofile = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
  if (ofile < 0)
  {
    perror(fname);
    return;
  }
  
  while ((rbytes=read(ifile, buf, 4096)) > 0)
  {
    int wb = write(ofile, buf, rbytes);
    if (wb < 0)
    {
      perror(fname);
      break;
    }
    if (wb < rbytes)
    {
      fprintf(stderr, "%s: disk full\n", fname);
      exit(1);
    }
  }
  close(ifile);
  close(ofile);
}

int main(int argc, char **argv)
{
  int i;
  for (i=1; i<argc; i++)
    exe2aout(argv[i]);
  return 0;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.