198ee79f4SPrasad Joshi
298ee79f4SPrasad Joshi /* user defined headers */
3ec52d504SLai Jiangshan #include <kvm/util.h>
498ee79f4SPrasad Joshi #include <kvm/strbuf.h>
598ee79f4SPrasad Joshi
prefixcmp(const char * str,const char * prefix)698ee79f4SPrasad Joshi int prefixcmp(const char *str, const char *prefix)
798ee79f4SPrasad Joshi {
898ee79f4SPrasad Joshi for (; ; str++, prefix++) {
998ee79f4SPrasad Joshi if (!*prefix)
1098ee79f4SPrasad Joshi return 0;
1198ee79f4SPrasad Joshi else if (*str != *prefix)
1298ee79f4SPrasad Joshi return (unsigned char)*prefix - (unsigned char)*str;
1398ee79f4SPrasad Joshi }
1498ee79f4SPrasad Joshi }
15ec52d504SLai Jiangshan
16*8f22adc4SAndre Przywara #ifndef HAVE_STRLCPY
17ec52d504SLai Jiangshan /**
18ec52d504SLai Jiangshan * strlcat - Append a length-limited, %NUL-terminated string to another
19ec52d504SLai Jiangshan * @dest: The string to be appended to
20ec52d504SLai Jiangshan * @src: The string to append to it
21ec52d504SLai Jiangshan * @count: The size of the destination buffer.
22ec52d504SLai Jiangshan */
strlcat(char * dest,const char * src,size_t count)23ec52d504SLai Jiangshan size_t strlcat(char *dest, const char *src, size_t count)
24ec52d504SLai Jiangshan {
25ec52d504SLai Jiangshan size_t dsize = strlen(dest);
26ec52d504SLai Jiangshan size_t len = strlen(src);
27ec52d504SLai Jiangshan size_t res = dsize + len;
28ec52d504SLai Jiangshan
29ec52d504SLai Jiangshan DIE_IF(dsize >= count);
30ec52d504SLai Jiangshan
31ec52d504SLai Jiangshan dest += dsize;
32ec52d504SLai Jiangshan count -= dsize;
33ec52d504SLai Jiangshan if (len >= count)
34ec52d504SLai Jiangshan len = count - 1;
35ec52d504SLai Jiangshan
36ec52d504SLai Jiangshan memcpy(dest, src, len);
37ec52d504SLai Jiangshan dest[len] = 0;
38ec52d504SLai Jiangshan
39ec52d504SLai Jiangshan return res;
40ec52d504SLai Jiangshan }
4124ed52dbSCyrill Gorcunov
4224ed52dbSCyrill Gorcunov /**
4324ed52dbSCyrill Gorcunov * strlcpy - Copy a %NUL terminated string into a sized buffer
4424ed52dbSCyrill Gorcunov * @dest: Where to copy the string to
4524ed52dbSCyrill Gorcunov * @src: Where to copy the string from
4624ed52dbSCyrill Gorcunov * @size: size of destination buffer
4724ed52dbSCyrill Gorcunov *
4824ed52dbSCyrill Gorcunov * Compatible with *BSD: the result is always a valid
4924ed52dbSCyrill Gorcunov * NUL-terminated string that fits in the buffer (unless,
5024ed52dbSCyrill Gorcunov * of course, the buffer size is zero). It does not pad
5124ed52dbSCyrill Gorcunov * out the result like strncpy() does.
5224ed52dbSCyrill Gorcunov */
strlcpy(char * dest,const char * src,size_t size)5324ed52dbSCyrill Gorcunov size_t strlcpy(char *dest, const char *src, size_t size)
5424ed52dbSCyrill Gorcunov {
5524ed52dbSCyrill Gorcunov size_t ret = strlen(src);
5624ed52dbSCyrill Gorcunov
5724ed52dbSCyrill Gorcunov if (size) {
5824ed52dbSCyrill Gorcunov size_t len = (ret >= size) ? size - 1 : ret;
5924ed52dbSCyrill Gorcunov memcpy(dest, src, len);
6024ed52dbSCyrill Gorcunov dest[len] = '\0';
6124ed52dbSCyrill Gorcunov }
6224ed52dbSCyrill Gorcunov return ret;
6324ed52dbSCyrill Gorcunov }
64*8f22adc4SAndre Przywara #endif
65