/**********************************************************************
io.c -
$Author$
$Date$
created at: Fri Oct 15 18:08:59 JST 1993
Copyright (C) 1993-2003 Yukihiro Matsumoto
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
Copyright (C) 2000 Information-technology Promotion Agency, Japan
**********************************************************************/
#if defined(__VMS)
#define _XOPEN_SOURCE
#define _POSIX_C_SOURCE 2
#endif
#include "ruby.h"
#include "rubyio.h"
#include "rubysig.h"
#include "env.h"
#include <ctype.h>
#include <errno.h>
#if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__human68k__) || defined(__EMX__) || defined(__BEOS__)
# define NO_SAFE_RENAME
#endif
#if defined(MSDOS) || defined(__CYGWIN__) || defined(_WIN32)
# define NO_LONG_FNAME
#endif
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(sun) || defined(_nec_ews)
# define USE_SETVBUF
#endif
#ifdef __QNXNTO__
#include "unix.h"
#endif
#include <sys/types.h>
#if defined(HAVE_SYS_IOCTL_H) && !defined(DJGPP) && !defined(_WIN32) && !defined(__human68k__)
#include <sys/ioctl.h>
#endif
#if defined(HAVE_FCNTL_H) || defined(_WIN32)
#include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
#include <sys/fcntl.h>
#endif
#if !HAVE_OFF_T && !defined(off_t)
# define off_t long
#endif
#if !HAVE_FSEEKO && !defined(fseeko)
# define fseeko fseek
#endif
#if !HAVE_FTELLO && !defined(ftello)
# define ftello ftell
#endif
#include <sys/stat.h>
/* EMX has sys/param.h, but.. */
#if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
# include <sys/param.h>
#endif
#if !defined NOFILE
# define NOFILE 64
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
extern void Init_File _((void));
#ifdef __BEOS__
# ifndef NOFILE
# define NOFILE (OPEN_MAX)
# endif
#include <net/socket.h>
#endif
#include "util.h"
#ifndef O_ACCMODE
#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
#endif
#if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
# error off_t is bigger than long, but you have no long long...
#endif
VALUE rb_cIO;
VALUE rb_eEOFError;
VALUE rb_eIOError;
VALUE rb_stdin, rb_stdout, rb_stderr;
VALUE rb_deferr; /* rescue VIM plugin */
static VALUE orig_stdout, orig_stderr;
VALUE rb_output_fs;
VALUE rb_rs;
VALUE rb_output_rs;
VALUE rb_default_rs;
static VALUE argf;
static ID id_write, id_read, id_getc;
extern char *ruby_inplace_mode;
struct timeval rb_time_interval _((VALUE));
static VALUE filename, current_file;
static int gets_lineno;
static int init_p = 0, next_p = 0;
static VALUE lineno = INT2FIX(0);
#ifdef _STDIO_USES_IOSTREAM /* GNU libc */
# ifdef _IO_fpos_t
# define READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_IO_read_end - (fp)->_IO_read_ptr)
# define READ_DATA_PENDING_PTR(fp) ((fp)->_IO_read_ptr)
# else
# define READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_egptr - (fp)->_gptr)
# define READ_DATA_PENDING_PTR(fp) ((fp)->_gptr)
# endif
#elif defined(FILE_COUNT)
# define READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->FILE_COUNT)
#elif defined(FILE_READEND)
# define READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
# define READ_DATA_PENDING_COUNT(fp) ((fp)->FILE_READEND - (fp)->FILE_READPTR)
#elif defined(__BEOS__)
# define READ_DATA_PENDING(fp) (fp->_state._eof == 0)
#elif defined(__VMS)
# define READ_DATA_PENDING_COUNT(fp) ((unsigned int)(*(fp))->_cnt)
# define READ_DATA_PENDING(fp) (((unsigned int)(*(fp))->_cnt) > 0)
# define READ_DATA_BUFFERED(fp) 0
#else
/* requires systems own version of the ReadDataPending() */
extern int ReadDataPending();
# define READ_DATA_PENDING(fp) (!feof(fp))
# define READ_DATA_BUFFERED(fp) 0
#endif
#ifndef READ_DATA_BUFFERED
# define READ_DATA_BUFFERED(fp) READ_DATA_PENDING(fp)
#endif
#ifndef READ_DATA_PENDING_PTR
# ifdef FILE_READPTR
# define READ_DATA_PENDING_PTR(fp) ((char *)(fp)->FILE_READPTR)
# endif
#endif
#if defined __DJGPP__
# undef READ_DATA_PENDING_COUNT
# undef READ_DATA_PENDING_PTR
#endif
#define READ_CHECK(fp) do {\
if (!READ_DATA_PENDING(fp)) {\
rb_thread_wait_fd(fileno(fp));\
rb_io_check_closed(fptr);\
}\
} while(0)
void
rb_eof_error()
{
rb_raise(rb_eEOFError, "end of file reached");
}
VALUE
rb_io_taint_check(io)
VALUE io;
{
if (!OBJ_TAINTED(io) && rb_safe_level() >= 4)
rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
rb_check_frozen(io);
return io;
}
void
rb_io_check_initialized(fptr)
OpenFile *fptr;
{
if (!fptr) {
rb_raise(rb_eIOError, "uninitialized stream");
}
}
void
rb_io_check_closed(fptr)
OpenFile *fptr;
{
rb_io_check_initialized(fptr);
if (!fptr->f && !fptr->f2) {
rb_raise(rb_eIOError, "closed stream");
}
}
static void io_fflush _((FILE *, OpenFile *));
static OpenFile *
flush_before_seek(fptr)
OpenFile *fptr;
{
if (fptr->mode & FMODE_WBUF) {
io_fflush(GetWriteFile(fptr), fptr);
}
return fptr;
}
#define io_seek(fptr, ofs, whence) fseeko(flush_before_seek(fptr)->f, ofs, whence)
#define io_tell(fptr) ftello(flush_before_seek(fptr)->f)
#ifndef SEEK_CUR
# define SEEK_SET 0
# define SEEK_CUR 1
# define SEEK_END 2
#endif
#define FMODE_SYNCWRITE (FMODE_SYNC|FMODE_WRITABLE)
void
rb_io_check_readable(fptr)
OpenFile *fptr;
{
rb_io_check_closed(fptr);
if (!(fptr->mode & FMODE_READABLE)) {
rb_raise(rb_eIOError, "not opened for reading");
}
#if NEED_IO_SEEK_BETWEEN_RW
if (((fptr->mode & FMODE_WBUF) ||
(fptr->mode & (FMODE_SYNCWRITE|FMODE_RBUF)) == FMODE_SYNCWRITE) &&
!feof(fptr->f) &&
!fptr->f2) {
io_seek(fptr, 0, SEEK_CUR);
}
#endif
fptr->mode |= FMODE_RBUF;
}
void
rb_io_check_writable(fptr)
OpenFile *fptr;
{
rb_io_check_closed(fptr);
if (!(fptr->mode & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "not opened for writing");
}
if ((fptr->mode & FMODE_RBUF) && !feof(fptr->f) && !fptr->f2) {
io_seek(fptr, 0, SEEK_CUR);
}
if (!fptr->f2) {
fptr->mode &= ~FMODE_RBUF;
}
}
int
rb_read_pending(fp)
FILE *fp;
{
return READ_DATA_PENDING(fp);
}
void
rb_read_check(fp)
FILE *fp;
{
if (!READ_DATA_PENDING(fp)) {
rb_thread_wait_fd(fileno(fp));
}
}
static int
ruby_dup(orig)
int orig;
{
int fd;
fd = dup(orig);
if (fd < 0) {
if (errno == EMFILE || errno == ENFILE) {
rb_gc();
fd = dup(orig);
}
if (fd < 0) {
rb_sys_fail(0);
}
}
return fd;
}
static VALUE io_alloc _((VALUE));
static VALUE
io_alloc(klass)
VALUE klass;
{
NEWOBJ(io, struct RFile);
OBJSETUP(io, klass, T_FILE);
io->fptr = 0;
return (VALUE)io;
}
static void
io_fflush(f, fptr)
FILE *f;
OpenFile *fptr;
{
int n;
if (!rb_thread_fd_writable(fileno(f))) {
rb_io_check_closed(fptr);
}
for (;;) {
TRAP_BEG;
n = fflush(f);
TRAP_END;
if (n != EOF) break;
if (!rb_io_wait_writable(fileno(f)))
rb_sys_fail(fptr->path);
}
fptr->mode &= ~FMODE_WBUF;
}
int
rb_io_wait_readable(f)
int f;
{
fd_set rfds;
switch (errno) {
case EINTR:
#if defined(ERESTART)
case ERESTART:
#endif
rb_thread_wait_fd(f);
return Qtrue;
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
FD_ZERO(&rfds);
FD_SET(f, &rfds);
rb_thread_select(f + 1, &rfds, NULL, NULL, NULL);
return Qtrue;
default:
return Qfalse;
}
}
int
rb_io_wait_writable(f)
int f;
{
fd_set wfds;
switch (errno) {
case EINTR:
#if defined(ERESTART)
case ERESTART:
#endif
rb_thread_fd_writable(f);
return Qtrue;
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
FD_ZERO(&wfds);
FD_SET(f, &wfds);
rb_thread_select(f + 1, NULL, &wfds, NULL, NULL);
return Qtrue;
default:
return Qfalse;
}
}
#ifndef S_ISREG
# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
#endif
static int
wsplit_p(OpenFile *fptr)
{
FILE *f = GetWriteFile(fptr);
int r;
if (!(fptr->mode & FMODE_WSPLIT_INITIALIZED)) {
struct stat buf;
if (fstat(fileno(f), &buf) == 0 &&
!S_ISREG(buf.st_mode)
#if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
&& (r = fcntl(fileno(f), F_GETFL)) != -1 &&
!(r & O_NONBLOCK)
#endif
) {
fptr->mode |= FMODE_WSPLIT;
}
fptr->mode |= FMODE_WSPLIT_INITIALIZED;
}
return fptr->mode & FMODE_WSPLIT;
}
/* writing functions */
static long
io_fwrite(str, fptr)
VALUE str;
OpenFile *fptr;
{
long len, n, r, l, offset = 0;
FILE *f = GetWriteFile(fptr);
len = RSTRING(str)->len;
if ((n = len) <= 0) return n;
if (fptr->mode & FMODE_SYNC) {
io_fflush(f, fptr);
if (!rb_thread_fd_writable(fileno(f))) {
rb_io_check_closed(fptr);
}
retry:
l = n;
if (PIPE_BUF < l &&
!rb_thread_critical &&
!rb_thread_alone() &&
wsplit_p(fptr)) {
l = PIPE_BUF;
}
r = write(fileno(f), RSTRING(str)->ptr+offset, l);
if (r == n) return len;
if (0 <= r) {
offset += r;
n -= r;
errno = EAGAIN;
}
if (rb_io_wait_writable(fileno(f))) {
rb_io_check_closed(fptr);
if (offset < RSTRING(str)->len)
goto retry;
}
return -1L;
}
#if defined(__human68k__) || defined(__vms)
do {
if (fputc(RSTRING(str)->ptr[offset++], f) == EOF) {
if (ferror(f)) return -1L;
break;
}
} while (--n > 0);
#else
while (errno = 0, offset += (r = fwrite(RSTRING(str)->ptr+offset, 1, n, f)), (n -= r) > 0) {
if (ferror(f)
#if defined __BORLANDC__
|| errno
#endif
) {
#ifdef __hpux
if (!errno) errno = EAGAIN;
#elif defined(_WIN32) && !defined(__BORLANDC__)
/* workaround for MSVCRT's bug */
if (!errno) {
if (GetLastError() == ERROR_NO_DATA)
errno = EPIPE;
else
errno = EBADF;
}
#endif
if (rb_io_wait_writable(fileno(f))) {
rb_io_check_closed(fptr);
clearerr(f);
if (offset < RSTRING(str)->len)
continue;
}
return -1L;
}
}
#endif
return len - n;
}
long
rb_io_fwrite(ptr, len, f)
const char *ptr;
long len;
FILE *f;
{
OpenFile of;
of.f = f;
of.f2 = NULL;
of.mode = FMODE_WRITABLE;
of.path = NULL;
return io_fwrite(rb_str_new(ptr, len), &of);
}
/*
* call-seq:
* ios.write(string) => integer
*
* Writes the given string to <em>ios</em>. The stream must be opened
* for writing. If the argument is not a string, it will be converted
* to a string using <code>to_s</code>. Returns the number of bytes
* written.
*
* count = $stdout.write( "This is a test\n" )
* puts "That was #{count} bytes of data"
*
* <em>produces:</em>
*
* This is a test
* That was 15 bytes of data
*/
static VALUE
io_write(io, str)
VALUE io, str;
{
OpenFile *fptr;
long n;
rb_secure(4);
if (TYPE(str) != T_STRING)
str = rb_obj_as_string(str);
if (TYPE(io) != T_FILE) {
/* port is not IO, call write method for it. */
return rb_funcall(io, id_write, 1, str);
}
if (RSTRING(str)->len == 0) return INT2FIX(0);
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
n = io_fwrite(str, fptr);
if (n == -1L) rb_sys_fail(fptr->path);
if (!(fptr->mode & FMODE_SYNC)) {
fptr->mode |= FMODE_WBUF;
}
return LONG2FIX(n);
}
VALUE
rb_io_write(io, str)
VALUE io, str;
{
return rb_funcall(io, id_write, 1, str);
}
/*
* call-seq:
* ios << obj => ios
*
* String Output---Writes <i>obj</i> to <em>ios</em>.
* <i>obj</i> will be converted to a string using
* <code>to_s</code>.
*
* $stdout << "Hello " << "world!\n"
*
* <em>produces:</em>
*
* Hello world!
*/
VALUE
rb_io_addstr(io, str)
VALUE io, str;
{
rb_io_write(io, str);
return io;
}
/*
* call-seq:
* ios.flush => ios
*
* Flushes any buffered data within <em>ios</em> to the underlying
* operating system (note that this is Ruby internal buffering only;
* the OS may buffer the data as well).
*
* $stdout.print "no newline"
* $stdout.flush
*
* <em>produces:</em>
*
* no newline
*/
static VALUE
rb_io_flush(io)
VALUE io;
{
OpenFile *fptr;
FILE *f;
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
f = GetWriteFile(fptr);
io_fflush(f, fptr);
return io;
}
/*
* call-seq:
* ios.pos => integer
* ios.tell => integer
*
* Returns the current offset (in bytes) of <em>ios</em>.
*
* f = File.new("testfile")
* f.pos #=> 0
* f.gets #=> "This is line one\n"
* f.pos #=> 17
*/
static VALUE
rb_io_tell(io)
VALUE io;
{
OpenFile *fptr;
off_t pos;
GetOpenFile(io, fptr);
pos = io_tell(fptr);
if (pos < 0) rb_sys_fail(fptr->path);
return OFFT2NUM(pos);
}
static VALUE
rb_io_seek(io, offset, whence)
VALUE io, offset;
int whence;
{
OpenFile *fptr;
off_t pos;
pos = NUM2OFFT(offset);
GetOpenFile(io, fptr);
pos = io_seek(fptr, pos, whence);
if (pos < 0) rb_sys_fail(fptr->path);
clearerr(fptr->f);
return INT2FIX(0);
}
/*
* call-seq:
* ios.seek(amount, whence=SEEK_SET) -> 0
*
* Seeks to a given offset <i>anInteger</i> in the stream according to
* the value of <i>whence</i>:
*
* IO::SEEK_CUR | Seeks to _amount_ plus current position
* --------------+----------------------------------------------------
* IO::SEEK_END | Seeks to _amount_ plus end of stream (you probably
* | want a negative value for _amount_)
* --------------+----------------------------------------------------
* IO::SEEK_SET | Seeks to the absolute location given by _amount_
*
* Example:
*
* f = File.new("testfile")
* f.seek(-13, IO::SEEK_END) #=> 0
* f.readline #=> "And so on...\n"
*/
static VALUE
rb_io_seek_m(argc, argv, io)
int argc;
VALUE *argv;
VALUE io;
{
VALUE offset, ptrname;
int whence = SEEK_SET;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
whence = NUM2INT(ptrname);
}
return rb_io_seek(io, offset, whence);
}
/*
* call-seq:
* ios.pos = integer => integer
*
* Seeks to the given position (in bytes) in <em>ios</em>.
*
* f = File.new("testfile")
* f.pos = 17
* f.gets #=> "This is line two\n"
*/
static VALUE
rb_io_set_pos(io, offset)
VALUE io, offset;
{
OpenFile *fptr;
off_t pos;
pos = NUM2OFFT(offset);
GetOpenFile(io, fptr);
pos = io_seek(fptr, pos, SEEK_SET);
if (pos != 0) rb_sys_fail(fptr->path);
clearerr(fptr->f);
return OFFT2NUM(pos);
}
/*
* call-seq:
* ios.rewind => 0
*
* Positions <em>ios</em> to the beginning of input, resetting
* <code>lineno</code> to zero.
*
* f = File.new("testfile")
* f.readline #=> "This is line one\n"
* f.rewind #=> 0
* f.lineno #=> 0
* f.readline #=> "This is line one\n"
*/
static VALUE
rb_io_rewind(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (io_seek(fptr, 0L, 0) != 0) rb_sys_fail(fptr->path);
clearerr(fptr->f);
if (io == current_file) {
gets_lineno -= fptr->lineno;
}
fptr->lineno = 0;
return INT2FIX(0);
}
/*
* call-seq:
* ios.eof => true or false
* ios.eof? => true or false
*
* Returns true if <em>ios</em> is at end of file. The stream must be
* opened for reading or an <code>IOError</code> will be raised.
*
* f = File.new("testfile")
* dummy = f.readlines
* f.eof #=> true
*/
VALUE
rb_io_eof(io)
VALUE io;
{
OpenFile *fptr;
int ch;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (feof(fptr->f)) return Qtrue;
if (READ_DATA_PENDING(fptr->f)) return Qfalse;
READ_CHECK(fptr->f);
clearerr(fptr->f);
TRAP_BEG;
ch = getc(fptr->f);
TRAP_END;
if (ch != EOF) {
ungetc(ch, fptr->f);
return Qfalse;
}
rb_io_check_closed(fptr);
clearerr(fptr->f);
return Qtrue;
}
/*
* call-seq:
* ios.sync => true or false
*
* Returns the current ``sync mode'' of <em>ios</em>. When sync mode is
* true, all output is immediately flushed to the underlying operating
* system and is not buffered by Ruby internally. See also
* <code>IO#fsync</code>.
*
* f = File.new("testfile")
* f.sync #=> false
*/
static VALUE
rb_io_sync(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
}
/*
* call-seq:
* ios.sync = boolean => boolean
*
* Sets the ``sync mode'' to <code>true</code> or <code>false</code>.
* When sync mode is true, all output is immediately flushed to the
* underlying operating system and is not buffered internally. Returns
* the new state. See also <code>IO#fsync</code>.
*
* f = File.new("testfile")
* f.sync = true
*
* <em>(produces no output)</em>
*/
static VALUE
rb_io_set_sync(io, mode)
VALUE io, mode;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (RTEST(mode)) {
fptr->mode |= FMODE_SYNC;
}
else {
fptr->mode &= ~FMODE_SYNC;
}
return mode;
}
/*
* call-seq:
* ios.fsync => 0 or nil
*
* Immediately writes all buffered data in <em>ios</em> to disk.
* Returns <code>nil</code> if the underlying operating system does not
* support <em>fsync(2)</em>. Note that <code>fsync</code> differs from
* using <code>IO#sync=</code>. The latter ensures that data is flushed
* from Ruby's buffers, but doesn't not guarantee that the underlying
* operating system actually writes it to disk.
*/
static VALUE
rb_io_fsync(io)
VALUE io;
{
#ifdef HAVE_FSYNC
OpenFile *fptr;
FILE *f;
GetOpenFile(io, fptr);
f = GetWriteFile(fptr);
io_fflush(f, fptr);
if (fsync(fileno(f)) < 0)
rb_sys_fail(fptr->path);
return INT2FIX(0);
#else
rb_notimplement();
return Qnil; /* not reached */
#endif
}
/*
* call-seq:
* ios.fileno => fixnum
* ios.to_i => fixnum
*
* Returns an integer representing the numeric file descriptor for
* <em>ios</em>.
*
* $stdin.fileno #=> 0
* $stdout.fileno #=> 1
*/
static VALUE
rb_io_fileno(io)
VALUE io;
{
OpenFile *fptr;
int fd;
GetOpenFile(io, fptr);
fd = fileno(fptr->f);
return INT2FIX(fd);
}
/*
* call-seq:
* ios.pid => fixnum
*
* Returns the process ID of a child process associated with
* <em>ios</em>. This will be set by <code>IO::popen</code>.
*
* pipe = IO.popen("-")
* if pipe
* $stderr.puts "In parent, child pid is #{pipe.pid}"
* else
* $stderr.puts "In child, pid is #{$$}"
* end
*
* <em>produces:</em>
*
* In child, pid is 26209
* In parent, child pid is 26209
*/
static VALUE
rb_io_pid(io)
VALUE io;
{
OpenFile *fptr;
GetOpenFile(io, fptr);
if (!fptr->pid)
return Qnil;
return INT2FIX(fptr->pid);
}
/*
* call-seq:
* ios.inspect => string
*
* Return a string describing this IO object.
*/
static VALUE
rb_io_inspect(obj)
VALUE obj;
{
OpenFile *fptr;
char *buf, *cname, *st = "";
long len;
fptr = RFILE(rb_io_taint_check(obj))->fptr;
if (!fptr || !fptr->path) return rb_any_to_s(obj);
cname = rb_obj_classname(obj);
len = strlen(cname) + strlen(fptr->path) + 5;
if (!(fptr->f || fptr->f2)) {
st = " (closed)";
len += 9;
}
buf = ALLOCA_N(char, len);
snprintf(buf, len, "#<%s:%s%s>", cname, fptr->path, st);
return rb_str_new2(buf);
}
/*
* call-seq:
* ios.to_io -> ios
*
* Returns <em>ios</em>.
*/
static VALUE
rb_io_to_io(io)
VALUE io;
{
return io;
}
/* reading functions */
static long
read_buffered_data(ptr, len, f)
char *ptr;
long len;
FILE *f;
{
long n;
#ifdef READ_DATA_PENDING_COUNT
n = READ_DATA_PENDING_COUNT(f);
if (n <= 0) return 0;
if (n > len) n = len;
return fread(ptr, 1, n, f);
#else
for (n = 0; n < len && READ_DATA_PENDING(f); ++n) {
*ptr++ = getc(f);
}
return n;
#endif
}
long
io_fread(ptr, len, fptr)
char *ptr;
long len;
OpenFile *fptr;
{
long n = len;
int c;
int saved_errno;
while (n > 0) {
c = read_buffered_data(ptr, n, fptr->f);
if (c < 0) goto eof;
if (c > 0) {
ptr += c;
if ((n -= c) <= 0) break;
}
rb_thread_wait_fd(fileno(fptr->f));
rb_io_check_closed(fptr);
clearerr(fptr->f);
TRAP_BEG;
c = getc(fptr->f);
TRAP_END;
if (c == EOF) {
eof:
if (ferror(fptr->f)) {
switch (errno) {
case EINTR:
#if defined(ERESTART)
case ERESTART:
#endif
clearerr(fptr->f);
continue;
case EAGAIN:
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
if (len > n) {
clearerr(fptr->f);
}
saved_errno = errno;
rb_warning("nonblocking IO#read is obsolete; use IO#readpartial or IO#sysread");
errno = saved_errno;
}
if (len == n) return 0;
}
break;
}
*ptr++ = c;
n--;
}
return len - n;
}
long
rb_io_fread(ptr, len, f)
char *ptr;
long len;
FILE *f;
{
OpenFile of;
of.f = f;
of.f2 = NULL;
return io_fread(ptr, len, &of);
}
#define SMALLBUF 100
static long
remain_size(fptr)
OpenFile *fptr;
{
struct stat st;
off_t siz = BUFSIZ;
off_t pos;
if (feof(fptr->f)) return 0;
if (fstat(fileno(fptr->f), &st) == 0 && S_ISREG(st.st_mode)
#ifdef __BEOS__
&& (st.st_dev > 3)
#endif
)
{
pos = io_tell(fptr);
if (st.st_size >= pos && pos >= 0) {
siz = st.st_size - pos + 1;
if (siz > LONG_MAX) {
rb_raise(rb_eIOError, "file too big for single read");
}
}
}
return (long)siz;
}
static VALUE
read_all(fptr, siz, str)
OpenFile *fptr;
long siz;
VALUE str;
{
long bytes = 0;
long n;
if (siz == 0) siz = BUFSIZ;
if (NIL_P(str)) {
str = rb_str_new(0, siz);
}
else {
rb_str_resize(str, siz);
}
for (;;) {
rb_str_locktmp(str);
READ_CHECK(fptr->f);
n = io_fread(RSTRING(str)->ptr+bytes, siz-bytes, fptr);
rb_str_unlocktmp(str);
if (n == 0 && bytes == 0) {
if (!fptr->f) break;
if (feof(fptr->f)) break;
if (!ferror(fptr->f)) break;
rb_sys_fail(fptr->path);
}
bytes += n;
if (bytes < siz) break;
siz += BUFSIZ;
rb_str_resize(str, siz);
}
if (bytes != siz) rb_str_resize(str, bytes);
OBJ_TAINT(str);
return str;
}
static VALUE
io_getpartial(int argc, VALUE *argv, VALUE io)
{
OpenFile *fptr;
VALUE length, str;
long n, len;
rb_scan_args(argc, argv, "11", &length, &str);
if ((len = NUM2LONG(length)) < 0) {
rb_raise(rb_eArgError, "negative length %ld given", len);
}
if (NIL_P(str)) {
str = rb_str_new(0, len);
}
else {
StringValue(str);
rb_str_modify(str);
rb_str_resize(str, len);
}
OBJ_TAINT(str);
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (len == 0)
return str;
READ_CHECK(fptr->f);
if (RSTRING(str)->len != len) {
modified:
rb_raise(rb_eRuntimeError, "buffer string modified");
}
n = read_buffered_data(RSTRING(str)->ptr, len, fptr->f);
if (n <= 0) {
again:
if (RSTRING(str)->len != len) goto modified;
TRAP_BEG;
n = read(fileno(fptr->f), RSTRING(str)->ptr, len);
TRAP_END;
if (n < 0) {
if (rb_io_wait_readable(fileno(fptr->f)))
goto again;
rb_sys_fail(fptr->path);
}
}
rb_str_resize(str, n);
if (n == 0)
return Qnil;
else
return str;
}
/*
* call-seq:
* ios.readpartial(maxlen[, outbuf]) => string, outbuf
*
* Reads at most <i>maxlen</i> bytes from the I/O stream but
* it blocks only if <em>ios</em> has no data immediately available.
* If the optional <i>outbuf</i> argument is present,
* it must reference a String, which will receive the data.
* It raises <code>EOFError</code> on end of file.
*
* readpartial is designed for streams such as pipe, socket, tty, etc.
* It blocks only when no data immediately available.
* This means that it blocks only when following all conditions hold.
|