Commit 9b6c93f6 authored by Piotr Caban's avatar Piotr Caban Committed by Alexandre Julliard

msvcrt: Add tmpnam_s implementation.

parent 5913a107
......@@ -154,6 +154,6 @@
@ cdecl tmpfile() ucrtbase.tmpfile
@ cdecl tmpfile_s(ptr) ucrtbase.tmpfile_s
@ cdecl tmpnam(ptr) ucrtbase.tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) ucrtbase.tmpnam_s
@ cdecl ungetc(long ptr) ucrtbase.ungetc
@ cdecl ungetwc(long ptr) ucrtbase.ungetwc
......@@ -1833,7 +1833,7 @@
@ cdecl tmpfile() MSVCRT_tmpfile
@ cdecl tmpfile_s(ptr) MSVCRT_tmpfile_s
@ cdecl tmpnam(ptr) MSVCRT_tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) MSVCRT_tmpnam_s
@ cdecl tolower(long) MSVCRT_tolower
@ cdecl toupper(long) MSVCRT_toupper
@ cdecl towlower(long) MSVCRT_towlower
......
......@@ -2192,7 +2192,7 @@
@ cdecl tmpfile() MSVCRT_tmpfile
@ cdecl tmpfile_s(ptr) MSVCRT_tmpfile_s
@ cdecl tmpnam(ptr) MSVCRT_tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) MSVCRT_tmpnam_s
@ cdecl tolower(long) MSVCRT_tolower
@ cdecl toupper(long) MSVCRT_toupper
@ cdecl towlower(long) MSVCRT_towlower
......
......@@ -2411,7 +2411,7 @@
@ cdecl tmpfile() MSVCRT_tmpfile
@ cdecl tmpfile_s(ptr) MSVCRT_tmpfile_s
@ cdecl tmpnam(ptr) MSVCRT_tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) MSVCRT_tmpnam_s
@ cdecl tolower(long) MSVCRT_tolower
@ cdecl toupper(long) MSVCRT_toupper
@ stub towctrans
......
......@@ -2073,7 +2073,7 @@
@ cdecl tmpfile() msvcr120.tmpfile
@ cdecl tmpfile_s(ptr) msvcr120.tmpfile_s
@ cdecl tmpnam(ptr) msvcr120.tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) msvcr120.tmpnam_s
@ cdecl tolower(long) msvcr120.tolower
@ cdecl toupper(long) msvcr120.toupper
@ stub towctrans
......
......@@ -1514,7 +1514,7 @@
@ cdecl tmpfile() MSVCRT_tmpfile
@ cdecl tmpfile_s(ptr) MSVCRT_tmpfile_s
@ cdecl tmpnam(ptr) MSVCRT_tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) MSVCRT_tmpnam_s
@ cdecl tolower(long) MSVCRT_tolower
@ cdecl toupper(long) MSVCRT_toupper
@ cdecl towlower(long) MSVCRT_towlower
......
......@@ -1486,7 +1486,7 @@
@ cdecl tmpfile() MSVCRT_tmpfile
@ cdecl tmpfile_s(ptr) MSVCRT_tmpfile_s
@ cdecl tmpnam(ptr) MSVCRT_tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) MSVCRT_tmpnam_s
@ cdecl tolower(long) MSVCRT_tolower
@ cdecl toupper(long) MSVCRT_toupper
@ cdecl towlower(long) MSVCRT_towlower
......
......@@ -128,6 +128,7 @@ static int MSVCRT_umask = 0;
/* INTERNAL: static data for tmpnam and _wtmpname functions */
static int tmpnam_unique;
static int tmpnam_s_unique;
static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
......@@ -4780,15 +4781,65 @@ void CDECL MSVCRT_setbuf(MSVCRT_FILE* file, char *buf)
MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ);
}
static int tmpnam_helper(char *s, MSVCRT_size_t size, int *tmpnam_unique, int tmp_max)
{
char tmpstr[8];
char *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(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(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 (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
GetLastError() == ERROR_FILE_NOT_FOUND)
break;
}
return 0;
}
int CDECL MSVCRT_tmpnam_s(char *s, MSVCRT_size_t size)
{
return tmpnam_helper(s, size, &tmpnam_s_unique, MSVCRT_TMP_MAX_S);
}
/*********************************************************************
* tmpnam (MSVCRT.@)
*/
char * CDECL MSVCRT_tmpnam(char *s)
{
char tmpstr[16];
char *p;
int count, size;
if (!s) {
thread_data_t *data = msvcrt_get_thread_data();
......@@ -4798,18 +4849,7 @@ char * CDECL MSVCRT_tmpnam(char *s)
s = data->tmpnam_buffer;
}
msvcrt_int_to_base32(GetCurrentProcessId(), tmpstr);
p = s + sprintf(s, "\\s%s.", tmpstr);
for (count = 0; count < MSVCRT_TMP_MAX; count++)
{
size = msvcrt_int_to_base32(tmpnam_unique++, tmpstr);
memcpy(p, tmpstr, size);
p[size] = '\0';
if (GetFileAttributesA(s) == INVALID_FILE_ATTRIBUTES &&
GetLastError() == ERROR_FILE_NOT_FOUND)
break;
}
return s;
return tmpnam_helper(s, -1, &tmpnam_unique, MSVCRT_TMP_MAX) ? NULL : s;
}
/*********************************************************************
......
......@@ -663,6 +663,7 @@ struct MSVCRT__stat64 {
#define MSVCRT_WEOF (MSVCRT_wint_t)(0xFFFF)
#define MSVCRT_EOF (-1)
#define MSVCRT_TMP_MAX 0x7fff
#define MSVCRT_TMP_MAX_S 0x7fffffff
#define MSVCRT_RAND_MAX 0x7fff
#define MSVCRT_BUFSIZ 512
......
......@@ -1457,7 +1457,7 @@
@ cdecl tmpfile() MSVCRT_tmpfile
@ cdecl tmpfile_s(ptr) MSVCRT_tmpfile_s
@ cdecl tmpnam(ptr) MSVCRT_tmpnam
# stub tmpnam_s(ptr long)
@ cdecl tmpnam_s(ptr long) MSVCRT_tmpnam_s
@ cdecl tolower(long) MSVCRT_tolower
@ cdecl toupper(long) MSVCRT_toupper
@ cdecl towlower(long) MSVCRT_towlower
......
......@@ -2539,7 +2539,7 @@
@ cdecl tmpfile() MSVCRT_tmpfile
@ cdecl tmpfile_s(ptr) MSVCRT_tmpfile_s
@ cdecl tmpnam(ptr) MSVCRT_tmpnam
@ stub tmpnam_s
@ cdecl tmpnam_s(ptr long) MSVCRT_tmpnam_s
@ cdecl tolower(long) MSVCRT_tolower
@ cdecl toupper(long) MSVCRT_toupper
@ stub towctrans
......
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