aboutsummaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorDouglas Gilbert <dgilbert@interlog.com>2014-02-14 07:01:18 +0000
committerDouglas Gilbert <dgilbert@interlog.com>2014-02-14 07:01:18 +0000
commita6cf83a7f0489516752d365830d169053c2f7fa3 (patch)
treed187453dea78c2ed6fe3ec8551429654f438b011 /utils
parent58ecb399b93382307cc7de20b76113d90b45336c (diff)
downloadsg3_utils-a6cf83a7f0489516752d365830d169053c2f7fa3.tar.gz
sg_inq: add --inhex=FN to fetch response from a file (rather than a device)
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@551 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'utils')
-rw-r--r--utils/hxascdmp.117
-rw-r--r--utils/hxascdmp.c183
2 files changed, 128 insertions, 72 deletions
diff --git a/utils/hxascdmp.1 b/utils/hxascdmp.1
index 898dc1a9..16300aa3 100644
--- a/utils/hxascdmp.1
+++ b/utils/hxascdmp.1
@@ -1,9 +1,9 @@
-.TH HXASCDMP "1" "September 2010" "sg3_utils\-1.30" SG3_UTILS
+.TH HXASCDMP "1" "February 2014" "sg3_utils\-1.38" SG3_UTILS
.SH NAME
hxascdmp \- hexadecimal ASCII dump
.SH SYNOPSIS
.B hxascdmp
-[\fI\-b=BPL\fR] [\fI\-h\fR] [\fI\-H\fR] [\fI\-V\fR]
+[\fI\-b=BPL\fR] [\fI\-h\fR] [\fI\-H\fR] [\fI\-N\fR] [\fI\-V\fR]
[\fIFILE+\fR]
.SH DESCRIPTION
.\" Add any additional description here
@@ -18,8 +18,12 @@ single space (apart from the 8th and 9th bytes which are separated by two
spaces). If the \fI\-H\fR is not given, there is then a string of 16 ASCII
characters corresponding to the hexadecimal bytes earlier in the line; only
bytes in the range 0x20 to 0x7e are printed in ASCII, other bytes values are
-printed as '.' . Each \fIFILE\fR name that appears on the command line is
-printed on a separate line prior to that file's hexadecimal ASCII dump.
+printed as '.' . If the \fI\-H\fR is not given, each \fIFILE\fR name that
+appears on the command line is printed on a separate line prior to that
+file's hexadecimal ASCII dump.
+.PP
+If the \fI\-N\fR option is given then no address is printed out and each
+line starts with the next hexadecimal byte.
.PP
This utility is pretty close to the 'hexdump -C' variant of BSD's
.B hexdump(1)
@@ -39,6 +43,9 @@ output the usage message then exit.
output hexadecimal only (i.e. don't place an ASCII representation at the
end of each line).
.TP
+\fB\-N\fR
+no address; so each line starts with the next hexadecimal byte.
+.TP
\fB\-V\fR, \fB\-\-version\fR
print the version string and then exit.
.SH NOTES
@@ -91,7 +98,7 @@ Written by Douglas Gilbert.
.SH "REPORTING BUGS"
Report bugs to <dgilbert at interlog dot com>.
.SH COPYRIGHT
-Copyright \(co 2004\-2010 Douglas Gilbert
+Copyright \(co 2004\-2014 Douglas Gilbert
.br
This software is distributed under a FreeBSD license. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
diff --git a/utils/hxascdmp.c b/utils/hxascdmp.c
index 1f62ef10..077fd93e 100644
--- a/utils/hxascdmp.c
+++ b/utils/hxascdmp.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2004-2010 Douglas Gilbert.
+ * Copyright (c) 2004-2014 Douglas Gilbert.
* All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the BSD_LICENSE file.
@@ -17,7 +17,7 @@
static int bytes_per_line = DEF_BYTES_PER_LINE;
-static const char * version_str = "1.13 20100312";
+static const char * version_str = "1.14 20140213";
#define CHARS_PER_HEX_BYTE 3
#define BINARY_START_COL 6
@@ -57,21 +57,41 @@ sg_set_binary_mode(int fd)
}
#endif
+/* Returns the number of times 'ch' is found in string 's' given the
+ * string's length. */
+static int
+num_chs_in_str(const char * s, int slen, int ch)
+{
+ int res = 0;
+
+ while (--slen >= 0) {
+ if (ch == s[slen])
+ ++res;
+ }
+ return res;
+}
static void
-dStrHex(const char* str, int len, long start)
+dStrHex(const char* str, int len, long start, int noAddr)
{
const char* p = str;
unsigned char c;
char buff[MAX_LINE_LENGTH];
long a = start;
- const int bpstart = BINARY_START_COL;
- const int cpstart = BINARY_START_COL +
+ int bpstart, cpstart;
+ int j, k, line_length, nl, cpos, bpos, midline_space;
+
+ if (noAddr) {
+ bpstart = 0;
+ cpstart = ((CHARS_PER_HEX_BYTE * bytes_per_line) + 1) + 5;
+ } else {
+ bpstart = BINARY_START_COL;
+ cpstart = BINARY_START_COL +
((CHARS_PER_HEX_BYTE * bytes_per_line) + 1) + 5;
- int cpos = cpstart;
- int bpos = bpstart;
- int midline_space = (bytes_per_line / 2) + 1;
- int i, k, line_length;
+ }
+ cpos = cpstart;
+ bpos = bpstart;
+ midline_space = ((bytes_per_line + 1) / 2);
if (len <= 0)
return;
@@ -84,50 +104,54 @@ dStrHex(const char* str, int len, long start)
}
memset(buff, ' ', line_length);
buff[line_length] = '\0';
- k = sprintf(buff + 1, "%.2lx", a);
- buff[k + 1] = ' ';
- if (bpos >= ((bpstart + (midline_space * CHARS_PER_HEX_BYTE))))
- bpos++;
+ if (0 == noAddr) {
+ k = sprintf(buff + 1, "%.2lx", a);
+ buff[k + 1] = ' ';
+ }
- for(i = 0; i < len; i++) {
+ for(j = 0; j < len; j++) {
+ nl = (0 == (j % bytes_per_line));
+ if ((j > 0) && nl) {
+ printf("%s\n", buff);
+ bpos = bpstart;
+ cpos = cpstart;
+ a += bytes_per_line;
+ memset(buff,' ', line_length);
+ if (0 == noAddr) {
+ k = sprintf(buff + 1, "%.2lx", a);
+ buff[k + 1] = ' ';
+ }
+ }
c = *p++;
- bpos += CHARS_PER_HEX_BYTE;
- if (bpos == (bpstart + (midline_space * CHARS_PER_HEX_BYTE)))
+ bpos += (nl && noAddr) ? 0 : CHARS_PER_HEX_BYTE;
+ if ((bytes_per_line > 4) && ((j % bytes_per_line) == midline_space))
bpos++;
sprintf(&buff[bpos], "%.2x", (int)(unsigned char)c);
buff[bpos + 2] = ' ';
if ((c < ' ') || (c >= 0x7f))
c='.';
buff[cpos++] = c;
- if (cpos >= (cpstart + bytes_per_line)) {
- printf("%s\n", buff);
- bpos = bpstart;
- cpos = cpstart;
- a += bytes_per_line;
- memset(buff,' ', line_length);
- k = sprintf(buff + 1, "%.2lx", a);
- buff[k + 1] = ' ';
- }
}
if (cpos > cpstart)
printf("%s\n", buff);
}
static void
-dStrHexOnly(const char* str, int len, long start)
+dStrHexOnly(const char* str, int len, long start, int noAddr)
{
const char* p = str;
unsigned char c;
char buff[MAX_LINE_LENGTH];
long a = start;
- const int bpstart = BINARY_START_COL;
- int bpos = bpstart;
- int midline_space = (bytes_per_line / 2) + 1;
- int i, k, line_length;
+ int bpstart, bpos, nl;
+ int midline_space = ((bytes_per_line + 1) / 2);
+ int j, k, line_length;
if (len <= 0)
return;
- line_length = BINARY_START_COL +
+ bpstart = (noAddr ? 0 : BINARY_START_COL);
+ bpos = bpstart;
+ line_length = (noAddr ? 0 : BINARY_START_COL) +
(bytes_per_line * CHARS_PER_HEX_BYTE) + 4;
if (line_length >= MAX_LINE_LENGTH) {
fprintf(stderr, "bytes_per_line causes maximum line length of %d "
@@ -136,27 +160,29 @@ dStrHexOnly(const char* str, int len, long start)
}
memset(buff, ' ', line_length);
buff[line_length] = '\0';
- k = sprintf(buff + 1, "%.2lx", a);
- buff[k + 1] = ' ';
- if (bpos >= ((bpstart + (midline_space * CHARS_PER_HEX_BYTE))))
- bpos++;
+ if (0 == noAddr) {
+ k = sprintf(buff + 1, "%.2lx", a);
+ buff[k + 1] = ' ';
+ }
- for(i = 0; i < len; i++) {
- c = *p++;
- bpos += CHARS_PER_HEX_BYTE;
- if (bpos == (bpstart + (midline_space * CHARS_PER_HEX_BYTE)))
- bpos++;
- sprintf(&buff[bpos], "%.2x", (int)(unsigned char)c);
- buff[bpos + 2] = ' ';
- if (bpos >= (bpstart + (bytes_per_line * CHARS_PER_HEX_BYTE)))
- {
+ for(j = 0; j < len; j++) {
+ nl = (0 == (j % bytes_per_line));
+ if ((j > 0) && nl) {
printf("%s\n", buff);
bpos = bpstart;
a += bytes_per_line;
memset(buff,' ', line_length);
- k = sprintf(buff + 1, "%.2lx", a);
- buff[k + 1] = ' ';
+ if (0 == noAddr) {
+ k = sprintf(buff + 1, "%.2lx", a);
+ buff[k + 1] = ' ';
+ }
}
+ c = *p++;
+ bpos += (nl && noAddr) ? 0 : CHARS_PER_HEX_BYTE;
+ if ((bytes_per_line > 4) && ((j % bytes_per_line) == midline_space))
+ bpos++;
+ sprintf(&buff[bpos], "%.2x", (int)(unsigned char)c);
+ buff[bpos + 2] = ' ';
}
if (bpos > bpstart)
printf("%s\n", buff);
@@ -165,7 +191,7 @@ dStrHexOnly(const char* str, int len, long start)
static void
usage()
{
- fprintf(stderr, "Usage: hxascdmp [-b=<n>] [-h] [-H] [-V] [-?] "
+ fprintf(stderr, "Usage: hxascdmp [-b=<n>] [-h] [-H] [-N] [-V] [-?] "
"[<file>+]\n");
fprintf(stderr, " where:\n");
fprintf(stderr, " -b=<n> bytes per line to display "
@@ -173,6 +199,7 @@ usage()
fprintf(stderr, " -h print this usage message\n");
fprintf(stderr, " -H print hex only (i.e. no ASCII "
"to right)\n");
+ fprintf(stderr, " -N no address, start in first column\n");
fprintf(stderr, " -V print version string then exits\n");
fprintf(stderr, " -? print this usage message\n");
fprintf(stderr, " <file>+ reads file(s) and outputs each "
@@ -187,32 +214,51 @@ main(int argc, const char ** argv)
char buff[8192];
int num = 8192;
long start = 0;
- int res, k, u;
+ int res, k, u, len, n;
int inFile = STDIN_FILENO;
int doHelp = 0;
int doHex = 0;
+ int noAddr = 0;
+ int doVersion = 0;
int hasFilename = 0;
int ret = 0;
+ const char * cp;
for (k = 1; k < argc; k++) {
- if (0 == strncmp("-b=", argv[k], 3)) {
- res = sscanf(argv[k] + 3, "%d", &u);
+ cp = argv[k];
+ len = strlen(cp);
+ if (0 == strncmp("-b=", cp, 3)) {
+ res = sscanf(cp + 3, "%d", &u);
if ((1 != res) || (u < 1)) {
- printf("Bad value after '-b' switch\n");
+ fprintf(stderr, "Bad value after '-b=' option\n");
usage();
return 1;
}
bytes_per_line = u;
- }
- else if (0 == strcmp("-h", argv[k]))
- doHelp = 1;
- else if (0 == strcmp("-H", argv[k]))
- doHex = 1;
- else if (0 == strcmp("-V", argv[k])) {
- fprintf(stderr, "%s\n", version_str);
- return 0;
+ } else if ((len > 1) && ('-' == cp[0]) && ('-' != cp[1])) {
+ res = 0;
+ n = num_chs_in_str(cp + 1, len - 1, 'h');
+ doHelp += n;
+ res += n;
+ n = num_chs_in_str(cp + 1, len - 1, 'H');
+ doHex += n;
+ res += n;
+ n = num_chs_in_str(cp + 1, len - 1, 'N');
+ noAddr += n;
+ res += n;
+ n = num_chs_in_str(cp + 1, len - 1, 'V');
+ doVersion += n;
+ res += n;
+ n = num_chs_in_str(cp + 1, len - 1, '?');
+ doHelp += n;
+ res += n;
+ if (0 == res) {
+ fprintf(stderr, "No option recognized in str: %s\n", cp);
+ usage();
+ return 1;
+ }
} else if (0 == strcmp("-?", argv[k]))
- doHelp = 1;
+ ++doHelp;
else if (*argv[k] == '-') {
fprintf(stderr, "unknown switch: %s\n", argv[k]);
usage();
@@ -222,6 +268,10 @@ main(int argc, const char ** argv)
break;
}
}
+ if (doVersion) {
+ printf("%s\n", version_str);
+ return 0;
+ }
if (doHelp) {
usage();
return 0;
@@ -241,26 +291,25 @@ main(int argc, const char ** argv)
} else {
sg_set_binary_mode(inFile);
start = 0;
- printf("%shex dump of file: %s\n",
- (doHex ? "" : "ASCII "), argv[k]);
+ if (! doHex)
+ printf("ASCII hex dump of file: %s\n", argv[k]);
while ((res = read(inFile, buff, num)) > 0) {
if (doHex)
- dStrHexOnly(buff, res, start);
+ dStrHexOnly(buff, res, start, noAddr);
else
- dStrHex(buff, res, start);
+ dStrHex(buff, res, start, noAddr);
start += (long)res;
}
}
close(inFile);
- printf("\n");
}
} else {
sg_set_binary_mode(inFile);
while ((res = read(inFile, buff, num)) > 0) {
if (doHex)
- dStrHexOnly(buff, res, start);
+ dStrHexOnly(buff, res, start, noAddr);
else
- dStrHex(buff, res, start);
+ dStrHex(buff, res, start, noAddr);
start += (long)res;
}
}