
/* Program to unfix the "P_PlayerInSpecialSector" error in E3M9. */

#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#define BUG_OFFSET	0x0026F90E

main()
{
	int fd;
	char byte;

	if ( (fd=open("doom.wad", O_RDWR, 0)) < 0 ) {
		perror("doom.wad");
		exit(1);
	}
	lseek(fd, BUG_OFFSET, SEEK_SET);
	if ( read(fd, &byte, 1) != 1 ) {
		perror("read()");
		exit(1);
	}
	if ( byte == 0x06 ) {
		printf("This wadfile has not been patched.\n");
		exit(0);
	} else if ( byte == 0x00 ) {
		lseek(fd, BUG_OFFSET, SEEK_SET);
		byte = 0x06;
		if ( write(fd, &byte, 1) != 1 ) {
			perror("write()");
			exit(1);
		}
		printf("Patched wadfile, creating sector error in E3M9.\n");
		exit(0);
	}
	/* Wierd magic number at offset */
	printf("Wierd magic number (0x%X) at offset 0x%X\n", byte, BUG_OFFSET);
	exit(1);
}
