diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 18:02:34 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 18:02:34 +0000 |
commit | fadeddfbb2aa38a980dd959b5ec1ffba7afd43cb (patch) | |
tree | a7bde6111c84ea64619656a38fba50909fa0bf60 /src/compat/strndup.c | |
parent | Initial commit. (diff) | |
download | lldpd-fadeddfbb2aa38a980dd959b5ec1ffba7afd43cb.tar.xz lldpd-fadeddfbb2aa38a980dd959b5ec1ffba7afd43cb.zip |
Adding upstream version 1.0.18.upstream/1.0.18upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/compat/strndup.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/compat/strndup.c b/src/compat/strndup.c new file mode 100644 index 0000000..1db7069 --- /dev/null +++ b/src/compat/strndup.c @@ -0,0 +1,24 @@ +/* -*- mode: c; c-file-style: "openbsd" -*- */ + +#include <stdlib.h> +#include <string.h> +#include "compat.h" + +/* + * Similar to `strdup()` but copies at most n bytes. + */ +char * +strndup(const char *string, size_t maxlen) +{ + char *result; + /* We may use `strnlen()` but it may be unavailable. */ + const char *end = memchr(string, '\0', maxlen); + size_t len = end ? (size_t)(end - string) : maxlen; + + result = malloc(len + 1); + if (!result) return 0; + + memcpy(result, string, len); + result[len] = '\0'; + return result; +} |