summaryrefslogtreecommitdiff
path: root/lib/string.c
diff options
context:
space:
mode:
authorDavid Brownell <david-b@pacbell.net>2008-05-01 04:34:42 -0700
committerLinus Torvalds <torvalds@linux-foundation.org>2008-05-01 08:03:59 -0700
commit34990cf702bdf2b4964e0629dab4af7669f8b2c5 (patch)
tree33ec40ee97be1a9a6b3565dcf49d1dcffff191f9 /lib/string.c
parent7dffa3c673fbcf835cd7be80bb4aec8ad3f51168 (diff)
downloadlwn-34990cf702bdf2b4964e0629dab4af7669f8b2c5.tar.gz
lwn-34990cf702bdf2b4964e0629dab4af7669f8b2c5.zip
Add a new sysfs_streq() string comparison function
Add a new sysfs_streq() string comparison function, which ignores the trailing newlines found in sysfs inputs. By example: sysfs_streq("a", "b") ==> false sysfs_streq("a", "a") ==> true sysfs_streq("a", "a\n") ==> true sysfs_streq("a\n", "a") ==> true This is intended to simplify parsing of sysfs inputs, letting them avoid the need to manually strip off newlines from inputs. Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Acked-by: Greg KH <greg@kroah.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/string.c')
-rw-r--r--lib/string.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c
index 5efafed3d6b6..b19b87af65a3 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -493,6 +493,33 @@ char *strsep(char **s, const char *ct)
EXPORT_SYMBOL(strsep);
#endif
+/**
+ * sysfs_streq - return true if strings are equal, modulo trailing newline
+ * @s1: one string
+ * @s2: another string
+ *
+ * This routine returns true iff two strings are equal, treating both
+ * NUL and newline-then-NUL as equivalent string terminations. It's
+ * geared for use with sysfs input strings, which generally terminate
+ * with newlines but are compared against values without newlines.
+ */
+bool sysfs_streq(const char *s1, const char *s2)
+{
+ while (*s1 && *s1 == *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_streq);
+
#ifndef __HAVE_ARCH_MEMSET
/**
* memset - Fill a region of memory with the given value