fillnops.c 1.68 KB
#include <stdio.h>

void useage(void)
{
	printf("Useage:\n");
	printf("fillnops file word file word extra\n");
	printf("  file1 = first file to search\n");
	printf("  word1 = label whos address to find in file1\n");
	printf("  file2 = second file to search\n");
	printf("  word2 = label whos address to find in file2\n");
	printf("  extra = number of extra nops to print\n");
}

main(int argc, char *argv[])
{
	char *word1, *word2, *filename1, *filename2;
	int c, num1, num2, numnop, extra;
	FILE *fp;

	if (argc != 6) {
		useage();
		exit(1);
	}
	filename1 	= argv[1];
	word1 		= argv[2];
	filename2 	= argv[3];
	word2 		= argv[4];
	sscanf(argv[5],"%i",&extra);


	if (!(fp=fopen(filename1,"r"))) {
		printf("trouble reading %s\n",filename1);
		exit(1);
	}
	if (!findword(fp,word1)) {
		printf("could not find label %s in file %s.\n",word1,filename1);
		exit(1);
	}
	fscanf(fp,"%x",&num1);
	fclose(fp);

	if (!(fp=fopen(filename2,"r"))) {
		printf("trouble reading %s\n",filename2);
		exit(1);
	}
	if (!findword(fp,word2)) {
		printf("could not find label %s in file %s.\n",word2,filename2);
		exit(1);
	}
	fscanf(fp,"%x",&num2);
	fclose(fp);

	numnop = (((num1-num2)/4 + extra) +1)&0xfffe;

	printf(" # This file is automatically generated by fillnops.s\n");
	printf(" # DO NOT EDIT THIS FILE\n");
	printf(" # The purpose of this file is to fill in the space between\n");
	printf(" # the end of ginit.s and the beginning of gsetup.s which\n");
	printf(" # will later be overlayed by gclip.s and gflight.s\n");

	while (numnop--) {
		printf("	nop\n");
	}

	exit(0);
}

int findword(FILE *fp,char *s)
{
	int c=1,i=0;
	while (c!=EOF) {
	c=getc(fp);
		if (c!=s[i++]) i=0;
		if (s[i]=='\0')
			return 1;
	}
	return 0;
}