Please consider a donation to the Higher Intellect project. See https://preterhuman.net/donate.php or the Donate to Higher Intellect page for more info.

Hxdump.shar

From Higher Intellect Vintage Wiki
Jump to navigation Jump to search
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	Makefile
#	hxdump.c
# This archive created: Fri Mar 19 15:52:12 1999
export PATH; PATH=/bin:$PATH
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
sed 's/^X//' << \SHAR_EOF > 'Makefile'
X
X#following for irix
XCFLAGS = -g
X# Following for linux
X#CFLAGS = -g -D_XOPEN_SOURCE -Wall -ansi -pedantic
X
Xall: hxdump
X
X
Xhxdump: hxdump.o
X	$(CC) hxdump.o -o hxdump
X
Xhxdump.o: hxdump.c
X	$(CC) -c $(CFLAGS) hxdump.c 
X
Xshar:
X	shar -p X Makefile hxdump.c >/d2/public/hxdump.shar
X
Xclean clobber:
X	-rm -f hxdump 
X	-rm -f hxdump.o
SHAR_EOF
fi # end of overwriting check
if test -f 'hxdump.c'
then
	echo shar: will not over-write existing file "'hxdump.c'"
else
sed 's/^X//' << \SHAR_EOF > 'hxdump.c'
X/*
X This is the hex dump format I prefer.
X  David B. Anderson
X  Public Domain software.
X
X*/
X
X#include <stdio.h>
X#include <stdlib.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#ifdef __linux__
X#include <unistd.h>
Xextern char *optarg;
Xextern int optind, opterr, optopt;
X#endif
X
X
X#define OPTSTR  "ds:e:l:"
X
Xstatic int errflag;
X
Xchar *Usage = 
X{
X	"Usage: hxdump [-d] [-s startaddr] [-e endaddr] [-l length] file ...\n"
X	" -s supply a starting offset to use, for example, -s 0x1000\n"
X	" -e supply an end address for the dump.\n"
X	" -l supply a length for the dump\n"
X	" -d display data only, not file offsets"
X	"    (If both -e and -l supplied, take the shortest dump implied)\n"
X	"    (dump length etc are not reset between files)\n"
X	"    (use the usual C number conventions for numeric values)\n"
X};
X
XFILE *fin;
Xstatic void dumpfile(FILE *f,long offset,long endoffset);
X
Xint shownooffsets;
X
Xint
Xmain(int argc, char **argv)
X{
X	long startoffset = 0;
X	long endoffset = 0;
X	long length = 0;
X	long finalendoffset;
X	int c;
X
X	if(argc == 1) {
X		printf("%s",Usage);
X		exit(1);
X	}
X
X	while ((c = getopt(argc, argv, OPTSTR)) != EOF)
X	{
X	        switch(c) {
X		case 'e':
X			  {
X			    char *istr = optarg;
X			    char *ostr = 0;
X			    endoffset = strtoul(istr,&ostr,0);
X			    if(endoffset == 0 && ostr == istr) {
X				printf("Error understanding -e value %s\n",
X					istr);
X				return 1;
X			    }
X			
X			  }
X			  break;
X		case 'd':
X			shownooffsets = 1;
X			break;
X		case 'l':
X			  {
X			    char *istr = optarg;
X			    char *ostr = 0;
X			    length = strtoul(istr,&ostr,0);
X			    if(length == 0 && ostr == istr) {
X				printf("Error understanding -l value %s\n",
X					istr);
X				return 1;
X			    }
X			
X			  }
X			  break;
X		case 's':
X		        {
X			 char *istr = optarg;
X			 char *ostr = 0;
X			 startoffset = strtoul(istr,&ostr,0);
X			 if(startoffset == 0 && ostr == istr) {
X				printf("Error understanding offset %s\n",
X					istr);
X				return 1;
X			 }
X			}         
X			break;
X		case '?':
X			return 1;
X		}
X	}
X	for(; optind < argc;optind++)
X	{
X		struct stat statbuf;
X		int res;
X		if( (fin = fopen(argv[optind],"r")) == NULL)
X		{
X			printf("Open of %s failed!\n",argv[optind]);
X		        ++errflag;
X			continue;
X		}
X		res = fstat(fileno(fin), &statbuf);
X		if(res != 0) {
X		  printf("fstat of %s failed!\n",argv[0]);
X		  fclose(fin);	
X		  ++errflag;
X		  continue;
X		}
X		if(statbuf.st_size <= startoffset) {
X		  printf("Only %lu bytes in the file. "
X			"Starting at %lu (decimal) impossible!\n",
X			(unsigned long)statbuf.st_size,
X			(unsigned long)startoffset);
X		  fclose(fin);
X		  ++errflag;
X		  continue;
X		}
X		res =fseek(fin,startoffset,SEEK_SET);
X		if(res) {
X			printf("fseek to %ld failed!\n",(long)startoffset);
X			fclose(fin);
X		        ++errflag;
X			continue;
X		}
X		finalendoffset = statbuf.st_size;
X		if(endoffset != 0 && endoffset < finalendoffset){
X			finalendoffset = endoffset;
X		}
X		if(length != 0 && (startoffset + length) < finalendoffset) {
X			finalendoffset = startoffset + length;
X		}
X		dumpfile(fin,startoffset,finalendoffset);
X		fclose(fin);
X	}
X	return(0);
X} 
X
X
Xstatic void
Xdumpfile(FILE *f,long offset,long endoffset)
X{
X	char buf[100];
X	int c;
X	int i;
X	int j;
X	char *bp;
X	int stop = 0;
X
X	while(stop == 0 && feof(f) ==0)
X	{
X		if(shownooffsets) {
X		   printf("       ");
X		} else {
X		   printf("%7lx",offset);
X		}
X		bp = buf;
X		*bp = 0;
X		for( i = 0; i < 4 && stop == 0; i++)
X		{
X			printf(" ");
X			for(j = 0; j < 4; j++,offset++)
X			{
X				if(feof(f)) {
X					printf("  ");
X					stop = 1;
X					break;
X				} else if (offset >= endoffset) {
X					printf("  ");
X					stop = 1;
X					break;
X				} else {
X				  c = getc(f);
X				  if(c  != EOF) {
X					printf("%02x",c);
X					if( c < ' ' || c > '~')
X						*bp = '.';
X					else
X						*bp = c;
X					bp++;
X					*bp = 0;
X				  } else {
X					printf("  ");
X				  }
X				}
X			}
X		}
X		printf("  %s\n",buf);
X	}
X}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0