fix iscsi-ls parameter parse
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.
This commit is contained in:
committed by
Bart Van Assche
parent
b087a09a0b
commit
045c2387e7
@@ -40,6 +40,7 @@ WSADATA wsaData;
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
#include <getopt.h>
|
||||||
#include "iscsi.h"
|
#include "iscsi.h"
|
||||||
#include "scsi-lowlevel.h"
|
#include "scsi-lowlevel.h"
|
||||||
|
|
||||||
@@ -328,7 +329,7 @@ void print_help(void)
|
|||||||
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
|
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n");
|
||||||
fprintf(stderr, " -d, --debug Print debug information\n");
|
fprintf(stderr, " -d, --debug Print debug information\n");
|
||||||
fprintf(stderr, " -s, --show-luns Show the luns for each target\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, " (does not work with -s)\n");
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, "Help options:\n");
|
fprintf(stderr, "Help options:\n");
|
||||||
@@ -349,7 +350,8 @@ int main(int argc, char *argv[])
|
|||||||
struct iscsi_url *iscsi_url = NULL;
|
struct iscsi_url *iscsi_url = NULL;
|
||||||
struct client_state state;
|
struct client_state state;
|
||||||
char *url = NULL;
|
char *url = NULL;
|
||||||
int i;
|
int c;
|
||||||
|
int option_index;
|
||||||
static int show_help = 0, show_usage = 0, debug = 0;
|
static int show_help = 0, show_usage = 0, debug = 0;
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -359,31 +361,44 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (i = 1; i < argc; i++) {
|
static struct option long_options[] = {
|
||||||
if (!strcmp(argv[i], "-?") ||
|
{"help", no_argument, NULL, 'h'},
|
||||||
!strcmp(argv[i], "-h") ||
|
{"usage", no_argument, NULL, 'u'},
|
||||||
!strcmp(argv[i], "--help")) {
|
{"debug", no_argument, NULL, 'd'},
|
||||||
show_help = 1;
|
{"initiator-name", required_argument, NULL, 'i'},
|
||||||
} else if (!strcmp(argv[i], "-u") ||
|
{"show-luns", no_argument, NULL, 's'},
|
||||||
!strcmp(argv[i], "-usage")) {
|
{"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;
|
show_usage = 1;
|
||||||
} else if (!strcmp(argv[i], "-d") ||
|
break;
|
||||||
!strcmp(argv[i], "--debug")) {
|
case 'd':
|
||||||
debug = 1;
|
debug = 1;
|
||||||
} else if (!strcmp(argv[i], "-i") ||
|
break;
|
||||||
!strcmp(argv[i], "--initiator-name")) {
|
case 'i':
|
||||||
initiator = argv[++i];
|
initiator = optarg;
|
||||||
} else if (!strcmp(argv[i], "-s") ||
|
break;
|
||||||
!strcmp(argv[i], "--show-luns")) {
|
case 's':
|
||||||
showluns = 1;
|
showluns = 1;
|
||||||
} else if (!strcmp(argv[i], "-U") ||
|
break;
|
||||||
!strcmp(argv[i], "--url")) {
|
case 'U':
|
||||||
useurls = 1;
|
useurls = 1;
|
||||||
} else if (!strncmp("iscsi://", argv[i], 8) ||
|
break;
|
||||||
!strncmp("iser://", argv[i], 7)) {
|
default:
|
||||||
url = strdup(argv[i]);
|
fprintf(stderr, "Unrecognized option '%c'\n\n", c);
|
||||||
}
|
print_help();
|
||||||
}
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (show_help != 0) {
|
if (show_help != 0) {
|
||||||
print_help();
|
print_help();
|
||||||
@@ -397,6 +412,9 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
memset(&state, 0, sizeof(state));
|
memset(&state, 0, sizeof(state));
|
||||||
|
|
||||||
|
if (argv[optind] != NULL) {
|
||||||
|
url = strdup(argv[optind]);
|
||||||
|
}
|
||||||
if (url == NULL) {
|
if (url == NULL) {
|
||||||
fprintf(stderr, "You must specify iscsi target portal.\n");
|
fprintf(stderr, "You must specify iscsi target portal.\n");
|
||||||
print_usage();
|
print_usage();
|
||||||
|
|||||||
Reference in New Issue
Block a user