]> git.kernelconcepts.de Git - karo-tx-linux.git/blobdiff - mm/util.c
remove-abs64-fix-fix
[karo-tx-linux.git] / mm / util.c
index 902b65a438991fb9bedc45f1a16700e0e53f50c4..55012d786a3545bc415d30b4774bf9b62dbf27ac 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -3,6 +3,7 @@
 #include <linux/string.h>
 #include <linux/compiler.h>
 #include <linux/export.h>
+#include <linux/ctype.h>
 #include <linux/err.h>
 #include <linux/sched.h>
 #include <linux/security.h>
@@ -99,6 +100,35 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp)
 }
 EXPORT_SYMBOL(kstrndup);
 
+/**
+ * kstrimdup - Trim and copy a %NUL terminated string.
+ * @s: the string to trim and duplicate
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Returns an address, which the caller must kfree, containing
+ * a duplicate of the passed string with leading and/or trailing
+ * whitespace (as defined by isspace) removed.
+ */
+char *kstrimdup(const char *s, gfp_t gfp)
+{
+       char *buf;
+       char *begin = skip_spaces(s);
+       size_t len = strlen(begin);
+
+       while (len && isspace(begin[len - 1]))
+               len--;
+
+       buf = kmalloc_track_caller(len + 1, gfp);
+       if (!buf)
+               return NULL;
+
+       memcpy(buf, begin, len);
+       buf[len] = '\0';
+
+       return buf;
+}
+EXPORT_SYMBOL(kstrimdup);
+
 /**
  * kmemdup - duplicate region of memory
  *