From b4266c7e679e61c06fbb0e7a55a370ddda3d03d7 Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Fri, 29 Nov 2013 13:34:28 +0400 Subject: [PATCH] string: Add strlcat helper We will need it for btrfs subvolumes handling. Signed-off-by: Cyrill Gorcunov Signed-off-by: Pavel Emelyanov --- Makefile.config | 3 +++ include/string.h | 4 ++++ scripts/feature-tests.mak | 13 +++++++++++++ string.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/Makefile.config b/Makefile.config index f376da154..4c074a78a 100644 --- a/Makefile.config +++ b/Makefile.config @@ -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__ */' >> $@ diff --git a/include/string.h b/include/string.h index f455fbc89..8454b4dd6 100644 --- a/include/string.h +++ b/include/string.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__ */ diff --git a/scripts/feature-tests.mak b/scripts/feature-tests.mak index 25a5e6753..61e68add3 100644 --- a/scripts/feature-tests.mak +++ b/scripts/feature-tests.mak @@ -42,3 +42,16 @@ int main(void) return strlcpy(dst, src, sizeof(dst)); } endef + +define STRLCAT_TEST + +#include + +int main(void) +{ + char src[32] = "strlcat"; + char dst[32]; + + return strlcat(dst, src, sizeof(dst)); +} +endef diff --git a/string.c b/string.c index 174823908..543c64291 100644 --- a/string.c +++ b/string.c @@ -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