From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/hexdump.c | 88 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/hexdump.c (limited to 'reference/C/CONTRIB/SNIP/hexdump.c') diff --git a/reference/C/CONTRIB/SNIP/hexdump.c b/reference/C/CONTRIB/SNIP/hexdump.c new file mode 100755 index 0000000..bb90b7a --- /dev/null +++ b/reference/C/CONTRIB/SNIP/hexdump.c @@ -0,0 +1,88 @@ +/* +** HEXDUMP.C - Dump a file. +** +** This Program Written By Paul Edwards w/ modifications by Bob Stout +** Released to the public domain +*/ + +#include +#include +#include +#include + +static void dodump(FILE *fp, long start, long count); +static void skipb(FILE *fp, long start); + +main(int argc, char **argv) +{ + FILE *fp; + long start, count; + + if (argc < 2) + { + puts("Usage: HEXDUMP file_name [start] [length]"); + return (EXIT_FAILURE); + } + if (argc > 2) + start = atol(*(argv + 2)); + else start = 0L; + if (argc > 3) + count = atol(*(argv + 3)); + else count = -1L; + fp = fopen(*(argv + 1), "rb"); + if (fp == NULL) + { + printf("unable to open file %s for input\n", *(argv+1)); + return (EXIT_FAILURE); + } + skipb(fp, start); + dodump(fp, start, count); + return (EXIT_SUCCESS); +} + +static void dodump(FILE *fp, long start, long count) +{ + int c, pos1, pos2; + long x = 0L; + char prtln[100]; + + while (((c = fgetc(fp)) != EOF) && (x != count)) + { + if (x%16 == 0) + { + memset(prtln,' ',sizeof prtln); + sprintf(prtln,"%0.6X ", start + x); + pos1 = 8; + pos2 = 45; + } + sprintf(prtln + pos1, "%0.2X", c); + if (isprint(c)) + sprintf(prtln + pos2, "%c", c); + else sprintf(prtln + pos2, "."); + pos1 += 2; + *(prtln+pos1) = ' '; + pos2++; + if (x % 4 == 3) + *(prtln + pos1++) = ' '; + if (x % 16 == 15) + printf("%s\n", prtln); + x++; + } + if (x % 16 != 15) + printf("%s\n", prtln); + return; +} + +static void skipb(FILE *fp, long start) +{ + long x = 0; + + if (start == 0) + return; + while (x < start) + { + fgetc(fp); + x++; + } + return; +} -- cgit v1.2.3-54-g00ecf