From patchwork Tue Jun 19 05:12:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10473173 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3792D60383 for ; Tue, 19 Jun 2018 05:12:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 28AF228C04 for ; Tue, 19 Jun 2018 05:12:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1D4BF28C3A; Tue, 19 Jun 2018 05:12:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E5C9728C04 for ; Tue, 19 Jun 2018 05:12:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S937301AbeFSFMe (ORCPT ); Tue, 19 Jun 2018 01:12:34 -0400 Received: from mx2.suse.de ([195.135.220.15]:43502 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935908AbeFSFMd (ORCPT ); Tue, 19 Jun 2018 01:12:33 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext-too.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 1893BAE89; Tue, 19 Jun 2018 05:12:32 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, Paul Jones , Hugo Mills Subject: [PATCH] btrfs-progs: Fix wrong optind re-initialization to allow mixed option and non-option Date: Tue, 19 Jun 2018 13:12:24 +0800 Message-Id: <20180619051224.21876-1-wqu@suse.com> X-Mailer: git-send-email 2.17.1 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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; }