From patchwork Wed May 11 16:12:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= X-Patchwork-Id: 9072681 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 54045BF440 for ; Wed, 11 May 2016 16:13:47 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 897C0200E5 for ; Wed, 11 May 2016 16:13:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9B8982014A for ; Wed, 11 May 2016 16:13:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932333AbcEKQNY (ORCPT ); Wed, 11 May 2016 12:13:24 -0400 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) Received: from potion (dhcp-1-215.brq.redhat.com [10.34.1.215]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id u4BGDK8m018587; Wed, 11 May 2016 12:13:20 -0400 Received: by potion (sSMTP sendmail emulation); Wed, 11 May 2016 18:13:19 +0200 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= To: kvm@vger.kernel.org Cc: Paolo Bonzini , Andrew Jones 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> In-Reply-To: <1462983171-4208-1-git-send-email-rkrcmar@redhat.com> References: <1462983171-4208-1-git-send-email-rkrcmar@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 11 May 2016 16:13:22 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The function parses command line arguments in "key=val" format, and treats "key" as "key=1". Signed-off-by: Radim Kr?má? --- 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 = atol(p+1); return p - s; } + +long args_parse_keyval(int argc, char **argv, char *key) +{ + int i; + size_t keylen = strlen(key); + + for (i = 1; i < argc; i++) { + if (keylen > 0 && strncmp(argv[i], key, keylen - 1)) + continue; + if (argv[i][keylen] == '\0') + return 1; + if (argv[i][keylen] == '=') + 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); +/* + * argc and argv are standard C arguments to main(). + * args_parse_keyval looks for an element of argv that matches the key. + * Returns val interpreted as a long if the element is in key=val format. + * Returns 1 if the element is key. + * Returns 0 otherwise. + * + */ +long args_parse_keyval(int argc, char **argv, char *key); + #endif