From 7fb70440cff73c57a2b3a53f22a6ff023718ea39 Mon Sep 17 00:00:00 2001 From: Qu Wenruo Date: Wed, 20 Jun 2018 08:38:38 +0800 Subject: btrfs-progs: Fix wrong optind re-initialization to allow mixed option and non-option In function handle_global_options(), we reset @optind to 1. However according to man page of getopt(3) NOTES section, if we need to rescan options later, @optind should be reset to 0 to initialize the internal variables correctly. This explains the reason why in cmd_check(), getopt_long() doesn't handle the following command correctly: "btrfs check /dev/data/btrfs --check-data-csum" While mkfs.btrfs handles mixed non-option and option correctly: "mkfs.btrfs -f /dev/data/disk1 --data raid1 /dev/data/disk2" Cc: Paul Jones Cc: Hugo Mills Fixes: 010ceab56e06 ("btrfs-progs: rework option parser to use getopt for global options") Signed-off-by: Qu Wenruo Signed-off-by: David Sterba --- cmds-subvolume.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'cmds-subvolume.c') diff --git a/cmds-subvolume.c b/cmds-subvolume.c index 45363a5a..e7a884af 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -102,6 +102,7 @@ static int cmd_subvol_create(int argc, char **argv) struct btrfs_qgroup_inherit *inherit = NULL; DIR *dirstream = NULL; + optind = 0; while (1) { int c = getopt(argc, argv, "c:i:"); if (c < 0) @@ -248,6 +249,7 @@ static int cmd_subvol_delete(int argc, char **argv) enum { COMMIT_AFTER = 1, COMMIT_EACH = 2 }; enum btrfs_util_error err; + optind = 0; while (1) { int c; static const struct option long_options[] = { @@ -466,6 +468,7 @@ static int cmd_subvol_list(int argc, char **argv) filter_set = btrfs_list_alloc_filter_set(); comparer_set = btrfs_list_alloc_comparer_set(); + optind = 0; while(1) { int c; static const struct option long_options[] = { @@ -636,6 +639,7 @@ static int cmd_subvol_snapshot(int argc, char **argv) DIR *dirstream1 = NULL, *dirstream2 = NULL; memset(&args, 0, sizeof(args)); + optind = 0; while (1) { int c = getopt(argc, argv, "c:i:r"); if (c < 0) @@ -933,6 +937,7 @@ static int cmd_subvol_show(int argc, char **argv) char *subvol_path = NULL; enum btrfs_util_error err; + optind = 0; while (1) { int c; static const struct option long_options[] = { @@ -1132,6 +1137,7 @@ static int cmd_subvol_sync(int argc, char **argv) int sleep_interval = 1; enum btrfs_util_error err; + optind = 0; while (1) { int c = getopt(argc, argv, "s:"); -- cgit v1.2.3