From 77feb6938d98df205a386ad9a11a3106756168a5 Mon Sep 17 00:00:00 2001 From: Piotr Caban Date: Tue, 22 Dec 2015 17:47:21 +0100 Subject: [PATCH] msvcrt: Add _wtmpnam_s implementation. Signed-off-by: Piotr Caban Signed-off-by: Alexandre Julliard --- .../api-ms-win-crt-stdio-l1-1-0.spec | 2 +- dlls/msvcr100/msvcr100.spec | 4 +- dlls/msvcr110/msvcr110.spec | 4 +- dlls/msvcr120/msvcr120.spec | 4 +- dlls/msvcr120_app/msvcr120_app.spec | 2 +- dlls/msvcr70/msvcr70.spec | 2 +- dlls/msvcr71/msvcr71.spec | 2 +- dlls/msvcr80/msvcr80.spec | 4 +- dlls/msvcr90/msvcr90.spec | 4 +- dlls/msvcrt/file.c | 76 ++++++++++++++----- dlls/msvcrt/msvcrt.spec | 4 +- dlls/ucrtbase/ucrtbase.spec | 4 +- 12 files changed, 77 insertions(+), 35 deletions(-) diff --git a/dlls/api-ms-win-crt-stdio-l1-1-0/api-ms-win-crt-stdio-l1-1-0.spec b/dlls/api-ms-win-crt-stdio-l1-1-0/api-ms-win-crt-stdio-l1-1-0.spec index 5e0c3c9b6bf..223aa7c3c22 100644 --- a/dlls/api-ms-win-crt-stdio-l1-1-0/api-ms-win-crt-stdio-l1-1-0.spec +++ b/dlls/api-ms-win-crt-stdio-l1-1-0/api-ms-win-crt-stdio-l1-1-0.spec @@ -111,7 +111,7 @@ @ cdecl _wsopen_s(ptr wstr long long long) ucrtbase._wsopen_s @ cdecl _wtempnam(wstr wstr) ucrtbase._wtempnam @ cdecl _wtmpnam(ptr) ucrtbase._wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam_s(ptr long) ucrtbase._wtmpnam_s @ cdecl clearerr(ptr) ucrtbase.clearerr @ stub clearerr_s @ cdecl fclose(ptr) ucrtbase.fclose diff --git a/dlls/msvcr100/msvcr100.spec b/dlls/msvcr100/msvcr100.spec index 7fd53eac006..c87a03d82af 100644 --- a/dlls/msvcr100/msvcr100.spec +++ b/dlls/msvcr100/msvcr100.spec @@ -1607,8 +1607,8 @@ @ cdecl _wstrtime_s(ptr long) @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam +@ cdecl _wtmpnam_s(ptr long) MSVCRT__wtmpnam_s @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l @ cdecl _wtoi(wstr) MSVCRT__wtoi diff --git a/dlls/msvcr110/msvcr110.spec b/dlls/msvcr110/msvcr110.spec index 8c5e69402cd..c1bf3690538 100644 --- a/dlls/msvcr110/msvcr110.spec +++ b/dlls/msvcr110/msvcr110.spec @@ -1965,8 +1965,8 @@ @ cdecl _wstrtime_s(ptr long) @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam +@ cdecl _wtmpnam_s(ptr long) MSVCRT__wtmpnam_s @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l @ cdecl _wtoi(wstr) MSVCRT__wtoi diff --git a/dlls/msvcr120/msvcr120.spec b/dlls/msvcr120/msvcr120.spec index 56f213b6fcc..3bddaf7ba09 100644 --- a/dlls/msvcr120/msvcr120.spec +++ b/dlls/msvcr120/msvcr120.spec @@ -1989,8 +1989,8 @@ @ cdecl _wstrtime_s(ptr long) @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam +@ cdecl _wtmpnam_s(ptr long) MSVCRT__wtmpnam_s @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l @ cdecl _wtoi(wstr) MSVCRT__wtoi diff --git a/dlls/msvcr120_app/msvcr120_app.spec b/dlls/msvcr120_app/msvcr120_app.spec index dd62c6a89b1..5883d326223 100644 --- a/dlls/msvcr120_app/msvcr120_app.spec +++ b/dlls/msvcr120_app/msvcr120_app.spec @@ -1656,7 +1656,7 @@ @ cdecl _wstrtime_s(ptr long) msvcr120._wstrtime_s @ cdecl _wtempnam(wstr wstr) msvcr120._wtempnam @ cdecl _wtmpnam(ptr) msvcr120._wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam_s(ptr long) msvcr120._wtmpnam_s @ cdecl _wtof(wstr) msvcr120._wtof @ cdecl _wtof_l(wstr ptr) msvcr120._wtof_l @ cdecl _wtoi(wstr) msvcr120._wtoi diff --git a/dlls/msvcr70/msvcr70.spec b/dlls/msvcr70/msvcr70.spec index 3382652a4f4..026d6579513 100644 --- a/dlls/msvcr70/msvcr70.spec +++ b/dlls/msvcr70/msvcr70.spec @@ -668,7 +668,7 @@ @ cdecl _wstrtime(ptr) MSVCRT__wstrtime @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtoi(wstr) MSVCRT__wtoi @ cdecl -ret64 _wtoi64(wstr) MSVCRT__wtoi64 diff --git a/dlls/msvcr71/msvcr71.spec b/dlls/msvcr71/msvcr71.spec index fbdf839c6d3..6ebaa3208c7 100644 --- a/dlls/msvcr71/msvcr71.spec +++ b/dlls/msvcr71/msvcr71.spec @@ -664,7 +664,7 @@ @ cdecl _wstrtime(ptr) MSVCRT__wstrtime @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtoi(wstr) MSVCRT__wtoi @ cdecl -ret64 _wtoi64(wstr) MSVCRT__wtoi64 diff --git a/dlls/msvcr80/msvcr80.spec b/dlls/msvcr80/msvcr80.spec index 479051b771f..1d2e533c0a9 100644 --- a/dlls/msvcr80/msvcr80.spec +++ b/dlls/msvcr80/msvcr80.spec @@ -1290,8 +1290,8 @@ @ cdecl _wstrtime_s(ptr long) @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam +@ cdecl _wtmpnam_s(ptr long) MSVCRT__wtmpnam_s @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l @ cdecl _wtoi(wstr) MSVCRT__wtoi diff --git a/dlls/msvcr90/msvcr90.spec b/dlls/msvcr90/msvcr90.spec index 01800c84b79..e660c379568 100644 --- a/dlls/msvcr90/msvcr90.spec +++ b/dlls/msvcr90/msvcr90.spec @@ -1262,8 +1262,8 @@ @ cdecl _wstrtime_s(ptr long) @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam +@ cdecl _wtmpnam_s(ptr long) MSVCRT__wtmpnam_s @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l @ cdecl _wtoi(wstr) MSVCRT__wtoi diff --git a/dlls/msvcrt/file.c b/dlls/msvcrt/file.c index a708c6795de..9f35a62101e 100644 --- a/dlls/msvcrt/file.c +++ b/dlls/msvcrt/file.c @@ -4852,15 +4852,68 @@ char * CDECL MSVCRT_tmpnam(char *s) return tmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s; } +static int wtmpnam_helper(MSVCRT_wchar_t *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max) +{ + MSVCRT_wchar_t tmpstr[8]; + MSVCRT_wchar_t *p = s; + int digits; + + if (!MSVCRT_CHECK_PMT(s != NULL)) return MSVCRT_EINVAL; + + if (size < 3) { + if (size) *s = 0; + *MSVCRT__errno() = MSVCRT_ERANGE; + return MSVCRT_ERANGE; + } + *p++ = '\\'; + *p++ = 's'; + size -= 2; + digits = msvcrt_int_to_base32_w(GetCurrentProcessId(), tmpstr); + if (digits+1 > size) { + *s = 0; + *MSVCRT__errno() = MSVCRT_ERANGE; + return MSVCRT_ERANGE; + } + memcpy(p, tmpstr, digits*sizeof(tmpstr[0])); + p += digits; + *p++ = '.'; + size -= digits+1; + + while(1) { + while ((digits = *tmpnam_unique)+1 < tmp_max) { + if (InterlockedCompareExchange(tmpnam_unique, digits+1, digits) == digits) + break; + } + + digits = msvcrt_int_to_base32_w(digits, tmpstr); + if (digits+1 > size) { + *s = 0; + *MSVCRT__errno() = MSVCRT_ERANGE; + return MSVCRT_ERANGE; + } + memcpy(p, tmpstr, digits*sizeof(tmpstr[0])); + p[digits] = 0; + + if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES && + GetLastError() == ERROR_FILE_NOT_FOUND) + break; + } + return 0; +} + +/********************************************************************* + * _wtmpnam_s (MSVCRT.@) + */ +int CDECL MSVCRT__wtmpnam_s(MSVCRT_wchar_t *s, MSVCRT_size_t size) +{ + return wtmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S); +} + /********************************************************************* * _wtmpnam (MSVCRT.@) */ -MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s) +MSVCRT_wchar_t * CDECL 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) { thread_data_t *data = msvcrt_get_thread_data(); @@ -4870,18 +4923,7 @@ MSVCRT_wchar_t * CDECL MSVCRT_wtmpnam(MSVCRT_wchar_t *s) s = data->wtmpnam_buffer; } - 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)); - p[size] = '\0'; - if (GetFileAttributesW(s) == INVALID_FILE_ATTRIBUTES && - GetLastError() == ERROR_FILE_NOT_FOUND) - break; - } - return s; + return wtmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s; } /********************************************************************* diff --git a/dlls/msvcrt/msvcrt.spec b/dlls/msvcrt/msvcrt.spec index e2c3e6225a6..dd451a37d8a 100644 --- a/dlls/msvcrt/msvcrt.spec +++ b/dlls/msvcrt/msvcrt.spec @@ -1224,8 +1224,8 @@ @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam # stub _wtempnam_dbg(wstr wstr long str long) -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam -# stub _wtmpnam_s(ptr long) +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam +@ cdecl _wtmpnam_s(ptr long) MSVCRT__wtmpnam_s @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l @ cdecl _wtoi(wstr) MSVCRT__wtoi diff --git a/dlls/ucrtbase/ucrtbase.spec b/dlls/ucrtbase/ucrtbase.spec index 604cc782c3c..6079bc4a914 100644 --- a/dlls/ucrtbase/ucrtbase.spec +++ b/dlls/ucrtbase/ucrtbase.spec @@ -2131,8 +2131,8 @@ @ cdecl _wstrtime_s(ptr long) @ cdecl _wsystem(wstr) @ cdecl _wtempnam(wstr wstr) MSVCRT__wtempnam -@ cdecl _wtmpnam(ptr) MSVCRT_wtmpnam -@ stub _wtmpnam_s +@ cdecl _wtmpnam(ptr) MSVCRT__wtmpnam +@ cdecl _wtmpnam_s(ptr long) MSVCRT__wtmpnam_s @ cdecl _wtof(wstr) MSVCRT__wtof @ cdecl _wtof_l(wstr ptr) MSVCRT__wtof_l @ cdecl _wtoi(wstr) MSVCRT__wtoi