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)
73 // Definitions for __basic_file<char>.
74 __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
75 : _M_cfile(NULL), _M_cfile_created(false) { }
77 __basic_file<char>::~__basic_file()
81 __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int& __p_mode,
84 bool __testb = __mode & ios_base::binary;
85 bool __testi = __mode & ios_base::in;
86 bool __testo = __mode & ios_base::out;
87 bool __testt = __mode & ios_base::trunc;
88 bool __testa = __mode & ios_base::app;
90 // Set __c_mode for use in fopen.
91 // Set __p_mode for use in open.
92 if (!__testi && __testo && !__testt && !__testa)
94 strcpy(__c_mode, "w");
95 __p_mode = O_WRONLY | O_CREAT;
97 if (!__testi && __testo && !__testt && __testa)
99 strcpy(__c_mode, "a");
100 __p_mode = O_WRONLY | O_CREAT | O_APPEND;
102 if (!__testi && __testo && __testt && !__testa)
104 strcpy(__c_mode, "w");
105 __p_mode = O_WRONLY | O_CREAT | O_TRUNC;
108 if (__testi && !__testo && !__testt && !__testa)
110 strcpy(__c_mode, "r");
113 if (__testi && __testo && !__testt && !__testa)
115 strcpy(__c_mode, "r+");
116 __p_mode = O_RDWR | O_CREAT;
118 if (__testi && __testo && __testt && !__testa)
120 strcpy(__c_mode, "w+");
121 __p_mode = O_RDWR | O_CREAT | O_TRUNC;
124 strcat(__c_mode, "b");
128 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
130 __basic_file* __ret = NULL;
131 if (!this->is_open() && __file)
134 _M_cfile_created = false;
141 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode,
144 __basic_file* __ret = NULL;
149 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
150 if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
152 // Iff __del is true, then close will fclose the fd.
153 _M_cfile_created = __del;
156 setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
164 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
167 __basic_file* __ret = NULL;
172 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
174 if (!this->is_open())
176 if ((_M_cfile = fopen(__name, __c_mode)))
178 _M_cfile_created = true;
186 __basic_file<char>::is_open() const
187 { return _M_cfile != 0; }
190 __basic_file<char>::fd()
191 { return fileno(_M_cfile) ; }
194 __basic_file<char>::close()
196 __basic_file* __retval = static_cast<__basic_file*>(NULL);
199 if (_M_cfile_created)
210 __basic_file<char>::xsgetn(char* __s, streamsize __n)
214 __ret = read(this->fd(), __s, __n);
215 while (__ret == -1L && errno == EINTR);
220 __basic_file<char>::xsputn(const char* __s, streamsize __n)
224 __ret = write(this->fd(), __s, __n);
225 while (__ret == -1L && errno == EINTR);
230 __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
231 const char* __s2, streamsize __n2)
233 streamsize __ret = 0;
234 #ifdef _GLIBCXX_HAVE_WRITEV
235 struct iovec __iov[2];
236 __iov[0].iov_base = const_cast<char*>(__s1);
237 __iov[0].iov_len = __n1;
238 __iov[1].iov_base = const_cast<char*>(__s2);
239 __iov[1].iov_len = __n2;
242 __ret = writev(this->fd(), __iov, 2);
243 while (__ret == -1L && errno == EINTR);
247 __ret = write(this->fd(), __s1, __n1);
248 while (__ret == -1L && errno == EINTR);
253 __ret = write(this->fd(), __s2, __n2);
254 while (__ret == -1L && errno == EINTR);
264 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way)
265 { return lseek(this->fd(), __off, __way); }
268 __basic_file<char>::seekpos(streampos __pos)
269 { return lseek(this->fd(), __pos, ios_base::beg); }
272 __basic_file<char>::sync()
273 { return fflush(_M_cfile); }
276 __basic_file<char>::showmanyc()
279 // Pipes and sockets.
281 int __r = ioctl(this->fd(), FIONREAD, &__num);
282 if (!__r && __num >= 0)
286 #ifdef _GLIBCXX_HAVE_POLL
288 struct pollfd __pfd[1];
289 __pfd[0].fd = this->fd();
290 __pfd[0].events = POLLIN;
291 if (poll(__pfd, 1, 0) <= 0)
295 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
297 struct stat __buffer;
298 int __ret = fstat(this->fd(), &__buffer);
299 if (!__ret && _GLIBCXX_ISREG(__buffer.st_mode))
300 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);