]> git.itanic.dy.fi Git - scan-pagemap/blob - main.c
main.c: Read arguments with getopt_long()
[scan-pagemap] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <getopt.h>
6
7 #include "parse.h"
8 #include "analyze.h"
9
10 void read_args(int argc, char *argv[], struct parse_opts *opts)
11 {
12         int optind = 0, c;
13         static struct option long_options[] = {
14                 { .val = 'p', .name = "pid", .has_arg = 1, },
15         };
16         char short_options[] = "p:";
17
18         while (1) {
19                 c = getopt_long(argc, argv, short_options, long_options,
20                                 &optind);
21
22                 if (c == -1)
23                         break;
24
25                 printf("%c: %s\n", c, optarg);
26                 switch (c) {
27                 case 'p':
28                         opts->pid = atoi(optarg);
29                         opts->parse_mask = PARSE_PID;
30                         break;
31                 }
32         }
33 }
34
35 int main(int argc, char *argv[])
36 {
37         struct pageframe pf;
38         struct process *process_list = NULL;
39         struct parse_opts opts;
40
41         if (argc < 3) {
42                 printf("A pid needs to be given as an argument\n");
43                 return 1;
44         }
45
46         read_args(argc, argv, &opts);
47
48         memset(&pf, 0, sizeof(pf));
49
50         scan_all_pids(&pf, &process_list, &opts);
51         print_pid_stats(&pf, process_list);
52         print_page_stats(&pf);
53
54         return 0;
55 }