]> git.ipfire.org Git - telemetry.git/commitdiff
string: Add function to copy strings of a set input length
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Oct 2025 09:33:16 +0000 (09:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Oct 2025 09:33:16 +0000 (09:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/string.h

index 0af03a9941e06281cf7356e77d937768ea47f822..e8d50f5036ce92475fa9881ca5d6dc454b06684e 100644 (file)
@@ -100,6 +100,24 @@ static inline int __collecty_string_set(char* s, const size_t length, const char
        return -ENOBUFS;
 }
 
+#define collecty_string_setn(s, value, l) \
+       __collecty_string_setn(s, sizeof(s), value, l)
+
+static inline int __collecty_string_setn(char* s, const size_t length, const char* value, const size_t l) {
+       // Fail if we don't have enough space
+       if (unlikely(l >= length))
+               return -ENOBUFS;
+
+       // Copy the data
+       if (likely(l > 0))
+               memcpy(s, value, l);
+
+       // Terminate the result buffer
+       s[l] = '\0';
+
+       return l;
+}
+
 static inline void collecty_string_lstrip(char* s) {
        if (!s)
                return;