1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 // ISO C++ 14882: 27.8 File-based streams
29 #include <bits/basic_file.h>
33 #ifdef _GLIBCXX_HAVE_POLL
37 // Pick up ioctl on Solaris 2.8
38 #ifdef _GLIBCXX_HAVE_UNISTD_H
42 // Pick up FIONREAD on Solaris 2
43 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
45 #include <sys/ioctl.h>
48 // Pick up FIONREAD on Solaris 2.5.
49 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
50 #include <sys/filio.h>
53 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
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)
62 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
66 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
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.
73 fopen_mode(std::ios_base::openmode mode)
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
84 // _GLIBCXX_RESOLVE_LIB_DEFECTS
85 // 596. 27.8.1.3 Table 112 omits "a+" and "a+b" modes.
86 switch (mode & (in|out|trunc|app|binary))
88 case ( out ): return "w";
89 case ( out |app ): return "a";
90 case ( app ): return "a";
91 case ( out|trunc ): return "w";
92 case (in ): return "r";
93 case (in|out ): return "r+";
94 case (in|out|trunc ): return "w+";
95 case (in|out |app ): return "a+";
96 case (in |app ): return "a+";
98 case ( out |binary): return "wb";
99 case ( out |app|binary): return "ab";
100 case ( app|binary): return "ab";
101 case ( out|trunc |binary): return "wb";
102 case (in |binary): return "rb";
103 case (in|out |binary): return "r+b";
104 case (in|out|trunc |binary): return "w+b";
105 case (in|out |app|binary): return "a+b";
106 case (in |app|binary): return "a+b";
108 default: return 0; // invalid
112 // Wrapper handling partial write.
113 static std::streamsize
114 xwrite(int __fd, const char* __s, std::streamsize __n)
116 std::streamsize __nleft = __n;
120 const std::streamsize __ret = write(__fd, __s, __nleft);
121 if (__ret == -1L && errno == EINTR)
133 return __n - __nleft;
136 #ifdef _GLIBCXX_HAVE_WRITEV
137 // Wrapper handling partial writev.
138 static std::streamsize
139 xwritev(int __fd, const char* __s1, std::streamsize __n1,
140 const char* __s2, std::streamsize __n2)
142 std::streamsize __nleft = __n1 + __n2;
143 std::streamsize __n1_left = __n1;
145 struct iovec __iov[2];
146 __iov[1].iov_base = const_cast<char*>(__s2);
147 __iov[1].iov_len = __n2;
151 __iov[0].iov_base = const_cast<char*>(__s1);
152 __iov[0].iov_len = __n1_left;
154 const std::streamsize __ret = writev(__fd, __iov, 2);
155 if (__ret == -1L && errno == EINTR)
164 const std::streamsize __off = __ret - __n1_left;
167 __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
175 return __n1 + __n2 - __nleft;
178 } // anonymous namespace
181 namespace std _GLIBCXX_VISIBILITY(default)
183 _GLIBCXX_BEGIN_NAMESPACE_VERSION
185 // Definitions for __basic_file<char>.
186 __basic_file<char>::__basic_file(__c_lock* /*__lock*/) throw()
187 : _M_cfile(NULL), _M_cfile_created(false) { }
189 __basic_file<char>::~__basic_file()
193 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
195 __basic_file* __ret = NULL;
196 if (!this->is_open() && __file)
198 int __err, __save_errno = errno;
199 // POSIX guarantees that fflush sets errno on error, but C doesn't.
202 __err = fflush(__file);
203 while (__err && errno == EINTR);
204 errno = __save_errno;
208 _M_cfile_created = false;
216 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode) throw ()
218 __basic_file* __ret = NULL;
219 const char* __c_mode = fopen_mode(__mode);
220 if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
223 _M_cfile_created = true;
225 setvbuf(_M_cfile, __buf, _IONBF, 0);
232 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
235 __basic_file* __ret = NULL;
236 const char* __c_mode = fopen_mode(__mode);
237 if (__c_mode && !this->is_open())
239 #ifdef _GLIBCXX_USE_LFS
240 if ((_M_cfile = fopen64(__name, __c_mode)))
242 if ((_M_cfile = fopen(__name, __c_mode)))
245 _M_cfile_created = true;
253 __basic_file<char>::is_open() const throw ()
254 { return _M_cfile != 0; }
257 __basic_file<char>::fd() throw ()
258 { return fileno(_M_cfile); }
261 __basic_file<char>::file() throw ()
265 __basic_file<char>::close()
267 __basic_file* __ret = static_cast<__basic_file*>(NULL);
271 if (_M_cfile_created)
272 __err = fclose(_M_cfile);
281 __basic_file<char>::xsgetn(char* __s, streamsize __n)
285 __ret = read(this->fd(), __s, __n);
286 while (__ret == -1L && errno == EINTR);
291 __basic_file<char>::xsputn(const char* __s, streamsize __n)
292 { return xwrite(this->fd(), __s, __n); }
295 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
296 const char* __s2, streamsize __n2)
298 streamsize __ret = 0;
299 #ifdef _GLIBCXX_HAVE_WRITEV
300 __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2);
303 __ret = xwrite(this->fd(), __s1, __n1);
306 __ret += xwrite(this->fd(), __s2, __n2);
312 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw ()
314 #ifdef _GLIBCXX_USE_LFS
315 return lseek64(this->fd(), __off, __way);
317 if (__off > numeric_limits<off_t>::max()
318 || __off < numeric_limits<off_t>::min())
320 return lseek(this->fd(), __off, __way);
325 __basic_file<char>::sync()
326 { return fflush(_M_cfile); }
329 __basic_file<char>::showmanyc()
331 #ifndef _GLIBCXX_NO_IOCTL
333 // Pipes and sockets.
335 int __r = ioctl(this->fd(), FIONREAD, &__num);
336 if (!__r && __num >= 0)
341 #ifdef _GLIBCXX_HAVE_POLL
343 struct pollfd __pfd[1];
344 __pfd[0].fd = this->fd();
345 __pfd[0].events = POLLIN;
346 if (poll(__pfd, 1, 0) <= 0)
350 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
352 #ifdef _GLIBCXX_USE_LFS
353 struct stat64 __buffer;
354 const int __err = fstat64(this->fd(), &__buffer);
355 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
357 const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
359 return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
362 struct stat __buffer;
363 const int __err = fstat(this->fd(), &__buffer);
364 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
365 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
371 _GLIBCXX_END_NAMESPACE_VERSION