| 1 | // Wrapper of C-language FILE struct -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2000-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | // |
| 26 | // ISO C++ 14882: 27.8 File-based streams |
| 27 | // |
| 28 | |
| 29 | #include <bits/largefile-config.h> |
| 30 | #include <bits/basic_file.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <errno.h> |
| 33 | |
| 34 | #ifdef _GLIBCXX_HAVE_POLL |
| 35 | #include <poll.h> |
| 36 | #endif |
| 37 | |
| 38 | // Pick up ioctl on Solaris 2.8 |
| 39 | #ifdef _GLIBCXX_HAVE_UNISTD_H |
| 40 | #include <unistd.h> |
| 41 | #endif |
| 42 | |
| 43 | // Pick up FIONREAD on Solaris 2 |
| 44 | #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H |
| 45 | #define BSD_COMP |
| 46 | #include <sys/ioctl.h> |
| 47 | #endif |
| 48 | |
| 49 | // Pick up FIONREAD on Solaris 2.5. |
| 50 | #ifdef _GLIBCXX_HAVE_SYS_FILIO_H |
| 51 | #include <sys/filio.h> |
| 52 | #endif |
| 53 | |
| 54 | #ifdef _GLIBCXX_HAVE_SYS_UIO_H |
| 55 | #include <sys/uio.h> |
| 56 | #endif |
| 57 | |
| 58 | #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG) |
| 59 | # include <sys/stat.h> |
| 60 | # ifdef _GLIBCXX_HAVE_S_ISREG |
| 61 | # define _GLIBCXX_ISREG(x) S_ISREG(x) |
| 62 | # else |
| 63 | # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 64 | # endif |
| 65 | #endif |
| 66 | |
| 67 | #include <limits> // For <off_t>::max() and min() and <streamsize>::max() |
| 68 | |
| 69 | #if _GLIBCXX_USE__GET_OSFHANDLE |
| 70 | # include <stdint.h> // For intptr_t |
| 71 | # include <io.h> // For _get_osfhandle |
| 72 | #endif |
| 73 | |
| 74 | namespace |
| 75 | { |
| 76 | // Map ios_base::openmode flags to a string for use in fopen(). |
| 77 | // Table of valid combinations as given in [lib.filebuf.members]/2. |
| 78 | static const char* |
| 79 | fopen_mode(std::ios_base::openmode mode) |
| 80 | { |
| 81 | enum |
| 82 | { |
| 83 | in = std::ios_base::in, |
| 84 | out = std::ios_base::out, |
| 85 | trunc = std::ios_base::trunc, |
| 86 | app = std::ios_base::app, |
| 87 | binary = std::ios_base::binary, |
| 88 | noreplace = std::_S_noreplace |
| 89 | }; |
| 90 | |
| 91 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 92 | // 596. 27.8.1.3 Table 112 omits "a+" and "a+b" modes. |
| 93 | switch (mode & (in|out|trunc|app|binary|noreplace)) |
| 94 | { |
| 95 | case ( out ): return "w"; |
| 96 | case ( out |noreplace): return "wx"; |
| 97 | case ( out|trunc ): return "w"; |
| 98 | case ( out|trunc |noreplace): return "wx"; |
| 99 | case ( out |app ): return "a"; |
| 100 | case ( app ): return "a"; |
| 101 | case (in ): return "r"; |
| 102 | case (in|out ): return "r+"; |
| 103 | case (in|out|trunc ): return "w+"; |
| 104 | case (in|out|trunc |noreplace): return "w+x"; |
| 105 | case (in|out |app ): return "a+"; |
| 106 | case (in |app ): return "a+"; |
| 107 | |
| 108 | case ( out |binary ): return "wb"; |
| 109 | case ( out |binary|noreplace): return "wbx"; |
| 110 | case ( out |app|binary ): return "ab"; |
| 111 | case ( app|binary ): return "ab"; |
| 112 | case ( out|trunc |binary ): return "wb"; |
| 113 | case (in |binary ): return "rb"; |
| 114 | case (in|out |binary ): return "r+b"; |
| 115 | case (in|out|trunc |binary ): return "w+b"; |
| 116 | case (in|out|trunc |binary|noreplace): return "w+bx"; |
| 117 | case (in|out |app|binary ): return "a+b"; |
| 118 | case (in |app|binary ): return "a+b"; |
| 119 | |
| 120 | default: return 0; // invalid |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Wrapper handling partial write. |
| 125 | static std::streamsize |
| 126 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 127 | xwrite(FILE *__file, const char* __s, std::streamsize __n) |
| 128 | #else |
| 129 | xwrite(int __fd, const char* __s, std::streamsize __n) |
| 130 | #endif |
| 131 | { |
| 132 | std::streamsize __nleft = __n; |
| 133 | |
| 134 | for (;;) |
| 135 | { |
| 136 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 137 | const std::streamsize __ret = fwrite(__s, 1, __nleft, __file); |
| 138 | if (__ret == 0 && ferror(__file)) |
| 139 | break; |
| 140 | #else |
| 141 | const std::streamsize __ret = write(__fd, __s, __nleft); |
| 142 | if (__ret == -1L && errno == EINTR) |
| 143 | continue; |
| 144 | if (__ret == -1L) |
| 145 | break; |
| 146 | #endif |
| 147 | __nleft -= __ret; |
| 148 | if (__nleft == 0) |
| 149 | break; |
| 150 | |
| 151 | __s += __ret; |
| 152 | } |
| 153 | |
| 154 | return __n - __nleft; |
| 155 | } |
| 156 | |
| 157 | #if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE) |
| 158 | // Wrapper handling partial writev. |
| 159 | static std::streamsize |
| 160 | xwritev(int __fd, const char* __s1, std::streamsize __n1, |
| 161 | const char* __s2, std::streamsize __n2) |
| 162 | { |
| 163 | std::streamsize __nleft = __n1 + __n2; |
| 164 | std::streamsize __n1_left = __n1; |
| 165 | |
| 166 | struct iovec __iov[2]; |
| 167 | __iov[1].iov_base = const_cast<char*>(__s2); |
| 168 | __iov[1].iov_len = __n2; |
| 169 | |
| 170 | for (;;) |
| 171 | { |
| 172 | __iov[0].iov_base = const_cast<char*>(__s1); |
| 173 | __iov[0].iov_len = __n1_left; |
| 174 | |
| 175 | const std::streamsize __ret = writev(__fd, __iov, 2); |
| 176 | if (__ret == -1L && errno == EINTR) |
| 177 | continue; |
| 178 | if (__ret == -1L) |
| 179 | break; |
| 180 | |
| 181 | __nleft -= __ret; |
| 182 | if (__nleft == 0) |
| 183 | break; |
| 184 | |
| 185 | const std::streamsize __off = __ret - __n1_left; |
| 186 | if (__off >= 0) |
| 187 | { |
| 188 | __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | __s1 += __ret; |
| 193 | __n1_left -= __ret; |
| 194 | } |
| 195 | |
| 196 | return __n1 + __n2 - __nleft; |
| 197 | } |
| 198 | #endif |
| 199 | } // anonymous namespace |
| 200 | |
| 201 | |
| 202 | namespace std _GLIBCXX_VISIBILITY(default) |
| 203 | { |
| 204 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 205 | |
| 206 | // Definitions for __basic_file<char>. |
| 207 | __basic_file<char>::__basic_file(__c_lock* /*__lock*/) throw() |
| 208 | : _M_cfile(NULL), _M_cfile_created(false) { } |
| 209 | |
| 210 | __basic_file<char>::~__basic_file() |
| 211 | { this->close(); } |
| 212 | |
| 213 | __basic_file<char>* |
| 214 | __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode) |
| 215 | { |
| 216 | __basic_file* __ret = NULL; |
| 217 | if (!this->is_open() && __file) |
| 218 | { |
| 219 | int __err, __save_errno = errno; |
| 220 | // POSIX guarantees that fflush sets errno on error, but C doesn't. |
| 221 | errno = 0; |
| 222 | do |
| 223 | __err = fflush(__file); |
| 224 | while (__err && errno == EINTR); |
| 225 | errno = __save_errno; |
| 226 | if (!__err) |
| 227 | { |
| 228 | _M_cfile = __file; |
| 229 | _M_cfile_created = false; |
| 230 | __ret = this; |
| 231 | } |
| 232 | } |
| 233 | return __ret; |
| 234 | } |
| 235 | |
| 236 | __basic_file<char>* |
| 237 | __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode) throw () |
| 238 | { |
| 239 | __basic_file* __ret = NULL; |
| 240 | const char* __c_mode = fopen_mode(__mode); |
| 241 | if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode))) |
| 242 | { |
| 243 | char* __buf = NULL; |
| 244 | _M_cfile_created = true; |
| 245 | if (__fd == 0) |
| 246 | setvbuf(_M_cfile, __buf, _IONBF, 0); |
| 247 | __ret = this; |
| 248 | } |
| 249 | return __ret; |
| 250 | } |
| 251 | |
| 252 | __basic_file<char>* |
| 253 | __basic_file<char>::open(const char* __name, ios_base::openmode __mode, |
| 254 | int /*__prot*/) |
| 255 | { |
| 256 | __basic_file* __ret = NULL; |
| 257 | const char* __c_mode = fopen_mode(__mode); |
| 258 | if (__c_mode && !this->is_open()) |
| 259 | { |
| 260 | if ((_M_cfile = fopen(__name, __c_mode))) |
| 261 | { |
| 262 | _M_cfile_created = true; |
| 263 | __ret = this; |
| 264 | } |
| 265 | } |
| 266 | return __ret; |
| 267 | } |
| 268 | |
| 269 | #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T |
| 270 | __basic_file<char>* |
| 271 | __basic_file<char>::open(const wchar_t* __name, ios_base::openmode __mode) |
| 272 | { |
| 273 | __basic_file* __ret = NULL; |
| 274 | const char* __c_mode = fopen_mode(__mode); |
| 275 | if (__c_mode && !this->is_open()) |
| 276 | { |
| 277 | wchar_t __wc_mode[4] = { }; |
| 278 | int __i = 0; |
| 279 | do |
| 280 | { |
| 281 | switch(__c_mode[__i]) { |
| 282 | case 'a': __wc_mode[__i] = L'a'; break; |
| 283 | case 'b': __wc_mode[__i] = L'b'; break; |
| 284 | case 'r': __wc_mode[__i] = L'r'; break; |
| 285 | case 'w': __wc_mode[__i] = L'w'; break; |
| 286 | case '+': __wc_mode[__i] = L'+'; break; |
| 287 | default: return __ret; |
| 288 | } |
| 289 | } |
| 290 | while (__c_mode[++__i]); |
| 291 | |
| 292 | if ((_M_cfile = _wfopen(__name, __wc_mode))) |
| 293 | { |
| 294 | _M_cfile_created = true; |
| 295 | __ret = this; |
| 296 | } |
| 297 | } |
| 298 | return __ret; |
| 299 | } |
| 300 | #endif |
| 301 | |
| 302 | bool |
| 303 | __basic_file<char>::is_open() const throw () |
| 304 | { return _M_cfile != 0; } |
| 305 | |
| 306 | #ifndef _GLIBCCXX_USE_STDIO_PURE |
| 307 | int |
| 308 | __basic_file<char>::fd() throw () |
| 309 | { return fileno(_M_cfile); } |
| 310 | #endif |
| 311 | |
| 312 | __c_file* |
| 313 | __basic_file<char>::file() throw () |
| 314 | { return _M_cfile; } |
| 315 | |
| 316 | __basic_file<char>* |
| 317 | __basic_file<char>::close() |
| 318 | { |
| 319 | __basic_file* __ret = static_cast<__basic_file*>(NULL); |
| 320 | if (this->is_open()) |
| 321 | { |
| 322 | int __err = 0; |
| 323 | if (_M_cfile_created) |
| 324 | __err = fclose(_M_cfile); |
| 325 | _M_cfile = 0; |
| 326 | if (!__err) |
| 327 | __ret = this; |
| 328 | } |
| 329 | return __ret; |
| 330 | } |
| 331 | |
| 332 | streamsize |
| 333 | __basic_file<char>::xsgetn(char* __s, streamsize __n) |
| 334 | { |
| 335 | streamsize __ret; |
| 336 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 337 | __ret = fread(__s, 1, __n, this->file()); |
| 338 | if (__ret == 0 && ferror(this->file())) |
| 339 | __ret = -1; |
| 340 | #else |
| 341 | |
| 342 | #ifdef _GLIBCXX_MAX_READ_SIZE |
| 343 | if (__builtin_expect(__n > _GLIBCXX_MAX_READ_SIZE, 0)) |
| 344 | __n = _GLIBCXX_MAX_READ_SIZE; |
| 345 | #endif |
| 346 | |
| 347 | do |
| 348 | __ret = read(this->fd(), __s, __n); |
| 349 | while (__ret == -1L && errno == EINTR); |
| 350 | #endif |
| 351 | return __ret; |
| 352 | } |
| 353 | |
| 354 | streamsize |
| 355 | __basic_file<char>::xsputn(const char* __s, streamsize __n) |
| 356 | { |
| 357 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 358 | return xwrite(this->file(), __s, __n); |
| 359 | #else |
| 360 | return xwrite(this->fd(), __s, __n); |
| 361 | #endif |
| 362 | } |
| 363 | |
| 364 | streamsize |
| 365 | __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1, |
| 366 | const char* __s2, streamsize __n2) |
| 367 | { |
| 368 | streamsize __ret = 0; |
| 369 | #if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE) |
| 370 | __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2); |
| 371 | #else |
| 372 | if (__n1) |
| 373 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 374 | __ret = xwrite(this->file(), __s1, __n1); |
| 375 | #else |
| 376 | __ret = xwrite(this->fd(), __s1, __n1); |
| 377 | #endif |
| 378 | |
| 379 | if (__ret == __n1) |
| 380 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 381 | __ret += xwrite(this->file(), __s2, __n2); |
| 382 | #else |
| 383 | __ret += xwrite(this->fd(), __s2, __n2); |
| 384 | #endif |
| 385 | #endif |
| 386 | return __ret; |
| 387 | } |
| 388 | |
| 389 | namespace |
| 390 | { |
| 391 | inline streamoff |
| 392 | get_file_offset(__basic_file<char>* __f) |
| 393 | { |
| 394 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 395 | # ifdef _GLIBCXX_USE_FSEEKO_FTELLO |
| 396 | return ftello(__f->file()); |
| 397 | # else |
| 398 | return ftell(__f->file()); |
| 399 | # endif |
| 400 | #else |
| 401 | return lseek(__f->fd(), 0, (int)ios_base::cur); |
| 402 | #endif |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | streamoff |
| 407 | __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw () |
| 408 | { |
| 409 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 410 | #ifdef _GLIBCXX_USE_FSEEKO_FTELLO |
| 411 | if _GLIBCXX17_CONSTEXPR (sizeof(off_t) > sizeof(long)) |
| 412 | { |
| 413 | if (fseeko(this->file(), __off, __way)) |
| 414 | return -1; |
| 415 | } |
| 416 | else |
| 417 | #endif |
| 418 | { |
| 419 | if (__off > numeric_limits<long>::max() |
| 420 | || __off < numeric_limits<long>::min()) |
| 421 | return -1; |
| 422 | if (fseek(this->file(), __off, __way)) |
| 423 | return -1; |
| 424 | } |
| 425 | return __way == ios_base::beg ? __off : std::get_file_offset(this); |
| 426 | #else |
| 427 | if _GLIBCXX17_CONSTEXPR (sizeof(streamoff) > sizeof(off_t)) |
| 428 | if (__off > numeric_limits<off_t>::max() |
| 429 | || __off < numeric_limits<off_t>::min()) |
| 430 | return -1L; |
| 431 | return lseek(this->fd(), __off, __way); |
| 432 | #endif |
| 433 | } |
| 434 | |
| 435 | int |
| 436 | __basic_file<char>::sync() |
| 437 | { return fflush(_M_cfile); } |
| 438 | |
| 439 | streamsize |
| 440 | __basic_file<char>::showmanyc() |
| 441 | { |
| 442 | #if !defined(_GLIBCXX_NO_IOCTL) && !defined(_GLIBCXX_USE_STDIO_PURE) |
| 443 | #ifdef FIONREAD |
| 444 | // Pipes and sockets. |
| 445 | int __num = 0; |
| 446 | int __r = ioctl(this->fd(), FIONREAD, &__num); |
| 447 | if (!__r && __num >= 0) |
| 448 | return __num; |
| 449 | #endif |
| 450 | #endif |
| 451 | |
| 452 | #if defined(_GLIBCXX_HAVE_POLL) && !defined(_GLIBCXX_USE_STDIO_PURE) |
| 453 | // Cheap test. |
| 454 | struct pollfd __pfd[1]; |
| 455 | __pfd[0].fd = this->fd(); |
| 456 | __pfd[0].events = POLLIN; |
| 457 | if (poll(__pfd, 1, 0) <= 0) |
| 458 | return 0; |
| 459 | #endif |
| 460 | |
| 461 | #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG) |
| 462 | // Regular files. |
| 463 | struct stat __buffer; |
| 464 | const int __err = fstat(this->fd(), &__buffer); |
| 465 | if (!__err && _GLIBCXX_ISREG(__buffer.st_mode)) |
| 466 | { |
| 467 | const streamoff __off = __buffer.st_size - std::get_file_offset(this); |
| 468 | return std::min(__off, streamoff(numeric_limits<streamsize>::max())); |
| 469 | } |
| 470 | #endif |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | __basic_file<char>::native_handle_type |
| 475 | __basic_file<char>::native_handle() const noexcept |
| 476 | { |
| 477 | #ifdef _GLIBCXX_USE_STDIO_PURE |
| 478 | return _M_cfile; |
| 479 | #elif _GLIBCXX_USE__GET_OSFHANDLE |
| 480 | const intptr_t handle = _M_cfile ? _get_osfhandle(fileno(_M_cfile)) : -1; |
| 481 | return reinterpret_cast<native_handle_type>(handle); |
| 482 | #else |
| 483 | return fileno(_M_cfile); |
| 484 | #endif |
| 485 | } |
| 486 | |
| 487 | _GLIBCXX_END_NAMESPACE_VERSION |
| 488 | } // namespace |
| 489 |