From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.5 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED, USER_AGENT_MUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E06AC169C4 for ; Wed, 6 Feb 2019 09:56:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 139ED20B1F for ; Wed, 6 Feb 2019 09:56:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729138AbfBFJ4w (ORCPT ); Wed, 6 Feb 2019 04:56:52 -0500 Received: from mx2.suse.de ([195.135.220.15]:45004 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726804AbfBFJ4w (ORCPT ); Wed, 6 Feb 2019 04:56:52 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 502FCAD2C; Wed, 6 Feb 2019 09:56:51 +0000 (UTC) Received: by unicorn.suse.cz (Postfix, from userid 1000) id E6F80E0142; Wed, 6 Feb 2019 10:56:50 +0100 (CET) Date: Wed, 6 Feb 2019 10:56:50 +0100 From: Michal Kubecek To: netdev@vger.kernel.org Cc: Jeff Kirsher , linville@tuxdriver.com, Nicholas Nunley , nhorman@redhat.com, sassmann@redhat.com Subject: Re: [PATCH v2 1/6] ethtool: move option parsing related code into function Message-ID: <20190206095650.GJ21401@unicorn.suse.cz> References: <20190206000106.24364-1-jeffrey.t.kirsher@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190206000106.24364-1-jeffrey.t.kirsher@intel.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Tue, Feb 05, 2019 at 04:01:01PM -0800, Jeff Kirsher wrote: > From: Nicholas Nunley > > Move option parsing code into find_option function. > > No behavior changes. > > Based on patch by Kan Liang > > Signed-off-by: Nicholas Nunley > Signed-off-by: Jeff Kirsher > --- > ethtool.c | 49 +++++++++++++++++++++++++++++++------------------ > 1 file changed, 31 insertions(+), 18 deletions(-) > > diff --git a/ethtool.c b/ethtool.c > index 2f7e96b..8b7c224 100644 > --- a/ethtool.c > +++ b/ethtool.c > @@ -5265,6 +5265,29 @@ static int show_usage(struct cmd_context *ctx) > return 0; > } > > +static int find_option(int argc, char **argp) > +{ > + const char *opt; > + size_t len; > + int k; > + > + for (k = 0; args[k].opts; k++) { > + opt = args[k].opts; > + for (;;) { > + len = strcspn(opt, "|"); > + if (strncmp(*argp, opt, len) == 0 && > + (*argp)[len] == 0) > + return k; > + > + if (opt[len] == 0) > + break; > + opt += len + 1; > + } > + } > + > + return -1; > +} > + > int main(int argc, char **argp) > { > int (*func)(struct cmd_context *); > @@ -5284,24 +5307,14 @@ int main(int argc, char **argp) > */ > if (argc == 0) > exit_bad_args(); > - for (k = 0; args[k].opts; k++) { > - const char *opt; > - size_t len; > - opt = args[k].opts; > - for (;;) { > - len = strcspn(opt, "|"); > - if (strncmp(*argp, opt, len) == 0 && > - (*argp)[len] == 0) { > - argp++; > - argc--; > - func = args[k].func; > - want_device = args[k].want_device; > - goto opt_found; > - } > - if (opt[len] == 0) > - break; > - opt += len + 1; > - } > + > + k = find_option(argc, argp); > + if (k >= 0) { > + argp++; > + argc--; > + func = args[k].func; > + want_device = args[k].want_device; > + goto opt_found; > } > if ((*argp)[0] == '-') > exit_bad_args(); After hiding the loop into find_option() helper, you can put the following few lines into else branch and get rid of the goto. Michal Kubecek