ISCSI-LS : Change from popt to the slightly less portable getopt_long

win32 and amiga/aros does not have getopt_long() so we will need a
replacement for the platforms where getopt_long() is missing.
This commit is contained in:
Ronnie Sahlberg
2013-04-21 09:44:23 -07:00
parent 622b38d679
commit c0d1c122c3

View File

@@ -22,11 +22,15 @@
#include <poll.h> #include <poll.h>
#endif #endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <popt.h> #include <getopt.h>
#include "iscsi.h" #include "iscsi.h"
#include "scsi-lowlevel.h" #include "scsi-lowlevel.h"
@@ -292,7 +296,7 @@ void discoveryconnect_cb(struct iscsi_context *iscsi, int status, void *command_
void print_usage(void) void print_usage(void)
{ {
fprintf(stderr, "Usage: iscsi-ls [-?s] [-?|--help] [--usage] [-i|--initiator-name=iqn-name]\n" fprintf(stderr, "Usage: iscsi-ls [-?|--help] [--usage] [-i|--initiator-name=iqn-name]\n"
"\t\t[-s|--show-luns] <iscsi-portal-url>\n"); "\t\t[-s|--show-luns] <iscsi-portal-url>\n");
} }
@@ -301,7 +305,6 @@ void print_help(void)
fprintf(stderr, "Usage: iscsi-ls [OPTION...] <iscsi-url>\n"); fprintf(stderr, "Usage: iscsi-ls [OPTION...] <iscsi-url>\n");
fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\n"); fprintf(stderr, " -i, --initiator-name=iqn-name Initiatorname to use\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, " -d, --debug=integer debug level (0=disabled)\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fprintf(stderr, "Help options:\n"); fprintf(stderr, "Help options:\n");
fprintf(stderr, " -?, --help Show this help message\n"); fprintf(stderr, " -?, --help Show this help message\n");
@@ -315,41 +318,48 @@ void print_help(void)
fprintf(stderr, " \"ipv6-address\" [fce0::1]\n"); fprintf(stderr, " \"ipv6-address\" [fce0::1]\n");
} }
int main(int argc, const char *argv[]) int main(int argc, char *argv[])
{ {
struct iscsi_context *iscsi; struct iscsi_context *iscsi;
struct iscsi_url *iscsi_url = NULL; struct iscsi_url *iscsi_url = NULL;
struct client_state state; struct client_state state;
const char **extra_argv;
int extra_argc = 0;
const char *url = NULL; const char *url = NULL;
poptContext pc; int c;
int res; static int show_help = 0, show_usage = 0, debug = 0;
int show_help = 0, show_usage = 0, debug = 0;
struct poptOption popt_options[] = { static struct option long_options[] = {
{ "help", '?', POPT_ARG_NONE, &show_help, 0, "Show this help message", NULL }, {"help", no_argument, NULL, 'h'},
{ "usage", 0, POPT_ARG_NONE, &show_usage, 0, "Display brief usage message", NULL }, {"usage", no_argument, NULL, 'u'},
{ "initiator-name", 'i', POPT_ARG_STRING, &initiator, 0, "Initiatorname to use", "iqn-name" }, {"debug", no_argument, NULL, 'd'},
{ "show-luns", 's', POPT_ARG_NONE, &showluns, 0, "Show the luns for each target", NULL }, {"show-luns", no_argument, NULL, 's'},
{ "debug", 'd', POPT_ARG_INT, &debug, 0, "Debugging level", "integer" }, {"initiator_name", required_argument, NULL, 'i'},
POPT_TABLEEND {0, 0, 0, 0}
}; };
int option_index;
memset(&state, 0, sizeof(state)); while ((c = getopt_long(argc, argv, "h?udi:s", long_options,
&option_index)) != -1) {
pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_POSIXMEHARDER); switch (c) {
if ((res = poptGetNextOpt(pc)) < -1) { case 'h':
fprintf(stderr, "Failed to parse option : %s %s\n", case '?':
poptBadOption(pc, 0), poptStrerror(res)); show_help = 1;
exit(10); break;
} case 'u':
extra_argv = poptGetArgs(pc); show_usage = 1;
if (extra_argv) { break;
url = strdup(*extra_argv); case 'd':
extra_argv++; debug = 1;
while (extra_argv[extra_argc]) { break;
extra_argc++; case 'i':
initiator = optarg;
break;
case 's':
showluns = 1;
break;
default:
fprintf(stderr, "Unrecognized option '%c'\n\n", c);
print_help();
exit(0);
} }
} }
@@ -363,8 +373,14 @@ int main(int argc, const char *argv[])
exit(0); exit(0);
} }
poptFreeContext(pc); if (optind != argc -1) {
print_usage();
exit(0);
}
memset(&state, 0, sizeof(state));
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();