Index: lib/rtl/path.c =================================================================== --- lib/rtl/path.c (revision 54527) +++ lib/rtl/path.c (working copy) @@ -878,64 +878,44 @@ { ULONG Length, Bytes; PCURDIR CurDir; - PWSTR CurDirName; + 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 */ + /* Get the Buffer and Path length */ CurDirName = CurDir->DosPath.Buffer; - Length = CurDir->DosPath.Length / sizeof(WCHAR); - ASSERT((CurDirName != NULL) && (Length > 0)); + Length = CurDir->DosPath.Length / sizeof(WCHAR); + ASSERT((CurDirName != NULL) && (Length > 0)); + + + /* Don't count the Trailing slash in X:\foo\bar\ */ + if (CurDirName[Length - 1] == L'\\' && + CurDirName[Length - 2] != L':') + Length--; + DPRINT("CurDirName %S Length %lu\n", CurDirName, Length); + + /* Check if there is enough room to store the Path */ + if (MaximumLength / sizeof(WCHAR) > Length) + { + /*Copy to the buffer just the needed characters*/ + memcpy (Buffer,CurDirName, Length * sizeof(WCHAR)); + Buffer[Length] = 0; + } + else + { + /*If there is not enough room to store X:\foo\bat let's count,also, the "dropped" trailing slash: */ + /*The function should return the number of Bytes needed to store the WHOLE string */ + Length++; + } - /* Check for x:\ vs x:\path\foo (note the trailing slash) */ - Bytes = Length * sizeof(WCHAR); - if ((Length <= 1) || (CurDirName[Length - 2] == L':')) - { - /* 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] != L':')) - { - /* 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); + Bytes = Length * sizeof(WCHAR); + DPRINT("Buffer: %S Bytes: %lu \n ", Buffer, Bytes); + return Bytes; } /*