Author: ion
Date: Mon Jul 11 01:13:34 2011
New Revision: 52625

URL: http://svn.reactos.org/svn/reactos?rev=52625&view=rev
Log:
[NTDLL]: Reimplement RtlGetCurrentDirectory_U... I couldn't even understand 
what the old version was doing. This one is at least commented and makes more 
sense.

Modified:
    trunk/reactos/lib/rtl/path.c

Modified: trunk/reactos/lib/rtl/path.c
URL: 
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/rtl/path.c?rev=52625&r1=52624&r2=52625&view=diff
==============================================================================
--- trunk/reactos/lib/rtl/path.c [iso-8859-1] (original)
+++ trunk/reactos/lib/rtl/path.c [iso-8859-1] Mon Jul 11 01:13:34 2011
@@ -159,49 +159,75 @@
     return 0;
 }
 
-
 /*
  * @implemented
  */
-ULONG NTAPI
-RtlGetCurrentDirectory_U(ULONG MaximumLength,
-                        PWSTR Buffer)
-{
-       ULONG Length;
-       PCURDIR cd;
-
-       DPRINT ("RtlGetCurrentDirectory %lu %p\n", MaximumLength, Buffer);
-
-       RtlAcquirePebLock();
-
-       cd = (PCURDIR)&(NtCurrentPeb 
()->ProcessParameters->CurrentDirectory.DosPath);
-       Length = cd->DosPath.Length / sizeof(WCHAR);
-       if (cd->DosPath.Buffer[Length - 1] == L'\\' &&
-           cd->DosPath.Buffer[Length - 2] != L':')
-               Length--;
-
-       DPRINT ("cd->DosPath.Buffer %S Length %lu\n",
-               cd->DosPath.Buffer, Length);
-
-       if (MaximumLength / sizeof(WCHAR) > Length)
-       {
-               memcpy (Buffer,
-                       cd->DosPath.Buffer,
-                       Length * sizeof(WCHAR));
-               Buffer[Length] = 0;
-       }
-       else
-       {
-               Length++;
-       }
-
-       RtlReleasePebLock ();
-
-       DPRINT ("CurrentDirectory %S\n", Buffer);
-
-       return (Length * sizeof(WCHAR));
-}
-
+ULONG
+NTAPI
+RtlGetCurrentDirectory_U(IN ULONG MaximumLength,
+                         IN PWSTR Buffer)
+{
+    ULONG Length;
+    PCURDIR CurDir;
+    PWSTR CurDirName;
+    DPRINT("RtlGetCurrentDirectory %lu %p\n", MaximumLength, Buffer);
+
+    /* Lock the PEB to get the current directory */
+    RtlAcquirePebLock();
+    CurDir = &NtCurrentPeb()->ProcessParameters->CurrentDirectory;
+
+    /* Get the buffer and character length */
+    CurDirName = CurDir->DosPath.Buffer;
+    Length = CurDir->DosPath.Length / sizeof(WCHAR);
+    ASSERT((CurDirName != NULL) && (Length > 0));
+
+    /* Check for x:\ vs x:\path\foo (note the trailing slash) */
+    Bytes = Length * sizeof(WCHAR);
+    if ((Length <= 1) || (CurDirName[Length - 2] == ":"))
+    {
+        /* Check if caller does not have enough space */
+        if (MaximumLength <= Bytes)
+        {
+            /* Call has no space for it, fail, add the trailing slash */
+            RtlReleasePebLock();
+            return Bytes + sizeof(L'\\');
+        }
+    }
+    else
+    {
+        /* Check if caller does not have enough space */
+        if (MaximumLength <= Bytes)
+        {
+            /* Call has no space for it, fail */
+            RtlReleasePebLock();
+            return Bytes;
+        }
+    }
+
+    /* Copy the buffer since we seem to have space */
+    RtlCopyMemory(Buffer, CurDirName, Bytes);
+
+    /* The buffer should end with a path separator */
+    ASSERT(Buffer[Length - 1] == L'\\');
+
+    /* Again check for our two cases (drive root vs path) */
+    if ((Length <= 1) || (Buffer[Length - 2] != ":"))
+    {
+        /* Replace the trailing slash with a null */
+        Buffer[Length - 1] = UNICODE_NULL;
+        --Length;
+    }
+    else
+    {
+        /* Append the null char since there's no trailing slash */
+        Buffer[Length] = UNICODE_NULL;
+    }
+
+    /* Release PEB lock */
+    RtlReleasePebLock();
+    DPRINT("CurrentDirectory %S\n", Buffer);
+    return Length * sizeof(WCHAR);
+}
 
 /*
  * @implemented


Reply via email to