]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
main.c: Read arguments with getopt_long()
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 14 Jul 2010 14:18:47 +0000 (17:18 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 14 Jul 2010 14:18:47 +0000 (17:18 +0300)
This makes it much easier to add new command line arguments in future.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c

diff --git a/main.c b/main.c
index 45d479677d127fa86a0231833e23095ddcb1ce6b..34ae261d7913cd6ec63b89f93bd042855cfed949 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,27 +2,50 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <getopt.h>
 
 #include "parse.h"
 #include "analyze.h"
 
+void read_args(int argc, char *argv[], struct parse_opts *opts)
+{
+       int optind = 0, c;
+       static struct option long_options[] = {
+               { .val = 'p', .name = "pid", .has_arg = 1, },
+       };
+       char short_options[] = "p:";
+
+       while (1) {
+               c = getopt_long(argc, argv, short_options, long_options,
+                               &optind);
+
+               if (c == -1)
+                       break;
+
+               printf("%c: %s\n", c, optarg);
+               switch (c) {
+               case 'p':
+                       opts->pid = atoi(optarg);
+                       opts->parse_mask = PARSE_PID;
+                       break;
+               }
+       }
+}
+
 int main(int argc, char *argv[])
 {
        struct pageframe pf;
        struct process *process_list = NULL;
        struct parse_opts opts;
-       int pid;
 
-       if (argc < 2) {
+       if (argc < 3) {
                printf("A pid needs to be given as an argument\n");
                return 1;
        }
 
-       pid = atoi(argv[1]);
+       read_args(argc, argv, &opts);
 
        memset(&pf, 0, sizeof(pf));
-       opts.parse_mask = PARSE_PID;
-       opts.pid = pid;
 
        scan_all_pids(&pf, &process_list, &opts);
        print_pid_stats(&pf, process_list);