1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 27.8 File-based streams
35 #include <bits/basic_file.h>
39 #ifdef _GLIBCXX_HAVE_POLL
43 // Pick up ioctl on Solaris 2.8
44 #ifdef _GLIBCXX_HAVE_UNISTD_H
48 // Pick up FIONREAD on Solaris 2
49 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
54 // Pick up FIONREAD on Solaris 2.5.
55 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
56 #include <sys/filio.h>
59 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
63 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
64 # include <sys/stat.h>
65 # ifdef _GLIBCXX_HAVE_S_ISREG
66 # define _GLIBCXX_ISREG(x) S_ISREG(x)
68 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
72 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
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
90 // _GLIBCXX_RESOLVE_LIB_DEFECTS
91 // 596. 27.8.1.3 Table 112 omits "a+" and "a+b" modes.
92 switch (mode
& (in
|out
|trunc
|app
|binary
))
94 case ( out
): return "w";
95 case ( out
|app
): return "a";
96 case ( app
): return "a";
97 case ( out
|trunc
): return "w";
98 case (in
): return "r";
99 case (in
|out
): return "r+";
100 case (in
|out
|trunc
): return "w+";
101 case (in
|out
|app
): return "a+";
102 case (in
|app
): return "a+";
104 case ( out
|binary
): return "wb";
105 case ( out
|app
|binary
): return "ab";
106 case ( app
|binary
): return "ab";
107 case ( out
|trunc
|binary
): return "wb";
108 case (in
|binary
): return "rb";
109 case (in
|out
|binary
): return "r+b";
110 case (in
|out
|trunc
|binary
): return "w+b";
111 case (in
|out
|app
|binary
): return "a+b";
112 case (in
|app
|binary
): return "a+b";
114 default: return 0; // invalid
118 // Wrapper handling partial write.
119 static std::streamsize
120 xwrite(int __fd
, const char* __s
, std::streamsize __n
)
122 std::streamsize __nleft
= __n
;
126 const std::streamsize __ret
= write(__fd
, __s
, __nleft
);
127 if (__ret
== -1L && errno
== EINTR
)
139 return __n
- __nleft
;
142 #ifdef _GLIBCXX_HAVE_WRITEV
143 // Wrapper handling partial writev.
144 static std::streamsize
145 xwritev(int __fd
, const char* __s1
, std::streamsize __n1
,
146 const char* __s2
, std::streamsize __n2
)
148 std::streamsize __nleft
= __n1
+ __n2
;
149 std::streamsize __n1_left
= __n1
;
151 struct iovec __iov
[2];
152 __iov
[1].iov_base
= const_cast<char*>(__s2
);
153 __iov
[1].iov_len
= __n2
;
157 __iov
[0].iov_base
= const_cast<char*>(__s1
);
158 __iov
[0].iov_len
= __n1_left
;
160 const std::streamsize __ret
= writev(__fd
, __iov
, 2);
161 if (__ret
== -1L && errno
== EINTR
)
170 const std::streamsize __off
= __ret
- __n1_left
;
173 __nleft
-= xwrite(__fd
, __s2
+ __off
, __n2
- __off
);
181 return __n1
+ __n2
- __nleft
;
184 } // anonymous namespace
187 _GLIBCXX_BEGIN_NAMESPACE(std
)
189 // Definitions for __basic_file<char>.
190 __basic_file
<char>::__basic_file(__c_lock
* /*__lock*/)
191 : _M_cfile(NULL
), _M_cfile_created(false) { }
193 __basic_file
<char>::~__basic_file()
197 __basic_file
<char>::sys_open(__c_file
* __file
, ios_base::openmode
)
199 __basic_file
* __ret
= NULL
;
200 if (!this->is_open() && __file
)
205 __err
= this->sync();
206 while (__err
&& errno
== EINTR
);
210 _M_cfile_created
= false;
218 __basic_file
<char>::sys_open(int __fd
, ios_base::openmode __mode
)
220 __basic_file
* __ret
= NULL
;
221 const char* __c_mode
= fopen_mode(__mode
);
222 if (__c_mode
&& !this->is_open() && (_M_cfile
= fdopen(__fd
, __c_mode
)))
225 _M_cfile_created
= true;
227 setvbuf(_M_cfile
, __buf
, _IONBF
, 0);
234 __basic_file
<char>::open(const char* __name
, ios_base::openmode __mode
,
237 __basic_file
* __ret
= NULL
;
238 const char* __c_mode
= fopen_mode(__mode
);
239 if (__c_mode
&& !this->is_open())
241 #ifdef _GLIBCXX_USE_LFS
242 if ((_M_cfile
= fopen64(__name
, __c_mode
)))
244 if ((_M_cfile
= fopen(__name
, __c_mode
)))
247 _M_cfile_created
= true;
255 __basic_file
<char>::is_open() const
256 { return _M_cfile
!= 0; }
259 __basic_file
<char>::fd()
260 { return fileno(_M_cfile
); }
263 __basic_file
<char>::file()
267 __basic_file
<char>::close()
269 __basic_file
* __ret
= static_cast<__basic_file
*>(NULL
);
273 if (_M_cfile_created
)
275 // In general, no need to zero errno in advance if checking
276 // for error first. However, C89/C99 (at variance with IEEE
277 // 1003.1, f.i.) do not mandate that fclose must set errno
281 __err
= fclose(_M_cfile
);
282 while (__err
&& errno
== EINTR
);
292 __basic_file
<char>::xsgetn(char* __s
, streamsize __n
)
296 __ret
= read(this->fd(), __s
, __n
);
297 while (__ret
== -1L && errno
== EINTR
);
302 __basic_file
<char>::xsputn(const char* __s
, streamsize __n
)
303 { return xwrite(this->fd(), __s
, __n
); }
306 __basic_file
<char>::xsputn_2(const char* __s1
, streamsize __n1
,
307 const char* __s2
, streamsize __n2
)
309 streamsize __ret
= 0;
310 #ifdef _GLIBCXX_HAVE_WRITEV
311 __ret
= xwritev(this->fd(), __s1
, __n1
, __s2
, __n2
);
314 __ret
= xwrite(this->fd(), __s1
, __n1
);
317 __ret
+= xwrite(this->fd(), __s2
, __n2
);
323 __basic_file
<char>::seekoff(streamoff __off
, ios_base::seekdir __way
)
325 #ifdef _GLIBCXX_USE_LFS
326 return lseek64(this->fd(), __off
, __way
);
328 if (__off
> numeric_limits
<off_t
>::max()
329 || __off
< numeric_limits
<off_t
>::min())
331 return lseek(this->fd(), __off
, __way
);
336 __basic_file
<char>::sync()
337 { return fflush(_M_cfile
); }
340 __basic_file
<char>::showmanyc()
343 // Pipes and sockets.
344 #ifdef _GLIBCXX_FIONREAD_TAKES_OFF_T
349 int __r
= ioctl(this->fd(), FIONREAD
, &__num
);
350 if (!__r
&& __num
>= 0)
354 #ifdef _GLIBCXX_HAVE_POLL
356 struct pollfd __pfd
[1];
357 __pfd
[0].fd
= this->fd();
358 __pfd
[0].events
= POLLIN
;
359 if (poll(__pfd
, 1, 0) <= 0)
363 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
365 #ifdef _GLIBCXX_USE_LFS
366 struct stat64 __buffer
;
367 const int __err
= fstat64(this->fd(), &__buffer
);
368 if (!__err
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
370 const streamoff __off
= __buffer
.st_size
- lseek64(this->fd(), 0,
372 return std::min(__off
, streamoff(numeric_limits
<streamsize
>::max()));
375 struct stat __buffer
;
376 const int __err
= fstat(this->fd(), &__buffer
);
377 if (!__err
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
378 return __buffer
.st_size
- lseek(this->fd(), 0, ios_base::cur
);
384 _GLIBCXX_END_NAMESPACE