1 /*
2  * Copyright (c) 1999
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Copyright (c) 1999
6  * Boris Fomitchev
7  *
8  * This material is provided "as is", with absolutely no warranty expressed
9  * or implied. Any use is at your own risk.
10  *
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.
16  *
17  */
18 
19 
20 #include "stlport_prefix.h"
21 
22 #if defined  (__SUNPPRO_CC)  && !defined (_STLP_NO_NEW_C_HEADERS)
23 #  include <time.h>
24 // For sunpro, it chokes if time.h is included through stat.h
25 #endif
26 
27 #include <fstream>
28 
29 #ifdef __CYGWIN__
30 #  define __int64 long long
31 #endif
32 
33 #if defined (_STLP_USE_UNIX_IO)
34 extern "C" {
35 // open/close/read/write
36 #  include <sys/stat.h>           // For stat
37 #  if !defined (_CRAY) && ! defined (__EMX__)
38 #    include <sys/mman.h>           // For mmap
39 #  endif
40 
41 //  on HP-UX 11, this one contradicts with pthread.h on pthread_atfork, unless we unset this
42 #  if defined (__hpux) && defined (__GNUC__)
43 #    undef _INCLUDE_POSIX1C_SOURCE
44 #  endif
45 
46 #  include <unistd.h>
47 #  include <fcntl.h>
48 }
49 #  ifdef __APPLE__
50 #    include <sys/sysctl.h>
51 #  endif
52 #elif defined (_STLP_USE_WIN32_IO)
53 #  define WIN32_LEAN_AND_MEAN
54 #  include <windows.h>
55 
56 #  ifdef __BORLANDC__
57 #      include <cfcntl.h>            // For _O_RDONLY, etc
58 #    include <sys/stat.h>         // For _fstat
59 #  elif !defined(_STLP_WCE)
60 #    include <io.h>               // For _get_osfhandle
61 #    include <fcntl.h>            // For _O_RDONLY, etc
62 #    include <sys/stat.h>         // For _fstat
63 #  endif
64 #  define _TEXTBUF_SIZE 0x1000
65 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
66 #  if defined( __MSL__ )
67 #    include <unistd.h>
68 #  else
69 #    include <io.h>
70 #  endif
71 #  include <fcntl.h>
72 #  include <sys/stat.h>
73 #elif defined (_STLP_USE_STDIO_IO)
74 #  include <cstdio>
75 #  if !(defined(__MRC__) || defined(__SC__) || defined(__ISCPP__) )
76 extern "C" {
77 #    include <sys/stat.h>
78 }
79 #  endif
80 #  if defined( __MSL__ )
81 #    include <unix.h>
82 #  endif
83 #  if defined(__ISCPP__)
84 #    include <c_locale_is/filestat.h>
85 #  endif
86 #  if (defined(__BEOS__) && defined(__INTEL__)) || defined(__PIPS__)
87 #    include <fcntl.h>
88 #    include <sys/stat.h>         // For _fstat
89 #    define _S_IREAD S_IREAD
90 #    define _S_IWRITE S_IWRITE
91 #    define _S_IFREG S_IFREG
92 #  endif
93 #elif defined(_STLP_USE_SYMBIAN_IO)
94 #  include <f32file.h>
95 #else
96 #  error "Configure i/o !"
97 #endif
98 
99 #if defined (_STLP_USE_WIN32_IO)
100 const _STLP_fd INVALID_STLP_FD = INVALID_HANDLE_VALUE;
101 #  if !defined (INVALID_SET_FILE_POINTER)
102 #    define INVALID_SET_FILE_POINTER 0xffffffff
103 #  endif
104 #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO) || defined (_STLP_USE_UNIX_IO)
105 const _STLP_fd INVALID_STLP_FD = -1;
106 #elif defined (_STLP_USE_SYMBIAN_IO)
107 const _STLP_fd INVALID_STLP_FD = RFile();
108 #else
109 #  error "Configure i/o !"
110 #endif
111 
112 // map permission masks
113 #if defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
114 #  ifndef S_IRUSR
115 #    define S_IRUSR _S_IREAD
116 #    define S_IWUSR _S_IWRITE
117 #    define S_IRGRP _S_IREAD
118 #    define S_IWGRP _S_IWRITE
119 #    define S_IROTH _S_IREAD
120 #    define S_IWOTH _S_IWRITE
121 #  endif
122 #  ifndef O_RDONLY
123 #    define O_RDONLY _O_RDONLY
124 #    define O_WRONLY _O_WRONLY
125 #    define O_RDWR   _O_RDWR
126 #    define O_APPEND _O_APPEND
127 #    define O_CREAT  _O_CREAT
128 #    define O_TRUNC  _O_TRUNC
129 #    define O_TEXT   _O_TEXT
130 #    define O_BINARY _O_BINARY
131 #  endif
132 
133 #  ifdef __MSL__
134 #    define _O_TEXT 0x0
135 #    if !defined( O_TEXT )
136 #      define O_TEXT _O_TEXT
137 #    endif
138 #    define _S_IFREG S_IFREG
139 #    define S_IREAD        S_IRUSR
140 #    define S_IWRITE       S_IWUSR
141 #    define S_IEXEC        S_IXUSR
142 #    define _S_IWRITE S_IWRITE
143 #    define _S_IREAD S_IREAD
144 #    define _open open
145 #    define _lseek lseek
146 #    define _close close
147 #    define _read read
148 #    define _write write
149 #  endif
150 #endif
151 
152 #ifndef O_ACCMODE
153 #  define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
154 #endif
155 
156 #include "fstream_impl.h"
157 
158 #ifdef _STLP_LONG_LONG
159 #  define ULL(x) ((unsigned _STLP_LONG_LONG)x)
160 #elif defined(__MRC__) || defined(__SC__)    //*TY 02/25/2000 - added support for MPW compilers
161 #  include <Math64.h>
162 #  define ULL(x) (U64SetU(x))
163 #elif defined(__ISCPP__)
164 #  include "uint64.h"
165 #else
166 #  error "there should be some long long type on the system!"
167 #endif
168 
169 // Symbian file server handle
170 #if defined (_STLP_USE_SYMBIAN_IO)
171 static RFs __sym_fs;
172 static int __sym_fs_count = 0;
173 #endif
174 
175 _STLP_BEGIN_NAMESPACE
176 
177 #ifdef _STLP_USE_DEFAULT_FILE_OFFSET
178 #  define FOPEN fopen
179 #  define FSEEK fseek
180 #  define FSTAT fstat
181 #  define STAT  stat
182 #  define FTELL ftell
183 #  define LSEEK lseek
184 #  define MMAP  mmap
185 #  define OPEN  open
186 #elif defined(_LARGEFILE_SOURCE) || defined(_LARGEFILE64_SOURCE) /* || defined(__USE_FILE_OFFSET64) */ \
187       /* || (defined(_FILE_OFFSET_BITS) && (_FILE_OFFSET_BITS == 64)) */ /* || defined(__sgi) */
188 #  define FOPEN fopen64
189 #  define FSEEK fseeko64
190 #  define FSTAT fstat64
191 #  define STAT  stat64
192 #  define FTELL ftello64
193 #  define LSEEK lseek64
194 #  define MMAP  mmap64
195 #  define OPEN  open64
196 #else
197 #  define OPEN  open
198 #  define FSEEK fseek
199 #  define FSTAT fstat
200 #  define STAT  stat
201 #  define FTELL ftell
202 #  define LSEEK lseek
203 #  define MMAP  mmap
204 #  define OPEN  open
205 #endif
206 #ifdef _STLP_USE_UNIX_IO
207 #  ifndef MAP_FAILED /* MMAP failure return code */
208 #    define MAP_FAILED -1
209 #  endif
210 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
211 #  define LSEEK _lseek
212 #endif
213 
214 #if !defined(__MSL__) && !defined(__MRC__) && !defined(__SC__) && !defined(_STLP_WCE) && !defined(_STLP_USE_SYMBIAN_IO)    //*TY 04/15/2000 - exclude mpw compilers also
flag_to_openmode(int mode)215 static ios_base::openmode flag_to_openmode(int mode) {
216   ios_base::openmode ret = ios_base::__default_mode;
217 
218   switch(mode & O_ACCMODE) {
219   case O_RDONLY:
220     ret = ios_base::in; break;
221   case O_WRONLY:
222     ret = ios_base::out; break;
223   case O_RDWR:
224     ret = ios_base::in | ios_base::out; break;
225   }
226 
227   if (mode & O_APPEND)
228     ret |= ios_base::app;
229 
230 #  if defined (_STLP_USE_WIN32_IO)
231   if (mode & O_BINARY)
232     ret |= ios_base::binary;
233 #  endif
234 
235   return ret;
236 }
237 #endif /* MSL */
238 
239 _STLP_MOVE_TO_PRIV_NAMESPACE
240 
241 // Helper functions for _Filebuf_base.
242 
__is_regular_file(_STLP_fd fd)243 bool __is_regular_file(_STLP_fd fd) {
244 
245 #if defined (_STLP_UNIX)
246 
247   struct STAT buf;
248   return FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode);
249 
250 #elif defined(__MRC__) || defined(__SC__)    //*TY 02/25/2000 - added support for MPW compilers
251 
252 #  pragma unused(fd)
253   return true;  // each file is a regular file under mac os, isn't it? (we don't have fstat())
254 
255 #elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
256 
257   struct STAT buf;
258   return FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0 ;
259 
260 #elif defined (_STLP_USE_WIN32_IO) && !defined(_STLP_WCE)
261 
262   return (GetFileType(fd) & ~FILE_TYPE_REMOTE) == FILE_TYPE_DISK;
263 
264 #elif defined (_STLP_USE_SYMBIAN_IO)
265 
266   TUint att;
267   if (fd.Att(att) != KErrNone)
268     return false;
269   return (att & (KEntryAttDir | KEntryAttVolume)) == 0;
270 
271 #else
272   (void)fd;    // dwa 4/27/00 - suppress unused parameter warning
273   return false;
274 #endif
275 }
276 
277 // Number of characters in the file.
__file_size(_STLP_fd fd)278 streamoff __file_size(_STLP_fd fd) {
279   streamoff ret = 0;
280 
281 #if defined (_STLP_UNIX)
282 
283   struct STAT buf;
284   if (FSTAT(fd, &buf) == 0 && S_ISREG(buf.st_mode))
285     ret = buf.st_size > 0 ? buf.st_size : 0;
286 
287 #elif defined(__MRC__) || defined(__SC__)    //*TY 02/25/2000 - added support for MPW compilers
288 
289 #  pragma unused(fd)
290 
291 #elif defined(_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
292 
293   struct STAT buf;
294   if (FSTAT(fd, &buf) == 0 && (buf.st_mode & _S_IFREG) != 0)
295     ret = buf.st_size > 0 ? buf.st_size : 0;
296 
297 #elif defined (_STLP_USE_WIN32_IO)
298 
299  LARGE_INTEGER li;
300  li.LowPart = GetFileSize(fd, (unsigned long*) &li.HighPart);
301  if (li.LowPart != INVALID_FILE_SIZE || GetLastError() == NO_ERROR)
302    ret = li.QuadPart;
303 
304 #elif defined (_STLP_USE_SYMBIAN_IO)
305 
306   TInt sz;
307   if (fd.Size(sz) != KErrNone)
308     return 0;
309   return sz;
310 
311 #else
312   (void)fd;    // dwa 4/27/00 - suppress unused parameter warning
313 #endif
314   return ret;
315 }
316 
317 _STLP_MOVE_TO_STD_NAMESPACE
318 
319 // Visual C++ and Intel use this, but not Metrowerks
320 // Also MinGW, msvcrt.dll (but not crtdll.dll) dependent version
321 #if (!defined (__MSL__) && !defined (_STLP_WCE) && defined (_STLP_MSVC_LIB) && defined (_WIN32)) || \
322     (defined (__MINGW32__) && defined (__MSVCRT__))
323 
324 // fcntl(fileno, F_GETFL) for Microsoft library
325 // 'semi-documented' defines:
326 #  define IOINFO_L2E          5
327 #  define IOINFO_ARRAY_ELTS   (1 << IOINFO_L2E)
328 #  define _pioinfo(i) ( __pioinfo[(i) >> IOINFO_L2E] + \
329               ((i) & (IOINFO_ARRAY_ELTS - 1)) )
330 #  define FAPPEND         0x20    // O_APPEND flag
331 #  define FTEXT           0x80    // O_TEXT flag
332 // end of 'semi-documented' defines
333 
334 // 'semi-documented' internal structure
335 extern "C" {
336   struct ioinfo {
337     long osfhnd;    // the real os HANDLE
338     char osfile;    // file handle flags
339     char pipech;    // pipe buffer
340 #  if defined (_MT)
341     // multi-threaded locking
342     int lockinitflag;
343     CRITICAL_SECTION lock;
344 #  endif  /* _MT */
345   };
346 #  if defined (__MINGW32__)
347  __MINGW_IMPORT ioinfo * __pioinfo[];
348 #  else
349   extern _CRTIMP ioinfo * __pioinfo[];
350 #  endif
351 } // extern "C"
352 // end of 'semi-documented' declarations
353 
354 static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) {
355   char dosflags = 0;
356   if (fd >= 0)
357     dosflags = _pioinfo(fd)->osfile;
358   //else
359     //the file will be considered as open in binary mode with no append attribute
360   // end of 'semi-documented' stuff
361 
362   int mode = 0;
363   if (dosflags & FAPPEND)
364     mode |= O_APPEND;
365 
366   if (dosflags & FTEXT)
367     mode |= O_TEXT;
368   else
369     mode |= O_BINARY;
370 
371   // For Read/Write access we have to guess
372   DWORD dummy, dummy2;
373   BOOL writeOk = WriteFile(oshandle, &dummy2, 0, &dummy, 0);
374   BOOL readOk = ReadFile(oshandle, &dummy2, 0, &dummy, NULL);
375   if (writeOk && readOk)
376     mode |= O_RDWR;
377   else if (readOk)
378     mode |= O_RDONLY;
379   else
380     mode |= O_WRONLY;
381 
382   return flag_to_openmode(mode);
383 }
384 
385 #elif defined (__DMC__)
386 
387 #  define FHND_APPEND 0x04
388 #  define FHND_DEVICE 0x08
389 #  define FHND_TEXT   0x10
390 
391 extern "C" unsigned char __fhnd_info[_NFILE];
392 
393 static ios_base::openmode _get_osfflags(int fd, HANDLE oshandle) {
394   int mode = 0;
395 
396   if (__fhnd_info[fd] & FHND_APPEND)
397     mode |= O_APPEND;
398 
399   if (__fhnd_info[fd] & FHND_TEXT == 0)
400     mode |= O_BINARY;
401 
402   for (FILE *fp = &_iob[0]; fp < &_iob[_NFILE]; fp++) {
403     if ((fileno(fp) == fd) && (fp->_flag & (_IOREAD | _IOWRT | _IORW))) {
404       const int osflags = fp->_flag;
405 
406       if ((osflags & _IOREAD) && !(osflags & _IOWRT) && !(osflags & _IORW))
407         mode |= O_RDONLY;
408       else if ((osflags & _IOWRT) && !(osflags & _IOREAD) && !(osflags & _IORW))
409         mode |= O_WRONLY;
410       else
411         mode |= O_RDWR;
412       break;
413     }
414   }
415 
416   return flag_to_openmode(mode);
417 }
418 #endif
419 
420 size_t _Filebuf_base::_M_page_size = 4096;
421 
_Filebuf_base()422 _Filebuf_base::_Filebuf_base()
423   : _M_file_id(INVALID_STLP_FD),
424 #if defined (_STLP_USE_WIN32_IO)
425     _M_view_id(0),
426 #endif
427     _M_openmode(0),
428     _M_is_open(false),
429     _M_should_close(false)
430 {
431 #if defined (_STLP_USE_SYMBIAN_IO)
432 	if (User::LockedInc(__sym_fs_count) == 0) {
433 		if (__sym_fs.Connect() != KErrNone)
434 			return;
435 
436 		TInt drive = EDriveC;
437 
438 		TParse parser;
439 		if (parser.SetNoWild(RProcess().FileName(), 0, 0) == KErrNone) {
440 			if (parser.Drive().Length() > 0)
441 				RFs::CharToDrive(parser.Drive()[0], drive);
442 
443 			if (__sym_fs.CreatePrivatePath(drive) != KErrNone) {
444 				drive = EDriveC;
445 				__sym_fs.CreatePrivatePath(drive);
446 			}
447 
448 			__sym_fs.SetSessionToPrivate(drive);
449 		}
450 	}
451 #endif
452 }
453 
~_Filebuf_base()454 _Filebuf_base::~_Filebuf_base()
455 {
456 #if defined (_STLP_USE_SYMBIAN_IO)
457 	if (User::LockedDec(__sym_fs_count) == 1)
458 		__sym_fs.Close();
459 #endif
460 }
461 
_S_initialize()462 void _Filebuf_base::_S_initialize() {
463 #if defined (_STLP_UNIX)  && !defined (__DJGPP) && !defined (_CRAY)
464 #  if defined (__APPLE__)
465   int mib[2];
466   size_t pagesize, len;
467   mib[0] = CTL_HW;
468   mib[1] = HW_PAGESIZE;
469   len = sizeof(pagesize);
470   sysctl(mib, 2, &pagesize, &len, NULL, 0);
471   _M_page_size = pagesize;
472 #  elif defined (__DJGPP) && defined (_CRAY)
473   _M_page_size = BUFSIZ;
474 #  else
475   _M_page_size = sysconf(_SC_PAGESIZE);
476 #  endif
477 #elif defined (_STLP_USE_WIN32_IO)
478   SYSTEM_INFO SystemInfo;
479   GetSystemInfo(&SystemInfo);
480   _M_page_size = SystemInfo.dwPageSize;
481   // might be .dwAllocationGranularity
482 #endif
483 }
484 
485 // Return the size of the file.  This is a wrapper for stat.
486 // Returns zero if the size cannot be determined or is ill-defined.
_M_file_size()487 streamoff _Filebuf_base::_M_file_size() {
488   return _STLP_PRIV __file_size(_M_file_id);
489 }
490 
_M_open(const char * name,ios_base::openmode openmode,long permission)491 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode,
492                             long permission) {
493   _STLP_fd file_no;
494 
495   if (_M_is_open)
496     return false;
497 
498 #if defined (_STLP_USE_SYMBIAN_IO)
499 
500   TBuf<256> sname;
501   sname.Copy(TPtrC8((unsigned char*)name));
502 
503   TInt filemode = EFileStream | EFileShareAny;
504 
505   if (openmode & ios_base::in)
506 	filemode |= EFileRead;
507   if (openmode & ios_base::out)
508     filemode |= EFileWrite;
509 
510   if ((openmode & ios_base::trunc) || !(openmode & ios_base::in)) {
511 	if (file_no.Replace(__sym_fs, sname, filemode) != KErrNone)
512 		return false;
513   }
514   else
515 	if (file_no.Open(__sym_fs, sname, filemode) != KErrNone)
516 	  return false;
517 
518   if (openmode & ios_base::ate ) {
519     TInt ofs = 0;
520     if (file_no.Seek(ESeekEnd, ofs) != KErrNone) {
521 	  file_no.Close();
522 	  return false;
523 	}
524   }
525 
526   _M_is_open = true;
527 
528 #elif defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO)
529   int flags = 0;
530 
531   // Unix makes no distinction between text and binary files.
532   switch(openmode & (~ios_base::ate & ~ios_base::binary)) {
533   case ios_base::out:
534   case ios_base::out | ios_base::trunc:
535     flags = O_WRONLY | O_CREAT | O_TRUNC;
536     break;
537   case ios_base::out | ios_base::app:
538     flags = O_WRONLY | O_CREAT | O_APPEND;
539     break;
540   case ios_base::in:
541     flags = O_RDONLY;
542     permission = 0;             // Irrelevant unless we're writing.
543     break;
544   case ios_base::in | ios_base::out:
545     flags = O_RDWR;
546     break;
547   case ios_base::in | ios_base::out | ios_base::trunc:
548     flags = O_RDWR | O_CREAT | O_TRUNC;
549     break;
550   default:                      // The above are the only combinations of
551     return false;               // flags allowed by the C++ standard.
552   }
553 
554 #  if defined (_STLP_USE_UNIX_EMULATION_IO)
555   if (openmode & ios_base::binary)
556     flags |= O_BINARY;
557   else
558     flags |= O_TEXT;
559 
560   file_no = _open(name, flags, permission);
561 #  else
562   file_no = OPEN(name, flags, permission);
563 #  endif /* _STLP_USE_UNIX_EMULATION_IO */
564 
565   if (file_no < 0)
566     return false;
567 
568   _M_is_open = true;
569 
570   if (openmode & ios_base::ate)
571     if (LSEEK(file_no, 0, SEEK_END) == -1)
572       _M_is_open = false;
573 
574 #elif defined (_STLP_USE_STDIO_IO)
575   // use FILE-based i/o
576   const char* flags;
577 
578   switch(openmode & (~ios_base::ate)) {
579   case ios_base::out:
580   case ios_base::out | ios_base::trunc:
581     flags = "w";
582     break;
583 
584   case ios_base::out | ios_base::binary:
585   case ios_base::out | ios_base::trunc | ios_base::binary:
586     flags = "wb";
587     break;
588 
589   case ios_base::out | ios_base::app:
590     flags = "a";
591     break;
592 
593   case ios_base::out | ios_base::app | ios_base::binary:
594     flags = "ab";
595     break;
596 
597   case ios_base::in:
598     flags = "r";
599     break;
600 
601   case ios_base::in | ios_base::binary:
602     flags = "rb";
603     break;
604 
605   case ios_base::in | ios_base::out:
606     flags = "r+";
607     break;
608 
609   case ios_base::in | ios_base::out | ios_base::binary:
610     flags = "r+b";
611     break;
612 
613 
614   case ios_base::in | ios_base::out | ios_base::trunc:
615     flags = "w+";
616     break;
617 
618   case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
619     flags = "w+b";
620     break;
621 
622   default:                      // The above are the only combinations of
623     return false;               // flags allowed by the C++ standard.
624   }
625 
626   // fbp : TODO : set permissions !
627   (void)permission; // currently unused    //*TY 02/26/2000 - added to suppress warning message
628   _M_file = FOPEN(name, flags);
629 
630   if (_M_file) {
631     file_no = fileno(_M_file);
632   }
633   else
634     return false;
635 
636   // unset buffering immediately
637   setbuf(_M_file, 0);
638 
639   _M_is_open = true;
640 
641   if (openmode & ios_base::ate) {
642     if (FSEEK(_M_file, 0, SEEK_END) == -1)
643       _M_is_open = false;
644   }
645 
646 #elif defined (_STLP_USE_WIN32_IO)
647   DWORD dwDesiredAccess, dwCreationDisposition;
648   bool doTruncate = false;
649 
650   switch (openmode & (~ios_base::ate & ~ios_base::binary)) {
651   case ios_base::out:
652   case ios_base::out | ios_base::trunc:
653     dwDesiredAccess = GENERIC_WRITE;
654     dwCreationDisposition = OPEN_ALWAYS;
655     // boris : even though it is very non-intuitive, standard
656     // requires them both to behave same.
657     doTruncate = true;
658     break;
659   case ios_base::out | ios_base::app:
660     dwDesiredAccess = GENERIC_WRITE;
661     dwCreationDisposition = OPEN_ALWAYS;
662     break;
663   case ios_base::in:
664     dwDesiredAccess = GENERIC_READ;
665     dwCreationDisposition = OPEN_EXISTING;
666     permission = 0;             // Irrelevant unless we're writing.
667     break;
668   case ios_base::in | ios_base::out:
669     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
670     dwCreationDisposition = OPEN_EXISTING;
671     break;
672   case ios_base::in | ios_base::out | ios_base::trunc:
673     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
674     dwCreationDisposition = OPEN_ALWAYS;
675     doTruncate = true;
676     break;
677   default:                      // The above are the only combinations of
678     return false;               // flags allowed by the C++ standard.
679   }
680 
681   DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
682 
683 #  if defined(_STLP_USE_WIDE_INTERFACE)
684     file_no = CreateFile (_STLP_PRIV __ASCIIToWide(name).c_str(),
685 #  else
686     file_no = CreateFileA(name,
687 #  endif
688                           dwDesiredAccess, dwShareMode, 0,
689                           dwCreationDisposition, permission, 0);
690 
691   if (file_no == INVALID_STLP_FD)
692     return false;
693 
694   if ((doTruncate && SetEndOfFile(file_no) == 0) ||
695       (((openmode & ios_base::ate) != 0) &&
696        (SetFilePointer(file_no, 0, NULL, FILE_END) == INVALID_SET_FILE_POINTER))) {
697     CloseHandle(file_no);
698     return false;
699   }
700 
701   _M_is_open = true;
702 
703 #else
704 #  error "Port!"
705 #endif /* __unix */
706 
707   _M_file_id = file_no;
708   _M_should_close = _M_is_open;
709   _M_openmode = openmode;
710 
711   if (_M_is_open)
712     _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
713 
714   return (_M_is_open != 0);
715 }
716 
717 
_M_open(const char * name,ios_base::openmode openmode)718 bool _Filebuf_base::_M_open(const char* name, ios_base::openmode openmode) {
719   // This doesn't really grant everyone in the world read/write
720   // access.  On Unix, file-creation system calls always clear
721   // bits that are set in the umask from the permissions flag.
722 #ifdef _STLP_USE_WIN32_IO
723   return this->_M_open(name, openmode, FILE_ATTRIBUTE_NORMAL);
724 #elif defined(__MRC__) || defined(__SC__)    //*TY 02/26/2000 - added support for MPW compilers
725   return this->_M_open(name, openmode, 0);
726 #else
727   return this->_M_open(name, openmode, S_IRUSR | S_IWUSR | S_IRGRP |
728                                        S_IWGRP | S_IROTH | S_IWOTH);
729 #endif
730 }
731 
732 
733 #if defined (_STLP_USE_WIN32_IO)
_M_open(_STLP_fd __id,ios_base::openmode init_mode)734 bool _Filebuf_base::_M_open(_STLP_fd __id, ios_base::openmode init_mode) {
735 #  if (defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE)) || \
736       (defined (__MINGW32__) && defined (__MSVCRT__)) || defined (__DMC__)
737 
738   if (_M_is_open || __id == INVALID_STLP_FD)
739     return false;
740 
741   if (init_mode != ios_base::__default_mode)
742     _M_openmode = init_mode;
743   else
744     _M_openmode = _get_osfflags(-1, __id);
745 
746   _M_is_open = true;
747   _M_file_id = __id;
748   _M_should_close = false;
749   _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
750 
751   return true;
752 #  else
753   (void)__id;
754   (void)init_mode;    // dwa 4/27/00 - suppress unused parameter warning
755 
756   // not available for the API
757   return false;
758 
759 #  endif
760 }
761 #endif /* _STLP_USE_WIN32_IO */
762 
763 // Associated the filebuf with a file descriptor pointing to an already-
764 // open file.  Mode is set to be consistent with the way that the file
765 // was opened.
_M_open(int file_no,ios_base::openmode init_mode)766 bool _Filebuf_base::_M_open(int file_no, ios_base::openmode init_mode) {
767   if (_M_is_open || file_no < 0)
768     return false;
769 
770 #if defined (_STLP_UNIX)
771   (void)init_mode;    // dwa 4/27/00 - suppress unused parameter warning
772   int mode ;
773   mode = fcntl(file_no, F_GETFL);
774 
775   if (mode == -1)
776     return false;
777 
778   _M_openmode = flag_to_openmode(mode);
779   _M_file_id = file_no;
780 #elif defined(__MRC__) || defined(__SC__)    //*TY 02/26/2000 - added support for MPW compilers
781   (void)init_mode;    // dwa 4/27/00 - suppress unused parameter warning
782   switch (_iob[file_no]._flag & (_IOREAD|_IOWRT|_IORW) )
783   {
784   case _IOREAD:
785     _M_openmode = ios_base::in; break;
786   case _IOWRT:
787     _M_openmode = ios_base::out; break;
788   case _IORW:
789     _M_openmode = ios_base::in | ios_base::out; break;
790   default:
791     return false;
792   }
793   _M_file_id = file_no;
794 #elif defined (_STLP_USE_UNIX_EMULATION_IO) || defined (_STLP_USE_STDIO_IO)
795   (void)init_mode;    // dwa 4/27/00 - suppress unused parameter warning
796   int mode ;
797   struct STAT buf;
798   if (FSTAT(file_no, &buf) != 0)
799     return false;
800   mode = buf.st_mode;
801 
802   switch(mode & (_S_IWRITE | _S_IREAD) ) {
803   case _S_IREAD:
804     _M_openmode = ios_base::in; break;
805   case _S_IWRITE:
806     _M_openmode = ios_base::out; break;
807   case (_S_IWRITE | _S_IREAD):
808     _M_openmode = ios_base::in | ios_base::out; break;
809   default:
810     return false;
811   }
812   _M_file_id = file_no;
813 #elif (defined (_STLP_USE_WIN32_IO) && defined (_STLP_MSVC_LIB) && !defined (_STLP_WCE) ) || \
814       (defined (__MINGW32__) && defined (__MSVCRT__)) || \
815        defined (__DMC__)
816 
817   HANDLE oshandle = (HANDLE)_get_osfhandle(file_no);
818   if (oshandle == INVALID_STLP_FD)
819     return false;
820 
821   if (init_mode != ios_base::__default_mode)
822     _M_openmode = init_mode;
823   else
824     _M_openmode = _get_osfflags(file_no, oshandle);
825 
826   _M_file_id = oshandle;
827 #else
828   (void)init_mode;    // dwa 4/27/00 - suppress unused parameter warning
829   // not available for the API
830 #  define _STLP_NO_IMPLEMENTATION
831 #endif
832 
833 #if !defined (_STLP_NO_IMPLEMENTATION)
834   _M_is_open = true;
835   _M_should_close = false;
836   _M_regular_file = _STLP_PRIV __is_regular_file(_M_file_id);
837   return true;
838 #else
839 #  undef _STLP_NO_IMPLEMENTATION
840   return false;
841 #endif
842 }
843 
_M_close()844 bool _Filebuf_base::_M_close() {
845   if (!_M_is_open)
846     return false;
847 
848   bool ok;
849 
850   if (!_M_should_close)
851     ok = true;
852   else {
853 
854 #if defined (_STLP_USE_UNIX_IO)
855 
856     ok = (close(_M_file_id) == 0);
857 
858 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
859 
860     ok = (_close(_M_file_id) == 0);
861 
862 #elif defined (_STLP_USE_STDIO_IO)
863 
864     ok = (fclose(_M_file) == 0);
865 
866 #elif defined (_STLP_USE_WIN32_IO)
867 
868     if (_M_file_id != INVALID_STLP_FD) {
869       ok = (CloseHandle(_M_file_id) != 0);
870     }
871     else {
872       ok = false;
873     }
874 
875 #elif defined (_STLP_USE_SYMBIAN_IO)
876 
877     ok = _M_file_id.Flush() == KErrNone;
878 	_M_file_id.Close();
879 
880 #else
881 
882     ok = false;
883 
884 #endif /* _STLP_USE_UNIX_IO */
885   }
886 
887   _M_is_open = _M_should_close = false;
888   _M_openmode = 0;
889   return ok;
890 }
891 
892 
893 #define _STLP_LF 10
894 #define _STLP_CR 13
895 #define _STLP_CTRLZ 26
896 
897 // Read up to n characters into a buffer.  Return value is number of
898 // characters read.
_M_read(char * buf,ptrdiff_t n)899 ptrdiff_t _Filebuf_base::_M_read(char* buf, ptrdiff_t n) {
900 #if defined (_STLP_USE_UNIX_IO)
901 
902   return read(_M_file_id, buf, n);
903 
904 #elif defined (_STLP_USE_SYMBIAN_IO)
905 
906   TPtr8 pbuf((unsigned char*)buf, 0, n);
907   if (_M_file_id.Read(pbuf, n) != KErrNone)
908     return -1;
909   return pbuf.Length();
910 
911 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
912 
913   return _read(_M_file_id, buf, n);
914 
915 #elif defined (_STLP_USE_WIN32_IO)
916   ptrdiff_t readen = 0;
917   //Here cast to size_t is safe as n cannot be negative.
918   size_t chunkSize = (min)(size_t(0xffffffff), __STATIC_CAST(size_t, n));
919   // The following, while validating that we are still able to extract chunkSize
920   // charaters to the buffer, avoids extraction of too small chunk of datas
921   // which would be counter performant.
922   while (__STATIC_CAST(size_t, (n - readen)) >= chunkSize) {
923     DWORD numberOfBytesRead;
924     ReadFile(_M_file_id, buf + readen, __STATIC_CAST(DWORD, chunkSize), &numberOfBytesRead, 0);
925 
926     if (numberOfBytesRead == 0)
927       break;
928 
929     if (!(_M_openmode & ios_base::binary)) {
930       // translate CR-LFs to LFs in the buffer
931       char *to = buf + readen;
932       char *from = to;
933       char *last = from + numberOfBytesRead - 1;
934       for (; from <= last && *from != _STLP_CTRLZ; ++from) {
935         if (*from != _STLP_CR)
936           *to++ = *from;
937         else { // found CR
938           if (from < last) { // not at buffer end
939             if (*(from + 1) != _STLP_LF)
940               *to++ = _STLP_CR;
941           }
942           else { // last char is CR, peek for LF
943             char peek = ' ';
944             DWORD NumberOfBytesPeeked;
945             ReadFile(_M_file_id, (LPVOID)&peek, 1, &NumberOfBytesPeeked, 0);
946             if (NumberOfBytesPeeked != 0) {
947               if (peek != _STLP_LF) { //not a <CR><LF> combination
948                 *to++ = _STLP_CR;
949                 if ((to < buf + n) && (peek != _STLP_CR))
950                   //We have enough place to store peek and it is no a special
951                   //_STLP_CR character, we can store it.
952                   *to++ = peek;
953                 else
954                   SetFilePointer(_M_file_id, (LONG)-1, 0, SEEK_CUR);
955               }
956               else {
957                 // A <CR><LF> combination, we keep the <LF>:
958                 *to++ = _STLP_LF;
959               }
960             }
961             else {
962               /* This case is tedious, we could
963                *  - put peek back in the file but this would then generate an infinite loop
964                *  - report an error as we don't know if in a future call to ReadFile we won't then
965                *    get a <LF>. Doing so would make all files with a <CR> last an invalid file
966                *    for STLport, a hard solution for STLport clients.
967                *  - store the <CR> in the returned buffer, the chosen solution, even if in this
968                *    case we could miss a <CR><LF> combination.
969                */
970               *to++ = _STLP_CR;
971             }
972           }
973         } // found CR
974       } // for
975       readen = to - buf;
976       // seek back to TEXT end of file if hit CTRL-Z
977       if (from <= last) { // terminated due to CTRLZ
978         SetFilePointer(_M_file_id, -(LONG)((last + 1) - from), 0, SEEK_CUR);
979         break;
980       }
981     }
982     else
983       readen += numberOfBytesRead;
984   }
985   return readen;
986 
987 #elif defined (_STLP_USE_STDIO_IO)
988 
989   return fread(buf, 1, n, _M_file);
990 
991 #else
992 #  error "Port!"
993 #endif /* __unix */
994 }
995 
996 // Write n characters from a buffer.  Return value: true if we managed
997 // to write the entire buffer, false if we didn't.
_M_write(char * buf,ptrdiff_t n)998 bool _Filebuf_base::_M_write(char* buf, ptrdiff_t n) {
999   for (;;) {
1000     ptrdiff_t written;
1001 
1002 #if defined (_STLP_USE_UNIX_IO)
1003 
1004     written = write(_M_file_id, buf, n);
1005 
1006 #elif defined (_STLP_USE_UNIX_EMULATION_IO)
1007 
1008     written = _write(_M_file_id, buf, n);
1009 
1010 #elif defined (_STLP_USE_WIN32_IO)
1011 
1012     //In the following implementation we are going to cast most of the ptrdiff_t
1013     //values in size_t to work with coherent unsigned values. Doing so make code
1014     //more simple especially in the min function call.
1015 
1016     // In append mode, every write does an implicit seek to the end
1017     // of the file.
1018     if (_M_openmode & ios_base::app)
1019       _M_seek(0, ios_base::end);
1020 
1021     if (_M_openmode & ios_base::binary) {
1022       // binary mode
1023       size_t bytes_to_write = (size_t)n;
1024       DWORD NumberOfBytesWritten;
1025       written = 0;
1026       for (; bytes_to_write != 0;) {
1027         WriteFile(_M_file_id, buf + written,
1028                   __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), bytes_to_write)),
1029                   &NumberOfBytesWritten, 0);
1030         if (NumberOfBytesWritten == 0)
1031           return false;
1032         bytes_to_write -= NumberOfBytesWritten;
1033         written += NumberOfBytesWritten;
1034       }
1035     }
1036     else {
1037       char textbuf[_TEXTBUF_SIZE + 1]; // extra 1 in case LF at end
1038       char * nextblock = buf, * ptrtextbuf = textbuf;
1039       char * endtextbuf = textbuf + _TEXTBUF_SIZE;
1040       char * endblock = buf + n;
1041       ptrdiff_t nextblocksize = (min) (n, (ptrdiff_t)_TEXTBUF_SIZE);
1042       char * nextlf;
1043 
1044       while ( (nextblocksize > 0) &&
1045               (nextlf = (char *)memchr(nextblock, _STLP_LF, nextblocksize)) != 0) {
1046         ptrdiff_t linelength = nextlf - nextblock;
1047         memcpy(ptrtextbuf, nextblock, linelength);
1048         ptrtextbuf += linelength;
1049         nextblock += (linelength + 1);
1050         * ptrtextbuf ++ = _STLP_CR;
1051         * ptrtextbuf ++ = _STLP_LF;
1052         nextblocksize = (min) (ptrdiff_t(endblock - nextblock),
1053                                (max) (ptrdiff_t(0), ptrdiff_t(endtextbuf - ptrtextbuf)));
1054       }
1055       // write out what's left, > condition is here since for LF at the end ,
1056       // endtextbuf may get < ptrtextbuf ...
1057       if (nextblocksize > 0) {
1058         memcpy(ptrtextbuf, nextblock, nextblocksize);
1059         ptrtextbuf += nextblocksize;
1060         nextblock += nextblocksize;
1061       }
1062       // now write out the translated buffer
1063       char * writetextbuf = textbuf;
1064       for (size_t NumberOfBytesToWrite = (size_t)(ptrtextbuf - textbuf);
1065            NumberOfBytesToWrite;) {
1066         DWORD NumberOfBytesWritten;
1067         WriteFile((HANDLE)_M_file_id, writetextbuf,
1068                   __STATIC_CAST(DWORD, (min)(size_t(0xffffffff), NumberOfBytesToWrite)),
1069                   &NumberOfBytesWritten, 0);
1070         if (!NumberOfBytesWritten) // write shortfall
1071           return false;
1072         writetextbuf += NumberOfBytesWritten;
1073         NumberOfBytesToWrite -= NumberOfBytesWritten;
1074       }
1075       // count non-translated characters
1076       written = (nextblock - buf);
1077     }
1078 
1079 #elif defined (_STLP_USE_STDIO_IO)
1080 
1081     written = fwrite(buf, 1, n, _M_file);
1082 
1083 #elif defined (_STLP_USE_SYMBIAN_IO)
1084 
1085 	if (_M_openmode & ios_base::app) {
1086 		TInt ofs = 0;
1087 		if (_M_file_id.Seek(ESeekEnd, ofs) != KErrNone)
1088 			return false;
1089 	}
1090 	TPtrC8 pbuf((unsigned char*)buf, n);
1091 	if (_M_file_id.Write(pbuf) != KErrNone)
1092 		return false;
1093 	written = pbuf.Length();
1094 
1095 #else
1096 #  error "Port!"
1097 #endif /* __unix */
1098 
1099     if (n == written)
1100       return true;
1101     else if (written > 0 && written < n) {
1102       n -= written;
1103       buf += written;
1104     }
1105     else
1106       return false;
1107   }
1108 }
1109 
1110 
1111 #ifdef _STLP_USE_WIN32_IO
1112 #  define STL_SEEK_SET FILE_BEGIN
1113 #  define STL_SEEK_CUR FILE_CURRENT
1114 #  define STL_SEEK_END FILE_END
1115 #elif defined(_STLP_USE_SYMBIAN_IO)
1116 #  define STL_SEEK_SET ESeekStart
1117 #  define STL_SEEK_CUR ESeekCurrent
1118 #  define STL_SEEK_END ESeekEnd
1119 #else
1120 #  define STL_SEEK_SET SEEK_SET
1121 #  define STL_SEEK_CUR SEEK_CUR
1122 #  define STL_SEEK_END SEEK_END
1123 #endif
1124 
1125 // Wrapper for lseek or the like.
_M_seek(streamoff offset,ios_base::seekdir dir)1126 streamoff _Filebuf_base::_M_seek(streamoff offset, ios_base::seekdir dir) {
1127   streamoff result = -1;
1128   int whence;
1129 
1130   switch(dir) {
1131   case ios_base::beg:
1132     if (offset < 0 /* || offset > _M_file_size() */ )
1133       return streamoff(-1);
1134     whence = STL_SEEK_SET;
1135     break;
1136   case ios_base::cur:
1137     whence = STL_SEEK_CUR;
1138     break;
1139   case ios_base::end:
1140     if (/* offset > 0 || */  -offset > _M_file_size() )
1141       return streamoff(-1);
1142     whence = STL_SEEK_END;
1143     break;
1144   default:
1145     return streamoff(-1);
1146   }
1147 
1148 #if defined (_STLP_USE_UNIX_IO) || defined (_STLP_USE_UNIX_EMULATION_IO)
1149 
1150   result = LSEEK(_M_file_id, offset, whence);
1151 
1152 #elif defined (_STLP_USE_STDIO_IO)
1153 
1154   if (FSEEK(_M_file, offset, whence) != 0)
1155 	return streamoff(-1);
1156   result = ftell(_M_file);
1157 
1158 #elif defined (_STLP_USE_WIN32_IO)
1159 
1160   LARGE_INTEGER li;
1161   li.QuadPart = offset;
1162   li.LowPart = SetFilePointer(_M_file_id, li.LowPart, &li.HighPart, whence);
1163   if (li.LowPart != INVALID_SET_FILE_POINTER || GetLastError() == NO_ERROR)
1164     result = li.QuadPart;
1165 
1166 #elif defined (_STLP_USE_SYMBIAN_IO)
1167 
1168   TInt ofs = (TInt)offset;
1169   if (_M_file_id.Seek((TSeek)whence, ofs) != KErrNone)
1170 	return streamoff(-1);
1171   return streamoff(ofs);
1172 
1173 #else
1174 #  error "Port!"
1175 #endif
1176 
1177   return result;
1178 }
1179 
1180 
1181 // Attempts to memory-map len bytes of the current file, starting
1182 // at position offset.  Precondition: offset is a multiple of the
1183 // page size.  Postcondition: return value is a null pointer if the
1184 // memory mapping failed.  Otherwise the return value is a pointer to
1185 // the memory-mapped file and the file position is set to offset.
_M_mmap(streamoff offset,streamoff len)1186 void* _Filebuf_base::_M_mmap(streamoff offset, streamoff len) {
1187   void* base;
1188 #if defined (_STLP_UNIX) && !defined(__DJGPP) && !defined(_CRAY)
1189   base = MMAP(0, len, PROT_READ, MAP_PRIVATE, _M_file_id, offset);
1190   if (base != (void*)MAP_FAILED) {
1191     if (LSEEK(_M_file_id, offset + len, SEEK_SET) < 0) {
1192       this->_M_unmap(base, len);
1193       base = 0;
1194     }
1195   } else
1196     base =0;
1197 
1198 #elif defined (_STLP_USE_WIN32_IO)
1199   _M_view_id = CreateFileMapping(_M_file_id, (PSECURITY_ATTRIBUTES)0 ,
1200                                  PAGE_READONLY, 0 /* len >> 32 */ ,
1201                                  0 /* len & 0xFFFFFFFF */ , // low-order DWORD of size
1202                                  0);
1203 
1204   if (_M_view_id) {
1205 #  if 0
1206 /*
1207     printf("view %x created from file %x, error = %d, size = %d, map_offset = %d map_len = %d\n",
1208      _M_view_id, _M_file_id, GetLastError(),
1209      (int)cur_filesize, ULL(offset) & 0xffffffff, len);
1210 */
1211 #  endif
1212     base = MapViewOfFile(_M_view_id, FILE_MAP_READ, __STATIC_CAST(DWORD, ULL(offset) >> 32),
1213                          __STATIC_CAST(DWORD, ULL(offset) & 0xffffffff),
1214 #  if !defined (__DMC__)
1215                          __STATIC_CAST(SIZE_T, len));
1216 #  else
1217                          __STATIC_CAST(DWORD, len));
1218 #  endif
1219     // check if mapping succeded and is usable
1220     if (base == 0  || _M_seek(offset + len, ios_base::beg) < 0) {
1221       this->_M_unmap(base, len);
1222       base = 0;
1223     }
1224   } else
1225     base = 0;
1226 #else
1227   (void)len;    //*TY 02/26/2000 - unused variables
1228   (void)offset;    //*TY 02/26/2000 -
1229   base = 0;
1230 #endif
1231   return base;
1232 }
1233 
_M_unmap(void * base,streamoff len)1234 void _Filebuf_base::_M_unmap(void* base, streamoff len) {
1235   // precondition : there is a valid mapping at the moment
1236 #if defined (_STLP_UNIX)  && !defined(__DJGPP) && !defined(_CRAY)
1237   munmap((char*)base, len);
1238 #elif defined (_STLP_USE_WIN32_IO)
1239   if (base != NULL)
1240     UnmapViewOfFile(base);
1241   // destroy view handle as well
1242   if (_M_view_id != NULL)
1243     CloseHandle(_M_view_id);
1244   _M_view_id = NULL;
1245   (void)len; //unused variable
1246 #else
1247   (void)len;    //*TY 02/26/2000 - unused variables
1248   (void)base;   //*TY 02/26/2000 -
1249 #endif
1250 }
1251 
1252 // fbp : let us map 1 MB maximum, just be sure not to trash VM
1253 #define MMAP_CHUNK 0x100000L
1254 
1255 int _STLP_CALL
_M_doit(basic_filebuf<char,char_traits<char>> * __this)1256 _Underflow<char, char_traits<char> >::_M_doit (basic_filebuf<char, char_traits<char> >* __this) {
1257   if (!__this->_M_in_input_mode) {
1258     if (!__this->_M_switch_to_input_mode())
1259       return traits_type::eof();
1260   }
1261   else if (__this->_M_in_putback_mode) {
1262     __this->_M_exit_putback_mode();
1263     if (__this->gptr() != __this->egptr()) {
1264       int_type __c = traits_type::to_int_type(*__this->gptr());
1265       return __c;
1266     }
1267   }
1268 
1269   // If it's a disk file, and if the internal and external character
1270   // sequences are guaranteed to be identical, then try to use memory
1271   // mapped I/O.  Otherwise, revert to ordinary read.
1272   if (__this->_M_base.__regular_file()
1273       && __this->_M_always_noconv
1274       && __this->_M_base._M_in_binary_mode()) {
1275     // If we've mmapped part of the file already, then unmap it.
1276     if (__this->_M_mmap_base)
1277       __this->_M_base._M_unmap(__this->_M_mmap_base, __this->_M_mmap_len);
1278 
1279     // Determine the position where we start mapping.  It has to be
1280     // a multiple of the page size.
1281     streamoff __cur = __this->_M_base._M_seek(0, ios_base::cur);
1282     streamoff __size = __this->_M_base._M_file_size();
1283     if (__size > 0 && __cur >= 0 && __cur < __size) {
1284       streamoff __offset = (__cur / __this->_M_base.__page_size()) * __this->_M_base.__page_size();
1285       streamoff __remainder = __cur - __offset;
1286 
1287       __this->_M_mmap_len = __size - __offset;
1288 
1289       if (__this->_M_mmap_len > MMAP_CHUNK)
1290         __this->_M_mmap_len = MMAP_CHUNK;
1291 
1292       if ((__this->_M_mmap_base =
1293         __this->_M_base._M_mmap(__offset, __this->_M_mmap_len)) != 0) {
1294         __this->setg((char*) __this->_M_mmap_base,
1295                      (char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __remainder),
1296                      (char*) __this->_M_mmap_base + __STATIC_CAST(ptrdiff_t, __this->_M_mmap_len));
1297         return traits_type::to_int_type(*__this->gptr());
1298       } else
1299         __this->_M_mmap_len = 0;
1300     }
1301     else {
1302       __this->_M_mmap_base = 0;
1303       __this->_M_mmap_len = 0;
1304     }
1305   }
1306 
1307   return __this->_M_underflow_aux();
1308 }
1309 
1310 
1311 //----------------------------------------------------------------------
1312 // Force instantiation of filebuf and fstream classes.
1313 #if !defined(_STLP_NO_FORCE_INSTANTIATE)
1314 
1315 template class basic_filebuf<char, char_traits<char> >;
1316 template class basic_ifstream<char, char_traits<char> >;
1317 template class basic_ofstream<char, char_traits<char> >;
1318 template class basic_fstream<char, char_traits<char> >;
1319 
1320 #  if !defined (_STLP_NO_WCHAR_T)
1321 template class _Underflow<wchar_t, char_traits<wchar_t> >;
1322 template class basic_filebuf<wchar_t, char_traits<wchar_t> >;
1323 template class basic_ifstream<wchar_t, char_traits<wchar_t> >;
1324 template class basic_ofstream<wchar_t, char_traits<wchar_t> >;
1325 template class basic_fstream<wchar_t, char_traits<wchar_t> >;
1326 #  endif /* _STLP_NO_WCHAR_T */
1327 
1328 #endif
1329 
1330 _STLP_END_NAMESPACE
1331 
1332 // Local Variables:
1333 // mode:C++
1334 // End:
1335