2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
blob3f4914b29cd091f4463ac4298137b336ab92589d
1 // Wrapper of C-language FILE struct -*- C++ -*-
3 // Copyright (C) 2000, 2001, 2002, 2003, 2004 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.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
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>
35 #include <fcntl.h>
36 #include <errno.h>
38 #ifdef _GLIBCXX_HAVE_POLL
39 #include <poll.h>
40 #endif
42 // Pick up ioctl on Solaris 2.8
43 #ifdef _GLIBCXX_HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
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
53 // Pick up FIONREAD on Solaris 2.5.
54 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
55 #include <sys/filio.h>
56 #endif
58 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
59 #include <sys/uio.h>
60 #endif
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
71 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
73 namespace __gnu_internal
75 // Map ios_base::openmode flags to a string for use in fopen().
76 // Table of valid combinations as given in [lib.filebuf.members]/2.
77 static const char*
78 fopen_mode(std::ios_base::openmode mode)
80 enum
82 in = std::ios_base::in,
83 out = std::ios_base::out,
84 trunc = std::ios_base::trunc,
85 app = std::ios_base::app,
86 binary = std::ios_base::binary
89 switch (mode & (in|out|trunc|app|binary))
91 case ( out ): return "w";
92 case ( out |app ): return "a";
93 case ( out|trunc ): return "w";
94 case (in ): return "r";
95 case (in|out ): return "r+";
96 case (in|out|trunc ): return "w+";
98 case ( out |binary): return "wb";
99 case ( out |app|binary): return "ab";
100 case ( out|trunc |binary): return "wb";
101 case (in |binary): return "rb";
102 case (in|out |binary): return "r+b";
103 case (in|out|trunc |binary): return "w+b";
105 default: return 0; // invalid
109 // Wrapper handling partial write.
110 static std::streamsize
111 xwrite(int __fd, const char* __s, std::streamsize __n)
113 std::streamsize __nleft = __n;
115 for (;;)
117 const std::streamsize __ret = write(__fd, __s, __nleft);
118 if (__ret == -1L && errno == EINTR)
119 continue;
120 if (__ret == -1L)
121 break;
123 __nleft -= __ret;
124 if (__nleft == 0)
125 break;
127 __s += __ret;
130 return __n - __nleft;
133 #ifdef _GLIBCXX_HAVE_WRITEV
134 // Wrapper handling partial writev.
135 static std::streamsize
136 xwritev(int __fd, const char* __s1, std::streamsize __n1,
137 const char* __s2, std::streamsize __n2)
139 std::streamsize __nleft = __n1 + __n2;
140 std::streamsize __n1_left = __n1;
142 struct iovec __iov[2];
143 __iov[1].iov_base = const_cast<char*>(__s2);
144 __iov[1].iov_len = __n2;
146 for (;;)
148 __iov[0].iov_base = const_cast<char*>(__s1);
149 __iov[0].iov_len = __n1_left;
151 const std::streamsize __ret = writev(__fd, __iov, 2);
152 if (__ret == -1L && errno == EINTR)
153 continue;
154 if (__ret == -1L)
155 break;
157 __nleft -= __ret;
158 if (__nleft == 0)
159 break;
161 const std::streamsize __off = __ret - __n1_left;
162 if (__off >= 0)
164 __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
165 break;
168 __s1 += __ret;
169 __n1_left -= __ret;
172 return __n1 + __n2 - __nleft;
174 #endif
175 } // namespace __gnu_internal
178 _GLIBCXX_BEGIN_NAMESPACE(std)
180 // Definitions for __basic_file<char>.
181 __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
182 : _M_cfile(NULL), _M_cfile_created(false) { }
184 __basic_file<char>::~__basic_file()
185 { this->close(); }
187 __basic_file<char>*
188 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
190 __basic_file* __ret = NULL;
191 if (!this->is_open() && __file)
193 int __err;
194 errno = 0;
196 __err = this->sync();
197 while (__err && errno == EINTR);
198 if (!__err)
200 _M_cfile = __file;
201 _M_cfile_created = false;
202 __ret = this;
205 return __ret;
208 __basic_file<char>*
209 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode)
211 __basic_file* __ret = NULL;
212 const char* __c_mode = __gnu_internal::fopen_mode(__mode);
213 if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
215 char* __buf = NULL;
216 _M_cfile_created = true;
217 if (__fd == 0)
218 setvbuf(_M_cfile, __buf, _IONBF, 0);
219 __ret = this;
221 return __ret;
224 __basic_file<char>*
225 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
226 int /*__prot*/)
228 __basic_file* __ret = NULL;
229 const char* __c_mode = __gnu_internal::fopen_mode(__mode);
230 if (__c_mode && !this->is_open())
232 #ifdef _GLIBCXX_USE_LFS
233 if ((_M_cfile = fopen64(__name, __c_mode)))
234 #else
235 if ((_M_cfile = fopen(__name, __c_mode)))
236 #endif
238 _M_cfile_created = true;
239 __ret = this;
242 return __ret;
245 bool
246 __basic_file<char>::is_open() const
247 { return _M_cfile != 0; }
249 int
250 __basic_file<char>::fd()
251 { return fileno(_M_cfile); }
253 __c_file*
254 __basic_file<char>::file()
255 { return _M_cfile; }
257 __basic_file<char>*
258 __basic_file<char>::close()
260 __basic_file* __ret = static_cast<__basic_file*>(NULL);
261 if (this->is_open())
263 int __err = 0;
264 if (_M_cfile_created)
266 // In general, no need to zero errno in advance if checking
267 // for error first. However, C89/C99 (at variance with IEEE
268 // 1003.1, f.i.) do not mandate that fclose must set errno
269 // upon error.
270 errno = 0;
272 __err = fclose(_M_cfile);
273 while (__err && errno == EINTR);
275 _M_cfile = 0;
276 if (!__err)
277 __ret = this;
279 return __ret;
282 streamsize
283 __basic_file<char>::xsgetn(char* __s, streamsize __n)
285 streamsize __ret;
287 __ret = read(this->fd(), __s, __n);
288 while (__ret == -1L && errno == EINTR);
289 return __ret;
292 streamsize
293 __basic_file<char>::xsputn(const char* __s, streamsize __n)
294 { return __gnu_internal::xwrite(this->fd(), __s, __n); }
296 streamsize
297 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
298 const char* __s2, streamsize __n2)
300 streamsize __ret = 0;
301 #ifdef _GLIBCXX_HAVE_WRITEV
302 __ret = __gnu_internal::xwritev(this->fd(), __s1, __n1, __s2, __n2);
303 #else
304 if (__n1)
305 __ret = __gnu_internal::xwrite(this->fd(), __s1, __n1);
307 if (__ret == __n1)
308 __ret += __gnu_internal::xwrite(this->fd(), __s2, __n2);
309 #endif
310 return __ret;
313 streamoff
314 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way)
316 #ifdef _GLIBCXX_USE_LFS
317 return lseek64(this->fd(), __off, __way);
318 #else
319 if (__off > numeric_limits<off_t>::max()
320 || __off < numeric_limits<off_t>::min())
321 return -1L;
322 return lseek(this->fd(), __off, __way);
323 #endif
326 int
327 __basic_file<char>::sync()
328 { return fflush(_M_cfile); }
330 streamsize
331 __basic_file<char>::showmanyc()
333 #ifdef FIONREAD
334 // Pipes and sockets.
335 #ifdef _GLIBCXX_FIONREAD_TAKES_OFF_T
336 off_t __num = 0;
337 #else
338 int __num = 0;
339 #endif
340 int __r = ioctl(this->fd(), FIONREAD, &__num);
341 if (!__r && __num >= 0)
342 return __num;
343 #endif
345 #ifdef _GLIBCXX_HAVE_POLL
346 // Cheap test.
347 struct pollfd __pfd[1];
348 __pfd[0].fd = this->fd();
349 __pfd[0].events = POLLIN;
350 if (poll(__pfd, 1, 0) <= 0)
351 return 0;
352 #endif
354 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
355 // Regular files.
356 #ifdef _GLIBCXX_USE_LFS
357 struct stat64 __buffer;
358 const int __err = fstat64(this->fd(), &__buffer);
359 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
361 const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
362 ios_base::cur);
363 return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
365 #else
366 struct stat __buffer;
367 const int __err = fstat(this->fd(), &__buffer);
368 if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
369 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
370 #endif
371 #endif
372 return 0;
375 _GLIBCXX_END_NAMESPACE