diff options
Diffstat (limited to 'linux/string.c')
-rw-r--r-- | linux/string.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/linux/string.c b/linux/string.c index fd2797ea..a32a8995 100644 --- a/linux/string.c +++ b/linux/string.c @@ -21,8 +21,10 @@ #include <ctype.h> #include <errno.h> +#include <limits.h> #include <string.h> +#include <linux/bug.h> #include <linux/compiler.h> #include <linux/string.h> @@ -62,6 +64,31 @@ size_t strlcpy(char *dest, const char *src, size_t size) return ret; } +ssize_t strscpy(char *dest, const char *src, size_t count) +{ + long res = 0; + + if (count == 0 || WARN_ON_ONCE(count > INT_MAX)) + 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; +} + void memzero_explicit(void *s, size_t count) { memset(s, 0, count); |