[comp.sources.misc] v06i029: hd- A hex dump utility

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (01/30/89)

Posting-number: Volume 6, Issue 29
Submitted-by: hal@dad.UUCP (Harold A. Miller)
Archive-name: hd

#! /bin/sh
echo shar: extracting hd.c
if test -f 'hd.c' ; then
    echo shar: will not overwrite hd.c
else
    cat > hd.c << '@EOF hd.c'
/* ********************************************************************** */
/*                                   hd.c                                 */
/*                         A CP/M dump-like utility                       */
/*                   Written by: Mike Ewan and Hal Miller                 */
/* tektronix!tekgen!nesa!raven!mike       uw-beaver!tikal!dad!hal         */
/* Use hereby authorized by anyone for any reason, except sale.           */
/* ********************************************************************** */

#include <stdio.h>

main(argc,argv)
int        argc ;
char    *argv[] ;
{
    FILE    *fopen(), *fp ;
    int     c     ;                       /* work character             */
    int     llgt  ;                       /* for efficiency in loops    */
    int     x     ;                       /* ascii column index         */
    int     z     ;                       /* loop counter               */
    long    addr  ;                       /* starting address for line  */
    long    start ;                       /* optional begin file offset */
    char    *flname ;                     /* pointer for file open      */
	char    str[8]  ;                     /* work buffer for conversion */
	char    asc[17] ;                     /* contents of ascii column   */

    if (argc == 3)
        {                     /* given filename and starting offset     */
        flname = argv[1] ;
        sscanf(argv[2], "%x", &start) ;
        }
    else
        {
        if (argc == 2)
            {                 /* no offset given, use beginning of file */
            flname = argv[1] ;
            start = 0L ;
            }
        else
            {
            printf("usage: %s filename [offset]\n", argv[0]) ;
            exit(1) ;
            }
        }
    if ((fp = fopen(flname,"r")) == NULL)
        {
        printf("%s: unable to open %s\n", argv[0], flname) ;
        exit(1) ;
        }

    x = 0 ;
    addr = start ;

    sprintf(str, "%08x", addr) ;          /* set up and print start address */
    llgt = strlen(str) ;                  /* for the first row              */
    for (z=0; z<llgt; z++)
        if (str[z] >= 'a' && str[z] <= 'f')
            str[z] &= 0137 ;
    printf("%8s:  ", str) ;

    if (start != 0)                       /* get to offset if need be       */
        fseek(fp, start, 0) ;

    while ((c = getc(fp)) != EOF)
        {                                 /* read and process entire file   */
        sprintf(str,"%02x",c) ;           /* set up and print the hex stuff */
        if (str[0] >= 'a' && str[0] <= 'f')
            str[0] &= 0137 ;
        if (str[1] >= 'a' && str[1] <= 'f')
            str[1] &= 0137 ;
        printf("%s ",str) ;

        if ((c >= ' ') && (c <= '~'))     /* set up the ascii stuff         */
            asc[x] = c ;
        else
            asc[x] = '.' ;
        x++ ;

        if (x >= 16)
            {                             /* got enough to print a line     */
            putchar(32) ;
            for (z=0; z<=15; z++)
                {
                putchar(asc[z]) ;
                asc[z] = '\0' ;
                }
            putchar(10) ;
            x = 0 ;

            addr += 16L ;                 /* set up for next line's address */
            sprintf(str, "%08x", addr) ;
            llgt = strlen(str) ;
            for (z=0; z<=llgt; z++)
                if (str[z] >= 'a' && str[z] <= 'f')
                    str[z] &= 0137 ;
            printf("%8s:  ",str) ;
            }
        }                                 /* end of file                    */

    llgt = 15 - x ;                       /* space fill rest of last line   */
    for (z=0; z<=llgt; z++)
        printf("   ") ;
    putchar(32) ;
    for (z=0; z<=15; z++)
        putchar(asc[z]) ;
    putchar(10) ;
    fclose(fp) ;
    exit(0) ;
}
@EOF hd.c
if test 3871 -ne "`wc -c < hd.c`"; then
    echo shar: checksum error - number of characters should be 3871
fi
fi
chmod 666 hd.c