]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/io/basic_file_stdio.cc
[multiple changes]
[thirdparty/gcc.git] / libstdc++-v3 / config / io / basic_file_stdio.cc
1 // Wrapper of C-language FILE struct -*- C++ -*-
2
3 // Copyright (C) 2000, 2001, 2002, 2003 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.
10
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.
15
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,
19 // USA.
20
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.
29
30 //
31 // ISO C++ 14882: 27.8 File-based streams
32 //
33
34 #include <bits/basic_file.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <errno.h>
38
39 #ifdef _GLIBCPP_HAVE_SYS_IOCTL_H
40 #define BSD_COMP /* Get FIONREAD on Solaris2. */
41 #include <sys/ioctl.h>
42 #endif
43
44 // Pick up FIONREAD on Solaris 2.5.
45 #ifdef _GLIBCPP_HAVE_SYS_FILIO_H
46 #include <sys/filio.h>
47 #endif
48
49 #ifdef _GLIBCPP_HAVE_POLL
50 #include <poll.h>
51 #endif
52
53 #if defined(_GLIBCPP_HAVE_S_ISREG) || defined(_GLIBCPP_HAVE_S_IFREG)
54 # include <sys/stat.h>
55 # ifdef _GLIBCPP_HAVE_S_ISREG
56 # define _GLIBCPP_ISREG(x) S_ISREG(x)
57 # else
58 # define _GLIBCPP_ISREG(x) (((x) & S_IFMT) == S_IFREG)
59 # endif
60 #endif
61
62 namespace std
63 {
64 // Definitions for __basic_file<char>.
65 __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
66 : _M_cfile(NULL), _M_cfile_created(false) { }
67
68 __basic_file<char>::~__basic_file()
69 { this->close(); }
70
71 void
72 __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int& __p_mode,
73 int&, char* __c_mode)
74 {
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;
80
81 // Set __c_mode for use in fopen.
82 // Set __p_mode for use in open.
83 if (!__testi && __testo && !__testt && !__testa)
84 {
85 strcpy(__c_mode, "w");
86 __p_mode = (O_WRONLY | O_CREAT);
87 }
88 if (!__testi && __testo && !__testt && __testa)
89 {
90 strcpy(__c_mode, "a");
91 __p_mode |= O_WRONLY | O_CREAT | O_APPEND;
92 }
93 if (!__testi && __testo && __testt && !__testa)
94 {
95 strcpy(__c_mode, "w");
96 __p_mode |= O_WRONLY | O_CREAT | O_TRUNC;
97 }
98
99 if (__testi && !__testo && !__testt && !__testa)
100 {
101 strcpy(__c_mode, "r");
102 __p_mode |= O_RDONLY;
103 }
104 if (__testi && __testo && !__testt && !__testa)
105 {
106 strcpy(__c_mode, "r+");
107 __p_mode |= O_RDWR | O_CREAT;
108 }
109 if (__testi && __testo && __testt && !__testa)
110 {
111 strcpy(__c_mode, "w+");
112 __p_mode |= O_RDWR | O_CREAT | O_TRUNC;
113 }
114 if (__testb)
115 strcat(__c_mode, "b");
116 }
117
118 __basic_file<char>*
119 __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
120 {
121 __basic_file* __ret = NULL;
122 if (!this->is_open() && __file)
123 {
124 _M_cfile = __file;
125 _M_cfile_created = false;
126 __ret = this;
127 }
128 return __ret;
129 }
130
131 __basic_file<char>*
132 __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode,
133 bool __del)
134 {
135 __basic_file* __ret = NULL;
136 int __p_mode = 0;
137 int __rw_mode = 0;
138 char __c_mode[4];
139
140 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
141 if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
142 {
143 // Iff __del is true, then close will fclose the fd.
144 _M_cfile_created = __del;
145
146 if (__fd == 0)
147 setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
148
149 __ret = this;
150 }
151 return __ret;
152 }
153
154 int
155 __basic_file<char>::sys_getc()
156 { return getc(_M_cfile); }
157
158 int
159 __basic_file<char>::sys_ungetc(int __c)
160 { return ungetc(__c, _M_cfile); }
161
162 __basic_file<char>*
163 __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
164 int /*__prot*/)
165 {
166 __basic_file* __ret = NULL;
167 int __p_mode = 0;
168 int __rw_mode = 0;
169 char __c_mode[4];
170
171 _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
172
173 if (!this->is_open())
174 {
175 if ((_M_cfile = fopen(__name, __c_mode)))
176 {
177 _M_cfile_created = true;
178 __ret = this;
179 }
180 }
181 return __ret;
182 }
183
184 bool
185 __basic_file<char>::is_open() const
186 { return _M_cfile != 0; }
187
188 int
189 __basic_file<char>::fd()
190 { return fileno(_M_cfile) ; }
191
192 __basic_file<char>*
193 __basic_file<char>::close()
194 {
195 __basic_file* __retval = static_cast<__basic_file*>(NULL);
196 if (this->is_open())
197 {
198 if (_M_cfile_created)
199 fclose(_M_cfile);
200 else
201 fflush(_M_cfile);
202 _M_cfile = 0;
203 __retval = this;
204 }
205 return __retval;
206 }
207
208 streamsize
209 __basic_file<char>::xsgetn(char* __s, streamsize __n)
210 {
211 streamsize __ret;
212 do
213 __ret = read(this->fd(), __s, __n);
214 while (__ret == -1L && errno == EINTR);
215 return __ret;
216 }
217
218 streamsize
219 __basic_file<char>::xsputn(const char* __s, streamsize __n)
220 {
221 streamsize __ret;
222 do
223 __ret = write(this->fd(), __s, __n);
224 while (__ret == -1L && errno == EINTR);
225 return __ret;
226 }
227
228 streamoff
229 __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way,
230 ios_base::openmode /*__mode*/)
231 { return lseek(this->fd(), __off, __way); }
232
233 streamoff
234 __basic_file<char>::seekpos(streamoff __pos, ios_base::openmode /*__mode*/)
235 { return lseek(this->fd(), __pos, ios_base::beg); }
236
237 int
238 __basic_file<char>::sync()
239 { return fflush(_M_cfile); }
240
241 streamsize
242 __basic_file<char>::showmanyc()
243 {
244 #ifdef FIONREAD
245 // Pipes and sockets.
246 int __num = 0;
247 int __r = ioctl(this->fd(), FIONREAD, &__num);
248 if (!__r && __num >= 0)
249 return __num;
250 #endif
251
252 #ifdef _GLIBCPP_HAVE_POLL
253 // Cheap test.
254 struct pollfd __pfd[1];
255 __pfd[0].fd = this->fd();
256 __pfd[0].events = POLLIN;
257 if (poll(__pfd, 1, 0) <= 0)
258 return 0;
259 #endif
260
261 #if defined(_GLIBCPP_HAVE_S_ISREG) || defined(_GLIBCPP_HAVE_S_IFREG)
262 // Regular files.
263 struct stat __buffer;
264 int __ret = fstat(this->fd(), &__buffer);
265 if (!__ret && _GLIBCPP_ISREG(__buffer.st_mode))
266 return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
267 #endif
268 return 0;
269 }
270
271 } // namespace std