changeset 226:f5b2d14c531f

lib: strncpy implementation Another string manipulation function...taken from Linux's lib/string.c Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sat, 10 Jan 2009 14:29:49 -0500
parents b59bb5c43a04
children 9940f9a7a49f
files include/nucleus.h lib/string.c
diffstat 2 files changed, 27 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/include/nucleus.h	Fri Jan 09 16:35:55 2009 -0500
+++ b/include/nucleus.h	Sat Jan 10 14:29:49 2009 -0500
@@ -47,6 +47,7 @@
 extern int strcmp(const char *cs, const char *ct);
 extern int strncmp(const char *cs, const char *ct, int len);
 extern int strcasecmp(const char *s1, const char *s2);
+extern char *strncpy(char *dest, const char *src, size_t count);
 
 static inline unsigned char toupper(unsigned char c)
 {
--- a/lib/string.c	Fri Jan 09 16:35:55 2009 -0500
+++ b/lib/string.c	Sat Jan 10 14:29:49 2009 -0500
@@ -64,3 +64,29 @@
         } while (c1 == c2 && c1 != 0);
         return c1 - c2;
 }
+
+/**
+ * strncpy - Copy a length-limited, %NUL-terminated string
+ * @dest: Where to copy the string to
+ * @src: Where to copy the string from
+ * @count: The maximum number of bytes to copy
+ *
+ * The result is not %NUL-terminated if the source exceeds
+ * @count bytes.
+ *
+ * In the case where the length of @src is less than  that  of
+ * count, the remainder of @dest will be padded with %NUL.
+ *
+ */
+char *strncpy(char *dest, const char *src, size_t count)
+{
+	char *tmp = dest;
+
+	while (count) {
+		if ((*tmp = *src) != 0)
+			src++;
+		tmp++;
+		count--;
+	}
+	return dest;
+}