In sysfs ->store() callbacks we usually need to remember that a
supplied sysfs input string might or might not contain whitespaces
and a trailing new-line symbol, which we need to take care of.
Examples:
        echo "newline" > /sys/.../attr
        echo -n "no_newine" > /sys/.../attr

That's why a typical sysfs ->store() should do something like
below when it copies a string:

        ssize_t FOO_store(struct device *dev, ....)
        {
                ....
                strlcpy(value, buf, MAX_SZ);
                sz = strlen(value);
                if (sz > 0 && value[sz - 1] == '\n')
                        value[sz - 1] = 0x00;
                ...
        }

or use a sysfs-string friendly strcmp() function when comparing
a given string to a pre-defined one:

        ssize_t FOO_store(struct device *dev, ....)
        {
                ...
                if (sysfs_streq(buf, "normal"))
                        mode = m_normal;
                ...
        }

Per Andrew Morton:
: There's a LOT of code which does basically-the-same-thing with sysfs
: input. And a lot of it misses things, such as leading whitespace.
: Passing all this through helpers would provide consistency as well
: as code-size reductions, improved reviewability, etc.

This patch introduces two such helpers - sysfs_strncpy() and
sysfs_strlcpy(), which, basically, do what strncpy() and strlcpy() do,
but additionally they remove leading and trailing white-spaces and
tailing new-line symbols. So a FOO_store() example which was posted
above may be rewritten to:

        ssize_t FOO_store(struct device *dev, ....)
        {
                ....
                sysfs_strlcpy(value, buf, MAX_SZ);
                ...
        }

Signed-off-by: Sergey Senozhatsky <sergey.senozhat...@gmail.com>
Suggested-by: Andrew Morton <a...@linux-foundation.org>
---
 include/linux/string.h |  3 +++
 lib/string.c           | 51 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 4a5a0eb7df51..62b08bbc3ada 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -175,6 +175,9 @@ extern char **argv_split(gfp_t gfp, const char *str, int 
*argcp);
 extern void argv_free(char **argv);
 
 extern bool sysfs_streq(const char *s1, const char *s2);
+extern char *sysfs_strncpy(char *dest, const char *src, size_t count);
+extern size_t sysfs_strlcpy(char *dest, const char *src, size_t size);
+
 extern int kstrtobool(const char *s, bool *res);
 static inline int strtobool(const char *s, bool *res)
 {
diff --git a/lib/string.c b/lib/string.c
index 2c0900a5d51a..e81b1be00796 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -631,6 +631,57 @@ bool sysfs_streq(const char *s1, const char *s2)
 }
 EXPORT_SYMBOL(sysfs_streq);
 
+/**
+ * sysfs_strncpy - Trim a length-limited C-string (wgutesoaces and a trailing
+ *                 newline symbol) and copy into a buffer
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @count: The maximum number of bytes to copy
+ *
+ * A wrapper around strncpy().
+ *
+ */
+char *sysfs_strncpy(char *dest, const char *src, size_t count)
+{
+       char *c;
+
+       strncpy(dest, skip_spaces(src), count);
+
+       c = dest + count - 1;
+       while (c >= dest && (isspace(*c) || *c == '\n' || *c == '\0')) {
+               *c = '\0';
+               c--;
+       }
+       return dest;
+}
+EXPORT_SYMBOL(sysfs_strncpy);
+
+/**
+ * sysfs_strlcpy - Trim a C-string (whitespaces and a trailing newline symbol)
+ *                 and copy it into a sized buffer
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @size: size of destination buffer
+ *
+ * A wrapper around strlcpy().
+ *
+ */
+size_t sysfs_strlcpy(char *dest, const char *src, size_t size)
+{
+       size_t ret;
+       char *c;
+
+       ret = strlcpy(dest, skip_spaces(src), size);
+
+       size = strlen(dest);
+       c = dest + size - 1;
+       while (c >= dest && (isspace(*c) || *c == '\n'))
+               c--;
+       *(c + 1) = '\0';
+       return ret;
+}
+EXPORT_SYMBOL(sysfs_strlcpy);
+
 /**
  * match_string - matches given string in an array
  * @array:     array of strings
-- 
2.18.0

Reply via email to