]> git.mdlowis.com Git - proto/aos.git/commitdiff
added strmcat
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 11 Dec 2020 18:06:50 +0000 (13:06 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 11 Dec 2020 18:06:50 +0000 (13:06 -0500)
lib/a/strmcat.c [new file with mode: 0644]

diff --git a/lib/a/strmcat.c b/lib/a/strmcat.c
new file mode 100644 (file)
index 0000000..1cefc62
--- /dev/null
@@ -0,0 +1,32 @@
+#include <liba.h>
+
+char* strmcat(char* first, ...)
+{
+    va_list args;
+    /* calculate the length of the final string */
+    size_t len = strlen(first);
+    va_start(args, first);
+    for (char* s = NULL; (s = va_arg(args, char*));)
+    {
+        len += strlen(s);
+    }
+    va_end(args);
+    /* allocate the final string and copy the args into it */
+    char *str  = malloc(len+1), *curr = str;
+    while (first && *first)
+    {
+        *(curr++) = *(first++);
+    }
+    va_start(args, first);
+    for (char* s = NULL; (s = va_arg(args, char*));)
+    {
+        while (s && *s)
+        {
+            *(curr++) = *(s++);
+        }
+    }
+    va_end(args);
+    /* null terminate and return */
+    *curr = '\0';
+    return str;
+}