3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
19 #if defined (__SUNPPRO_CC) && !defined (_STLP_NO_NEW_C_HEADERS)
21 // For sunpro, it chokes if time.h is included through stat.h
27 # define __int64 long long
31 #if !defined(__ISCPP__)
33 # include <sys/stat.h>
37 #if defined( __MSL__ )
41 #if defined(__ISCPP__)
42 # include <c_locale_is/filestat.h>
45 #if defined(__BEOS__) && defined(__INTEL__)
47 # include <sys/stat.h> // For _fstat
50 #if defined (_STLP_MSVC) || defined (__MINGW32__)
52 # define S_IREAD _S_IREAD
53 # define S_IWRITE _S_IWRITE
54 # define S_IFREG _S_IFREG
55 // map permission masks
57 # define S_IRUSR _S_IREAD
58 # define S_IWUSR _S_IWRITE
61 # define S_IRGRP _S_IREAD
62 # define S_IWGRP _S_IWRITE
65 # define S_IROTH _S_IREAD
66 # define S_IWOTH _S_IWRITE
70 # define O_RDONLY _O_RDONLY
71 # define O_WRONLY _O_WRONLY
72 # define O_RDWR _O_RDWR
73 # define O_APPEND _O_APPEND
74 # define O_CREAT _O_CREAT
75 # define O_TRUNC _O_TRUNC
76 # define O_TEXT _O_TEXT
77 # define O_BINARY _O_BINARY
81 # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
85 const _STLP_fd INVALID_STLP_FD = -1;
90 # if !defined( O_TEXT )
91 # define O_TEXT _O_TEXT
93 # define _S_IFREG S_IFREG
94 # define S_IREAD S_IRUSR
95 # define S_IWRITE S_IWUSR
96 # define S_IEXEC S_IXUSR
97 # define _S_IWRITE S_IWRITE
98 # define _S_IREAD S_IREAD
100 # define _close close
102 # define _write write
105 _STLP_BEGIN_NAMESPACE
107 // Compare with streamoff definition in stl/char_traits.h!
109 #if defined (_STLP_USE_DEFAULT_FILE_OFFSET) || \
110 (!defined(_LARGEFILE_SOURCE) && !defined(_LARGEFILE64_SOURCE))
117 # define FOPEN fopen64
118 # define FSEEK fseeko64
119 # define FSTAT fstat64
121 # define FTELL ftello64
124 _STLP_MOVE_TO_PRIV_NAMESPACE
126 // Helper functions for _Filebuf_base.
128 static bool __is_regular_file(_STLP_fd fd) {
130 return FSTAT(fd, &buf) == 0 && (buf.st_mode & S_IFREG) != 0 ;
133 // Number of characters in the file.
134 static streamoff __file_size(_STLP_fd fd) {
138 if (FSTAT(fd, &buf) == 0 && (buf.st_mode & S_IFREG) != 0)
139 ret = buf.st_size > 0 ? buf.st_size : 0;
144 _STLP_MOVE_TO_STD_NAMESPACE
146 // All version of Unix have mmap and lseek system calls. Some also have
147 // longer versions of those system calls to accommodate 64-bit offsets.
148 // If we're on a Unix system, define some macros to encapsulate those
151 size_t _Filebuf_base::_M_page_size = 4096;
153 _Filebuf_base::_Filebuf_base()
154 : _M_file_id(INVALID_STLP_FD),
157 _M_should_close(false)
160 void _Filebuf_base::_S_initialize()
165 // Return the size of the file. This is a wrapper for stat.
166 // Returns zero if the size cannot be determined or is ill-defined.
167 streamoff _Filebuf_base::_M_file_size()
169 return _STLP_PRIV __file_size(_M_file_id);
172 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode,
180 // use FILE-based i/o
183 switch (openmode & (~ios_base::ate)) {
185 case ios_base::out | ios_base::trunc:
189 case ios_base::out | ios_base::binary:
190 case ios_base::out | ios_base::trunc | ios_base::binary:
194 case ios_base::out | ios_base::app:
198 case ios_base::out | ios_base::app | ios_base::binary:
206 case ios_base::in | ios_base::binary:
210 case ios_base::in | ios_base::out:
214 case ios_base::in | ios_base::out | ios_base::binary:
218 case ios_base::in | ios_base::out | ios_base::trunc:
222 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
226 default: // The above are the only combinations of
227 return false; // flags allowed by the C++ standard.
230 // fbp : TODO : set permissions !
231 (void)permission; // currently unused //*TY 02/26/2000 - added to suppress warning message
232 _M_file = FOPEN(name, flags);
235 file_no = fileno(_M_file);
240 // unset buffering immediately
245 if (openmode & ios_base::ate) {
246 if (FSEEK(_M_file, 0, SEEK_END) != 0)
250 _M_file_id = file_no;
251 _M_should_close = _M_is_open;
252 _M_openmode = openmode;
255 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
257 return (_M_is_open != 0);
261 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode)
263 // This doesn't really grant everyone in the world read/write
264 // access. On Unix, file-creation system calls always clear
265 // bits that are set in the umask from the permissions flag.
266 return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP |
267 S_IWGRP | S_IROTH | S_IWOTH);
270 // Associated the filebuf with a file descriptor pointing to an already-
271 // open file. Mode is set to be consistent with the way that the file
273 bool _Filebuf_base::_M_open( int file_no, ios_base::openmode )
275 if (_M_is_open || file_no < 0)
279 if (FSTAT(file_no, &buf) != 0)
281 int mode = buf.st_mode;
283 switch ( mode & (S_IWRITE | S_IREAD) ) {
285 _M_openmode = ios_base::in;
288 _M_openmode = ios_base::out;
290 case (S_IWRITE | S_IREAD):
291 _M_openmode = ios_base::in | ios_base::out;
296 _M_file_id = file_no;
298 _M_should_close = false;
299 _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
303 bool _Filebuf_base::_M_close()
308 bool ok = _M_should_close ? (fclose(_M_file) == 0) : true;
310 _M_is_open = _M_should_close = false;
315 // Read up to n characters into a buffer. Return value is number of
317 ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n) {
318 return fread(buf, 1, n, _M_file);
321 // Write n characters from a buffer. Return value: true if we managed
322 // to write the entire buffer, false if we didn't.
323 bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n)
326 ptrdiff_t written = fwrite(buf, 1, n, _M_file);
332 if (written > 0 && written < n) {
341 // Wrapper for lseek or the like.
342 streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir)
348 if (offset < 0 /* || offset > _M_file_size() */ )
349 return streamoff(-1);
356 if (/* offset > 0 || */ -offset > _M_file_size() )
357 return streamoff(-1);
361 return streamoff(-1);
364 if ( FSEEK(_M_file, offset, whence) == 0 ) {
365 return FTELL(_M_file);
368 return streamoff(-1);
372 // Attempts to memory-map len bytes of the current file, starting
373 // at position offset. Precondition: offset is a multiple of the
374 // page size. Postcondition: return value is a null pointer if the
375 // memory mapping failed. Otherwise the return value is a pointer to
376 // the memory-mapped file and the file position is set to offset.
377 void *_Filebuf_base::_M_mmap(streamoff, streamoff )
382 void _Filebuf_base::_M_unmap(void*, streamoff)
384 // precondition : there is a valid mapping at the moment