Branch data Line data Source code
1 : : /*
2 : : * Adopted from linux kernel
3 : : */
4 : : #include <sys/types.h>
5 : : #include <string.h>
6 : :
7 : : #include "string.h"
8 : :
9 : : #ifndef CONFIG_HAS_STRLCPY
10 : : /**
11 : : * strlcpy - Copy a %NUL terminated string into a sized buffer
12 : : * @dest: Where to copy the string to
13 : : * @src: Where to copy the string from
14 : : * @size: size of destination buffer
15 : : *
16 : : * Compatible with *BSD: the result is always a valid
17 : : * NUL-terminated string that fits in the buffer (unless,
18 : : * of course, the buffer size is zero). It does not pad
19 : : * out the result like strncpy() does.
20 : : */
21 : 0 : size_t strlcpy(char *dest, const char *src, size_t size)
22 : : {
23 : 0 : size_t ret = strlen(src);
24 : :
25 [ # # ]: 0 : if (size) {
26 [ # # ]: 0 : size_t len = (ret >= size) ? size - 1 : ret;
27 : : memcpy(dest, src, len);
28 : 0 : dest[len] = '\0';
29 : : }
30 : 0 : return ret;
31 : : }
32 : : #endif
33 : :
34 : : #ifndef CONFIG_HAS_STRLCAT
35 : : /**
36 : : * strlcat - Append a length-limited, %NUL-terminated string to another
37 : : * @dest: The string to be appended to
38 : : * @src: The string to append to it
39 : : * @count: The size of the destination buffer.
40 : : */
41 : 0 : size_t strlcat(char *dest, const char *src, size_t count)
42 : : {
43 : 0 : size_t dsize = strlen(dest);
44 : 0 : size_t len = strlen(src);
45 : 0 : size_t res = dsize + len;
46 : :
47 : : /*
48 : : * It's assumed that @dsize strictly
49 : : * less than count. Otherwise it's
50 : : * a bug. But we left it to a caller.
51 : : */
52 : 0 : dest += dsize;
53 : 0 : count -= dsize;
54 [ # # ]: 0 : if (len >= count)
55 : 0 : len = count-1;
56 : : memcpy(dest, src, len);
57 : 0 : dest[len] = 0;
58 : 0 : return res;
59 : : }
60 : : #endif
|