]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/io/basic_file_stdio.cc
1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.8 File-based streams
34 #include <bits/basic_file.h>
38 #ifdef _GLIBCXX_HAVE_POLL
42 // Pick up ioctl on Solaris 2.8
43 #ifdef _GLIBCXX_HAVE_UNISTD_H
47 // Pick up FIONREAD on Solaris 2
48 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
53 // Pick up FIONREAD on Solaris 2.5.
54 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
55 #include <sys/filio.h>
58 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
62 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
63 # include <sys/stat.h>
64 # ifdef _GLIBCXX_HAVE_S_ISREG
65 # define _GLIBCXX_ISREG(x) S_ISREG(x)
67 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
71 #include <limits> // For <off_t>::max() and min()
75 // Definitions for __basic_file<char>.
76 __basic_file
<char>::__basic_file(__c_lock
* /*__lock*/)
77 : _M_cfile(NULL
), _M_cfile_created(false) { }
79 __basic_file
<char>::~__basic_file()
83 __basic_file
<char>::_M_open_mode(ios_base::openmode __mode
, int& __p_mode
,
86 bool __testb
= __mode
& ios_base::binary
;
87 bool __testi
= __mode
& ios_base::in
;
88 bool __testo
= __mode
& ios_base::out
;
89 bool __testt
= __mode
& ios_base::trunc
;
90 bool __testa
= __mode
& ios_base::app
;
92 // Set __c_mode for use in fopen.
93 // Set __p_mode for use in open.
94 if (!__testi
&& __testo
&& !__testt
&& !__testa
)
96 strcpy(__c_mode
, "w");
97 __p_mode
= O_WRONLY
| O_CREAT
;
99 if (!__testi
&& __testo
&& !__testt
&& __testa
)
101 strcpy(__c_mode
, "a");
102 __p_mode
= O_WRONLY
| O_CREAT
| O_APPEND
;
104 if (!__testi
&& __testo
&& __testt
&& !__testa
)
106 strcpy(__c_mode
, "w");
107 __p_mode
= O_WRONLY
| O_CREAT
| O_TRUNC
;
110 if (__testi
&& !__testo
&& !__testt
&& !__testa
)
112 strcpy(__c_mode
, "r");
115 if (__testi
&& __testo
&& !__testt
&& !__testa
)
117 strcpy(__c_mode
, "r+");
118 __p_mode
= O_RDWR
| O_CREAT
;
120 if (__testi
&& __testo
&& __testt
&& !__testa
)
122 strcpy(__c_mode
, "w+");
123 __p_mode
= O_RDWR
| O_CREAT
| O_TRUNC
;
126 strcat(__c_mode
, "b");
130 __basic_file
<char>::sys_open(__c_file
* __file
, ios_base::openmode
)
132 __basic_file
* __ret
= NULL
;
133 if (!this->is_open() && __file
)
136 _M_cfile_created
= false;
143 __basic_file
<char>::sys_open(int __fd
, ios_base::openmode __mode
,
146 __basic_file
* __ret
= NULL
;
151 _M_open_mode(__mode
, __p_mode
, __rw_mode
, __c_mode
);
152 if (!this->is_open() && (_M_cfile
= fdopen(__fd
, __c_mode
)))
154 // Iff __del is true, then close will fclose the fd.
155 _M_cfile_created
= __del
;
158 setvbuf(_M_cfile
, reinterpret_cast<char*>(NULL
), _IONBF
, 0);
166 __basic_file
<char>::open(const char* __name
, ios_base::openmode __mode
,
169 __basic_file
* __ret
= NULL
;
174 _M_open_mode(__mode
, __p_mode
, __rw_mode
, __c_mode
);
176 if (!this->is_open())
178 #ifdef _GLIBCXX_USE_LFS
179 if ((_M_cfile
= fopen64(__name
, __c_mode
)))
181 if ((_M_cfile
= fopen(__name
, __c_mode
)))
184 _M_cfile_created
= true;
192 __basic_file
<char>::is_open() const
193 { return _M_cfile
!= 0; }
196 __basic_file
<char>::fd()
197 { return fileno(_M_cfile
) ; }
200 __basic_file
<char>::close()
202 __basic_file
* __retval
= static_cast<__basic_file
*>(NULL
);
205 if (_M_cfile_created
)
216 __basic_file
<char>::xsgetn(char* __s
, streamsize __n
)
220 __ret
= read(this->fd(), __s
, __n
);
221 while (__ret
== -1L && errno
== EINTR
);
226 __basic_file
<char>::xsputn(const char* __s
, streamsize __n
)
230 __ret
= write(this->fd(), __s
, __n
);
231 while (__ret
== -1L && errno
== EINTR
);
236 __basic_file
<char>::xsputn_2(const char* __s1
, streamsize __n1
,
237 const char* __s2
, streamsize __n2
)
239 streamsize __ret
= 0;
240 #ifdef _GLIBCXX_HAVE_WRITEV
241 struct iovec __iov
[2];
242 __iov
[0].iov_base
= const_cast<char*>(__s1
);
243 __iov
[0].iov_len
= __n1
;
244 __iov
[1].iov_base
= const_cast<char*>(__s2
);
245 __iov
[1].iov_len
= __n2
;
248 __ret
= writev(this->fd(), __iov
, 2);
249 while (__ret
== -1L && errno
== EINTR
);
253 __ret
= write(this->fd(), __s1
, __n1
);
254 while (__ret
== -1L && errno
== EINTR
);
259 __ret
= write(this->fd(), __s2
, __n2
);
260 while (__ret
== -1L && errno
== EINTR
);
270 __basic_file
<char>::seekoff(streamoff __off
, ios_base::seekdir __way
)
272 #ifdef _GLIBCXX_USE_LFS
273 return lseek64(this->fd(), __off
, __way
);
275 if (__off
> std::numeric_limits
<off_t
>::max()
276 || __off
< std::numeric_limits
<off_t
>::min())
278 return lseek(this->fd(), __off
, __way
);
283 __basic_file
<char>::sync()
284 { return fflush(_M_cfile
); }
287 __basic_file
<char>::showmanyc()
290 // Pipes and sockets.
292 int __r
= ioctl(this->fd(), FIONREAD
, &__num
);
293 if (!__r
&& __num
>= 0)
297 #ifdef _GLIBCXX_HAVE_POLL
299 struct pollfd __pfd
[1];
300 __pfd
[0].fd
= this->fd();
301 __pfd
[0].events
= POLLIN
;
302 if (poll(__pfd
, 1, 0) <= 0)
306 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
308 struct stat __buffer
;
309 int __ret
= fstat(this->fd(), &__buffer
);
310 if (!__ret
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
311 return __buffer
.st_size
- lseek(this->fd(), 0, ios_base::cur
);