From 9247d6c94bf9cca613b85b25d8002789b53577e0 Mon Sep 17 00:00:00 2001 From: Krzysztof Opasiak Date: Fri, 18 Mar 2016 17:35:19 +0100 Subject: [PATCH] Use getopt_long() to parse cmd line Use a standard linux way of parsing cmd line arguments to avoid unnecessary code. Change-Id: I64325e7804048c1abce013f78df5163ee4af85c8 Signed-off-by: Krzysztof Opasiak --- lthor.c | 98 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 45 deletions(-) mode change 100755 => 100644 lthor.c diff --git a/lthor.c b/lthor.c old mode 100755 new mode 100644 index 1c5ebdc..5ebc357 --- a/lthor.c +++ b/lthor.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -1174,74 +1175,81 @@ int test_tar_file_list(char **tarfilelist) void usage(const char *exename) { - fprintf(stderr, "%s: [-t] [-v] [-c] [-d port] [-p pitfile] [tar] [tar] ..\n", - exename); + fprintf(stderr, + "Usage: %s: [options] [-d port] [-p pitfile] [tar] [tar] ..\n" + "Options:\n" + " -t, --test Don't flash, just check if given tar files are correct\n" + " -v, --verbose Be more verbose\n" + " -c, --check Don't flash, just check if given tty port is thor capable\n" + " -d=, --port= Use specific tty port for communication\n" + " -p=, --pitfile= Flash new partition table\n" + " --help Print this help message\n", + exename); exit(1); } int main(int argc, char **argv) { - const char *exename, *portname, *pitfile; + const char *exename = NULL, *portname = NULL, *pitfile = NULL; int opt; int opt_check = 0; - - exename = argv[0]; - - opt = 1; - - pitfile = NULL; - portname = NULL; + int optindex; + struct option opts[] = { + {"test", no_argument, 0, 't'}, + {"verbose", no_argument, 0, 'v'}, + {"check", no_argument, 0, 'c'}, + {"port", required_argument, 0, 'd'}, + {"pitfile", required_argument, 0, 'p'}, + {"help", no_argument, 0, 1}, + {0, 0, 0, 0} + }; printf("\n"); printf("Linux Thor downloader, version %s \n", PACKAGE_VERSION); - printf("Authors: Jaehoon You \n\n"); - - while (opt < argc) { - /* check if we're verbose */ - if (!strcmp(argv[opt], "-v")) { - opt_verbose = 1; - opt++; - continue; - } + printf("Authors: Jaehoon You \n" + " Krzysztof Opasiak \n\n"); - if (!strcmp(argv[opt], "-t")) { - opt_test = 1; - opt++; - continue; - } - - if (!strcmp(argv[opt], "-c")) { - opt_check = 1; - opt++; - continue; - } + exename = argv[0]; - if (!strcmp(argv[opt], "-p")) { - pitfile = argv[opt+1]; - opt += 2; - continue; - } + while (1) { + opt = getopt_long(argc, argv, "tvcd:p:", opts, &optindex); + if (opt == -1) + break; - if (!strcmp(argv[opt], "-d") && (opt+1) < argc) { - portname = argv[opt+1]; - opt += 2; - continue; + switch (opt) { + case 't': + opt_test = 1; + break; + case 'v': + opt_verbose = 1; + break; + case 'c': + opt_check = 1; + break; + case 'd': + portname = optarg; + break; + case 'p': + pitfile = optarg; + break; + case 1: + default: + usage(exename); + return 0; } - - break; } if (opt_test) - return test_tar_file_list(&argv[opt]); + return test_tar_file_list(&(argv[optind])); if (opt_check) return check_proto(portname); - if ((pitfile)&&(opt == argc)) + if (pitfile && !argv[optind]) return process_download(portname, pitfile, NULL); - if (opt < argc) - return process_download(portname, pitfile, &argv[opt]); + if (argv[optind]) + return process_download(portname, pitfile, &(argv[optind])); usage(exename); return 0; -- 2.34.1