Second Extended Filesystem (ext2) and other improvements (#33)
[lunaix-os.git] / lunaix-os / libs / klibc / string / strcpy.c
index 12586064cd6767fe36abb89139dc4eb2f180a45a..4002d5b5e546acb788e26847e4122aa5a7a87467 100644 (file)
@@ -1,23 +1,44 @@
 #include <klibc/string.h>
+#include <lunaix/compiler.h>
 
-char*
-strcpy(char* dest, const char* src) {
+char* weak
+strcpy(char* dest, const char* src)
+{
     char c;
     unsigned int i = 0;
-    while ((c = src[i]))
-    {
+    while ((c = src[i])) {
         dest[i] = c;
         i++;
     }
-    dest[i] = '\0';
-    return dest;
+    dest[i++] = '\0';
+    return &dest[i];
 }
 
-char*
-strncpy(char* dest, const char* src, size_t n) {
+/**
+ * @brief strcpy with constrain on numbers of character.
+ *        this version is smarter than stdc, it will automatically
+ *        handle the null-terminator.
+ * 
+ * @param dest 
+ * @param src 
+ * @param n 
+ * @return char* 
+ */
+char* weak
+strncpy(char* dest, const char* src, unsigned long n)
+{
     char c;
     unsigned int i = 0;
-    while ((c = src[i]) && i < n) dest[i++] = c;
-    while (i < n) dest[i++] = 0;
+    while (i <= n && (c = src[i]))
+        dest[i++] = c;
+
+    if (!(n < i && src[i - 1])) {
+        while (i <= n)
+            dest[i++] = 0;
+    }
+    else {
+        dest[i - 1] = 0;
+    }
+    
     return dest;
 }
\ No newline at end of file