Copyright (C) 1990 Free Software Foundation
written by Doug Lea (dl@rocky.oswego.edu)
This file is part of GNU CC.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the GNU CC General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License. A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies.
#include <sys/file.h> // needed to determine values of O_RDONLY...
:streambuf(), fd(-1), opened(0) {}
filebuf::filebuf(int newfd
)
: streambuf(), fd(newfd
), opened(1) {}
filebuf::filebuf(int newfd
, char* buf
, int buflen
)
: streambuf(buf
, buflen
), fd(newfd
), opened(1) {}
filebuf::filebuf(const char* filename
, io_mode m
, access_mode a
)
filebuf::filebuf(const char* filename
, const char* m
)
filebuf::filebuf(int filedesc
, io_mode m
)
filebuf::filebuf(FILE* fileptr
)
streambuf
* filebuf::open(const char* filename
, open_mode m
)
int mode
= -1; // any illegal value
case input
: mode
= O_RDONLY
;
case output
: mode
= O_WRONLY
| O_CREAT
| O_TRUNC
;
case append
: mode
= O_APPEND
| O_CREAT
| O_WRONLY
;
fd
= ::open(filename
, mode
, 0666);
streambuf
* filebuf::open(const char* filename
, io_mode m
, access_mode a
)
int open_arg
= open_cmd_arg(m
, a
);
if (open_arg
== -1) return 0;
fd
= ::open(filename
, open_arg
, 0666);
streambuf
* filebuf::open(const char* filename
, const char* m
)
int open_arg
= open_cmd_arg(m
);
if (open_arg
== -1) return 0;
fd
= ::open(filename
, open_arg
, 0666);
streambuf
* filebuf::open(int filedesc
, io_mode m
)
int open_arg
= open_cmd_arg(m
, a_use
);
if (open_arg
== -1) return 0;
fd
= ::open(name
, open_arg
, 0666);
streambuf
* filebuf::open(FILE* fileptr
)
if (base
== 0) allocate();
int nwanted
= eptr
- base
+ 1;
int nread
= ::read(fd
, base
, nwanted
);
return (nread
<= 0)? EOF
: int(*gptr
);
int filebuf::overflow(int ch
)
if (base
== 0) allocate();
if (ch
!= EOF
) // overflow *must* be called before really full
// loop, in case write can't handle full request
// From: Rene' Seindal <seindal@diku.dk>
for (w
= t
= 0, n
= pptr
- base
; n
> 0; n
-= w
, t
+= w
)
if ((w
= ::write(fd
, base
+ t
, n
)) < 0)
return (n
== 0 && w
>= 0)? 0 : EOF
;