#include <STDIO.H>
#include <STRING.H>

void main(int argc, char* argv[])
	{
	char filename[256];
	int buf[14];
	int seg, off;     // used for secondary header
	int chance = 0;
	char sign[2];

	if (argc==1)
		{
		printf("\nEnter filename:");
		scanf("%s", filename);
		}
	else
		{
		int i,j;
		i=j=0;
		while (  (filename[i++]=argv[1][j++]) != 0);
		}

	FILE* fp = fopen(filename, "rb");

	if (fp==0)
		{
		printf("Could not open file.");
		return;
		}

	fread(buf, 1, 28, fp);

	fseek(fp, 0, SEEK_END);
	long file_size = ftell(fp);

	long loadimage_size = ((long)buf[2]-1)*512 + buf[1];

	if (buf[12]>=64)	// chance of a NE or PE file
		{
		fseek(fp, 60, 0);
		fread(&off, 1, 2, fp);
		fread(&seg, 1, 2, fp);
		if ((seg*16+off)<file_size)
			{
			chance = 1;
			fseek(fp, (long)seg*16+off, 0);
			fread(sign, 1, 2, fp);
			}
		}

	fclose(fp);

printf("\n\n SHOWHEAD - By Gaurang --> Utility to display the exe header");
printf("\nThe header of file %s is as follows: ", filename);

printf("\n00:Identification Bytes:                     %02X %02X        %c%c", buf[0]%256, buf[0]/256,buf[0]%256, buf[0]/256 );
printf("\n02:Number of bytes in last block:            %04X      %5u", buf[1], buf[1]);
printf("\n04:Size of file in 512-byte blocks:          %04X      %5u", buf[2], buf[2]);
printf("\n06:Number of relocation items:               %04X      %5u", buf[3], buf[3]);
printf("\n08:Size of Header in paras:                  %04X      %5u", buf[4], buf[4]);
printf("\n0A:Min Paras at the end of program:          %04X      %5u", buf[5], buf[5]);
printf("\n0C:Max Paras at the end of program:          %04X      %5u", buf[6], buf[6]);
printf("\n0E:Offset in module of SS in Paras:          %04X      %5u", buf[7], buf[7]);
printf("\n10:To be loaded in Stack Pointer(SP):        %04X      %5u", buf[8], buf[8]);
printf("\n12:CheckSum Value:                           %04X      %5u", buf[9], buf[9]);
printf("\n14:To be loaded in Instruction Pointer(IP):  %04X      %5u", buf[10], buf[10]);
printf("\n16:Offset in module of Code Segment(CS):     %04X      %5u", buf[11], buf[11]);
printf("\n18:Offset of relocation table:               %04X      %5u", buf[12], buf[12]);
printf("\n1A:Overlay number:                           %04X      %5u", buf[13], buf[13]);

char ExeType[25];

if (chance==1)
	{
	printf("\nSignature at %04X:%04X: %c%c", seg, off, sign[0], sign[1]);
	if (sign[0]=='N' && sign[1]=='E')
		strcpy(ExeType, "New Executable");
	else if (sign[0]=='P' && sign[1] == 'E')
		strcpy(ExeType, "Portable Executable");
	else
		strcpy(ExeType, "Unknown Signature");
	}
else
	strcpy(ExeType, "DOS executable");

printf("\nEXE header type: %s", ExeType);
printf("\nLoad Image Size from above 2 entries:  %08lX ( %10lu)", loadimage_size, loadimage_size);
printf("\nSize of File from fseek, ftell:        %08lX ( %10lu)", file_size, file_size);
printf("\nSize of Header in bytes:               %04X ( %5u)", buf[4]*16, buf[4]*16);
printf("\n");

}


