Newer
Older
tdump / tdump.c
@tundra tundra on 19 Jan 2020 2 KB cleanup formatting
/* tdump - Dumps stdin To stdout In Hexadecimal Format
           Copyright (C) 1986-2020, T.A. Daneliuk
*/

#include	<stdio.h>
#include	<fcntl.h>
#include	<ctype.h>
#define		WIDTH	16

/* Function declarations */

void dl(void);
int  prnascii(char *equiv);
void tohex2(int x, char array[]);


/* Program globals */

static	char	lineno[]="000000";
static 	unsigned long line=0;

void main()

{

    int	c,count;
    char	array[4],equiv[WIDTH+1];
    array[2]=' ';
    array[3]='\0';
    equiv[WIDTH]='\0';
    count=WIDTH+1;
    dl();

	while ((c=getc(stdin)) != EOF)
		{
		if (! (--count))
			{
			count=prnascii(equiv);
			dl();
			}
		tohex2(c,array);
		if (isprint(c))
			equiv[WIDTH-count]=c;
		else
			equiv[WIDTH-count]='.';
		fputs(array,stdout);
		}
	while (--count)
		{
		fputs("   ",stdout);
		equiv[WIDTH-count]=' ';
		}
	prnascii(equiv);
}

/************************ Display Current Line No. **************************/

void dl()

{
    tohex2((int) (line & 0xffL),&lineno[4]);
    tohex2((int) ((line >> 8) & 0xffL),&lineno[2]);
    tohex2((int) ((line >> 16) & 0xffl),&lineno[0]);
    fputs(lineno,stdout);
    fputs(" => ",stdout);
}

/*********************** Display ASCII Equivalents **************************/

int	prnascii(equiv)

char	*equiv;

{
    fputs(" | ",stdout);
    fputs(equiv,stdout);
    fputs(" |\n",stdout);
    line=line + 0x10;
    return(WIDTH);
}




/********************* Convert Binary Value To Hex **************************/

/* TOHEX2.C - 	This function is passed an integer value and an array
				location which can hold at least a 2 element string.
				The low 8 bits of the integer are converted to an
				equivalent hexadecimal string which is stored in the
				array at subscripts 0 and 1.
*/


void tohex2(i,array)

int		i;
char	array[];

{

    int		temp;
    static char	conv[]={"0123456789ABCDEF"};

    temp=i;
    temp=temp & 0x000f;		/* Mask all but low nyble */
    array[1]=conv[temp];	/* Use to index into conversion table */
    temp=i;					/* Now do high nyble */
    temp=temp >> 4;
    temp=temp & 0x000f;
    array[0]=conv[temp];

}