|
2 | 2 | #ifndef _LINUX_STRING_H_
|
3 | 3 | #define _LINUX_STRING_H_
|
4 | 4 |
|
| 5 | +#include <linux/args.h> |
5 | 6 | #include <linux/array_size.h>
|
6 | 7 | #include <linux/compiler.h> /* for inline */
|
7 | 8 | #include <linux/types.h> /* for size_t */
|
@@ -66,9 +67,40 @@ extern char * strcpy(char *,const char *);
|
66 | 67 | #ifndef __HAVE_ARCH_STRNCPY
|
67 | 68 | extern char * strncpy(char *,const char *, __kernel_size_t);
|
68 | 69 | #endif
|
69 |
| -#ifndef __HAVE_ARCH_STRSCPY |
70 |
| -ssize_t strscpy(char *, const char *, size_t); |
71 |
| -#endif |
| 70 | +ssize_t sized_strscpy(char *, const char *, size_t); |
| 71 | + |
| 72 | +/* |
| 73 | + * The 2 argument style can only be used when dst is an array with a |
| 74 | + * known size. |
| 75 | + */ |
| 76 | +#define __strscpy0(dst, src, ...) \ |
| 77 | + sized_strscpy(dst, src, sizeof(dst) + __must_be_array(dst)) |
| 78 | +#define __strscpy1(dst, src, size) sized_strscpy(dst, src, size) |
| 79 | + |
| 80 | +/** |
| 81 | + * strscpy - Copy a C-string into a sized buffer |
| 82 | + * @dst: Where to copy the string to |
| 83 | + * @src: Where to copy the string from |
| 84 | + * @...: Size of destination buffer (optional) |
| 85 | + * |
| 86 | + * Copy the source string @src, or as much of it as fits, into the |
| 87 | + * destination @dst buffer. The behavior is undefined if the string |
| 88 | + * buffers overlap. The destination @dst buffer is always NUL terminated, |
| 89 | + * unless it's zero-sized. |
| 90 | + * |
| 91 | + * The size argument @... is only required when @dst is not an array, or |
| 92 | + * when the copy needs to be smaller than sizeof(@dst). |
| 93 | + * |
| 94 | + * Preferred to strncpy() since it always returns a valid string, and |
| 95 | + * doesn't unnecessarily force the tail of the destination buffer to be |
| 96 | + * zero padded. If padding is desired please use strscpy_pad(). |
| 97 | + * |
| 98 | + * Returns the number of characters copied in @dst (not including the |
| 99 | + * trailing %NUL) or -E2BIG if @size is 0 or the copy from @src was |
| 100 | + * truncated. |
| 101 | + */ |
| 102 | +#define strscpy(dst, src, ...) \ |
| 103 | + CONCATENATE(__strscpy, COUNT_ARGS(__VA_ARGS__))(dst, src, __VA_ARGS__) |
72 | 104 |
|
73 | 105 | /**
|
74 | 106 | * strscpy_pad() - Copy a C-string into a sized buffer
|
|
0 commit comments