// Wrapper of C-language FILE struct -*- C++ -*-
-// Copyright (C) 2000-2021 Free Software Foundation, Inc.
+// Copyright (C) 2000-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// ISO C++ 14882: 27.8 File-based streams
//
+#include <bits/largefile-config.h>
#include <bits/basic_file.h>
#include <fcntl.h>
#include <errno.h>
#include <limits> // For <off_t>::max() and min() and <streamsize>::max()
+#if _GLIBCXX_USE__GET_OSFHANDLE
+# include <stdint.h> // For intptr_t
+# include <io.h> // For _get_osfhandle
+#endif
+
namespace
{
// Map ios_base::openmode flags to a string for use in fopen().
for (;;)
{
#ifdef _GLIBCXX_USE_STDIO_PURE
- const std::streamsize __ret = fwrite(__file, 1, __nleft, __file);
+ const std::streamsize __ret = fwrite(__s, 1, __nleft, __file);
+ if (__ret == 0 && ferror(__file))
+ break;
#else
const std::streamsize __ret = write(__fd, __s, __nleft);
-#endif
if (__ret == -1L && errno == EINTR)
continue;
if (__ret == -1L)
break;
-
+#endif
__nleft -= __ret;
if (__nleft == 0)
break;
const char* __c_mode = fopen_mode(__mode);
if (__c_mode && !this->is_open())
{
-#ifdef _GLIBCXX_USE_LFS
- if ((_M_cfile = fopen64(__name, __c_mode)))
-#else
if ((_M_cfile = fopen(__name, __c_mode)))
-#endif
{
_M_cfile_created = true;
__ret = this;
__basic_file<char>::xsgetn(char* __s, streamsize __n)
{
streamsize __ret;
- do
#ifdef _GLIBCXX_USE_STDIO_PURE
- __ret = fread(__s, 1, __n, this->file());
+ __ret = fread(__s, 1, __n, this->file());
+ if (__ret == 0 && ferror(this->file()))
+ __ret = -1;
#else
+ do
__ret = read(this->fd(), __s, __n);
-#endif
while (__ret == -1L && errno == EINTR);
+#endif
return __ret;
}
return __ret;
}
+ namespace
+ {
+ inline streamoff
+ get_file_offset(__basic_file<char>* __f)
+ {
+#ifdef _GLIBCXX_USE_STDIO_PURE
+# ifdef _GLIBCXX_USE_FSEEKO_FTELLO
+ return ftello(__f->file());
+# else
+ return ftell(__f->file());
+# endif
+#else
+ return lseek(__f->fd(), 0, (int)ios_base::cur);
+#endif
+ }
+ }
+
streamoff
__basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw ()
{
-#ifdef _GLIBCXX_USE_LFS
- return lseek64(this->fd(), __off, __way);
-#else
- if (__off > numeric_limits<off_t>::max()
- || __off < numeric_limits<off_t>::min())
- return -1L;
#ifdef _GLIBCXX_USE_STDIO_PURE
- return fseek(this->file(), __off, __way);
+#ifdef _GLIBCXX_USE_FSEEKO_FTELLO
+ if _GLIBCXX17_CONSTEXPR (sizeof(off_t) > sizeof(long))
+ {
+ if (fseeko(this->file(), __off, __way))
+ return -1;
+ }
+ else
+#endif
+ {
+ if (__off > numeric_limits<long>::max()
+ || __off < numeric_limits<long>::min())
+ return -1;
+ if (fseek(this->file(), __off, __way))
+ return -1;
+ }
+ return __way == ios_base::beg ? __off : std::get_file_offset(this);
#else
+ if _GLIBCXX17_CONSTEXPR (sizeof(streamoff) > sizeof(off_t))
+ if (__off > numeric_limits<off_t>::max()
+ || __off < numeric_limits<off_t>::min())
+ return -1L;
return lseek(this->fd(), __off, __way);
-#endif
#endif
}
#if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
// Regular files.
-#ifdef _GLIBCXX_USE_LFS
- struct stat64 __buffer;
- const int __err = fstat64(this->fd(), &__buffer);
+ struct stat __buffer;
+ const int __err = fstat(this->fd(), &__buffer);
if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
{
- const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
- ios_base::cur);
+ const streamoff __off = __buffer.st_size - std::get_file_offset(this);
return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
}
-#else
- struct stat __buffer;
- const int __err = fstat(this->fd(), &__buffer);
- if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
+#endif
+ return 0;
+ }
+
+ __basic_file<char>::native_handle_type
+ __basic_file<char>::native_handle() const noexcept
+ {
#ifdef _GLIBCXX_USE_STDIO_PURE
- return __buffer.st_size - fseek(this->file(), 0, ios_base::cur);
+ return _M_cfile;
+#elif _GLIBCXX_USE__GET_OSFHANDLE
+ const intptr_t handle = _M_cfile ? _get_osfhandle(fileno(_M_cfile)) : -1;
+ return reinterpret_cast<native_handle_type>(handle);
#else
- return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
-#endif
+ return fileno(_M_cfile);
#endif
-#endif
- return 0;
}
_GLIBCXX_END_NAMESPACE_VERSION