OSDN Git Service

2003-03-28 Paolo Carlini <pcarlini@unitus.it>
[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 <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         fflush(_M_cfile);
199         if ((_M_cfile_created && fclose(_M_cfile) == 0) || !_M_cfile_created)
200           {
201             _M_cfile = 0;
202             __retval = this;
203           }
204       }
205     return __retval;
206   }
207  
208   // In the next four functions we want to use stdio functions only
209   // when synced with stdio (_M_buf_size == 1): I/O primitives do not
210   // block until the asked number of bytes are available.
211   streamsize 
212   __basic_file<char>::xsgetn(char* __s, streamsize __n, bool __stdio)
213   {
214     if (__stdio)
215       return fread(__s, 1, __n, _M_cfile);
216     else
217       {
218         streamsize __ret;
219         do
220           __ret = read(this->fd(), __s, __n);
221         while (__ret == -1L && errno == EINTR);
222         return __ret;
223       }
224   }
225     
226   streamsize 
227   __basic_file<char>::xsputn(const char* __s, streamsize __n, bool __stdio)
228   {
229     if (__stdio)
230       return fwrite(__s, 1, __n, _M_cfile);
231     else
232       {
233         streamsize __ret;
234         do
235           __ret = write(this->fd(), __s, __n);
236         while (__ret == -1L && errno == EINTR);
237         return __ret;
238       }
239   }
240   
241   streamoff
242   __basic_file<char>::seekoff(streamoff __off, ios_base::seekdir __way, 
243                               bool __stdio, ios_base::openmode /*__mode*/)
244   { 
245     if (!__stdio)
246       return lseek(this->fd(), __off, __way);
247     else
248       {
249         if (!fseek(_M_cfile, __off, __way))
250           return ftell(_M_cfile); 
251         else
252           // Fseek failed.
253           return -1L;
254       }
255   }
256
257   streamoff
258   __basic_file<char>::seekpos(streamoff __pos, bool __stdio,
259                               ios_base::openmode /*__mode*/)
260   { 
261     if (!__stdio)
262       return lseek(this->fd(), __pos, ios_base::beg);
263     else
264       {
265         if (!fseek(_M_cfile, __pos, ios_base::beg))
266           return ftell(_M_cfile);
267         else
268           // Fseek failed.
269           return -1L;
270       }
271   }
272   
273   int 
274   __basic_file<char>::sync() 
275   { return fflush(_M_cfile); }
276
277   streamsize
278   __basic_file<char>::showmanyc_helper(bool __stdio)
279   {
280 #ifdef FIONREAD
281     // Pipes and sockets.    
282     int __num = 0;
283     int __r = ioctl(this->fd(), FIONREAD, &__num);
284     if (!__r && __num >= 0)
285       return __num; 
286 #endif    
287
288 #ifdef _GLIBCPP_HAVE_POLL
289     // Cheap test.
290     struct pollfd __pfd[1];
291     __pfd[0].fd = this->fd();
292     __pfd[0].events = POLLIN;
293     if (poll(__pfd, 1, 0) <= 0)
294       return 0;
295 #endif   
296
297 #if defined(_GLIBCPP_HAVE_S_ISREG) || defined(_GLIBCPP_HAVE_S_IFREG)
298     // Regular files.
299     struct stat __buffer;
300     int __ret = fstat(this->fd(), &__buffer);
301     if (!__ret && _GLIBCPP_ISREG(__buffer.st_mode))
302       if (__stdio)
303         return __buffer.st_size - ftell(_M_cfile);
304       else
305         return __buffer.st_size - lseek(this->fd(), 0, ios_base::cur);
306 #endif
307     return 0;
308   }
309
310 }  // namespace std