src/hd.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- main
- hd
1 /*
2 * hd.c - a hexadecimal dump
3 * by Hirotsugu Kakugawa
4 *
5 */
6 /*
7 * Copyright (C) 1996 Hirotsugu Kakugawa.
8 * All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #define SEPARETER " "
30 int hd(FILE*);
31
32
33 int
34 main(argc, argv)
/* [<][>][^][v][top][bottom][index][help] */
35 int argc;
36 char **argv;
37 {
38 if (argc > 1){
39 if (strncmp(argv[1], "-h", 2) == 0){
40 printf("hd --- hex dump\n");
41 printf("Usage: hd [file ... ]\n");
42 exit(0);
43 }
44 hd(fopen(argv[1], "rb"));
45 } else
46 hd(stdin);
47
48 return 0;
49 }
50
51 int hd(FILE *fp)
/* [<][>][^][v][top][bottom][index][help] */
52 {
53 int c, addr, oaddr, i;
54 char cstr[17];
55
56 if (fp == NULL)
57 exit(1);
58
59 addr = 0;
60 while ((c = getc(fp)) >= 0){
61 if (addr % 256 == 0)
62 printf(" +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F\n");
63 if (addr % 16 == 0)
64 printf("%08x: ", addr);
65
66 printf("%02x ", c);
67 cstr[addr%16] = ((c <= 0x1f)||(0x7f <= c)) ? '.': (char)c;
68
69 addr++;
70 if (addr % 16 == 0){
71 printf("%s", SEPARETER);
72 for (i = 0; i < 16; i++)
73 printf("%c", cstr[i]);
74 putchar('\n');
75 }
76 if (addr % 256 == 0)
77 putchar('\n');
78 }
79
80 addr = addr % 16;
81 if (addr != 0){
82 for (oaddr = addr; addr < 16; addr++)
83 printf("%s", " ");
84 printf("%s", SEPARETER);
85 for (i = 0; i < oaddr; i++)
86 printf("%c", cstr[i]);
87 putchar('\n');
88 }
89
90 return 0;
91 }
92
93 /*EOF*/