backports: add strscpy()
authorJohannes Berg <johannes.berg@intel.com>
Tue, 7 Feb 2017 13:57:48 +0000 (14:57 +0100)
committerJohannes Berg <johannes.berg@intel.com>
Tue, 7 Feb 2017 14:29:36 +0000 (15:29 +0100)
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
backport/backport-include/linux/string.h
backport/compat/backport-4.3.c

index e8c5cb007b3ad738915caf4a7ea2273f6ec534a4..1fdf4cc34566c5fbc9f67ffd78ec35774e37aff6 100644 (file)
@@ -25,4 +25,8 @@ extern void *memdup_user_nul(const void __user *, size_t);
 void memzero_explicit(void *s, size_t count);
 #endif
 
+#if LINUX_VERSION_IS_LESS(4,3,0)
+ssize_t strscpy(char *dest, const char *src, size_t count);
+#endif
+
 #endif /* __BACKPORT_LINUX_STRING_H */
index c2bf1b5c6690a1bae574ef053c21fc39b4d11dbe..6f4fc3b1a39a4474fe0cd59d942f17c26767f436 100644 (file)
@@ -217,3 +217,29 @@ void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
        }
 }
 EXPORT_SYMBOL_GPL(seq_hex_dump);
+
+ssize_t strscpy(char *dest, const char *src, size_t count)
+{
+       long res = 0;
+
+       if (count == 0)
+               return -E2BIG;
+
+       while (count) {
+               char c;
+
+               c = src[res];
+               dest[res] = c;
+               if (!c)
+                       return res;
+               res++;
+               count--;
+       }
+
+       /* Hit buffer length without finding a NUL; force NUL-termination. */
+       if (res)
+               dest[res-1] = '\0';
+
+       return -E2BIG;
+}
+EXPORT_SYMBOL_GPL(strscpy);