[comp.sources.misc] v05i057: xdump, hex/char dump of file or shared memory

bengsig@orcenl.UUCP (Bjorn Engsig) (11/26/88)

Posting-number: Volume 5, Issue 57
Submitted-by: "Bjorn Engsig" <bengsig@orcenl.UUCP>
Archive-name: xdump

# This is a shell archive
#
# file: README
#
if test -f README; then echo shar: README exists; else
sed 's/^X//' << EOF_EOF > README
Xxdump is a utility to give hexadecimal dumps of a file or a
Xshared memory segment.
X
Xxdump may also be used to copy a shared memory segment to a file.
X
XThere is no man-page, but a describing comment in the code.
X
XIt will give an output like:
X
X     0  78 64 75 6d 70 20 69 73 20 61 20 75 74 69 6c 69   xdump is a utili
X    10  74 79 20 74 6f 20 67 69 76 65 20 68 65 78 61 64   ty to give hexad
X    20  65 63 69 6d 61 6c 20 64 75 6d 70 73 20 6f 66 20   ecimal dumps of 
X    30  61 20 66 69 6c 65 20 6f 72 20 61 0a 73 68 61 72   a file or a.shar
X    40  65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d 65 6e   ed memory segmen
X
Xwith the dump shown as a hexpart and a charpart.  The charpart shows all
Xnon-printable characters as '.'
X
XIt compiles and runs on System V, without any Makefile, just using the
Xdefault make rules.
X
XYou need to remove the define ONLY7BITS, if you want the charpart to include
Xprintable 8-bit characters
EOF_EOF
if test `wc -c <README` -ne     920; then echo shar: README has bad length
else echo shar: README extracted; fi
fi
# file: xdump.c
#
if test -f xdump.c; then echo shar: xdump.c exists; else
sed 's/^X//' << EOF_EOF > xdump.c
X/* xdump.c - make hex dumps */			char usage[]=
X
X"Usage: %s [-f file] [-m shmid] [-M shmkey] [-b list] [-cxo]\n";  /*
X
X
X-f file		: Dump file
X-m shmid	: Dump shared memory segment, given id
X-M shmkey	: Dump shared memory segment, given key
X-b list		: Dump only bytes within list
X-c		: Don't show char part
X-x		: Don't show hex part
X-o		: Copy input (file or sh. mem. segment)
X		  to stdout. Dump goes to stderr.
X<stdin>		  is taken as input if no -f, -m or -M option is present.
X
Xxdump -oxcM shmkey > file	: These copies a sh. mem. segment
Xxdump -oxcm shmid  > file	: to a file without any printed output
X
Xlist could be -b0x10-0x30,0x50
X	   or -b0-256
X	   or -b0100-0200
Xand is used to constraint the dump to a range of the file or shmem.
X
Xxdump [options] file is equivalent to xdump [options] -f file.
X
XThe algorith used to dump ranges of the input is not very smart.
X
XThis program is copyrighted by Bjorn Engsig.  Permission is hereby granted
Xto copy, redistribute and use the program, provided only handling fees
Xare charged, and provided this copyright notice is included.
X
XBjorn Engsig, 15.11.88
X*/
X#define ONLY7BIT /* remove this for (simple) 8 bit support,
X		    see also Isprint below */
X
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/ipc.h>
X#include <sys/shm.h>
X#include <signal.h>
X#include <ctype.h>
X#include <fcntl.h>
X
X#ifdef ONLY7BIT
X#define Isprint(c) isprint(c)
X#else
X#define Isprint(c) (isprint(c&0x7f))
X#endif ONLY7BIT
X
Xchar optstring[] = "b:f:M:m:cxo";
Xchar input, *name, *list;
Xunsigned char *line;
Xint nochar, nohex, swchar, swhex, pipeit;
Xint infile,byteno,bflag;
Xunsigned char  *address;
Xextern char *optarg;
Xextern int optind, opterr;
X
Xexithandle()
X{ 
X  exit(0);
X}
X
Xopeninput()
X{
Xint shmid;
X  switch (input) {
X    case 'f':
X      if ((infile=open(name,O_RDONLY))== -1) {
X	perror("xdump: Cannot open file");
X	exit(2);
X      }
X      break;
X    case 'M':
X      if ((shmid = shmget( strtol(name, (char **)0, 0), 0)) == -1) {
X	perror("xdump: Cannot get shmid");
X	exit(2);
X      }
X      goto Doshm;
X      break;
X    case 'm':
X      shmid = strtol(name, (char **)0, 0);
X    Doshm:
X      if ((address = (unsigned char *)shmat(shmid,0,SHM_RDONLY))== (unsigned char *) -1) {
X	perror("xdump: Cannot attach to shared memory");
X	exit(2);
X      }
X      signal(SIGBUS,exithandle);
X      signal(SIGSEGV,exithandle);
X      break;
X    case 0: /* stdin */
X      input='f';
X      infile=0;
X      break;
X    }
X}
X
Xgetline()
X{
X  static unsigned char buf[18];
X  switch(input) {
X    case 'f':
X      line=buf;
X      return read(infile,buf,16);
X    break;
X    case 'm':
X    case 'M':
X      line=(address+=16);
X      return 16;
X    break;
X  }
X}
X
Xinrange(bn)
X  int bn;
X{
X  char *end;
X  static int lower= -1,upper= -1;
X  if (bn<lower)
X    return 0;
X  if (bn>=lower && bn<=upper)
X    return 1;
X  if (*list) {
X    lower=strtol(list,&end,0);
X    list=end+1;
X    if (*end==',' || *end==(char) 0) {
X      upper=lower;
X    } else
X    if (*end=='-') {
X      upper=strtol(list,&end,0);
X      list=end+(!! *end);
X    }
X    lower &= 0xfffffff0;
X    upper &= 0xfffffff0;
X    if (bn>=lower && bn<=upper)
X      return 1;
X  } else
X    if (bflag) exit(0); /* ugly exit on end of list */
X  return 0;
X}
X
Xshowline(out,count)
X  FILE *out;
X{
X  unsigned char outline[74];
X  int i,pc;
X  if (nochar && nohex) {
X    pc = line[0]; /* to provoke buserror */
X    return;
X  }
X  if (list && ! inrange(byteno)) {
X    byteno+=count;
X    return;
X  }
X  sprintf(outline,"%6x  ",byteno);
X  if (!nohex) {
X    for (i=0; i<count; i++)
X      sprintf(strchr(outline,0),"%.2x ",line[i]);
X    for (   ; i<16   ; i++)
X      strcat(outline,"   ");
X    }
X  strcat(outline,"  ");
X  if (!nochar) {
X    for (i=0; i<count; i++) {
X      pc = line[i];
X      if (!Isprint(pc))
X	pc = '.';
X      sprintf(strchr(outline,0),"%c",pc);
X    }
X    for (   ; i<16   ; i++)
X      strcat(outline," ");
X    }
X  fprintf(out,"%s\n",outline);
X  byteno+=count;
X}
X
Xmain(ac,av)
X  int ac;
X  char *av[];
X{
X  int opt,error,count;
X  error=0;
X  while ((opt=getopt(ac,av,optstring))!=EOF)
X    switch (opt) {
X    case 'f':
X    case 'm':
X    case 'M':
X      if (input) error=2;
X      input=opt;
X      name=optarg;
X    break;
X    case 'b':
X      if (list) error=2;
X      list=optarg;
X      bflag++;
X    break;
X    case 'o':
X      pipeit=1;
X    break;
X    case 'c':
X      nochar=1;
X    break;
X    case 'x':
X      nohex=1;
X    break;
X    default:
X      error=1;
X  }
X  if (optind<ac) {
X    if (input) error=2;
X    input='f';
X    name=av[optind];
X  }
X  switch (error) {
X    case 2:
X      fprintf(stderr,"%s: illegal option use\n",av[0]);
X    case 1:
X      fprintf(stderr,usage,av[0]);
X      exit(1);
X  }
X  openinput();
X  signal(SIGQUIT,exithandle);
X  while (count=getline()) {
X    if (pipeit) {
X      showline(stderr,count);
X      write(1,line,count);
X    } else
X      showline(stdout,count);
X  }
X}
X
X  
EOF_EOF
if test `wc -c <xdump.c` -ne    4820; then echo shar: xdump.c has bad length
else echo shar: xdump.c extracted; fi
fi