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

Fixes: #4571
PiperOrigin-RevId: 970040273
Change-Id: Id1aed06aeddde7dbcdff6eaa4702f356c034cadb
This commit is contained in:
Abseil Team
2026-08-24 13:25:36 -07:00
committed by Copybara-Service
parent 7260682388
commit bfff9fc387
3 changed files with 54 additions and 12 deletions
+44 -4
View File
@@ -2188,7 +2188,7 @@ inline int IsATTY(int fd) {
GTEST_DISABLE_DEPRECATED_PUSH_()
// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and
// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), Dup(), Dup2(), and
// StrError() aren't needed on Windows CE at this time and thus not
// defined there.
#if GTEST_HAS_FILE_SYSTEM
@@ -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,51 @@ 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 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_HAS_FILE_SYSTEM
+6 -6
View File
@@ -1085,7 +1085,7 @@ 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), uncaptured_fd_(posix::Dup(fd)) {
#ifdef GTEST_OS_WINDOWS
char temp_dir_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)
<< "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 +1167,8 @@ class CapturedStream {
filename_ = std::move(name_template);
#endif // GTEST_OS_WINDOWS
fflush(nullptr);
dup2(captured_fd, fd_);
close(captured_fd);
posix::Dup2(captured_fd, fd_);
posix::Close(captured_fd);
}
~CapturedStream() { remove(filename_.c_str()); }
@@ -1177,8 +1177,8 @@ class CapturedStream {
if (uncaptured_fd_ != -1) {
// Restores the original stream.
fflush(nullptr);
dup2(uncaptured_fd_, fd_);
close(uncaptured_fd_);
posix::Dup2(uncaptured_fd_, fd_);
posix::Close(uncaptured_fd_);
uncaptured_fd_ = -1;
}
+4 -2
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".