OSDN Git Service

2003-09-25 Brad Spencer <spencer@infointeractive.com>
[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 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 namespace std 
72 {
73   // Definitions for __basic_file<char>.
74   __basic_file<char>::__basic_file(__c_lock* /*__lock*/) 
75   : _M_cfile(NULL), _M_cfile_created(false) { }
76
77   __basic_file<char>::~__basic_file()
78   { this->close(); }
79       
80   void 
81   __basic_file<char>::_M_open_mode(ios_base::openmode __mode, int& __p_mode, 
82                                    int&, char* __c_mode)
83   {  
84     bool __testb = __mode & ios_base::binary;
85     bool __testi = __mode & ios_base::in;
86     bool __testo = __mode & ios_base::out;
87     bool __testt = __mode & ios_base::trunc;
88     bool __testa = __mode & ios_base::app;
89       
90     // Set __c_mode for use in fopen.
91     // Set __p_mode for use in open.
92     if (!__testi && __testo && !__testt && !__testa)
93       {
94         strcpy(__c_mode, "w");
95         __p_mode = O_WRONLY | O_CREAT;
96       }
97     if (!__testi && __testo && !__testt && __testa)
98       {
99         strcpy(__c_mode, "a");
100         __p_mode = O_WRONLY | O_CREAT | O_APPEND;
101       }
102     if (!__testi && __testo && __testt && !__testa)
103       {
104         strcpy(__c_mode, "w");
105         __p_mode = O_WRONLY | O_CREAT | O_TRUNC;
106       }
107
108     if (__testi && !__testo && !__testt && !__testa)
109       {
110         strcpy(__c_mode, "r");
111         __p_mode = O_RDONLY;
112       }
113     if (__testi && __testo && !__testt && !__testa)
114       {
115         strcpy(__c_mode, "r+");
116         __p_mode = O_RDWR | O_CREAT;
117       }
118     if (__testi && __testo && __testt && !__testa)
119       {
120         strcpy(__c_mode, "w+");
121         __p_mode = O_RDWR | O_CREAT | O_TRUNC;
122       }
123     if (__testb)
124       strcat(__c_mode, "b");
125   }
126   
127   __basic_file<char>*
128   __basic_file<char>::sys_open(__c_file* __file, ios_base::openmode) 
129   {
130     __basic_file* __ret = NULL;
131     if (!this->is_open() && __file)
132       {
133         _M_cfile = __file;
134         _M_cfile_created = false;
135         __ret = this;
136       }
137     return __ret;
138   }
139   
140   __basic_file<char>*
141   __basic_file<char>::sys_open(int __fd, ios_base::openmode __mode, 
142                                bool __del) 
143   {
144     __basic_file* __ret = NULL;
145     int __p_mode = 0;
146     int __rw_mode = 0;
147     char __c_mode[4];
148     
149     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
150     if (!this->is_open() && (_M_cfile = fdopen(__fd, __c_mode)))
151       {
152         // Iff __del is true, then close will fclose the fd.
153         _M_cfile_created = __del;
154
155         if (__fd == 0)
156           setvbuf(_M_cfile, reinterpret_cast<char*>(NULL), _IONBF, 0);
157
158         __ret = this;
159       }
160     return __ret;
161   }
162   
163   __basic_file<char>* 
164   __basic_file<char>::open(const char* __name, ios_base::openmode __mode, 
165                            int /*__prot*/)
166   {
167     __basic_file* __ret = NULL;
168     int __p_mode = 0;
169     int __rw_mode = 0;
170     char __c_mode[4];
171       
172     _M_open_mode(__mode, __p_mode, __rw_mode, __c_mode);
173
174     if (!this->is_open())
175       {
176         if ((_M_cfile = fopen(__name, __c_mode)))
177           {
178             _M_cfile_created = true;
179             __ret = this;
180           }
181       }
182     return __ret;
183   }
184   
185   bool 
186   __basic_file<char>::is_open() const 
187   { return _M_cfile != 0; }
188   
189   int 
190   __basic_file<char>::fd() 
191   { return fileno(_M_cfile) ; }
192   
193   __basic_file<char>* 
194   __basic_file<char>::close()
195   { 
196     __basic_file* __retval = static_cast<__basic_file*>(NULL);
197     if (this->is_open())
198       {
199         if (_M_cfile_created)
200           fclose(_M_cfile);
201         else
202           fflush(_M_cfile);
203         _M_cfile = 0;
204         __retval = this;
205       }
206     return __retval;
207   }
208  
209   streamsize 
210   __basic_file<char>::xsgetn(char* __s, streamsize __n)
211   {
212     streamsize __ret;
213     do
214       __ret = read(this->fd(), __s, __n);
215     while (__ret == -1L && errno == EINTR);
216     return __ret;
217   }
218     
219   streamsize 
220   __basic_file<char>::xsputn(const char* __s, streamsize __n)
221   {
222     streamsize __ret;
223     do
224       __ret = write(this->fd(), __s, __n);
225     while (__ret == -1L && errno == EINTR);
226     return __ret;
227   }
228
229   streamsize 
230   __basic_file<char>::xsputn_2(const char* __s1, streamsize __n1,
231                                const char* __s2, streamsize __n2)
232   {
233     streamsize __ret = 0;
234 #ifdef _GLIBCXX_HAVE_WRITEV
235     struct iovec __iov[2];
236     __iov[0].iov_base = const_cast<char*>(__s1);
237     __iov[0].iov_len = __n1;
238     __iov[1].iov_base = const_cast<char*>(__s2);
239     __iov[1].iov_len = __n2;
240
241     do
242       __ret = writev(this->fd(), __iov, 2);
243     while (__ret == -1L && errno == EINTR);
244 #else
245     if (__n1)
246       do
247         __ret = write(this->fd(), __s1, __n1);
248       while (__ret == -1L && errno == EINTR);
249
250     if (__ret == __n1)
251       {
252         do
253           __ret = write(this->fd(), __s2, __n2);
254         while (__ret == -1L && errno == EINTR);
255         
256         if (__ret != -1L)
257           __ret += __n1;
258       }
259 #endif
260     return __ret;
261   }
262
263   streampos
264   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way, 
265                               ios_base::openmode /*__mode*/)
266   { return lseek(this->fd(), __off, __way); }
267
268   streampos
269   __basic_file<char>::seekpos(streampos __pos, ios_base::openmode /*__mode*/)
270   { return lseek(this->fd(), __pos, ios_base::beg); }
271
272   int 
273   __basic_file<char>::sync() 
274   { return fflush(_M_cfile); }
275
276   streamsize
277   __basic_file<char>::showmanyc()
278   {
279 #ifdef FIONREAD
280     // Pipes and sockets.    
281     int __num = 0;
282     int __r = ioctl(this->fd(), FIONREAD, &__num);
283     if (!__r && __num >= 0)
284       return __num; 
285 #endif    
286
287 #ifdef _GLIBCXX_HAVE_POLL
288     // Cheap test.
289     struct pollfd __pfd[1];
290     __pfd[0].fd = this->fd();
291     __pfd[0].events = POLLIN;
292     if (poll(__pfd, 1, 0) <= 0)
293       return 0;
294 #endif   
295
296 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
297     // Regular files.
298     struct stat __buffer;
299     int __ret = fstat(this->fd(), &__buffer);
300     if (!__ret && _GLIBCXX_ISREG(__buffer.st_mode))
301         return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
302 #endif
303     return 0;
304   }
305
306 }  // namespace std