Commit 34990cf702bdf2b4964e0629dab4af7669f8b2c5

Authored by David Brownell
Committed by Linus Torvalds
1 parent 7dffa3c673

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>

Showing 2 changed files with 29 additions and 0 deletions Side-by-side Diff

include/linux/string.h
... ... @@ -109,6 +109,8 @@
109 109 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
110 110 extern void argv_free(char **argv);
111 111  
  112 +extern bool sysfs_streq(const char *s1, const char *s2);
  113 +
112 114 #endif
113 115 #endif /* _LINUX_STRING_H_ */
... ... @@ -493,6 +493,33 @@
493 493 EXPORT_SYMBOL(strsep);
494 494 #endif
495 495  
  496 +/**
  497 + * sysfs_streq - return true if strings are equal, modulo trailing newline
  498 + * @s1: one string
  499 + * @s2: another string
  500 + *
  501 + * This routine returns true iff two strings are equal, treating both
  502 + * NUL and newline-then-NUL as equivalent string terminations. It's
  503 + * geared for use with sysfs input strings, which generally terminate
  504 + * with newlines but are compared against values without newlines.
  505 + */
  506 +bool sysfs_streq(const char *s1, const char *s2)
  507 +{
  508 + while (*s1 && *s1 == *s2) {
  509 + s1++;
  510 + s2++;
  511 + }
  512 +
  513 + if (*s1 == *s2)
  514 + return true;
  515 + if (!*s1 && *s2 == '\n' && !s2[1])
  516 + return true;
  517 + if (*s1 == '\n' && !s1[1] && !*s2)
  518 + return true;
  519 + return false;
  520 +}
  521 +EXPORT_SYMBOL(sysfs_streq);
  522 +
496 523 #ifndef __HAVE_ARCH_MEMSET
497 524 /**
498 525 * memset - Fill a region of memory with the given value