OSDN Git Service

Add strndup, written by Stefan Soucek <ssoucek@coactive.com>
authorEric Andersen <andersen@codepoet.org>
Thu, 11 Oct 2001 08:36:33 +0000 (08:36 -0000)
committerEric Andersen <andersen@codepoet.org>
Thu, 11 Oct 2001 08:36:33 +0000 (08:36 -0000)
include/string.h
libc/string/Makefile
libc/string/string.c

index 3c03c7c..13bdca9 100644 (file)
@@ -125,8 +125,7 @@ extern char *strdup (__const char *__s) __THROW __attribute_malloc__;
 /* Return a malloc'd copy of at most N bytes of STRING.  The
    resultant string is terminated even if no null terminator
    appears before STRING[N].  */
-#if 0
-//#if defined __USE_GNU
+#if defined __USE_GNU
 extern char *strndup (__const char *__string, size_t __n)
      __THROW __attribute_malloc__;
 #endif
index 748a935..7ef9000 100644 (file)
@@ -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)
index 7ccc557..94bf542 100644 (file)
@@ -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 ********************************************/