From 3ef8550a26c95c97f9fdc4ef249a5767fd573e7a Mon Sep 17 00:00:00 2001 From: Douglas Gilbert Date: Fri, 29 Jun 2018 09:46:13 +0000 Subject: sg_opcode: support MLU, Multiple Logical Units (18-045r1); sg_lib: sg_simple_inquiry_pt(); ./configure: option --enable-debug added for testing git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@779 6180dd3e-e324-4e3e-922d-17de1ae2f315 --- examples/Makefile | 9 ++++-- examples/sgq_dd.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 17 deletions(-) (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile index 73df9bc2..0416a7f8 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -29,9 +29,12 @@ MAN_PREF = man8 LARGE_FILE_FLAGS = -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -CFLAGS = -g -O2 -W -Wall -iquote ../include -D_REENTRANT $(LARGE_FILE_FLAGS) -# CFLAGS = -g -O2 -Wall -iquote ../include -D_REENTRANT -DSG_KERNEL_INCLUDES $(LARGE_FILE_FLAGS) -# CFLAGS = -g -O2 -Wall -pedantic -iquote ../include -D_REENTRANT $(LARGE_FILE_FLAGS) +##CPPFLAGS = -iquote ../include -D_REENTRANT $(LARGE_FILE_FLAGS) +CPPFLAGS = -iquote ../include -D_REENTRANT $(LARGE_FILE_FLAGS) -DDEBUG + +CFLAGS = -g -O2 -W -Wall +# CFLAGS = -g -O2 -Wall -DSG_KERNEL_INCLUDES +# CFLAGS = -g -O2 -Wall -pedantic LDFLAGS = diff --git a/examples/sgq_dd.c b/examples/sgq_dd.c index 01693295..debbe377 100644 --- a/examples/sgq_dd.c +++ b/examples/sgq_dd.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -49,13 +50,12 @@ typedef uint8_t u_char; /* horrible, for scsi.h */ */ -static char * version_str = "0.60 20180220"; +static char * version_str = "0.61 20180627"; /* resurrected from "0.55 20020509" */ #define DEF_BLOCK_SIZE 512 #define DEF_BLOCKS_PER_TRANSFER 128 -/* #define SG_DEBUG */ #define SENSE_BUFF_LEN 32 /* Arbitrary, could be larger */ #define DEF_TIMEOUT 60000 /* 60,000 millisecs == 60 seconds */ @@ -150,6 +150,20 @@ static const char * proc_allow_dio = "/proc/scsi/sg/allow_dio"; static int sg_finish_io(int wr, Rq_elem * rep); +/* 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 install_handler (int sig_num, void (*sig_handler) (int sig)) { @@ -315,7 +329,7 @@ read_capacity(int sg_fd, int * num_sect, int * sect_sz) } *num_sect = 1 + sg_get_unaligned_be32(rcBuff + 0); *sect_sz = sg_get_unaligned_be32(rcBuff + 4); -#ifdef SG_DEBUG +#ifdef DEBUG fprintf(stderr, "number of sectors=%d, sector size=%d\n", *num_sect, *sect_sz); #endif @@ -717,6 +731,8 @@ decider(Rq_coll * clp, int first_xfer, int * req_indexp) int main(int argc, char * argv[]) { + bool verbose_given = false; + bool version_given = false; int skip = 0; int seek = 0; int ibs = 0; @@ -726,7 +742,7 @@ main(int argc, char * argv[]) char * buf; char inf[INOUTF_SZ]; char outf[INOUTF_SZ]; - int res, k; + int res, k, n, keylen; int in_num_sect = 0; int out_num_sect = 0; int num_threads = DEF_NUM_THREADS; @@ -744,10 +760,6 @@ main(int argc, char * argv[]) rcoll.out_type = FT_OTHER; inf[0] = '\0'; outf[0] = '\0'; - if (argc < 2) { - usage(); - return 1; - } for(k = 1; k < argc; k++) { if (argv[k]) @@ -758,6 +770,7 @@ main(int argc, char * argv[]) buf++; if (*buf) *buf++ = '\0'; + keylen = strlen(key); if (strcmp(key,"if") == 0) strncpy(inf, buf, INOUTF_SZ); else if (strcmp(key,"of") == 0) @@ -784,21 +797,73 @@ main(int argc, char * argv[]) rcoll.coe = sg_get_num(buf); else if (0 == strcmp(key,"gen")) gen = sg_get_num(buf); - else if (0 == strncmp(key,"deb", 3)) + else if ((0 == strncmp(key,"deb", 3)) || (0 == strncmp(key,"verb", 4))) rcoll.debug = sg_get_num(buf); else if (0 == strcmp(key,"time")) do_time = sg_get_num(buf); - else if (0 == strncmp(key, "--vers", 6)) { - fprintf(stderr, "sgq_dd for sg version 3 driver: %s\n", - version_str); + else if ((keylen > 1) && ('-' == key[0]) && ('-' != key[1])) { + res = 0; + n = num_chs_in_str(key + 1, keylen - 1, 'h'); + if (n > 0) { + usage(); + return 0; + } + n = num_chs_in_str(key + 1, keylen - 1, 'v'); + if (n > 0) + verbose_given = true; + rcoll.debug += n; + res += n; + n = num_chs_in_str(key + 1, keylen - 1, 'V'); + if (n > 0) + version_given = true; + res += n; + if (res < (keylen - 1)) { + fprintf(stderr, "Unrecognised short option in '%s', try " + "'--help'\n", key); + return SG_LIB_SYNTAX_ERROR; + } + } else if (0 == strncmp(key, "--help", 6)) { + usage(); return 0; - } + } else if (0 == strncmp(key, "--verb", 6)) { + verbose_given = true; + ++rcoll.debug; + } else if (0 == strncmp(key, "--vers", 6)) + version_given = true; else { fprintf(stderr, "Unrecognized argument '%s'\n", key); usage(); return 1; } } +#ifdef DEBUG + fprintf(stderr, "In DEBUG mode, "); + if (verbose_given && version_given) { + fprintf(stderr, "but override: '-vV' given, zero verbose and " + "continue\n"); + verbose_given = false; + version_given = false; + rcoll.debug = 0; + } else if (! verbose_given) { + fprintf(stderr, "set '-vv'\n"); + rcoll.debug = 2; + } else + fprintf(stderr, "keep verbose=%d\n", rcoll.debug); +#else + if (verbose_given && version_given) + fprintf(stderr, "Not in DEBUG mode, so '-vV' has no special action\n"); +#endif + if (version_given) { + fprintf(stderr, "sgq_dd for sg version 3 driver: %s\n", + version_str); + return 0; + return 0; + } + + if (argc < 2) { + usage(); + return 1; + } if (rcoll.bs <= 0) { rcoll.bs = DEF_BLOCK_SIZE; fprintf(stderr, "Assume default 'bs' (block size) of %d bytes\n", @@ -901,7 +966,7 @@ main(int argc, char * argv[]) } } if ((STDIN_FILENO == rcoll.infd) && (STDOUT_FILENO == rcoll.outfd)) { - fprintf(stderr, "Disallow both if and of to be stdin and stdout"); + fprintf(stderr, "Disallow both if and of to be stdin and stdout\n"); return 1; } if ((FT_OTHER == rcoll.in_type) && (FT_OTHER == rcoll.out_type) && !gen) { -- cgit v1.2.3