1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000-2024 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/largefile-config.h>
30 #include <bits/basic_file.h>
34 #ifdef _GLIBCXX_HAVE_POLL
38 // Pick up ioctl on Solaris 2.8
39 #ifdef _GLIBCXX_HAVE_UNISTD_H
43 // Pick up FIONREAD on Solaris 2
44 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
49 // Pick up FIONREAD on Solaris 2.5.
50 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
51 #include <sys/filio.h>
54 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
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)
63 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
67 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
69 #if _GLIBCXX_USE__GET_OSFHANDLE
70 # include <stdint.h> // For intptr_t
71 # include <io.h> // For _get_osfhandle
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.
79 fopen_mode(std::ios_base::openmode mode
)
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
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
))
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+";
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";
120 default: return 0; // invalid
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
)
129 xwrite(int __fd
, const char* __s
, std::streamsize __n
)
132 std::streamsize __nleft
= __n
;
136 #ifdef _GLIBCXX_USE_STDIO_PURE
137 const std::streamsize __ret
= fwrite(__s
, 1, __nleft
, __file
);
138 if (__ret
== 0 && ferror(__file
))
141 const std::streamsize __ret
= write(__fd
, __s
, __nleft
);
142 if (__ret
== -1L && errno
== EINTR
)
154 return __n
- __nleft
;
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
)
163 std::streamsize __nleft
= __n1
+ __n2
;
164 std::streamsize __n1_left
= __n1
;
166 struct iovec __iov
[2];
167 __iov
[1].iov_base
= const_cast<char*>(__s2
);
168 __iov
[1].iov_len
= __n2
;
172 __iov
[0].iov_base
= const_cast<char*>(__s1
);
173 __iov
[0].iov_len
= __n1_left
;
175 const std::streamsize __ret
= writev(__fd
, __iov
, 2);
176 if (__ret
== -1L && errno
== EINTR
)
185 const std::streamsize __off
= __ret
- __n1_left
;
188 __nleft
-= xwrite(__fd
, __s2
+ __off
, __n2
- __off
);
196 return __n1
+ __n2
- __nleft
;
199 } // anonymous namespace
202 namespace std
_GLIBCXX_VISIBILITY(default)
204 _GLIBCXX_BEGIN_NAMESPACE_VERSION
206 // Definitions for __basic_file<char>.
207 __basic_file
<char>::__basic_file(__c_lock
* /*__lock*/) throw()
208 : _M_cfile(NULL
), _M_cfile_created(false) { }
210 __basic_file
<char>::~__basic_file()
214 __basic_file
<char>::sys_open(__c_file
* __file
, ios_base::openmode
)
216 __basic_file
* __ret
= NULL
;
217 if (!this->is_open() && __file
)
219 int __err
, __save_errno
= errno
;
220 // POSIX guarantees that fflush sets errno on error, but C doesn't.
223 __err
= fflush(__file
);
224 while (__err
&& errno
== EINTR
);
225 errno
= __save_errno
;
229 _M_cfile_created
= false;
237 __basic_file
<char>::sys_open(int __fd
, ios_base::openmode __mode
) throw ()
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
)))
244 _M_cfile_created
= true;
246 setvbuf(_M_cfile
, __buf
, _IONBF
, 0);
253 __basic_file
<char>::open(const char* __name
, ios_base::openmode __mode
,
256 __basic_file
* __ret
= NULL
;
257 const char* __c_mode
= fopen_mode(__mode
);
258 if (__c_mode
&& !this->is_open())
260 if ((_M_cfile
= fopen(__name
, __c_mode
)))
262 _M_cfile_created
= true;
269 #if _GLIBCXX_HAVE__WFOPEN && _GLIBCXX_USE_WCHAR_T
271 __basic_file
<char>::open(const wchar_t* __name
, ios_base::openmode __mode
)
273 __basic_file
* __ret
= NULL
;
274 const char* __c_mode
= fopen_mode(__mode
);
275 if (__c_mode
&& !this->is_open())
277 wchar_t __wc_mode
[4] = { };
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
;
290 while (__c_mode
[++__i
]);
292 if ((_M_cfile
= _wfopen(__name
, __wc_mode
)))
294 _M_cfile_created
= true;
303 __basic_file
<char>::is_open() const throw ()
304 { return _M_cfile
!= 0; }
306 #ifndef _GLIBCCXX_USE_STDIO_PURE
308 __basic_file
<char>::fd() throw ()
309 { return fileno(_M_cfile
); }
313 __basic_file
<char>::file() throw ()
317 __basic_file
<char>::close()
319 __basic_file
* __ret
= static_cast<__basic_file
*>(NULL
);
323 if (_M_cfile_created
)
324 __err
= fclose(_M_cfile
);
333 __basic_file
<char>::xsgetn(char* __s
, streamsize __n
)
336 #ifdef _GLIBCXX_USE_STDIO_PURE
337 __ret
= fread(__s
, 1, __n
, this->file());
338 if (__ret
== 0 && ferror(this->file()))
342 #ifdef _GLIBCXX_MAX_READ_SIZE
343 if (__builtin_expect(__n
> _GLIBCXX_MAX_READ_SIZE
, 0))
344 __n
= _GLIBCXX_MAX_READ_SIZE
;
348 __ret
= read(this->fd(), __s
, __n
);
349 while (__ret
== -1L && errno
== EINTR
);
355 __basic_file
<char>::xsputn(const char* __s
, streamsize __n
)
357 #ifdef _GLIBCXX_USE_STDIO_PURE
358 return xwrite(this->file(), __s
, __n
);
360 return xwrite(this->fd(), __s
, __n
);
365 __basic_file
<char>::xsputn_2(const char* __s1
, streamsize __n1
,
366 const char* __s2
, streamsize __n2
)
368 streamsize __ret
= 0;
369 #if defined(_GLIBCXX_HAVE_WRITEV) && !defined(_GLIBCXX_USE_STDIO_PURE)
370 __ret
= xwritev(this->fd(), __s1
, __n1
, __s2
, __n2
);
373 #ifdef _GLIBCXX_USE_STDIO_PURE
374 __ret
= xwrite(this->file(), __s1
, __n1
);
376 __ret
= xwrite(this->fd(), __s1
, __n1
);
380 #ifdef _GLIBCXX_USE_STDIO_PURE
381 __ret
+= xwrite(this->file(), __s2
, __n2
);
383 __ret
+= xwrite(this->fd(), __s2
, __n2
);
392 get_file_offset(__basic_file
<char>* __f
)
394 #ifdef _GLIBCXX_USE_STDIO_PURE
395 # ifdef _GLIBCXX_USE_FSEEKO_FTELLO
396 return ftello(__f
->file());
398 return ftell(__f
->file());
401 return lseek(__f
->fd(), 0, (int)ios_base::cur
);
407 __basic_file
<char>::seekoff(streamoff __off
, ios_base::seekdir __way
) throw ()
409 #ifdef _GLIBCXX_USE_STDIO_PURE
410 #ifdef _GLIBCXX_USE_FSEEKO_FTELLO
411 if _GLIBCXX17_CONSTEXPR (sizeof(off_t
) > sizeof(long))
413 if (fseeko(this->file(), __off
, __way
))
419 if (__off
> numeric_limits
<long>::max()
420 || __off
< numeric_limits
<long>::min())
422 if (fseek(this->file(), __off
, __way
))
425 return __way
== ios_base::beg
? __off
: std::get_file_offset(this);
427 if _GLIBCXX17_CONSTEXPR (sizeof(streamoff
) > sizeof(off_t
))
428 if (__off
> numeric_limits
<off_t
>::max()
429 || __off
< numeric_limits
<off_t
>::min())
431 return lseek(this->fd(), __off
, __way
);
436 __basic_file
<char>::sync()
437 { return fflush(_M_cfile
); }
440 __basic_file
<char>::showmanyc()
442 #if !defined(_GLIBCXX_NO_IOCTL) && !defined(_GLIBCXX_USE_STDIO_PURE)
444 // Pipes and sockets.
446 int __r
= ioctl(this->fd(), FIONREAD
, &__num
);
447 if (!__r
&& __num
>= 0)
452 #if defined(_GLIBCXX_HAVE_POLL) && !defined(_GLIBCXX_USE_STDIO_PURE)
454 struct pollfd __pfd
[1];
455 __pfd
[0].fd
= this->fd();
456 __pfd
[0].events
= POLLIN
;
457 if (poll(__pfd
, 1, 0) <= 0)
461 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
463 struct stat __buffer
;
464 const int __err
= fstat(this->fd(), &__buffer
);
465 if (!__err
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
467 const streamoff __off
= __buffer
.st_size
- std::get_file_offset(this);
468 return std::min(__off
, streamoff(numeric_limits
<streamsize
>::max()));
474 __basic_file
<char>::native_handle_type
475 __basic_file
<char>::native_handle() const noexcept
477 #ifdef _GLIBCXX_USE_STDIO_PURE
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
);
483 return fileno(_M_cfile
);
487 _GLIBCXX_END_NAMESPACE_VERSION