From a1d3b8fe9282488e4b94a306659c6af02eda0e41 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 15 Jun 2024 19:48:21 +0200 Subject: [PATCH] common/str.c, include/str.h: introduce str_concat() Imported from FTY/DMF branch, aggregated from commits: * d5237b61f1112d6dd01f299b2f836c0974798e5f * 27cbd4d3e2cbac93c6e47a772f4e91f84749b9c6 Currently has no consumers in upstreamed code, though. Signed-off-by: Jim Klimov --- common/str.c | 39 +++++++++++++++++++++++++++++++++++++++ include/str.h | 4 ++++ 2 files changed, 43 insertions(+) diff --git a/common/str.c b/common/str.c index 295f450804..d2c83fb2da 100644 --- a/common/str.c +++ b/common/str.c @@ -34,6 +34,8 @@ #include /* for strncasecmp() and strcasecmp() */ #endif +#include /* get the va_* routines */ + #include "nut_stdint.h" #include "str.h" @@ -627,3 +629,40 @@ int str_ends_with(const char *s, const char *suff) { return (slen >= sufflen) && (!memcmp(s + slen - sufflen, suff, sufflen)); } + +/* Based on code by "mmdemirbas" posted "Jul 9 '12 at 11:41" to forum page + * http://stackoverflow.com/questions/8465006/how-to-concatenate-2-strings-in-c + * This concatenates the given number of strings into one freshly allocated + * heap object; NOTE that it is up to the caller to free the object afterwards. + */ +char * str_concat(size_t count, ...) +{ + va_list ap; + size_t i, len, null_pos; + char* merged = NULL; + + /* Find required length to store merged string */ + va_start(ap, count); + len = 1; /* room for '\0' in the end */ + for(i=0 ; i