32cb1a5dc20f8cc39ac4e3f64c36cd2e92962348
[hexdump.git] / hexdump.c
1 /* depend: */
2 /* cflags: */
3 /* linker: debug.o */
4
5 #include <assert.h>
6 #include <getopt.h>
7 #include <malloc.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "debug.h"
13
14 /* macros */
15
16 #define CEIL(x, y) (((x) + (y) - 1) / (y))
17 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
18 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
19
20 //#define BUFFERSIZE 4096
21 #define BUFFERSIZE 256
22 #define NBCOLS 8
23
24 /* gobal variables */
25
26 char *progname = NULL;
27
28 /* help function */
29
30 void usage (int ret)
31 {
32 FILE *fd = ret ? stderr : stdout;
33 fprintf (fd, "usage: %s [-i file] [-h] [-n nbcols] [-o file] [-v]\n", progname);
34 fprintf (fd, " -i : input file\n");
35 fprintf (fd, " -h : help message\n");
36 fprintf (fd, " -n : number of columns\n");
37 fprintf (fd, " -o : output file\n");
38 fprintf (fd, " -v : verbose level (%d)\n", verbose);
39
40 exit (ret);
41 }
42
43 /* get number of digits */
44
45 int getnbdigits (long int l) {
46 int n = 0;
47 while (l) {
48 n += 2;
49 l /= 256;
50 }
51 return n;
52 }
53
54 /* print a line */
55
56 void printline (char *buffer, int nbcols, int nb, int addr, int nbdigits) {
57 int i;
58
59 printf ("0x%0*x:", nbdigits, addr);
60 for (i = 0; i < nb; i++) {
61 printf (" %02x", buffer[i]);
62 }
63 for (i = nb; i < nbcols; i++) {
64 printf (" ");
65 }
66 printf (" ");
67 for (i = 0; i < nb; i++) {
68 char c = buffer[i];
69 printf ("%c", (c > 31) && (c < 127) ? c : '.');
70 }
71 printf ("\n");
72 }
73
74 /* indent function */
75
76 int hexdump (FILE *fin, int nbcols) {
77 char buffer[BUFFERSIZE] = {0};
78 int i;
79
80 char *pt = buffer;
81
82 /* get file size */
83 int nbdigits = 0;
84 if (fin != stdin) {
85 fseek (fin, 0 , SEEK_END);
86 long int filesize = ftell (fin);
87 fseek (fin, 0 , SEEK_SET);
88 nbdigits = getnbdigits (filesize);
89 } else {
90 nbdigits = 6;
91 }
92
93 int addr = 0;
94 int nb = 0;
95 while (!feof (fin)) {
96 nb += fread (pt, 1, BUFFERSIZE - (pt - buffer), fin);
97 pt = buffer;
98
99 /* print line */
100 while ((nb - (int)(pt - buffer)) / nbcols > 0) {
101 printline (pt, nbcols, nbcols, addr, nbdigits);
102 pt += nbcols;
103 addr += nbcols;
104 }
105
106 /* copy end buffer */
107 nb -= pt - buffer;
108 for (i = 0; i < nb; i++) {
109 buffer[i] = pt[i];
110 }
111 pt = buffer + nb;
112 }
113
114 /* last line */
115 if (nb > 0) {
116 printline (buffer, nbcols, nb, addr, nbdigits);
117 }
118
119 return 0;
120 }
121
122 /* main function */
123
124 int main (int argc, char *argv[])
125 {
126 char *input = NULL;
127 char *output = NULL;
128 int nbcols = NBCOLS;
129
130 /* get basename */
131 char *pt = progname = argv[0];
132 while (*pt) {
133 if ((*pt == '/') || (*pt == '\\')) {
134 progname = pt + 1;
135 }
136 pt++;
137 }
138
139 int c;
140 while ((c = getopt(argc, argv, "i:hn:o:v:")) != EOF) {
141 switch (c) {
142 case 'i':
143 input = optarg;
144 break;
145 case 'n':
146 nbcols = atoi (optarg);
147 break;
148 case 'o':
149 output = optarg;
150 break;
151 case 'v':
152 verbose = atoi (optarg);
153 break;
154 case 'h':
155 default:
156 usage (c != 'h');
157 }
158 }
159 if (argc - optind != 0) {
160 fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]);
161 usage (1);
162 }
163
164 /* check input */
165 FILE *fin = NULL;
166 if (input) {
167 fin = fopen (input, "rb");
168 if (!fin) {
169 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
170 }
171 } else {
172 fin = stdin;
173 }
174
175 /* check output */
176 FILE *fout = NULL;
177 if (output) {
178 fout = fopen (input, "wb");
179 if (!fout) {
180 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
181 fclose (fin);
182 }
183 } else {
184 fout = stdout;
185 }
186
187 hexdump (fin, nbcols);
188
189 /* close all */
190 fclose (fin);
191 fclose (fout);
192
193 return 0;
194 }
195
196 // test: hexdump.exe -h
197 // test: hexdump.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
198 // test: hexdump.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
199 // test: hexdump.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
200 // test: hexdump.exe -i hexdump.c | grep -q '0x[0-9a-f]*: '
201 // test: hexdump.exe -i hexdump.c -n 3|head -2|tail -1| grep '0x0003: 64 65 70 dep'
202
203 /* vim: set ts=4 sw=4 et: */