From 7f9f3fa0b1d130f207efd2bba049bbcc98cd407d Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sun, 20 Jul 2025 16:51:30 +0200 Subject: [PATCH] lib/string/strcmp/: strneq(), STRNEQ(): Add APIs Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 2 ++ lib/string/README | 9 +++++++-- lib/string/strcmp/strneq.c | 13 +++++++++++++ lib/string/strcmp/strneq.h | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 lib/string/strcmp/strneq.c create mode 100644 lib/string/strcmp/strneq.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 31e9c0ee3..dc701148e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -235,6 +235,8 @@ libshadow_la_SOURCES = \ string/strcmp/strcaseprefix.h \ string/strcmp/streq.c \ string/strcmp/streq.h \ + string/strcmp/strneq.c \ + string/strcmp/strneq.h \ string/strcmp/strprefix.c \ string/strcmp/strprefix.h \ string/strcpy/stpecpy.c \ diff --git a/lib/string/README b/lib/string/README index 13d332110..147b278fd 100644 --- a/lib/string/README +++ b/lib/string/README @@ -33,6 +33,11 @@ Don't use some libc functions without Really Good Reasons: The return value of strcmp(3) is confusing. strcmp(3) would be legitimate for sorting strings. + strncmp(3) + Use STRNEQ(), or strpfx(), or else, depending on what you want. + The return value of strncmp(3) is confusing, + and it is unclear the purpose of its use when you read it. + strcasecmp(3) Use strcaseeq() instead. @@ -131,9 +136,9 @@ strcmp/ - String comparison Like strsfx(), but ignore upper-/lower-case. n/ - strneq() // Unimplemented + strneq() Return true if a [[gnu::nonstring]] is equal to a string. - STRNEQ() // Unimplemented + STRNEQ() Like strneq(), but takes an array. strnpfx() // Unimplemented diff --git a/lib/string/strcmp/strneq.c b/lib/string/strcmp/strneq.c new file mode 100644 index 000000000..5791811a9 --- /dev/null +++ b/lib/string/strcmp/strneq.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include "config.h" + +#include "string/strcmp/strneq.h" + +#include +#include + + +extern inline bool strneq(const char *strn, const char *s, size_t size); diff --git a/lib/string/strcmp/strneq.h b/lib/string/strcmp/strneq.h new file mode 100644 index 000000000..de8958bae --- /dev/null +++ b/lib/string/strcmp/strneq.h @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRNEQ_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRNEQ_H_ + + +#include "config.h" + +#include +#include + +#include "attr.h" +#include "sizeof.h" + + +#define STRNEQ(strn, s) strneq(strn, s, countof(strn)) + + +ATTR_STRING(2) +inline bool strneq(ATTR_NONSTRING const char *strn, const char *s, size_t size); + + +// nonstring equal +/* Return true if the nonstring strn and the string s compare equal. */ +inline bool +strneq(const char *strn, const char *s, size_t size) +{ + if (strlen(s) > size) + return false; + + return strncmp(strn, s, size) == 0; +} + + +#endif // include guard -- 2.47.3