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>
39 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
40 #define BSD_COMP /* Get FIONREAD on Solaris2. */
41 #include <sys/ioctl.h>
44 // Pick up FIONREAD on Solaris 2.5.
45 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
46 #include <sys/filio.h>
49 #ifdef _GLIBCXX_HAVE_POLL
53 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
54 # include <sys/stat.h>
55 # ifdef _GLIBCXX_HAVE_S_ISREG
56 # define _GLIBCXX_ISREG(x) S_ISREG(x)
58 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
64 // Definitions for __basic_file<char>.
65 __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
66 : _M_cfile(NULL), _M_cfile_created(false) { }
68 __basic_file<char>::~__basic_file()
72 __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int& __p_mode,
75 bool __testb = __mode & ios_base::binary;
76 bool __testi = __mode & ios_base::in;
77 bool __testo = __mode & ios_base::out;
78 bool __testt = __mode & ios_base::trunc;
79 bool __testa = __mode & ios_base::app;
81 // Set __c_mode for use in fopen.
82 // Set __p_mode for use in open.
83 if (!__testi && __testo && !__testt && !__testa)
85 strcpy(__c_mode, "w");
86 __p_mode = (O_WRONLY | O_CREAT);
88 if (!__testi && __testo && !__testt && __testa)
90 strcpy(__c_mode, "a");
91 __p_mode |= O_WRONLY | O_CREAT | O_APPEND;
93 if (!__testi && __testo && __testt && !__testa)
95 strcpy(__c_mode, "w");
96 __p_mode |= O_WRONLY | O_CREAT | O_TRUNC;
99 if (__testi && !__testo && !__testt && !__testa)
101 strcpy(__c_mode, "r");
102 __p_mode |= O_RDONLY;
104 if (__testi && __testo && !__testt && !__testa)
106 strcpy(__c_mode, "r+");
107 __p_mode |= O_RDWR | O_CREAT;
109 if (__testi && __testo && __testt && !__testa)
111 strcpy(__c_mode, "w+");
112 __p_mode |= O_RDWR | O_CREAT | O_TRUNC;
115 strcat(__c_mode, "b");
119 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
121 __basic_file* __ret = NULL;
122 if (!this->is_open() && __file)
125 _M_cfile_created = false;
132 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode,
135 __basic_file* __ret = NULL;
140 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
141 if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
143 // Iff __del is true, then close will fclose the fd.
144 _M_cfile_created = __del;
147 setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
155 __basic_file<char>::sys_getc()
156 { return getc(_M_cfile); }
159 __basic_file<char>::sys_ungetc(int __c)
160 { return ungetc(__c, _M_cfile); }
163 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
166 __basic_file* __ret = NULL;
171 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
173 if (!this->is_open())
175 if ((_M_cfile = fopen(__name, __c_mode)))
177 _M_cfile_created = true;
185 __basic_file<char>::is_open() const
186 { return _M_cfile != 0; }
189 __basic_file<char>::fd()
190 { return fileno(_M_cfile) ; }
193 __basic_file<char>::close()
195 __basic_file* __retval = static_cast<__basic_file*>(NULL);
198 if (_M_cfile_created)
209 __basic_file<char>::xsgetn(char* __s, streamsize __n)
213 __ret = read(this->fd(), __s, __n);
214 while (__ret == -1L && errno == EINTR);
219 __basic_file<char>::xsputn(const char* __s, streamsize __n)
223 __ret = write(this->fd(), __s, __n);
224 while (__ret == -1L && errno == EINTR);
229 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
230 ios_base::openmode /*__mode*/)
231 { return lseek(this->fd(), __off, __way); }
234 __basic_file<char>::seekpos(streampos __pos, ios_base::openmode /*__mode*/)
235 { return lseek(this->fd(), __pos, ios_base::beg); }
238 __basic_file<char>::sync()
239 { return fflush(_M_cfile); }
242 __basic_file<char>::showmanyc()
245 // Pipes and sockets.
247 int __r = ioctl(this->fd(), FIONREAD, &__num);
248 if (!__r && __num >= 0)
252 #ifdef _GLIBCXX_HAVE_POLL
254 struct pollfd __pfd[1];
255 __pfd[0].fd = this->fd();
256 __pfd[0].events = POLLIN;
257 if (poll(__pfd, 1, 0) <= 0)
261 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
263 struct stat __buffer;
264 int __ret = fstat(this->fd(), &__buffer);
265 if (!__ret && _GLIBCXX_ISREG(__buffer.st_mode))
266 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);