From 54d5ba67489b3fdaf437c05675eb9b1f6d085a84 Mon Sep 17 00:00:00 2001 From: Marc Pervaz Boocha Date: Thu, 7 Apr 2022 19:33:30 +0530 Subject: Initial commit --- memory/strndup.c | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 memory/strndup.c (limited to 'memory/strndup.c') diff --git a/memory/strndup.c b/memory/strndup.c new file mode 100644 index 0000000..8b369ed --- /dev/null +++ b/memory/strndup.c @@ -0,0 +1,10 @@ +#include "memory.h" + +char *strndup(char const *restrict string, size_t max_lenght) +{ + size_t lenght = strnlen(string, max_lenght) + 1; + char *duplicate = reallocarray(NULL, lenght, sizeof *duplicate); + if (duplicate != NULL) + memcpy(duplicate, string, lenght); + return duplicate; +} -- cgit v1.2.3-70-g09d2