From 2a3484a22d6a2de286debb2b8934c28745a56f04 Mon Sep 17 00:00:00 2001 From: Piotr Caban Date: Tue, 3 May 2011 14:26:08 +0200 Subject: [PATCH] msvcrt: Added _wtmpnam implementation. --- dlls/msvcrt/file.c | 64 +++++++++++++++++++++++++++++++++++++---- dlls/msvcrt/msvcrt.spec | 2 +- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index c9ad136d17..84ec729661 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -88,8 +88,10 @@ static int MSVCRT_stream_idx; /* INTERNAL: process umask */ static int MSVCRT_umask = 0; -/* INTERNAL: Static buffer for temp file name */ +/* INTERNAL: static data for tmpnam and _wtmpname functions */ +static int tmpnam_unique; static char MSVCRT_tmpname[MAX_PATH]; +static MSVCRT_wchar_t MSVCRT_wtmpname[MAX_PATH]; static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e'; static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't'; @@ -455,7 +457,7 @@ static void msvcrt_alloc_buffer(MSVCRT_FILE* file) } /* INTERNAL: Convert integer to base32 string (0-9a-v), 0 becomes "" */ -static void msvcrt_int_to_base32(int num, char *str) +static int msvcrt_int_to_base32(int num, char *str) { char *p; int n = num; @@ -475,6 +477,33 @@ static void msvcrt_int_to_base32(int num, char *str) *p += ('a' - '0' - 10); num >>= 5; } + + return digits; +} + +/* INTERNAL: wide character version of msvcrt_int_to_base32 */ +static int msvcrt_int_to_base32_w(int num, MSVCRT_wchar_t *str) +{ + MSVCRT_wchar_t *p; + int n = num; + int digits = 0; + + while (n != 0) + { + n >>= 5; + digits++; + } + p = str + digits; + *p = 0; + while (--p >= str) + { + *p = (num & 31) + '0'; + if (*p > '9') + *p += ('a' - '0' - 10); + num >>= 5; + } + + return digits; } /********************************************************************* @@ -3336,18 +3365,17 @@ void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf) */ char * CDECL MSVCRT_tmpnam(char *s) { - static int unique; char tmpstr[16]; char *p; - int count; + int count, size; if (s == 0) s = MSVCRT_tmpname; msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr); p = s + sprintf(s, "\\s%s.", tmpstr); for (count = 0; count < MSVCRT_TMP_MAX; count++) { - msvcrt_int_to_base32(unique++, tmpstr); - strcpy(p, tmpstr); + size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr); + memcpy(p, tmpstr, size); if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES && GetLastError() == ERROR_FILE_NOT_FOUND) break; @@ -3355,6 +3383,30 @@ char * CDECL MSVCRT_tmpnam(char *s) return s; } +/********************************************************************* + * _wtmpnam (MSVCRT.@) + */ +MSVCRT_wchar_t * MSVCRT_wtmpnam(MSVCRT_wchar_t *s) +{ + static const MSVCRT_wchar_t format[] = {'\\','s','%','s','.',0}; + MSVCRT_wchar_t tmpstr[16]; + MSVCRT_wchar_t *p; + int count, size; + if (s == 0) + s = MSVCRT_wtmpname; + msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr); + p = s + MSVCRT__snwprintf(s, MAX_PATH, format, tmpstr); + for (count = 0; count < MSVCRT_TMP_MAX; count++) + { + size = msvcrt_int_to_base32_w(tmpnam_unique++, tmpstr); + memcpy(p, tmpstr, size*sizeof(MSVCRT_wchar_t)); + if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES && + GetLastError() == ERROR_FILE_NOT_FOUND) + break; + } + return s; +} + /********************************************************************* * tmpfile (MSVCRT.@) */ diff --git a/dlls/msvcrt/msvcrt.spec b/dlls/msvcrt/msvcrt.spec index ca738d2d73..4e6333f06d 100644 --- a/dlls/msvcrt/msvcrt.spec +++ b/dlls/msvcrt/msvcrt.spec @@ -1192,7 +1192,7 @@ @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) # stub _wtempnam_dbg(wstr wstr long str long) -@ stub _wtmpnam(ptr) +@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam # stub _wtmpnam_s(ptr long) @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l -- 2.32.0.93.g670b81a890