Browse Source

I just wrote a stpncpy() since someone wanted it

Eric Andersen 24 years ago
parent
commit
1459bf3832
3 changed files with 20 additions and 1 deletions
  1. 2 0
      include/string.h
  2. 1 1
      libc/string/Makefile
  3. 17 0
      libc/string/string.c

+ 2 - 0
include/string.h

@@ -22,6 +22,8 @@ extern char *strcpy __P ((char *__restrict __dest,
 			  __const char *__restrict __src));
 extern char *stpcpy __P ((char *__restrict __dest,
 			  __const char *__restrict __src));
+extern char *stpncpy __P ((char *__restrict __dest,
+			   __const char *__restrict __src, size_t __n));
 /* Copy no more than N characters of SRC to DEST.  */
 extern char *strncpy __P ((char *__restrict __dest,
 			   __const char *__restrict __src, size_t __n));

+ 1 - 1
libc/string/Makefile

@@ -26,7 +26,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 \
-	memmove.o memcmp.o memchr.o ffs.o strnlen.o strxfrm.o stpcpy.o
+	memmove.o memcmp.o memchr.o ffs.o strnlen.o strxfrm.o stpcpy.o stpncpy.o
 
 ifeq ($(HAS_LOCALE),true)
 	MOBJ += strcoll.o

+ 17 - 0
libc/string/string.c

@@ -79,6 +79,23 @@ char *stpcpy(char *dst, const char *src)
 }
 #endif
 
+/********************** Function stpncpy ************************************/
+
+#ifdef L_stpncpy
+char *stpncpy(char *dst, const char *src, size_t len)
+{
+	while (len--) {
+		if (*src)
+			*dst++ = *src++;
+		else
+			*dst++ = '\0';
+	}
+
+	return dst;
+}
+#endif
+
+
 /********************** Function strcmp ************************************/
 
 #ifdef L_strcmp