From mboxrd@z Thu Jan 1 00:00:00 1970 From: Radim =?utf-8?B?S3LEjW3DocWZ?= Subject: Re: [kvm-unit-tests PATCH v4 05/13] lib/util: add args_parse_keyval Date: Wed, 11 May 2016 19:19:31 +0200 Message-ID: <20160511171930.GA5864@potion> References: <1462983171-4208-1-git-send-email-rkrcmar@redhat.com> <1462983171-4208-6-git-send-email-rkrcmar@redhat.com> <20160511165818.dzk6muttgr5fctw5@hawk.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: kvm@vger.kernel.org, Paolo Bonzini To: Andrew Jones Return-path: Received: from mx1.redhat.com ([209.132.183.28]:41797 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751491AbcEKRTe (ORCPT ); Wed, 11 May 2016 13:19:34 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 10F476F685 for ; Wed, 11 May 2016 17:19:34 +0000 (UTC) Content-Disposition: inline In-Reply-To: <20160511165818.dzk6muttgr5fctw5@hawk.localdomain> Sender: kvm-owner@vger.kernel.org List-ID: 2016-05-11 18:58+0200, Andrew Jones: > On Wed, May 11, 2016 at 06:12:47PM +0200, Radim Kr=C4=8Dm=C3=A1=C5=99= wrote: >> The function parses command line arguments in "key=3Dval" format, an= d >> treats "key" as "key=3D1". >>=20 >> Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 >> --- >> v4: new >>=20 >> lib/util.c | 17 +++++++++++++++++ >> lib/util.h | 10 ++++++++++ >> 2 files changed, 27 insertions(+) >>=20 >> diff --git a/lib/util.c b/lib/util.c >> index 69b18100c972..8b33d474f4c0 100644 >> --- a/lib/util.c >> +++ b/lib/util.c >> @@ -16,3 +16,20 @@ int parse_keyval(char *s, long *val) >> *val =3D atol(p+1); >> return p - s; >> } >> + >> +long args_parse_keyval(int argc, char **argv, char *key) >> +{ >=20 > I like this better than parse_keyval, except for... >=20 >> + int i; >> + size_t keylen =3D strlen(key); >> + >> + for (i =3D 1; i < argc; i++) { >> + if (keylen > 0 && strncmp(argv[i], key, keylen - 1)) >> + continue; >> + if (argv[i][keylen] =3D=3D '\0') >> + return 1; >> + if (argv[i][keylen] =3D=3D '=3D') >> + return atol(argv[i] + keylen + 1); >> + } >> + >> + return 0; >=20 > ...this. Here we have ambiguous results. Either key was there > and had a value of 0, or wasn't there, and we still get zero. Yes, I implied a boolean key. > How about merging the two, and then fixing-up the arm and powerpc > uses of the old one. I'd drop the "args_" from the name then > too. Seems reasonable. I'll make a new series: [1/3] add strncmp [2/3] add a slightly modified args_parse_keyval, int __parse_keyval(int argc, char **argv, char *key, long *val) that will return -1/0/1 on !key/key/key=3Dval [3/3] replace parse_keyval with renamed __parse_keyval ([2/3] and [3/3] may be squished) The series will depend on your "make argv[0] the program name" and v5 of mine will depend on it.