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 04/13] lib/string: add strncmp Date: Wed, 11 May 2016 18:12:46 +0200 Message-ID: <1462983171-4208-5-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]:40616 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932165AbcEKQNU (ORCPT ); Wed, 11 May 2016 12:13:20 -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 B1456C084AE0 for ; Wed, 11 May 2016 16:13:19 +0000 (UTC) In-Reply-To: <1462983171-4208-1-git-send-email-rkrcmar@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 --- v4: new lib/string.c | 17 ++++++++++------- lib/string.h | 1 + 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/string.c b/lib/string.c index e7bcfe945fcf..ee93e25e9821 100644 --- a/lib/string.c +++ b/lib/string.c @@ -26,15 +26,18 @@ char *strcpy(char *dest, const char *src) return strcat(dest, src); } =20 +int strncmp(const char *a, const char *b, size_t n) +{ + for (; n--; ++a, ++b) + if (*a !=3D *b || *a =3D=3D '\0') + return *a - *b; + + return 0; +} + int strcmp(const char *a, const char *b) { - while (*a =3D=3D *b) { - if (*a =3D=3D '\0') { - break; - } - ++a, ++b; - } - return *a - *b; + return strncmp(a, b, SIZE_MAX); } =20 char *strchr(const char *s, int c) diff --git a/lib/string.h b/lib/string.h index 4e24f54d9e23..2391013ad2b1 100644 --- a/lib/string.h +++ b/lib/string.h @@ -5,6 +5,7 @@ extern unsigned long strlen(const char *buf); extern char *strcat(char *dest, const char *src); extern char *strcpy(char *dest, const char *src); extern int strcmp(const char *a, const char *b); +extern int strncmp(const char *a, const char *b, size_t n); extern char *strchr(const char *s, int c); extern char *strstr(const char *haystack, const char *needle); extern void *memset(void *s, int c, size_t n); --=20 2.8.2