michael@0: /* michael@0: * Copyright (c) 1999 michael@0: * Silicon Graphics Computer Systems, Inc. michael@0: * michael@0: * Copyright (c) 1999 michael@0: * Boris Fomitchev michael@0: * michael@0: * This material is provided "as is", with absolutely no warranty expressed michael@0: * or implied. Any use is at your own risk. michael@0: * michael@0: * Permission to use or copy this software for any purpose is hereby granted michael@0: * without fee, provided the above notices are retained on all copies. michael@0: * Permission to modify the code and to distribute modified code is granted, michael@0: * provided the above notices are retained, and a notice that the code was michael@0: * modified is included with the above copyright notice. michael@0: * michael@0: */ michael@0: michael@0: #if defined (__SUNPPRO_CC) && !defined (_STLP_NO_NEW_C_HEADERS) michael@0: # include michael@0: // For sunpro, it chokes if time.h is included through stat.h michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #ifdef __CYGWIN__ michael@0: # define __int64 long long michael@0: #endif michael@0: michael@0: extern "C" { michael@0: // open/close/read/write michael@0: #include // For stat michael@0: #if !defined (_CRAY) && ! defined (__EMX__) michael@0: # include // For mmap michael@0: #endif michael@0: michael@0: // on HP-UX 11, this one contradicts with pthread.h on pthread_atfork, unless we unset this michael@0: #if defined (__hpux) && defined (__GNUC__) michael@0: # undef _INCLUDE_POSIX1C_SOURCE michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: } michael@0: michael@0: #ifdef __APPLE__ michael@0: # include michael@0: #endif michael@0: michael@0: const _STLP_fd INVALID_STLP_FD = -1; michael@0: michael@0: #ifndef O_ACCMODE michael@0: # define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) michael@0: #endif michael@0: michael@0: // Compare with streamoff definition in stl/char_traits.h! michael@0: #if defined (_STLP_USE_DEFAULT_FILE_OFFSET) || \ michael@0: (!defined(_LARGEFILE_SOURCE) && !defined (_LARGEFILE64_SOURCE)) michael@0: # define FSTAT fstat michael@0: # define STAT stat michael@0: # define LSEEK lseek michael@0: # define MMAP mmap michael@0: # define OPEN open michael@0: #else michael@0: # define FSTAT fstat64 michael@0: # define STAT stat64 michael@0: # define LSEEK lseek64 michael@0: # define MMAP mmap64 michael@0: # define OPEN open64 michael@0: #endif michael@0: michael@0: #ifndef MAP_FAILED /* MMAP failure return code */ michael@0: # define MAP_FAILED -1 michael@0: #endif michael@0: michael@0: _STLP_BEGIN_NAMESPACE michael@0: michael@0: static ios_base::openmode flag_to_openmode(int mode) michael@0: { michael@0: ios_base::openmode ret = ios_base::__default_mode; michael@0: michael@0: switch ( mode & O_ACCMODE ) { michael@0: case O_RDONLY: michael@0: ret = ios_base::in; michael@0: break; michael@0: case O_WRONLY: michael@0: ret = ios_base::out; michael@0: break; michael@0: case O_RDWR: michael@0: ret = ios_base::in | ios_base::out; michael@0: break; michael@0: } michael@0: michael@0: if ( mode & O_APPEND ) michael@0: ret |= ios_base::app; michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: _STLP_MOVE_TO_PRIV_NAMESPACE michael@0: michael@0: // Helper functions for _Filebuf_base. michael@0: michael@0: static bool __is_regular_file(_STLP_fd fd) { michael@0: struct STAT buf; michael@0: return FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode); michael@0: } michael@0: michael@0: // Number of characters in the file. michael@0: static streamoff __file_size(_STLP_fd fd) { michael@0: streamoff ret = 0; michael@0: michael@0: struct STAT buf; michael@0: if (FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode)) michael@0: ret = buf.st_size > 0 ? buf.st_size : 0; michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: _STLP_MOVE_TO_STD_NAMESPACE michael@0: michael@0: size_t _Filebuf_base::_M_page_size = 4096; michael@0: michael@0: _Filebuf_base::_Filebuf_base() michael@0: : _M_file_id(INVALID_STLP_FD), michael@0: _M_openmode(0), michael@0: _M_is_open(false), michael@0: _M_should_close(false) michael@0: {} michael@0: michael@0: void _Filebuf_base::_S_initialize() michael@0: { michael@0: #if defined (__APPLE__) michael@0: int mib[2]; michael@0: size_t pagesize, len; michael@0: mib[0] = CTL_HW; michael@0: mib[1] = HW_PAGESIZE; michael@0: len = sizeof(pagesize); michael@0: sysctl(mib, 2, &pagesize, &len, NULL, 0); michael@0: _M_page_size = pagesize; michael@0: #elif defined (__DJGPP) && defined (_CRAY) michael@0: _M_page_size = BUFSIZ; michael@0: #else michael@0: _M_page_size = sysconf(_SC_PAGESIZE); michael@0: #endif michael@0: } michael@0: michael@0: // Return the size of the file. This is a wrapper for stat. michael@0: // Returns zero if the size cannot be determined or is ill-defined. michael@0: streamoff _Filebuf_base::_M_file_size() michael@0: { michael@0: return _STLP_PRIV __file_size(_M_file_id); michael@0: } michael@0: michael@0: bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode, michael@0: long permission) michael@0: { michael@0: _STLP_fd file_no; michael@0: michael@0: if (_M_is_open) michael@0: return false; michael@0: michael@0: int flags = 0; michael@0: michael@0: // Unix makes no distinction between text and binary files. michael@0: switch ( openmode & (~ios_base::ate & ~ios_base::binary) ) { michael@0: case ios_base::out: michael@0: case ios_base::out | ios_base::trunc: michael@0: flags = O_WRONLY | O_CREAT | O_TRUNC; michael@0: break; michael@0: case ios_base::app: michael@0: case ios_base::out | ios_base::app: michael@0: flags = O_WRONLY | O_CREAT | O_APPEND; michael@0: break; michael@0: case ios_base::in: michael@0: flags = O_RDONLY; michael@0: permission = 0; // Irrelevant unless we're writing. michael@0: break; michael@0: case ios_base::in | ios_base::out: michael@0: flags = O_RDWR; michael@0: break; michael@0: case ios_base::in | ios_base::out | ios_base::trunc: michael@0: flags = O_RDWR | O_CREAT | O_TRUNC; michael@0: break; michael@0: case ios_base::in | ios_base::app: michael@0: case ios_base::in | ios_base::out | ios_base::app: michael@0: flags = O_RDWR | O_CREAT | O_APPEND; michael@0: break; michael@0: default: // The above are the only combinations of michael@0: return false; // flags allowed by the C++ standard. michael@0: } michael@0: michael@0: file_no = OPEN(name, flags, permission); michael@0: michael@0: if (file_no < 0) michael@0: return false; michael@0: michael@0: _M_is_open = true; michael@0: michael@0: if ((openmode & (ios_base::ate | ios_base::app)) && (LSEEK(file_no, 0, SEEK_END) == -1)) { michael@0: _M_is_open = false; michael@0: } michael@0: michael@0: _M_file_id = file_no; michael@0: _M_should_close = _M_is_open; michael@0: _M_openmode = openmode; michael@0: michael@0: if (_M_is_open) michael@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); michael@0: michael@0: return (_M_is_open != 0); michael@0: } michael@0: michael@0: michael@0: bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode) michael@0: { michael@0: // This doesn't really grant everyone in the world read/write michael@0: // access. On Unix, file-creation system calls always clear michael@0: // bits that are set in the umask from the permissions flag. michael@0: return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP | michael@0: S_IWGRP | S_IROTH | S_IWOTH); michael@0: } michael@0: michael@0: // Associated the filebuf with a file descriptor pointing to an already- michael@0: // open file. Mode is set to be consistent with the way that the file michael@0: // was opened. michael@0: bool _Filebuf_base::_M_open(int file_no, ios_base::openmode) michael@0: { michael@0: if (_M_is_open || file_no < 0) michael@0: return false; michael@0: michael@0: int mode = fcntl(file_no, F_GETFL); michael@0: michael@0: if (mode == -1) michael@0: return false; michael@0: michael@0: _M_openmode = flag_to_openmode(mode); michael@0: _M_file_id = file_no; michael@0: michael@0: _M_is_open = true; michael@0: _M_should_close = false; michael@0: _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id); michael@0: return true; michael@0: } michael@0: michael@0: bool _Filebuf_base::_M_close() michael@0: { michael@0: if (!_M_is_open) michael@0: return false; michael@0: michael@0: bool ok = _M_should_close ? (close(_M_file_id) == 0) : true; michael@0: michael@0: _M_is_open = _M_should_close = false; michael@0: _M_openmode = 0; michael@0: return ok; michael@0: } michael@0: michael@0: // Read up to n characters into a buffer. Return value is number of michael@0: // characters read. michael@0: ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n) michael@0: { michael@0: return read(_M_file_id, buf, n); michael@0: } michael@0: michael@0: // Write n characters from a buffer. Return value: true if we managed michael@0: // to write the entire buffer, false if we didn't. michael@0: bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n) michael@0: { michael@0: for (;;) { michael@0: ptrdiff_t written = write(_M_file_id, buf, n); michael@0: michael@0: if (n == written) { michael@0: return true; michael@0: } michael@0: michael@0: if (written > 0 && written < n) { michael@0: n -= written; michael@0: buf += written; michael@0: } else { michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Wrapper for lseek or the like. michael@0: streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir) michael@0: { michael@0: int whence; michael@0: michael@0: switch ( dir ) { michael@0: case ios_base::beg: michael@0: if (offset < 0 /* || offset > _M_file_size() */ ) michael@0: return streamoff(-1); michael@0: whence = SEEK_SET; michael@0: break; michael@0: case ios_base::cur: michael@0: whence = SEEK_CUR; michael@0: break; michael@0: case ios_base::end: michael@0: if (/* offset > 0 || */ -offset > _M_file_size() ) michael@0: return streamoff(-1); michael@0: whence = SEEK_END; michael@0: break; michael@0: default: michael@0: return streamoff(-1); michael@0: } michael@0: michael@0: return LSEEK(_M_file_id, offset, whence); michael@0: } michael@0: michael@0: // Attempts to memory-map len bytes of the current file, starting michael@0: // at position offset. Precondition: offset is a multiple of the michael@0: // page size. Postcondition: return value is a null pointer if the michael@0: // memory mapping failed. Otherwise the return value is a pointer to michael@0: // the memory-mapped file and the file position is set to offset. michael@0: void* _Filebuf_base::_M_mmap(streamoff offset, streamoff len) michael@0: { michael@0: void* base; michael@0: #if !defined (__DJGPP) && !defined (_CRAY) michael@0: base = MMAP(0, len, PROT_READ, MAP_PRIVATE, _M_file_id, offset); michael@0: if (base != (void*)MAP_FAILED) { michael@0: if (LSEEK(_M_file_id, offset + len, SEEK_SET) < 0) { michael@0: this->_M_unmap(base, len); michael@0: base = 0; michael@0: } michael@0: } else michael@0: base =0; michael@0: #else michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&offset) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&len) michael@0: base = 0; michael@0: #endif michael@0: return base; michael@0: } michael@0: michael@0: void _Filebuf_base::_M_unmap(void* base, streamoff len) michael@0: { michael@0: // precondition : there is a valid mapping at the moment michael@0: #if !defined (__DJGPP) && !defined (_CRAY) michael@0: munmap((char*)base, len); michael@0: #else michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(&len) michael@0: _STLP_MARK_PARAMETER_AS_UNUSED(base) michael@0: #endif michael@0: } michael@0: michael@0: _STLP_END_NAMESPACE