aboutsummaryrefslogtreecommitdiff
path: root/lib/string.c
diff options
context:
space:
mode:
authorThierry Reding2019-04-15 11:32:14 +0200
committerTom Warren2019-06-05 09:16:32 -0700
commit0c4e2658e8e8489956e48a6c9842c5d21b9593fe (patch)
treec4f1dfe07c5ef92c12f9dd5c6463bd2b33653612 /lib/string.c
parentebf30e8451f457aebc8232a674fa75823dd10d49 (diff)
lib: Implement strndup()
Signed-off-by: Thierry Reding <treding@nvidia.com> Signed-off-by: Tom Warren <twarren@nvidia.com>
Diffstat (limited to 'lib/string.c')
-rw-r--r--lib/string.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c
index af17c16f616..9b779ddc3bb 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -326,6 +326,29 @@ char * strdup(const char *s)
}
#endif
+char * strndup(const char *s, size_t n)
+{
+ size_t len;
+ char *new;
+
+ if (s == NULL)
+ return NULL;
+
+ len = strlen(s);
+
+ if (n < len)
+ len = n;
+
+ new = malloc(len + 1);
+ if (new == NULL)
+ return NULL;
+
+ strncpy(new, s, len);
+ new[len] = '\0';
+
+ return new;
+}
+
#ifndef __HAVE_ARCH_STRSPN
/**
* strspn - Calculate the length of the initial substring of @s which only