From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [kvm-unit-tests PATCH v4 05/13] lib/util: add args_parse_keyval Date: Wed, 11 May 2016 18:12:47 +0200 Message-ID: <1462983171-4208-6-git-send-email-rkrcmar@redhat.com> References: <1462983171-4208-1-git-send-email-rkrcmar@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Paolo Bonzini , Andrew Jones To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:39787 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932311AbcEKQNW (ORCPT ); Wed, 11 May 2016 12:13:22 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (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 70608C04B305 for ; Wed, 11 May 2016 16:13:22 +0000 (UTC) In-Reply-To: <1462983171-4208-1-git-send-email-rkrcmar@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: The function parses command line arguments in "key=3Dval" format, and treats "key" as "key=3D1". Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 --- v4: new lib/util.c | 17 +++++++++++++++++ lib/util.h | 10 ++++++++++ 2 files changed, 27 insertions(+) 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) +{ + 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; +} diff --git a/lib/util.h b/lib/util.h index 4c4b44132277..d475b058526b 100644 --- a/lib/util.h +++ b/lib/util.h @@ -20,4 +20,14 @@ */ extern int parse_keyval(char *s, long *val); =20 +/* + * argc and argv are standard C arguments to main(). + * args_parse_keyval looks for an element of argv that matches the key= =2E + * Returns val interpreted as a long if the element is in key=3Dval fo= rmat. + * Returns 1 if the element is key. + * Returns 0 otherwise. + * + */ +long args_parse_keyval(int argc, char **argv, char *key); + #endif --=20 2.8.2