From bef22c1887f4b984969e2e2b5082bd42a16d661a Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 11 Oct 2001 08:36:33 +0000 Subject: Add strndup, written by Stefan Soucek --- libc/string/Makefile | 2 +- libc/string/string.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) (limited to 'libc') diff --git a/libc/string/Makefile b/libc/string/Makefile index 748a935eb..7ef900041 100644 --- a/libc/string/Makefile +++ b/libc/string/Makefile @@ -25,7 +25,7 @@ include $(TOPDIR)Rules.mak MSRC=string.c MOBJ=strlen.o strcat.o strcpy.o strchr.o strcmp.o strncat.o strncpy.o \ - strncmp.o strrchr.o strdup.o memcpy.o memccpy.o memset.o \ + strncmp.o strrchr.o strdup.o strndup.o memcpy.o memccpy.o memset.o \ memmove.o memcmp.o memchr.o ffs.o strnlen.o strxfrm.o stpcpy.o stpncpy.o ifeq ($(HAS_LOCALE),true) diff --git a/libc/string/string.c b/libc/string/string.c index 7ccc55733..94bf542e4 100644 --- a/libc/string/string.c +++ b/libc/string/string.c @@ -274,6 +274,25 @@ char *strdup(const char *str) } #endif +/********************** Function strndup ************************************/ +#ifdef L_strndup +char *strndup(const char *str, size_t len) +{ + register size_t n; + register char *dst; + + n = strlen(str); + if (len < n) + n = len; + dst = (char *) malloc(n+1); + if (dst) { + memcpy(dst, str, n); + dst[n] = '\0'; + } + return dst; +} +#endif + /********************** Function memcpy ************************************/ #ifdef L_memcpy @@ -409,5 +428,4 @@ int ffs(int x) } #endif - /********************** THE END ********************************************/ -- cgit v1.2.3