]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strcmp/: strneq(), STRNEQ(): Add APIs
authorAlejandro Colomar <alx@kernel.org>
Sun, 20 Jul 2025 14:51:30 +0000 (16:51 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 18 Oct 2025 19:08:47 +0000 (14:08 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/README
lib/string/strcmp/strneq.c [new file with mode: 0644]
lib/string/strcmp/strneq.h [new file with mode: 0644]

index 31e9c0ee323e6e554ed52bcdb3a8a57300e0df35..dc701148e11b0358853033a3a05f08693ca321c7 100644 (file)
@@ -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 \
index 13d33211062c081e7663061f2694b4990bacef7f..147b278fd44afe236ab8bf5c512baf66b9c5f57d 100644 (file)
@@ -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 (file)
index 0000000..5791811
--- /dev/null
@@ -0,0 +1,13 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include "config.h"
+
+#include "string/strcmp/strneq.h"
+
+#include <stdbool.h>
+#include <stddef.h>
+
+
+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 (file)
index 0000000..de8958b
--- /dev/null
@@ -0,0 +1,37 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// 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 <stdbool.h>
+#include <string.h>
+
+#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