2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
io.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Oct 15 18:08:59 JST 1993
|
|
|
|
|
2002-01-11 12:18:54 +03:00
|
|
|
Copyright (C) 1993-2002 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-03-22 10:26:42 +03:00
|
|
|
#if defined(__VMS)
|
|
|
|
#define _XOPEN_SOURCE
|
|
|
|
#define _POSIX_C_SOURCE 2
|
|
|
|
#endif
|
|
|
|
|
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>
|
|
|
|
|
2000-09-04 12:24:09 +04:00
|
|
|
#if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(NT) || defined(__human68k__) || defined(__EMX__) || defined(__BEOS__)
|
2000-08-25 12:26:06 +04:00
|
|
|
# define NO_SAFE_RENAME
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MSDOS) || defined(__CYGWIN__) || defined(NT)
|
|
|
|
# define NO_LONG_FNAME
|
|
|
|
#endif
|
|
|
|
|
2001-09-05 10:54:57 +04:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(sun) || defined(_nec_ews)
|
2001-05-02 08:22:21 +04:00
|
|
|
# define USE_SETVBUF
|
|
|
|
#endif
|
|
|
|
|
2001-08-06 07:05:23 +04:00
|
|
|
#ifdef __QNXNTO__
|
|
|
|
#include "unix.h"
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
#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
|
|
|
|
|
2002-03-14 09:23:46 +03:00
|
|
|
#if !HAVE_OFF_T && !defined(off_t)
|
|
|
|
# define off_t long
|
|
|
|
#endif
|
|
|
|
#if !HAVE_FSEEKO && !defined(fseeko)
|
|
|
|
# define fseeko fseek
|
|
|
|
#endif
|
|
|
|
#if !HAVE_FTELLO && !defined(ftello)
|
|
|
|
# define ftello ftell
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
#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
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2002-05-02 11:50:36 +04:00
|
|
|
/* EMX has sys/param.h, but.. */
|
|
|
|
#if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
|
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
|
|
|
extern void Init_File _((void));
|
|
|
|
|
|
|
|
#ifdef __BEOS__
|
2000-05-16 06:46:57 +04:00
|
|
|
# ifndef NOFILE
|
1999-08-13 09:45:20 +04:00
|
|
|
# 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
|
|
|
|
2002-03-27 08:28:00 +03:00
|
|
|
#if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
|
|
|
|
# error off_t is bigger than long, but you have no long long...
|
|
|
|
#endif
|
|
|
|
|
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;
|
2001-02-26 08:29:06 +03:00
|
|
|
static int saved_fd[3] = {0, 1, 2};
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
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;
|
2000-08-15 10:22:49 +04:00
|
|
|
static int init_p = 0, next_p = 0, first_p = 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
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)
|
2001-07-24 13:07:33 +04:00
|
|
|
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_IO_read_end - (fp)->_IO_read_ptr)
|
2002-03-25 17:50:40 +03:00
|
|
|
# define READ_DATA_PENDING_PTR(fp) ((fp)->_IO_read_ptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
# else
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
|
2001-07-24 13:07:33 +04:00
|
|
|
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_egptr - (fp)->_gptr)
|
2002-03-25 17:50:40 +03:00
|
|
|
# define READ_DATA_PENDING_PTR(fp) ((fp)->_gptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
# endif
|
1999-01-20 07:59:39 +03:00
|
|
|
#elif defined(FILE_COUNT)
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
|
2001-07-24 13:07:33 +04:00
|
|
|
# define READ_DATA_PENDING_COUNT(fp) ((fp)->FILE_COUNT)
|
2002-03-25 17:50:40 +03:00
|
|
|
#elif defined(FILE_READEND)
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
|
|
|
|
# define READ_DATA_PENDING_COUNT(fp) ((fp)->FILE_READEND - (fp)->FILE_READPTR)
|
1999-01-20 07:59:39 +03:00
|
|
|
#elif defined(__BEOS__)
|
2000-05-24 08:34:26 +04:00
|
|
|
# define READ_DATA_PENDING(fp) (fp->_state._eof == 0)
|
2002-03-22 10:26:42 +03:00
|
|
|
#elif defined(__VMS)
|
|
|
|
# define READ_DATA_PENDING(fp) (((unsigned int)((*(fp))->_flag) & _IOEOF) == 0)
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
/* requires systems own version of the ReadDataPending() */
|
|
|
|
extern int ReadDataPending();
|
2002-03-27 08:28:00 +03:00
|
|
|
# define READ_DATA_PENDING(fp) (!feof(fp))
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
2002-03-25 17:50:40 +03:00
|
|
|
#ifndef READ_DATA_PENDING_PTR
|
|
|
|
# ifdef FILE_READPTR
|
|
|
|
# define READ_DATA_PENDING_PTR(fp) ((fp)->FILE_READPTR)
|
|
|
|
# endif
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-11-10 10:16:52 +03:00
|
|
|
VALUE
|
|
|
|
rb_io_taint_check(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
if (!OBJ_TAINTED(io) && rb_safe_level() >= 4)
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: operation on untainted IO");
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
if (!fptr->f && !fptr->f2) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eIOError, "closed stream");
|
2000-07-06 11:21:26 +04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2000-01-17 11:37:53 +03:00
|
|
|
int
|
|
|
|
rb_read_pending(fp)
|
1999-08-13 09:45:20 +04:00
|
|
|
FILE *fp;
|
|
|
|
{
|
2000-01-17 11:37:53 +03:00
|
|
|
return READ_DATA_PENDING(fp);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
2000-02-01 06:18:03 +03:00
|
|
|
void
|
|
|
|
rb_read_check(fp)
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
if (!READ_DATA_PENDING(fp)) {
|
|
|
|
rb_thread_wait_fd(fileno(fp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
static int
|
2001-07-14 19:17:19 +04:00
|
|
|
ruby_dup(orig)
|
2000-05-12 13:07:57 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-11-08 08:29:37 +03:00
|
|
|
static void
|
2002-03-29 17:50:09 +03:00
|
|
|
io_fflush(f, fptr)
|
2000-11-08 08:29:37 +03:00
|
|
|
FILE *f;
|
2002-03-29 17:50:09 +03:00
|
|
|
OpenFile *fptr;
|
2000-11-08 08:29:37 +03:00
|
|
|
{
|
2000-11-17 07:41:19 +03:00
|
|
|
int n;
|
|
|
|
|
2000-11-08 08:29:37 +03:00
|
|
|
rb_thread_fd_writable(fileno(f));
|
2000-11-17 07:41:19 +03:00
|
|
|
TRAP_BEG;
|
|
|
|
n = fflush(f);
|
|
|
|
TRAP_END;
|
2002-03-29 17:50:09 +03:00
|
|
|
if (n == EOF) rb_sys_fail(fptr->path);
|
2002-03-28 12:17:54 +03:00
|
|
|
fptr->mode &= ~FMODE_WBUF;
|
2000-11-08 08:29:37 +03:00
|
|
|
}
|
|
|
|
|
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;
|
2002-04-24 08:54:16 +04:00
|
|
|
long 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
|
|
|
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;
|
2002-04-24 08:54:16 +04:00
|
|
|
n = RSTRING(str)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (--n >= 0)
|
|
|
|
if (fputc(*ptr++, f) == EOF)
|
2002-01-21 10:44:06 +03:00
|
|
|
break;
|
1999-01-20 07:59:39 +03:00
|
|
|
n = ptr - RSTRING(str)->ptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-04-12 12:45:02 +04:00
|
|
|
if (n != RSTRING(str)->len && ferror(f))
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
#else
|
1998-01-16 15:19:22 +03:00
|
|
|
n = fwrite(RSTRING(str)->ptr, 1, RSTRING(str)->len, f);
|
2002-04-12 12:45:02 +04:00
|
|
|
if (n != RSTRING(str)->len && ferror(f)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (fptr->mode & FMODE_SYNC) {
|
2002-03-29 17:50:09 +03:00
|
|
|
io_fflush(f, fptr);
|
2002-02-07 07:18:08 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fptr->mode |= FMODE_WBUF;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
return LONG2FIX(n);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-02-20 09:35:37 +03:00
|
|
|
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);
|
2002-03-29 17:50:09 +03:00
|
|
|
|
|
|
|
io_fflush(f, fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_io_tell(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2002-03-14 09:23:46 +03:00
|
|
|
off_t pos;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-03-14 09:23:46 +03:00
|
|
|
pos = ftello(fptr->f);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(fptr->f)) rb_sys_fail(fptr->path);
|
2002-03-27 08:28:00 +03:00
|
|
|
return OFFT2NUM(pos);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2000-06-12 11:48:31 +04:00
|
|
|
#ifndef SEEK_CUR
|
|
|
|
# define SEEK_SET 0
|
|
|
|
# define SEEK_CUR 1
|
|
|
|
# define SEEK_END 2
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_io_seek(io, offset, whence)
|
|
|
|
VALUE io, offset;
|
|
|
|
int whence;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2002-04-18 12:46:18 +04:00
|
|
|
off_t pos;
|
2001-03-13 08:45:13 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-03-14 09:23:46 +03:00
|
|
|
pos = fseeko(fptr->f, NUM2OFFT(offset), whence);
|
2001-03-13 08:45:13 +03:00
|
|
|
if (pos != 0) rb_sys_fail(fptr->path);
|
|
|
|
clearerr(fptr->f);
|
|
|
|
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_seek_m(argc, argv, io)
|
2000-06-12 11:48:31 +04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2000-06-12 11:48:31 +04:00
|
|
|
VALUE offset, ptrname;
|
2002-04-18 12:46:18 +04:00
|
|
|
int whence = SEEK_SET;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
|
|
|
|
whence = NUM2INT(ptrname);
|
|
|
|
}
|
2000-06-12 11:48:31 +04:00
|
|
|
|
2001-03-13 08:45:13 +03:00
|
|
|
return rb_io_seek(io, offset, whence);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_set_pos(io, offset)
|
|
|
|
VALUE io, offset;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2002-04-18 12:46:18 +04:00
|
|
|
off_t pos;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-03-14 09:23:46 +03:00
|
|
|
pos = fseeko(fptr->f, NUM2OFFT(offset), SEEK_SET);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (pos != 0) rb_sys_fail(fptr->path);
|
|
|
|
clearerr(fptr->f);
|
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
return OFFT2NUM(pos);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_rewind(io)
|
2002-03-14 09:23:46 +03:00
|
|
|
VALUE io;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-03-14 09:23:46 +03:00
|
|
|
if (fseeko(fptr->f, 0L, 0) != 0) rb_sys_fail(fptr->path);
|
1999-01-20 07:59:39 +03:00
|
|
|
clearerr(fptr->f);
|
2000-06-19 12:38:11 +04:00
|
|
|
if (io == current_file) {
|
|
|
|
gets_lineno -= fptr->lineno;
|
|
|
|
}
|
|
|
|
fptr->lineno = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-01-23 10:30:43 +03:00
|
|
|
static VALUE
|
|
|
|
rb_io_fsync(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FSYNC
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_writable(fptr);
|
|
|
|
f = GetWriteFile(fptr);
|
2002-03-29 17:50:09 +03:00
|
|
|
|
|
|
|
io_fflush(f, fptr);
|
2002-01-23 10:30:43 +03:00
|
|
|
if (fsync(fileno(f)) < 0)
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
return INT2FIX(0);
|
|
|
|
#else
|
|
|
|
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_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);
|
|
|
|
}
|
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
static VALUE
|
|
|
|
rb_io_pid(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if (!fptr->pid)
|
|
|
|
return Qnil;
|
|
|
|
return INT2FIX(fptr->pid);
|
|
|
|
}
|
|
|
|
|
2002-02-22 13:28:47 +03:00
|
|
|
static VALUE
|
|
|
|
rb_io_inspect(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
char *buf, *cname;
|
|
|
|
|
|
|
|
GetOpenFile(obj, fptr);
|
|
|
|
if (!fptr->path) return rb_any_to_s(obj);
|
|
|
|
cname = rb_class2name(CLASS_OF(obj));
|
|
|
|
buf = ALLOCA_N(char, strlen(cname) + strlen(fptr->path) + 5);
|
|
|
|
sprintf(buf, "#<%s:%s>", cname, fptr->path);
|
|
|
|
return rb_str_new2(buf);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2000-11-20 10:31:55 +03:00
|
|
|
static long
|
2000-07-14 11:18:58 +04:00
|
|
|
io_fread(ptr, len, f)
|
|
|
|
char *ptr;
|
2000-11-20 10:31:55 +03:00
|
|
|
long len;
|
2000-07-14 11:18:58 +04:00
|
|
|
FILE *f;
|
|
|
|
{
|
2000-11-20 10:31:55 +03:00
|
|
|
long n = len;
|
2000-07-14 11:18:58 +04:00
|
|
|
int c;
|
|
|
|
|
2001-07-24 13:07:33 +04:00
|
|
|
while (n > 0) {
|
|
|
|
#ifdef READ_DATA_PENDING_COUNT
|
|
|
|
int i = READ_DATA_PENDING_COUNT(f);
|
|
|
|
if (i <= 0) {
|
|
|
|
rb_thread_wait_fd(fileno(f));
|
|
|
|
i = READ_DATA_PENDING_COUNT(f);
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
if (i > n) i = n;
|
|
|
|
TRAP_BEG;
|
|
|
|
c = fread(ptr, 1, i, f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c < 0) goto eof;
|
|
|
|
ptr += c;
|
|
|
|
n -= c;
|
|
|
|
if (c < i) goto eof;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#else
|
2000-07-14 11:18:58 +04:00
|
|
|
if (!READ_DATA_PENDING(f)) {
|
|
|
|
rb_thread_wait_fd(fileno(f));
|
|
|
|
}
|
2001-07-24 13:07:33 +04:00
|
|
|
#endif
|
2000-07-14 11:18:58 +04:00
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c == EOF) {
|
2001-07-24 13:07:33 +04:00
|
|
|
eof:
|
2000-07-14 11:18:58 +04:00
|
|
|
if (ferror(f)) {
|
2001-12-19 10:48:32 +03:00
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
continue;
|
|
|
|
case EAGAIN:
|
2001-12-10 10:18:16 +03:00
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
2001-12-19 10:48:32 +03:00
|
|
|
case EWOULDBLOCK:
|
2002-06-11 05:27:48 +04:00
|
|
|
#endif
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
case EPIPE:
|
2001-12-10 10:18:16 +03:00
|
|
|
#endif
|
2001-12-19 10:48:32 +03:00
|
|
|
return len - n;
|
|
|
|
}
|
2001-12-21 12:23:28 +03:00
|
|
|
return 0;
|
2000-07-14 11:18:58 +04:00
|
|
|
}
|
|
|
|
*ptr = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*ptr++ = c;
|
2001-07-24 13:07:33 +04:00
|
|
|
n--;
|
2000-07-14 11:18:58 +04:00
|
|
|
}
|
|
|
|
|
2001-07-24 13:07:33 +04:00
|
|
|
return len - n;
|
2000-07-14 11:18:58 +04:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
#ifndef S_ISREG
|
|
|
|
# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SMALLBUF 100
|
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
static long
|
|
|
|
remain_size(fptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
2002-03-25 17:50:40 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
struct stat st;
|
2002-03-14 09:23:46 +03:00
|
|
|
off_t siz = BUFSIZ;
|
2002-04-24 08:54:16 +04:00
|
|
|
off_t pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
if (feof(fptr->f)) return 0;
|
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
|
|
|
|
)
|
|
|
|
{
|
2002-03-25 09:18:07 +03:00
|
|
|
pos = ftello(fptr->f);
|
|
|
|
if (st.st_size > pos && pos >= 0) {
|
|
|
|
siz = st.st_size - pos + 1;
|
|
|
|
if (siz > LONG_MAX) {
|
|
|
|
rb_raise(rb_eIOError, "file too big for single read");
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-25 17:50:40 +03:00
|
|
|
return (long)siz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
read_all(fptr, siz)
|
|
|
|
OpenFile *fptr;
|
|
|
|
long siz;
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
long bytes = 0;
|
2002-04-24 08:54:16 +04:00
|
|
|
long n;
|
2002-03-25 17:50:40 +03:00
|
|
|
|
|
|
|
if (feof(fptr->f)) return Qnil;
|
2000-07-14 11:18:58 +04:00
|
|
|
READ_CHECK(fptr->f);
|
2002-03-25 17:50:40 +03:00
|
|
|
if (!siz) siz = BUFSIZ;
|
|
|
|
str = rb_tainted_str_new(0, siz);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (;;) {
|
2002-04-24 08:54:16 +04:00
|
|
|
n = io_fread(RSTRING(str)->ptr+bytes, siz-bytes, fptr->f);
|
1999-11-25 12:03:08 +03:00
|
|
|
if (n == 0 && bytes == 0) {
|
1999-08-13 09:45:20 +04:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
2002-03-25 17:50:40 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(length)) {
|
2002-03-25 17:50:40 +03:00
|
|
|
return read_all(fptr, remain_size(fptr));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
len = NUM2INT(length);
|
1999-10-13 10:44:42 +04:00
|
|
|
if (len < 0) {
|
|
|
|
rb_raise(rb_eArgError, "negative length %d given", len);
|
|
|
|
}
|
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);
|
1999-10-13 10:44:42 +04:00
|
|
|
if (len == 0) return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
READ_CHECK(fptr->f);
|
1999-09-20 11:14:18 +04:00
|
|
|
n = io_fread(RSTRING(str)->ptr, len, fptr->f);
|
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
|
|
|
}
|
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
static int
|
|
|
|
appendline(fptr, delim, strp)
|
2002-02-06 10:30:13 +03:00
|
|
|
OpenFile *fptr;
|
2002-03-25 17:50:40 +03:00
|
|
|
int delim;
|
|
|
|
VALUE *strp;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-02-06 10:30:13 +03:00
|
|
|
FILE *f = fptr->f;
|
2002-03-25 17:50:40 +03:00
|
|
|
VALUE str = *strp;
|
|
|
|
int c = EOF;
|
|
|
|
#ifndef READ_DATA_PENDING_PTR
|
2002-02-06 10:30:13 +03:00
|
|
|
char buf[8192];
|
2002-03-25 17:50:40 +03:00
|
|
|
char *bp = buf, *bpe = buf + sizeof buf - 3;
|
2002-02-06 10:30:13 +03:00
|
|
|
int cnt;
|
2002-03-25 17:50:40 +03:00
|
|
|
#endif
|
2002-02-06 10:30:13 +03:00
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
do {
|
|
|
|
#ifdef READ_DATA_PENDING_PTR
|
|
|
|
int pending = READ_DATA_PENDING_COUNT(f);
|
|
|
|
if (pending > 0) {
|
|
|
|
const char *p = READ_DATA_PENDING_PTR(f);
|
|
|
|
const char *e = memchr(p, delim, pending);
|
|
|
|
long last = 0;
|
|
|
|
if (e) pending = e - p + 1;
|
|
|
|
if (!NIL_P(str)) {
|
|
|
|
last = RSTRING(str)->len;
|
|
|
|
rb_str_resize(str, last + (c != EOF) + pending);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*strp = str = rb_str_new(0, (c != EOF) + pending);
|
|
|
|
}
|
|
|
|
if (c != EOF) {
|
|
|
|
RSTRING(str)->ptr[last++] = c;
|
|
|
|
}
|
|
|
|
fread(RSTRING(str)->ptr + last, 1, pending, f); /* must not fail */
|
|
|
|
if (e) return delim;
|
|
|
|
}
|
|
|
|
rb_thread_wait_fd(fileno(f));
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
#else
|
2002-02-06 10:30:13 +03:00
|
|
|
READ_CHECK(f);
|
2002-03-25 17:50:40 +03:00
|
|
|
#endif
|
2002-02-06 10:30:13 +03:00
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c == EOF) {
|
|
|
|
if (ferror(f)) {
|
|
|
|
if (errno == EINTR) continue;
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
2002-03-25 17:50:40 +03:00
|
|
|
return c;
|
2002-02-06 10:30:13 +03:00
|
|
|
}
|
2002-03-25 17:50:40 +03:00
|
|
|
#ifndef READ_DATA_PENDING_PTR
|
|
|
|
if ((*bp++ = c) == delim || bp == bpe) {
|
|
|
|
cnt = bp - buf;
|
2002-02-06 10:30:13 +03:00
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
if (cnt > 0) {
|
|
|
|
if (!NIL_P(str))
|
|
|
|
rb_str_cat(str, buf, cnt);
|
|
|
|
else
|
|
|
|
*strp = str = rb_str_new(buf, cnt);
|
|
|
|
}
|
|
|
|
bp = buf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} while (c != delim);
|
|
|
|
|
|
|
|
#ifdef READ_DATA_PENDING_PTR
|
|
|
|
{
|
|
|
|
char ch = c;
|
|
|
|
if (!NIL_P(str)) {
|
|
|
|
rb_str_cat(str, &ch, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*strp = str = rb_str_new(&ch, 1);
|
|
|
|
}
|
2002-02-06 10:30:13 +03:00
|
|
|
}
|
2002-03-25 17:50:40 +03:00
|
|
|
#endif
|
2002-02-06 10:30:13 +03:00
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
return c;
|
|
|
|
}
|
2002-02-06 10:30:13 +03:00
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
static inline int
|
|
|
|
swallow(fptr, term)
|
|
|
|
OpenFile *fptr;
|
|
|
|
int term;
|
|
|
|
{
|
|
|
|
FILE *f = fptr->f;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
do {
|
|
|
|
#ifdef READ_DATA_PENDING_PTR
|
|
|
|
int cnt;
|
|
|
|
while ((cnt = READ_DATA_PENDING_COUNT(f)) > 0) {
|
|
|
|
char buf[1024];
|
|
|
|
const char *p = READ_DATA_PENDING_PTR(f);
|
|
|
|
int i;
|
|
|
|
if (cnt > sizeof buf) cnt = sizeof buf;
|
|
|
|
if (*p != term) return Qtrue;
|
|
|
|
i = cnt;
|
|
|
|
while (--i && *++p == term);
|
|
|
|
if (!fread(buf, 1, cnt - i, f)) /* must not fail */
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
|
|
|
rb_thread_wait_fd(fileno(f));
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
#else
|
|
|
|
READ_CHECK(f);
|
|
|
|
#endif
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c != term) {
|
|
|
|
ungetc(c, f);
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
} while (c != EOF);
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_getline_fast(fptr, delim)
|
|
|
|
OpenFile *fptr;
|
|
|
|
int delim;
|
|
|
|
{
|
|
|
|
VALUE str = Qnil;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = appendline(fptr, delim, &str)) != EOF && c != delim);
|
2002-02-06 10:30:13 +03:00
|
|
|
|
|
|
|
if (!NIL_P(str)) {
|
|
|
|
fptr->lineno++;
|
|
|
|
lineno = INT2FIX(fptr->lineno);
|
|
|
|
OBJ_TAINT(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_getline(rs, fptr)
|
|
|
|
VALUE rs;
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
2002-02-06 10:30:13 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str = Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(rs)) {
|
2002-03-25 17:50:40 +03:00
|
|
|
str = read_all(fptr, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-05-01 13:42:38 +04:00
|
|
|
else if (rs == rb_default_rs) {
|
2002-03-25 17:50:40 +03:00
|
|
|
return rb_io_getline_fast(fptr, '\n');
|
2000-05-01 13:42:38 +04:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
2002-03-25 17:50:40 +03:00
|
|
|
int c, newline;
|
|
|
|
char *rsptr;
|
|
|
|
int rslen, rspara = 0;
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(rs);
|
1998-01-16 15:13:05 +03:00
|
|
|
rslen = RSTRING(rs)->len;
|
|
|
|
if (rslen == 0) {
|
|
|
|
rsptr = "\n\n";
|
|
|
|
rslen = 2;
|
|
|
|
rspara = 1;
|
2002-03-25 17:50:40 +03:00
|
|
|
swallow(fptr, '\n');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-03-25 17:50:40 +03:00
|
|
|
else if (rslen == 1) {
|
|
|
|
return rb_io_getline_fast(fptr, RSTRING(rs)->ptr[0]);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
|
|
|
rsptr = RSTRING(rs)->ptr;
|
|
|
|
}
|
2002-03-25 17:50:40 +03:00
|
|
|
newline = rsptr[rslen - 1];
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
while ((c = appendline(fptr, newline, &str)) != EOF &&
|
|
|
|
(c != newline || RSTRING(str)->len < rslen ||
|
|
|
|
memcmp(RSTRING(str)->ptr+RSTRING(str)->len-rslen,rsptr,rslen)));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-03-25 17:50:40 +03:00
|
|
|
if (rspara) {
|
|
|
|
if (c != EOF) {
|
|
|
|
swallow(fptr, '\n');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
2002-03-25 17:50:40 +03:00
|
|
|
return rb_io_getline_fast(fptr, '\n');
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_io_gets_m(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;
|
|
|
|
{
|
2002-02-06 10:30:13 +03:00
|
|
|
VALUE rs, str;
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
rs = rb_rs;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "1", &rs);
|
|
|
|
}
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
str = rb_io_getline(rs, fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
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);
|
2000-06-14 09:30:29 +04:00
|
|
|
fptr->lineno = NUM2INT(lineno);
|
|
|
|
return lineno;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
{
|
2000-01-05 07:41:21 +03:00
|
|
|
VALUE line = rb_io_gets_m(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;
|
2002-04-24 08:54:16 +04:00
|
|
|
VALUE rs;
|
2002-02-06 10:30:13 +03:00
|
|
|
OpenFile *fptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-02-06 10:30:13 +03:00
|
|
|
if (argc == 0) {
|
|
|
|
rs = rb_rs;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "1", &rs);
|
|
|
|
}
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new();
|
2002-02-06 10:30:13 +03:00
|
|
|
while (!NIL_P(line = rb_io_getline(rs, fptr))) {
|
1999-01-20 07:59:39 +03:00
|
|
|
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;
|
2002-02-06 10:30:13 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
VALUE rs;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-02-06 10:30:13 +03:00
|
|
|
if (argc == 0) {
|
|
|
|
rs = rb_rs;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "1", &rs);
|
|
|
|
}
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
while (!NIL_P(str = rb_io_getline(rs, fptr))) {
|
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;
|
2000-06-13 13:42:40 +04:00
|
|
|
if (c == EOF) {
|
|
|
|
if (ferror(f)) {
|
|
|
|
if (errno == EINTR) continue;
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(INT2FIX(c & 0xff));
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ferror(f)) rb_sys_fail(fptr->path);
|
2002-01-18 17:24:01 +03:00
|
|
|
return io;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2000-06-13 13:42:40 +04:00
|
|
|
retry:
|
1998-01-16 15:13:05 +03:00
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
|
|
|
|
if (c == EOF) {
|
2000-06-13 13:42:40 +04:00
|
|
|
if (ferror(f)) {
|
|
|
|
if (errno == EINTR) goto retry;
|
|
|
|
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;
|
2000-01-05 07:41:21 +03:00
|
|
|
int cc = NUM2INT(c);
|
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
|
|
|
|
2002-02-19 07:19:06 +03:00
|
|
|
if (ungetc(cc, fptr->f) == EOF && cc != EOF)
|
1998-01-16 15:13:05 +03:00
|
|
|
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
|
2002-02-05 10:56:31 +03:00
|
|
|
fptr_finalize(fptr, fin)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
2002-04-18 12:46:18 +04:00
|
|
|
int fin;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-02-05 20:10:54 +03:00
|
|
|
int n1 = 0, n2 = 0, e = 0, f1, f2 = -1;
|
2002-02-05 10:56:31 +03:00
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
if (fptr->f2) {
|
2002-02-05 20:10:54 +03:00
|
|
|
f2 = fileno(fptr->f2);
|
2002-02-05 10:56:31 +03:00
|
|
|
n2 = fclose(fptr->f2);
|
2002-02-07 07:18:08 +03:00
|
|
|
fptr->f2 = 0;
|
2002-02-05 20:10:54 +03:00
|
|
|
if (n2 < 0) e = errno;
|
|
|
|
}
|
2002-02-07 07:18:08 +03:00
|
|
|
if (fptr->f) {
|
2002-02-05 20:10:54 +03:00
|
|
|
f1 = fileno(fptr->f);
|
|
|
|
n1 = fclose(fptr->f);
|
2002-02-07 07:18:08 +03:00
|
|
|
fptr->f = 0;
|
|
|
|
if (n1 < 0 && errno == EBADF) {
|
|
|
|
if (f1 == f2 || !(fptr->mode & FMODE_WBUF)) {
|
|
|
|
n1 = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-02-05 10:56:31 +03:00
|
|
|
}
|
|
|
|
if (!fin && (n1 < 0 || n2 < 0)) {
|
2002-02-05 20:10:54 +03:00
|
|
|
if (n1 == 0) errno = e;
|
2002-02-05 10:56:31 +03:00
|
|
|
rb_sys_fail(fptr->path);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-12 10:42:35 +03:00
|
|
|
static void
|
2002-02-05 10:56:31 +03:00
|
|
|
rb_io_fptr_cleanup(fptr, fin)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
2002-02-05 10:56:31 +03:00
|
|
|
int fin;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (fptr->finalize) {
|
|
|
|
(*fptr->finalize)(fptr);
|
|
|
|
}
|
|
|
|
else {
|
2002-02-05 10:56:31 +03:00
|
|
|
fptr_finalize(fptr, fin);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (fptr->path) {
|
|
|
|
free(fptr->path);
|
2000-07-06 11:21:26 +04:00
|
|
|
fptr->path = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-12 10:42:35 +03:00
|
|
|
void
|
|
|
|
rb_io_fptr_finalize(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
if (!fptr) return;
|
|
|
|
if (!fptr->f && !fptr->f2) return;
|
|
|
|
if (fileno(fptr->f) < 3) return;
|
|
|
|
|
2002-02-05 10:56:31 +03:00
|
|
|
rb_io_fptr_cleanup(fptr, Qtrue);
|
2000-12-12 10:42:35 +03:00
|
|
|
}
|
|
|
|
|
2002-02-05 10:56:31 +03:00
|
|
|
VALUE
|
|
|
|
rb_io_close(io)
|
|
|
|
VALUE io;
|
2000-05-01 13:42:38 +04:00
|
|
|
{
|
2002-02-05 10:56:31 +03:00
|
|
|
OpenFile *fptr;
|
2000-05-01 13:42:38 +04:00
|
|
|
int fd;
|
|
|
|
|
2002-02-05 10:56:31 +03:00
|
|
|
fptr = RFILE(io)->fptr;
|
2002-02-20 07:31:50 +03:00
|
|
|
if (!fptr) return Qnil;
|
2002-02-20 07:42:49 +03:00
|
|
|
if (!fptr->f && !fptr->f2) return Qnil;
|
2000-05-01 13:42:38 +04:00
|
|
|
|
|
|
|
fd = fileno(fptr->f);
|
2002-02-05 10:56:31 +03:00
|
|
|
rb_io_fptr_cleanup(fptr, Qfalse);
|
2000-05-01 13:42:38 +04:00
|
|
|
rb_thread_fd_close(fd);
|
|
|
|
|
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
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_io_close_m(io)
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-12-14 09:50:43 +03:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't close");
|
|
|
|
}
|
2002-01-18 17:24:01 +03:00
|
|
|
rb_io_check_closed(RFILE(io)->fptr);
|
1999-01-20 07:59:39 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
* intern.h: add prototypes.
rb_gc_enable(), rb_gc_disable(), rb_gc_start(), rb_str_new5()
rb_str_buf_append(), rb_str_buf_cat(), rb_str_buf_cat2(),
rb_str_dup_frozen()
* ruby.h: added declaration.
rb_defout, rb_stdin, rb_stdout, rb_stderr, ruby_errinfo
* rubyio.h: changed double include guard macro to RUBYIO_H.
* array.c (inspect_call): make static.
* eval.c (dvar_asgn): ditto.
* io.c (rb_io_close_read): ditto.
* lex.c (rb_reserved_word): ditto.
* ruby.c: (req_list_head, req_list_last): ditto.
* ruby.c (require_libraries): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1915 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2001-12-17 10:52:35 +03:00
|
|
|
static 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-12-14 09:50:43 +03:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't close");
|
|
|
|
}
|
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-12-14 09:50:43 +03:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't close");
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2002-03-27 08:28:00 +03:00
|
|
|
static VALUE
|
|
|
|
rb_io_sysseek(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE offset, ptrname;
|
2002-04-18 12:46:18 +04:00
|
|
|
int whence = SEEK_SET;
|
2002-03-27 08:28:00 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
off_t pos;
|
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
|
|
|
|
whence = NUM2INT(ptrname);
|
|
|
|
}
|
2002-03-27 08:28:00 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if ((fptr->mode & FMODE_READABLE) && READ_DATA_PENDING(fptr->f)) {
|
2002-03-27 08:52:36 +03:00
|
|
|
rb_raise(rb_eIOError, "sysseek for buffered IO");
|
2002-03-27 08:28:00 +03:00
|
|
|
}
|
|
|
|
if ((fptr->mode & FMODE_WRITABLE) && (fptr->mode & FMODE_WBUF)) {
|
|
|
|
rb_warn("sysseek for buffered IO");
|
|
|
|
}
|
|
|
|
pos = lseek(fileno(fptr->f), NUM2OFFT(offset), whence);
|
|
|
|
if (pos == -1) rb_sys_fail(fptr->path);
|
|
|
|
clearerr(fptr->f);
|
|
|
|
|
|
|
|
return OFFT2NUM(pos);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2002-02-07 07:18:08 +03:00
|
|
|
if (fptr->mode & FMODE_WBUF) {
|
|
|
|
rb_warn("syswrite for buffered IO");
|
|
|
|
}
|
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
|
|
|
|
2000-01-17 11:37:53 +03:00
|
|
|
if (READ_DATA_PENDING(fptr->f)) {
|
|
|
|
rb_raise(rb_eIOError, "sysread for buffered IO");
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
str = rb_str_new(0, ilen);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-02-26 08:29:06 +03:00
|
|
|
n = fileno(fptr->f);
|
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;
|
|
|
|
{
|
2000-05-16 19:50:49 +04:00
|
|
|
#if defined(NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || 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
|
|
|
|
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);
|
|
|
|
#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;
|
2001-06-05 11:19:39 +04:00
|
|
|
const char *m = mode;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-06-05 11:19:39 +04:00
|
|
|
switch (*m++) {
|
1998-01-16 15:13:05 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
while (*m) {
|
|
|
|
switch (*m++) {
|
|
|
|
case 'b':
|
|
|
|
flags |= FMODE_BINMODE;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
flags |= FMODE_READWRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-04-01 11:39:09 +04:00
|
|
|
rb_io_modenum_flags(mode)
|
1999-08-13 09:45:20 +04:00
|
|
|
int mode;
|
|
|
|
{
|
2002-04-24 08:54:16 +04:00
|
|
|
int flags = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
|
2002-03-29 17:50:09 +03:00
|
|
|
case O_RDONLY:
|
1999-08-13 09:45:20 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-09-03 09:37:42 +04:00
|
|
|
static int
|
2002-04-01 11:39:09 +04:00
|
|
|
rb_io_mode_modenum(mode)
|
2001-06-05 11:19:39 +04:00
|
|
|
const char *mode;
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
const char *m = mode;
|
|
|
|
|
|
|
|
switch (*m++) {
|
|
|
|
case 'r':
|
|
|
|
flags |= O_RDONLY;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
flags |= O_WRONLY | O_CREAT | O_TRUNC;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
flags |= O_WRONLY | O_CREAT | O_APPEND;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error:
|
|
|
|
rb_raise(rb_eArgError, "illegal access mode %s", mode);
|
|
|
|
}
|
|
|
|
|
2002-04-24 08:54:16 +04:00
|
|
|
while (*m) {
|
|
|
|
switch (*m++) {
|
|
|
|
case 'b':
|
2001-06-05 11:19:39 +04:00
|
|
|
#ifdef O_BINARY
|
2002-04-24 08:54:16 +04:00
|
|
|
flags |= O_BINARY;
|
2001-06-05 11:19:39 +04:00
|
|
|
#endif
|
2002-04-24 08:54:16 +04:00
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
flags |= O_RDWR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
2001-06-05 11:19:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static char*
|
2002-04-01 11:39:09 +04:00
|
|
|
rb_io_modenum_mode(flags, mode)
|
1999-08-13 09:45:20 +04:00
|
|
|
int flags;
|
2001-09-05 10:54:57 +04:00
|
|
|
char *mode;
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
char *p = mode;
|
|
|
|
|
|
|
|
switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
|
2002-03-29 17:50:09 +03:00
|
|
|
case O_RDONLY:
|
1999-08-13 09:45:20 +04:00
|
|
|
*p++ = 'r';
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
*p++ = 'w';
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
2002-04-01 11:39:09 +04:00
|
|
|
*p++ = 'r';
|
1999-08-13 09:45:20 +04:00
|
|
|
*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
|
2001-02-02 14:38:20 +03:00
|
|
|
rb_sysopen(fname, flags, mode)
|
1999-08-13 09:45:20 +04:00
|
|
|
char *fname;
|
2001-02-02 14:38:20 +03:00
|
|
|
int flags;
|
1999-08-13 09:45:20 +04:00
|
|
|
unsigned int mode;
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2001-02-02 14:38:20 +03:00
|
|
|
fd = open(fname, flags, mode);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
2001-02-02 14:38:20 +03:00
|
|
|
fd = open(fname, flags, mode);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
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);
|
2000-07-06 11:21:26 +04:00
|
|
|
if (!file) {
|
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
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
if (!file) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(fname);
|
|
|
|
}
|
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
#ifdef USE_SETVBUF
|
|
|
|
if (setvbuf(file, NULL, _IOFBF, 0) != 0)
|
|
|
|
rb_warn("setvbuf() can't be honered for %s", fname);
|
|
|
|
#endif
|
2000-01-05 07:41:21 +03:00
|
|
|
#ifdef __human68k__
|
|
|
|
fmode(file, _IOTEXT);
|
|
|
|
#endif
|
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);
|
2000-07-06 11:21:26 +04:00
|
|
|
if (!file) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
|
|
|
file = fdopen(fd, mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
if (!file) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
#ifdef USE_SETVBUF
|
|
|
|
if (setvbuf(file, NULL, _IOFBF, 0) != 0)
|
|
|
|
rb_warn("setvbuf() can't be honered (fd=%d)", fd);
|
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-03-23 11:37:35 +03:00
|
|
|
rb_file_open_internal(io, fname, mode)
|
|
|
|
VALUE io;
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *fname, *mode;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2000-03-23 11:37:35 +03:00
|
|
|
|
|
|
|
MakeOpenFile(io, fptr);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
fptr->mode = rb_io_mode_flags(mode);
|
|
|
|
fptr->f = rb_fopen(fname, mode);
|
|
|
|
fptr->path = strdup(fname);
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
return io;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_file_open(fname, mode)
|
|
|
|
const char *fname, *mode;
|
|
|
|
{
|
2001-10-03 11:19:19 +04:00
|
|
|
VALUE io = rb_obj_alloc(rb_cFile);
|
|
|
|
|
|
|
|
return rb_file_open_internal(io, fname, mode);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
static VALUE
|
|
|
|
rb_file_sysopen_internal(io, fname, flags, mode)
|
|
|
|
VALUE io;
|
1999-08-13 09:45:20 +04:00
|
|
|
char *fname;
|
|
|
|
int flags, mode;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int fd;
|
|
|
|
char *m;
|
2001-09-03 09:37:42 +04:00
|
|
|
char mbuf[4];
|
2000-03-23 11:37:35 +03:00
|
|
|
|
|
|
|
MakeOpenFile(io, fptr);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
fd = rb_sysopen(fname, flags, mode);
|
2002-04-01 11:39:09 +04:00
|
|
|
m = rb_io_modenum_mode(flags, mbuf);
|
|
|
|
fptr->mode = rb_io_modenum_flags(flags);
|
1999-08-13 09:45:20 +04:00
|
|
|
fptr->f = rb_fdopen(fd, m);
|
|
|
|
fptr->path = strdup(fname);
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
return io;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_file_sysopen(fname, flags, mode)
|
|
|
|
const char *fname;
|
|
|
|
int flags, mode;
|
|
|
|
{
|
2001-10-03 11:19:19 +04:00
|
|
|
VALUE io = rb_obj_alloc(rb_cFile);
|
|
|
|
|
|
|
|
return rb_file_sysopen_internal(io, fname, flags, mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-03-22 10:26:42 +03:00
|
|
|
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__VMS)
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-22 10:26:42 +03:00
|
|
|
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__VMS)
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
2000-08-07 09:05:04 +04:00
|
|
|
pipe_atexit _((void))
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2000-08-07 09:05:04 +04:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-06 19:06:00 +04:00
|
|
|
static void pipe_finalize _((OpenFile *fptr));
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
|
|
|
pipe_finalize(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
2002-06-28 22:26:01 +04:00
|
|
|
#if !defined (__CYGWIN__) && !defined(NT)
|
2000-07-10 10:30:11 +04:00
|
|
|
extern VALUE rb_last_status;
|
|
|
|
int status;
|
2000-07-06 11:21:26 +04:00
|
|
|
if (fptr->f) {
|
2000-07-10 10:30:11 +04:00
|
|
|
status = pclose(fptr->f);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
if (fptr->f2) {
|
2000-07-10 10:30:11 +04:00
|
|
|
status = pclose(fptr->f2);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
fptr->f = fptr->f2 = 0;
|
2000-11-13 13:11:31 +03:00
|
|
|
#if defined DJGPP
|
2000-07-10 10:47:04 +04:00
|
|
|
status <<= 8;
|
|
|
|
#endif
|
2000-07-10 10:30:11 +04:00
|
|
|
rb_last_status = INT2FIX(status);
|
1999-01-20 07:59:39 +03:00
|
|
|
#else
|
2002-02-05 20:10:54 +03:00
|
|
|
fptr_finalize(fptr, Qtrue);
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
pipe_del_fptr(fptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
1999-09-20 11:14:18 +04:00
|
|
|
rb_io_synchronized(fptr)
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
fptr->mode |= FMODE_SYNC;
|
|
|
|
}
|
|
|
|
|
1999-09-20 11:14:18 +04:00
|
|
|
void
|
|
|
|
rb_io_unbuffered(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
rb_io_synchronized(fptr);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
pipe_open(pname, mode)
|
|
|
|
char *pname, *mode;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
int modef = rb_io_mode_flags(mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
|
|
|
|
2002-06-28 22:26:01 +04:00
|
|
|
#if defined(DJGPP) || defined(__human68k__) || defined(__VMS)
|
1998-01-16 15:13:05 +03:00
|
|
|
FILE *f = popen(pname, mode);
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
if (!f) rb_sys_fail(pname);
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2001-10-03 11:19:19 +04:00
|
|
|
VALUE port = rb_obj_alloc(rb_cIO);
|
|
|
|
|
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) {
|
2000-01-08 08:00:25 +03:00
|
|
|
if (fptr->f) fptr->f2 = f;
|
|
|
|
else fptr->f = f;
|
1999-09-20 11:14:18 +04:00
|
|
|
rb_io_synchronized(fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-06-28 22:26:01 +04:00
|
|
|
return (VALUE)port;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#if defined(NT)
|
|
|
|
int pid;
|
|
|
|
FILE *fpr, *fpw;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
pid = pipe_exec(pname, rb_io_mode_modenum(mode), &fpr, &fpw);
|
|
|
|
if (pid == -1) { /* exec failed */
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
rb_thread_sleep(1);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
rb_sys_fail(pname);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE port = rb_obj_alloc(rb_cIO);
|
|
|
|
|
|
|
|
MakeOpenFile(port, fptr);
|
|
|
|
fptr->mode = modef;
|
|
|
|
fptr->mode |= FMODE_SYNC;
|
|
|
|
fptr->pid = pid;
|
|
|
|
|
|
|
|
if (modef & FMODE_READABLE) {
|
|
|
|
fptr->f = fpr;
|
|
|
|
}
|
|
|
|
if (modef & FMODE_WRITABLE) {
|
|
|
|
if (fptr->f) fptr->f2 = fpw;
|
|
|
|
else fptr->f = fpw;
|
|
|
|
}
|
|
|
|
fptr->finalize = pipe_finalize;
|
|
|
|
pipe_add_fptr(fptr);
|
|
|
|
return (VALUE)port;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
#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:
|
2002-01-04 17:15:33 +03:00
|
|
|
switch ((pid = fork())) {
|
1998-01-16 15:13:05 +03:00
|
|
|
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);
|
|
|
|
}
|
2000-05-17 08:38:19 +04:00
|
|
|
rb_io_synchronized(RFILE(orig_stdout)->fptr);
|
|
|
|
rb_io_synchronized(RFILE(orig_stderr)->fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
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 {
|
2001-10-03 11:19:19 +04:00
|
|
|
VALUE port = rb_obj_alloc(rb_cIO);
|
|
|
|
|
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;
|
|
|
|
}
|
1999-11-25 12:03:08 +03:00
|
|
|
#if defined (__CYGWIN__)
|
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
|
2002-01-18 17:24:01 +03:00
|
|
|
return port;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-06-28 22:26:01 +04:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-05-24 08:34:26 +04:00
|
|
|
rb_io_popen(str, argc, argv, klass)
|
|
|
|
char *str;
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2000-05-24 08:34:26 +04:00
|
|
|
VALUE klass;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
char *mode;
|
2000-05-25 09:55:12 +04:00
|
|
|
VALUE pname, pmode, port;
|
2001-09-03 09:37:42 +04:00
|
|
|
char mbuf[4];
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-25 09:55:12 +04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
|
1998-01-16 15:13:05 +03:00
|
|
|
mode = "r";
|
|
|
|
}
|
2001-09-03 09:37:42 +04:00
|
|
|
else if (FIXNUM_P(pmode)) {
|
2002-04-01 11:39:09 +04:00
|
|
|
mode = rb_io_modenum_mode(FIX2INT(pmode), mbuf);
|
2001-09-03 09:37:42 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
mode = StringValuePtr(pmode);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(pname);
|
2000-05-24 08:34:26 +04:00
|
|
|
port = pipe_open(str, mode);
|
2000-02-01 06:12:21 +03:00
|
|
|
if (NIL_P(port)) {
|
2000-05-15 10:38:46 +04:00
|
|
|
/* child */
|
2000-05-25 09:55:12 +04:00
|
|
|
if (rb_block_given_p()) {
|
2000-05-15 10:38:46 +04:00
|
|
|
rb_yield(Qnil);
|
2000-05-25 09:55:12 +04:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
_exit(0);
|
2000-05-10 06:58:41 +04:00
|
|
|
}
|
2000-05-25 09:55:12 +04:00
|
|
|
return Qnil;
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
2000-05-24 08:34:26 +04:00
|
|
|
RBASIC(port)->klass = klass;
|
|
|
|
if (rb_block_given_p()) {
|
2000-06-05 12:46:59 +04:00
|
|
|
return rb_ensure(rb_yield, port, rb_io_close, port);
|
2000-02-01 06:12:21 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return port;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
static VALUE
|
2000-05-24 08:34:26 +04:00
|
|
|
rb_io_s_popen(argc, argv, klass)
|
2000-03-23 11:37:35 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2000-05-24 08:34:26 +04:00
|
|
|
VALUE klass;
|
2000-03-23 11:37:35 +03:00
|
|
|
{
|
2000-05-24 08:34:26 +04:00
|
|
|
char *str = 0;
|
2000-03-23 11:37:35 +03:00
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
if (argc >= 1) {
|
2001-05-02 08:22:21 +04:00
|
|
|
str = StringValuePtr(argv[0]);
|
2000-03-23 11:37:35 +03:00
|
|
|
}
|
2000-05-24 08:34:26 +04:00
|
|
|
return rb_io_popen(str, argc, argv, klass);
|
2000-03-23 11:37:35 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2001-06-05 11:19:39 +04:00
|
|
|
rb_open_file(argc, argv, io)
|
1999-08-13 09:45:20 +04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2001-06-05 11:19:39 +04:00
|
|
|
VALUE io;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-05-23 09:35:32 +04:00
|
|
|
VALUE fname, vmode, perm;
|
1999-08-13 09:45:20 +04:00
|
|
|
char *path, *mode;
|
2001-06-05 11:19:39 +04:00
|
|
|
int flags, fmode;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(fname);
|
1999-08-13 09:45:20 +04:00
|
|
|
path = RSTRING(fname)->ptr;
|
|
|
|
|
2001-06-05 11:19:39 +04:00
|
|
|
if (FIXNUM_P(vmode) || !NIL_P(perm)) {
|
2002-04-01 11:39:09 +04:00
|
|
|
flags = FIXNUM_P(vmode) ? NUM2INT(vmode) : rb_io_mode_modenum(StringValuePtr(vmode));
|
2001-06-05 11:19:39 +04:00
|
|
|
fmode = NIL_P(perm) ? 0666 : NUM2INT(perm);
|
2001-09-03 09:37:42 +04:00
|
|
|
|
2002-05-23 09:35:32 +04:00
|
|
|
rb_file_sysopen_internal(io, path, flags, fmode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
mode = NIL_P(vmode) ? "r" : StringValuePtr(vmode);
|
2002-05-23 09:35:32 +04:00
|
|
|
rb_file_open_internal(io, RSTRING(fname)->ptr, mode);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2001-06-05 11:19:39 +04:00
|
|
|
return io;
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2001-06-05 11:19:39 +04:00
|
|
|
static VALUE
|
2002-01-18 17:24:01 +03:00
|
|
|
rb_io_s_open(argc, argv, klass)
|
2001-06-05 11:19:39 +04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE klass;
|
|
|
|
{
|
2002-01-18 17:24:01 +03:00
|
|
|
VALUE io = rb_class_new_instance(argc, argv, klass);
|
2001-06-05 11:19:39 +04:00
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
if (rb_block_given_p()) {
|
2002-01-18 17:24:01 +03:00
|
|
|
return rb_ensure(rb_yield, io, rb_io_close, io);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-01-18 17:24:01 +03:00
|
|
|
return io;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-05-23 09:35:32 +04:00
|
|
|
static VALUE
|
|
|
|
rb_io_s_sysopen(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE fname, vmode, perm;
|
|
|
|
int flags, fmode, fd;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
|
|
|
|
SafeStringValue(fname);
|
|
|
|
|
|
|
|
if (NIL_P(vmode)) flags = O_RDONLY;
|
|
|
|
else if (FIXNUM_P(vmode)) flags = NUM2INT(vmode);
|
|
|
|
else {
|
|
|
|
flags = rb_io_mode_modenum(StringValuePtr(vmode));
|
|
|
|
}
|
|
|
|
if (NIL_P(perm)) fmode = 0666;
|
|
|
|
else fmode = NUM2INT(perm);
|
|
|
|
|
|
|
|
fd = rb_sysopen(RSTRING(fname)->ptr, flags, fmode);
|
|
|
|
return INT2NUM(fd);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
{
|
2000-05-24 08:34:26 +04:00
|
|
|
if (argc >= 1) {
|
2001-05-02 08:22:21 +04:00
|
|
|
char *str = StringValuePtr(argv[0]);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
if (str[0] == '|') {
|
|
|
|
return rb_io_popen(str+1, argc, argv, rb_cIO);
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2002-01-18 17:24:01 +03:00
|
|
|
return rb_io_s_open(argc, argv, rb_cFile);
|
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
|
2000-02-29 11:05:32 +03:00
|
|
|
io_reopen(io, nfile)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE io, nfile;
|
|
|
|
{
|
|
|
|
OpenFile *fptr, *orig;
|
|
|
|
char *mode;
|
|
|
|
int fd;
|
2002-05-29 09:20:39 +04:00
|
|
|
off_t pos = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
nfile = rb_io_get_io(nfile);
|
1999-12-14 09:50:43 +03:00
|
|
|
if (rb_safe_level() >= 4 && (!OBJ_TAINTED(io) || !OBJ_TAINTED(nfile))) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't reopen");
|
|
|
|
}
|
|
|
|
GetOpenFile(io, fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
GetOpenFile(nfile, orig);
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (fptr == orig) return io;
|
2000-06-14 12:08:50 +04:00
|
|
|
if (orig->mode & FMODE_READABLE) {
|
2002-03-14 09:23:46 +03:00
|
|
|
pos = ftello(orig->f);
|
2000-06-14 12:08:50 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if (orig->f2) {
|
2002-03-29 17:50:09 +03:00
|
|
|
io_fflush(orig->f2, orig);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else if (orig->mode & FMODE_WRITABLE) {
|
2002-03-29 17:50:09 +03:00
|
|
|
io_fflush(orig->f, orig);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
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) {
|
2000-07-14 11:18:58 +04:00
|
|
|
clearerr(fptr->f);
|
2000-07-17 13:38:10 +04:00
|
|
|
/* need to keep stdio objects */
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2000-07-17 13:38:10 +04:00
|
|
|
}
|
|
|
|
if ((orig->mode & FMODE_READABLE) && pos >= 0) {
|
2002-03-14 09:23:46 +03:00
|
|
|
fseeko(fptr->f, pos, SEEK_SET);
|
|
|
|
fseeko(orig->f, pos, SEEK_SET);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
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
|
2000-02-29 11:05:32 +03:00
|
|
|
rb_io_reopen(argc, argv, file)
|
1999-08-13 09:45:20 +04:00
|
|
|
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) {
|
2000-04-10 09:48:43 +04:00
|
|
|
if (TYPE(fname) != T_STRING) { /* fname must be IO */
|
2000-02-29 11:05:32 +03:00
|
|
|
return io_reopen(file, fname);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(fname);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!NIL_P(nmode)) {
|
2001-05-02 08:22:21 +04:00
|
|
|
mode = StringValuePtr(nmode);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = "r";
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(file, fptr);
|
2000-07-06 11:21:26 +04:00
|
|
|
if (fptr->path) {
|
|
|
|
free(fptr->path);
|
|
|
|
fptr->path = 0;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
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);
|
2000-07-06 11:21:26 +04:00
|
|
|
fptr->f2 = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2000-07-06 11:21:26 +04:00
|
|
|
if (freopen(RSTRING(fname)->ptr, mode, fptr->f) == 0) {
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
#ifdef USE_SETVBUF
|
|
|
|
if (setvbuf(fptr->f, NULL, _IOFBF, 0) != 0)
|
|
|
|
rb_warn("setvbuf() can't be honered for %s", RSTRING(fname)->ptr);
|
|
|
|
#endif
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (fptr->f2) {
|
2000-07-06 11:21:26 +04:00
|
|
|
if (freopen(RSTRING(fname)->ptr, "w", fptr->f2) == 0) {
|
1999-08-13 09:45:20 +04:00
|
|
|
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;
|
2001-10-03 11:19:19 +04:00
|
|
|
VALUE clone = rb_obj_clone(io);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
GetOpenFile(io, orig);
|
2000-01-05 07:41:21 +03:00
|
|
|
MakeOpenFile(clone, fptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (orig->f2) {
|
2002-03-29 17:50:09 +03:00
|
|
|
io_fflush(orig->f2, orig);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else if (orig->mode & FMODE_WRITABLE) {
|
2002-03-29 17:50:09 +03:00
|
|
|
io_fflush(orig->f, orig);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2001-07-14 19:17:19 +04:00
|
|
|
fd = ruby_dup(fileno(orig->f));
|
1998-01-16 15:13:05 +03:00
|
|
|
fptr->f = rb_fdopen(fd, mode);
|
2002-05-20 08:33:59 +04:00
|
|
|
if (orig->f2) {
|
2001-02-13 08:09:11 +03:00
|
|
|
if (fileno(orig->f) != fileno(orig->f2)) {
|
2001-07-14 19:17:19 +04:00
|
|
|
fd = ruby_dup(fileno(orig->f2));
|
2001-02-13 08:09:11 +03:00
|
|
|
}
|
2002-05-20 08:33:59 +04:00
|
|
|
fptr->f2 = rb_fdopen(fd, "w");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
if (fptr->mode & FMODE_BINMODE) {
|
2002-01-18 17:24:01 +03:00
|
|
|
rb_io_binmode(clone);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-01-18 17:24:01 +03:00
|
|
|
return clone;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-02-20 09:35:37 +03:00
|
|
|
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;
|
|
|
|
{
|
2000-04-10 09:48:43 +04:00
|
|
|
rb_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--;
|
|
|
|
}
|
2000-04-10 09:48:43 +04:00
|
|
|
rb_io_write(out, rb_f_sprintf(argc, argv));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-02-20 09:35:37 +03:00
|
|
|
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);
|
2000-05-01 13:42:38 +04:00
|
|
|
if (fptr->mode & FMODE_SYNC) {
|
2002-03-29 17:50:09 +03:00
|
|
|
io_fflush(f, fptr);
|
2002-02-07 07:18:08 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fptr->mode |= FMODE_WBUF;
|
2000-05-01 13:42:38 +04:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_putc(recv, ch)
|
|
|
|
VALUE recv, ch;
|
|
|
|
{
|
|
|
|
return rb_io_putc(rb_defout, ch);
|
|
|
|
}
|
|
|
|
|
2001-12-10 10:18:16 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-02-20 09:35:37 +03:00
|
|
|
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++) {
|
2001-10-30 11:43:28 +03:00
|
|
|
if (NIL_P(argv[i])) {
|
1999-01-20 07:59:39 +03:00
|
|
|
line = rb_str_new2("nil");
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2001-10-30 11:43:28 +03:00
|
|
|
else {
|
2001-12-10 10:18:16 +03:00
|
|
|
line = rb_check_convert_type(argv[i], T_ARRAY, "Array", "to_ary");
|
|
|
|
if (!NIL_P(line)) {
|
|
|
|
rb_protect_inspect(io_puts_ary, line, out);
|
|
|
|
continue;
|
2001-11-19 08:03:03 +03:00
|
|
|
}
|
2001-10-30 11:43:28 +03:00
|
|
|
line = rb_obj_as_string(argv[i]);
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
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;
|
|
|
|
{
|
2000-07-25 11:21:54 +04:00
|
|
|
rb_io_write(rb_defout, rb_obj_as_string(rb_inspect(obj)));
|
|
|
|
rb_io_write(rb_defout, rb_default_rs);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
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]);
|
|
|
|
}
|
2000-12-26 11:32:35 +03:00
|
|
|
if (TYPE(rb_defout) == T_FILE) {
|
2000-12-26 11:08:50 +03:00
|
|
|
rb_io_flush(rb_defout);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
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);
|
2001-02-26 08:29:06 +03:00
|
|
|
if (fileno(fptr->f) == 0 && saved_fd[0] != 0) {
|
|
|
|
dup2(saved_fd[0], 0);
|
|
|
|
close(saved_fd[0]);
|
|
|
|
saved_fd[0] = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
saved_fd[0] = dup(0);
|
|
|
|
dup2(fileno(fptr->f), 0);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
*var = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_outfile(val, var, orig, stdf)
|
|
|
|
VALUE val;
|
|
|
|
VALUE *var;
|
|
|
|
VALUE orig;
|
|
|
|
FILE *stdf;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
2001-02-26 08:29:06 +03:00
|
|
|
int fd;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
if (val == *var) return;
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
if (TYPE(*var) == T_FILE && !rb_io_closed(*var)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
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);
|
|
|
|
f = GetWriteFile(fptr);
|
2001-02-26 08:29:06 +03:00
|
|
|
fd = fileno(stdf);
|
|
|
|
if (fileno(fptr->f) == fd && saved_fd[fd] != fd) {
|
|
|
|
dup2(saved_fd[fd], fd);
|
|
|
|
close(saved_fd[fd]);
|
|
|
|
saved_fd[fd] = fd;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
saved_fd[fd] = dup(fd);
|
|
|
|
dup2(fileno(fptr->f), fd);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
*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;
|
2001-10-03 11:19:19 +04:00
|
|
|
VALUE io = rb_obj_alloc(klass);
|
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;
|
|
|
|
|
2002-01-18 17:24:01 +03:00
|
|
|
return io;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-01-15 10:01:00 +03:00
|
|
|
static void
|
2000-08-25 12:26:06 +04:00
|
|
|
prep_path(io, path)
|
|
|
|
VALUE io;
|
|
|
|
char *path;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if (fptr->path) rb_bug("illegal prep_path() call");
|
|
|
|
fptr->path = strdup(path);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2001-10-03 11:19:19 +04:00
|
|
|
rb_io_s_alloc(klass)
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE klass;
|
2000-03-23 11:37:35 +03:00
|
|
|
{
|
|
|
|
NEWOBJ(io, struct RFile);
|
|
|
|
OBJSETUP(io, klass, T_FILE);
|
2002-03-29 17:50:09 +03:00
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
io->fptr = 0;
|
|
|
|
|
|
|
|
return (VALUE)io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_initialize(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE fnum, mode;
|
2000-03-23 11:37:35 +03:00
|
|
|
OpenFile *fp;
|
2002-04-01 11:39:09 +04:00
|
|
|
int fd, flags;
|
|
|
|
char mbuf[4];
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-04-01 11:39:09 +04:00
|
|
|
rb_scan_args(argc, argv, "11", &fnum, &mode);
|
|
|
|
fd = NUM2INT(fnum);
|
|
|
|
if (argc == 2) {
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(mode);
|
2002-04-01 12:31:49 +04:00
|
|
|
flags = rb_io_mode_modenum(RSTRING(mode)->ptr);
|
2002-04-01 11:39:09 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if defined(HAVE_FCNTL) && defined(F_GETFL)
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
#else
|
|
|
|
flags = O_RDONLY;
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-03-23 11:37:35 +03:00
|
|
|
MakeOpenFile(io, fp);
|
2002-04-15 11:48:47 +04:00
|
|
|
fp->mode = rb_io_modenum_flags(flags);
|
2002-04-01 11:39:09 +04:00
|
|
|
fp->f = rb_fdopen(fd, rb_io_modenum_mode(flags, mbuf));
|
2000-03-23 11:37:35 +03:00
|
|
|
|
|
|
|
return io;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
static VALUE
|
|
|
|
rb_file_initialize(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
2000-07-06 11:21:26 +04:00
|
|
|
if (RFILE(io)->fptr) {
|
|
|
|
rb_io_close_m(io);
|
|
|
|
free(RFILE(io)->fptr);
|
|
|
|
RFILE(io)->fptr = 0;
|
|
|
|
}
|
2002-02-05 10:56:31 +03:00
|
|
|
if (0 < argc && argc < 3) {
|
|
|
|
VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");
|
|
|
|
|
|
|
|
if (!NIL_P(fd)) {
|
|
|
|
argv[0] = fd;
|
|
|
|
return rb_io_initialize(argc, argv, io);
|
|
|
|
}
|
|
|
|
}
|
2001-06-05 11:19:39 +04:00
|
|
|
rb_open_file(argc, argv, io);
|
2000-05-24 08:34:26 +04:00
|
|
|
|
2001-06-05 11:19:39 +04:00
|
|
|
return io;
|
2000-05-24 08:34:26 +04:00
|
|
|
}
|
|
|
|
|
2001-02-13 08:09:11 +03:00
|
|
|
static VALUE
|
2002-01-18 17:24:01 +03:00
|
|
|
rb_io_s_new(argc, argv, klass)
|
2001-02-13 08:09:11 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE klass;
|
|
|
|
{
|
2002-01-18 17:24:01 +03:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
char *cname = rb_class2name(klass);
|
2001-10-03 11:19:19 +04:00
|
|
|
|
2002-04-01 11:39:09 +04:00
|
|
|
rb_warn("%s::new() does not take block; use %s::open() instead",
|
2002-01-18 17:24:01 +03:00
|
|
|
cname, cname);
|
2001-02-13 08:09:11 +03:00
|
|
|
}
|
2002-01-18 17:24:01 +03:00
|
|
|
return rb_class_new_instance(argc, argv, klass);
|
2001-02-13 08:09:11 +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);
|
|
|
|
}
|
|
|
|
|
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;
|
2000-08-25 12:26:06 +04:00
|
|
|
filename = rb_str_new2("-");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
init_p = 1;
|
2000-08-15 10:22:49 +04:00
|
|
|
first_p = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
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);
|
2001-05-02 08:22:21 +04:00
|
|
|
fn = StringValuePtr(filename);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (strlen(fn) == 1 && fn[0] == '-') {
|
|
|
|
current_file = rb_stdin;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ruby_inplace_mode) {
|
2000-08-25 12:26:06 +04:00
|
|
|
rb_warn("Can't do inplace edit for stdio");
|
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);
|
2000-08-25 12:26:06 +04:00
|
|
|
#ifdef NO_LONG_FNAME
|
1999-01-20 07:59:39 +03:00
|
|
|
ruby_add_suffix(str, ruby_inplace_mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
2000-04-10 09:48:43 +04:00
|
|
|
rb_str_cat2(str, ruby_inplace_mode);
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
2000-08-25 12:26:06 +04:00
|
|
|
#ifdef NO_SAFE_RENAME
|
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 {
|
2000-08-25 12:26:06 +04:00
|
|
|
#ifdef NO_SAFE_RENAME
|
|
|
|
rb_fatal("Can't do inplace edit without backup");
|
|
|
|
#else
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
fw = rb_fopen(fn, "w");
|
2000-08-25 12:26:06 +04:00
|
|
|
#ifndef NO_SAFE_RENAME
|
1998-01-16 15:13:05 +03:00
|
|
|
fstat(fileno(fw), &st2);
|
2000-11-13 08:39:35 +03:00
|
|
|
#ifdef HAVE_FCHMOD
|
1998-01-16 15:13:05 +03:00
|
|
|
fchmod(fileno(fw), st.st_mode);
|
2000-11-13 08:39:35 +03:00
|
|
|
#else
|
|
|
|
chmod(fn, st.st_mode);
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
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);
|
2000-08-25 12:26:06 +04:00
|
|
|
prep_path(rb_defout, fn);
|
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);
|
2000-08-25 12:26:06 +04:00
|
|
|
prep_path(current_file, fn);
|
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
|
2002-02-06 10:30:13 +03:00
|
|
|
argf_getline(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);
|
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
else if (argc == 0 && rb_rs == rb_default_rs) {
|
1999-08-13 09:45:20 +04:00
|
|
|
line = rb_io_gets(current_file);
|
|
|
|
}
|
|
|
|
else {
|
2002-02-06 10:30:13 +03:00
|
|
|
VALUE rs;
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
rs = rb_rs;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "1", &rs);
|
|
|
|
}
|
|
|
|
GetOpenFile(current_file, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
line = rb_io_getline(rs, fptr);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
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;
|
|
|
|
{
|
2002-02-06 10:30:13 +03:00
|
|
|
VALUE line = argf_getline(argc, argv);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
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();
|
2002-02-06 10:30:13 +03:00
|
|
|
while (!NIL_P(line = argf_getline(argc, argv))) {
|
1999-01-20 07:59:39 +03:00
|
|
|
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_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;
|
2002-03-25 17:50:40 +03:00
|
|
|
OpenFile *fptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(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);
|
2002-03-25 17:50:40 +03:00
|
|
|
|
|
|
|
GetOpenFile(port, fptr);
|
|
|
|
result = read_all(fptr, remain_size(fptr));
|
1998-01-16 15:13:05 +03:00
|
|
|
|
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)) {
|
2000-07-06 11:21:26 +04:00
|
|
|
tp = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
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
|
2000-07-06 11:21:26 +04:00
|
|
|
rp = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
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
|
2000-07-06 11:21:26 +04:00
|
|
|
wp = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-07-06 11:21:26 +04:00
|
|
|
else {
|
|
|
|
ep = 0;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2001-03-28 12:43:25 +04:00
|
|
|
#if !defined(MSDOS) && !defined(__human68k__)
|
2000-11-27 12:23:38 +03:00
|
|
|
static int
|
2001-06-05 11:19:39 +04:00
|
|
|
io_cntl(fd, cmd, narg, io_p)
|
2000-11-27 12:23:38 +03:00
|
|
|
int fd, cmd, io_p;
|
|
|
|
long narg;
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL
|
|
|
|
TRAP_BEG;
|
|
|
|
# if defined(__CYGWIN__)
|
|
|
|
retval = io_p?ioctl(fd, cmd, (void*)narg):fcntl(fd, cmd, narg);
|
|
|
|
# else
|
|
|
|
retval = io_p?ioctl(fd, cmd, narg):fcntl(fd, cmd, narg);
|
|
|
|
# endif
|
|
|
|
TRAP_END;
|
|
|
|
#else
|
|
|
|
if (!io_p) {
|
|
|
|
rb_notimplement();
|
|
|
|
}
|
|
|
|
TRAP_BEG;
|
|
|
|
retval = ioctl(fd, cmd, narg);
|
|
|
|
TRAP_END;
|
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
2001-03-28 12:43:25 +04:00
|
|
|
#endif
|
2000-11-27 12:23:38 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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__)
|
1999-10-16 14:33:06 +04:00
|
|
|
int cmd = NUM2ULONG(req);
|
1998-01-16 15:13:05 +03:00
|
|
|
OpenFile *fptr;
|
1999-01-20 07:59:39 +03:00
|
|
|
int len = 0;
|
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;
|
|
|
|
}
|
2001-06-05 11:19:39 +04:00
|
|
|
else if (rb_obj_is_kind_of(arg, rb_cInteger)) {
|
|
|
|
narg = NUM2LONG(arg);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(arg);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#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
|
|
|
}
|
2000-11-27 12:23:38 +03:00
|
|
|
retval = io_cntl(fileno(fptr->f), cmd, narg, io_p);
|
1998-01-16 15:13:05 +03:00
|
|
|
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
|
|
|
}
|
2000-11-27 12:23:38 +03:00
|
|
|
|
2001-02-13 08:09:11 +03:00
|
|
|
if (fptr->f2 && fileno(fptr->f) != fileno(fptr->f2)) {
|
|
|
|
/* call on f2 too; ignore result */
|
2000-11-27 12:23:38 +03:00
|
|
|
io_cntl(fileno(fptr->f2), cmd, narg, io_p);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
{
|
2000-08-09 11:35:01 +04:00
|
|
|
#if defined(HAVE_SYSCALL) && !defined(__CHECKER__)
|
1998-01-16 15:13:05 +03:00
|
|
|
#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)) {
|
2001-03-28 12:43:25 +04:00
|
|
|
arg[i] = (unsigned long)NUM2INT(*argv);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE v = *argv;
|
2001-03-28 12:43:25 +04:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(v);
|
2001-03-28 12:43:25 +04:00
|
|
|
rb_str_modify(v);
|
2001-05-02 08:22:21 +04:00
|
|
|
arg[i] = (unsigned long)RSTRING(v)->ptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-03-28 12:43:25 +04:00
|
|
|
argv++;
|
1998-01-16 15:13:05 +03:00
|
|
|
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];
|
2001-05-16 13:05:54 +04:00
|
|
|
VALUE r, w;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#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);
|
2000-05-01 13:42:38 +04:00
|
|
|
w = prep_stdio(rb_fdopen(pipes[1], "w"), FMODE_WRITABLE|FMODE_SYNC, rb_cIO);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-04 08:17:26 +04:00
|
|
|
return rb_assoc_new(r, w);
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
struct foreach_arg {
|
|
|
|
int argc;
|
|
|
|
VALUE sep;
|
|
|
|
VALUE io;
|
2002-02-06 10:30:13 +03:00
|
|
|
OpenFile *fptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
2001-01-18 11:43:14 +03:00
|
|
|
io_s_foreach(arg)
|
1998-01-16 15:13:05 +03:00
|
|
|
struct foreach_arg *arg;
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
2002-02-06 10:30:13 +03:00
|
|
|
while (!NIL_P(str = rb_io_getline(arg->sep, arg->fptr))) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_yield(str);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-02-06 10:30:13 +03:00
|
|
|
rb_io_s_foreach(argc, argv)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2002-02-06 10:30:13 +03:00
|
|
|
VALUE fname, io;
|
|
|
|
OpenFile *fptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
struct foreach_arg arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &fname, &arg.sep);
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(fname);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-02-06 10:30:13 +03:00
|
|
|
if (argc == 1) {
|
|
|
|
arg.sep = rb_default_rs;
|
|
|
|
}
|
|
|
|
io = rb_io_open(RSTRING(fname)->ptr, "r");
|
|
|
|
if (NIL_P(io)) return Qnil;
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
arg.fptr = fptr;
|
|
|
|
|
|
|
|
return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, io);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2001-01-18 11:43:14 +03:00
|
|
|
io_s_readlines(arg)
|
1998-01-16 15:13:05 +03:00
|
|
|
struct foreach_arg *arg;
|
|
|
|
{
|
2001-01-18 11:43:14 +03:00
|
|
|
return rb_io_readlines(arg->argc, &arg->sep, arg->io);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(fname);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
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;
|
2001-01-18 11:43:14 +03:00
|
|
|
return rb_ensure(io_s_readlines, (VALUE)&arg, rb_io_close, arg.io);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
io_s_read(arg)
|
|
|
|
struct foreach_arg *arg;
|
|
|
|
{
|
|
|
|
return io_read(arg->argc, &arg->sep, arg->io);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_s_read(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE fname, offset;
|
|
|
|
struct foreach_arg arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "12", &fname, &arg.sep, &offset);
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(fname);
|
2001-01-18 11:43:14 +03:00
|
|
|
|
|
|
|
arg.argc = argc ? 1 : 0;
|
|
|
|
arg.io = rb_io_open(RSTRING(fname)->ptr, "r");
|
|
|
|
if (NIL_P(arg.io)) return Qnil;
|
|
|
|
if (!NIL_P(offset)) {
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_io_seek(arg.io, offset, SEEK_SET);
|
2001-01-18 11:43:14 +03:00
|
|
|
}
|
|
|
|
return rb_ensure(io_s_read, (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
|
2001-03-13 08:45:13 +03:00
|
|
|
argf_seek_m(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
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();
|
|
|
|
}
|
2001-03-13 08:45:13 +03:00
|
|
|
return rb_io_seek_m(argc, argv, current_file);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_set_pos(self, offset)
|
|
|
|
VALUE self, offset;
|
|
|
|
{
|
|
|
|
if (!next_argv()) {
|
2001-01-18 11:43:14 +03:00
|
|
|
rb_raise(rb_eArgError, "no stream to set position");
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2002-05-29 09:20:39 +04:00
|
|
|
int len = 0;
|
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();
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(tmp);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
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;
|
2000-04-10 09:48:43 +04:00
|
|
|
else rb_str_append(str, tmp);
|
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()
|
|
|
|
{
|
2000-08-15 10:22:49 +04:00
|
|
|
if (!next_argv()) return Qtrue;
|
2001-08-23 10:02:15 +04:00
|
|
|
if (next_p == 1) {
|
1999-08-13 09:45:20 +04:00
|
|
|
return Qtrue;
|
2000-08-15 10:22:49 +04:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
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;
|
|
|
|
|
2002-02-06 10:30:13 +03:00
|
|
|
while (RTEST(str = argf_getline(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
|
|
|
{
|
2002-03-28 12:17:54 +03:00
|
|
|
next_argv();
|
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
|
|
|
{
|
2002-03-28 12:17:54 +03:00
|
|
|
next_argv();
|
1999-08-13 09:45:20 +04:00
|
|
|
return current_file;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-03-28 12:17:54 +03:00
|
|
|
static VALUE
|
|
|
|
argf_binmode()
|
|
|
|
{
|
|
|
|
binmode = 1;
|
|
|
|
next_argv();
|
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
argf_forward();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_io_binmode(current_file);
|
|
|
|
}
|
|
|
|
return argf;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2001-06-22 13:12:24 +04:00
|
|
|
if (!RTEST(val)) {
|
|
|
|
if (ruby_inplace_mode) free(ruby_inplace_mode);
|
|
|
|
ruby_inplace_mode = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
StringValue(val);
|
2001-05-02 08:22:21 +04:00
|
|
|
if (ruby_inplace_mode) free(ruby_inplace_mode);
|
2001-06-19 08:35:17 +04:00
|
|
|
ruby_inplace_mode = 0;
|
2001-05-02 08:22:21 +04:00
|
|
|
ruby_inplace_mode = strdup(RSTRING(val)->ptr);
|
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);
|
|
|
|
|
2001-10-03 11:19:19 +04:00
|
|
|
rb_define_singleton_method(rb_cIO, "allocate", rb_io_s_alloc, 0);
|
2002-01-18 17:24:01 +03:00
|
|
|
rb_define_singleton_method(rb_cIO, "new", rb_io_s_new, -1);
|
|
|
|
rb_define_singleton_method(rb_cIO, "open", rb_io_s_open, -1);
|
2002-05-23 09:35:32 +04:00
|
|
|
rb_define_singleton_method(rb_cIO, "sysopen", rb_io_s_sysopen, -1);
|
2002-01-18 17:24:01 +03:00
|
|
|
rb_define_singleton_method(rb_cIO, "for_fd", rb_class_new_instance, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2001-01-18 11:43:14 +03:00
|
|
|
rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
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
|
|
|
|
2002-01-18 17:24:01 +03:00
|
|
|
rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_output_fs = Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
|
|
|
|
|
2001-10-29 08:07:26 +03:00
|
|
|
rb_rs = rb_default_rs = rb_str_new2("\n");
|
|
|
|
rb_output_rs = Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_global_variable(&rb_default_rs);
|
2000-02-01 06:12:21 +03:00
|
|
|
OBJ_FREEZE(rb_default_rs); /* avoid modifying RS_default */
|
2001-05-02 08:22:21 +04:00
|
|
|
rb_define_variable("$/", &rb_rs);
|
|
|
|
rb_define_variable("$-0", &rb_rs);
|
|
|
|
rb_define_variable("$\\", &rb_output_rs);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
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);
|
2000-02-29 11:05:32 +03:00
|
|
|
rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2002-01-23 10:30:43 +03:00
|
|
|
rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_define_method(rb_cIO, "seek", rb_io_seek_m, -1);
|
1999-11-04 11:39:57 +03:00
|
|
|
rb_define_const(rb_cIO, "SEEK_SET", INT2FIX(SEEK_SET));
|
|
|
|
rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
|
|
|
|
rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
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);
|
2002-03-27 08:28:00 +03:00
|
|
|
rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
|
|
|
|
rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
|
2000-05-24 08:34:26 +04:00
|
|
|
rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
|
2002-02-22 13:28:47 +03:00
|
|
|
rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
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);
|
1999-11-17 10:30:37 +03:00
|
|
|
rb_define_hooked_variable("$defout", &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);
|
2001-03-13 08:45:13 +03:00
|
|
|
rb_define_singleton_method(argf, "seek", argf_seek_m, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
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);
|
|
|
|
|
1999-11-25 12:03:08 +03:00
|
|
|
#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
|
1998-01-16 15:13:05 +03:00
|
|
|
atexit(pipe_atexit);
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
Init_File();
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2000-03-23 11:37:35 +03:00
|
|
|
rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
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
|
2000-02-17 10:11:22 +03:00
|
|
|
#ifdef O_SYNC
|
2000-02-18 09:59:36 +03:00
|
|
|
rb_file_const("SYNC", INT2FIX(O_SYNC));
|
2000-02-17 10:11:22 +03:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|