Use non-POSIX function names on MSVC to avoid errors when _CRT_DECLARE_NONSTDC_NAMES = 0

Fixes: #4571
PiperOrigin-RevId: 971422465
Change-Id: Ic32cceedd80490bd688cb240600becc23efb7191
This commit is contained in:
Abseil Team 2026-08-26 12:05:06 -07:00 committed by Copybara-Service
parent e273a2d81e
commit 938220d0f6
3 changed files with 52 additions and 9 deletions

View File

@ -2196,7 +2196,13 @@ GTEST_DISABLE_DEPRECATED_PUSH_()
!defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \
!defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \
!defined(GTEST_OS_QURT)
inline int ChDir(const char* dir) { return chdir(dir); }
inline int ChDir(const char* dir) {
#ifdef GTEST_OS_WINDOWS
return _chdir(dir);
#else
return chdir(dir);
#endif
}
#endif
inline FILE* FOpen(const char* path, const char* mode) {
#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
@ -2213,17 +2219,37 @@ inline FILE* FOpen(const char* path, const char* mode) {
inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
return freopen(path, mode, stream);
}
inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
inline FILE* FDOpen(int fd, const char* mode) {
#ifdef GTEST_OS_WINDOWS
return _fdopen(fd, mode);
#else
return fdopen(fd, mode);
#endif
}
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
inline int FClose(FILE* fp) { return fclose(fp); }
#if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
inline int Read(int fd, void* buf, unsigned int count) {
#ifdef GTEST_OS_WINDOWS
return static_cast<int>(_read(fd, buf, count));
#else
return static_cast<int>(read(fd, buf, count));
#endif
}
inline int Write(int fd, const void* buf, unsigned int count) {
#ifdef GTEST_OS_WINDOWS
return static_cast<int>(_write(fd, buf, count));
#else
return static_cast<int>(write(fd, buf, count));
#endif
}
inline int Close(int fd) {
#ifdef GTEST_OS_WINDOWS
return _close(fd);
#else
return close(fd);
#endif
}
inline int Close(int fd) { return close(fd); }
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
#endif // GTEST_HAS_FILE_SYSTEM

View File

@ -1085,7 +1085,14 @@ bool EndsWithPathSeparator(const std::string& path) {
class CapturedStream {
public:
// The ctor redirects the stream to a temporary file.
explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {
explicit CapturedStream(int fd)
: fd_(fd),
#ifdef GTEST_OS_WINDOWS
uncaptured_fd_(_dup(fd))
#else
uncaptured_fd_(dup(fd))
#endif
{
#ifdef GTEST_OS_WINDOWS
char temp_dir_path[MAX_PATH + 1] = {'\0'}; // NOLINT
char temp_file_path[MAX_PATH + 1] = {'\0'}; // NOLINT
@ -1097,7 +1104,7 @@ class CapturedStream {
GTEST_CHECK_(success != 0)
<< "Failed to create temporary file in " << temp_dir_path
<< " with error " << ::GetLastError();
const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);
const int captured_fd = _creat(temp_file_path, _S_IREAD | _S_IWRITE);
GTEST_CHECK_(captured_fd != -1)
<< "Failed to open temporary file " << temp_file_path << " with error "
<< ::GetLastError();
@ -1167,8 +1174,12 @@ class CapturedStream {
filename_ = std::move(name_template);
#endif // GTEST_OS_WINDOWS
fflush(nullptr);
#ifdef GTEST_OS_WINDOWS
_dup2(captured_fd, fd_);
#else
dup2(captured_fd, fd_);
close(captured_fd);
#endif
posix::Close(captured_fd);
}
~CapturedStream() { remove(filename_.c_str()); }
@ -1177,8 +1188,12 @@ class CapturedStream {
if (uncaptured_fd_ != -1) {
// Restores the original stream.
fflush(nullptr);
#ifdef GTEST_OS_WINDOWS
_dup2(uncaptured_fd_, fd_);
#else
dup2(uncaptured_fd_, fd_);
close(uncaptured_fd_);
#endif
posix::Close(uncaptured_fd_);
uncaptured_fd_ = -1;
}

View File

@ -484,9 +484,11 @@ class FormatEpochTimeInMillisAsIso8601Test : public Test {
const std::string env_var =
std::string("TZ=") + (time_zone ? time_zone : "");
_putenv(env_var.c_str());
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996 /* deprecated function */)
#if defined(GTEST_OS_WINDOWS)
_tzset();
#else
tzset();
GTEST_DISABLE_MSC_WARNINGS_POP_()
#endif
#else
#if defined(GTEST_OS_LINUX_ANDROID) && __ANDROID_API__ < 21
// Work around KitKat bug in tzset by setting "UTC" before setting "UTC+00".