/* ==================================== *\
 *	File      : \BinDump.c               *
 *	Påbegyndt : 05.03.02 Henning Karlby  *
 *	Færdig    :                          *
 *	Ændret    :                          *
\* ==================================== */

/* ---- INCLUDES ------- */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LENGTH 10      // længden af den udskrevne linie
#define TRUE    1
#define FALSE   0

int main( int argc, char *argv[] )
	{
	FILE *fileptr;              // pointer til en fil
	int ch;
	int j, not_eof;
	unsigned char string[LENGTH+1];  // buffer for tegn

	if(argc != 2)               // kontrol af antal argumenter
		{ printf("Formattet er: C>bindump filenavn"); exit(1); }
	if( (fileptr=fopen(argv[1],"rb"))==NULL )   // binary læsning
		{ printf("Kan ikke åbne filen %s", argv[1]); exit(1); }
	not_eof = TRUE;                    // not EOF flag
	do
		{
		for(j=0; j<LENGTH; j++)         // tegn i en linie
			{
	 if( (ch=getc(fileptr)) == EOF )  // læs tegn
		 not_eof = FALSE;          // nulstil flag for EOF
	 printf("%3x ", ch);          // udskriv ASCII kode
			if (ch > 31)
		 *(string+j) = ch;         // gem udskrevne tegn
	 else                         // brug punktum for
		 *(string+j) = '.';        // tegn som ikke kan udskrives
			}
		*(string+j) = '\0';             // slut på en streng
		printf("   %s\n", string);      // udskriv string
		}
	while(not_eof == TRUE);            // stop hvis EOF
	fclose(fileptr);                   // luk filen
	return(0);
	}
