LinuxKPI: Implement strscpy
strscpy copies the src string, or as much of it as fits, into the dst buffer. The dst buffer is always NUL terminated, unless it's zero-sized. strscpy returns the number of characters copied (not including the trailing NUL) or -E2BIG if len is 0 or src was truncated. Currently drm-kmod replaces strscpy with strncpy that is not quite correct as strncpy does not NUL-terminate truncated strings and returns different values on exit. Reviewed by: hselasky, imp, manu MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D31005
This commit is contained in:
parent
98a6984a9e
commit
019391bf85
@ -167,4 +167,20 @@ str_has_prefix(const char *str, const char *prefix)
|
||||
return (strncmp(str, prefix, len) == 0 ? len : 0);
|
||||
}
|
||||
|
||||
static inline ssize_t
|
||||
strscpy(char* dst, const char* src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (len <= INT_MAX) {
|
||||
for (i = 0; i < len; i++)
|
||||
if ('\0' == (dst[i] = src[i]))
|
||||
return ((ssize_t)i);
|
||||
if (i != 0)
|
||||
dst[--i] = '\0';
|
||||
}
|
||||
|
||||
return (-E2BIG);
|
||||
}
|
||||
|
||||
#endif /* _LINUX_STRING_H_ */
|
||||
|
Loading…
Reference in New Issue
Block a user