libiscsi/0019-fix-iscsi-ls-parameter-parse.patch
2022-06-10 14:58:46 +08:00

122 lines
4.1 KiB
Diff

From 045c2387e77f2c20359e956e3327b006e5814cc6 Mon Sep 17 00:00:00 2001
From: sallyjunjun <72725839+sallyjunjun@users.noreply.github.com>
Date: Wed, 8 Jun 2022 10:02:04 +0800
Subject: [PATCH 3/4] fix iscsi-ls parameter parse
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
If invalid option is input with iscsi-ls, such as "iscsi-ls -a iscsi://", the command just stuck here and do not print useful information for the user to correct.
Fix this problem with getopt_long.
---
utils/iscsi-ls.c | 64 ++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 41 insertions(+), 23 deletions(-)
diff --git a/utils/iscsi-ls.c b/utils/iscsi-ls.c
index 2b1d5e2..107121d 100644
--- a/utils/iscsi-ls.c
+++ b/utils/iscsi-ls.c
@@ -37,6 +37,7 @@ WSADATA wsaData;
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
+#include <getopt.h>
#include "iscsi.h"
#include "scsi-lowlevel.h"
@@ -329,7 +330,7 @@ void print_help(void)
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
fprintf(stderr, " -d, --debug Print debug information\n");
fprintf(stderr, " -s, --show-luns Show the luns for each target\n");
- fprintf(stderr, " --url Output targets in URL format\n");
+ fprintf(stderr, " -U, --url Output targets in URL format\n");
fprintf(stderr, " (does not work with -s)\n");
fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n");
@@ -350,7 +351,8 @@ int main(int argc, char *argv[])
struct iscsi_url *iscsi_url = NULL;
struct client_state state;
const char *url = NULL;
- int i;
+ int c;
+ int option_index;
static int show_help = 0, show_usage = 0, debug = 0;
#ifdef _WIN32
@@ -360,31 +362,44 @@ int main(int argc, char *argv[])
}
#endif
- for (i = 1; i < argc; i++) {
- if (!strcmp(argv[i], "-?") ||
- !strcmp(argv[i], "-h") ||
- !strcmp(argv[i], "--help")) {
- show_help = 1;
- } else if (!strcmp(argv[i], "-u") ||
- !strcmp(argv[i], "-usage")) {
+ static struct option long_options[] = {
+ {"help", no_argument, NULL, 'h'},
+ {"usage", no_argument, NULL, 'u'},
+ {"debug", no_argument, NULL, 'd'},
+ {"initiator-name", required_argument, NULL, 'i'},
+ {"show-luns", no_argument, NULL, 's'},
+ {"url", no_argument, NULL, 'U'},
+ {0, 0, 0, 0}
+ };
+
+ while ((c = getopt_long(argc, argv, "h?udi:sU", long_options,
+ &option_index)) != -1) {
+ switch (c) {
+ case 'h':
+ case '?':
+ show_help = 1;
+ break;
+ case 'u':
show_usage = 1;
- } else if (!strcmp(argv[i], "-d") ||
- !strcmp(argv[i], "--debug")) {
+ break;
+ case 'd':
debug = 1;
- } else if (!strcmp(argv[i], "-i") ||
- !strcmp(argv[i], "--initiator-name")) {
- initiator = argv[++i];
- } else if (!strcmp(argv[i], "-s") ||
- !strcmp(argv[i], "--show-luns")) {
+ break;
+ case 'i':
+ initiator = optarg;
+ break;
+ case 's':
showluns = 1;
- } else if (!strcmp(argv[i], "-U") ||
- !strcmp(argv[i], "--url")) {
+ break;
+ case 'U':
useurls = 1;
- } else if (!strncmp("iscsi://", argv[i], 8) ||
- !strncmp("iser://", argv[i], 7)) {
- url = strdup(argv[i]);
- }
- }
+ break;
+ default:
+ fprintf(stderr, "Unrecognized option '%c'\n\n", c);
+ print_help();
+ exit(0);
+ }
+ }
if (show_help != 0) {
print_help();
@@ -398,6 +413,9 @@ int main(int argc, char *argv[])
memset(&state, 0, sizeof(state));
+ if (argv[optind] != NULL) {
+ url = strdup(argv[optind]);
+ }
if (url == NULL) {
fprintf(stderr, "You must specify iscsi target portal.\n");
print_usage();