git: f94d7319540b - main - LinuxKPI: sysfs: implement sysfs_match_string()

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Thu, 24 Apr 2025 22:14:38 UTC
The branch main has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=f94d7319540b9b2256be60c2c666efe0b6e635ef

commit f94d7319540b9b2256be60c2c666efe0b6e635ef
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2025-04-24 14:09:08 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2025-04-24 22:13:54 +0000

    LinuxKPI: sysfs: implement sysfs_match_string()
    
    Use a macro to automatically gather the length of the array while
    we can.
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      3 days
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D50007
---
 sys/compat/linuxkpi/common/include/linux/sysfs.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/sysfs.h b/sys/compat/linuxkpi/common/include/linux/sysfs.h
index f11db5ece515..65e023031bb2 100644
--- a/sys/compat/linuxkpi/common/include/linux/sysfs.h
+++ b/sys/compat/linuxkpi/common/include/linux/sysfs.h
@@ -464,6 +464,24 @@ sysfs_emit_at(char *buf, int at, const char *fmt, ...)
 	return (i);
 }
 
+static inline int
+_sysfs_match_string(const char * const *a, size_t l, const char *s)
+{
+	const char *p;
+	int i;
+
+	for (i = 0; i < l; i++) {
+		p = a[i];
+		if (p == NULL)
+			break;
+		if (sysfs_streq(p, s))
+			return (i);
+	}
+
+	return (-ENOENT);
+}
+#define	sysfs_match_string(a, s)	_sysfs_match_string(a, ARRAY_SIZE(a), s)
+
 #define sysfs_attr_init(attr) do {} while(0)
 
 #endif	/* _LINUXKPI_LINUX_SYSFS_H_ */