[PATCH] btrfs-progs: Fix wrong optind re-initialization to allow mixed option and non-option

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 <paul@xxxxxxxxxxxxxxx>
Cc: Hugo Mills <hugo@xxxxxxxxxxxxx>
Fixes: 010ceab56e06 ("btrfs-progs: rework option parser to use getopt for global options")
Signed-off-by: Qu Wenruo <wqu@xxxxxxxx>
---
 btrfs.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/btrfs.c b/btrfs.c
index 2d39f2ced3e8..ebc4a507165d 100644
--- a/btrfs.c
+++ b/btrfs.c
@@ -169,7 +169,7 @@ static int cmd_version(int argc, char **argv)
  * Parse global options, between binary name and first non-option argument
  * after processing all valid options (including those with arguments).
  *
- * Returns index to argv where parsting stopped, optind is reset to 1
+ * Returns index to argv where parsting stopped, optind is reset to 0
  */
 static int handle_global_options(int argc, char **argv)
 {
@@ -205,7 +205,13 @@ static int handle_global_options(int argc, char **argv)
 	}
 
 	shift = optind;
-	optind = 1;
+	/*
+	 * optind must to be init to 0 other than 1.
+	 * Since later subcommand will call getopt_long() again, we need
+	 * to re-initialize the internal variables correct.
+	 * Check getopt(3) NOTES sections.
+	 */
+	optind = 0;
 
 	return shift;
 }
-- 
2.17.1

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux