diff options
author | Colin Cross <ccross@android.com> | 2013-05-22 14:49:19 -0700 |
---|---|---|
committer | Colin Cross <ccross@google.com> | 2013-06-11 00:44:42 +0000 |
commit | eff57afe53f2cbdf48f988b692036a7c470c41fa (patch) | |
tree | ce4db285ae4f1b0d7bb6c7a565691aaadf35a72a /librank | |
parent | aae2a03f6a2cf6774a719182b1dc02fdf863777f (diff) | |
download | extras-eff57afe53f2cbdf48f988b692036a7c470c41fa.tar.gz |
librank: convert argument parsing to getopt_long
Change-Id: Ia145018d9096a550febe465c9f5b5f00d36c444d
Diffstat (limited to 'librank')
-rw-r--r-- | librank/librank.c | 70 |
1 files changed, 51 insertions, 19 deletions
diff --git a/librank/librank.c b/librank/librank.c index e54d1c34..bc6991c4 100644 --- a/librank/librank.c +++ b/librank/librank.c @@ -17,6 +17,7 @@ #include <assert.h> #include <dirent.h> #include <errno.h> +#include <getopt.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> @@ -189,29 +190,60 @@ int main(int argc, char *argv[]) { order = -1; prefix = NULL; prefix_len = 0; - - for (i = 1; i < argc; i++) { - if (!strcmp(argv[i], "-P")) { - if (i + 1 >= argc) { - fprintf(stderr, "Option -P requires an argument.\n"); - usage(argv[0]); - exit(EXIT_FAILURE); - } - prefix = argv[++i]; + opterr = 0; + + while (1) { + int c; + const struct option longopts[] = { + {"help", 0, 0, 'h'}, + {"pss", 0, 0, 'p'}, + {"uss", 0, 0, 'u'}, + {"vss", 0, 0, 'v'}, + {"rss", 0, 0, 'r'}, + {"reverse", 0, 0, 'R'}, + {"path", required_argument, 0, 'P'}, + {0, 0, 0, 0} + }; + c = getopt_long(argc, argv, "hpP:uvrR", longopts, NULL); + if (c < 0) { + break; + } + /* Alphabetical cases */ + switch (c) { + case 'h': + usage(argv[0]); + exit(EXIT_SUCCESS); + case 'p': + compfn = &sort_by_pss; + break; + case 'P': + prefix = optarg; prefix_len = strlen(prefix); - continue; + break; + case 'u': + compfn = &sort_by_uss; + break; + case 'v': + compfn = &sort_by_vss; + break; + case 'r': + compfn = &sort_by_rss; + break; + case 'R': + order *= -1; + break; + case '?': + fprintf(stderr, "Invalid argument \"%s\".\n", argv[optind - 1]); + usage(argv[0]); + exit(EXIT_FAILURE); + default: + abort(); } - if (!strcmp(argv[i], "-v")) { compfn = &sort_by_vss; continue; } - if (!strcmp(argv[i], "-r")) { compfn = &sort_by_rss; continue; } - if (!strcmp(argv[i], "-p")) { compfn = &sort_by_pss; continue; } - if (!strcmp(argv[i], "-u")) { compfn = &sort_by_uss; continue; } - if (!strcmp(argv[i], "-R")) { order *= -1; continue; } - if (!strcmp(argv[i], "-h")) { usage(argv[0]); exit(0); } - fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]); - usage(argv[0]); - exit(EXIT_FAILURE); } + argc -= optind; + argv += optind; + libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *)); libraries_count = 0; libraries_size = INIT_LIBRARIES; |