qemu-img resize: Fix option parsing

For shrinking images, you're supposed to use a negative size. However, the
leading minus makes getopt think that it's an option and so you get the help
text if you don't use -- like in 'qemu-img resize test.img -- -1G'.

This patch handles the size first and removes it from the argument list so that
getopt won't even try to interpret it and you don't need -- any more.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Kevin Wolf 2011-04-29 10:58:12 +02:00
parent 57aa265d46
commit e80fec7feb
1 changed files with 11 additions and 2 deletions

View File

@ -1442,6 +1442,16 @@ static int img_resize(int argc, char **argv)
{ NULL } { NULL }
}; };
/* Remove size from argv manually so that negative numbers are not treated
* as options by getopt. */
if (argc < 3) {
help();
return 1;
}
size = argv[--argc];
/* Parse getopt arguments */
fmt = NULL; fmt = NULL;
for(;;) { for(;;) {
c = getopt(argc, argv, "f:h"); c = getopt(argc, argv, "f:h");
@ -1458,11 +1468,10 @@ static int img_resize(int argc, char **argv)
break; break;
} }
} }
if (optind + 1 >= argc) { if (optind >= argc) {
help(); help();
} }
filename = argv[optind++]; filename = argv[optind++];
size = argv[optind++];
/* Choose grow, shrink, or absolute resize mode */ /* Choose grow, shrink, or absolute resize mode */
switch (size[0]) { switch (size[0]) {