1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 // ISO C++ 14882: 27.8 File-based streams
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()
71 // Map ios_base::openmode flags to a string for use in fopen().
72 // Table of valid combinations as given in [lib.filebuf.members]/2.
74 fopen_mode(std::ios_base::openmode mode
)
78 in
= std::ios_base::in
,
79 out
= std::ios_base::out
,
80 trunc
= std::ios_base::trunc
,
81 app
= std::ios_base::app
,
82 binary
= std::ios_base::binary
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
))
89 case ( out
): return "w";
90 case ( out
|app
): return "a";
91 case ( app
): return "a";
92 case ( out
|trunc
): return "w";
93 case (in
): return "r";
94 case (in
|out
): return "r+";
95 case (in
|out
|trunc
): return "w+";
96 case (in
|out
|app
): return "a+";
97 case (in
|app
): return "a+";
99 case ( out
|binary
): return "wb";
100 case ( out
|app
|binary
): return "ab";
101 case ( app
|binary
): return "ab";
102 case ( out
|trunc
|binary
): return "wb";
103 case (in
|binary
): return "rb";
104 case (in
|out
|binary
): return "r+b";
105 case (in
|out
|trunc
|binary
): return "w+b";
106 case (in
|out
|app
|binary
): return "a+b";
107 case (in
|app
|binary
): return "a+b";
109 default: return 0; // invalid
113 // Wrapper handling partial write.
114 static std::streamsize
115 xwrite(int __fd
, const char* __s
, std::streamsize __n
)
117 std::streamsize __nleft
= __n
;
121 const std::streamsize __ret
= write(__fd
, __s
, __nleft
);
122 if (__ret
== -1L && errno
== EINTR
)
134 return __n
- __nleft
;
137 #ifdef _GLIBCXX_HAVE_WRITEV
138 // Wrapper handling partial writev.
139 static std::streamsize
140 xwritev(int __fd
, const char* __s1
, std::streamsize __n1
,
141 const char* __s2
, std::streamsize __n2
)
143 std::streamsize __nleft
= __n1
+ __n2
;
144 std::streamsize __n1_left
= __n1
;
146 struct iovec __iov
[2];
147 __iov
[1].iov_base
= const_cast<char*>(__s2
);
148 __iov
[1].iov_len
= __n2
;
152 __iov
[0].iov_base
= const_cast<char*>(__s1
);
153 __iov
[0].iov_len
= __n1_left
;
155 const std::streamsize __ret
= writev(__fd
, __iov
, 2);
156 if (__ret
== -1L && errno
== EINTR
)
165 const std::streamsize __off
= __ret
- __n1_left
;
168 __nleft
-= xwrite(__fd
, __s2
+ __off
, __n2
- __off
);
176 return __n1
+ __n2
- __nleft
;
179 } // anonymous namespace
182 _GLIBCXX_BEGIN_NAMESPACE(std
)
184 // Definitions for __basic_file<char>.
185 __basic_file
<char>::__basic_file(__c_lock
* /*__lock*/) throw()
186 : _M_cfile(NULL
), _M_cfile_created(false) { }
188 __basic_file
<char>::~__basic_file()
192 __basic_file
<char>::sys_open(__c_file
* __file
, ios_base::openmode
)
194 __basic_file
* __ret
= NULL
;
195 if (!this->is_open() && __file
)
200 __err
= this->sync();
201 while (__err
&& errno
== EINTR
);
205 _M_cfile_created
= false;
213 __basic_file
<char>::sys_open(int __fd
, ios_base::openmode __mode
) throw ()
215 __basic_file
* __ret
= NULL
;
216 const char* __c_mode
= fopen_mode(__mode
);
217 if (__c_mode
&& !this->is_open() && (_M_cfile
= fdopen(__fd
, __c_mode
)))
220 _M_cfile_created
= true;
222 setvbuf(_M_cfile
, __buf
, _IONBF
, 0);
229 __basic_file
<char>::open(const char* __name
, ios_base::openmode __mode
,
232 __basic_file
* __ret
= NULL
;
233 const char* __c_mode
= fopen_mode(__mode
);
234 if (__c_mode
&& !this->is_open())
236 #ifdef _GLIBCXX_USE_LFS
237 if ((_M_cfile
= fopen64(__name
, __c_mode
)))
239 if ((_M_cfile
= fopen(__name
, __c_mode
)))
242 _M_cfile_created
= true;
250 __basic_file
<char>::is_open() const throw ()
251 { return _M_cfile
!= 0; }
254 __basic_file
<char>::fd() throw ()
255 { return fileno(_M_cfile
); }
258 __basic_file
<char>::file() throw ()
262 __basic_file
<char>::close()
264 __basic_file
* __ret
= static_cast<__basic_file
*>(NULL
);
268 if (_M_cfile_created
)
270 // In general, no need to zero errno in advance if checking
271 // for error first. However, C89/C99 (at variance with IEEE
272 // 1003.1, f.i.) do not mandate that fclose must set errno
276 __err
= fclose(_M_cfile
);
277 while (__err
&& errno
== EINTR
);
287 __basic_file
<char>::xsgetn(char* __s
, streamsize __n
)
291 __ret
= read(this->fd(), __s
, __n
);
292 while (__ret
== -1L && errno
== EINTR
);
297 __basic_file
<char>::xsputn(const char* __s
, streamsize __n
)
298 { return xwrite(this->fd(), __s
, __n
); }
301 __basic_file
<char>::xsputn_2(const char* __s1
, streamsize __n1
,
302 const char* __s2
, streamsize __n2
)
304 streamsize __ret
= 0;
305 #ifdef _GLIBCXX_HAVE_WRITEV
306 __ret
= xwritev(this->fd(), __s1
, __n1
, __s2
, __n2
);
309 __ret
= xwrite(this->fd(), __s1
, __n1
);
312 __ret
+= xwrite(this->fd(), __s2
, __n2
);
318 __basic_file
<char>::seekoff(streamoff __off
, ios_base::seekdir __way
) throw ()
320 #ifdef _GLIBCXX_USE_LFS
321 return lseek64(this->fd(), __off
, __way
);
323 if (__off
> numeric_limits
<off_t
>::max()
324 || __off
< numeric_limits
<off_t
>::min())
326 return lseek(this->fd(), __off
, __way
);
331 __basic_file
<char>::sync()
332 { return fflush(_M_cfile
); }
335 __basic_file
<char>::showmanyc()
338 // Pipes and sockets.
339 #ifdef _GLIBCXX_FIONREAD_TAKES_OFF_T
344 int __r
= ioctl(this->fd(), FIONREAD
, &__num
);
345 if (!__r
&& __num
>= 0)
349 #ifdef _GLIBCXX_HAVE_POLL
351 struct pollfd __pfd
[1];
352 __pfd
[0].fd
= this->fd();
353 __pfd
[0].events
= POLLIN
;
354 if (poll(__pfd
, 1, 0) <= 0)
358 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
360 #ifdef _GLIBCXX_USE_LFS
361 struct stat64 __buffer
;
362 const int __err
= fstat64(this->fd(), &__buffer
);
363 if (!__err
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
365 const streamoff __off
= __buffer
.st_size
- lseek64(this->fd(), 0,
367 return std::min(__off
, streamoff(numeric_limits
<streamsize
>::max()));
370 struct stat __buffer
;
371 const int __err
= fstat(this->fd(), &__buffer
);
372 if (!__err
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
373 return __buffer
.st_size
- lseek(this->fd(), 0, ios_base::cur
);
379 _GLIBCXX_END_NAMESPACE