/*
 * This Program extracts the filenames from SCHICKM.EXE/BLADEM.EXE
 * which are needed for SCHICK.DAT/BLADEM.DAT.
 *
 * That may be useful if other Versions (Floppy, maybe)
 * should be supported, too!
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <error.h>

const unsigned char magic[]="TEMP\\\%s";

int main(int argc, char** argv)
{
	FILE *fd;
	unsigned char* buf;
	size_t filelen, readlen, i=0;
	unsigned long strnr=0;

	if (argc != 2)
		error(1, 0, "Usage %s: SCHICKM.EXE/BLADEM.EXE", argv[0]);

	fd=fopen(argv[1], "ro");
	if (fd == NULL)
		error(1, 0, "Can't open file %s", argv[1]);

	fseek(fd, 0, SEEK_END);
	filelen=ftell(fd);
	fseek(fd, 0, SEEK_SET);

	buf=calloc(filelen, 1);
	if (buf == NULL)
	{
		fclose(fd);
		error(1, ENOMEM, " ");
	}

	readlen=fread(buf, 1, filelen, fd);
	fclose(fd);
	if (readlen != filelen)
	{
		free(buf);
		error(1,0,"Can't read the whole file %s", argv[1]);
	}

	while (i<filelen-strlen(magic) && memcmp(magic, buf+i, strlen(magic)))
		i++;

	if (i>=filelen-strlen(magic))
	{
		free(buf);
		error(1,0,"But I still haven't found what I'm looking for");
	}

	i+=strlen(magic)+1;	/* This is the start of the filename array */

	while (strncmp(".CHR", buf+i, 5))
	{
		printf("%3d: %s\n", strnr++, buf+i);
		i+=strlen(buf+i)+1;
	}

	free(buf);
	return 0;
}





