1 // Wrapper of C-language FILE struct -*- C++ -*-
2 
3 // Copyright (C) 2000-2020 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       };
83 
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))
87       {
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+";
97 
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";
107 
108       default: return 0; // invalid
109       }
110   }
111 
112   // Wrapper handling partial write.
113   static std::streamsize
xwrite(int __fd,const char * __s,std::streamsize __n)114   xwrite(int __fd, const char* __s, std::streamsize __n)
115   {
116     std::streamsize __nleft = __n;
117 
118     for (;;)
119       {
120 	const std::streamsize __ret = write(__fd, __s, __nleft);
121 	if (__ret == -1L && errno == EINTR)
122 	  continue;
123 	if (__ret == -1L)
124 	  break;
125 
126 	__nleft -= __ret;
127 	if (__nleft == 0)
128 	  break;
129 
130 	__s += __ret;
131       }
132 
133     return __n - __nleft;
134   }
135 
136 #ifdef _GLIBCXX_HAVE_WRITEV
137   // Wrapper handling partial writev.
138   static std::streamsize
xwritev(int __fd,const char * __s1,std::streamsize __n1,const char * __s2,std::streamsize __n2)139   xwritev(int __fd, const char* __s1, std::streamsize __n1,
140 	  const char* __s2, std::streamsize __n2)
141   {
142     std::streamsize __nleft = __n1 + __n2;
143     std::streamsize __n1_left = __n1;
144 
145     struct iovec __iov[2];
146     __iov[1].iov_base = const_cast<char*>(__s2);
147     __iov[1].iov_len = __n2;
148 
149     for (;;)
150       {
151 	__iov[0].iov_base = const_cast<char*>(__s1);
152 	__iov[0].iov_len = __n1_left;
153 
154 	const std::streamsize __ret = writev(__fd, __iov, 2);
155 	if (__ret == -1L && errno == EINTR)
156 	  continue;
157 	if (__ret == -1L)
158 	  break;
159 
160 	__nleft -= __ret;
161 	if (__nleft == 0)
162 	  break;
163 
164 	const std::streamsize __off = __ret - __n1_left;
165 	if (__off >= 0)
166 	  {
167 	    __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
168 	    break;
169 	  }
170 
171 	__s1 += __ret;
172 	__n1_left -= __ret;
173       }
174 
175     return __n1 + __n2 - __nleft;
176   }
177 #endif
178 } // anonymous namespace
179 
180 
181 namespace std _GLIBCXX_VISIBILITY(default)
182 {
183 _GLIBCXX_BEGIN_NAMESPACE_VERSION
184 
185   // Definitions for __basic_file<char>.
__basic_file(__c_lock *)186   __basic_file<char>::__basic_file(__c_lock* /*__lock*/) throw()
187   : _M_cfile(NULL), _M_cfile_created(false) { }
188 
~__basic_file()189   __basic_file<char>::~__basic_file()
190   { this->close(); }
191 
192   __basic_file<char>*
sys_open(__c_file * __file,ios_base::openmode)193   __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
194   {
195     __basic_file* __ret = NULL;
196     if (!this->is_open() && __file)
197       {
198 	int __err, __save_errno = errno;
199 	// POSIX guarantees that fflush sets errno on error, but C doesn't.
200 	errno = 0;
201 	do
202 	  __err = fflush(__file);
203 	while (__err && errno == EINTR);
204 	errno = __save_errno;
205 	if (!__err)
206 	  {
207 	    _M_cfile = __file;
208 	    _M_cfile_created = false;
209 	    __ret = this;
210 	  }
211       }
212     return __ret;
213   }
214 
215   __basic_file<char>*
sys_open(int __fd,ios_base::openmode __mode)216   __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode) throw ()
217   {
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)))
221       {
222 	char* __buf = NULL;
223 	_M_cfile_created = true;
224 	if (__fd == 0)
225 	  setvbuf(_M_cfile, __buf, _IONBF, 0);
226 	__ret = this;
227       }
228     return __ret;
229   }
230 
231   __basic_file<char>*
open(const char * __name,ios_base::openmode __mode,int)232   __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
233 			   int /*__prot*/)
234   {
235     __basic_file* __ret = NULL;
236     const char* __c_mode = fopen_mode(__mode);
237     if (__c_mode && !this->is_open())
238       {
239 #ifdef _GLIBCXX_USE_LFS
240 	if ((_M_cfile = fopen64(__name, __c_mode)))
241 #else
242 	if ((_M_cfile = fopen(__name, __c_mode)))
243 #endif
244 	  {
245 	    _M_cfile_created = true;
246 	    __ret = this;
247 	  }
248       }
249     return __ret;
250   }
251 
252 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
253   __basic_file<char>*
open(const wchar_t * __name,ios_base::openmode __mode)254   __basic_file<char>::open(const wchar_t* __name, ios_base::openmode __mode)
255   {
256     __basic_file* __ret = NULL;
257     const char* __c_mode = fopen_mode(__mode);
258     if (__c_mode && !this->is_open())
259       {
260 	wchar_t __wc_mode[4] = { };
261 	int __i = 0;
262 	do
263 	  {
264 	    switch(__c_mode[__i]) {
265 	    case 'a': __wc_mode[__i] = L'a'; break;
266 	    case 'b': __wc_mode[__i] = L'b'; break;
267 	    case 'r': __wc_mode[__i] = L'r'; break;
268 	    case 'w': __wc_mode[__i] = L'w'; break;
269 	    case '+': __wc_mode[__i] = L'+'; break;
270 	    default: return __ret;
271 	    }
272 	  }
273 	while (__c_mode[++__i]);
274 
275 	if ((_M_cfile = _wfopen(__name, __wc_mode)))
276 	  {
277 	    _M_cfile_created = true;
278 	    __ret = this;
279 	  }
280       }
281     return __ret;
282   }
283 #endif
284 
285   bool
is_open() const286   __basic_file<char>::is_open() const throw ()
287   { return _M_cfile != 0; }
288 
289   int
fd()290   __basic_file<char>::fd() throw ()
291   { return fileno(_M_cfile); }
292 
293   __c_file*
file()294   __basic_file<char>::file() throw ()
295   { return _M_cfile; }
296 
297   __basic_file<char>*
close()298   __basic_file<char>::close()
299   {
300     __basic_file* __ret = static_cast<__basic_file*>(NULL);
301     if (this->is_open())
302       {
303 	int __err = 0;
304 	if (_M_cfile_created)
305 	  __err = fclose(_M_cfile);
306 	_M_cfile = 0;
307 	if (!__err)
308 	  __ret = this;
309       }
310     return __ret;
311   }
312 
313   streamsize
xsgetn(char * __s,streamsize __n)314   __basic_file<char>::xsgetn(char* __s, streamsize __n)
315   {
316     streamsize __ret;
317     do
318       __ret = read(this->fd(), __s, __n);
319     while (__ret == -1L && errno == EINTR);
320     return __ret;
321   }
322 
323   streamsize
xsputn(const char * __s,streamsize __n)324   __basic_file<char>::xsputn(const char* __s, streamsize __n)
325   { return xwrite(this->fd(), __s, __n); }
326 
327   streamsize
xsputn_2(const char * __s1,streamsize __n1,const char * __s2,streamsize __n2)328   __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
329 			       const char* __s2, streamsize __n2)
330   {
331     streamsize __ret = 0;
332 #ifdef _GLIBCXX_HAVE_WRITEV
333     __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2);
334 #else
335     if (__n1)
336       __ret = xwrite(this->fd(), __s1, __n1);
337 
338     if (__ret == __n1)
339       __ret += xwrite(this->fd(), __s2, __n2);
340 #endif
341     return __ret;
342   }
343 
344   streamoff
seekoff(streamoff __off,ios_base::seekdir __way)345   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw ()
346   {
347 #ifdef _GLIBCXX_USE_LFS
348     return lseek64(this->fd(), __off, __way);
349 #else
350     if (__off > numeric_limits<off_t>::max()
351 	|| __off < numeric_limits<off_t>::min())
352       return -1L;
353     return lseek(this->fd(), __off, __way);
354 #endif
355   }
356 
357   int
sync()358   __basic_file<char>::sync()
359   { return fflush(_M_cfile); }
360 
361   streamsize
showmanyc()362   __basic_file<char>::showmanyc()
363   {
364 #ifndef _GLIBCXX_NO_IOCTL
365 #ifdef FIONREAD
366     // Pipes and sockets.
367     int __num = 0;
368     int __r = ioctl(this->fd(), FIONREAD, &__num);
369     if (!__r && __num >= 0)
370       return __num;
371 #endif
372 #endif
373 
374 #ifdef _GLIBCXX_HAVE_POLL
375     // Cheap test.
376     struct pollfd __pfd[1];
377     __pfd[0].fd = this->fd();
378     __pfd[0].events = POLLIN;
379     if (poll(__pfd, 1, 0) <= 0)
380       return 0;
381 #endif
382 
383 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
384     // Regular files.
385 #ifdef _GLIBCXX_USE_LFS
386     struct stat64 __buffer;
387     const int __err = fstat64(this->fd(), &__buffer);
388     if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
389       {
390 	const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
391 							   ios_base::cur);
392 	return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
393       }
394 #else
395     struct stat __buffer;
396     const int __err = fstat(this->fd(), &__buffer);
397     if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
398       return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
399 #endif
400 #endif
401     return 0;
402   }
403 
404 _GLIBCXX_END_NAMESPACE_VERSION
405 } // namespace
406 
407