+/* depend: */
+/* cflags: */
+/* linker: debug.o */
+
+#include <assert.h>
+#include <getopt.h>
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "debug.h"
+
+/* macros */
+
+#define CEIL(x, y) (((x) + (y) - 1) / (y))
+#define MIN(x, y) (((x) < (y)) ? (x) : (y))
+#define MAX(x, y) (((x) > (y)) ? (x) : (y))
+
+//#define BUFFERSIZE 4096
+#define BUFFERSIZE 256
+#define NBCOLS 8
+
+/* gobal variables */
+
+char *progname = NULL;
+
+/* help function */
+
+void usage (int ret)
+{
+ FILE *fd = ret ? stderr : stdout;
+ fprintf (fd, "usage: %s [-i file] [-h] [-n nbcols] [-o file] [-v]\n", progname);
+ fprintf (fd, " -i : input file\n");
+ fprintf (fd, " -h : help message\n");
+ fprintf (fd, " -n : number of columns\n");
+ fprintf (fd, " -o : output file\n");
+ fprintf (fd, " -v : verbose level (%d)\n", verbose);
+
+ exit (ret);
+}
+
+/* get number of digits */
+
+int getnbdigits (long int l) {
+ int n = 0;
+ while (l) {
+ n += 2;
+ l /= 256;
+ }
+ return n;
+}
+
+/* print a line */
+
+void printline (char *buffer, int nbcols, int nb, int addr, int nbdigits) {
+ int i;
+
+ printf ("0x%0*x:", nbdigits, addr);
+ for (i = 0; i < nb; i++) {
+ printf (" %02x", buffer[i]);
+ }
+ for (i = nb; i < nbcols; i++) {
+ printf (" ");
+ }
+ printf (" ");
+ for (i = 0; i < nb; i++) {
+ char c = buffer[i];
+ printf ("%c", (c > 31) && (c < 127) ? c : '.');
+ }
+ printf ("\n");
+}
+
+/* indent function */
+
+int hexdump (FILE *fin, int nbcols) {
+ char buffer[BUFFERSIZE] = {0};
+ int i;
+
+ char *pt = buffer;
+
+ /* get file size */
+ int nbdigits = 0;
+ if (fin != stdin) {
+ fseek (fin, 0 , SEEK_END);
+ long int filesize = ftell (fin);
+ fseek (fin, 0 , SEEK_SET);
+ nbdigits = getnbdigits (filesize);
+ } else {
+ nbdigits = 6;
+ }
+
+ int addr = 0;
+ int nb = 0;
+ while (!feof (fin)) {
+ nb += fread (pt, 1, BUFFERSIZE - (pt - buffer), fin);
+ pt = buffer;
+
+ /* print line */
+ while ((nb - (int)(pt - buffer)) / nbcols > 0) {
+ printline (pt, nbcols, nbcols, addr, nbdigits);
+ pt += nbcols;
+ addr += nbcols;
+ }
+
+ /* copy end buffer */
+ nb -= pt - buffer;
+ for (i = 0; i < nb; i++) {
+ buffer[i] = pt[i];
+ }
+ pt = buffer + nb;
+ }
+
+ /* last line */
+ if (nb > 0) {
+ printline (buffer, nbcols, nb, addr, nbdigits);
+ }
+
+ return 0;
+}
+
+/* main function */
+
+int main (int argc, char *argv[])
+{
+ char *input = NULL;
+ char *output = NULL;
+ int nbcols = NBCOLS;
+
+ /* get basename */
+ char *pt = progname = argv[0];
+ while (*pt) {
+ if ((*pt == '/') || (*pt == '\\')) {
+ progname = pt + 1;
+ }
+ pt++;
+ }
+
+ int c;
+ while ((c = getopt(argc, argv, "i:hn:o:v:")) != EOF) {
+ switch (c) {
+ case 'i':
+ input = optarg;
+ break;
+ case 'n':
+ nbcols = atoi (optarg);
+ break;
+ case 'o':
+ output = optarg;
+ break;
+ case 'v':
+ verbose = atoi (optarg);
+ break;
+ case 'h':
+ default:
+ usage (c != 'h');
+ }
+ }
+ if (argc - optind != 0) {
+ fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]);
+ usage (1);
+ }
+
+ /* check input */
+ FILE *fin = NULL;
+ if (input) {
+ fin = fopen (input, "rb");
+ if (!fin) {
+ VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
+ }
+ } else {
+ fin = stdin;
+ }
+
+ /* check output */
+ FILE *fout = NULL;
+ if (output) {
+ fout = fopen (input, "wb");
+ if (!fout) {
+ VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
+ fclose (fin);
+ }
+ } else {
+ fout = stdout;
+ }
+
+ hexdump (fin, nbcols);
+
+ /* close all */
+ fclose (fin);
+ fclose (fout);
+
+ return 0;
+}
+
+// test: hexdump.exe -h
+// test: hexdump.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: hexdump.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
+// test: hexdump.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: hexdump.exe -i hexdump.c | grep -q '0x[0-9a-f]*: '
+// test: hexdump.exe -i hexdump.c -n 3|head -2|tail -1| grep '0x0003: 64 65 70 dep'
+
+/* vim: set ts=4 sw=4 et: */