PR other/29176
[official-gcc.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
blob2bc17baead9c3adc85aff4724ab74f95211ad16f
1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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>
36 #include <fcntl.h>
37 #include <errno.h>
39 #ifdef _GLIBCXX_HAVE_POLL
40 #include <poll.h>
41 #endif
43 // Pick up ioctl on Solaris 2.8
44 #ifdef _GLIBCXX_HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
48 // Pick up FIONREAD on Solaris 2
49 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
50 #define BSD_COMP
51 #include <sys/ioctl.h>
52 #endif
54 // Pick up FIONREAD on Solaris 2.5.
55 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
56 #include <sys/filio.h>
57 #endif
59 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
60 #include <sys/uio.h>
61 #endif
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)
67 # else
68 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
69 # endif
70 #endif
72 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
74 namespace
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.
78 static const char*
79 fopen_mode(std::ios_base::openmode mode)
81 enum
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 switch (mode & (in|out|trunc|app|binary))
92 case ( out ): return "w";
93 case ( out |app ): return "a";
94 case ( out|trunc ): return "w";
95 case (in ): return "r";
96 case (in|out ): return "r+";
97 case (in|out|trunc ): return "w+";
99 case ( out |binary): return "wb";
100 case ( out |app|binary): return "ab";
101 case ( out|trunc |binary): return "wb";
102 case (in |binary): return "rb";
103 case (in|out |binary): return "r+b";
104 case (in|out|trunc |binary): return "w+b";
106 default: return 0; // invalid
110 // Wrapper handling partial write.
111 static std::streamsize
112 xwrite(int __fd, const char* __s, std::streamsize __n)
114 std::streamsize __nleft = __n;
116 for (;;)
118 const std::streamsize __ret = write(__fd, __s, __nleft);
119 if (__ret == -1L && errno == EINTR)
120 continue;
121 if (__ret == -1L)
122 break;
124 __nleft -= __ret;
125 if (__nleft == 0)
126 break;
128 __s += __ret;
131 return __n - __nleft;
134 #ifdef _GLIBCXX_HAVE_WRITEV
135 // Wrapper handling partial writev.
136 static std::streamsize
137 xwritev(int __fd, const char* __s1, std::streamsize __n1,
138 const char* __s2, std::streamsize __n2)
140 std::streamsize __nleft = __n1 + __n2;
141 std::streamsize __n1_left = __n1;
143 struct iovec __iov[2];
144 __iov[1].iov_base = const_cast<char*>(__s2);
145 __iov[1].iov_len = __n2;
147 for (;;)
149 __iov[0].iov_base = const_cast<char*>(__s1);
150 __iov[0].iov_len = __n1_left;
152 const std::streamsize __ret = writev(__fd, __iov, 2);
153 if (__ret == -1L && errno == EINTR)
154 continue;
155 if (__ret == -1L)
156 break;
158 __nleft -= __ret;
159 if (__nleft == 0)
160 break;
162 const std::streamsize __off = __ret - __n1_left;
163 if (__off >= 0)
165 __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
166 break;
169 __s1 += __ret;
170 __n1_left -= __ret;
173 return __n1 + __n2 - __nleft;
175 #endif
176 } // anonymous namespace
179 _GLIBCXX_BEGIN_NAMESPACE(std)
181 // Definitions for __basic_file<char>.
182 __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
183 : _M_cfile(NULL), _M_cfile_created(false) { }
185 __basic_file<char>::~__basic_file()
186 { this->close(); }
188 __basic_file<char>*
189 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
191 __basic_file* __ret = NULL;
192 if (!this->is_open() && __file)
194 int __err;
195 errno = 0;
197 __err = this->sync();
198 while (__err && errno == EINTR);
199 if (!__err)
201 _M_cfile = __file;
202 _M_cfile_created = false;
203 __ret = this;
206 return __ret;
209 __basic_file<char>*
210 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode)
212 __basic_file* __ret = NULL;
213 const char* __c_mode = fopen_mode(__mode);
214 if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
216 char* __buf = NULL;
217 _M_cfile_created = true;
218 if (__fd == 0)
219 setvbuf(_M_cfile, __buf, _IONBF, 0);
220 __ret = this;
222 return __ret;
225 __basic_file<char>*
226 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
227 int /*__prot*/)
229 __basic_file* __ret = NULL;
230 const char* __c_mode = fopen_mode(__mode);
231 if (__c_mode && !this->is_open())
233 #ifdef _GLIBCXX_USE_LFS
234 if ((_M_cfile = fopen64(__name, __c_mode)))
235 #else
236 if ((_M_cfile = fopen(__name, __c_mode)))
237 #endif
239 _M_cfile_created = true;
240 __ret = this;
243 return __ret;
246 bool
247 __basic_file<char>::is_open() const
248 { return _M_cfile != 0; }
250 int
251 __basic_file<char>::fd()
252 { return fileno(_M_cfile); }
254 __c_file*
255 __basic_file<char>::file()
256 { return _M_cfile; }
258 __basic_file<char>*
259 __basic_file<char>::close()
261 __basic_file* __ret = static_cast<__basic_file*>(NULL);
262 if (this->is_open())
264 int __err = 0;
265 if (_M_cfile_created)
267 // In general, no need to zero errno in advance if checking
268 // for error first. However, C89/C99 (at variance with IEEE
269 // 1003.1, f.i.) do not mandate that fclose must set errno
270 // upon error.
271 errno = 0;
273 __err = fclose(_M_cfile);
274 while (__err && errno == EINTR);
276 _M_cfile = 0;
277 if (!__err)
278 __ret = this;
280 return __ret;
283 streamsize
284 __basic_file<char>::xsgetn(char* __s, streamsize __n)
286 streamsize __ret;
288 __ret = read(this->fd(), __s, __n);
289 while (__ret == -1L && errno == EINTR);
290 return __ret;
293 streamsize
294 __basic_file<char>::xsputn(const char* __s, streamsize __n)
295 { return xwrite(this->fd(), __s, __n); }
297 streamsize
298 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
299 const char* __s2, streamsize __n2)
301 streamsize __ret = 0;
302 #ifdef _GLIBCXX_HAVE_WRITEV
303 __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2);
304 #else
305 if (__n1)
306 __ret = xwrite(this->fd(), __s1, __n1);
308 if (__ret == __n1)
309 __ret += xwrite(this->fd(), __s2, __n2);
310 #endif
311 return __ret;
314 streamoff
315 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way)
317 #ifdef _GLIBCXX_USE_LFS
318 return lseek64(this->fd(), __off, __way);
319 #else
320 if (__off > numeric_limits<off_t>::max()
321 || __off < numeric_limits<off_t>::min())
322 return -1L;
323 return lseek(this->fd(), __off, __way);
324 #endif
327 int
328 __basic_file<char>::sync()
329 { return fflush(_M_cfile); }
331 streamsize
332 __basic_file<char>::showmanyc()
334 #ifdef FIONREAD
335 // Pipes and sockets.
336 #ifdef _GLIBCXX_FIONREAD_TAKES_OFF_T
337 off_t __num = 0;
338 #else
339 int __num = 0;
340 #endif
341 int __r = ioctl(this->fd(), FIONREAD, &__num);
342 if (!__r && __num >= 0)
343 return __num;
344 #endif
346 #ifdef _GLIBCXX_HAVE_POLL
347 // Cheap test.
348 struct pollfd __pfd[1];
349 __pfd[0].fd = this->fd();
350 __pfd[0].events = POLLIN;
351 if (poll(__pfd, 1, 0) <= 0)
352 return 0;
353 #endif
355 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
356 // Regular files.
357 #ifdef _GLIBCXX_USE_LFS
358 struct stat64 __buffer;
359 const int __err = fstat64(this->fd(), &__buffer);
360 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
362 const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
363 ios_base::cur);
364 return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
366 #else
367 struct stat __buffer;
368 const int __err = fstat(this->fd(), &__buffer);
369 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
370 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
371 #endif
372 #endif
373 return 0;
376 _GLIBCXX_END_NAMESPACE