]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/io/basic_file_stdio.cc
re PR libstdc++/8610 (large file support in libstdc++-v3 (std::streamoff type is...
[thirdparty/gcc.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
1 // Wrapper of C-language FILE struct -*- C++ -*-
2
3 // Copyright (C) 2000, 2001, 2002, 2003 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 2, 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 // 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,
19 // USA.
20
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.
29
30 //
31 // ISO C++ 14882: 27.8 File-based streams
32 //
33
34 #include <bits/basic_file.h>
35 #include <fcntl.h>
36 #include <errno.h>
37
38 #ifdef _GLIBCXX_HAVE_POLL
39 #include <poll.h>
40 #endif
41
42 // Pick up ioctl on Solaris 2.8
43 #ifdef _GLIBCXX_HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 // Pick up FIONREAD on Solaris 2
48 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
49 #define BSD_COMP
50 #include <sys/ioctl.h>
51 #endif
52
53 // Pick up FIONREAD on Solaris 2.5.
54 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
55 #include <sys/filio.h>
56 #endif
57
58 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
59 #include <sys/uio.h>
60 #endif
61
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)
66 # else
67 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
68 # endif
69 #endif
70
71 #include <limits> // For <off_t>::max() and min()
72
73 namespace std
74 {
75 // Definitions for __basic_file<char>.
76 __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
77 : _M_cfile(NULL), _M_cfile_created(false) { }
78
79 __basic_file<char>::~__basic_file()
80 { this->close(); }
81
82 void
83 __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int& __p_mode,
84 int&, char* __c_mode)
85 {
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;
91
92 // Set __c_mode for use in fopen.
93 // Set __p_mode for use in open.
94 if (!__testi && __testo && !__testt && !__testa)
95 {
96 strcpy(__c_mode, "w");
97 __p_mode = O_WRONLY | O_CREAT;
98 }
99 if (!__testi && __testo && !__testt && __testa)
100 {
101 strcpy(__c_mode, "a");
102 __p_mode = O_WRONLY | O_CREAT | O_APPEND;
103 }
104 if (!__testi && __testo && __testt && !__testa)
105 {
106 strcpy(__c_mode, "w");
107 __p_mode = O_WRONLY | O_CREAT | O_TRUNC;
108 }
109
110 if (__testi && !__testo && !__testt && !__testa)
111 {
112 strcpy(__c_mode, "r");
113 __p_mode = O_RDONLY;
114 }
115 if (__testi && __testo && !__testt && !__testa)
116 {
117 strcpy(__c_mode, "r+");
118 __p_mode = O_RDWR | O_CREAT;
119 }
120 if (__testi && __testo && __testt && !__testa)
121 {
122 strcpy(__c_mode, "w+");
123 __p_mode = O_RDWR | O_CREAT | O_TRUNC;
124 }
125 if (__testb)
126 strcat(__c_mode, "b");
127 }
128
129 __basic_file<char>*
130 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
131 {
132 __basic_file* __ret = NULL;
133 if (!this->is_open() && __file)
134 {
135 _M_cfile = __file;
136 _M_cfile_created = false;
137 __ret = this;
138 }
139 return __ret;
140 }
141
142 __basic_file<char>*
143 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode,
144 bool __del)
145 {
146 __basic_file* __ret = NULL;
147 int __p_mode = 0;
148 int __rw_mode = 0;
149 char __c_mode[4];
150
151 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
152 if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
153 {
154 // Iff __del is true, then close will fclose the fd.
155 _M_cfile_created = __del;
156
157 if (__fd == 0)
158 setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
159
160 __ret = this;
161 }
162 return __ret;
163 }
164
165 __basic_file<char>*
166 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
167 int /*__prot*/)
168 {
169 __basic_file* __ret = NULL;
170 int __p_mode = 0;
171 int __rw_mode = 0;
172 char __c_mode[4];
173
174 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
175
176 if (!this->is_open())
177 {
178 #ifdef _GLIBCXX_USE_LFS
179 if ((_M_cfile = fopen64(__name, __c_mode)))
180 #else
181 if ((_M_cfile = fopen(__name, __c_mode)))
182 #endif
183 {
184 _M_cfile_created = true;
185 __ret = this;
186 }
187 }
188 return __ret;
189 }
190
191 bool
192 __basic_file<char>::is_open() const
193 { return _M_cfile != 0; }
194
195 int
196 __basic_file<char>::fd()
197 { return fileno(_M_cfile) ; }
198
199 __basic_file<char>*
200 __basic_file<char>::close()
201 {
202 __basic_file* __retval = static_cast<__basic_file*>(NULL);
203 if (this->is_open())
204 {
205 if (_M_cfile_created)
206 fclose(_M_cfile);
207 else
208 fflush(_M_cfile);
209 _M_cfile = 0;
210 __retval = this;
211 }
212 return __retval;
213 }
214
215 streamsize
216 __basic_file<char>::xsgetn(char* __s, streamsize __n)
217 {
218 streamsize __ret;
219 do
220 __ret = read(this->fd(), __s, __n);
221 while (__ret == -1L && errno == EINTR);
222 return __ret;
223 }
224
225 streamsize
226 __basic_file<char>::xsputn(const char* __s, streamsize __n)
227 {
228 streamsize __ret;
229 do
230 __ret = write(this->fd(), __s, __n);
231 while (__ret == -1L && errno == EINTR);
232 return __ret;
233 }
234
235 streamsize
236 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
237 const char* __s2, streamsize __n2)
238 {
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;
246
247 do
248 __ret = writev(this->fd(), __iov, 2);
249 while (__ret == -1L && errno == EINTR);
250 #else
251 if (__n1)
252 do
253 __ret = write(this->fd(), __s1, __n1);
254 while (__ret == -1L && errno == EINTR);
255
256 if (__ret == __n1)
257 {
258 do
259 __ret = write(this->fd(), __s2, __n2);
260 while (__ret == -1L && errno == EINTR);
261
262 if (__ret != -1L)
263 __ret += __n1;
264 }
265 #endif
266 return __ret;
267 }
268
269 streamoff
270 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way)
271 {
272 #ifdef _GLIBCXX_USE_LFS
273 return lseek64(this->fd(), __off, __way);
274 #else
275 if (__off > std::numeric_limits<off_t>::max()
276 || __off < std::numeric_limits<off_t>::min())
277 return -1L;
278 return lseek(this->fd(), __off, __way);
279 #endif
280 }
281
282 int
283 __basic_file<char>::sync()
284 { return fflush(_M_cfile); }
285
286 streamsize
287 __basic_file<char>::showmanyc()
288 {
289 #ifdef FIONREAD
290 // Pipes and sockets.
291 int __num = 0;
292 int __r = ioctl(this->fd(), FIONREAD, &__num);
293 if (!__r && __num >= 0)
294 return __num;
295 #endif
296
297 #ifdef _GLIBCXX_HAVE_POLL
298 // Cheap test.
299 struct pollfd __pfd[1];
300 __pfd[0].fd = this->fd();
301 __pfd[0].events = POLLIN;
302 if (poll(__pfd, 1, 0) <= 0)
303 return 0;
304 #endif
305
306 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
307 // Regular files.
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);
312 #endif
313 return 0;
314 }
315
316 } // namespace std