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

Fixes: #4571
PiperOrigin-RevId: 970216754
Change-Id: Iadfed64d59d0cfd32568a3f46001b19f64cfb125
This commit is contained in:
Abseil Team 2026-08-24 18:39:36 -07:00 committed by Copybara-Service
parent bfff9fc387
commit e273a2d81e
3 changed files with 12 additions and 54 deletions

View File

@ -2188,7 +2188,7 @@ inline int IsATTY(int fd) {
GTEST_DISABLE_DEPRECATED_PUSH_() GTEST_DISABLE_DEPRECATED_PUSH_()
// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), Dup(), Dup2(), and // ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
// StrError() aren't needed on Windows CE at this time and thus not // StrError() aren't needed on Windows CE at this time and thus not
// defined there. // defined there.
#if GTEST_HAS_FILE_SYSTEM #if GTEST_HAS_FILE_SYSTEM
@ -2196,13 +2196,7 @@ GTEST_DISABLE_DEPRECATED_PUSH_()
!defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \ !defined(GTEST_OS_WINDOWS_RT) && !defined(GTEST_OS_WINDOWS_GAMES) && \
!defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \ !defined(GTEST_OS_ESP8266) && !defined(GTEST_OS_XTENSA) && \
!defined(GTEST_OS_QURT) !defined(GTEST_OS_QURT)
inline int ChDir(const char* dir) { inline int ChDir(const char* dir) { return chdir(dir); }
#ifdef GTEST_OS_WINDOWS
return _chdir(dir);
#else
return chdir(dir);
#endif
}
#endif #endif
inline FILE* FOpen(const char* path, const char* mode) { inline FILE* FOpen(const char* path, const char* mode) {
#if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW) #if defined(GTEST_OS_WINDOWS) && !defined(GTEST_OS_WINDOWS_MINGW)
@ -2219,51 +2213,17 @@ inline FILE* FOpen(const char* path, const char* mode) {
inline FILE* FReopen(const char* path, const char* mode, FILE* stream) { inline FILE* FReopen(const char* path, const char* mode, FILE* stream) {
return freopen(path, mode, stream); return freopen(path, mode, stream);
} }
inline FILE* FDOpen(int fd, const char* mode) { inline FILE* FDOpen(int fd, const char* mode) { return fdopen(fd, mode); }
#ifdef GTEST_OS_WINDOWS
return _fdopen(fd, mode);
#else
return fdopen(fd, mode);
#endif
}
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
inline int FClose(FILE* fp) { return fclose(fp); } inline int FClose(FILE* fp) { return fclose(fp); }
#if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT) #if !defined(GTEST_OS_WINDOWS_MOBILE) && !defined(GTEST_OS_QURT)
inline int Read(int fd, void* buf, unsigned int count) { 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)); return static_cast<int>(read(fd, buf, count));
#endif
} }
inline int Write(int fd, const void* buf, unsigned int count) { 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)); 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 Dup(int fd) {
#ifdef GTEST_OS_WINDOWS
return _dup(fd);
#else
return dup(fd);
#endif
}
inline int Dup2(int fd1, int fd2) {
#ifdef GTEST_OS_WINDOWS
return _dup2(fd1, fd2);
#else
return dup2(fd1, fd2);
#endif
} }
inline int Close(int fd) { return close(fd); }
#endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT #endif // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_QURT
#endif // GTEST_HAS_FILE_SYSTEM #endif // GTEST_HAS_FILE_SYSTEM

View File

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

View File

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