
/* This module lists the directory of a wadfile to stdout. */

#include	<stdio.h>
#include	<ctype.h>
#include	"doomdefs.h"
#include	"directory.h"

#define EM_END	"BLOCKMAP"	/* The last entry in a level resource list */

int waddir(int fd)
{
	dir_entry entry;
	char dosdir[32]={'\0'};
	char entryname[32];

	if ( set_directory(fd) < 0 ) {
		fprintf(stderr, "Can't find the directory entries.\n");
		return(-1);
	}

	/* Print out the WADDIR.EXE header */
	printf("# DOOM WAD FILE DIRECTORY LISTING\n");
	printf("# LINES STARTING WITH # ARE IGNORED\n");
	printf("# EACH LINE CONTAINS DOSFILE WADFILE SIZE\n");
	printf("# THIS FILE CAN BE USED TO UNPACK/PACK WAD FILES\n");
	printf("# DO NOT CHANGE THE ORDER OF THESE LINES!!!!\n");
	printf("# HOWEVER, DELETION IS OKAY\n");

	/* Dump the directory listing */
	while ( read(fd, (char *)&entry, ENTRY_SIZE) == ENTRY_SIZE ) {
		/* Null terminate the name */
		entry.name[8]='\0';
		
		sprintf(entryname, "%s%c", 
			entry.name, ((entry.res_len == 0) ? '\\' : ' '));

		/* Print it out */
		printf("%s%-20s %-20s %d\n", dosdir, entryname, entryname,
								entry.res_len);

		/* See if we are at the start of an E?M? entry */
		if ( entryname[0] == 'E' && isdigit(entryname[1]) &&
		     entryname[2] == 'M' && isdigit(entryname[1]) )
			strcpy(dosdir, entryname);

		/* See if we are at the end of an E?M? entry */
		if ( strcasecmp(EM_END, entry.name) == 0 )
			dosdir[0]='\0';

		if ( strcasecmp(WADDIR_END, entry.name) == 0 )
			break;
	}
	return(0);
}

