mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] string: add strlcat support
@ 2024-10-16  8:56 Ahmad Fatoum
  2024-10-16  8:56 ` [PATCH 2/2] string: support overriding strchr/strrchr/strstr macros Ahmad Fatoum
  2024-10-18  8:52 ` [PATCH 1/2] string: add strlcat support Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-10-16  8:56 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We have a lot of kernel string functions already for use by common,
driver and board code, but we are lacking strlcat.

Add it to simplify porting code using it.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/linux/string.h |  3 +++
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 0d046f783280..4aa11555a005 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -55,6 +55,9 @@ extern char * strcat(char *, const char *);
 #ifndef __HAVE_ARCH_STRNCAT
 extern char * strncat(char *, const char *, __kernel_size_t);
 #endif
+#ifndef __HAVE_ARCH_STRLCAT
+extern __kernel_size_t strlcat(char *dest, const char *src, __kernel_size_t count);
+#endif
 #ifndef __HAVE_ARCH_STRCMP
 extern int strcmp(const char *,const char *);
 #endif
diff --git a/lib/string.c b/lib/string.c
index 50c2016c2bd8..cab543baf38d 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -270,6 +270,27 @@ char * strncat(char *dest, const char *src, size_t count)
 #endif
 EXPORT_SYMBOL(strncat);
 
+#ifndef __HAVE_ARCH_STRLCAT
+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;
+
+	/* This would be a bug */
+	BUG_ON(dsize >= count);
+
+	dest += dsize;
+	count -= dsize;
+	if (len >= count)
+		len = count-1;
+	__builtin_memcpy(dest, src, len);
+	dest[len] = 0;
+	return res;
+}
+EXPORT_SYMBOL(strlcat);
+#endif
+
 #ifndef __HAVE_ARCH_STRCMP
 /**
  * strcmp - Compare two strings
-- 
2.39.5




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-18  8:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-16  8:56 [PATCH 1/2] string: add strlcat support Ahmad Fatoum
2024-10-16  8:56 ` [PATCH 2/2] string: support overriding strchr/strrchr/strstr macros Ahmad Fatoum
2024-10-18  8:52 ` [PATCH 1/2] string: add strlcat support Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox