Commit 289613a5 authored by Alexandre Julliard's avatar Alexandre Julliard

Make sure GetTempFileName never returns 0 on success.

parent b107b928
......@@ -1064,17 +1064,44 @@ BOOL WINAPI GetFileTime( HANDLE hFile, FILETIME *lpCreationTime,
/***********************************************************************
* FILE_GetTempFileName : utility for GetTempFileName
* GetTempFileNameA (KERNEL32.@)
*/
UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
LPSTR buffer)
{
UNICODE_STRING pathW, prefixW;
WCHAR bufferW[MAX_PATH];
UINT ret;
if ( !path || !prefix || !buffer )
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
RtlCreateUnicodeStringFromAsciiz(&pathW, path);
RtlCreateUnicodeStringFromAsciiz(&prefixW, prefix);
ret = GetTempFileNameW(pathW.Buffer, prefixW.Buffer, unique, bufferW);
if (ret)
WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
RtlFreeUnicodeString(&pathW);
RtlFreeUnicodeString(&prefixW);
return ret;
}
/***********************************************************************
* GetTempFileNameW (KERNEL32.@)
*/
static UINT FILE_GetTempFileName( LPCWSTR path, LPCWSTR prefix, UINT unique,
UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
LPWSTR buffer )
{
static UINT unique_temp;
static const WCHAR formatW[] = {'%','0','4','x','.','t','m','p',0};
DOS_FULL_NAME full_name;
int i;
LPWSTR p;
UINT num;
char buf[20];
if ( !path || !prefix || !buffer )
{
......@@ -1082,29 +1109,31 @@ static UINT FILE_GetTempFileName( LPCWSTR path, LPCWSTR prefix, UINT unique,
return 0;
}
if (!unique_temp) unique_temp = time(NULL) & 0xffff;
num = unique ? (unique & 0xffff) : (unique_temp++ & 0xffff);
strcpyW( buffer, path );
p = buffer + strlenW(buffer);
/* add a \, if there isn't one and path is more than just the drive letter ... */
if ( !((strlenW(buffer) == 2) && (buffer[1] == ':'))
&& ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
&& ((p == buffer) || (p[-1] != '\\'))) *p++ = '\\';
for (i = 3; (i > 0) && (*prefix); i--) *p++ = *prefix++;
sprintf( buf, "%04x.tmp", num );
MultiByteToWideChar(CP_ACP, 0, buf, -1, p, 20);
unique &= 0xffff;
/* Now try to create it */
if (!unique)
if (unique) sprintfW( p, formatW, unique );
else
{
/* get a "random" unique number and try to create the file */
HANDLE handle;
UINT num = GetTickCount() & 0xffff;
if (!num) num = 1;
unique = num;
do
{
HANDLE handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
sprintfW( p, formatW, unique );
handle = CreateFileW( buffer, GENERIC_WRITE, 0, NULL,
CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
if (handle != INVALID_HANDLE_VALUE)
{ /* We created it */
TRACE("created %s\n", debugstr_w(buffer) );
......@@ -1114,10 +1143,8 @@ static UINT FILE_GetTempFileName( LPCWSTR path, LPCWSTR prefix, UINT unique,
if (GetLastError() != ERROR_FILE_EXISTS &&
GetLastError() != ERROR_SHARING_VIOLATION)
break; /* No need to go on */
num++;
sprintf( buf, "%04x.tmp", num );
MultiByteToWideChar(CP_ACP, 0, buf, -1, p, 20);
} while (num != (unique & 0xffff));
if (!(++unique & 0xffff)) unique = 1;
} while (unique != num);
}
/* Get the full path name */
......@@ -1132,45 +1159,7 @@ static UINT FILE_GetTempFileName( LPCWSTR path, LPCWSTR prefix, UINT unique,
debugstr_w(buffer) );
}
TRACE("returning %s\n", debugstr_w(buffer) );
return unique ? unique : num;
}
/***********************************************************************
* GetTempFileNameA (KERNEL32.@)
*/
UINT WINAPI GetTempFileNameA( LPCSTR path, LPCSTR prefix, UINT unique,
LPSTR buffer)
{
UNICODE_STRING pathW, prefixW;
WCHAR bufferW[MAX_PATH];
UINT ret;
if ( !path || !prefix || !buffer )
{
SetLastError( ERROR_INVALID_PARAMETER );
return 0;
}
RtlCreateUnicodeStringFromAsciiz(&pathW, path);
RtlCreateUnicodeStringFromAsciiz(&prefixW, prefix);
ret = GetTempFileNameW(pathW.Buffer, prefixW.Buffer, unique, bufferW);
if (ret)
WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, MAX_PATH, NULL, NULL);
RtlFreeUnicodeString(&pathW);
RtlFreeUnicodeString(&prefixW);
return ret;
}
/***********************************************************************
* GetTempFileNameW (KERNEL32.@)
*/
UINT WINAPI GetTempFileNameW( LPCWSTR path, LPCWSTR prefix, UINT unique,
LPWSTR buffer )
{
return FILE_GetTempFileName( path, prefix, unique, buffer );
return unique;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment