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