diff options
| author | riastradh <riastradh@NetBSD.org> | 2021-12-19 00:48:30 +0000 |
|---|---|---|
| committer | riastradh <riastradh@NetBSD.org> | 2021-12-19 00:48:30 +0000 |
| commit | 34bd2372c7f8db3cd2f8c94face6a0bc70361f6b (patch) | |
| tree | bdb3e6fc5626c9aa587d51f6631b1c93beb33a9f /sys/external/bsd/drm2/include/linux/string.h | |
| parent | e5a638b191a9c5f0a135ce40cd9694b9bb2e0949 (diff) | |
Implement strscpy.
Diffstat (limited to 'sys/external/bsd/drm2/include/linux/string.h')
| -rw-r--r-- | sys/external/bsd/drm2/include/linux/string.h | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/sys/external/bsd/drm2/include/linux/string.h b/sys/external/bsd/drm2/include/linux/string.h index d9ba6c5afdf..74298e553bb 100644 --- a/sys/external/bsd/drm2/include/linux/string.h +++ b/sys/external/bsd/drm2/include/linux/string.h @@ -1,4 +1,4 @@ -/* $NetBSD: string.h,v 1.5 2018/08/27 06:49:23 riastradh Exp $ */ +/* $NetBSD: string.h,v 1.6 2021/12/19 00:48:30 riastradh Exp $ */ /*- * Copyright (c) 2013 The NetBSD Foundation, Inc. @@ -34,6 +34,7 @@ #include <sys/types.h> #include <sys/cdefs.h> +#include <sys/errno.h> #include <sys/null.h> #include <linux/slab.h> @@ -93,4 +94,25 @@ kstrdup(const char *src, gfp_t gfp) return kstrndup(src, strlen(src), gfp); } +static inline ssize_t +strscpy(char *dst, const char *src, size_t dstsize) +{ + size_t n = dstsize; + + /* If no space for a NUL terminator, fail. */ + if (n == 0) + return -E2BIG; + + /* Copy until we get a NUL terminator or the end of the buffer. */ + while ((*dst++ = *src++) != '\0') { + if (__predict_false(--n == 0)) { + dst[-1] = '\0'; /* NUL-terminate */ + return -E2BIG; + } + } + + /* Return the number of bytes copied, excluding NUL. */ + return dstsize - n; +} + #endif /* _LINUX_STRING_H_ */ |
