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