Fixed GetTempPathA/W and GetCurrentDirectoryA/W. Patch by tinus, Eric Kohl and me, fixes bug #482

svn path=/trunk/; revision=13313
This commit is contained in:
Thomas Bluemel 2005-01-26 14:15:38 +00:00
parent 77a7f19e4f
commit a2ea8e6fef

View file

@ -37,6 +37,7 @@ GetCurrentDirectoryA (
{ {
ANSI_STRING AnsiString; ANSI_STRING AnsiString;
UNICODE_STRING UnicodeString; UNICODE_STRING UnicodeString;
ULONG Length;
/* allocate buffer for unicode string */ /* allocate buffer for unicode string */
UnicodeString.Length = 0; UnicodeString.Length = 0;
@ -56,33 +57,48 @@ GetCurrentDirectoryA (
{ {
UnicodeString.Buffer = NULL; UnicodeString.Buffer = NULL;
} }
/* get current directory */ /* get current directory */
UnicodeString.Length = RtlGetCurrentDirectory_U (UnicodeString.MaximumLength, UnicodeString.Length = RtlGetCurrentDirectory_U (UnicodeString.MaximumLength,
UnicodeString.Buffer); UnicodeString.Buffer);
if (nBufferLength > 0) DPRINT("UnicodeString.Buffer %wZ\n", &UnicodeString);
/* convert unicode string to ansi (or oem) */
if (bIsFileApiAnsi)
{ {
DPRINT("UnicodeString.Buffer %wZ\n", &UnicodeString); Length = RtlUnicodeStringToAnsiSize (&UnicodeString);
if (Length > nBufferLength)
/* convert unicode string to ansi (or oem) */ {
if (bIsFileApiAnsi) RtlFreeHeap (RtlGetProcessHeap (),
RtlUnicodeStringToAnsiString (&AnsiString, 0,
&UnicodeString, UnicodeString.Buffer);
FALSE); return Length-1;
else }
RtlUnicodeStringToOemString (&AnsiString, RtlUnicodeStringToAnsiString (&AnsiString,
&UnicodeString, &UnicodeString,
FALSE); FALSE);
DPRINT("AnsiString.Buffer %s\n", AnsiString.Buffer);
/* free unicode string */
RtlFreeHeap (RtlGetProcessHeap (),
0,
UnicodeString.Buffer);
} }
else else
{ {
AnsiString.Length = UnicodeString.Length / sizeof(WCHAR); Length = RtlUnicodeStringToOemSize (&UnicodeString);
if (Length > nBufferLength)
{
RtlFreeHeap (RtlGetProcessHeap (),
0,
UnicodeString.Buffer);
return Length-1;
}
RtlUnicodeStringToOemString (&AnsiString,
&UnicodeString,
FALSE);
} }
DPRINT("AnsiString.Buffer %s\n", AnsiString.Buffer);
/* free unicode string */
RtlFreeHeap (RtlGetProcessHeap (),
0,
UnicodeString.Buffer);
return AnsiString.Length; return AnsiString.Length;
} }
@ -184,6 +200,7 @@ GetTempPathA (
{ {
UNICODE_STRING UnicodeString; UNICODE_STRING UnicodeString;
ANSI_STRING AnsiString; ANSI_STRING AnsiString;
DWORD Length;
AnsiString.Length = 0; AnsiString.Length = 0;
AnsiString.MaximumLength = nBufferLength; AnsiString.MaximumLength = nBufferLength;
@ -191,30 +208,50 @@ GetTempPathA (
/* initialize allocate unicode string */ /* initialize allocate unicode string */
UnicodeString.Length = 0; UnicodeString.Length = 0;
UnicodeString.MaximumLength = nBufferLength * sizeof(WCHAR); if(nBufferLength > 0)
UnicodeString.Buffer = RtlAllocateHeap (RtlGetProcessHeap(), {
0, UnicodeString.MaximumLength = (nBufferLength + 1) * sizeof(WCHAR);
UnicodeString.MaximumLength); UnicodeString.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
0,
UnicodeString.Length = GetTempPathW (nBufferLength, UnicodeString.MaximumLength);
UnicodeString.Buffer) * sizeof(WCHAR); if (UnicodeString.Buffer == NULL)
{
/* convert unicode string to ansi (or oem) */ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
if (bIsFileApiAnsi) return 0;
RtlUnicodeStringToAnsiString (&AnsiString, }
&UnicodeString, }
FALSE);
else else
RtlUnicodeStringToOemString (&AnsiString, {
&UnicodeString, UnicodeString.MaximumLength = 0;
FALSE); UnicodeString.Buffer = NULL;
}
Length = GetTempPathW (nBufferLength,
UnicodeString.Buffer);
if (nBufferLength >= Length)
{
/* only touch the buffer if the supplied buffer length is at least
the length that GetTempPathW returned! */
UnicodeString.Length = Length * sizeof(WCHAR);
/* convert unicode string to ansi (or oem) */
if (bIsFileApiAnsi)
RtlUnicodeStringToAnsiString (&AnsiString,
&UnicodeString,
FALSE);
else
RtlUnicodeStringToOemString (&AnsiString,
&UnicodeString,
FALSE);
}
/* free unicode string buffer */ /* free unicode string buffer */
RtlFreeHeap (RtlGetProcessHeap (), RtlFreeHeap (RtlGetProcessHeap (),
0, 0,
UnicodeString.Buffer); UnicodeString.Buffer);
return AnsiString.Length; return Length;
} }
@ -229,42 +266,67 @@ GetTempPathW (
) )
{ {
UNICODE_STRING Name; UNICODE_STRING Name;
UNICODE_STRING Value; PUNICODE_STRING Value;
PTEB Teb;
DWORD Length;
NTSTATUS Status; NTSTATUS Status;
Value.Length = 0; Teb = NtCurrentTeb();
Value.MaximumLength = (nBufferLength - 1) * sizeof(WCHAR); Teb->StaticUnicodeString.Length = 0;
Value.Buffer = lpBuffer; Teb->StaticUnicodeString.MaximumLength = MAX_PATH * sizeof(WCHAR);
Teb->StaticUnicodeString.Buffer = Teb->StaticUnicodeBuffer;
Value = &Teb->StaticUnicodeString;
RtlRosInitUnicodeStringFromLiteral (&Name, RtlRosInitUnicodeStringFromLiteral (&Name,
L"TMP"); L"TMP");
Status = RtlQueryEnvironmentVariable_U (NULL, Status = RtlQueryEnvironmentVariable_U (NULL,
&Name, &Name,
&Value); Value);
if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL) if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
{ {
RtlRosInitUnicodeStringFromLiteral (&Name, RtlRosInitUnicodeStringFromLiteral (&Name,
L"TEMP"); L"TEMP");
Status = RtlQueryEnvironmentVariable_U (NULL, Status = RtlQueryEnvironmentVariable_U (NULL,
&Name, &Name,
&Value); Value);
if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL) if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
{ {
Value.Length = RtlGetCurrentDirectory_U (Value.MaximumLength, Value->Length = RtlGetCurrentDirectory_U(Value->MaximumLength,
Value.Buffer); Value->Buffer);
} }
} }
if (NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
lpBuffer[Value.Length / sizeof(WCHAR)] = L'\\'; SetLastError(RtlNtStatusToDosError(Status));
lpBuffer[Value.Length / sizeof(WCHAR) + 1] = 0; return 0;
} }
return Value.Length / sizeof(WCHAR) + 1; Length = Value->Length / sizeof(WCHAR) + 1;
if (nBufferLength < Value->Length / sizeof(WCHAR) + 2)
Length++;
if (lpBuffer != NULL)
{
if (nBufferLength < Value->Length / sizeof(WCHAR) + 2)
{
memcpy (lpBuffer,
Value->Buffer,
nBufferLength * sizeof(WCHAR));
}
else
{
memcpy (lpBuffer,
Value->Buffer,
Value->Length);
lpBuffer[Value->Length / sizeof(WCHAR)] = L'\\';
lpBuffer[Value->Length / sizeof(WCHAR) + 1] = 0;
}
}
return Length;
} }