[comp.sys.atari.st] strip3.c, for converting mwc 3 to mwc 2 format

hcj@lzaz.ATT.COM (HC Johnson) (02/27/89)

This tool makes a mwc 3 executable look like a DRI or mwc 2 version.
Distomwc cannot disassembel a mwc 3 format.
--This compiles on mwc.

Howard C. Johnson
ATT Bell Labs
att!lzaz!hcj
hcj@lzaz.att.com

==============================cut here========================
/*
 * use to strip mwc 3.0 exec files to allow disassembly
 */
#include <stdio.h>
#include <gemout.h>
#include <stat.h>

main(argc,argv)
char **argv;
{
	long lseek();
	struct stat xstat;
	struct gemohdr header;
	char *name_in,*name_out;
	long filelen;
	long address;
	FILE *fin,*fout;
	int ret;
	long tmp;
	long offset;

	if(argc != 3) {
		fprintf(stderr,"Usage: %s  file_in file_out\nStrip symbol and debugging info from mwc 3.0\n",argv[0]);
		exit(1);
	}
	name_in = argv[1];
	name_out = argv[2];
	if(stat(name_in,&xstat) < 0) {
		perror("");
		fprintf(stderr,"Cannot open %s\n",name_in);
		exit(1);
	}
	filelen = xstat.st_size;
	if((fin = fopen(name_in,"rb")) == 0) {
		perror("");
		fprintf(stderr,"Cannot open %s\n",name_in);
		exit(1);
	}
	ret = fread(&header,sizeof(header),1,fin);
	if(ret == 0) {
		printf("Error reading header\n");
		goto error;
	}
	if((fout = fopen(name_out,"wb")) == 0) {
		perror("");
		fprintf(stderr,"Cannot open %s\n",name_out);
		exit(1);
	}
	offset =header.g_ssize[GO_TEXT] +
		header.g_ssize[GO_DATA] +
		header.g_ssize[GO_SYM] + sizeof(header);
	tmp =  	header.g_ssize[GO_SYM] > 0x30L? 0x30L:header.g_ssize[GO_SYM];

	tmp =  	header.g_ssize[GO_SYM] ;
	
	fprintf(stderr,"File Sizes: Text = %X, Data = %X Sym = %X New Sym = %X\n",
	 	header.g_ssize[GO_TEXT],
 		header.g_ssize[GO_DATA],
 		header.g_ssize[GO_SYM],
		header.g_ssize[GO_SYM]-tmp);
	if(filelen - offset < 0) {
		fprintf(stderr,"size error: filelen = %D, size = %D\n",filelen,offset);
		exit(1);
	}
	header.g_ssize[GO_SYM] -= tmp;
/*
	if(header.g_ssize[GO_SYM]) {
		header.g_relflag = 1;
	}
*/
	ret = fwrite(&header,sizeof(header),1,fout);
	if(ret == 0) {
		printf("Error writing header\n");
		goto error;
	}
	copy(fin,fout,header.g_ssize[GO_TEXT]);
	copy(fin,fout,header.g_ssize[GO_DATA]);
	if(tmp)
		fseek(fin,(long)tmp,1); /* skip over 0x30 bytes */
	copy(fin,fout,header.g_ssize[GO_SYM]); /* copy balance of symbols */
	address = filelen - offset;  /* net relo size */
	copy(fin,fout,address);
	fclose(fin);
	fclose(fout);
	exit(0);


error:
	perror("");
	printf("Error in File %s\n",name_in);
	fprintf(stderr,"Error in File %s\n",name_in);
		exit(1);
}
copy(fin,fout,lsize)
FILE *fin,*fout;
long lsize;
{
	char *space;
	short size;
	int ret;

fprintf(stderr,"Copy %D\n",lsize);
	if(!lsize)
		return;
	space = malloc(lsize>0xfffeL?0xfffe:(short)lsize);
	if(!space) {
		printf("Cannot allocate %D bytes\n",size);
		exit(1);
	}
	while(lsize) {
		size = lsize>0xfffeL?0xfffe:(short)lsize;
		lsize -= size;
		ret = fread(space,size,1,fin);
		if(ret == 0) {
			perror("");
			printf("Error reading input file\n");
			exit(1);
		}
		ret = fwrite(space,size,1,fout);
		if(ret == 0) {
			perror("");
			printf("Error writing output file\n");
			exit(1);
		}
	}
	free(space);
}