Logo Search packages:      
Sourcecode: vflib3 version File versions  Download package

hd.c

/* 
 * hd.c - a hexadecimal dump 
 * by Hirotsugu Kakugawa
 *
 */
/*
 * Copyright (C) 1996 Hirotsugu Kakugawa. 
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
 */


#include <stdio.h>
#include <stdlib.h>

#define SEPARETER  "  "
int hd(FILE*);


int
main(argc, argv)
     int argc;
     char **argv;
{
  if (argc > 1){
    if (strncmp(argv[1], "-h", 2) == 0){
      printf("hd --- hex dump\n");
      printf("Usage: hd [file ... ]\n");
      exit(0);
    }
    hd(fopen(argv[1], "rb"));
  } else 
    hd(stdin);

  return 0;
}
 
int hd(FILE *fp)
{
  int  c, addr, oaddr, i;
  char cstr[17];

  if (fp == NULL)
    exit(1);

  addr = 0;
  while ((c = getc(fp)) >= 0){
    if (addr % 256 == 0)
      printf("          +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F\n");
    if (addr % 16 == 0)
      printf("%08x: ", addr);

    printf("%02x ", c);
    cstr[addr%16] = ((c <= 0x1f)||(0x7f <= c)) ? '.': (char)c;

    addr++;
    if (addr % 16 == 0){
      printf("%s", SEPARETER);
      for (i = 0; i < 16; i++)
      printf("%c", cstr[i]);
      putchar('\n');
    }
    if (addr % 256 == 0)
      putchar('\n');
  }

  addr = addr % 16;
  if (addr != 0){
    for (oaddr = addr; addr < 16; addr++)
      printf("%s", "   ");
    printf("%s", SEPARETER);
    for (i = 0; i < oaddr; i++)
      printf("%c", cstr[i]);
    putchar('\n');
  }

  return 0;
}

/*EOF*/

Generated by  Doxygen 1.6.0   Back to index