Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
blobd2de028f099ad67d24887ec1fa75da02f17a9f3c
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.
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 3, 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 // 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>
31 #include <fcntl.h>
32 #include <errno.h>
34 #ifdef _GLIBCXX_HAVE_POLL
35 #include <poll.h>
36 #endif
38 // Pick up ioctl on Solaris 2.8
39 #ifdef _GLIBCXX_HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
43 // Pick up FIONREAD on Solaris 2
44 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
45 #define BSD_COMP
46 #include <sys/ioctl.h>
47 #endif
49 // Pick up FIONREAD on Solaris 2.5.
50 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
51 #include <sys/filio.h>
52 #endif
54 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
55 #include <sys/uio.h>
56 #endif
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)
62 # else
63 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
64 # endif
65 #endif
67 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
69 namespace
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.
73 static const char*
74 fopen_mode(std::ios_base::openmode mode)
76 enum
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;
119 for (;;)
121 const std::streamsize __ret = write(__fd, __s, __nleft);
122 if (__ret == -1L && errno == EINTR)
123 continue;
124 if (__ret == -1L)
125 break;
127 __nleft -= __ret;
128 if (__nleft == 0)
129 break;
131 __s += __ret;
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;
150 for (;;)
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)
157 continue;
158 if (__ret == -1L)
159 break;
161 __nleft -= __ret;
162 if (__nleft == 0)
163 break;
165 const std::streamsize __off = __ret - __n1_left;
166 if (__off >= 0)
168 __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
169 break;
172 __s1 += __ret;
173 __n1_left -= __ret;
176 return __n1 + __n2 - __nleft;
178 #endif
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()
189 { this->close(); }
191 __basic_file<char>*
192 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
194 __basic_file* __ret = NULL;
195 if (!this->is_open() && __file)
197 int __err;
198 errno = 0;
200 __err = this->sync();
201 while (__err && errno == EINTR);
202 if (!__err)
204 _M_cfile = __file;
205 _M_cfile_created = false;
206 __ret = this;
209 return __ret;
212 __basic_file<char>*
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)))
219 char* __buf = NULL;
220 _M_cfile_created = true;
221 if (__fd == 0)
222 setvbuf(_M_cfile, __buf, _IONBF, 0);
223 __ret = this;
225 return __ret;
228 __basic_file<char>*
229 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
230 int /*__prot*/)
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)))
238 #else
239 if ((_M_cfile = fopen(__name, __c_mode)))
240 #endif
242 _M_cfile_created = true;
243 __ret = this;
246 return __ret;
249 bool
250 __basic_file<char>::is_open() const throw ()
251 { return _M_cfile != 0; }
253 int
254 __basic_file<char>::fd() throw ()
255 { return fileno(_M_cfile); }
257 __c_file*
258 __basic_file<char>::file() throw ()
259 { return _M_cfile; }
261 __basic_file<char>*
262 __basic_file<char>::close()
264 __basic_file* __ret = static_cast<__basic_file*>(NULL);
265 if (this->is_open())
267 int __err = 0;
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
273 // upon error.
274 errno = 0;
276 __err = fclose(_M_cfile);
277 while (__err && errno == EINTR);
279 _M_cfile = 0;
280 if (!__err)
281 __ret = this;
283 return __ret;
286 streamsize
287 __basic_file<char>::xsgetn(char* __s, streamsize __n)
289 streamsize __ret;
291 __ret = read(this->fd(), __s, __n);
292 while (__ret == -1L && errno == EINTR);
293 return __ret;
296 streamsize
297 __basic_file<char>::xsputn(const char* __s, streamsize __n)
298 { return xwrite(this->fd(), __s, __n); }
300 streamsize
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);
307 #else
308 if (__n1)
309 __ret = xwrite(this->fd(), __s1, __n1);
311 if (__ret == __n1)
312 __ret += xwrite(this->fd(), __s2, __n2);
313 #endif
314 return __ret;
317 streamoff
318 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw ()
320 #ifdef _GLIBCXX_USE_LFS
321 return lseek64(this->fd(), __off, __way);
322 #else
323 if (__off > numeric_limits<off_t>::max()
324 || __off < numeric_limits<off_t>::min())
325 return -1L;
326 return lseek(this->fd(), __off, __way);
327 #endif
330 int
331 __basic_file<char>::sync()
332 { return fflush(_M_cfile); }
334 streamsize
335 __basic_file<char>::showmanyc()
337 #ifndef _GLIBCXX_NO_IOCTL
338 #ifdef FIONREAD
339 // Pipes and sockets.
340 #ifdef _GLIBCXX_FIONREAD_TAKES_OFF_T
341 off_t __num = 0;
342 #else
343 int __num = 0;
344 #endif
345 int __r = ioctl(this->fd(), FIONREAD, &__num);
346 if (!__r && __num >= 0)
347 return __num;
348 #endif
349 #endif
351 #ifdef _GLIBCXX_HAVE_POLL
352 // Cheap test.
353 struct pollfd __pfd[1];
354 __pfd[0].fd = this->fd();
355 __pfd[0].events = POLLIN;
356 if (poll(__pfd, 1, 0) <= 0)
357 return 0;
358 #endif
360 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
361 // Regular files.
362 #ifdef _GLIBCXX_USE_LFS
363 struct stat64 __buffer;
364 const int __err = fstat64(this->fd(), &__buffer);
365 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
367 const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
368 ios_base::cur);
369 return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
371 #else
372 struct stat __buffer;
373 const int __err = fstat(this->fd(), &__buffer);
374 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
375 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
376 #endif
377 #endif
378 return 0;
381 _GLIBCXX_END_NAMESPACE