]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
main: Assume non-option arguments to be pids or process names
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 2 Sep 2010 15:35:55 +0000 (18:35 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 2 Sep 2010 15:35:55 +0000 (18:35 +0300)
If arguments are given without any option arguments, it is tested if
it is a valid pid number, and add it to pidlist. If it is not a pid
number, then it is threated as a process name the system is scanned
for processes with a matching name.

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

diff --git a/main.c b/main.c
index 9fc6d7114b081699c73051585b08d42a2f06f7b4..b3d6a1206d409a18b6f3ed20ba0ae9e49b12c7dd 100644 (file)
--- a/main.c
+++ b/main.c
@@ -42,9 +42,24 @@ static void get_all_pids_by_name(struct parse_opts *opts, char *name)
        }
 }
 
+static int add_pid_to_pidlist(int pidi, struct list_head *pidlist)
+{
+       struct pidlist *pid = alloc_pidlist();
+
+       if (pid == NULL) {
+               perror("malloc");
+               return -1;
+       }
+       pid->pid = pidi;
+
+       list_add_tail(&pid->list, pidlist);
+
+       return 0;
+}
+
 void read_args(int argc, char *argv[], struct parse_opts *opts)
 {
-       int optind = 0, c;
+       int option_index = 0, c;
        static struct option long_options[] = {
                { .val = 'p', .name = "pid", .has_arg = 1, },
                { .val = 'P', .name = "process", .has_arg = 1, },
@@ -59,7 +74,7 @@ void read_args(int argc, char *argv[], struct parse_opts *opts)
 
        while (1) {
                c = getopt_long(argc, argv, short_options, long_options,
-                               &optind);
+                               &option_index);
 
                if (c == -1)
                        break;
@@ -67,15 +82,15 @@ void read_args(int argc, char *argv[], struct parse_opts *opts)
                switch (c) {
                case 'p':
                {
-                       struct pidlist *pid = alloc_pidlist();
-
-                       if (pid == NULL) {
-                               perror("malloc");
-                               return;
+                       int pid = pidstr_is_ok(optarg);
+                       if (!pid) {
+                               fprintf(stderr, "Invalid pid number %s\n",
+                                       optarg);
+                               break;
                        }
-                       pid->pid = atoi(optarg);
+
                        opts->parse_mask |= PARSE_PID;
-                       list_add_tail(&pid->list, &opts->pidlist);
+                       add_pid_to_pidlist(pid, &opts->pidlist);
                        break;
                }
                case 'P':
@@ -100,6 +115,20 @@ void read_args(int argc, char *argv[], struct parse_opts *opts)
                        break;
                }
        }
+
+       while (optind < argc) {
+               printf("%s ", argv[optind]);
+               int pid = pidstr_is_ok(argv[optind]);
+
+               if (pid) {
+                       opts->parse_mask |= PARSE_PID;
+                       add_pid_to_pidlist(pid, &opts->pidlist);
+               } else {
+                       get_all_pids_by_name(opts, argv[optind]);
+                       opts->parse_mask |= PARSE_PID;
+               }
+               optind++;
+       }
 }
 
 int main(int argc, char *argv[])
@@ -117,8 +146,9 @@ int main(int argc, char *argv[])
 
        read_args(argc, argv, &opts);
 
-       if (argc < 3) {
-               printf("A pid needs to be given as an argument\n");
+       if (argc < 2) {
+               printf("A pid or process name "
+                       "needs to be given as an argument\n");
                print_help_and_die(argv[0]);
        }