[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[tyndur-devel] [PATCH 3/5] libc: strndup()
+ libc: strndup() implementiert
Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
src/include/string.h | 1 +
src/lib/string/strdup.c | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/src/include/string.h b/src/include/string.h
index dd11385..c7a6983 100644
--- a/src/include/string.h
+++ b/src/include/string.h
@@ -78,6 +78,7 @@ void itoa(unsigned int n, char* s, unsigned int base);
char* index(const char* p, int ch);
char* strdup(const char* str);
+char* strndup(const char* str, size_t n);
char *strpbrk(const char *s1, const char *s2);
diff --git a/src/lib/string/strdup.c b/src/lib/string/strdup.c
index 18d19eb..c4e139c 100644
--- a/src/lib/string/strdup.c
+++ b/src/lib/string/strdup.c
@@ -45,3 +45,21 @@ strdup(str)
memcpy(copy, str, len);
return (copy);
}
+
+char* strndup(const char* s, size_t n)
+{
+ char* res;
+ size_t len = strlen(s);
+
+ if (len > n) {
+ len = n;
+ }
+
+ res = malloc(len + 1);
+ if (res) {
+ strncpy(res, s, len);
+ res[len] = '\0';
+ }
+
+ return res;
+}
--
2.1.4