From 8e47649eff96c303e02fbd58cdc6c4ed341066ec Mon Sep 17 00:00:00 2001 From: Abner Chang Date: Sat, 25 Jul 2020 17:30:39 +0800 Subject: [PATCH] lib: Add sbi_strncmp implementation This commit add an implementation of sbi_strncmp. Signed-off-by: Abner Chang Reviewed-by: Atish Patra Reviewed-by: Anup Patel --- include/sbi/sbi_string.h | 7 +++++++ lib/sbi/sbi_string.c | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/sbi/sbi_string.h b/include/sbi/sbi_string.h index 338075fc..b7c2bc22 100644 --- a/include/sbi/sbi_string.h +++ b/include/sbi/sbi_string.h @@ -12,8 +12,15 @@ #include +/* + Provides sbi_strcmp for the completeness of supporting string functions. + it is not recommended to use sbi_strcmp() but use sbi_strncmp instead. +*/ + int sbi_strcmp(const char *a, const char *b); +int sbi_strncmp(const char *a, const char *b, size_t count); + size_t sbi_strlen(const char *str); size_t sbi_strnlen(const char *str, size_t count); diff --git a/lib/sbi/sbi_string.c b/lib/sbi/sbi_string.c index 38b700bb..7805ba4a 100644 --- a/lib/sbi/sbi_string.c +++ b/lib/sbi/sbi_string.c @@ -14,6 +14,10 @@ #include +/* + Provides sbi_strcmp for the completeness of supporting string functions. + it is not recommended to use sbi_strcmp() but use sbi_strncmp instead. +*/ int sbi_strcmp(const char *a, const char *b) { /* search first diff or end of string */ @@ -23,6 +27,15 @@ int sbi_strcmp(const char *a, const char *b) return *a - *b; } +int sbi_strncmp(const char *a, const char *b, size_t count) +{ + /* search first diff or end of string */ + for (; count > 0 && *a == *b && *a != '\0'; a++, b++, count--) + ; + + return *a - *b; +} + size_t sbi_strlen(const char *str) { unsigned long ret = 0;