Commit b4266c7e authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Pavel Emelyanov

string: Add strlcat helper

We will need it for btrfs subvolumes handling.
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent f4e40ef7
......@@ -15,6 +15,9 @@ ifeq ($(call try-cc,$(PRLIMIT_TEST),,),y)
endif
ifeq ($(call try-cc,$(STRLCPY_TEST),,),y)
$(Q) @echo '#define CONFIG_HAS_STRLCPY' >> $@
endif
ifeq ($(call try-cc,$(STRLCAT_TEST),,),y)
$(Q) @echo '#define CONFIG_HAS_STRLCAT' >> $@
endif
$(Q) @echo '#endif /* __CR_CONFIG_H__ */' >> $@
......
......@@ -10,4 +10,8 @@
extern size_t strlcpy(char *dest, const char *src, size_t size);
#endif
#ifndef CONFIG_HAS_STRLCAT
extern size_t strlcat(char *dest, const char *src, size_t count);
#endif
#endif /* __CR_STRING_H__ */
......@@ -42,3 +42,16 @@ int main(void)
return strlcpy(dst, src, sizeof(dst));
}
endef
define STRLCAT_TEST
#include <string.h>
int main(void)
{
char src[32] = "strlcat";
char dst[32];
return strlcat(dst, src, sizeof(dst));
}
endef
......@@ -30,3 +30,31 @@ size_t strlcpy(char *dest, const char *src, size_t size)
return ret;
}
#endif
#ifndef CONFIG_HAS_STRLCAT
/**
* strlcat - Append a length-limited, %NUL-terminated string to another
* @dest: The string to be appended to
* @src: The string to append to it
* @count: The size of the destination buffer.
*/
size_t strlcat(char *dest, const char *src, size_t count)
{
size_t dsize = strlen(dest);
size_t len = strlen(src);
size_t res = dsize + len;
/*
* It's assumed that @dsize strictly
* less than count. Otherwise it's
* a bug. But we left it to a caller.
*/
dest += dsize;
count -= dsize;
if (len >= count)
len = count-1;
memcpy(dest, src, len);
dest[len] = 0;
return res;
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment