Module Name: src
Committed By: riastradh
Date: Sun Dec 19 00:48:31 UTC 2021
Modified Files:
src/sys/external/bsd/drm2/include/linux: string.h
Log Message:
Implement strscpy.
To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/external/bsd/drm2/include/linux/string.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/external/bsd/drm2/include/linux/string.h
diff -u src/sys/external/bsd/drm2/include/linux/string.h:1.5 src/sys/external/bsd/drm2/include/linux/string.h:1.6
--- src/sys/external/bsd/drm2/include/linux/string.h:1.5 Mon Aug 27 06:49:23 2018
+++ src/sys/external/bsd/drm2/include/linux/string.h Sun Dec 19 00:48:30 2021
@@ -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_ */