strncpy does add NUL bytes to fill dest buffer completely,
authorGé van Geldorp <ge@gse.nl>
Wed, 28 Jan 2004 08:51:09 +0000 (08:51 +0000)
committerGé van Geldorp <ge@gse.nl>
Wed, 28 Jan 2004 08:51:09 +0000 (08:51 +0000)
however lstrcpyn does not.

svn path=/trunk/; revision=7893

reactos/lib/kernel32/string/lstring.c
reactos/lib/string/i386/tcsncpy.h
reactos/lib/string/tcsncpy.h

index dddc150..5ec20d0 100644 (file)
@@ -50,7 +50,25 @@ lstrcpynA(
          int iMaxLength
          )
 {
-       return strncpy(lpString1,lpString2,iMaxLength);
+  /* Can't use strncpy, because strncpy will fill unused bytes in
+     lpString1 with NUL bytes while lstrcpynA doesn't */
+
+  if (0 != iMaxLength)
+    {
+      char *d = lpString1;
+      const char *s = lpString2;
+
+      do
+        {
+          if (0 == (*d++ = *s++))
+            {
+              break;
+            }
+        }
+      while(0 != --iMaxLength);
+    }
+
+  return lpString1;
 }
 
 
@@ -134,7 +152,25 @@ lstrcpynW(
     int iMaxLength
     )
 {
-       return wcsncpy(lpString1,lpString2,iMaxLength);
+  /* Can't use wcsncpy, because wcsncpy will fill unused bytes in
+     lpString1 with NUL bytes while lstrcpynW doesn't */
+
+  if (0 != iMaxLength)
+    {
+      WCHAR *d = lpString1;
+      const WCHAR *s = lpString2;
+
+      do
+        {
+          if (0 == (*d++ = *s++))
+            {
+              break;
+            }
+        }
+      while(0 != --iMaxLength);
+    }
+
+  return lpString1;
 }
 
 
index 93a484a..8159b00 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tcsncpy.h,v 1.2 2004/01/27 21:43:47 gvg Exp $
+/* $Id: tcsncpy.h,v 1.3 2004/01/28 08:51:09 gvg Exp $
  */
 
 #include "tchar.h"
@@ -22,6 +22,7 @@ _tcsncpy:
  _tstos
  test %_treg(a), %_treg(a)
  jnz  .L1
+ rep  _tstos
 
 .L2:
  mov  0x0C(%esp), %eax
index 03b363b..70e63dc 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: tcsncpy.h,v 1.2 2004/01/27 21:43:47 gvg Exp $
+/* $Id: tcsncpy.h,v 1.3 2004/01/28 08:51:09 gvg Exp $
  */
 
 #include <stddef.h>
@@ -15,6 +15,7 @@ _TCHAR * _tcsncpy(_TCHAR * dst, const _TCHAR * src, size_t n)
   {
    if((*d ++ = *s ++) == 0)
    {
+    while (-- n != 0) *d ++ = 0;
     break;
    }
   }