1 // Wrapper of C-language FILE struct -*- C++ -*- 2 3 // Copyright (C) 2000-2022 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/basic_file.h> 30 #include <fcntl.h> 31 #include <errno.h> 32 33 #ifdef _GLIBCXX_HAVE_POLL 34 #include <poll.h> 35 #endif 36 37 // Pick up ioctl on Solaris 2.8 38 #ifdef _GLIBCXX_HAVE_UNISTD_H 39 #include <unistd.h> 40 #endif 41 42 // Pick up FIONREAD on Solaris 2 43 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H 44 #define BSD_COMP 45 #include <sys/ioctl.h> 46 #endif 47 48 // Pick up FIONREAD on Solaris 2.5. 49 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H 50 #include <sys/filio.h> 51 #endif 52 53 #ifdef _GLIBCXX_HAVE_SYS_UIO_H 54 #include <sys/uio.h> 55 #endif 56 57 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG) 58 # include <sys/stat.h> 59 # ifdef _GLIBCXX_HAVE_S_ISREG 60 # define _GLIBCXX_ISREG(x) S_ISREG(x) 61 # else 62 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG) 63 # endif 64 #endif 65 66 #include <limits> // For <off_t>::max() and min() and <streamsize>::max() 67 68 namespace 69 { 70 // Map ios_base::openmode flags to a string for use in fopen(). 71 // Table of valid combinations as given in [lib.filebuf.members]/2. 72 static const char* fopen_mode(std::ios_base::openmode mode)73 fopen_mode(std::ios_base::openmode mode) 74 { 75 enum 76 { 77 in = std::ios_base::in, 78 out = std::ios_base::out, 79 trunc = std::ios_base::trunc, 80 app = std::ios_base::app, 81 binary = std::ios_base::binary, 82 noreplace = std::_S_noreplace 83 }; 84 85 // _GLIBCXX_RESOLVE_LIB_DEFECTS 86 // 596. 27.8.1.3 Table 112 omits "a+" and "a+b" modes. 87 switch (mode & (in|out|trunc|app|binary|noreplace)) 88 { 89 case ( out ): return "w"; 90 case ( out |noreplace): return "wx"; 91 case ( out|trunc ): return "w"; 92 case ( out|trunc |noreplace): return "wx"; 93 case ( out |app ): return "a"; 94 case ( app ): return "a"; 95 case (in ): return "r"; 96 case (in|out ): return "r+"; 97 case (in|out|trunc ): return "w+"; 98 case (in|out|trunc |noreplace): return "w+x"; 99 case (in|out |app ): return "a+"; 100 case (in |app ): return "a+"; 101 102 case ( out |binary ): return "wb"; 103 case ( out |binary|noreplace): return "wbx"; 104 case ( out |app|binary ): return "ab"; 105 case ( app|binary ): return "ab"; 106 case ( out|trunc |binary ): return "wb"; 107 case (in |binary ): return "rb"; 108 case (in|out |binary ): return "r+b"; 109 case (in|out|trunc |binary ): return "w+b"; 110 case (in|out|trunc |binary|noreplace): return "w+bx"; 111 case (in|out |app|binary ): return "a+b"; 112 case (in |app|binary ): return "a+b"; 113 114 default: return 0; // invalid 115 } 116 } 117 118 // Wrapper handling partial write. 119 static std::streamsize 120 #ifdef _GLIBCXX_USE_STDIO_PURE xwrite(FILE * __file,const char * __s,std::streamsize __n)121 xwrite(FILE *__file, const char* __s, std::streamsize __n) 122 #else 123 xwrite(int __fd, const char* __s, std::streamsize __n) 124 #endif 125 { 126 std::streamsize __nleft = __n; 127 128 for (;;) 129 { 130 #ifdef _GLIBCXX_USE_STDIO_PURE 131 const std::streamsize __ret = fwrite(__s, 1, __nleft, __file); 132 #else 133 const std::streamsize __ret = write(__fd, __s, __nleft); 134 #endif 135 if (__ret == -1L && errno == EINTR) 136 continue; 137 if (__ret == -1L) 138 break; 139 140 __nleft -= __ret; 141 if (__nleft == 0) 142 break; 143 144 __s += __ret; 145 } 146 147 return __n - __nleft; 148 } 149 150 #if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE) 151 // Wrapper handling partial writev. 152 static std::streamsize xwritev(int __fd,const char * __s1,std::streamsize __n1,const char * __s2,std::streamsize __n2)153 xwritev(int __fd, const char* __s1, std::streamsize __n1, 154 const char* __s2, std::streamsize __n2) 155 { 156 std::streamsize __nleft = __n1 + __n2; 157 std::streamsize __n1_left = __n1; 158 159 struct iovec __iov[2]; 160 __iov[1].iov_base = const_cast<char*>(__s2); 161 __iov[1].iov_len = __n2; 162 163 for (;;) 164 { 165 __iov[0].iov_base = const_cast<char*>(__s1); 166 __iov[0].iov_len = __n1_left; 167 168 const std::streamsize __ret = writev(__fd, __iov, 2); 169 if (__ret == -1L && errno == EINTR) 170 continue; 171 if (__ret == -1L) 172 break; 173 174 __nleft -= __ret; 175 if (__nleft == 0) 176 break; 177 178 const std::streamsize __off = __ret - __n1_left; 179 if (__off >= 0) 180 { 181 __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off); 182 break; 183 } 184 185 __s1 += __ret; 186 __n1_left -= __ret; 187 } 188 189 return __n1 + __n2 - __nleft; 190 } 191 #endif 192 } // anonymous namespace 193 194 195 namespace std _GLIBCXX_VISIBILITY(default) 196 { 197 _GLIBCXX_BEGIN_NAMESPACE_VERSION 198 199 // Definitions for __basic_file<char>. __basic_file(__c_lock *)200 __basic_file<char>::__basic_file(__c_lock* /*__lock*/) throw() 201 : _M_cfile(NULL), _M_cfile_created(false) { } 202 ~__basic_file()203 __basic_file<char>::~__basic_file() 204 { this->close(); } 205 206 __basic_file<char>* sys_open(__c_file * __file,ios_base::openmode __mode)207 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode __mode) 208 { 209 __basic_file* __ret = NULL; 210 if (!this->is_open() && __file) 211 { 212 int __err, __save_errno = errno; 213 // POSIX guarantees that fflush sets errno on error, but C doesn't. 214 errno = 0; 215 do 216 __err = (__mode == std::ios_base::in ? 0 : fflush(__file)); 217 while (__err && errno == EINTR); 218 errno = __save_errno; 219 if (!__err) 220 { 221 _M_cfile = __file; 222 _M_cfile_created = false; 223 __ret = this; 224 } 225 } 226 return __ret; 227 } 228 229 __basic_file<char>* sys_open(int __fd,ios_base::openmode __mode)230 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode) throw () 231 { 232 __basic_file* __ret = NULL; 233 const char* __c_mode = fopen_mode(__mode); 234 if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode))) 235 { 236 char* __buf = NULL; 237 _M_cfile_created = true; 238 if (__fd == 0) 239 setvbuf(_M_cfile, __buf, _IONBF, 0); 240 __ret = this; 241 } 242 return __ret; 243 } 244 245 __basic_file<char>* open(const char * __name,ios_base::openmode __mode,int)246 __basic_file<char>::open(const char* __name, ios_base::openmode __mode, 247 int /*__prot*/) 248 { 249 __basic_file* __ret = NULL; 250 const char* __c_mode = fopen_mode(__mode); 251 if (__c_mode && !this->is_open()) 252 { 253 #ifdef _GLIBCXX_USE_LFS 254 if ((_M_cfile = fopen64(__name, __c_mode))) 255 #else 256 if ((_M_cfile = fopen(__name, __c_mode))) 257 #endif 258 { 259 _M_cfile_created = true; 260 __ret = this; 261 } 262 } 263 return __ret; 264 } 265 266 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T 267 __basic_file<char>* open(const wchar_t * __name,ios_base::openmode __mode)268 __basic_file<char>::open(const wchar_t* __name, ios_base::openmode __mode) 269 { 270 __basic_file* __ret = NULL; 271 const char* __c_mode = fopen_mode(__mode); 272 if (__c_mode && !this->is_open()) 273 { 274 wchar_t __wc_mode[4] = { }; 275 int __i = 0; 276 do 277 { 278 switch(__c_mode[__i]) { 279 case 'a': __wc_mode[__i] = L'a'; break; 280 case 'b': __wc_mode[__i] = L'b'; break; 281 case 'r': __wc_mode[__i] = L'r'; break; 282 case 'w': __wc_mode[__i] = L'w'; break; 283 case '+': __wc_mode[__i] = L'+'; break; 284 default: return __ret; 285 } 286 } 287 while (__c_mode[++__i]); 288 289 if ((_M_cfile = _wfopen(__name, __wc_mode))) 290 { 291 _M_cfile_created = true; 292 __ret = this; 293 } 294 } 295 return __ret; 296 } 297 #endif 298 299 bool is_open() const300 __basic_file<char>::is_open() const throw () 301 { return _M_cfile != 0; } 302 303 #ifndef _GLIBCCXX_USE_STDIO_PURE 304 int fd()305 __basic_file<char>::fd() throw () 306 { return fileno(_M_cfile); } 307 #endif 308 309 __c_file* file()310 __basic_file<char>::file() throw () 311 { return _M_cfile; } 312 313 __basic_file<char>* close()314 __basic_file<char>::close() 315 { 316 __basic_file* __ret = static_cast<__basic_file*>(NULL); 317 if (this->is_open()) 318 { 319 int __err = 0; 320 if (_M_cfile_created) 321 __err = fclose(_M_cfile); 322 _M_cfile = 0; 323 if (!__err) 324 __ret = this; 325 } 326 return __ret; 327 } 328 329 streamsize xsgetn(char * __s,streamsize __n)330 __basic_file<char>::xsgetn(char* __s, streamsize __n) 331 { 332 streamsize __ret; 333 do 334 #ifdef _GLIBCXX_USE_STDIO_PURE 335 __ret = fread(__s, 1, __n, this->file()); 336 #else 337 __ret = read(this->fd(), __s, __n); 338 #endif 339 while (__ret == -1L && errno == EINTR); 340 return __ret; 341 } 342 343 streamsize xsputn(const char * __s,streamsize __n)344 __basic_file<char>::xsputn(const char* __s, streamsize __n) 345 { 346 #ifdef _GLIBCXX_USE_STDIO_PURE 347 return xwrite(this->file(), __s, __n); 348 #else 349 return xwrite(this->fd(), __s, __n); 350 #endif 351 } 352 353 streamsize xsputn_2(const char * __s1,streamsize __n1,const char * __s2,streamsize __n2)354 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1, 355 const char* __s2, streamsize __n2) 356 { 357 streamsize __ret = 0; 358 #if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE) 359 __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2); 360 #else 361 if (__n1) 362 #ifdef _GLIBCXX_USE_STDIO_PURE 363 __ret = xwrite(this->file(), __s1, __n1); 364 #else 365 __ret = xwrite(this->fd(), __s1, __n1); 366 #endif 367 368 if (__ret == __n1) 369 #ifdef _GLIBCXX_USE_STDIO_PURE 370 __ret += xwrite(this->file(), __s2, __n2); 371 #else 372 __ret += xwrite(this->fd(), __s2, __n2); 373 #endif 374 #endif 375 return __ret; 376 } 377 378 streamoff seekoff(streamoff __off,ios_base::seekdir __way)379 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw () 380 { 381 #ifdef _GLIBCXX_USE_LFS 382 return lseek64(this->fd(), __off, __way); 383 #else 384 if (__off > numeric_limits<off_t>::max() 385 || __off < numeric_limits<off_t>::min()) 386 return -1L; 387 #ifdef _GLIBCXX_USE_STDIO_PURE 388 return fseek(this->file(), __off, __way); 389 #else 390 return lseek(this->fd(), __off, __way); 391 #endif 392 #endif 393 } 394 395 int sync()396 __basic_file<char>::sync() 397 { return fflush(_M_cfile); } 398 399 streamsize showmanyc()400 __basic_file<char>::showmanyc() 401 { 402 #if !defined(_GLIBCXX_NO_IOCTL) && !defined(_GLIBCXX_USE_STDIO_PURE) 403 #ifdef FIONREAD 404 // Pipes and sockets. 405 int __num = 0; 406 int __r = ioctl(this->fd(), FIONREAD, &__num); 407 if (!__r && __num >= 0) 408 return __num; 409 #endif 410 #endif 411 412 #if defined(_GLIBCXX_HAVE_POLL) && !defined(_GLIBCXX_USE_STDIO_PURE) 413 // Cheap test. 414 struct pollfd __pfd[1]; 415 __pfd[0].fd = this->fd(); 416 __pfd[0].events = POLLIN; 417 if (poll(__pfd, 1, 0) <= 0) 418 return 0; 419 #endif 420 421 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG) 422 // Regular files. 423 #ifdef _GLIBCXX_USE_LFS 424 struct stat64 __buffer; 425 const int __err = fstat64(this->fd(), &__buffer); 426 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode)) 427 { 428 const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0, 429 ios_base::cur); 430 return std::min(__off, streamoff(numeric_limits<streamsize>::max())); 431 } 432 #else 433 struct stat __buffer; 434 const int __err = fstat(this->fd(), &__buffer); 435 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode)) 436 #ifdef _GLIBCXX_USE_STDIO_PURE 437 return __buffer.st_size - fseek(this->file(), 0, ios_base::cur); 438 #else 439 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur); 440 #endif 441 #endif 442 #endif 443 return 0; 444 } 445 446 _GLIBCXX_END_NAMESPACE_VERSION 447 } // namespace 448 449