[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.

svn path=/trunk/; revision=52625
This commit is contained in:
Alex Ionescu 2011-07-11 01:13:34 +00:00
parent 67f44266b0
commit 043ba4e228

View file

@ -159,50 +159,76 @@ RtlIsDosDeviceName_U(PWSTR dos_name)
return 0; return 0;
} }
/* /*
* @implemented * @implemented
*/ */
ULONG NTAPI ULONG
RtlGetCurrentDirectory_U(ULONG MaximumLength, NTAPI
PWSTR Buffer) RtlGetCurrentDirectory_U(IN ULONG MaximumLength,
IN PWSTR Buffer)
{ {
ULONG Length; ULONG Length;
PCURDIR cd; PCURDIR CurDir;
PWSTR CurDirName;
DPRINT ("RtlGetCurrentDirectory %lu %p\n", MaximumLength, Buffer); DPRINT("RtlGetCurrentDirectory %lu %p\n", MaximumLength, Buffer);
/* Lock the PEB to get the current directory */
RtlAcquirePebLock(); RtlAcquirePebLock();
CurDir = &NtCurrentPeb()->ProcessParameters->CurrentDirectory;
cd = (PCURDIR)&(NtCurrentPeb ()->ProcessParameters->CurrentDirectory.DosPath); /* Get the buffer and character length */
Length = cd->DosPath.Length / sizeof(WCHAR); CurDirName = CurDir->DosPath.Buffer;
if (cd->DosPath.Buffer[Length - 1] == L'\\' && Length = CurDir->DosPath.Length / sizeof(WCHAR);
cd->DosPath.Buffer[Length - 2] != L':') ASSERT((CurDirName != NULL) && (Length > 0));
Length--;
DPRINT ("cd->DosPath.Buffer %S Length %lu\n", /* Check for x:\ vs x:\path\foo (note the trailing slash) */
cd->DosPath.Buffer, Length); Bytes = Length * sizeof(WCHAR);
if ((Length <= 1) || (CurDirName[Length - 2] == ":"))
if (MaximumLength / sizeof(WCHAR) > Length)
{ {
memcpy (Buffer, /* Check if caller does not have enough space */
cd->DosPath.Buffer, if (MaximumLength <= Bytes)
Length * sizeof(WCHAR)); {
Buffer[Length] = 0; /* Call has no space for it, fail, add the trailing slash */
RtlReleasePebLock();
return Bytes + sizeof(L'\\');
}
} }
else else
{ {
Length++; /* Check if caller does not have enough space */
if (MaximumLength <= Bytes)
{
/* Call has no space for it, fail */
RtlReleasePebLock();
return Bytes;
}
} }
RtlReleasePebLock (); /* Copy the buffer since we seem to have space */
RtlCopyMemory(Buffer, CurDirName, Bytes);
DPRINT ("CurrentDirectory %S\n", Buffer); /* The buffer should end with a path separator */
ASSERT(Buffer[Length - 1] == L'\\');
return (Length * sizeof(WCHAR)); /* 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 * @implemented
*/ */