xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/config/io/basic_file_stdio.cc (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 // Wrapper of C-language FILE struct -*- C++ -*-
2 
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.
11 
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.
16 
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.
20 
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/>.
25 
26 //
27 // ISO C++ 14882: 27.8  File-based streams
28 //
29 
30 #include <bits/basic_file.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 
34 #ifdef _GLIBCXX_HAVE_POLL
35 #include <poll.h>
36 #endif
37 
38 // Pick up ioctl on Solaris 2.8
39 #ifdef _GLIBCXX_HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
42 
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
48 
49 // Pick up FIONREAD on Solaris 2.5.
50 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
51 #include <sys/filio.h>
52 #endif
53 
54 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
55 #include <sys/uio.h>
56 #endif
57 
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
66 
67 #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
68 
69 namespace
70 {
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)
75   {
76     enum
77       {
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
83       };
84 
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))
88       {
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+";
98 
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";
108 
109       default: return 0; // invalid
110       }
111   }
112 
113   // Wrapper handling partial write.
114   static std::streamsize
115   xwrite(int __fd, const char* __s, std::streamsize __n)
116   {
117     std::streamsize __nleft = __n;
118 
119     for (;;)
120       {
121 	const std::streamsize __ret = write(__fd, __s, __nleft);
122 	if (__ret == -1L && errno == EINTR)
123 	  continue;
124 	if (__ret == -1L)
125 	  break;
126 
127 	__nleft -= __ret;
128 	if (__nleft == 0)
129 	  break;
130 
131 	__s += __ret;
132       }
133 
134     return __n - __nleft;
135   }
136 
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)
142   {
143     std::streamsize __nleft = __n1 + __n2;
144     std::streamsize __n1_left = __n1;
145 
146     struct iovec __iov[2];
147     __iov[1].iov_base = const_cast<char*>(__s2);
148     __iov[1].iov_len = __n2;
149 
150     for (;;)
151       {
152 	__iov[0].iov_base = const_cast<char*>(__s1);
153 	__iov[0].iov_len = __n1_left;
154 
155 	const std::streamsize __ret = writev(__fd, __iov, 2);
156 	if (__ret == -1L && errno == EINTR)
157 	  continue;
158 	if (__ret == -1L)
159 	  break;
160 
161 	__nleft -= __ret;
162 	if (__nleft == 0)
163 	  break;
164 
165 	const std::streamsize __off = __ret - __n1_left;
166 	if (__off >= 0)
167 	  {
168 	    __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
169 	    break;
170 	  }
171 
172 	__s1 += __ret;
173 	__n1_left -= __ret;
174       }
175 
176     return __n1 + __n2 - __nleft;
177   }
178 #endif
179 } // anonymous namespace
180 
181 
182 _GLIBCXX_BEGIN_NAMESPACE(std)
183 
184   // Definitions for __basic_file<char>.
185   __basic_file<char>::__basic_file(__c_lock* /*__lock*/) throw()
186   : _M_cfile(NULL), _M_cfile_created(false) { }
187 
188   __basic_file<char>::~__basic_file()
189   { this->close(); }
190 
191   __basic_file<char>*
192   __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
193   {
194     __basic_file* __ret = NULL;
195     if (!this->is_open() && __file)
196       {
197 	int __err;
198 	errno = 0;
199 	do
200 	  __err = this->sync();
201 	while (__err && errno == EINTR);
202 	if (!__err)
203 	  {
204 	    _M_cfile = __file;
205 	    _M_cfile_created = false;
206 	    __ret = this;
207 	  }
208       }
209     return __ret;
210   }
211 
212   __basic_file<char>*
213   __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode) throw ()
214   {
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)))
218       {
219 	char* __buf = NULL;
220 	_M_cfile_created = true;
221 	if (__fd == 0)
222 	  setvbuf(_M_cfile, __buf, _IONBF, 0);
223 	__ret = this;
224       }
225     return __ret;
226   }
227 
228   __basic_file<char>*
229   __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
230 			   int /*__prot*/)
231   {
232     __basic_file* __ret = NULL;
233     const char* __c_mode = fopen_mode(__mode);
234     if (__c_mode && !this->is_open())
235       {
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
241 	  {
242 	    _M_cfile_created = true;
243 	    __ret = this;
244 	  }
245       }
246     return __ret;
247   }
248 
249   bool
250   __basic_file<char>::is_open() const throw ()
251   { return _M_cfile != 0; }
252 
253   int
254   __basic_file<char>::fd() throw ()
255   { return fileno(_M_cfile); }
256 
257   __c_file*
258   __basic_file<char>::file() throw ()
259   { return _M_cfile; }
260 
261   __basic_file<char>*
262   __basic_file<char>::close()
263   {
264     __basic_file* __ret = static_cast<__basic_file*>(NULL);
265     if (this->is_open())
266       {
267 	int __err = 0;
268 	if (_M_cfile_created)
269 	  {
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;
275 	    do
276 	      __err = fclose(_M_cfile);
277 	    while (__err && errno == EINTR);
278 	  }
279 	_M_cfile = 0;
280 	if (!__err)
281 	  __ret = this;
282       }
283     return __ret;
284   }
285 
286   streamsize
287   __basic_file<char>::xsgetn(char* __s, streamsize __n)
288   {
289     streamsize __ret;
290     do
291       __ret = read(this->fd(), __s, __n);
292     while (__ret == -1L && errno == EINTR);
293     return __ret;
294   }
295 
296   streamsize
297   __basic_file<char>::xsputn(const char* __s, streamsize __n)
298   { return xwrite(this->fd(), __s, __n); }
299 
300   streamsize
301   __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
302 			       const char* __s2, streamsize __n2)
303   {
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);
310 
311     if (__ret == __n1)
312       __ret += xwrite(this->fd(), __s2, __n2);
313 #endif
314     return __ret;
315   }
316 
317   streamoff
318   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way) throw ()
319   {
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
328   }
329 
330   int
331   __basic_file<char>::sync()
332   { return fflush(_M_cfile); }
333 
334   streamsize
335   __basic_file<char>::showmanyc()
336   {
337 #ifdef FIONREAD
338     // Pipes and sockets.
339 #ifdef _GLIBCXX_FIONREAD_TAKES_OFF_T
340     off_t __num = 0;
341 #else
342     int __num = 0;
343 #endif
344     int __r = ioctl(this->fd(), FIONREAD, &__num);
345     if (!__r && __num >= 0)
346       return __num;
347 #endif
348 
349 #ifdef _GLIBCXX_HAVE_POLL
350     // Cheap test.
351     struct pollfd __pfd[1];
352     __pfd[0].fd = this->fd();
353     __pfd[0].events = POLLIN;
354     if (poll(__pfd, 1, 0) <= 0)
355       return 0;
356 #endif
357 
358 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
359     // Regular files.
360 #ifdef _GLIBCXX_USE_LFS
361     struct stat64 __buffer;
362     const int __err = fstat64(this->fd(), &__buffer);
363     if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
364       {
365 	const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
366 							   ios_base::cur);
367 	return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
368       }
369 #else
370     struct stat __buffer;
371     const int __err = fstat(this->fd(), &__buffer);
372     if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
373       return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
374 #endif
375 #endif
376     return 0;
377   }
378 
379 _GLIBCXX_END_NAMESPACE
380 
381