1*404b540aSrobert // Wrapper of C-language FILE struct -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert //
32*404b540aSrobert // ISO C++ 14882: 27.8 File-based streams
33*404b540aSrobert //
34*404b540aSrobert
35*404b540aSrobert #include <bits/basic_file.h>
36*404b540aSrobert #include <fcntl.h>
37*404b540aSrobert #include <errno.h>
38*404b540aSrobert
39*404b540aSrobert #ifdef _GLIBCXX_HAVE_POLL
40*404b540aSrobert #include <poll.h>
41*404b540aSrobert #endif
42*404b540aSrobert
43*404b540aSrobert // Pick up ioctl on Solaris 2.8
44*404b540aSrobert #ifdef _GLIBCXX_HAVE_UNISTD_H
45*404b540aSrobert #include <unistd.h>
46*404b540aSrobert #endif
47*404b540aSrobert
48*404b540aSrobert // Pick up FIONREAD on Solaris 2
49*404b540aSrobert #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
50*404b540aSrobert #define BSD_COMP
51*404b540aSrobert #include <sys/ioctl.h>
52*404b540aSrobert #endif
53*404b540aSrobert
54*404b540aSrobert // Pick up FIONREAD on Solaris 2.5.
55*404b540aSrobert #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
56*404b540aSrobert #include <sys/filio.h>
57*404b540aSrobert #endif
58*404b540aSrobert
59*404b540aSrobert #ifdef _GLIBCXX_HAVE_SYS_UIO_H
60*404b540aSrobert #include <sys/uio.h>
61*404b540aSrobert #endif
62*404b540aSrobert
63*404b540aSrobert #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
64*404b540aSrobert # include <sys/stat.h>
65*404b540aSrobert # ifdef _GLIBCXX_HAVE_S_ISREG
66*404b540aSrobert # define _GLIBCXX_ISREG(x) S_ISREG(x)
67*404b540aSrobert # else
68*404b540aSrobert # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
69*404b540aSrobert # endif
70*404b540aSrobert #endif
71*404b540aSrobert
72*404b540aSrobert #include <limits> // For <off_t>::max() and min() and <streamsize>::max()
73*404b540aSrobert
74*404b540aSrobert namespace
75*404b540aSrobert {
76*404b540aSrobert // Map ios_base::openmode flags to a string for use in fopen().
77*404b540aSrobert // Table of valid combinations as given in [lib.filebuf.members]/2.
78*404b540aSrobert static const char*
fopen_mode(std::ios_base::openmode mode)79*404b540aSrobert fopen_mode(std::ios_base::openmode mode)
80*404b540aSrobert {
81*404b540aSrobert enum
82*404b540aSrobert {
83*404b540aSrobert in = std::ios_base::in,
84*404b540aSrobert out = std::ios_base::out,
85*404b540aSrobert trunc = std::ios_base::trunc,
86*404b540aSrobert app = std::ios_base::app,
87*404b540aSrobert binary = std::ios_base::binary
88*404b540aSrobert };
89*404b540aSrobert
90*404b540aSrobert switch (mode & (in|out|trunc|app|binary))
91*404b540aSrobert {
92*404b540aSrobert case ( out ): return "w";
93*404b540aSrobert case ( out |app ): return "a";
94*404b540aSrobert case ( out|trunc ): return "w";
95*404b540aSrobert case (in ): return "r";
96*404b540aSrobert case (in|out ): return "r+";
97*404b540aSrobert case (in|out|trunc ): return "w+";
98*404b540aSrobert // Extension to Table 92.
99*404b540aSrobert case (in|out |app ): return "a+";
100*404b540aSrobert
101*404b540aSrobert case ( out |binary): return "wb";
102*404b540aSrobert case ( out |app|binary): return "ab";
103*404b540aSrobert case ( out|trunc |binary): return "wb";
104*404b540aSrobert case (in |binary): return "rb";
105*404b540aSrobert case (in|out |binary): return "r+b";
106*404b540aSrobert case (in|out|trunc |binary): return "w+b";
107*404b540aSrobert // Extension to Table 92.
108*404b540aSrobert case (in|out |app|binary): return "a+b";
109*404b540aSrobert
110*404b540aSrobert default: return 0; // invalid
111*404b540aSrobert }
112*404b540aSrobert }
113*404b540aSrobert
114*404b540aSrobert // Wrapper handling partial write.
115*404b540aSrobert static std::streamsize
xwrite(int __fd,const char * __s,std::streamsize __n)116*404b540aSrobert xwrite(int __fd, const char* __s, std::streamsize __n)
117*404b540aSrobert {
118*404b540aSrobert std::streamsize __nleft = __n;
119*404b540aSrobert
120*404b540aSrobert for (;;)
121*404b540aSrobert {
122*404b540aSrobert const std::streamsize __ret = write(__fd, __s, __nleft);
123*404b540aSrobert if (__ret == -1L && errno == EINTR)
124*404b540aSrobert continue;
125*404b540aSrobert if (__ret == -1L)
126*404b540aSrobert break;
127*404b540aSrobert
128*404b540aSrobert __nleft -= __ret;
129*404b540aSrobert if (__nleft == 0)
130*404b540aSrobert break;
131*404b540aSrobert
132*404b540aSrobert __s += __ret;
133*404b540aSrobert }
134*404b540aSrobert
135*404b540aSrobert return __n - __nleft;
136*404b540aSrobert }
137*404b540aSrobert
138*404b540aSrobert #ifdef _GLIBCXX_HAVE_WRITEV
139*404b540aSrobert // Wrapper handling partial writev.
140*404b540aSrobert static std::streamsize
xwritev(int __fd,const char * __s1,std::streamsize __n1,const char * __s2,std::streamsize __n2)141*404b540aSrobert xwritev(int __fd, const char* __s1, std::streamsize __n1,
142*404b540aSrobert const char* __s2, std::streamsize __n2)
143*404b540aSrobert {
144*404b540aSrobert std::streamsize __nleft = __n1 + __n2;
145*404b540aSrobert std::streamsize __n1_left = __n1;
146*404b540aSrobert
147*404b540aSrobert struct iovec __iov[2];
148*404b540aSrobert __iov[1].iov_base = const_cast<char*>(__s2);
149*404b540aSrobert __iov[1].iov_len = __n2;
150*404b540aSrobert
151*404b540aSrobert for (;;)
152*404b540aSrobert {
153*404b540aSrobert __iov[0].iov_base = const_cast<char*>(__s1);
154*404b540aSrobert __iov[0].iov_len = __n1_left;
155*404b540aSrobert
156*404b540aSrobert const std::streamsize __ret = writev(__fd, __iov, 2);
157*404b540aSrobert if (__ret == -1L && errno == EINTR)
158*404b540aSrobert continue;
159*404b540aSrobert if (__ret == -1L)
160*404b540aSrobert break;
161*404b540aSrobert
162*404b540aSrobert __nleft -= __ret;
163*404b540aSrobert if (__nleft == 0)
164*404b540aSrobert break;
165*404b540aSrobert
166*404b540aSrobert const std::streamsize __off = __ret - __n1_left;
167*404b540aSrobert if (__off >= 0)
168*404b540aSrobert {
169*404b540aSrobert __nleft -= xwrite(__fd, __s2 + __off, __n2 - __off);
170*404b540aSrobert break;
171*404b540aSrobert }
172*404b540aSrobert
173*404b540aSrobert __s1 += __ret;
174*404b540aSrobert __n1_left -= __ret;
175*404b540aSrobert }
176*404b540aSrobert
177*404b540aSrobert return __n1 + __n2 - __nleft;
178*404b540aSrobert }
179*404b540aSrobert #endif
180*404b540aSrobert } // anonymous namespace
181*404b540aSrobert
182*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)183*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
184*404b540aSrobert
185*404b540aSrobert // Definitions for __basic_file<char>.
186*404b540aSrobert __basic_file<char>::__basic_file(__c_lock* /*__lock*/)
187*404b540aSrobert : _M_cfile(NULL), _M_cfile_created(false) { }
188*404b540aSrobert
~__basic_file()189*404b540aSrobert __basic_file<char>::~__basic_file()
190*404b540aSrobert { this->close(); }
191*404b540aSrobert
192*404b540aSrobert __basic_file<char>*
sys_open(__c_file * __file,ios_base::openmode)193*404b540aSrobert __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode)
194*404b540aSrobert {
195*404b540aSrobert __basic_file* __ret = NULL;
196*404b540aSrobert if (!this->is_open() && __file)
197*404b540aSrobert {
198*404b540aSrobert int __err;
199*404b540aSrobert errno = 0;
200*404b540aSrobert do
201*404b540aSrobert __err = this->sync();
202*404b540aSrobert while (__err && errno == EINTR);
203*404b540aSrobert if (!__err)
204*404b540aSrobert {
205*404b540aSrobert _M_cfile = __file;
206*404b540aSrobert _M_cfile_created = false;
207*404b540aSrobert __ret = this;
208*404b540aSrobert }
209*404b540aSrobert }
210*404b540aSrobert return __ret;
211*404b540aSrobert }
212*404b540aSrobert
213*404b540aSrobert __basic_file<char>*
sys_open(int __fd,ios_base::openmode __mode)214*404b540aSrobert __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode)
215*404b540aSrobert {
216*404b540aSrobert __basic_file* __ret = NULL;
217*404b540aSrobert const char* __c_mode = fopen_mode(__mode);
218*404b540aSrobert if (__c_mode && !this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
219*404b540aSrobert {
220*404b540aSrobert char* __buf = NULL;
221*404b540aSrobert _M_cfile_created = true;
222*404b540aSrobert if (__fd == 0)
223*404b540aSrobert setvbuf(_M_cfile, __buf, _IONBF, 0);
224*404b540aSrobert __ret = this;
225*404b540aSrobert }
226*404b540aSrobert return __ret;
227*404b540aSrobert }
228*404b540aSrobert
229*404b540aSrobert __basic_file<char>*
open(const char * __name,ios_base::openmode __mode,int)230*404b540aSrobert __basic_file<char>::open(const char* __name, ios_base::openmode __mode,
231*404b540aSrobert int /*__prot*/)
232*404b540aSrobert {
233*404b540aSrobert __basic_file* __ret = NULL;
234*404b540aSrobert const char* __c_mode = fopen_mode(__mode);
235*404b540aSrobert if (__c_mode && !this->is_open())
236*404b540aSrobert {
237*404b540aSrobert #ifdef _GLIBCXX_USE_LFS
238*404b540aSrobert if ((_M_cfile = fopen64(__name, __c_mode)))
239*404b540aSrobert #else
240*404b540aSrobert if ((_M_cfile = fopen(__name, __c_mode)))
241*404b540aSrobert #endif
242*404b540aSrobert {
243*404b540aSrobert _M_cfile_created = true;
244*404b540aSrobert __ret = this;
245*404b540aSrobert }
246*404b540aSrobert }
247*404b540aSrobert return __ret;
248*404b540aSrobert }
249*404b540aSrobert
250*404b540aSrobert bool
is_open() const251*404b540aSrobert __basic_file<char>::is_open() const
252*404b540aSrobert { return _M_cfile != 0; }
253*404b540aSrobert
254*404b540aSrobert int
fd()255*404b540aSrobert __basic_file<char>::fd()
256*404b540aSrobert { return fileno(_M_cfile); }
257*404b540aSrobert
258*404b540aSrobert __c_file*
file()259*404b540aSrobert __basic_file<char>::file()
260*404b540aSrobert { return _M_cfile; }
261*404b540aSrobert
262*404b540aSrobert __basic_file<char>*
close()263*404b540aSrobert __basic_file<char>::close()
264*404b540aSrobert {
265*404b540aSrobert __basic_file* __ret = static_cast<__basic_file*>(NULL);
266*404b540aSrobert if (this->is_open())
267*404b540aSrobert {
268*404b540aSrobert int __err = 0;
269*404b540aSrobert if (_M_cfile_created)
270*404b540aSrobert {
271*404b540aSrobert // In general, no need to zero errno in advance if checking
272*404b540aSrobert // for error first. However, C89/C99 (at variance with IEEE
273*404b540aSrobert // 1003.1, f.i.) do not mandate that fclose must set errno
274*404b540aSrobert // upon error.
275*404b540aSrobert errno = 0;
276*404b540aSrobert do
277*404b540aSrobert __err = fclose(_M_cfile);
278*404b540aSrobert while (__err && errno == EINTR);
279*404b540aSrobert }
280*404b540aSrobert _M_cfile = 0;
281*404b540aSrobert if (!__err)
282*404b540aSrobert __ret = this;
283*404b540aSrobert }
284*404b540aSrobert return __ret;
285*404b540aSrobert }
286*404b540aSrobert
287*404b540aSrobert streamsize
xsgetn(char * __s,streamsize __n)288*404b540aSrobert __basic_file<char>::xsgetn(char* __s, streamsize __n)
289*404b540aSrobert {
290*404b540aSrobert streamsize __ret;
291*404b540aSrobert do
292*404b540aSrobert __ret = read(this->fd(), __s, __n);
293*404b540aSrobert while (__ret == -1L && errno == EINTR);
294*404b540aSrobert return __ret;
295*404b540aSrobert }
296*404b540aSrobert
297*404b540aSrobert streamsize
xsputn(const char * __s,streamsize __n)298*404b540aSrobert __basic_file<char>::xsputn(const char* __s, streamsize __n)
299*404b540aSrobert { return xwrite(this->fd(), __s, __n); }
300*404b540aSrobert
301*404b540aSrobert streamsize
xsputn_2(const char * __s1,streamsize __n1,const char * __s2,streamsize __n2)302*404b540aSrobert __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
303*404b540aSrobert const char* __s2, streamsize __n2)
304*404b540aSrobert {
305*404b540aSrobert streamsize __ret = 0;
306*404b540aSrobert #ifdef _GLIBCXX_HAVE_WRITEV
307*404b540aSrobert __ret = xwritev(this->fd(), __s1, __n1, __s2, __n2);
308*404b540aSrobert #else
309*404b540aSrobert if (__n1)
310*404b540aSrobert __ret = xwrite(this->fd(), __s1, __n1);
311*404b540aSrobert
312*404b540aSrobert if (__ret == __n1)
313*404b540aSrobert __ret += xwrite(this->fd(), __s2, __n2);
314*404b540aSrobert #endif
315*404b540aSrobert return __ret;
316*404b540aSrobert }
317*404b540aSrobert
318*404b540aSrobert streamoff
seekoff(streamoff __off,ios_base::seekdir __way)319*404b540aSrobert __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way)
320*404b540aSrobert {
321*404b540aSrobert #ifdef _GLIBCXX_USE_LFS
322*404b540aSrobert return lseek64(this->fd(), __off, __way);
323*404b540aSrobert #else
324*404b540aSrobert if (__off > numeric_limits<off_t>::max()
325*404b540aSrobert || __off < numeric_limits<off_t>::min())
326*404b540aSrobert return -1L;
327*404b540aSrobert return lseek(this->fd(), __off, __way);
328*404b540aSrobert #endif
329*404b540aSrobert }
330*404b540aSrobert
331*404b540aSrobert int
sync()332*404b540aSrobert __basic_file<char>::sync()
333*404b540aSrobert { return fflush(_M_cfile); }
334*404b540aSrobert
335*404b540aSrobert streamsize
showmanyc()336*404b540aSrobert __basic_file<char>::showmanyc()
337*404b540aSrobert {
338*404b540aSrobert #ifdef FIONREAD
339*404b540aSrobert // Pipes and sockets.
340*404b540aSrobert #ifdef _GLIBCXX_FIONREAD_TAKES_OFF_T
341*404b540aSrobert off_t __num = 0;
342*404b540aSrobert #else
343*404b540aSrobert int __num = 0;
344*404b540aSrobert #endif
345*404b540aSrobert int __r = ioctl(this->fd(), FIONREAD, &__num);
346*404b540aSrobert if (!__r && __num >= 0)
347*404b540aSrobert return __num;
348*404b540aSrobert #endif
349*404b540aSrobert
350*404b540aSrobert #ifdef _GLIBCXX_HAVE_POLL
351*404b540aSrobert // Cheap test.
352*404b540aSrobert struct pollfd __pfd[1];
353*404b540aSrobert __pfd[0].fd = this->fd();
354*404b540aSrobert __pfd[0].events = POLLIN;
355*404b540aSrobert if (poll(__pfd, 1, 0) <= 0)
356*404b540aSrobert return 0;
357*404b540aSrobert #endif
358*404b540aSrobert
359*404b540aSrobert #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
360*404b540aSrobert // Regular files.
361*404b540aSrobert #ifdef _GLIBCXX_USE_LFS
362*404b540aSrobert struct stat64 __buffer;
363*404b540aSrobert const int __err = fstat64(this->fd(), &__buffer);
364*404b540aSrobert if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
365*404b540aSrobert {
366*404b540aSrobert const streamoff __off = __buffer.st_size - lseek64(this->fd(), 0,
367*404b540aSrobert ios_base::cur);
368*404b540aSrobert return std::min(__off, streamoff(numeric_limits<streamsize>::max()));
369*404b540aSrobert }
370*404b540aSrobert #else
371*404b540aSrobert struct stat __buffer;
372*404b540aSrobert const int __err = fstat(this->fd(), &__buffer);
373*404b540aSrobert if (!__err && _GLIBCXX_ISREG(__buffer.st_mode))
374*404b540aSrobert return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
375*404b540aSrobert #endif
376*404b540aSrobert #endif
377*404b540aSrobert return 0;
378*404b540aSrobert }
379*404b540aSrobert
380*404b540aSrobert _GLIBCXX_END_NAMESPACE
381*404b540aSrobert
382