Hex display program V1.0

From: Dan Mercer (dmercer_at_mn.rr.com)
Date: 05/27/04


Date: Thu, 27 May 2004 17:19:59 GMT

For anyone that's interested:

/***************************************************************
 * This work is provided free As Is to anyone provided they
 * not claim the work as their own or seek in any way to
 * prevent others from distributing it.
 *
 * Copyright (C) 1992-2004, by Dan Mercer
 *
 * Program Name : $Source: /home/dam/glossary/RCS/c,v $
 * Version : $Revision: 1.0 $
 * Date Created : Tue May 25 20:21:48 2004
 * Author : Dan A. Mercer
 * Last Modified : $Date: 99/06/28 10:06:25 $
 * Modified By : $Author: dam $
 *
 * Description : list files in hexadecimal
 ***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <malloc.h>
#include <errno.h>
#include <ctype.h>

#define TEXTONLY 1
#define HEXONLY 2
char *argname;
char *version = "Hexd version 1.0\nCopyright (C) 1992-2004, by Dan Mercer";

void help(char *object, char *msg)
   {
   char *help_msg;
   char *help_object;
   char *t;
   int l;

   t = argname;
   l = strlen(t);
   help_msg = (!msg || !strlen(msg)) ?
                 "dump file contents as hexadecimal" : msg;
   help_object = (!object || !strlen(object)) ?
                 t : object;

   fprintf(stderr,
         "%s: %s\n"
         "%s -h -t -v -x [file]\n"
         "Options:\n"
         " -h this message.\n"
         " -t display text only.\n"
         " -v display version info.\n"
         " -x display hex only.\n"
         " file file to open, else stdin\n"
         "If no file is named, stdin is used and output\n"
         "is to stdout. If a file is named, and stdin is\n"
         "a terminal, output is to file.hex\n",
      help_object, help_msg, t,l," ");

   exit(0);
   }

int main (int argc, char **argv)

{
/*
 *start of main procedure
 */
extern char *optarg;
extern int optind;
extern int optopt;
int ostyle = 0;
int chrs2rd = 16;
int chrsread;
int opt;
int i;
unsigned int filepos;
char *ifile;
char *ofile;
unsigned char *ibuf;
char *obuf;

argname = strrchr(argv[0],'/');
argname = (argname) ? argname + 1 : argv[0];

while ((opt = getopt(argc, argv, ":htvx")) != EOF)
   {
   switch (opt)
      {
      case 'h' :
         help("","");
         break;
      case 't' :
         ostyle = TEXTONLY;
         chrs2rd = 80;
         break;
      case 'v' :
         fputs(version,stderr);
         exit(0);
         break;
      case 'x' :
         ostyle = HEXONLY;
         chrs2rd = 40;
         break;
      case ':' :
         fprintf(stderr, "Option -%c requires an argument\n",optopt);
         exit(1);
         break;
      case '?' :
         fprintf(stderr, "Unrecognized option: -%c\n",optopt);
         exit(1);
         break;
      }
   }

if (argc > (optind + 1))
   help("", "Too many parameters");

if (argc > optind)
   {
   // open input
   ifile = argv[optind];
   if (NULL == freopen(ifile,"r",stdin))
      help(ifile, strerror(errno));
   if (isatty(fileno(stdout)))
      {
      // open output
      if (NULL == (ofile = malloc(strlen(ifile) + 5)))
         help("malloc", strerror(errno));
      sprintf(ofile,"%s.hex",ifile);
      if (NULL == freopen(ofile,"w",stdout))
         help(ofile, strerror(errno));
      }
   }

if (NULL == (ibuf = malloc(chrs2rd + 1)))
   help("malloc", strerror(errno));

filepos = 0;
while ((chrsread = fread(ibuf, 1, chrs2rd, stdin)) > 0)
   {
   switch (ostyle)
      {
      case HEXONLY :
         for (i=0;i<chrsread;i++)
            printf ("%.2X", ibuf[i]);
         break;
      case TEXTONLY :
         for (i=0;i<chrsread;i++)
            printf ("%c", isprint(ibuf[i]) ? ibuf[i] : '.');
         break;
      default :
         printf("%8.8X ", filepos);
         for (i=0;i<chrs2rd;i++)
            {
            if (!(i%4))
               putchar(' ');
            if (i < chrsread)
               printf ("%.2X", ibuf[i]);
            else
               printf (" ");
            }
         printf(" ");
         for (i=0;i<chrsread;i++)
            printf ("%c", isprint(ibuf[i]) ? ibuf[i] : '.');
         break;
      }
   putchar('\n');
   filepos += chrsread;
   }

exit(0);
}
-------------------------------
Dan Mercer
dmercer@mn.rr.com



Relevant Pages