OSDN Git Service

b1d55d0e02e09a46150ff83474803ca9f274494c
[pf3gnuchains/gcc-fork.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, 2004 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 <errno.h>
37
38 #ifdef _GLIBCXX_HAVE_POLL
39 #include <poll.h>
40 #endif
41
42 // Pick up ioctl on Solaris 2.8
43 #ifdef _GLIBCXX_HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 // Pick up FIONREAD on Solaris 2
48 #ifdef _GLIBCXX_HAVE_SYS_IOCTL_H
49 #define BSD_COMP 
50 #include <sys/ioctl.h>
51 #endif
52
53 // Pick up FIONREAD on Solaris 2.5.
54 #ifdef _GLIBCXX_HAVE_SYS_FILIO_H
55 #include <sys/filio.h>
56 #endif
57
58 #ifdef _GLIBCXX_HAVE_SYS_UIO_H
59 #include <sys/uio.h>
60 #endif
61
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)
66 # else
67 #  define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
68 # endif
69 #endif
70
71 #include <limits> // For <off_t>::max() and min()
72
73 namespace __gnu_internal
74 {
75   // Map ios_base::openmode flags to a string for use in fopen().
76   // Table of valid combinations as given in [lib.filebuf.members]/2.
77   static const char*
78   fopen_mode(std::ios_base::openmode mode)
79   {
80     enum 
81       {
82         in     = std::ios_base::in,
83         out    = std::ios_base::out,
84         trunc  = std::ios_base::trunc,
85         app    = std::ios_base::app,
86         binary = std::ios_base::binary
87       };
88     
89     switch (mode & (in|out|trunc|app|binary))
90       {
91       case (   out                 ): return "w";  
92       case (   out      |app       ): return "a";  
93       case (   out|trunc           ): return "w";  
94       case (in                     ): return "r";  
95       case (in|out                 ): return "r+"; 
96       case (in|out|trunc           ): return "w+"; 
97         
98       case (   out          |binary): return "wb"; 
99       case (   out      |app|binary): return "ab"; 
100       case (   out|trunc    |binary): return "wb"; 
101       case (in              |binary): return "rb"; 
102       case (in|out          |binary): return "r+b";
103       case (in|out|trunc    |binary): return "w+b";
104         
105       default: return 0; // invalid
106       }
107   }
108 } // namespace __gnu_internal
109
110 namespace std 
111 {
112   // Definitions for __basic_file<char>.
113   __basic_file<char>::__basic_file(__c_lock* /*__lock*/) 
114   : _M_cfile(NULL), _M_cfile_created(false) { }
115
116   __basic_file<char>::~__basic_file()
117   { this->close(); }
118       
119   __basic_file<char>*
120   __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode) 
121   {
122     __basic_file* __ret = NULL;
123     if (!this->is_open() && __file)
124       {
125         _M_cfile = __file;
126         _M_cfile_created = false;
127         this->sync();
128         __ret = this;
129       }
130     return __ret;
131   }
132   
133   __basic_file<char>*
134   __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode)
135   {
136     __basic_file* __ret = NULL;
137     const char* __c_mode = __gnu_internal::fopen_mode(__mode);
138     if (__c_mode && !this->is_open() 
139         && (_M_cfile = fdopen(__fd, __c_mode)))
140       {
141         _M_cfile_created = true;
142         if (__fd == 0)
143           setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
144         __ret = this;
145       }
146     return __ret;
147   }
148   
149   __basic_file<char>* 
150   __basic_file<char>::open(const char* __name, ios_base::openmode __mode, 
151                            int /*__prot*/)
152   {
153     __basic_file* __ret = NULL;
154     const char* __c_mode = __gnu_internal::fopen_mode(__mode);
155     if (__c_mode && !this->is_open())
156       {
157 #ifdef _GLIBCXX_USE_LFS
158         if ((_M_cfile = fopen64(__name, __c_mode)))
159 #else
160         if ((_M_cfile = fopen(__name, __c_mode)))
161 #endif
162           {
163             _M_cfile_created = true;
164             __ret = this;
165           }
166       }
167     return __ret;
168   }
169   
170   bool 
171   __basic_file<char>::is_open() const 
172   { return _M_cfile != 0; }
173   
174   int 
175   __basic_file<char>::fd() 
176   { return fileno(_M_cfile) ; }
177   
178   __basic_file<char>* 
179   __basic_file<char>::close()
180   { 
181     __basic_file* __ret = static_cast<__basic_file*>(NULL);
182     if (this->is_open())
183       {
184         if (_M_cfile_created)
185           fclose(_M_cfile);
186         else
187           this->sync();
188         _M_cfile = 0;
189         __ret = this;
190       }
191     return __ret;
192   }
193  
194   streamsize 
195   __basic_file<char>::xsgetn(char* __s, streamsize __n)
196   {
197     streamsize __ret;
198     do
199       __ret = read(this->fd(), __s, __n);
200     while (__ret == -1L && errno == EINTR);
201     return __ret;
202   }
203     
204   streamsize 
205   __basic_file<char>::xsputn(const char* __s, streamsize __n)
206   {
207     streamsize __ret;
208     do
209       __ret = write(this->fd(), __s, __n);
210     while (__ret == -1L && errno == EINTR);
211     return __ret;
212   }
213
214   streamsize 
215   __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
216                                const char* __s2, streamsize __n2)
217   {
218     streamsize __ret = 0;
219 #ifdef _GLIBCXX_HAVE_WRITEV
220     struct iovec __iov[2];
221     __iov[0].iov_base = const_cast<char*>(__s1);
222     __iov[0].iov_len = __n1;
223     __iov[1].iov_base = const_cast<char*>(__s2);
224     __iov[1].iov_len = __n2;
225
226     do
227       __ret = writev(this->fd(), __iov, 2);
228     while (__ret == -1L && errno == EINTR);
229 #else
230     if (__n1)
231       do
232         __ret = write(this->fd(), __s1, __n1);
233       while (__ret == -1L && errno == EINTR);
234
235     if (__ret == __n1)
236       {
237         do
238           __ret = write(this->fd(), __s2, __n2);
239         while (__ret == -1L && errno == EINTR);
240         
241         if (__ret != -1L)
242           __ret += __n1;
243       }
244 #endif
245     return __ret;
246   }
247
248   streamoff
249   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way)
250   {
251 #ifdef _GLIBCXX_USE_LFS
252     return lseek64(this->fd(), __off, __way);
253 #else
254     if (__off > std::numeric_limits<off_t>::max()
255         || __off < std::numeric_limits<off_t>::min())
256       return -1L;
257     return lseek(this->fd(), __off, __way);
258 #endif
259   }
260
261   int 
262   __basic_file<char>::sync() 
263   { return fflush(_M_cfile); }
264
265   streamsize
266   __basic_file<char>::showmanyc()
267   {
268 #ifdef FIONREAD
269     // Pipes and sockets.    
270     int __num = 0;
271     int __r = ioctl(this->fd(), FIONREAD, &__num);
272     if (!__r && __num >= 0)
273       return __num; 
274 #endif    
275
276 #ifdef _GLIBCXX_HAVE_POLL
277     // Cheap test.
278     struct pollfd __pfd[1];
279     __pfd[0].fd = this->fd();
280     __pfd[0].events = POLLIN;
281     if (poll(__pfd, 1, 0) <= 0)
282       return 0;
283 #endif   
284
285 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
286     // Regular files.
287     struct stat __buffer;
288     int __ret = fstat(this->fd(), &__buffer);
289     if (!__ret && _GLIBCXX_ISREG(__buffer.st_mode))
290         return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
291 #endif
292     return 0;
293   }
294 }  // namespace std