static errno_t __cdecl _int_wmktemp_s (wchar_t *d, size_t dn) { size_t sz; if (!d || !dn) { _wmktemp (NULL); return EINVAL; } sz = wcsnlen (d, dn); if (sz >= dn || sz < 6) { d[0] = 0; _wmktemp (NULL); return EINVAL; } if (_wmktemp (d) != NULL) return 0; return errno; }
/* * os_mkstemp -- generate a unique temporary filename from template */ int os_mkstemp(char *temp) { unsigned rnd; wchar_t *utemp = util_toUTF16(temp); if (utemp == NULL) return -1; wchar_t *path = _wmktemp(utemp); if (path == NULL) { util_free_UTF16(utemp); return -1; } wchar_t *npath = Malloc(sizeof(*npath) * wcslen(path) + _MAX_FNAME); if (npath == NULL) { util_free_UTF16(utemp); return -1; } wcscpy(npath, path); util_free_UTF16(utemp); /* * Use rand_s to generate more unique tmp file name than _mktemp do. * In case with multiple threads and multiple files even after close() * file name conflicts occurred. * It resolved issue with synchronous removing * multiples files by system. */ rand_s(&rnd); int ret = _snwprintf(npath + wcslen(npath), _MAX_FNAME, L"%u", rnd); if (ret < 0) goto out; /* * Use O_TEMPORARY flag to make sure the file is deleted when * the last file descriptor is closed. Also, it prevents opening * this file from another process. */ ret = _wopen(npath, O_RDWR | O_CREAT | O_EXCL | O_TEMPORARY, S_IWRITE | S_IREAD); out: Free(npath); return ret; }
int mkstemp (char *tmp_template) { int fd; gunichar2* utf16_template; utf16_template = u8to16 (tmp_template); fd = -1; utf16_template = _wmktemp( utf16_template); if (utf16_template && *utf16_template) { /* FIXME: _O_TEMPORARY causes file to disappear on close causing a test to fail */ fd = _wopen( utf16_template, _O_BINARY | _O_CREAT /*| _O_TEMPORARY*/ | _O_RDWR | _O_EXCL, _S_IREAD | _S_IWRITE); } /* FIXME: this will crash if utf16_template == NULL */ sprintf (tmp_template + strlen (tmp_template) - 6, "%S", utf16_template + wcslen (utf16_template) - 6); g_free (utf16_template); return fd; }