1998-01-16 15:13:05 +03:00
|
|
|
/************************************************
|
|
|
|
|
|
|
|
io.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Oct 15 18:08:59 JST 1993
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
Copyright (C) 1993-1999 Yukihiro Matsumoto
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
1999-01-20 07:59:39 +03:00
|
|
|
#include "rubyio.h"
|
|
|
|
#include "rubysig.h"
|
1999-08-13 09:45:20 +04:00
|
|
|
#include "env.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#if !defined(DJGPP) && !defined(NT) && !defined(__human68k__)
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
#if defined(HAVE_FCNTL_H) || defined(NT)
|
1998-01-16 15:13:05 +03:00
|
|
|
#include <fcntl.h>
|
1999-08-13 09:45:20 +04:00
|
|
|
#elif defined(HAVE_SYS_FCNTL_H)
|
|
|
|
#include <sys/fcntl.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#else
|
|
|
|
#ifndef NT
|
|
|
|
struct timeval {
|
|
|
|
long tv_sec; /* seconds */
|
|
|
|
long tv_usec; /* and microseconds */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_VFORK_H
|
|
|
|
#include <vfork.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
/* EMX has sys/parm.h, but.. */
|
|
|
|
#if defined(HAVE_SYS_PARAM_H) && !defined(__EMX__)
|
1998-01-16 15:13:05 +03:00
|
|
|
# include <sys/param.h>
|
|
|
|
#else
|
|
|
|
# define NOFILE 64
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifdef USE_CWGUSI
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <unix.mac.h>
|
|
|
|
#include <compat.h>
|
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
#ifndef strdup
|
|
|
|
char *strdup();
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
extern void Init_File _((void));
|
|
|
|
|
|
|
|
#ifdef __BEOS__
|
1999-08-13 09:45:20 +04:00
|
|
|
# ifdef _X86_
|
|
|
|
# define NOFILE (OPEN_MAX)
|
|
|
|
# endif
|
1999-01-20 07:59:39 +03:00
|
|
|
#include <net/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "util.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cIO;
|
|
|
|
VALUE rb_eEOFError;
|
|
|
|
VALUE rb_eIOError;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
VALUE rb_stdin, rb_stdout, rb_stderr, rb_defout;
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE orig_stdin, orig_stdout, orig_stderr;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_fs;
|
|
|
|
VALUE rb_output_fs;
|
|
|
|
VALUE rb_rs;
|
|
|
|
VALUE rb_output_rs;
|
|
|
|
VALUE rb_default_rs;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static VALUE argf;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static ID id_write;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
extern char *ruby_inplace_mode;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
struct timeval rb_time_interval _((VALUE));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE filename, current_file;
|
1999-01-20 07:59:39 +03:00
|
|
|
static int gets_lineno;
|
|
|
|
static int init_p = 0, next_p = 0;
|
|
|
|
static VALUE lineno;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#ifdef _STDIO_USES_IOSTREAM /* GNU libc */
|
|
|
|
# ifdef _IO_fpos_t
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
|
|
|
|
# else
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
|
|
|
|
# endif
|
1999-01-20 07:59:39 +03:00
|
|
|
#elif defined(FILE_COUNT)
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
|
|
|
|
#elif defined(__BEOS__)
|
|
|
|
# define ReadDataPending(fp) (fp->_state._eof == 0)
|
|
|
|
#elif defined(USE_CWGUSI)
|
1999-08-13 09:45:20 +04:00
|
|
|
# define READ_DATA_PENDING(fp) (fp->state.eof == 0)
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
/* requires systems own version of the ReadDataPending() */
|
|
|
|
extern int ReadDataPending();
|
1999-01-20 07:59:39 +03:00
|
|
|
# define READ_DATA_PENDING(fp) ReadDataPending(fp)
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#define READ_CHECK(fp) do {\
|
|
|
|
if (!READ_DATA_PENDING(fp)) {\
|
|
|
|
rb_thread_wait_fd(fileno(fp));\
|
|
|
|
rb_io_check_closed(fptr);\
|
|
|
|
}\
|
1998-01-16 15:13:05 +03:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_eof_error()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eEOFError, "End of file reached");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_closed(fptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!fptr) {
|
|
|
|
rb_raise(rb_eIOError, "uninitialized stream");
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->f == NULL && fptr->f2 == NULL)
|
|
|
|
rb_raise(rb_eIOError, "closed stream");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
if (!(fptr->mode & FMODE_READABLE)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eIOError, "not opened for reading");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
|
|
|
rb_io_check_writable(fptr)
|
|
|
|
OpenFile *fptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!(fptr->mode & FMODE_WRITABLE)) {
|
|
|
|
rb_raise(rb_eIOError, "not opened for writing");
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
void
|
|
|
|
rb_read_check(fp)
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
if (!READ_DATA_PENDING(fp)) {
|
|
|
|
rb_thread_wait_fd(fileno(fp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
/* writing functions */
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
io_write(io, str)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE io, str;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int n;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_secure(4);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (TYPE(str) != T_STRING)
|
1999-01-20 07:59:39 +03:00
|
|
|
str = rb_obj_as_string(str);
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RSTRING(str)->len == 0) return INT2FIX(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-24 12:21:56 +04:00
|
|
|
if (TYPE(io) != T_FILE) {
|
1999-01-20 07:59:39 +03:00
|
|
|
/* port is not IO, call write method for it. */
|
1998-01-16 15:13:05 +03:00
|
|
|
return rb_funcall(io, id_write, 1, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_writable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
|
|
|
|
#ifdef __human68k__
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
register char *ptr = RSTRING(str)->ptr;
|
|
|
|
n = (int)RSTRING(str)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (--n >= 0)
|
|
|
|
if (fputc(*ptr++, f) == EOF)
|
|
|
|
rb_sys_fail(fptr->path);
|
1999-01-20 07:59:39 +03:00
|
|
|
n = ptr - RSTRING(str)->ptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
if (ferror(f))
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
#else
|
1998-01-16 15:19:22 +03:00
|
|
|
n = fwrite(RSTRING(str)->ptr, 1, RSTRING(str)->len, f);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(f)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (fptr->mode & FMODE_SYNC) {
|
|
|
|
fflush(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2FIX(n);
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE
|
|
|
|
rb_io_write(io, str)
|
|
|
|
VALUE io, str;
|
|
|
|
{
|
|
|
|
return rb_funcall(io, id_write, 1, str);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_addstr(io, str)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io, str;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_write(io, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_flush(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_writable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
|
|
|
|
if (fflush(f) == EOF) rb_sys_fail(0);
|
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_tell(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
long pos;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
pos = ftell(fptr->f);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(fptr->f)) rb_sys_fail(fptr->path);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return rb_int2inum(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_seek(io, offset, ptrname)
|
|
|
|
VALUE io, offset, ptrname;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
long pos;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
pos = fseek(fptr->f, NUM2INT(offset), NUM2INT(ptrname));
|
|
|
|
if (pos != 0) rb_sys_fail(fptr->path);
|
|
|
|
clearerr(fptr->f);
|
|
|
|
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SEEK_CUR
|
|
|
|
# define SEEK_SET 0
|
|
|
|
# define SEEK_CUR 1
|
|
|
|
# define SEEK_END 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_set_pos(io, offset)
|
|
|
|
VALUE io, offset;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
long pos;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
pos = fseek(fptr->f, NUM2INT(offset), SEEK_SET);
|
|
|
|
if (pos != 0) rb_sys_fail(fptr->path);
|
|
|
|
clearerr(fptr->f);
|
|
|
|
|
|
|
|
return INT2NUM(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_rewind(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if (fseek(fptr->f, 0L, 0) != 0) rb_sys_fail(fptr->path);
|
|
|
|
clearerr(fptr->f);
|
|
|
|
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_io_eof(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (feof(fptr->f)) return Qtrue;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (READ_DATA_PENDING(fptr->f)) return Qfalse;
|
1999-01-20 07:59:39 +03:00
|
|
|
READ_CHECK(fptr->f);
|
1998-01-16 15:13:05 +03:00
|
|
|
TRAP_BEG;
|
|
|
|
ch = getc(fptr->f);
|
|
|
|
TRAP_END;
|
|
|
|
|
|
|
|
if (ch != EOF) {
|
1999-01-20 07:59:39 +03:00
|
|
|
ungetc(ch, fptr->f);
|
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_sync(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_set_sync(io, mode)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io, mode;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if (RTEST(mode)) {
|
|
|
|
fptr->mode |= FMODE_SYNC;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fptr->mode &= ~FMODE_SYNC;
|
|
|
|
}
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_fileno(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
fd = fileno(fptr->f);
|
|
|
|
return INT2FIX(fd);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_io_to_io(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
/* reading functions */
|
1998-01-16 15:19:22 +03:00
|
|
|
|
|
|
|
#ifndef S_ISREG
|
|
|
|
# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SMALLBUF 100
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
read_all(port)
|
|
|
|
VALUE port;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
VALUE str = Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
struct stat st;
|
1999-01-20 07:59:39 +03:00
|
|
|
long siz = BUFSIZ;
|
|
|
|
long bytes = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
int n;
|
|
|
|
|
|
|
|
GetOpenFile(port, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (feof(fptr->f)) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fstat(fileno(fptr->f), &st) == 0 && S_ISREG(st.st_mode)
|
|
|
|
#ifdef __BEOS__
|
|
|
|
&& (st.st_dev > 3)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
if (st.st_size == 0) {
|
|
|
|
getc(fptr->f); /* force EOF */
|
|
|
|
return rb_str_new(0, 0);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
long pos = ftell(fptr->f);
|
1998-01-16 15:19:22 +03:00
|
|
|
if (st.st_size > pos && pos >= 0) {
|
|
|
|
siz = st.st_size - pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
str = rb_str_new(0, siz);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (;;) {
|
|
|
|
READ_CHECK(fptr->f);
|
|
|
|
TRAP_BEG;
|
1998-01-16 15:19:22 +03:00
|
|
|
n = fread(RSTRING(str)->ptr+bytes, 1, siz-bytes, fptr->f);
|
1998-01-16 15:13:05 +03:00
|
|
|
TRAP_END;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (n == 0) {
|
|
|
|
if (feof(fptr->f)) return Qnil;
|
|
|
|
rb_sys_fail(fptr->path);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
bytes += n;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (bytes < siz) break;
|
1998-01-16 15:19:22 +03:00
|
|
|
siz += BUFSIZ;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_str_resize(str, siz);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (bytes == 0) return rb_str_new(0,0);
|
|
|
|
if (bytes != siz) rb_str_resize(str, bytes);
|
|
|
|
OBJ_TAINT(str);
|
|
|
|
|
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
io_read(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
1998-01-16 15:19:22 +03:00
|
|
|
int n, len;
|
|
|
|
VALUE length, str;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_scan_args(argc, argv, "01", &length);
|
|
|
|
if (NIL_P(length)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
return read_all(io);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
len = NUM2INT(length);
|
1998-01-16 15:13:05 +03:00
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (feof(fptr->f)) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
str = rb_str_new(0, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
READ_CHECK(fptr->f);
|
|
|
|
TRAP_BEG;
|
1998-01-16 15:19:22 +03:00
|
|
|
n = fread(RSTRING(str)->ptr, 1, len, fptr->f);
|
1998-01-16 15:13:05 +03:00
|
|
|
TRAP_END;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (n == 0) {
|
|
|
|
if (feof(fptr->f)) return Qnil;
|
|
|
|
rb_sys_fail(fptr->path);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
RSTRING(str)->len = n;
|
|
|
|
RSTRING(str)->ptr[n] = '\0';
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJ_TAINT(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_io_gets_internal(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str = Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
int c, newline;
|
|
|
|
char *rsptr;
|
|
|
|
int rslen, rspara = 0;
|
|
|
|
VALUE rs;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (argc == 0) {
|
|
|
|
rs = rb_rs;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "1", &rs);
|
|
|
|
if (!NIL_P(rs)) Check_Type(rs, T_STRING);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(rs)) {
|
|
|
|
rsptr = 0;
|
|
|
|
rslen = 0;
|
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:13:05 +03:00
|
|
|
rslen = RSTRING(rs)->len;
|
|
|
|
if (rslen == 0) {
|
|
|
|
rsptr = "\n\n";
|
|
|
|
rslen = 2;
|
|
|
|
rspara = 1;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (rslen == 1 && RSTRING(rs)->ptr[0] == '\n') {
|
|
|
|
return rb_io_gets(io);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
|
|
|
rsptr = RSTRING(rs)->ptr;
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
f = fptr->f;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (rspara) {
|
|
|
|
do {
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c != '\n') {
|
|
|
|
ungetc(c,f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (c != EOF);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
newline = rslen ? rsptr[rslen - 1] : 0777;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
char buf[8192];
|
|
|
|
char *bp, *bpe = buf + sizeof buf - 3;
|
|
|
|
int cnt;
|
|
|
|
int append = 0;
|
|
|
|
|
|
|
|
again:
|
|
|
|
bp = buf;
|
|
|
|
|
|
|
|
if (rslen) {
|
|
|
|
for (;;) {
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (c == EOF) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(f) && errno == EINTR) continue;
|
1999-01-20 07:59:39 +03:00
|
|
|
break;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if ((*bp++ = c) == newline) break;
|
|
|
|
if (bp == bpe) break;
|
|
|
|
}
|
|
|
|
cnt = bp - buf;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
cnt = fread(buf, 1, sizeof(buf), f);
|
|
|
|
TRAP_END;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (cnt == 0) {
|
|
|
|
if (ferror(f)) rb_sys_fail(fptr->path);
|
|
|
|
c = EOF;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = 0;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (c == EOF && !append && cnt == 0) {
|
|
|
|
str = Qnil;
|
|
|
|
goto return_gets;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (append)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_str_cat(str, buf, cnt);
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
1999-01-20 07:59:39 +03:00
|
|
|
str = rb_str_new(buf, cnt);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (c != EOF &&
|
|
|
|
(!rslen ||
|
1998-01-16 15:19:22 +03:00
|
|
|
RSTRING(str)->len < rslen ||
|
1999-01-20 07:59:39 +03:00
|
|
|
memcmp(RSTRING(str)->ptr+RSTRING(str)->len-rslen,rsptr,rslen))) {
|
1998-01-16 15:13:05 +03:00
|
|
|
append = 1;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return_gets:
|
|
|
|
if (rspara) {
|
|
|
|
while (c != EOF) {
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c != '\n') {
|
|
|
|
ungetc(c, f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (!NIL_P(str)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
fptr->lineno++;
|
|
|
|
lineno = INT2FIX(fptr->lineno);
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJ_TAINT(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_gets(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
VALUE str = Qnil;
|
|
|
|
int c;
|
|
|
|
char buf[8192];
|
|
|
|
char *bp, *bpe = buf + sizeof buf - 3;
|
|
|
|
int cnt;
|
|
|
|
int append = 0;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
f = fptr->f;
|
|
|
|
|
|
|
|
again:
|
|
|
|
bp = buf;
|
|
|
|
for (;;) {
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c == EOF) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(f) && errno == EINTR) continue;
|
1999-01-20 07:59:39 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((*bp++ = c) == '\n') break;
|
|
|
|
if (bp == bpe) break;
|
|
|
|
}
|
|
|
|
cnt = bp - buf;
|
|
|
|
|
|
|
|
if (c == EOF && !append && cnt == 0) {
|
|
|
|
str = Qnil;
|
|
|
|
goto return_gets;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (append)
|
|
|
|
rb_str_cat(str, buf, cnt);
|
|
|
|
else
|
|
|
|
str = rb_str_new(buf, cnt);
|
|
|
|
|
|
|
|
if (c != EOF && RSTRING(str)->ptr[RSTRING(str)->len-1] != '\n') {
|
|
|
|
append = 1;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
return_gets:
|
|
|
|
if (!NIL_P(str)) {
|
|
|
|
fptr->lineno++;
|
|
|
|
lineno = INT2FIX(fptr->lineno);
|
|
|
|
OBJ_TAINT(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_gets_method(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE str = rb_io_gets_internal(argc, argv, io);
|
|
|
|
|
|
|
|
if (!NIL_P(str)) {
|
|
|
|
rb_lastline_set(str);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_lineno(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
return INT2NUM(fptr->lineno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_set_lineno(io, lineno)
|
|
|
|
VALUE io, lineno;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
return fptr->lineno = NUM2INT(lineno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lineno_setter(val, id, var)
|
|
|
|
VALUE val;
|
|
|
|
ID id;
|
|
|
|
VALUE *var;
|
|
|
|
{
|
|
|
|
gets_lineno = NUM2INT(val);
|
|
|
|
*var = INT2FIX(gets_lineno);
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
argf_set_lineno(argf, val)
|
|
|
|
VALUE argf, val;
|
|
|
|
{
|
|
|
|
gets_lineno = NUM2INT(val);
|
|
|
|
lineno = INT2FIX(gets_lineno);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_lineno()
|
|
|
|
{
|
|
|
|
return lineno;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_io_readline(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE line = rb_io_gets_method(argc, argv, io);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (NIL_P(line)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_eof_error();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_readlines(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE line, ary;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new();
|
|
|
|
while (!NIL_P(line = rb_io_gets_internal(argc, argv, io))) {
|
|
|
|
rb_ary_push(ary, line);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_each_line(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
while (!NIL_P(str = rb_io_gets_internal(argc, argv, io))) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(str);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return io;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_each_byte(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
f = fptr->f;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c == EOF) break;
|
|
|
|
rb_yield(INT2FIX(c & 0xff));
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(f)) rb_sys_fail(fptr->path);
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_getc(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
f = fptr->f;
|
|
|
|
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
|
|
|
|
if (c == EOF) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(f)) rb_sys_fail(fptr->path);
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return INT2FIX(c & 0xff);
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
int
|
|
|
|
rb_getc(f)
|
|
|
|
FILE *f;
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if (!READ_DATA_PENDING(f)) {
|
|
|
|
rb_thread_wait_fd(fileno(f));
|
|
|
|
}
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_readchar(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE c = rb_io_getc(io);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (NIL_P(c)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_eof_error();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_ungetc(io, c)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io, c;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
Check_Type(c, T_FIXNUM);
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (ungetc(FIX2INT(c), fptr->f) == EOF)
|
|
|
|
rb_sys_fail(fptr->path);
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_isatty(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if (isatty(fileno(fptr->f)) == 0)
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fptr_finalize(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
if (fptr->f != NULL) {
|
|
|
|
fclose(fptr->f);
|
|
|
|
}
|
|
|
|
if (fptr->f2 != NULL) {
|
|
|
|
fclose(fptr->f2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static void
|
|
|
|
rb_io_fptr_close(fptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
int fd;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->f == NULL && fptr->f2 == NULL) return;
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
fd = fileno(fptr->f);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (fptr->finalize) {
|
|
|
|
(*fptr->finalize)(fptr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fptr_finalize(fptr);
|
|
|
|
}
|
|
|
|
fptr->f = fptr->f2 = NULL;
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_thread_fd_close(fd);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
|
|
|
rb_io_fptr_finalize(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
rb_io_fptr_close(fptr);
|
|
|
|
if (fptr->path) {
|
|
|
|
free(fptr->path);
|
|
|
|
fptr->path = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_io_close(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_fptr_close(fptr);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (fptr->pid) {
|
|
|
|
rb_syswait(fptr->pid);
|
|
|
|
fptr->pid = 0;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_close_method(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
rb_secure(4);
|
|
|
|
rb_io_close(io);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_closed(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
fptr = RFILE(io)->fptr;
|
|
|
|
return (fptr->f || fptr->f2)?Qfalse:Qtrue;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_close_read(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
1999-08-13 09:45:20 +04:00
|
|
|
int n;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_secure(4);
|
1998-01-16 15:13:05 +03:00
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->f2 == 0 && (fptr->mode & FMODE_WRITABLE)) {
|
|
|
|
rb_raise(rb_eIOError, "closing non-duplex IO for reading");
|
|
|
|
}
|
|
|
|
if (fptr->f2 == 0) {
|
|
|
|
return rb_io_close(io);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
n = fclose(fptr->f);
|
1999-01-20 07:59:39 +03:00
|
|
|
fptr->mode &= ~FMODE_READABLE;
|
|
|
|
fptr->f = fptr->f2;
|
|
|
|
fptr->f2 = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (n != 0) rb_sys_fail(fptr->path);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_close_write(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
1999-08-13 09:45:20 +04:00
|
|
|
int n;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_secure(4);
|
1998-01-16 15:13:05 +03:00
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->f2 == 0 && (fptr->mode & FMODE_READABLE)) {
|
|
|
|
rb_raise(rb_eIOError, "closing non-duplex IO for writing");
|
|
|
|
}
|
|
|
|
if (fptr->f2 == 0) {
|
|
|
|
return rb_io_close(io);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
n = fclose(fptr->f2);
|
1999-01-20 07:59:39 +03:00
|
|
|
fptr->f2 = 0;
|
|
|
|
fptr->mode &= ~FMODE_WRITABLE;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (n != 0) rb_sys_fail(fptr->path);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_syswrite(io, str)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io, str;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
rb_secure(4);
|
|
|
|
if (TYPE(str) != T_STRING)
|
1999-01-20 07:59:39 +03:00
|
|
|
str = rb_obj_as_string(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_writable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!rb_thread_fd_writable(fileno(f))) {
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
n = write(fileno(f), RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
|
|
|
|
if (n == -1) rb_sys_fail(fptr->path);
|
|
|
|
|
|
|
|
return INT2FIX(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_sysread(io, len)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io, len;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int n, ilen;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
ilen = NUM2INT(len);
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
str = rb_str_new(0, ilen);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_thread_wait_fd(fileno(fptr->f));
|
1998-01-16 15:13:05 +03:00
|
|
|
TRAP_BEG;
|
|
|
|
n = read(fileno(fptr->f), RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
TRAP_END;
|
|
|
|
|
|
|
|
if (n == -1) rb_sys_fail(fptr->path);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (n == 0 && ilen > 0) {
|
|
|
|
rb_eof_error();
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
RSTRING(str)->len = n;
|
|
|
|
RSTRING(str)->ptr[n] = '\0';
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJ_TAINT(str);
|
|
|
|
|
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_binmode(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
#if defined(NT) || defined(DJGPP) || defined(__CYGWIN32__)\
|
|
|
|
|| defined(__human68k__) || defined(USE_CWGUSI) || defined(__EMX__)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
#ifdef __human68k__
|
|
|
|
if (fptr->f)
|
|
|
|
fmode(fptr->f, _IOBIN);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->f2)
|
1998-01-16 15:13:05 +03:00
|
|
|
fmode(fptr->f2, _IOBIN);
|
|
|
|
#else
|
1999-01-20 07:59:39 +03:00
|
|
|
# ifndef USE_CWGUSI
|
1998-01-16 15:13:05 +03:00
|
|
|
if (fptr->f && setmode(fileno(fptr->f), O_BINARY) == -1)
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
if (fptr->f2 && setmode(fileno(fptr->f2), O_BINARY) == -1)
|
|
|
|
rb_sys_fail(fptr->path);
|
1999-01-20 07:59:39 +03:00
|
|
|
# else /* USE_CWGUSI */
|
|
|
|
if (fptr->f)
|
1999-08-13 09:45:20 +04:00
|
|
|
fptr->f->mode.binary_io = 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->f2)
|
1999-08-13 09:45:20 +04:00
|
|
|
fptr->f2->mode.binary_io = 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
# endif /* USE_CWGUSI */
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
fptr->mode |= FMODE_BINMODE;
|
|
|
|
#endif
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_mode_flags(mode)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *mode;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
switch (mode[0]) {
|
|
|
|
case 'r':
|
|
|
|
flags |= FMODE_READABLE;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
flags |= FMODE_WRITABLE;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
flags |= FMODE_WRITABLE;
|
|
|
|
break;
|
|
|
|
default:
|
1999-08-13 09:45:20 +04:00
|
|
|
error:
|
|
|
|
rb_raise(rb_eArgError, "illegal access mode %s", mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode[1] == 'b') {
|
|
|
|
flags |= FMODE_BINMODE;
|
|
|
|
mode++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode[1] == '+') {
|
|
|
|
flags |= FMODE_READWRITE;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (mode[2] != 0) goto error;
|
|
|
|
}
|
|
|
|
else if (mode[1] != 0) goto error;
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rb_io_mode_flags2(mode)
|
|
|
|
int mode;
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
|
|
|
|
case O_RDONLY:
|
|
|
|
flags = FMODE_READABLE;
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
flags = FMODE_WRITABLE;
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
flags = FMODE_WRITABLE|FMODE_READABLE;
|
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef O_BINARY
|
|
|
|
if (mode & O_BINARY) {
|
|
|
|
flags |= FMODE_BINMODE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static char*
|
|
|
|
rb_io_flags_mode(flags)
|
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
static char mode[4];
|
|
|
|
char *p = mode;
|
|
|
|
|
|
|
|
switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
|
|
|
|
case O_RDONLY:
|
|
|
|
*p++ = 'r';
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
*p++ = 'w';
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
|
|
|
*p++ = 'w';
|
|
|
|
*p++ = '+';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*p++ = '\0';
|
|
|
|
#ifdef O_BINARY
|
|
|
|
if (flags & O_BINARY) {
|
|
|
|
if (mode[1] == '+') {
|
|
|
|
mode[1] = 'b'; mode[2] = '+'; mode[3] = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode[1] = 'b'; mode[2] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rb_open(fname, flag, mode)
|
|
|
|
char *fname;
|
|
|
|
int flag;
|
|
|
|
unsigned int mode;
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(fname, flag, mode);
|
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
|
|
|
fd = open(fname, flag, mode);
|
|
|
|
}
|
|
|
|
if (fd < 0) {
|
|
|
|
rb_sys_fail(fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
FILE *
|
|
|
|
rb_fopen(fname, mode)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *fname;
|
|
|
|
const char *mode;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
FILE *file;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
file = fopen(fname, mode);
|
|
|
|
if (file == NULL) {
|
1998-01-16 15:13:05 +03:00
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_gc();
|
1999-08-13 09:45:20 +04:00
|
|
|
file = fopen(fname, mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (file == NULL) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(fname);
|
|
|
|
}
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return file;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
rb_fdopen(fd, mode)
|
|
|
|
int fd;
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *mode;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
FILE *file;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
file = fdopen(fd, mode);
|
|
|
|
if (file == NULL) {
|
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
|
|
|
file = fdopen(fd, mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (file == NULL) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_file_open_internal(klass, fname, mode)
|
|
|
|
VALUE klass;
|
|
|
|
const char *fname, *mode;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
NEWOBJ(port, struct RFile);
|
|
|
|
OBJSETUP(port, klass, T_FILE);
|
|
|
|
MakeOpenFile(port, fptr);
|
|
|
|
|
|
|
|
fptr->mode = rb_io_mode_flags(mode);
|
|
|
|
fptr->f = rb_fopen(fname, mode);
|
|
|
|
fptr->path = strdup(fname);
|
|
|
|
|
|
|
|
return (VALUE)port;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_file_open(fname, mode)
|
|
|
|
const char *fname, *mode;
|
|
|
|
{
|
|
|
|
return rb_file_open_internal(rb_cFile, fname, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_file_sysopen_internal(klass, fname, flags, mode)
|
|
|
|
VALUE klass;
|
|
|
|
char *fname;
|
|
|
|
int flags, mode;
|
|
|
|
{
|
|
|
|
#ifdef USE_CWGUSI
|
|
|
|
if (mode != 0666) {
|
|
|
|
rb_warn("can't specify file mode on this platform");
|
|
|
|
}
|
|
|
|
return rb_file_open_internal(klass, fname, rb_io_flags_mode(flags));
|
|
|
|
#else
|
|
|
|
OpenFile *fptr;
|
|
|
|
int fd;
|
|
|
|
char *m;
|
|
|
|
NEWOBJ(port, struct RFile);
|
|
|
|
OBJSETUP(port, klass, T_FILE);
|
|
|
|
MakeOpenFile(port, fptr);
|
|
|
|
|
|
|
|
fd = rb_open(fname, flags, mode);
|
|
|
|
m = rb_io_flags_mode(flags);
|
|
|
|
fptr->mode = rb_io_mode_flags2(flags);
|
|
|
|
fptr->f = rb_fdopen(fd, m);
|
|
|
|
fptr->path = strdup(fname);
|
|
|
|
|
|
|
|
return (VALUE)port;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_file_sysopen(fname, flags, mode)
|
|
|
|
const char *fname;
|
|
|
|
int flags, mode;
|
|
|
|
{
|
|
|
|
return rb_file_sysopen_internal(rb_cFile, fname, flags, mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN32__) || defined(__human68k__)
|
|
|
|
static struct pipe_list {
|
|
|
|
OpenFile *fptr;
|
|
|
|
struct pipe_list *next;
|
|
|
|
} *pipe_list;
|
|
|
|
|
|
|
|
static void
|
|
|
|
pipe_add_fptr(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
struct pipe_list *list;
|
|
|
|
|
|
|
|
list = ALLOC(struct pipe_list);
|
|
|
|
list->fptr = fptr;
|
|
|
|
list->next = pipe_list;
|
|
|
|
pipe_list = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pipe_del_fptr(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
struct pipe_list *list = pipe_list;
|
|
|
|
struct pipe_list *tmp;
|
|
|
|
|
|
|
|
if (list->fptr == fptr) {
|
|
|
|
pipe_list = list->next;
|
1999-01-20 07:59:39 +03:00
|
|
|
free(list);
|
1998-01-16 15:13:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (list->next) {
|
|
|
|
if (list->next->fptr == fptr) {
|
|
|
|
tmp = list->next;
|
|
|
|
list->next = list->next->next;
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pipe_atexit()
|
|
|
|
{
|
|
|
|
struct pipe_list *list = pipe_list;
|
1999-01-20 07:59:39 +03:00
|
|
|
struct pipe_list *tmp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (list) {
|
1999-01-20 07:59:39 +03:00
|
|
|
tmp = list->next;
|
|
|
|
rb_io_fptr_finalize(list->fptr);
|
|
|
|
list = tmp;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pipe_finalize(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
#if !defined (__CYGWIN32__)
|
1998-01-16 15:13:05 +03:00
|
|
|
if (fptr->f != NULL) {
|
|
|
|
pclose(fptr->f);
|
|
|
|
}
|
|
|
|
if (fptr->f2 != NULL) {
|
|
|
|
pclose(fptr->f2);
|
|
|
|
}
|
|
|
|
fptr->f = fptr->f2 = NULL;
|
1999-01-20 07:59:39 +03:00
|
|
|
#else
|
|
|
|
fptr_finalize(fptr);
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
pipe_del_fptr(fptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_unbuffered(fptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->f2 == 0) rb_raise(rb_eTypeError, "non-writable fptr");
|
1998-01-16 15:13:05 +03:00
|
|
|
if (fptr->f != 0) setbuf(fptr->f, NULL);
|
|
|
|
setbuf(fptr->f2, NULL);
|
|
|
|
fptr->mode |= FMODE_SYNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
pipe_open(pname, mode)
|
|
|
|
char *pname, *mode;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
#ifndef USE_CWGUSI
|
|
|
|
int modef = rb_io_mode_flags(mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
#if defined(NT) || defined(DJGPP) || defined(__human68k__)
|
|
|
|
FILE *f = popen(pname, mode);
|
|
|
|
|
|
|
|
if (f == NULL) rb_sys_fail(pname);
|
|
|
|
else {
|
|
|
|
NEWOBJ(port, struct RFile);
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJSETUP(port, rb_cIO, T_FILE);
|
1998-01-16 15:13:05 +03:00
|
|
|
MakeOpenFile(port, fptr);
|
|
|
|
fptr->finalize = pipe_finalize;
|
|
|
|
fptr->mode = modef;
|
|
|
|
|
|
|
|
pipe_add_fptr(fptr);
|
|
|
|
if (modef & FMODE_READABLE) fptr->f = f;
|
|
|
|
if (modef & FMODE_WRITABLE) {
|
|
|
|
fptr->f2 = f;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_unbuffered(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return (VALUE)port;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int pid, pr[2], pw[2];
|
|
|
|
volatile int doexec;
|
|
|
|
|
|
|
|
if (((modef & FMODE_READABLE) && pipe(pr) == -1) ||
|
|
|
|
((modef & FMODE_WRITABLE) && pipe(pw) == -1))
|
|
|
|
rb_sys_fail(pname);
|
|
|
|
|
|
|
|
doexec = (strcmp("-", pname) != 0);
|
|
|
|
if (!doexec) {
|
|
|
|
fflush(stdin); /* is it really needed? */
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
retry:
|
|
|
|
switch (pid = (doexec?vfork():fork())) {
|
|
|
|
case 0: /* child */
|
|
|
|
if (modef & FMODE_READABLE) {
|
|
|
|
close(pr[0]);
|
|
|
|
if (pr[1] != 1) {
|
|
|
|
dup2(pr[1], 1);
|
|
|
|
close(pr[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (modef & FMODE_WRITABLE) {
|
|
|
|
close(pw[1]);
|
|
|
|
if (pw[0] != 0) {
|
|
|
|
dup2(pw[0], 0);
|
|
|
|
close(pw[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doexec) {
|
1998-01-16 15:19:22 +03:00
|
|
|
int fd;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
for (fd = 3; fd < NOFILE; fd++)
|
|
|
|
close(fd);
|
|
|
|
rb_proc_exec(pname);
|
|
|
|
fprintf(stderr, "%s:%d: command not found: %s\n",
|
1999-01-20 07:59:39 +03:00
|
|
|
ruby_sourcefile, ruby_sourceline, pname);
|
1998-01-16 15:13:05 +03:00
|
|
|
_exit(127);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
case -1: /* fork failed */
|
|
|
|
if (errno == EAGAIN) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_thread_sleep(1);
|
1998-01-16 15:13:05 +03:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
close(pr[0]); close(pw[1]);
|
|
|
|
rb_sys_fail(pname);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* parent */
|
1999-01-20 07:59:39 +03:00
|
|
|
if (pid < 0) rb_sys_fail(pname);
|
|
|
|
else {
|
1998-01-16 15:13:05 +03:00
|
|
|
NEWOBJ(port, struct RFile);
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJSETUP(port, rb_cIO, T_FILE);
|
1998-01-16 15:13:05 +03:00
|
|
|
MakeOpenFile(port, fptr);
|
|
|
|
fptr->mode = modef;
|
|
|
|
fptr->mode |= FMODE_SYNC;
|
|
|
|
fptr->pid = pid;
|
|
|
|
|
|
|
|
if (modef & FMODE_READABLE) {
|
|
|
|
close(pr[1]);
|
|
|
|
fptr->f = rb_fdopen(pr[0], "r");
|
|
|
|
}
|
|
|
|
if (modef & FMODE_WRITABLE) {
|
|
|
|
FILE *f = rb_fdopen(pw[1], "w");
|
|
|
|
|
|
|
|
close(pw[0]);
|
|
|
|
if (fptr->f) fptr->f2 = f;
|
|
|
|
else fptr->f = f;
|
|
|
|
}
|
|
|
|
#if defined (__CYGWIN32__)
|
1999-01-20 07:59:39 +03:00
|
|
|
fptr->finalize = pipe_finalize;
|
1998-01-16 15:13:05 +03:00
|
|
|
pipe_add_fptr(fptr);
|
|
|
|
#endif
|
|
|
|
return (VALUE)port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
#else /* USE_CWGUSI */
|
|
|
|
rb_notimplement();
|
|
|
|
return Qnil; /* not reached */
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_s_popen(argc, argv, self)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
char *mode;
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE pname, pmode, port;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
|
1998-01-16 15:13:05 +03:00
|
|
|
mode = "r";
|
|
|
|
}
|
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
int len;
|
|
|
|
|
|
|
|
mode = STR2CSTR(pmode);
|
|
|
|
len = strlen(mode);
|
|
|
|
if (len == 0 || len > 3)
|
|
|
|
rb_raise(rb_eArgError, "illegal access mode");
|
|
|
|
}
|
|
|
|
Check_SafeStr(pname);
|
1999-08-13 09:45:20 +04:00
|
|
|
port = pipe_open(RSTRING(pname)->ptr, mode);
|
|
|
|
if (NIL_P(port)) return Qnil;
|
|
|
|
return port;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_file_s_open(argc, argv, klass)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE klass;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE fname, vmode, file, perm;
|
|
|
|
char *path, *mode;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
|
|
|
|
Check_SafeStr(fname);
|
|
|
|
path = RSTRING(fname)->ptr;
|
|
|
|
|
|
|
|
if (FIXNUM_P(vmode)) {
|
|
|
|
int flags = FIX2INT(vmode);
|
|
|
|
int fmode = NIL_P(perm) ? 0666 : FIX2INT(perm);
|
|
|
|
|
|
|
|
file = rb_file_sysopen_internal(klass, path, flags, fmode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!NIL_P(vmode)) {
|
|
|
|
mode = STR2CSTR(vmode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = "r";
|
|
|
|
}
|
|
|
|
file = rb_file_open_internal(klass, RSTRING(fname)->ptr, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb_iterator_p()) {
|
|
|
|
return rb_ensure(rb_yield, file, rb_io_close, file);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
return file;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_open(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
char *mode;
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE pname, pmode, perm;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE port;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_scan_args(argc, argv, "12", &pname, &pmode, &perm);
|
1998-01-16 15:13:05 +03:00
|
|
|
Check_SafeStr(pname);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (RSTRING(pname)->ptr[0] != '|') /* open file */
|
|
|
|
return rb_file_s_open(argc, argv, rb_cFile);
|
|
|
|
|
|
|
|
/* open pipe */
|
1998-01-16 15:13:05 +03:00
|
|
|
if (NIL_P(pmode)) {
|
|
|
|
mode = "r";
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
else if (FIXNUM_P(pmode)) {
|
|
|
|
mode = rb_io_flags_mode(FIX2INT(pmode));
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
1999-01-20 07:59:39 +03:00
|
|
|
int len;
|
|
|
|
|
|
|
|
mode = STR2CSTR(pmode);
|
|
|
|
len = strlen(mode);
|
|
|
|
if (len == 0 || len > 3)
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eArgError, "illegal access mode %s", mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
port = pipe_open(RSTRING(pname)->ptr+1, mode);
|
|
|
|
if (NIL_P(port)) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_iterator_p()) {
|
|
|
|
return rb_ensure(rb_yield, port, rb_io_close, port);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return port;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
rb_io_open(fname, mode)
|
|
|
|
char *fname, *mode;
|
|
|
|
{
|
|
|
|
if (fname[0] == '|') {
|
|
|
|
return pipe_open(fname+1, mode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_file_open(fname, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_io_get_io(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
return rb_convert_type(io, T_FILE, "IO", "to_io");
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static char*
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_mode_string(fptr)
|
1998-01-16 15:19:22 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
switch (fptr->mode & FMODE_READWRITE) {
|
|
|
|
case FMODE_READABLE:
|
|
|
|
default:
|
|
|
|
return "r";
|
|
|
|
case FMODE_WRITABLE:
|
|
|
|
return "w";
|
|
|
|
case FMODE_READWRITE:
|
|
|
|
return "r+";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_reopen(io, nfile)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io, nfile;
|
|
|
|
{
|
|
|
|
OpenFile *fptr, *orig;
|
|
|
|
char *mode;
|
|
|
|
int fd;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_secure(4);
|
1998-01-16 15:13:05 +03:00
|
|
|
GetOpenFile(io, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
nfile = rb_io_get_io(nfile);
|
1998-01-16 15:13:05 +03:00
|
|
|
GetOpenFile(nfile, orig);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (fptr == orig) return io;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (orig->f2) {
|
|
|
|
fflush(orig->f2);
|
|
|
|
}
|
|
|
|
else if (orig->mode & FMODE_WRITABLE) {
|
|
|
|
fflush(orig->f);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_thread_fd_close(fileno(fptr->f));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
/* copy OpenFile structure */
|
|
|
|
fptr->mode = orig->mode;
|
|
|
|
fptr->pid = orig->pid;
|
|
|
|
fptr->lineno = orig->lineno;
|
|
|
|
if (fptr->path) free(fptr->path);
|
|
|
|
if (orig->path) fptr->path = strdup(orig->path);
|
|
|
|
else fptr->path = 0;
|
|
|
|
fptr->finalize = orig->finalize;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
mode = rb_io_mode_string(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
fd = fileno(fptr->f);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fd < 3) {
|
|
|
|
/* need to keep stdio */
|
|
|
|
if (dup2(fileno(orig->f), fd) < 0)
|
|
|
|
rb_sys_fail(orig->path);
|
|
|
|
}
|
|
|
|
else {
|
1998-01-16 15:19:22 +03:00
|
|
|
fclose(fptr->f);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (dup2(fileno(orig->f), fd) < 0)
|
|
|
|
rb_sys_fail(orig->path);
|
|
|
|
fptr->f = rb_fdopen(fd, mode);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (fptr->f2) {
|
|
|
|
fd = fileno(fptr->f2);
|
1999-01-20 07:59:39 +03:00
|
|
|
fclose(fptr->f2);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (orig->f2) {
|
1999-01-20 07:59:39 +03:00
|
|
|
if (dup2(fileno(orig->f2), fd) < 0)
|
|
|
|
rb_sys_fail(orig->path);
|
|
|
|
fptr->f2 = rb_fdopen(fd, "w");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fptr->f2 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fptr->mode & FMODE_BINMODE) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_binmode(io);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
RBASIC(io)->klass = RBASIC(nfile)->klass;
|
1998-01-16 15:13:05 +03:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
rb_file_reopen(argc, argv, file)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE file;
|
|
|
|
{
|
|
|
|
VALUE fname, nmode;
|
|
|
|
char *mode;
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
rb_secure(4);
|
|
|
|
if (rb_scan_args(argc, argv, "11", &fname, &nmode) == 1) {
|
|
|
|
if (TYPE(fname) == T_FILE) { /* fname must be IO */
|
|
|
|
return rb_io_reopen(file, fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Check_SafeStr(fname);
|
|
|
|
if (!NIL_P(nmode)) {
|
|
|
|
mode = STR2CSTR(nmode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = "r";
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(file, fptr);
|
|
|
|
if (fptr->path) free(fptr->path);
|
|
|
|
fptr->path = strdup(RSTRING(fname)->ptr);
|
|
|
|
fptr->mode = rb_io_mode_flags(mode);
|
|
|
|
if (!fptr->f) {
|
|
|
|
fptr->f = rb_fopen(RSTRING(fname)->ptr, mode);
|
|
|
|
if (fptr->f2) {
|
|
|
|
fclose(fptr->f2);
|
|
|
|
fptr->f2 = NULL;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freopen(RSTRING(fname)->ptr, mode, fptr->f) == NULL) {
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
|
|
|
if (fptr->f2) {
|
|
|
|
if (freopen(RSTRING(fname)->ptr, "w", fptr->f2) == NULL) {
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_clone(io)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr, *orig;
|
|
|
|
int fd;
|
|
|
|
char *mode;
|
|
|
|
|
|
|
|
NEWOBJ(obj, struct RFile);
|
1999-08-13 09:45:20 +04:00
|
|
|
CLONESETUP(obj, io);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, orig);
|
|
|
|
MakeOpenFile(obj, fptr);
|
|
|
|
|
|
|
|
if (orig->f2) {
|
|
|
|
fflush(orig->f2);
|
|
|
|
}
|
|
|
|
else if (orig->mode & FMODE_WRITABLE) {
|
|
|
|
fflush(orig->f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy OpenFile structure */
|
|
|
|
fptr->mode = orig->mode;
|
|
|
|
fptr->pid = orig->pid;
|
|
|
|
fptr->lineno = orig->lineno;
|
|
|
|
if (orig->path) fptr->path = strdup(orig->path);
|
|
|
|
fptr->finalize = orig->finalize;
|
|
|
|
|
|
|
|
switch (fptr->mode & FMODE_READWRITE) {
|
|
|
|
case FMODE_READABLE:
|
|
|
|
default:
|
|
|
|
mode = "r"; break;
|
|
|
|
case FMODE_WRITABLE:
|
|
|
|
mode = "w"; break;
|
|
|
|
case FMODE_READWRITE:
|
|
|
|
if (orig->f2) mode = "r";
|
|
|
|
else mode = "r+";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fd = dup(fileno(orig->f));
|
|
|
|
fptr->f = rb_fdopen(fd, mode);
|
|
|
|
if (fptr->f2) {
|
|
|
|
fd = dup(fileno(orig->f2));
|
|
|
|
fptr->f = rb_fdopen(fd, "w");
|
|
|
|
}
|
|
|
|
if (fptr->mode & FMODE_BINMODE) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_binmode((VALUE)obj);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (VALUE)obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_printf(argc, argv, out)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE argv[];
|
|
|
|
VALUE out;
|
|
|
|
{
|
1999-08-24 12:21:56 +04:00
|
|
|
io_write(out, rb_f_sprintf(argc, argv));
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-24 12:21:56 +04:00
|
|
|
rb_f_printf(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE argv[];
|
|
|
|
{
|
|
|
|
VALUE out;
|
|
|
|
|
|
|
|
if (argc == 0) return Qnil;
|
|
|
|
if (TYPE(argv[0]) == T_STRING) {
|
|
|
|
out = rb_defout;
|
|
|
|
}
|
1999-08-24 12:21:56 +04:00
|
|
|
else {
|
1998-01-16 15:13:05 +03:00
|
|
|
out = argv[0];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
1999-08-24 12:21:56 +04:00
|
|
|
io_write(out, rb_f_sprintf(argc, argv));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_print(argc, argv, out)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE out;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
int i;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE line;
|
|
|
|
|
|
|
|
/* if no argument given, print `$_' */
|
|
|
|
if (argc == 0) {
|
|
|
|
argc = 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
line = rb_lastline_get();
|
1998-01-16 15:13:05 +03:00
|
|
|
argv = &line;
|
|
|
|
}
|
|
|
|
for (i=0; i<argc; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!NIL_P(rb_output_fs) && i>0) {
|
|
|
|
rb_io_write(out, rb_output_fs);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
switch (TYPE(argv[i])) {
|
|
|
|
case T_NIL:
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_write(out, rb_str_new2("nil"));
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
default:
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_write(out, argv[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!NIL_P(rb_output_rs)) {
|
|
|
|
rb_io_write(out, rb_output_rs);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_print(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_print(argc, argv, rb_defout);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_putc(io, ch)
|
|
|
|
VALUE io, ch;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int c = NUM2CHR(ch);
|
|
|
|
|
|
|
|
rb_secure(4);
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_writable(fptr);
|
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (fputc(c, f) == EOF)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
if (fptr->mode & FMODE_SYNC)
|
|
|
|
fflush(f);
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_putc(recv, ch)
|
|
|
|
VALUE recv, ch;
|
|
|
|
{
|
|
|
|
return rb_io_putc(rb_defout, ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE rb_io_puts _((int, VALUE*, VALUE));
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
io_puts_ary(ary, out)
|
|
|
|
VALUE ary, out;
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<RARRAY(ary)->len; i++) {
|
|
|
|
tmp = RARRAY(ary)->ptr[i];
|
|
|
|
if (rb_inspecting_p(tmp)) {
|
|
|
|
tmp = rb_str_new2("[...]");
|
|
|
|
}
|
|
|
|
rb_io_puts(1, &tmp, out);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_puts(argc, argv, out)
|
1998-01-16 15:19:22 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE out;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
int i;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE line;
|
|
|
|
|
|
|
|
/* if no argument given, print newline. */
|
|
|
|
if (argc == 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_write(out, rb_default_rs);
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
switch (TYPE(argv[i])) {
|
|
|
|
case T_NIL:
|
1999-01-20 07:59:39 +03:00
|
|
|
line = rb_str_new2("nil");
|
1998-01-16 15:19:22 +03:00
|
|
|
break;
|
|
|
|
case T_ARRAY:
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_protect_inspect(io_puts_ary, argv[i], out);
|
1998-01-16 15:19:22 +03:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
line = argv[i];
|
|
|
|
break;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
line = rb_obj_as_string(line);
|
|
|
|
rb_io_write(out, line);
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RSTRING(line)->ptr[RSTRING(line)->len-1] != '\n') {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_write(out, rb_default_rs);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_puts(argc, argv)
|
1998-01-16 15:19:22 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_puts(argc, argv, rb_defout);
|
1998-01-16 15:19:22 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
|
|
|
rb_p(obj) /* for debug print within C code */
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
obj = rb_obj_as_string(rb_inspect(obj));
|
|
|
|
fwrite(RSTRING(obj)->ptr, 1, RSTRING(obj)->len, stdout);
|
|
|
|
obj = rb_default_rs;
|
|
|
|
fwrite(RSTRING(obj)->ptr, 1, RSTRING(obj)->len, stdout);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_p(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
int i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_p(argv[i]);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_obj_display(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE out;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "01", &out) == 0) {
|
|
|
|
out = rb_defout;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_io_write(out, self);
|
|
|
|
|
|
|
|
return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_defset(val, id)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE val;
|
|
|
|
ID id;
|
|
|
|
{
|
|
|
|
if (!rb_respond_to(val, id_write)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_raise(rb_eTypeError, "$> must have write method, %s given",
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_class2name(CLASS_OF(val)));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
rb_defout = val;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static int
|
|
|
|
rb_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;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static void
|
1999-08-13 09:45:20 +04:00
|
|
|
set_stdin(val, id, var)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE val;
|
|
|
|
ID id;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE *var;
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int fd;
|
1999-08-13 09:45:20 +04:00
|
|
|
char *mode;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (val == *var) return;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (TYPE(val) != T_FILE) {
|
1999-08-13 09:45:20 +04:00
|
|
|
*var = val;
|
|
|
|
return;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (TYPE(*var) != T_FILE) {
|
|
|
|
*var = orig_stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(val, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
GetOpenFile(*var, fptr);
|
1999-08-13 09:45:20 +04:00
|
|
|
mode = rb_io_mode_string(fptr);
|
|
|
|
fd = rb_dup(fileno(fptr->f));
|
|
|
|
if (fileno(fptr->f) > 2) {
|
|
|
|
fclose(fptr->f);
|
|
|
|
}
|
|
|
|
fptr->f = rb_fdopen(fd, mode);
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
GetOpenFile(val, fptr);
|
1999-08-13 09:45:20 +04:00
|
|
|
dup2(fileno(fptr->f), 0);
|
|
|
|
fclose(fptr->f);
|
|
|
|
fptr->f = stdin;
|
|
|
|
|
|
|
|
*var = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_outfile(val, var, orig, stdf)
|
|
|
|
VALUE val;
|
|
|
|
VALUE *var;
|
|
|
|
VALUE orig;
|
|
|
|
FILE *stdf;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int fd;
|
|
|
|
char *mode;
|
|
|
|
|
|
|
|
if (val == *var) return;
|
|
|
|
|
|
|
|
if (TYPE(*var) == T_FILE) {
|
|
|
|
rb_io_flush(*var);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (TYPE(val) != T_FILE) {
|
|
|
|
*var = val;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (TYPE(*var) != T_FILE) {
|
|
|
|
*var = orig;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(val, fptr);
|
|
|
|
rb_io_check_writable(fptr);
|
|
|
|
|
|
|
|
GetOpenFile(*var, fptr);
|
|
|
|
mode = rb_io_mode_string(fptr);
|
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
fd = rb_dup(fileno(f));
|
|
|
|
if (fileno(f) > 2) {
|
|
|
|
fclose(fptr->f);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
f = rb_fdopen(fd, mode);
|
|
|
|
if (fptr->f2) fptr->f2 = f;
|
|
|
|
else fptr->f = f;
|
|
|
|
|
|
|
|
GetOpenFile(val, fptr);
|
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
dup2(fileno(f), fileno(stdf));
|
|
|
|
fclose(f);
|
|
|
|
if (fptr->f2) fptr->f2 = stdf;
|
|
|
|
else fptr->f = stdf;
|
|
|
|
|
|
|
|
*var = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_stdout(val, id, var)
|
|
|
|
VALUE val;
|
|
|
|
ID id;
|
|
|
|
VALUE *var;
|
|
|
|
{
|
|
|
|
set_outfile(val, var, orig_stdout, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_stderr(val, id, var)
|
|
|
|
VALUE val;
|
|
|
|
ID id;
|
|
|
|
VALUE *var;
|
|
|
|
{
|
|
|
|
set_outfile(val, var, orig_stderr, stderr);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
prep_stdio(f, mode, klass)
|
1998-01-16 15:13:05 +03:00
|
|
|
FILE *f;
|
|
|
|
int mode;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
OpenFile *fp;
|
1999-01-20 07:59:39 +03:00
|
|
|
NEWOBJ(io, struct RFile);
|
|
|
|
OBJSETUP(io, klass, T_FILE);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
MakeOpenFile(io, fp);
|
1998-01-16 15:13:05 +03:00
|
|
|
fp->f = f;
|
|
|
|
fp->mode = mode;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
return (VALUE)io;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_s_new(argc, argv, klass)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE fnum, mode;
|
|
|
|
char *m = "r";
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_scan_args(argc, argv, "11", &fnum, &mode) == 2) {
|
1998-01-16 15:13:05 +03:00
|
|
|
Check_SafeStr(mode);
|
|
|
|
m = RSTRING(mode)->ptr;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return prep_stdio(rb_fdopen(NUM2INT(fnum), m), rb_io_mode_flags(m), klass);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static int binmode = 0;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_forward()
|
|
|
|
{
|
|
|
|
return rb_funcall3(current_file, ruby_frame->last_func,
|
|
|
|
ruby_frame->argc, ruby_frame->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_binmode()
|
|
|
|
{
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
argf_forward();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_io_binmode(current_file);
|
|
|
|
}
|
|
|
|
binmode = 1;
|
|
|
|
return argf;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static int
|
|
|
|
next_argv()
|
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
extern VALUE rb_argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
char *fn;
|
|
|
|
|
|
|
|
if (init_p == 0) {
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RARRAY(rb_argv)->len > 0) {
|
1998-01-16 15:13:05 +03:00
|
|
|
next_p = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next_p = -1;
|
1999-08-13 09:45:20 +04:00
|
|
|
current_file = rb_stdin;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
init_p = 1;
|
|
|
|
gets_lineno = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (next_p == 1) {
|
|
|
|
next_p = 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RARRAY(rb_argv)->len > 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
filename = rb_ary_shift(rb_argv);
|
1999-08-13 09:45:20 +04:00
|
|
|
fn = STR2CSTR(filename);
|
|
|
|
if (strlen(fn) == 1 && fn[0] == '-') {
|
|
|
|
current_file = rb_stdin;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ruby_inplace_mode) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_defout = rb_stdout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FILE *fr = rb_fopen(fn, "r");
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ruby_inplace_mode) {
|
1998-01-16 15:13:05 +03:00
|
|
|
struct stat st, st2;
|
|
|
|
VALUE str;
|
|
|
|
FILE *fw;
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (TYPE(rb_defout) == T_FILE && rb_defout != rb_stdout) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_close(rb_defout);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
fstat(fileno(fr), &st);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (*ruby_inplace_mode) {
|
|
|
|
str = rb_str_new2(fn);
|
1998-01-16 15:13:05 +03:00
|
|
|
#if defined(MSDOS) || defined(__CYGWIN32__) || defined(NT)
|
1999-01-20 07:59:39 +03:00
|
|
|
ruby_add_suffix(str, ruby_inplace_mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_str_cat(str, ruby_inplace_mode,
|
|
|
|
strlen(ruby_inplace_mode));
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
#if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN32__) || defined(NT) || defined(__human68k__) || defined(__EMX__)
|
1998-01-16 15:13:05 +03:00
|
|
|
(void)fclose(fr);
|
|
|
|
(void)unlink(RSTRING(str)->ptr);
|
|
|
|
(void)rename(fn, RSTRING(str)->ptr);
|
|
|
|
fr = rb_fopen(RSTRING(str)->ptr, "r");
|
|
|
|
#else
|
|
|
|
if (rename(fn, RSTRING(str)->ptr) < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_warn("Can't rename %s to %s: %s, skipping file",
|
1998-01-16 15:13:05 +03:00
|
|
|
fn, RSTRING(str)->ptr, strerror(errno));
|
|
|
|
fclose(fr);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if !defined(MSDOS) && !defined(__BOW__) && !defined(__CYGWIN32__) && !defined(NT) && !defined(__human68k__)
|
|
|
|
if (unlink(fn) < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_warn("Can't remove %s: %s, skipping file",
|
|
|
|
fn, strerror(errno));
|
1998-01-16 15:13:05 +03:00
|
|
|
fclose(fr);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#else
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_fatal("Can't do inplace edit without backup");
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
fw = rb_fopen(fn, "w");
|
1999-08-13 09:45:20 +04:00
|
|
|
#if !defined(MSDOS) && !defined(__CYGWIN32__) && !(NT) && !defined(__human68k__) && !defined(USE_CWGUSI) && !defined(__BEOS__) && !defined(__EMX__)
|
1998-01-16 15:13:05 +03:00
|
|
|
fstat(fileno(fw), &st2);
|
|
|
|
fchmod(fileno(fw), st.st_mode);
|
|
|
|
if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
|
|
|
|
fchown(fileno(fw), st.st_uid, st.st_gid);
|
|
|
|
}
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_defout = prep_stdio(fw, FMODE_WRITABLE, rb_cFile);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
current_file = prep_stdio(fr, FMODE_READABLE, rb_cFile);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (binmode) rb_io_binmode(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
init_p = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static void
|
|
|
|
any_close(file)
|
|
|
|
VALUE file;
|
|
|
|
{
|
|
|
|
if (TYPE(file) == T_FILE)
|
|
|
|
rb_io_close(file);
|
|
|
|
else
|
|
|
|
rb_funcall3(file, rb_intern("close"), 0, 0);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_gets_internal(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE line;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (!next_argv()) return Qnil;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
|
|
|
|
}
|
|
|
|
if (argc == 0 && rb_rs == rb_default_rs) {
|
|
|
|
line = rb_io_gets(current_file);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
line = rb_io_gets_internal(argc, argv, current_file);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if (NIL_P(line) && next_p != -1) {
|
1999-08-13 09:45:20 +04:00
|
|
|
any_close(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
gets_lineno++;
|
|
|
|
lineno = INT2FIX(gets_lineno);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
rb_f_gets(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE line = rb_f_gets_internal(argc, argv);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_lastline_set(line);
|
1999-01-20 07:59:39 +03:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_gets()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE line;
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (rb_rs != rb_default_rs) {
|
|
|
|
return rb_f_gets(0, 0);
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
retry:
|
|
|
|
if (!next_argv()) return Qnil;
|
1999-08-13 09:45:20 +04:00
|
|
|
line = rb_io_gets(current_file);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(line) && next_p != -1) {
|
1999-08-13 09:45:20 +04:00
|
|
|
any_close(current_file);
|
1999-01-20 07:59:39 +03:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_lastline_set(line);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!NIL_P(line)) {
|
|
|
|
gets_lineno++;
|
|
|
|
lineno = INT2FIX(gets_lineno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_readline(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE line = rb_f_gets(argc, argv);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (NIL_P(line)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_eof_error();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_getc()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_warn("getc is obsolete; use STDIN.getc instead");
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_io_getc(rb_stdin);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_readlines(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE line, ary;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new();
|
|
|
|
while (!NIL_P(line = rb_f_gets_internal(argc, argv))) {
|
|
|
|
rb_ary_push(ary, line);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_str_setter(val, id, var)
|
|
|
|
VALUE val;
|
|
|
|
ID id;
|
|
|
|
VALUE *var;
|
|
|
|
{
|
|
|
|
if (!NIL_P(val) && TYPE(val) != T_STRING) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eTypeError, "value of %s must be String", rb_id2name(id));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
*var = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_backquote(obj, str)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE obj, str;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE port, result;
|
|
|
|
|
|
|
|
Check_SafeStr(str);
|
1998-01-16 15:19:22 +03:00
|
|
|
port = pipe_open(RSTRING(str)->ptr, "r");
|
1999-08-13 09:45:20 +04:00
|
|
|
if (NIL_P(port)) return rb_str_new(0,0);
|
1998-01-16 15:13:05 +03:00
|
|
|
result = read_all(port);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_close(port);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(result)) return rb_str_new(0,0);
|
1998-01-16 15:13:05 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_select(argc, argv, obj)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE read, write, except, timeout, res, list;
|
|
|
|
fd_set rset, wset, eset, pset;
|
|
|
|
fd_set *rp, *wp, *ep;
|
|
|
|
struct timeval *tp, timerec;
|
|
|
|
OpenFile *fptr;
|
|
|
|
int i, max = 0, n;
|
1999-01-20 07:59:39 +03:00
|
|
|
int interrupt_flag = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
int pending = 0;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "13", &read, &write, &except, &timeout);
|
|
|
|
if (NIL_P(timeout)) {
|
|
|
|
tp = NULL;
|
|
|
|
}
|
|
|
|
else {
|
1999-08-13 09:45:20 +04:00
|
|
|
timerec = rb_time_interval(timeout);
|
1998-01-16 15:13:05 +03:00
|
|
|
tp = &timerec;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_ZERO(&pset);
|
|
|
|
if (!NIL_P(read)) {
|
|
|
|
Check_Type(read, T_ARRAY);
|
|
|
|
rp = &rset;
|
|
|
|
FD_ZERO(rp);
|
|
|
|
for (i=0; i<RARRAY(read)->len; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(read)->ptr[i]), fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
FD_SET(fileno(fptr->f), rp);
|
|
|
|
if (READ_DATA_PENDING(fptr->f)) { /* check for buffered data */
|
|
|
|
pending++;
|
|
|
|
FD_SET(fileno(fptr->f), &pset);
|
|
|
|
}
|
|
|
|
if (max < fileno(fptr->f)) max = fileno(fptr->f);
|
|
|
|
}
|
|
|
|
if (pending) { /* no blocking if there's buffered data */
|
|
|
|
timerec.tv_sec = timerec.tv_usec = 0;
|
|
|
|
tp = &timerec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rp = NULL;
|
|
|
|
|
|
|
|
if (!NIL_P(write)) {
|
|
|
|
Check_Type(write, T_ARRAY);
|
|
|
|
wp = &wset;
|
|
|
|
FD_ZERO(wp);
|
|
|
|
for (i=0; i<RARRAY(write)->len; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(write)->ptr[i]), fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
FD_SET(fileno(fptr->f), wp);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (max < fileno(fptr->f)) max = fileno(fptr->f);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (fptr->f2) {
|
|
|
|
FD_SET(fileno(fptr->f2), wp);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (max < fileno(fptr->f2)) max = fileno(fptr->f2);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wp = NULL;
|
|
|
|
|
|
|
|
if (!NIL_P(except)) {
|
|
|
|
Check_Type(except, T_ARRAY);
|
|
|
|
ep = &eset;
|
|
|
|
FD_ZERO(ep);
|
|
|
|
for (i=0; i<RARRAY(except)->len; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(except)->ptr[i]), fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
FD_SET(fileno(fptr->f), ep);
|
|
|
|
if (max < fileno(fptr->f)) max = fileno(fptr->f);
|
|
|
|
if (fptr->f2) {
|
|
|
|
FD_SET(fileno(fptr->f2), ep);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (max < fileno(fptr->f2)) max = fileno(fptr->f2);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ep = NULL;
|
|
|
|
|
|
|
|
max++;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
n = rb_thread_select(max, rp, wp, ep, tp);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (n < 0) {
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
if (!pending && n == 0) return Qnil; /* returns nil on timeout */
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
res = rb_ary_new2(3);
|
|
|
|
rb_ary_push(res, rp?rb_ary_new():rb_ary_new2(0));
|
|
|
|
rb_ary_push(res, wp?rb_ary_new():rb_ary_new2(0));
|
|
|
|
rb_ary_push(res, ep?rb_ary_new():rb_ary_new2(0));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (interrupt_flag == 0) {
|
1998-01-16 15:13:05 +03:00
|
|
|
if (rp) {
|
|
|
|
list = RARRAY(res)->ptr[0];
|
|
|
|
for (i=0; i< RARRAY(read)->len; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(read)->ptr[i]), fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (FD_ISSET(fileno(fptr->f), rp)
|
|
|
|
|| FD_ISSET(fileno(fptr->f), &pset)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(list, RARRAY(read)->ptr[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wp) {
|
|
|
|
list = RARRAY(res)->ptr[1];
|
|
|
|
for (i=0; i< RARRAY(write)->len; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(write)->ptr[i]), fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (FD_ISSET(fileno(fptr->f), wp)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(list, RARRAY(write)->ptr[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else if (fptr->f2 && FD_ISSET(fileno(fptr->f2), wp)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(list, RARRAY(write)->ptr[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ep) {
|
|
|
|
list = RARRAY(res)->ptr[2];
|
|
|
|
for (i=0; i< RARRAY(except)->len; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(except)->ptr[i]), fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (FD_ISSET(fileno(fptr->f), ep)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(list, RARRAY(except)->ptr[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else if (fptr->f2 && FD_ISSET(fileno(fptr->f2), ep)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(list, RARRAY(except)->ptr[i]);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res; /* returns an empty array on interrupt */
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_ctl(io, req, arg, io_p)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE io, req, arg;
|
1998-01-16 15:13:05 +03:00
|
|
|
int io_p;
|
|
|
|
{
|
|
|
|
#if !defined(MSDOS) && !defined(__human68k__)
|
|
|
|
int cmd = NUM2INT(req);
|
|
|
|
OpenFile *fptr;
|
1999-01-20 07:59:39 +03:00
|
|
|
int len = 0;
|
|
|
|
int fd;
|
1998-01-16 15:13:05 +03:00
|
|
|
long narg = 0;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
rb_secure(2);
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(arg) || arg == Qfalse) {
|
1998-01-16 15:13:05 +03:00
|
|
|
narg = 0;
|
|
|
|
}
|
|
|
|
else if (FIXNUM_P(arg)) {
|
|
|
|
narg = FIX2INT(arg);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (arg == Qtrue) {
|
1998-01-16 15:13:05 +03:00
|
|
|
narg = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(arg, T_STRING);
|
|
|
|
|
|
|
|
#ifdef IOCPARM_MASK
|
|
|
|
#ifndef IOCPARM_LEN
|
|
|
|
#define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef IOCPARM_LEN
|
|
|
|
len = IOCPARM_LEN(cmd); /* on BSDish systems we're safe */
|
|
|
|
#else
|
|
|
|
len = 256; /* otherwise guess at what's safe */
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_str_modify(arg);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (len <= RSTRING(arg)->len) {
|
|
|
|
len = RSTRING(arg)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RSTRING(arg)->len < len) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_str_resize(arg, len+1);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
RSTRING(arg)->ptr[len] = 17; /* a little sanity check here */
|
|
|
|
narg = (long)RSTRING(arg)->ptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
fd = fileno(fptr->f);
|
|
|
|
#ifdef HAVE_FCNTL
|
1999-01-20 07:59:39 +03:00
|
|
|
TRAP_BEG;
|
|
|
|
# ifdef USE_CWGUSI
|
|
|
|
retval = io_p?ioctl(fd, cmd, (void*) narg):fcntl(fd, cmd, narg);
|
|
|
|
# else
|
1998-01-16 15:13:05 +03:00
|
|
|
retval = io_p?ioctl(fd, cmd, narg):fcntl(fd, cmd, narg);
|
1999-01-20 07:59:39 +03:00
|
|
|
# endif
|
|
|
|
TRAP_END;
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
if (!io_p) {
|
|
|
|
rb_notimplement();
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
TRAP_BEG;
|
1998-01-16 15:13:05 +03:00
|
|
|
retval = ioctl(fd, cmd, narg);
|
1999-01-20 07:59:39 +03:00
|
|
|
TRAP_END;
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
if (retval < 0) rb_sys_fail(fptr->path);
|
1998-01-16 15:19:22 +03:00
|
|
|
if (TYPE(arg) == T_STRING && RSTRING(arg)->ptr[len] != 17) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "return value overflowed string");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return INT2NUM(retval);
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_ioctl(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE req, arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &req, &arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_io_ctl(io, req, arg, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_fcntl(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FCNTL
|
|
|
|
VALUE req, arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &req, &arg);
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_io_ctl(io, req, arg, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_f_syscall(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
#ifdef HAVE_SYSCALL
|
|
|
|
#ifdef atarist
|
|
|
|
unsigned long arg[14]; /* yes, we really need that many ! */
|
|
|
|
#else
|
|
|
|
unsigned long arg[8];
|
|
|
|
#endif
|
|
|
|
int retval = -1;
|
|
|
|
int i = 1;
|
|
|
|
int items = argc - 1;
|
|
|
|
|
|
|
|
/* This probably won't work on machines where sizeof(long) != sizeof(int)
|
|
|
|
* or where sizeof(long) != sizeof(char*). But such machines will
|
|
|
|
* not likely have syscall implemented either, so who cares?
|
|
|
|
*/
|
|
|
|
|
|
|
|
rb_secure(2);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (argc == 0)
|
|
|
|
rb_raise(rb_eArgError, "too few arguments for syscall");
|
1998-01-16 15:13:05 +03:00
|
|
|
arg[0] = NUM2INT(argv[0]); argv++;
|
|
|
|
while (items--) {
|
|
|
|
if (FIXNUM_P(*argv)) {
|
|
|
|
arg[i] = (unsigned long)NUM2INT(*argv); argv++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Check_Type(*argv, T_STRING);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_str_modify(*argv);
|
1998-01-16 15:13:05 +03:00
|
|
|
arg[i] = (unsigned long)RSTRING(*argv)->ptr; argv++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
TRAP_BEG;
|
1998-01-16 15:13:05 +03:00
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
|
|
|
retval = syscall(arg[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
retval = syscall(arg[0],arg[1]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2]);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3]);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4]);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
|
|
|
|
arg[7]);
|
|
|
|
break;
|
|
|
|
#ifdef atarist
|
|
|
|
case 9:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
|
|
|
|
arg[7], arg[8]);
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
|
|
|
|
arg[7], arg[8], arg[9]);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
|
|
|
|
arg[7], arg[8], arg[9], arg[10]);
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
|
|
|
|
arg[7], arg[8], arg[9], arg[10], arg[11]);
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
|
|
|
|
arg[7], arg[8], arg[9], arg[10], arg[11], arg[12]);
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
retval = syscall(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
|
|
|
|
arg[7], arg[8], arg[9], arg[10], arg[11], arg[12], arg[13]);
|
|
|
|
break;
|
|
|
|
#endif /* atarist */
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
TRAP_END;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (retval < 0) rb_sys_fail(0);
|
|
|
|
return INT2NUM(retval);
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_s_pipe()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
#ifndef __human68k__
|
|
|
|
int pipes[2];
|
|
|
|
VALUE r, w, ary;
|
|
|
|
|
|
|
|
#ifdef NT
|
|
|
|
if (_pipe(pipes, 1024, O_BINARY) == -1)
|
|
|
|
#else
|
|
|
|
if (pipe(pipes) == -1)
|
|
|
|
#endif
|
|
|
|
rb_sys_fail(0);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
r = prep_stdio(rb_fdopen(pipes[0], "r"), FMODE_READABLE, rb_cIO);
|
|
|
|
w = prep_stdio(rb_fdopen(pipes[1], "w"), FMODE_WRITABLE, rb_cIO);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new2(2);
|
|
|
|
rb_ary_push(ary, r);
|
|
|
|
rb_ary_push(ary, w);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return ary;
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct foreach_arg {
|
|
|
|
int argc;
|
|
|
|
VALUE sep;
|
|
|
|
VALUE io;
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_foreach_line(arg)
|
1998-01-16 15:13:05 +03:00
|
|
|
struct foreach_arg *arg;
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
while (!NIL_P(str = rb_io_gets_internal(arg->argc, &arg->sep, arg->io))) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(str);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_s_foreach(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE fname;
|
1998-01-16 15:13:05 +03:00
|
|
|
struct foreach_arg arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &fname, &arg.sep);
|
|
|
|
Check_SafeStr(fname);
|
|
|
|
|
|
|
|
arg.argc = argc - 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
arg.io = rb_io_open(RSTRING(fname)->ptr, "r");
|
1999-08-13 09:45:20 +04:00
|
|
|
if (NIL_P(arg.io)) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ensure(rb_io_foreach_line, (VALUE)&arg, rb_io_close, arg.io);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_readline_line(arg)
|
1998-01-16 15:13:05 +03:00
|
|
|
struct foreach_arg *arg;
|
|
|
|
{
|
|
|
|
VALUE line, ary;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new();
|
|
|
|
while (!NIL_P(line = rb_io_gets_internal(arg->argc, &arg->sep, arg->io))) {
|
|
|
|
rb_ary_push(ary, line);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_s_readlines(argc, argv, io)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE fname;
|
1998-01-16 15:13:05 +03:00
|
|
|
struct foreach_arg arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &fname, &arg.sep);
|
|
|
|
Check_SafeStr(fname);
|
|
|
|
|
|
|
|
arg.argc = argc - 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
arg.io = rb_io_open(RSTRING(fname)->ptr, "r");
|
1999-08-13 09:45:20 +04:00
|
|
|
if (NIL_P(arg.io)) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ensure(rb_io_readline_line, (VALUE)&arg, rb_io_close, arg.io);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_tell()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!next_argv()) {
|
|
|
|
rb_raise(rb_eArgError, "no stream to tell");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
return rb_io_tell(current_file);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_seek(self, offset, ptrname)
|
|
|
|
VALUE self, offset, ptrname;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!next_argv()) {
|
|
|
|
rb_raise(rb_eArgError, "no stream to seek");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
return rb_io_seek(current_file, offset, ptrname);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_set_pos(self, offset)
|
|
|
|
VALUE self, offset;
|
|
|
|
{
|
|
|
|
if (!next_argv()) {
|
|
|
|
rb_raise(rb_eArgError, "no stream to pos");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
return rb_io_set_pos(current_file, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_rewind()
|
|
|
|
{
|
|
|
|
if (!next_argv()) {
|
|
|
|
rb_raise(rb_eArgError, "no stream to rewind");
|
|
|
|
}
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
return rb_io_rewind(current_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_fileno()
|
|
|
|
{
|
|
|
|
if (!next_argv()) {
|
|
|
|
rb_raise(rb_eArgError, "no stream");
|
|
|
|
}
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
return rb_io_fileno(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_to_io()
|
|
|
|
{
|
|
|
|
return current_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_read(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE tmp, str;
|
1999-08-13 09:45:20 +04:00
|
|
|
int len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (argc == 1) len = NUM2INT(argv[0]);
|
|
|
|
str = Qnil;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (!next_argv()) return str;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
tmp = argf_forward();
|
|
|
|
STR2CSTR(tmp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmp = io_read(argc, argv, current_file);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if (NIL_P(tmp) && next_p != -1) {
|
1999-08-13 09:45:20 +04:00
|
|
|
any_close(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(tmp) || RSTRING(tmp)->len == 0) return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
else if (NIL_P(str)) str = tmp;
|
1999-01-20 07:59:39 +03:00
|
|
|
else rb_str_cat(str, RSTRING(tmp)->ptr, RSTRING(tmp)->len);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (argc == 0) {
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (RSTRING(tmp)->len < len) {
|
|
|
|
len -= RSTRING(tmp)->len;
|
|
|
|
argv[0] = INT2FIX(len);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_getc()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE byte;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (!next_argv()) return Qnil;
|
1999-08-13 09:45:20 +04:00
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
byte = rb_funcall3(current_file, rb_intern("getc"), 0, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
byte = rb_io_getc(current_file);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if (NIL_P(byte) && next_p != -1) {
|
1999-08-13 09:45:20 +04:00
|
|
|
any_close(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_readchar()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE c = argf_getc();
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (NIL_P(c)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_eof_error();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_eof()
|
|
|
|
{
|
|
|
|
if (init_p == 0 && !next_argv())
|
|
|
|
return Qtrue;
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
if (rb_io_eof(current_file)) {
|
|
|
|
next_p = 1;
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_each_line(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
while (RTEST(str = rb_f_gets_internal(argc, argv))) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(str);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return argf;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_each_byte()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE byte;
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
while (!NIL_P(byte = argf_getc())) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(byte);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_filename()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_file()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
return current_file;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_skip()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (next_p != -1) {
|
1999-08-13 09:45:20 +04:00
|
|
|
any_close(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
next_p = 1;
|
|
|
|
}
|
|
|
|
return argf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_close()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
any_close(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (next_p != -1) {
|
|
|
|
next_p = 1;
|
|
|
|
}
|
|
|
|
gets_lineno = 0;
|
|
|
|
return argf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
argf_closed()
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
return rb_io_closed(current_file);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
opt_i_get()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!ruby_inplace_mode) return Qnil;
|
|
|
|
return rb_str_new2(ruby_inplace_mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opt_i_set(val)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE val;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!RTEST(val)) {
|
|
|
|
ruby_inplace_mode = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
return;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
ruby_inplace_mode = STR2CSTR(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_IO()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_eIOError = rb_define_class("IOError", rb_eStandardError);
|
|
|
|
rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
id_write = rb_intern("write");
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_global_function("syscall", rb_f_syscall, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("open", rb_f_open, -1);
|
1999-08-24 12:21:56 +04:00
|
|
|
rb_define_global_function("printf", rb_f_printf, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_global_function("print", rb_f_print, -1);
|
|
|
|
rb_define_global_function("putc", rb_f_putc, 1);
|
|
|
|
rb_define_global_function("puts", rb_f_puts, -1);
|
|
|
|
rb_define_global_function("gets", rb_f_gets, -1);
|
|
|
|
rb_define_global_function("readline", rb_f_readline, -1);
|
|
|
|
rb_define_global_function("getc", rb_f_getc, 0);
|
|
|
|
rb_define_global_function("select", rb_f_select, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("readlines", rb_f_readlines, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("`", rb_f_backquote, 1);
|
|
|
|
|
|
|
|
rb_define_global_function("p", rb_f_p, -1);
|
|
|
|
rb_define_method(rb_mKernel, "display", rb_obj_display, -1);
|
|
|
|
|
|
|
|
rb_cIO = rb_define_class("IO", rb_cObject);
|
|
|
|
rb_include_module(rb_cIO, rb_mEnumerable);
|
|
|
|
|
|
|
|
rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1);
|
|
|
|
rb_define_singleton_method(rb_cIO, "popen", rb_io_s_popen, -1);
|
|
|
|
rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1);
|
|
|
|
rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
|
|
|
|
rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_fs = rb_output_fs = Qnil;
|
|
|
|
rb_define_hooked_variable("$;", &rb_fs, 0, rb_str_setter);
|
|
|
|
rb_define_hooked_variable("$-F", &rb_fs, 0, rb_str_setter);
|
|
|
|
rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
|
|
|
|
|
|
|
|
rb_rs = rb_default_rs = rb_str_new2("\n"); rb_output_rs = Qnil;
|
|
|
|
rb_global_variable(&rb_default_rs);
|
|
|
|
rb_str_freeze(rb_default_rs); /* avoid modifying RS_default */
|
|
|
|
rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
|
|
|
|
rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
|
|
|
|
rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
|
|
|
|
|
|
|
|
rb_define_hooked_variable("$.", &lineno, 0, lineno_setter);
|
|
|
|
rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "clone", rb_io_clone, 0);
|
|
|
|
rb_define_method(rb_cIO, "reopen", rb_io_reopen, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "print", rb_io_print, -1);
|
|
|
|
rb_define_method(rb_cIO, "putc", rb_io_putc, 1);
|
|
|
|
rb_define_method(rb_cIO, "puts", rb_io_puts, -1);
|
|
|
|
rb_define_method(rb_cIO, "printf", rb_io_printf, -1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "each", rb_io_each_line, -1);
|
|
|
|
rb_define_method(rb_cIO, "each_line", rb_io_each_line, -1);
|
|
|
|
rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
|
|
|
|
rb_define_method(rb_cIO, "sysread", rb_io_sysread, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
|
|
|
|
rb_define_alias(rb_cIO, "to_i", "fileno");
|
|
|
|
rb_define_method(rb_cIO, "to_io", rb_io_to_io, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "sync", rb_io_sync, 0);
|
|
|
|
rb_define_method(rb_cIO, "sync=", rb_io_set_sync, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "lineno", rb_io_lineno, 0);
|
|
|
|
rb_define_method(rb_cIO, "lineno=", rb_io_set_lineno, 1);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "readlines", rb_io_readlines, -1);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_cIO, "read", io_read, -1);
|
|
|
|
rb_define_method(rb_cIO, "write", io_write, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cIO, "gets", rb_io_gets_method, -1);
|
|
|
|
rb_define_method(rb_cIO, "readline", rb_io_readline, -1);
|
|
|
|
rb_define_method(rb_cIO, "getc", rb_io_getc, 0);
|
|
|
|
rb_define_method(rb_cIO, "readchar", rb_io_readchar, 0);
|
|
|
|
rb_define_method(rb_cIO, "ungetc",rb_io_ungetc, 1);
|
|
|
|
rb_define_method(rb_cIO, "<<", rb_io_addstr, 1);
|
|
|
|
rb_define_method(rb_cIO, "flush", rb_io_flush, 0);
|
|
|
|
rb_define_method(rb_cIO, "tell", rb_io_tell, 0);
|
|
|
|
rb_define_method(rb_cIO, "seek", rb_io_seek, 2);
|
|
|
|
rb_define_const(rb_cIO, "SEEK_SET", SEEK_SET);
|
|
|
|
rb_define_const(rb_cIO, "SEEK_CUR", SEEK_CUR);
|
|
|
|
rb_define_const(rb_cIO, "SEEK_END", SEEK_END);
|
|
|
|
rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
|
|
|
|
rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
|
|
|
|
rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
|
|
|
|
rb_define_method(rb_cIO, "eof", rb_io_eof, 0);
|
|
|
|
rb_define_method(rb_cIO, "eof?", rb_io_eof, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "close", rb_io_close_method, 0);
|
|
|
|
rb_define_method(rb_cIO, "closed?", rb_io_closed, 0);
|
|
|
|
rb_define_method(rb_cIO, "close_read", rb_io_close_read, 0);
|
|
|
|
rb_define_method(rb_cIO, "close_write", rb_io_close_write, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "isatty", rb_io_isatty, 0);
|
|
|
|
rb_define_method(rb_cIO, "tty?", rb_io_isatty, 0);
|
|
|
|
rb_define_method(rb_cIO, "binmode", rb_io_binmode, 0);
|
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
|
|
|
|
rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_stdin = orig_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO);
|
|
|
|
rb_define_hooked_variable("$stdin", &rb_stdin, 0, set_stdin);
|
|
|
|
rb_stdout = orig_stdout = prep_stdio(stdout, FMODE_WRITABLE, rb_cIO);
|
|
|
|
rb_define_hooked_variable("$stdout", &rb_stdout, 0, set_stdout);
|
|
|
|
rb_stderr = orig_stderr = prep_stdio(stderr, FMODE_WRITABLE, rb_cIO);
|
|
|
|
rb_define_hooked_variable("$stderr", &rb_stderr, 0, set_stderr);
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_defout = rb_stdout;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_hooked_variable("$>", &rb_defout, 0, rb_io_defset);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
rb_define_global_const("STDIN", rb_stdin);
|
|
|
|
rb_define_global_const("STDOUT", rb_stdout);
|
|
|
|
rb_define_global_const("STDERR", rb_stderr);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
argf = rb_obj_alloc(rb_cObject);
|
|
|
|
rb_extend_object(argf, rb_mEnumerable);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
rb_define_readonly_variable("$<", &argf);
|
|
|
|
rb_define_global_const("ARGF", argf);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_singleton_method(argf, "fileno", argf_fileno, 0);
|
|
|
|
rb_define_singleton_method(argf, "to_i", argf_fileno, 0);
|
|
|
|
rb_define_singleton_method(argf, "to_io", argf_to_io, 0);
|
|
|
|
rb_define_singleton_method(argf, "each", argf_each_line, -1);
|
|
|
|
rb_define_singleton_method(argf, "each_line", argf_each_line, -1);
|
|
|
|
rb_define_singleton_method(argf, "each_byte", argf_each_byte, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_singleton_method(argf, "read", argf_read, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_singleton_method(argf, "readlines", rb_f_readlines, -1);
|
|
|
|
rb_define_singleton_method(argf, "to_a", rb_f_readlines, -1);
|
|
|
|
rb_define_singleton_method(argf, "gets", rb_f_gets, -1);
|
|
|
|
rb_define_singleton_method(argf, "readline", rb_f_readline, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_singleton_method(argf, "getc", argf_getc, 0);
|
|
|
|
rb_define_singleton_method(argf, "readchar", argf_readchar, 0);
|
|
|
|
rb_define_singleton_method(argf, "tell", argf_tell, 0);
|
|
|
|
rb_define_singleton_method(argf, "seek", argf_seek, 2);
|
|
|
|
rb_define_singleton_method(argf, "rewind", argf_rewind, 0);
|
|
|
|
rb_define_singleton_method(argf, "pos", argf_tell, 0);
|
|
|
|
rb_define_singleton_method(argf, "pos=", argf_set_pos, 1);
|
|
|
|
rb_define_singleton_method(argf, "eof", argf_eof, 0);
|
|
|
|
rb_define_singleton_method(argf, "eof?", argf_eof, 0);
|
|
|
|
rb_define_singleton_method(argf, "binmode", argf_binmode, 0);
|
|
|
|
|
|
|
|
rb_define_singleton_method(argf, "to_s", argf_filename, 0);
|
|
|
|
rb_define_singleton_method(argf, "filename", argf_filename, 0);
|
|
|
|
rb_define_singleton_method(argf, "file", argf_file, 0);
|
|
|
|
rb_define_singleton_method(argf, "skip", argf_skip, 0);
|
|
|
|
rb_define_singleton_method(argf, "close", argf_close, 0);
|
|
|
|
rb_define_singleton_method(argf, "closed?", argf_closed, 0);
|
|
|
|
|
|
|
|
rb_define_singleton_method(argf, "lineno", argf_lineno, 0);
|
|
|
|
rb_define_singleton_method(argf, "lineno=", argf_set_lineno, 1);
|
|
|
|
|
|
|
|
current_file = rb_stdin;
|
|
|
|
rb_global_variable(¤t_file);
|
1999-01-20 07:59:39 +03:00
|
|
|
filename = rb_str_new2("-");
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_define_readonly_variable("$FILENAME", &filename);
|
|
|
|
|
|
|
|
rb_define_virtual_variable("$-i", opt_i_get, opt_i_set);
|
|
|
|
|
|
|
|
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN32__) || defined(__human68k__)
|
|
|
|
atexit(pipe_atexit);
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
Init_File();
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
rb_define_method(rb_cFile, "reopen", rb_file_reopen, -1);
|
|
|
|
|
|
|
|
rb_define_singleton_method(rb_cFile, "new", rb_file_s_open, -1);
|
|
|
|
rb_define_singleton_method(rb_cFile, "open", rb_file_s_open, -1);
|
|
|
|
|
|
|
|
rb_file_const("RDONLY", INT2FIX(O_RDONLY));
|
|
|
|
rb_file_const("WRONLY", INT2FIX(O_WRONLY));
|
|
|
|
rb_file_const("RDWR", INT2FIX(O_RDWR));
|
|
|
|
rb_file_const("APPEND", INT2FIX(O_APPEND));
|
|
|
|
rb_file_const("CREAT", INT2FIX(O_CREAT));
|
|
|
|
rb_file_const("EXCL", INT2FIX(O_EXCL));
|
|
|
|
#if defined(O_NDELAY) || defined(O_NONBLOCK)
|
|
|
|
# ifdef O_NONBLOCK
|
|
|
|
rb_file_const("NONBLOCK", INT2FIX(O_NONBLOCK));
|
|
|
|
# else
|
|
|
|
rb_file_const("NONBLOCK", INT2FIX(O_NDELAY));
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
rb_file_const("TRUNC", INT2FIX(O_TRUNC));
|
|
|
|
#ifdef O_NOCTTY
|
|
|
|
rb_file_const("NOCTTY", INT2FIX(O_NOCTTY));
|
|
|
|
#endif
|
|
|
|
#ifdef O_BINARY
|
|
|
|
rb_file_const("BINARY", INT2FIX(O_BINARY));
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|