[PATCH] lib/string: Introduce sysfs_streqcase

From: Gioh Kim
Date: Fri Apr 02 2021 - 05:41:11 EST


As the name shows, it checks if strings are equal in case insensitive
manner. I found some cases using strncasecmp to check the entire
strings and they would not work as intended.

For example, drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c uses
strncasecmp to check that the input via sysfs is "mi". But it would
work even-if the input is "min-wrongcommand".
And also drivers/pnp/interface.c checks "disable" command with
strncasecmp but it would also work if the command is "disable-wrong".

Signed-off-by: Gioh Kim <gi-oh.kim@xxxxxxxxx>
---
lib/string.c | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/lib/string.c b/lib/string.c
index 7548eb715ddb..5e6bc0d3d5c6 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -714,6 +714,29 @@ bool sysfs_streq(const char *s1, const char *s2)
}
EXPORT_SYMBOL(sysfs_streq);

+/**
+ * sysfs_streqcase - same to sysfs_streq and case insensitive
+ * @s1: one string
+ * @s2: another string
+ *
+ */
+bool sysfs_streqcase(const char *s1, const char *s2)
+{
+ while (*s1 && tolower(*s1) == tolower(*s2)) {
+ s1++;
+ s2++;
+ }
+
+ if (*s1 == *s2)
+ return true;
+ if (!*s1 && *s2 == '\n' && !s2[1])
+ return true;
+ if (*s1 == '\n' && !s1[1] && !*s2)
+ return true;
+ return false;
+}
+EXPORT_SYMBOL(sysfs_streqcase);
+
/**
* match_string - matches given string in an array
* @array: array of strings
--
2.25.1