Commit 11818045 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov

crtools: Switch to getopt facility

Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@gmail.com>
parent c2ac1d2f
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <getopt.h>
#include <string.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -228,6 +230,16 @@ int main(int argc, char *argv[]) ...@@ -228,6 +230,16 @@ int main(int argc, char *argv[])
{ {
pid_t pid; pid_t pid;
int ret = -1; int ret = -1;
int opt, idx;
int action = -1;
static const char short_opts[] = "drsp:t:h";
static const struct option long_opts[] = {
{ "dump", no_argument, NULL, 'd' },
{ "restore", no_argument, NULL, 'r' },
{ "show", no_argument, NULL, 's' },
{ NULL, no_argument, NULL, 0 }
};
BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE); BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
...@@ -236,32 +248,51 @@ int main(int argc, char *argv[]) ...@@ -236,32 +248,51 @@ int main(int argc, char *argv[])
memset(&zero_page_entry, 0, sizeof(zero_page_entry)); memset(&zero_page_entry, 0, sizeof(zero_page_entry));
switch (argv[2][1]) { for (opt = getopt_long(argc, argv, short_opts, long_opts, &idx); opt != -1;
opt = getopt_long(argc, argv, short_opts, long_opts, &idx)) {
switch (opt) {
case 'p': case 'p':
pid = atol(argv[3]); pid = atoi(optarg);
opts.leader_only = true; opts.leader_only = true;
break; break;
case 't': case 't':
pid = atol(argv[3]); pid = atoi(optarg);
opts.leader_only = false; opts.leader_only = false;
break; break;
case 'd':
action = opt;
break;
case 'r':
action = opt;
break;
case 's':
action = opt;
break;
case 'h':
default: default:
goto usage; goto usage;
} }
}
if (!strcmp(argv[1], "dump")) { switch (action) {
case 'd':
ret = cr_dump_tasks(pid, &opts); ret = cr_dump_tasks(pid, &opts);
} else if (!strcmp(argv[1], "restore")) { break;
case 'r':
ret = cr_restore_tasks(pid, &opts); ret = cr_restore_tasks(pid, &opts);
} else if (!strcmp(argv[1], "show")) { break;
case 's':
ret = cr_show(pid, &opts); ret = cr_show(pid, &opts);
} else break;
default:
goto usage; goto usage;
break;
}
return ret; return ret;
usage: usage:
printk("\nUsage:\n"); printk("\nUsage:\n");
printk("\tcrtools (dump|show|restore) (-p|-t) pid\n\n"); printk("\tcrtools ([--dump|-d]|[--show|-s]|[--restore|-r]) (-p|-t) pid\n\n");
return -1; return -1;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment