Commit dd16fec4 authored by Pavel Emelyanov's avatar Pavel Emelyanov

crtools: Sanitize options parsing

Do not presume that the argv[1] is action. Use the optind index at the end
of parsing instead. This allows to specify --help option properly.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent af3b2869
...@@ -54,7 +54,6 @@ int main(int argc, char *argv[]) ...@@ -54,7 +54,6 @@ int main(int argc, char *argv[])
pid_t pid = 0; pid_t pid = 0;
int ret = -1; int ret = -1;
int opt, idx; int opt, idx;
int action = -1;
int log_inited = 0; int log_inited = 0;
int log_level = 0; int log_level = 0;
...@@ -65,8 +64,6 @@ int main(int argc, char *argv[]) ...@@ -65,8 +64,6 @@ int main(int argc, char *argv[])
if (argc < 2) if (argc < 2)
goto usage; goto usage;
action = argv[1][0];
/* Default options */ /* Default options */
opts.final_state = TASK_DEAD; opts.final_state = TASK_DEAD;
...@@ -89,7 +86,7 @@ int main(int argc, char *argv[]) ...@@ -89,7 +86,7 @@ int main(int argc, char *argv[])
{ }, { },
}; };
opt = getopt_long(argc - 1, argv + 1, short_opts, long_opts, &idx); opt = getopt_long(argc, argv, short_opts, long_opts, &idx);
if (opt == -1) if (opt == -1)
break; break;
...@@ -187,15 +184,15 @@ int main(int argc, char *argv[]) ...@@ -187,15 +184,15 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
if (strcmp(argv[1], "dump") && if (strcmp(argv[optind], "dump") &&
strcmp(argv[1], "restore") && strcmp(argv[optind], "restore") &&
strcmp(argv[1], "show") && strcmp(argv[optind], "show") &&
strcmp(argv[1], "check")) { strcmp(argv[optind], "check")) {
pr_err("Unknown command"); pr_err("Unknown command %s", argv[optind]);
goto usage; goto usage;
} }
switch (action) { switch (argv[optind][0]) {
case 'd': case 'd':
if (!pid) if (!pid)
goto opt_pid_missing; goto opt_pid_missing;
...@@ -263,6 +260,9 @@ usage: ...@@ -263,6 +260,9 @@ usage:
pr_msg(" -D|--images-dir directory where to get images from\n"); pr_msg(" -D|--images-dir directory where to get images from\n");
pr_msg(" -c|--contents show contents of pages dumped in hexdump format\n"); pr_msg(" -c|--contents show contents of pages dumped in hexdump format\n");
pr_msg("\nOther options:\n");
pr_msg(" -h|--help show this text\n");
return -1; return -1;
opt_pid_missing: opt_pid_missing:
......
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