xref: /kvmtool/util/strbuf.c (revision 24ed52db066022f3063e4ef4fb591e2780133323)
1 
2 /* user defined headers */
3 #include <kvm/util.h>
4 #include <kvm/strbuf.h>
5 
6 int prefixcmp(const char *str, const char *prefix)
7 {
8 	for (; ; str++, prefix++) {
9 		if (!*prefix)
10 			return 0;
11 		else if (*str != *prefix)
12 			return (unsigned char)*prefix - (unsigned char)*str;
13 	}
14 }
15 
16 /**
17  * strlcat - Append a length-limited, %NUL-terminated string to another
18  * @dest: The string to be appended to
19  * @src: The string to append to it
20  * @count: The size of the destination buffer.
21  */
22 size_t strlcat(char *dest, const char *src, size_t count)
23 {
24 	size_t dsize = strlen(dest);
25 	size_t len = strlen(src);
26 	size_t res = dsize + len;
27 
28 	DIE_IF(dsize >= count);
29 
30 	dest += dsize;
31 	count -= dsize;
32 	if (len >= count)
33 		len = count - 1;
34 
35 	memcpy(dest, src, len);
36 	dest[len] = 0;
37 
38 	return res;
39 }
40 
41 /**
42  * strlcpy - Copy a %NUL terminated string into a sized buffer
43  * @dest: Where to copy the string to
44  * @src: Where to copy the string from
45  * @size: size of destination buffer
46  *
47  * Compatible with *BSD: the result is always a valid
48  * NUL-terminated string that fits in the buffer (unless,
49  * of course, the buffer size is zero). It does not pad
50  * out the result like strncpy() does.
51  */
52 size_t strlcpy(char *dest, const char *src, size_t size)
53 {
54 	size_t ret = strlen(src);
55 
56 	if (size) {
57 		size_t len = (ret >= size) ? size - 1 : ret;
58 		memcpy(dest, src, len);
59 		dest[len] = '\0';
60 	}
61 	return ret;
62 }
63