2000-05-01 05:42:38 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
io.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Oct 15 18:08:59 JST 1993
|
|
|
|
|
2003-01-16 02:34:03 -05:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-01 05:42:38 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-03-22 02:26:42 -05:00
|
|
|
#if defined(__VMS)
|
|
|
|
#define _XOPEN_SOURCE
|
|
|
|
#define _POSIX_C_SOURCE 2
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
#include "ruby.h"
|
1999-01-19 23:59:39 -05:00
|
|
|
#include "rubyio.h"
|
|
|
|
#include "rubysig.h"
|
1999-08-13 01:45:20 -04:00
|
|
|
#include "env.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__human68k__) || defined(__EMX__) || defined(__BEOS__)
|
2000-08-25 04:26:06 -04:00
|
|
|
# define NO_SAFE_RENAME
|
|
|
|
#endif
|
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if defined(MSDOS) || defined(__CYGWIN__) || defined(_WIN32)
|
2000-08-25 04:26:06 -04:00
|
|
|
# define NO_LONG_FNAME
|
|
|
|
#endif
|
|
|
|
|
2001-09-05 02:54:57 -04:00
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(sun) || defined(_nec_ews)
|
2001-05-02 00:22:21 -04:00
|
|
|
# define USE_SETVBUF
|
|
|
|
#endif
|
|
|
|
|
2001-08-05 23:05:23 -04:00
|
|
|
#ifdef __QNXNTO__
|
|
|
|
#include "unix.h"
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <sys/types.h>
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if !defined(DJGPP) && !defined(_WIN32) && !defined(__human68k__)
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if defined(HAVE_FCNTL_H) || defined(_WIN32)
|
1998-01-16 07:13:05 -05:00
|
|
|
#include <fcntl.h>
|
1999-08-13 01:45:20 -04:00
|
|
|
#elif defined(HAVE_SYS_FCNTL_H)
|
|
|
|
#include <sys/fcntl.h>
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
|
2002-03-14 01:23:46 -05: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 07:13:05 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2002-05-02 03:50:36 -04:00
|
|
|
/* EMX has sys/param.h, but.. */
|
|
|
|
#if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
|
1998-01-16 07:13:05 -05:00
|
|
|
# include <sys/param.h>
|
2002-08-16 03:23:04 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined NOFILE
|
1998-01-16 07:13:05 -05:00
|
|
|
# define NOFILE 64
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
extern void Init_File _((void));
|
|
|
|
|
|
|
|
#ifdef __BEOS__
|
2000-05-15 22:46:57 -04:00
|
|
|
# ifndef NOFILE
|
1999-08-13 01:45:20 -04:00
|
|
|
# define NOFILE (OPEN_MAX)
|
|
|
|
# endif
|
1999-01-19 23:59:39 -05:00
|
|
|
#include <net/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "util.h"
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-03-27 00:28:00 -05: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-19 23:59:39 -05:00
|
|
|
VALUE rb_cIO;
|
|
|
|
VALUE rb_eEOFError;
|
|
|
|
VALUE rb_eIOError;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
VALUE rb_stdin, rb_stdout, rb_stderr, rb_defout;
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE orig_stdin, orig_stdout, orig_stderr;
|
2001-02-26 00:29:06 -05:00
|
|
|
static int saved_fd[3] = {0, 1, 2};
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE rb_output_fs;
|
|
|
|
VALUE rb_rs;
|
|
|
|
VALUE rb_output_rs;
|
|
|
|
VALUE rb_default_rs;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static VALUE argf;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static ID id_write;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
extern char *ruby_inplace_mode;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
struct timeval rb_time_interval _((VALUE));
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE filename, current_file;
|
1999-01-19 23:59:39 -05:00
|
|
|
static int gets_lineno;
|
2000-08-15 02:22:49 -04:00
|
|
|
static int init_p = 0, next_p = 0, first_p = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE lineno;
|
1998-01-16 07:13:05 -05: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 05:07:33 -04:00
|
|
|
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_IO_read_end - (fp)->_IO_read_ptr)
|
2002-03-25 09:50:40 -05:00
|
|
|
# define READ_DATA_PENDING_PTR(fp) ((fp)->_IO_read_ptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
# else
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
|
2001-07-24 05:07:33 -04:00
|
|
|
# define READ_DATA_PENDING_COUNT(fp) ((fp)->_egptr - (fp)->_gptr)
|
2002-03-25 09:50:40 -05:00
|
|
|
# define READ_DATA_PENDING_PTR(fp) ((fp)->_gptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
# endif
|
1999-01-19 23:59:39 -05:00
|
|
|
#elif defined(FILE_COUNT)
|
|
|
|
# define READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
|
2001-07-24 05:07:33 -04:00
|
|
|
# define READ_DATA_PENDING_COUNT(fp) ((fp)->FILE_COUNT)
|
2002-03-25 09:50:40 -05: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-19 23:59:39 -05:00
|
|
|
#elif defined(__BEOS__)
|
2000-05-24 00:34:26 -04:00
|
|
|
# define READ_DATA_PENDING(fp) (fp->_state._eof == 0)
|
2002-03-22 02:26:42 -05:00
|
|
|
#elif defined(__VMS)
|
|
|
|
# define READ_DATA_PENDING(fp) (((unsigned int)((*(fp))->_flag) & _IOEOF) == 0)
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
|
|
|
/* requires systems own version of the ReadDataPending() */
|
|
|
|
extern int ReadDataPending();
|
2002-03-27 00:28:00 -05:00
|
|
|
# define READ_DATA_PENDING(fp) (!feof(fp))
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
2002-10-27 04:04:55 -05:00
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
#ifndef READ_DATA_PENDING_PTR
|
|
|
|
# ifdef FILE_READPTR
|
|
|
|
# define READ_DATA_PENDING_PTR(fp) ((fp)->FILE_READPTR)
|
|
|
|
# endif
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-27 04:04:55 -05:00
|
|
|
#if defined __DJGPP__
|
|
|
|
# undef READ_DATA_PENDING_COUNT
|
|
|
|
# undef READ_DATA_PENDING_PTR
|
|
|
|
#endif
|
|
|
|
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_eof_error()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eEOFError, "End of file reached");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-11-10 02:16:52 -05: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");
|
2002-09-03 01:20:14 -04:00
|
|
|
rb_check_frozen(io);
|
2000-11-10 02:16:52 -05:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_closed(fptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
if (!fptr) {
|
|
|
|
rb_raise(rb_eIOError, "uninitialized stream");
|
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
if (!fptr->f && !fptr->f2) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eIOError, "closed stream");
|
2000-07-06 03:21:26 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-10-02 10:59:25 -04:00
|
|
|
static void io_fflush _((FILE *, OpenFile *));
|
|
|
|
|
|
|
|
#if NEED_IO_FLUSH_BEFORE_SEEK
|
|
|
|
static OpenFile *
|
|
|
|
flush_before_seek(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
2003-01-09 03:05:32 -05:00
|
|
|
if (fptr->mode & FMODE_WBUF) {
|
|
|
|
io_fflush(GetWriteFile(fptr), fptr);
|
2002-10-02 10:59:25 -04:00
|
|
|
}
|
|
|
|
return fptr;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define flush_before_seek(fptr) fptr
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define io_seek(fptr, ofs, whence) fseeko(flush_before_seek(fptr)->f, ofs, whence)
|
|
|
|
#define io_tell(fptr) ftello(flush_before_seek(fptr)->f)
|
|
|
|
|
2003-01-09 03:05:32 -05:00
|
|
|
#ifndef SEEK_CUR
|
|
|
|
# define SEEK_SET 0
|
|
|
|
# define SEEK_CUR 1
|
|
|
|
# define SEEK_END 2
|
|
|
|
#endif
|
|
|
|
|
2003-01-10 11:47:03 -05:00
|
|
|
#define FMODE_SYNCWRITE (FMODE_SYNC|FMODE_WRITABLE)
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_readable(fptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
if (!(fptr->mode & FMODE_READABLE)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eIOError, "not opened for reading");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2003-01-09 03:05:32 -05:00
|
|
|
#if NEED_IO_SEEK_BETWEEN_RW
|
2003-01-10 11:47:03 -05:00
|
|
|
if (((fptr->mode & FMODE_WBUF) ||
|
|
|
|
(fptr->mode & (FMODE_SYNCWRITE|FMODE_RBUF)) == FMODE_SYNCWRITE) &&
|
|
|
|
!fptr->f2) {
|
2003-01-09 03:05:32 -05:00
|
|
|
io_seek(fptr, 0, SEEK_CUR);
|
2002-10-02 10:59:25 -04:00
|
|
|
}
|
|
|
|
fptr->mode |= FMODE_RBUF;
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
|
|
|
rb_io_check_writable(fptr)
|
|
|
|
OpenFile *fptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!(fptr->mode & FMODE_WRITABLE)) {
|
|
|
|
rb_raise(rb_eIOError, "not opened for writing");
|
|
|
|
}
|
2003-01-09 03:05:32 -05:00
|
|
|
#if NEED_IO_SEEK_BETWEEN_RW
|
2002-10-02 10:59:25 -04:00
|
|
|
if ((fptr->mode & FMODE_RBUF) && !fptr->f2) {
|
2003-01-09 03:05:32 -05:00
|
|
|
io_seek(fptr, 0, SEEK_CUR);
|
2002-10-02 10:59:25 -04:00
|
|
|
}
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-01-17 03:37:53 -05:00
|
|
|
int
|
|
|
|
rb_read_pending(fp)
|
1999-08-13 01:45:20 -04:00
|
|
|
FILE *fp;
|
|
|
|
{
|
2000-01-17 03:37:53 -05:00
|
|
|
return READ_DATA_PENDING(fp);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
|
2000-01-31 22:18:03 -05:00
|
|
|
void
|
|
|
|
rb_read_check(fp)
|
|
|
|
FILE *fp;
|
|
|
|
{
|
|
|
|
if (!READ_DATA_PENDING(fp)) {
|
|
|
|
rb_thread_wait_fd(fileno(fp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
static int
|
2001-07-14 11:17:19 -04:00
|
|
|
ruby_dup(orig)
|
2000-05-12 05: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;
|
|
|
|
}
|
|
|
|
|
* array.c (ary_alloc), dir.c (dir_s_alloc), eval.c (thgroup_s_alloc),
file.c (rb_stat_s_alloc), hash.c (hash_alloc), io.c (io_alloc),
object.c (rb_module_s_alloc, rb_class_allocate_instance),
re.c (match_alloc, rb_reg_s_alloc), string.c (str_alloc),
time.c (time_s_alloc), ext/digest/digest.c (rb_digest_base_alloc),
ext/tcltklib/tcltklib.c (ip_alloc),
ext/win32ole/win32ole.c (fole_s_allocate, fev_s_allocate)
: add prototype to get rid of VC++ warnings.
* ext/sdbm/init.c (fsdbm_alloc): allocator takes only one argument.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-21 13:02:01 -05:00
|
|
|
static VALUE io_alloc _((VALUE));
|
2002-12-20 03:33:17 -05:00
|
|
|
static VALUE
|
|
|
|
io_alloc(klass)
|
|
|
|
VALUE klass;
|
|
|
|
{
|
|
|
|
NEWOBJ(io, struct RFile);
|
|
|
|
OBJSETUP(io, klass, T_FILE);
|
|
|
|
|
|
|
|
io->fptr = 0;
|
|
|
|
|
|
|
|
return (VALUE)io;
|
|
|
|
}
|
|
|
|
|
2000-11-08 00:29:37 -05:00
|
|
|
static void
|
2002-03-29 09:50:09 -05:00
|
|
|
io_fflush(f, fptr)
|
2000-11-08 00:29:37 -05:00
|
|
|
FILE *f;
|
2002-03-29 09:50:09 -05:00
|
|
|
OpenFile *fptr;
|
2000-11-08 00:29:37 -05:00
|
|
|
{
|
2000-11-16 23:41:19 -05:00
|
|
|
int n;
|
|
|
|
|
2000-11-08 00:29:37 -05:00
|
|
|
rb_thread_fd_writable(fileno(f));
|
2002-10-09 02:12:54 -04:00
|
|
|
for (;;) {
|
|
|
|
TRAP_BEG;
|
|
|
|
n = fflush(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (n != EOF) break;
|
|
|
|
if (!rb_io_wait_writable(fileno(f)))
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
2002-03-28 04:17:54 -05:00
|
|
|
fptr->mode &= ~FMODE_WBUF;
|
2000-11-08 00:29:37 -05:00
|
|
|
}
|
|
|
|
|
2002-10-02 10:13:58 -04:00
|
|
|
int
|
2002-09-10 21:09:04 -04:00
|
|
|
rb_io_wait_readable(f)
|
|
|
|
int f;
|
|
|
|
{
|
|
|
|
fd_set rfds;
|
2002-10-02 10:13:58 -04:00
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
#if defined(ERESTART)
|
|
|
|
case ERESTART:
|
|
|
|
#endif
|
|
|
|
rb_thread_wait_fd(f);
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
case EAGAIN:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(f, &rfds);
|
|
|
|
rb_thread_select(f + 1, &rfds, NULL, NULL, NULL);
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2002-09-10 21:09:04 -04:00
|
|
|
}
|
|
|
|
|
2002-10-02 10:13:58 -04:00
|
|
|
int
|
2002-09-10 21:09:04 -04:00
|
|
|
rb_io_wait_writable(f)
|
|
|
|
int f;
|
|
|
|
{
|
|
|
|
fd_set wfds;
|
2002-10-02 10:13:58 -04:00
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
#if defined(ERESTART)
|
|
|
|
case ERESTART:
|
|
|
|
#endif
|
|
|
|
rb_thread_fd_writable(f);
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
case EAGAIN:
|
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
#endif
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(f, &wfds);
|
|
|
|
rb_thread_select(f + 1, NULL, &wfds, NULL, NULL);
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2002-09-10 21:09:04 -04:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
/* writing functions */
|
2002-12-20 04:29:41 -05:00
|
|
|
long
|
|
|
|
rb_io_fwrite(ptr, len, f)
|
|
|
|
const char *ptr;
|
|
|
|
long len;
|
|
|
|
FILE *f;
|
|
|
|
{
|
|
|
|
long n, r;
|
|
|
|
|
|
|
|
if ((n = len) <= 0) return n;
|
|
|
|
#ifdef __human68k__
|
|
|
|
do {
|
|
|
|
if (fputc(*ptr++, f) == EOF) {
|
|
|
|
if (ferror(f)) return -1L;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (--n > 0);
|
|
|
|
#else
|
|
|
|
while (ptr += (r = fwrite(ptr, 1, n, f)), (n -= r) > 0) {
|
|
|
|
if (ferror(f)) {
|
|
|
|
if (rb_io_wait_writable(fileno(f))) {
|
|
|
|
clearerr(f);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return -1L;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return len - n;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE
|
|
|
|
io_write(io, str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE io, str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
2002-12-20 04:29:41 -05:00
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_secure(4);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (TYPE(str) != T_STRING)
|
1999-01-19 23:59:39 -05:00
|
|
|
str = rb_obj_as_string(str);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRING(str)->len == 0) return INT2FIX(0);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-24 04:21:56 -04:00
|
|
|
if (TYPE(io) != T_FILE) {
|
1999-01-19 23:59:39 -05:00
|
|
|
/* port is not IO, call write method for it. */
|
1998-01-16 07:13:05 -05:00
|
|
|
return rb_funcall(io, id_write, 1, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_writable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
|
2002-12-20 04:29:41 -05:00
|
|
|
n = rb_io_fwrite(RSTRING(str)->ptr, RSTRING(str)->len, f);
|
|
|
|
if (n == -1L) rb_sys_fail(fptr->path);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (fptr->mode & FMODE_SYNC) {
|
2002-03-29 09:50:09 -05:00
|
|
|
io_fflush(f, fptr);
|
2002-02-06 23:18:08 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fptr->mode |= FMODE_WBUF;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-04-24 00:54:16 -04:00
|
|
|
return LONG2FIX(n);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE
|
|
|
|
rb_io_write(io, str)
|
|
|
|
VALUE io, str;
|
|
|
|
{
|
|
|
|
return rb_funcall(io, id_write, 1, str);
|
|
|
|
}
|
|
|
|
|
2002-02-20 01:35:37 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_addstr(io, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io, str;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_write(io, str);
|
1998-01-16 07:13:05 -05:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_flush(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_writable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
f = GetWriteFile(fptr);
|
2002-03-29 09:50:09 -05:00
|
|
|
|
|
|
|
io_fflush(f, fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_tell(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2002-03-14 01:23:46 -05:00
|
|
|
off_t pos;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-10-02 10:59:25 -04:00
|
|
|
pos = io_tell(fptr);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (ferror(fptr->f)) rb_sys_fail(fptr->path);
|
2002-03-27 00:28:00 -05:00
|
|
|
return OFFT2NUM(pos);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2001-03-13 00:45:13 -05:00
|
|
|
rb_io_seek(io, offset, whence)
|
|
|
|
VALUE io, offset;
|
|
|
|
int whence;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2002-04-18 04:46:18 -04:00
|
|
|
off_t pos;
|
2001-03-13 00:45:13 -05:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-10-02 10:59:25 -04:00
|
|
|
pos = io_seek(fptr, NUM2OFFT(offset), whence);
|
2001-03-13 00:45:13 -05: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 03:48:31 -04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
2000-06-12 03:48:31 -04:00
|
|
|
VALUE offset, ptrname;
|
2002-04-18 04:46:18 -04:00
|
|
|
int whence = SEEK_SET;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
|
|
|
|
whence = NUM2INT(ptrname);
|
|
|
|
}
|
2000-06-12 03:48:31 -04:00
|
|
|
|
2001-03-13 00:45:13 -05:00
|
|
|
return rb_io_seek(io, offset, whence);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_set_pos(io, offset)
|
|
|
|
VALUE io, offset;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2002-04-18 04:46:18 -04:00
|
|
|
off_t pos;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-10-02 10:59:25 -04:00
|
|
|
pos = io_seek(fptr, NUM2OFFT(offset), SEEK_SET);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (pos != 0) rb_sys_fail(fptr->path);
|
|
|
|
clearerr(fptr->f);
|
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
return OFFT2NUM(pos);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_rewind(io)
|
2002-03-14 01:23:46 -05:00
|
|
|
VALUE io;
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2002-10-02 10:59:25 -04:00
|
|
|
if (io_seek(fptr, 0L, 0) != 0) rb_sys_fail(fptr->path);
|
1999-01-19 23:59:39 -05:00
|
|
|
clearerr(fptr->f);
|
2000-06-19 04:38:11 -04:00
|
|
|
if (io == current_file) {
|
|
|
|
gets_lineno -= fptr->lineno;
|
|
|
|
}
|
|
|
|
fptr->lineno = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_io_eof(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (feof(fptr->f)) return Qtrue;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (READ_DATA_PENDING(fptr->f)) return Qfalse;
|
1999-01-19 23:59:39 -05:00
|
|
|
READ_CHECK(fptr->f);
|
1998-01-16 07:13:05 -05:00
|
|
|
TRAP_BEG;
|
|
|
|
ch = getc(fptr->f);
|
|
|
|
TRAP_END;
|
|
|
|
|
|
|
|
if (ch != EOF) {
|
1999-01-19 23:59:39 -05:00
|
|
|
ungetc(ch, fptr->f);
|
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_sync(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_set_sync(io, mode)
|
1998-01-16 07:13:05 -05: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 02:30:43 -05: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 09:50:09 -05:00
|
|
|
|
|
|
|
io_fflush(f, fptr);
|
2002-01-23 02:30:43 -05: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 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_fileno(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
fd = fileno(fptr->f);
|
|
|
|
return INT2FIX(fd);
|
|
|
|
}
|
|
|
|
|
2000-05-24 00: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 05:28:47 -05:00
|
|
|
static VALUE
|
|
|
|
rb_io_inspect(obj)
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
char *buf, *cname;
|
|
|
|
|
2002-09-30 07:31:28 -04:00
|
|
|
fptr = RFILE(rb_io_taint_check(obj))->fptr;
|
|
|
|
if (!fptr || !(fptr->f || fptr->f2) || !fptr->path) return rb_any_to_s(obj);
|
2003-01-30 23:00:17 -05:00
|
|
|
cname = rb_obj_classname(obj);
|
2002-02-22 05:28:47 -05:00
|
|
|
buf = ALLOCA_N(char, strlen(cname) + strlen(fptr->path) + 5);
|
|
|
|
sprintf(buf, "#<%s:%s>", cname, fptr->path);
|
|
|
|
return rb_str_new2(buf);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_io_to_io(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
/* reading functions */
|
2002-08-15 22:52:25 -04:00
|
|
|
long
|
|
|
|
rb_io_fread(ptr, len, f)
|
2000-07-14 03:18:58 -04:00
|
|
|
char *ptr;
|
2000-11-20 02:31:55 -05:00
|
|
|
long len;
|
2000-07-14 03:18:58 -04:00
|
|
|
FILE *f;
|
|
|
|
{
|
2000-11-20 02:31:55 -05:00
|
|
|
long n = len;
|
2000-07-14 03:18:58 -04:00
|
|
|
int c;
|
|
|
|
|
2001-07-24 05:07:33 -04:00
|
|
|
while (n > 0) {
|
|
|
|
#ifdef READ_DATA_PENDING_COUNT
|
2002-08-28 04:05:23 -04:00
|
|
|
long i = READ_DATA_PENDING_COUNT(f);
|
2001-07-24 05:07:33 -04:00
|
|
|
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 03:18:58 -04:00
|
|
|
if (!READ_DATA_PENDING(f)) {
|
|
|
|
rb_thread_wait_fd(fileno(f));
|
|
|
|
}
|
2001-07-24 05:07:33 -04:00
|
|
|
#endif
|
2000-07-14 03:18:58 -04:00
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c == EOF) {
|
2001-07-24 05:07:33 -04:00
|
|
|
eof:
|
2000-07-14 03:18:58 -04:00
|
|
|
if (ferror(f)) {
|
2001-12-19 02:48:32 -05:00
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
2002-10-02 10:13:58 -04:00
|
|
|
#if defined(ERESTART)
|
|
|
|
case ERESTART:
|
|
|
|
#endif
|
2002-09-10 21:09:04 -04:00
|
|
|
clearerr(f);
|
2001-12-19 02:48:32 -05:00
|
|
|
continue;
|
|
|
|
case EAGAIN:
|
2001-12-10 02:18:16 -05:00
|
|
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
2001-12-19 02:48:32 -05:00
|
|
|
case EWOULDBLOCK:
|
2001-12-10 02:18:16 -05:00
|
|
|
#endif
|
2003-03-03 00:17:39 -05:00
|
|
|
if (len - n > 0) {
|
|
|
|
clearerr(f);
|
|
|
|
return len - n;
|
|
|
|
}
|
2001-12-19 02:48:32 -05:00
|
|
|
}
|
2001-12-21 04:23:28 -05:00
|
|
|
return 0;
|
2000-07-14 03:18:58 -04:00
|
|
|
}
|
|
|
|
*ptr = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*ptr++ = c;
|
2001-07-24 05:07:33 -04:00
|
|
|
n--;
|
2000-07-14 03:18:58 -04:00
|
|
|
}
|
2001-07-24 05:07:33 -04:00
|
|
|
return len - n;
|
2000-07-14 03:18:58 -04:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
#ifndef S_ISREG
|
|
|
|
# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SMALLBUF 100
|
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
static long
|
|
|
|
remain_size(fptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
2002-03-25 09:50:40 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
struct stat st;
|
2002-03-14 01:23:46 -05:00
|
|
|
off_t siz = BUFSIZ;
|
2002-04-24 00:54:16 -04:00
|
|
|
off_t pos;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
if (feof(fptr->f)) return 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (fstat(fileno(fptr->f), &st) == 0 && S_ISREG(st.st_mode)
|
|
|
|
#ifdef __BEOS__
|
|
|
|
&& (st.st_dev > 3)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
2002-10-02 10:59:25 -04:00
|
|
|
pos = io_tell(fptr);
|
2002-03-25 01:18:07 -05:00
|
|
|
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 07:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-25 09:50:40 -05:00
|
|
|
return (long)siz;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-12-11 04:32:41 -05:00
|
|
|
read_all(fptr, siz, str)
|
2002-03-25 09:50:40 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
long siz;
|
|
|
|
VALUE str;
|
2002-12-11 04:32:41 -05:00
|
|
|
{
|
2002-03-25 09:50:40 -05:00
|
|
|
long bytes = 0;
|
2002-04-24 00:54:16 -04:00
|
|
|
long n;
|
2002-08-29 00:38:37 -04:00
|
|
|
off_t pos = 0;
|
2002-03-25 09:50:40 -05:00
|
|
|
|
|
|
|
if (feof(fptr->f)) return Qnil;
|
2000-07-14 03:18:58 -04:00
|
|
|
READ_CHECK(fptr->f);
|
2002-03-25 09:50:40 -05:00
|
|
|
if (!siz) siz = BUFSIZ;
|
2002-12-11 04:32:41 -05:00
|
|
|
if (NIL_P(str)) {
|
|
|
|
str = rb_tainted_str_new(0, siz);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_str_resize(str, siz);
|
|
|
|
}
|
2002-10-02 10:59:25 -04:00
|
|
|
pos = io_tell(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
for (;;) {
|
2002-08-15 22:52:25 -04:00
|
|
|
n = rb_io_fread(RSTRING(str)->ptr+bytes, siz-bytes, fptr->f);
|
2002-08-27 04:31:08 -04:00
|
|
|
if (pos > 0 && n == 0 && bytes == 0) {
|
2003-03-20 01:27:22 -05:00
|
|
|
rb_str_resize(str,0);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (feof(fptr->f)) return Qnil;
|
2003-03-20 01:27:22 -05:00
|
|
|
if (!ferror(fptr->f)) return str;
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_sys_fail(fptr->path);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
bytes += n;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (bytes < siz) break;
|
1998-01-16 07:19:22 -05:00
|
|
|
siz += BUFSIZ;
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_resize(str, siz);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (bytes == 0) return rb_str_new(0,0);
|
|
|
|
if (bytes != siz) rb_str_resize(str, bytes);
|
|
|
|
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
io_read(argc, argv, io)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2002-08-21 11:47:54 -04:00
|
|
|
long n, len;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE length, str;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-12-11 04:32:41 -05:00
|
|
|
rb_scan_args(argc, argv, "02", &length, &str);
|
2002-03-25 09:50:40 -05:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(length)) {
|
2002-12-11 04:32:41 -05:00
|
|
|
return read_all(fptr, remain_size(fptr), str);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-08-21 11:47:54 -04:00
|
|
|
len = NUM2LONG(length);
|
1999-10-13 02:44:42 -04:00
|
|
|
if (len < 0) {
|
2002-10-29 16:35:28 -05:00
|
|
|
rb_raise(rb_eArgError, "negative length %ld given", len);
|
1999-10-13 02:44:42 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (feof(fptr->f)) return Qnil;
|
2002-12-11 04:32:41 -05:00
|
|
|
if (NIL_P(str)) {
|
|
|
|
str = rb_str_new(0, len);
|
|
|
|
if (len == 0) return str;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
StringValue(str);
|
|
|
|
rb_str_modify(str);
|
2003-03-20 01:27:22 -05:00
|
|
|
rb_str_resize(str,len);
|
2002-12-11 04:32:41 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
READ_CHECK(fptr->f);
|
2002-08-15 22:52:25 -04:00
|
|
|
n = rb_io_fread(RSTRING(str)->ptr, len, fptr->f);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (n == 0) {
|
2003-03-20 01:27:22 -05:00
|
|
|
rb_str_resize(str,0);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (feof(fptr->f)) return Qnil;
|
|
|
|
rb_sys_fail(fptr->path);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
RSTRING(str)->len = n;
|
|
|
|
RSTRING(str)->ptr[n] = '\0';
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJ_TAINT(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
static int
|
|
|
|
appendline(fptr, delim, strp)
|
2002-02-06 02:30:13 -05:00
|
|
|
OpenFile *fptr;
|
2002-03-25 09:50:40 -05:00
|
|
|
int delim;
|
|
|
|
VALUE *strp;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-02-06 02:30:13 -05:00
|
|
|
FILE *f = fptr->f;
|
2002-03-25 09:50:40 -05:00
|
|
|
VALUE str = *strp;
|
|
|
|
int c = EOF;
|
|
|
|
#ifndef READ_DATA_PENDING_PTR
|
2002-02-06 02:30:13 -05:00
|
|
|
char buf[8192];
|
2002-03-25 09:50:40 -05:00
|
|
|
char *bp = buf, *bpe = buf + sizeof buf - 3;
|
2002-02-06 02:30:13 -05:00
|
|
|
int cnt;
|
2002-03-25 09:50:40 -05:00
|
|
|
#endif
|
2002-02-06 02:30:13 -05:00
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
do {
|
|
|
|
#ifdef READ_DATA_PENDING_PTR
|
2002-08-28 04:05:23 -04:00
|
|
|
long pending = READ_DATA_PENDING_COUNT(f);
|
2002-03-25 09:50:40 -05:00
|
|
|
if (pending > 0) {
|
|
|
|
const char *p = READ_DATA_PENDING_PTR(f);
|
|
|
|
const char *e = memchr(p, delim, pending);
|
2002-08-28 10:36:38 -04:00
|
|
|
long last = 0, len = (c != EOF);
|
2002-03-25 09:50:40 -05:00
|
|
|
if (e) pending = e - p + 1;
|
2002-08-28 10:36:38 -04:00
|
|
|
len += pending;
|
2002-03-25 09:50:40 -05:00
|
|
|
if (!NIL_P(str)) {
|
|
|
|
last = RSTRING(str)->len;
|
2002-08-28 10:36:38 -04:00
|
|
|
rb_str_resize(str, last + len);
|
2002-03-25 09:50:40 -05:00
|
|
|
}
|
|
|
|
else {
|
2002-08-28 10:36:38 -04:00
|
|
|
*strp = str = rb_str_buf_new(len);
|
|
|
|
RSTRING(str)->len = len;
|
2002-09-25 03:03:05 -04:00
|
|
|
RSTRING(str)->ptr[len] = '\0';
|
2002-03-25 09:50:40 -05:00
|
|
|
}
|
|
|
|
if (c != EOF) {
|
|
|
|
RSTRING(str)->ptr[last++] = c;
|
|
|
|
}
|
|
|
|
fread(RSTRING(str)->ptr + last, 1, pending, f); /* must not fail */
|
|
|
|
if (e) return delim;
|
|
|
|
}
|
2002-08-28 10:36:38 -04:00
|
|
|
else if (c != EOF) {
|
|
|
|
if (!NIL_P(str)) {
|
|
|
|
char ch = c;
|
|
|
|
rb_str_buf_cat(str, &ch, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*strp = str = rb_str_buf_new(1);
|
|
|
|
RSTRING(str)->ptr[RSTRING(str)->len++] = c;
|
|
|
|
}
|
|
|
|
}
|
2002-03-25 09:50:40 -05:00
|
|
|
rb_thread_wait_fd(fileno(f));
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
#else
|
2002-02-06 02:30:13 -05:00
|
|
|
READ_CHECK(f);
|
2002-03-25 09:50:40 -05:00
|
|
|
#endif
|
2002-02-06 02:30:13 -05:00
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
if (c == EOF) {
|
|
|
|
if (ferror(f)) {
|
2002-10-02 10:13:58 -04:00
|
|
|
if (!rb_io_wait_readable(fileno(f)))
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
clearerr(f);
|
2002-09-10 21:09:04 -04:00
|
|
|
continue;
|
2002-02-06 02:30:13 -05:00
|
|
|
}
|
2002-03-25 09:50:40 -05:00
|
|
|
return c;
|
2002-02-06 02:30:13 -05:00
|
|
|
}
|
2002-03-25 09:50:40 -05:00
|
|
|
#ifndef READ_DATA_PENDING_PTR
|
|
|
|
if ((*bp++ = c) == delim || bp == bpe) {
|
|
|
|
cnt = bp - buf;
|
2002-02-06 02:30:13 -05:00
|
|
|
|
2002-03-25 09:50:40 -05: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 02:30:13 -05:00
|
|
|
}
|
2002-03-25 09:50:40 -05:00
|
|
|
#endif
|
2002-02-06 02:30:13 -05:00
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
return c;
|
|
|
|
}
|
2002-02-06 02:30:13 -05:00
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
static inline int
|
|
|
|
swallow(fptr, term)
|
|
|
|
OpenFile *fptr;
|
|
|
|
int term;
|
|
|
|
{
|
|
|
|
FILE *f = fptr->f;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
do {
|
|
|
|
#ifdef READ_DATA_PENDING_PTR
|
2002-08-28 04:05:23 -04:00
|
|
|
long cnt;
|
2002-03-25 09:50:40 -05:00
|
|
|
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 02:30:13 -05: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 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
2002-02-06 02:30:13 -05:00
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE str = Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(rs)) {
|
2002-12-11 04:32:41 -05:00
|
|
|
str = read_all(fptr, 0, Qnil);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2000-05-01 05:42:38 -04:00
|
|
|
else if (rs == rb_default_rs) {
|
2002-03-25 09:50:40 -05:00
|
|
|
return rb_io_getline_fast(fptr, '\n');
|
2000-05-01 05:42:38 -04:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else {
|
2002-03-25 09:50:40 -05:00
|
|
|
int c, newline;
|
|
|
|
char *rsptr;
|
2002-08-21 11:47:54 -04:00
|
|
|
long rslen;
|
|
|
|
int rspara = 0;
|
2002-03-25 09:50:40 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(rs);
|
1998-01-16 07:13:05 -05:00
|
|
|
rslen = RSTRING(rs)->len;
|
|
|
|
if (rslen == 0) {
|
|
|
|
rsptr = "\n\n";
|
|
|
|
rslen = 2;
|
|
|
|
rspara = 1;
|
2002-03-25 09:50:40 -05:00
|
|
|
swallow(fptr, '\n');
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-03-25 09:50:40 -05:00
|
|
|
else if (rslen == 1) {
|
|
|
|
return rb_io_getline_fast(fptr, RSTRING(rs)->ptr[0]);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
else {
|
|
|
|
rsptr = RSTRING(rs)->ptr;
|
|
|
|
}
|
2002-03-25 09:50:40 -05:00
|
|
|
newline = rsptr[rslen - 1];
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-03-25 09:50:40 -05: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 07:13:05 -05:00
|
|
|
|
2002-03-25 09:50:40 -05:00
|
|
|
if (rspara) {
|
|
|
|
if (c != EOF) {
|
|
|
|
swallow(fptr, '\n');
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (!NIL_P(str)) {
|
1998-01-16 07:13:05 -05:00
|
|
|
fptr->lineno++;
|
|
|
|
lineno = INT2FIX(fptr->lineno);
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJ_TAINT(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_gets(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
2002-03-25 09:50:40 -05:00
|
|
|
return rb_io_getline_fast(fptr, '\n');
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_io_gets_m(argc, argv, io)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE *argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
2002-02-06 02:30:13 -05: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-19 23:59:39 -05: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 01:30:29 -04:00
|
|
|
fptr->lineno = NUM2INT(lineno);
|
|
|
|
return lineno;
|
1999-01-19 23:59:39 -05: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 01: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-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_io_readline(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
2000-01-04 23:41:21 -05:00
|
|
|
VALUE line = rb_io_gets_m(argc, argv, io);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (NIL_P(line)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_eof_error();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_readlines(argc, argv, io)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE *argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE line, ary;
|
2002-04-24 00:54:16 -04:00
|
|
|
VALUE rs;
|
2002-02-06 02:30:13 -05:00
|
|
|
OpenFile *fptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-02-06 02:30:13 -05: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-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
2002-02-06 02:30:13 -05:00
|
|
|
while (!NIL_P(line = rb_io_getline(rs, fptr))) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, line);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_each_line(argc, argv, io)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE *argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE str;
|
2002-02-06 02:30:13 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
VALUE rs;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-02-06 02:30:13 -05: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 07:13:05 -05:00
|
|
|
rb_yield(str);
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return io;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_each_byte(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
f = fptr->f;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
2000-06-13 05:42:40 -04:00
|
|
|
if (c == EOF) {
|
|
|
|
if (ferror(f)) {
|
2002-10-02 10:13:58 -04:00
|
|
|
if (!rb_io_wait_readable(fileno(f)))
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
clearerr(f);
|
2002-09-10 21:09:04 -04:00
|
|
|
continue;
|
2000-06-13 05:42:40 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_yield(INT2FIX(c & 0xff));
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (ferror(f)) rb_sys_fail(fptr->path);
|
2002-01-18 09:24:01 -05:00
|
|
|
return io;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_getc(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
f = fptr->f;
|
|
|
|
|
2000-06-13 05:42:40 -04:00
|
|
|
retry:
|
1998-01-16 07:13:05 -05:00
|
|
|
READ_CHECK(f);
|
|
|
|
TRAP_BEG;
|
|
|
|
c = getc(f);
|
|
|
|
TRAP_END;
|
|
|
|
|
|
|
|
if (c == EOF) {
|
2000-06-13 05:42:40 -04:00
|
|
|
if (ferror(f)) {
|
2002-10-02 10:13:58 -04:00
|
|
|
if (!rb_io_wait_readable(fileno(f)))
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
clearerr(f);
|
2002-09-10 21:09:04 -04:00
|
|
|
goto retry;
|
2000-06-13 05:42:40 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return INT2FIX(c & 0xff);
|
|
|
|
}
|
|
|
|
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_readchar(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE c = rb_io_getc(io);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (NIL_P(c)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_eof_error();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_ungetc(io, c)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io, c;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2000-01-04 23:41:21 -05:00
|
|
|
int cc = NUM2INT(c);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-02-18 23:19:06 -05:00
|
|
|
if (ungetc(cc, fptr->f) == EOF && cc != EOF)
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_sys_fail(fptr->path);
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_isatty(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if (isatty(fileno(fptr->f)) == 0)
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-05 02:56:31 -05:00
|
|
|
fptr_finalize(fptr, fin)
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
2002-04-18 04:46:18 -04:00
|
|
|
int fin;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-02-05 12:10:54 -05:00
|
|
|
int n1 = 0, n2 = 0, e = 0, f1, f2 = -1;
|
2002-02-05 02:56:31 -05:00
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
if (fptr->f2) {
|
2002-02-05 12:10:54 -05:00
|
|
|
f2 = fileno(fptr->f2);
|
2002-10-09 02:12:54 -04:00
|
|
|
while ((n2 = fclose(fptr->f2)) < 0) {
|
|
|
|
if (!rb_io_wait_writable(f2)) {
|
|
|
|
e = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-02-06 23:18:08 -05:00
|
|
|
fptr->f2 = 0;
|
2002-02-05 12:10:54 -05:00
|
|
|
}
|
2002-02-06 23:18:08 -05:00
|
|
|
if (fptr->f) {
|
2002-02-05 12:10:54 -05:00
|
|
|
f1 = fileno(fptr->f);
|
2002-10-09 02:12:54 -04:00
|
|
|
while ((n1 = fclose(fptr->f)) < 0) {
|
|
|
|
if (f2 != -1 || !(fptr->mode & FMODE_WBUF)) break;
|
|
|
|
if (!rb_io_wait_writable(f1)) break;
|
|
|
|
}
|
2002-02-06 23:18:08 -05:00
|
|
|
fptr->f = 0;
|
|
|
|
if (n1 < 0 && errno == EBADF) {
|
|
|
|
if (f1 == f2 || !(fptr->mode & FMODE_WBUF)) {
|
|
|
|
n1 = 0;
|
|
|
|
}
|
|
|
|
}
|
2002-02-05 02:56:31 -05:00
|
|
|
}
|
|
|
|
if (!fin && (n1 < 0 || n2 < 0)) {
|
2002-02-05 12:10:54 -05:00
|
|
|
if (n1 == 0) errno = e;
|
2002-02-05 02:56:31 -05:00
|
|
|
rb_sys_fail(fptr->path);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-12 02:42:35 -05:00
|
|
|
static void
|
2002-02-05 02:56:31 -05:00
|
|
|
rb_io_fptr_cleanup(fptr, fin)
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
2002-02-05 02:56:31 -05:00
|
|
|
int fin;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
if (fptr->finalize) {
|
|
|
|
(*fptr->finalize)(fptr);
|
|
|
|
}
|
|
|
|
else {
|
2002-02-05 02:56:31 -05:00
|
|
|
fptr_finalize(fptr, fin);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (fptr->path) {
|
|
|
|
free(fptr->path);
|
2000-07-06 03:21:26 -04:00
|
|
|
fptr->path = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-12 02:42:35 -05: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 02:56:31 -05:00
|
|
|
rb_io_fptr_cleanup(fptr, Qtrue);
|
2000-12-12 02:42:35 -05:00
|
|
|
}
|
|
|
|
|
2002-02-05 02:56:31 -05:00
|
|
|
VALUE
|
|
|
|
rb_io_close(io)
|
|
|
|
VALUE io;
|
2000-05-01 05:42:38 -04:00
|
|
|
{
|
2002-02-05 02:56:31 -05:00
|
|
|
OpenFile *fptr;
|
2000-05-01 05:42:38 -04:00
|
|
|
int fd;
|
|
|
|
|
2002-02-05 02:56:31 -05:00
|
|
|
fptr = RFILE(io)->fptr;
|
2002-02-19 23:31:50 -05:00
|
|
|
if (!fptr) return Qnil;
|
2002-02-19 23:42:49 -05:00
|
|
|
if (!fptr->f && !fptr->f2) return Qnil;
|
2000-05-01 05:42:38 -04:00
|
|
|
|
|
|
|
fd = fileno(fptr->f);
|
2002-02-05 02:56:31 -05:00
|
|
|
rb_io_fptr_cleanup(fptr, Qfalse);
|
2000-05-01 05:42:38 -04:00
|
|
|
rb_thread_fd_close(fd);
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (fptr->pid) {
|
|
|
|
rb_syswait(fptr->pid);
|
|
|
|
fptr->pid = 0;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_io_close_m(io)
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
1999-12-14 01:50:43 -05:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't close");
|
|
|
|
}
|
2002-01-18 09:24:01 -05:00
|
|
|
rb_io_check_closed(RFILE(io)->fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_close(io);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2003-03-04 02:04:11 -05:00
|
|
|
static VALUE
|
|
|
|
io_close(io)
|
|
|
|
{
|
|
|
|
return rb_funcall(io, rb_intern("close"), 0, 0);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
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 02:52:35 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_close_read(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
1999-08-13 01:45:20 -04:00
|
|
|
int n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't close");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05: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 01:45:20 -04:00
|
|
|
n = fclose(fptr->f);
|
1999-01-19 23:59:39 -05:00
|
|
|
fptr->mode &= ~FMODE_READABLE;
|
|
|
|
fptr->f = fptr->f2;
|
|
|
|
fptr->f2 = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (n != 0) rb_sys_fail(fptr->path);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_close_write(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
1999-08-13 01:45:20 -04:00
|
|
|
int n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-12-14 01:50:43 -05:00
|
|
|
if (rb_safe_level() >= 4 && !OBJ_TAINTED(io)) {
|
|
|
|
rb_raise(rb_eSecurityError, "Insecure: can't close");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05: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 01:45:20 -04:00
|
|
|
n = fclose(fptr->f2);
|
1999-01-19 23:59:39 -05:00
|
|
|
fptr->f2 = 0;
|
|
|
|
fptr->mode &= ~FMODE_WRITABLE;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (n != 0) rb_sys_fail(fptr->path);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
return Qnil;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-03-27 00:28:00 -05:00
|
|
|
static VALUE
|
|
|
|
rb_io_sysseek(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE offset, ptrname;
|
2002-04-18 04:46:18 -04:00
|
|
|
int whence = SEEK_SET;
|
2002-03-27 00:28:00 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
off_t pos;
|
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
|
|
|
|
whence = NUM2INT(ptrname);
|
|
|
|
}
|
2002-03-27 00:28:00 -05:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if ((fptr->mode & FMODE_READABLE) && READ_DATA_PENDING(fptr->f)) {
|
2002-03-27 00:52:36 -05:00
|
|
|
rb_raise(rb_eIOError, "sysseek for buffered IO");
|
2002-03-27 00:28:00 -05: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 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_syswrite(io, str)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io, str;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
FILE *f;
|
2002-08-21 11:47:54 -04:00
|
|
|
long n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
rb_secure(4);
|
|
|
|
if (TYPE(str) != T_STRING)
|
1999-01-19 23:59:39 -05:00
|
|
|
str = rb_obj_as_string(str);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_writable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
f = GetWriteFile(fptr);
|
|
|
|
|
2002-02-06 23:18:08 -05:00
|
|
|
if (fptr->mode & FMODE_WBUF) {
|
|
|
|
rb_warn("syswrite for buffered IO");
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (!rb_thread_fd_writable(fileno(f))) {
|
|
|
|
rb_io_check_closed(fptr);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
n = write(fileno(f), RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
|
|
|
|
if (n == -1) rb_sys_fail(fptr->path);
|
|
|
|
|
2002-08-21 11:47:54 -04:00
|
|
|
return LONG2FIX(n);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-12-11 04:32:41 -05:00
|
|
|
rb_io_sysread(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-12-11 04:32:41 -05:00
|
|
|
VALUE len, str;
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
2002-08-21 11:47:54 -04:00
|
|
|
long n, ilen;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-12-11 04:32:41 -05:00
|
|
|
rb_scan_args(argc, argv, "11", &len, &str);
|
2002-08-21 11:47:54 -04:00
|
|
|
ilen = NUM2LONG(len);
|
1998-01-16 07:13:05 -05:00
|
|
|
GetOpenFile(io, fptr);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_check_readable(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-01-17 03:37:53 -05:00
|
|
|
if (READ_DATA_PENDING(fptr->f)) {
|
|
|
|
rb_raise(rb_eIOError, "sysread for buffered IO");
|
|
|
|
}
|
2002-12-11 04:32:41 -05:00
|
|
|
if (NIL_P(str)) {
|
|
|
|
str = rb_str_new(0, ilen);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
StringValue(str);
|
|
|
|
rb_str_modify(str);
|
|
|
|
rb_str_resize(str, ilen);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-02-26 00:29:06 -05:00
|
|
|
n = fileno(fptr->f);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_thread_wait_fd(fileno(fptr->f));
|
1998-01-16 07:13:05 -05:00
|
|
|
TRAP_BEG;
|
|
|
|
n = read(fileno(fptr->f), RSTRING(str)->ptr, RSTRING(str)->len);
|
|
|
|
TRAP_END;
|
|
|
|
|
2003-03-20 01:27:22 -05:00
|
|
|
if (n == -1) {
|
|
|
|
rb_str_resize(str, 0);
|
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (n == 0 && ilen > 0) {
|
2003-03-20 01:27:22 -05:00
|
|
|
rb_str_resize(str, 0);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_eof_error();
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
RSTRING(str)->len = n;
|
|
|
|
RSTRING(str)->ptr[n] = '\0';
|
1999-01-19 23:59:39 -05:00
|
|
|
OBJ_TAINT(str);
|
|
|
|
|
|
|
|
return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_binmode(io)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io;
|
|
|
|
{
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if defined(_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__EMX__)
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
#ifdef __human68k__
|
|
|
|
if (fptr->f)
|
|
|
|
fmode(fptr->f, _IOBIN);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (fptr->f2)
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
rb_io_mode_flags(mode)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *mode;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
int flags = 0;
|
2001-06-05 03:19:39 -04:00
|
|
|
const char *m = mode;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-06-05 03:19:39 -04:00
|
|
|
switch (*m++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'r':
|
|
|
|
flags |= FMODE_READABLE;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
flags |= FMODE_WRITABLE;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
flags |= FMODE_WRITABLE;
|
|
|
|
break;
|
|
|
|
default:
|
1999-08-13 01:45:20 -04:00
|
|
|
error:
|
|
|
|
rb_raise(rb_eArgError, "illegal access mode %s", mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-04-24 00: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 01:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-04-01 02:39:09 -05:00
|
|
|
rb_io_modenum_flags(mode)
|
1999-08-13 01:45:20 -04:00
|
|
|
int mode;
|
|
|
|
{
|
2002-04-24 00:54:16 -04:00
|
|
|
int flags = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
|
2002-03-29 09:50:09 -05:00
|
|
|
case O_RDONLY:
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
#ifdef O_BINARY
|
|
|
|
if (mode & O_BINARY) {
|
|
|
|
flags |= FMODE_BINMODE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2001-09-03 01:37:42 -04:00
|
|
|
static int
|
2002-04-01 02:39:09 -05:00
|
|
|
rb_io_mode_modenum(mode)
|
2001-06-05 03: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 00:54:16 -04:00
|
|
|
while (*m) {
|
|
|
|
switch (*m++) {
|
|
|
|
case 'b':
|
2001-06-05 03:19:39 -04:00
|
|
|
#ifdef O_BINARY
|
2002-04-24 00:54:16 -04:00
|
|
|
flags |= O_BINARY;
|
2001-06-05 03:19:39 -04:00
|
|
|
#endif
|
2002-04-24 00:54:16 -04:00
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
flags |= O_RDWR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error;
|
|
|
|
}
|
2001-06-05 03:19:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static char*
|
2002-04-01 02:39:09 -05:00
|
|
|
rb_io_modenum_mode(flags, mode)
|
1999-08-13 01:45:20 -04:00
|
|
|
int flags;
|
2001-09-05 02:54:57 -04:00
|
|
|
char *mode;
|
1999-08-13 01:45:20 -04:00
|
|
|
{
|
|
|
|
char *p = mode;
|
|
|
|
|
|
|
|
switch (flags & (O_RDONLY|O_WRONLY|O_RDWR)) {
|
2002-03-29 09:50:09 -05:00
|
|
|
case O_RDONLY:
|
1999-08-13 01:45:20 -04:00
|
|
|
*p++ = 'r';
|
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
*p++ = 'w';
|
|
|
|
break;
|
|
|
|
case O_RDWR:
|
2002-04-01 02:39:09 -05:00
|
|
|
*p++ = 'r';
|
1999-08-13 01: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 06:38:20 -05:00
|
|
|
rb_sysopen(fname, flags, mode)
|
1999-08-13 01:45:20 -04:00
|
|
|
char *fname;
|
2001-02-02 06:38:20 -05:00
|
|
|
int flags;
|
1999-08-13 01:45:20 -04:00
|
|
|
unsigned int mode;
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2001-02-02 06:38:20 -05:00
|
|
|
fd = open(fname, flags, mode);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
2001-02-02 06:38:20 -05:00
|
|
|
fd = open(fname, flags, mode);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
if (fd < 0) {
|
|
|
|
rb_sys_fail(fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
FILE *
|
|
|
|
rb_fopen(fname, mode)
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *fname;
|
|
|
|
const char *mode;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
FILE *file;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
file = fopen(fname, mode);
|
2000-07-06 03:21:26 -04:00
|
|
|
if (!file) {
|
1998-01-16 07:13:05 -05:00
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gc();
|
1999-08-13 01:45:20 -04:00
|
|
|
file = fopen(fname, mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
if (!file) {
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_sys_fail(fname);
|
|
|
|
}
|
|
|
|
}
|
2001-05-02 00: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-04 23:41:21 -05:00
|
|
|
#ifdef __human68k__
|
|
|
|
fmode(file, _IOTEXT);
|
|
|
|
#endif
|
1999-08-13 01:45:20 -04:00
|
|
|
return file;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
rb_fdopen(fd, mode)
|
|
|
|
int fd;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *mode;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
FILE *file;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
file = fdopen(fd, mode);
|
2000-07-06 03:21:26 -04:00
|
|
|
if (!file) {
|
1999-08-13 01:45:20 -04:00
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
|
|
|
rb_gc();
|
|
|
|
file = fdopen(fd, mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
if (!file) {
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
}
|
2001-05-02 00: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 01:45:20 -04:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-03-23 03:37:35 -05:00
|
|
|
rb_file_open_internal(io, fname, mode)
|
|
|
|
VALUE io;
|
1999-08-13 01:45:20 -04:00
|
|
|
const char *fname, *mode;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2000-03-23 03:37:35 -05:00
|
|
|
|
|
|
|
MakeOpenFile(io, fptr);
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
fptr->mode = rb_io_mode_flags(mode);
|
|
|
|
fptr->f = rb_fopen(fname, mode);
|
|
|
|
fptr->path = strdup(fname);
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
return io;
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_file_open(fname, mode)
|
|
|
|
const char *fname, *mode;
|
|
|
|
{
|
2002-12-20 03:33:17 -05:00
|
|
|
return rb_file_open_internal(io_alloc(rb_cFile), fname, mode);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
static VALUE
|
|
|
|
rb_file_sysopen_internal(io, fname, flags, mode)
|
|
|
|
VALUE io;
|
1999-08-13 01:45:20 -04:00
|
|
|
char *fname;
|
|
|
|
int flags, mode;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
int fd;
|
|
|
|
char *m;
|
2001-09-03 01:37:42 -04:00
|
|
|
char mbuf[4];
|
2000-03-23 03:37:35 -05:00
|
|
|
|
|
|
|
MakeOpenFile(io, fptr);
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
fd = rb_sysopen(fname, flags, mode);
|
2002-04-01 02:39:09 -05:00
|
|
|
m = rb_io_modenum_mode(flags, mbuf);
|
|
|
|
fptr->mode = rb_io_modenum_flags(flags);
|
1999-08-13 01:45:20 -04:00
|
|
|
fptr->f = rb_fdopen(fd, m);
|
|
|
|
fptr->path = strdup(fname);
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
return io;
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_file_sysopen(fname, flags, mode)
|
|
|
|
const char *fname;
|
|
|
|
int flags, mode;
|
|
|
|
{
|
2003-02-09 13:36:22 -05:00
|
|
|
return rb_file_sysopen_internal(io_alloc(rb_cFile), fname, flags, mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__VMS)
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
free(list);
|
1998-01-16 07:13:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (list->next) {
|
|
|
|
if (list->next->fptr == fptr) {
|
|
|
|
tmp = list->next;
|
|
|
|
list->next = list->next->next;
|
|
|
|
free(tmp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list = list->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) || defined(__VMS)
|
1998-01-16 07:13:05 -05:00
|
|
|
static void
|
2000-08-07 01:05:04 -04:00
|
|
|
pipe_atexit _((void))
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
struct pipe_list *list = pipe_list;
|
1999-01-19 23:59:39 -05:00
|
|
|
struct pipe_list *tmp;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
while (list) {
|
1999-01-19 23:59:39 -05:00
|
|
|
tmp = list->next;
|
|
|
|
rb_io_fptr_finalize(list->fptr);
|
|
|
|
list = tmp;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
2000-08-07 01:05:04 -04:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
static void pipe_finalize _((OpenFile *fptr));
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static void
|
|
|
|
pipe_finalize(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if !defined (__CYGWIN__) && !defined(_WIN32)
|
2000-07-10 02:30:11 -04:00
|
|
|
extern VALUE rb_last_status;
|
|
|
|
int status;
|
2000-07-06 03:21:26 -04:00
|
|
|
if (fptr->f) {
|
2000-07-10 02:30:11 -04:00
|
|
|
status = pclose(fptr->f);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
if (fptr->f2) {
|
2000-07-10 02:30:11 -04:00
|
|
|
status = pclose(fptr->f2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
fptr->f = fptr->f2 = 0;
|
2000-11-13 05:11:31 -05:00
|
|
|
#if defined DJGPP
|
2000-07-10 02:47:04 -04:00
|
|
|
status <<= 8;
|
|
|
|
#endif
|
2000-07-10 02:30:11 -04:00
|
|
|
rb_last_status = INT2FIX(status);
|
1999-01-19 23:59:39 -05:00
|
|
|
#else
|
2002-02-05 12:10:54 -05:00
|
|
|
fptr_finalize(fptr, Qtrue);
|
1999-01-19 23:59:39 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
pipe_del_fptr(fptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
1999-09-20 03:14:18 -04:00
|
|
|
rb_io_synchronized(fptr)
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
fptr->mode |= FMODE_SYNC;
|
|
|
|
}
|
|
|
|
|
1999-09-20 03:14:18 -04:00
|
|
|
void
|
|
|
|
rb_io_unbuffered(fptr)
|
|
|
|
OpenFile *fptr;
|
|
|
|
{
|
|
|
|
rb_io_synchronized(fptr);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
|
|
|
pipe_open(pname, mode)
|
|
|
|
char *pname, *mode;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
int modef = rb_io_mode_flags(mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
|
2002-06-28 14:26:01 -04:00
|
|
|
#if defined(DJGPP) || defined(__human68k__) || defined(__VMS)
|
1998-01-16 07:13:05 -05:00
|
|
|
FILE *f = popen(pname, mode);
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
if (!f) rb_sys_fail(pname);
|
1998-01-16 07:13:05 -05:00
|
|
|
else {
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE port = io_alloc(rb_cIO);
|
2001-10-03 03:19:19 -04:00
|
|
|
|
1998-01-16 07:13:05 -05: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 00:00:25 -05:00
|
|
|
if (fptr->f) fptr->f2 = f;
|
|
|
|
else fptr->f = f;
|
1999-09-20 03:14:18 -04:00
|
|
|
rb_io_synchronized(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2002-06-28 14:26:01 -04:00
|
|
|
return (VALUE)port;
|
|
|
|
}
|
|
|
|
#else
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#ifdef _WIN32
|
2002-06-28 14:26:01 -04:00
|
|
|
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 {
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE port = io_alloc(rb_cIO);
|
2002-06-28 14:26:01 -04:00
|
|
|
|
|
|
|
MakeOpenFile(port, fptr);
|
|
|
|
fptr->mode = modef;
|
|
|
|
fptr->mode |= FMODE_SYNC;
|
|
|
|
fptr->pid = pid;
|
|
|
|
|
|
|
|
if (modef & FMODE_READABLE) {
|
|
|
|
fptr->f = fpr;
|
2002-08-28 05:41:58 -04:00
|
|
|
}
|
2002-06-28 14:26:01 -04:00
|
|
|
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 07:13:05 -05: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 09:15:33 -05:00
|
|
|
switch ((pid = fork())) {
|
1998-01-16 07:13:05 -05: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 07:19:22 -05:00
|
|
|
int fd;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
for (fd = 3; fd < NOFILE; fd++)
|
|
|
|
close(fd);
|
|
|
|
rb_proc_exec(pname);
|
|
|
|
fprintf(stderr, "%s:%d: command not found: %s\n",
|
1999-01-19 23:59:39 -05:00
|
|
|
ruby_sourcefile, ruby_sourceline, pname);
|
1998-01-16 07:13:05 -05:00
|
|
|
_exit(127);
|
|
|
|
}
|
2000-05-17 00:38:19 -04:00
|
|
|
rb_io_synchronized(RFILE(orig_stdout)->fptr);
|
|
|
|
rb_io_synchronized(RFILE(orig_stderr)->fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
case -1: /* fork failed */
|
|
|
|
if (errno == EAGAIN) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_thread_sleep(1);
|
1998-01-16 07:13:05 -05:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
close(pr[0]); close(pw[1]);
|
|
|
|
rb_sys_fail(pname);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* parent */
|
1999-01-19 23:59:39 -05:00
|
|
|
if (pid < 0) rb_sys_fail(pname);
|
|
|
|
else {
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE port = io_alloc(rb_cIO);
|
2001-10-03 03:19:19 -04:00
|
|
|
|
1998-01-16 07:13:05 -05: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 04:03:08 -05:00
|
|
|
#if defined (__CYGWIN__)
|
1999-01-19 23:59:39 -05:00
|
|
|
fptr->finalize = pipe_finalize;
|
1998-01-16 07:13:05 -05:00
|
|
|
pipe_add_fptr(fptr);
|
|
|
|
#endif
|
2002-01-18 09:24:01 -05:00
|
|
|
return port;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2002-06-28 14:26:01 -04:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-05-24 00:34:26 -04:00
|
|
|
rb_io_popen(str, argc, argv, klass)
|
|
|
|
char *str;
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2000-05-24 00:34:26 -04:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
char *mode;
|
2000-05-25 01:55:12 -04:00
|
|
|
VALUE pname, pmode, port;
|
2001-09-03 01:37:42 -04:00
|
|
|
char mbuf[4];
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-25 01:55:12 -04:00
|
|
|
if (rb_scan_args(argc, argv, "11", &pname, &pmode) == 1) {
|
1998-01-16 07:13:05 -05:00
|
|
|
mode = "r";
|
|
|
|
}
|
2001-09-03 01:37:42 -04:00
|
|
|
else if (FIXNUM_P(pmode)) {
|
2002-04-01 02:39:09 -05:00
|
|
|
mode = rb_io_modenum_mode(FIX2INT(pmode), mbuf);
|
2001-09-03 01:37:42 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
else {
|
2001-05-02 00:22:21 -04:00
|
|
|
mode = StringValuePtr(pmode);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(pname);
|
2000-05-24 00:34:26 -04:00
|
|
|
port = pipe_open(str, mode);
|
2000-01-31 22:12:21 -05:00
|
|
|
if (NIL_P(port)) {
|
2000-05-15 02:38:46 -04:00
|
|
|
/* child */
|
2000-05-25 01:55:12 -04:00
|
|
|
if (rb_block_given_p()) {
|
2000-05-15 02:38:46 -04:00
|
|
|
rb_yield(Qnil);
|
2000-05-25 01:55:12 -04:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
_exit(0);
|
2000-05-09 22:58:41 -04:00
|
|
|
}
|
2000-05-25 01:55:12 -04:00
|
|
|
return Qnil;
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
RBASIC(port)->klass = klass;
|
|
|
|
if (rb_block_given_p()) {
|
2003-03-04 02:04:11 -05:00
|
|
|
return rb_ensure(rb_yield, port, io_close, port);
|
2000-01-31 22:12:21 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return port;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
static VALUE
|
2000-05-24 00:34:26 -04:00
|
|
|
rb_io_s_popen(argc, argv, klass)
|
2000-03-23 03:37:35 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2000-05-24 00:34:26 -04:00
|
|
|
VALUE klass;
|
2000-03-23 03:37:35 -05:00
|
|
|
{
|
2000-05-24 00:34:26 -04:00
|
|
|
char *str = 0;
|
2000-03-23 03:37:35 -05:00
|
|
|
|
2000-05-24 00:34:26 -04:00
|
|
|
if (argc >= 1) {
|
2001-05-02 00:22:21 -04:00
|
|
|
str = StringValuePtr(argv[0]);
|
2000-03-23 03:37:35 -05:00
|
|
|
}
|
2000-05-24 00:34:26 -04:00
|
|
|
return rb_io_popen(str, argc, argv, klass);
|
2000-03-23 03:37:35 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2001-06-05 03:19:39 -04:00
|
|
|
rb_open_file(argc, argv, io)
|
1999-08-13 01:45:20 -04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
2001-06-05 03:19:39 -04:00
|
|
|
VALUE io;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-05-23 01:35:32 -04:00
|
|
|
VALUE fname, vmode, perm;
|
1999-08-13 01:45:20 -04:00
|
|
|
char *path, *mode;
|
2001-06-05 03:19:39 -04:00
|
|
|
int flags, fmode;
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "12", &fname, &vmode, &perm);
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(fname);
|
1999-08-13 01:45:20 -04:00
|
|
|
path = RSTRING(fname)->ptr;
|
|
|
|
|
2001-06-05 03:19:39 -04:00
|
|
|
if (FIXNUM_P(vmode) || !NIL_P(perm)) {
|
2002-04-01 02:39:09 -05:00
|
|
|
flags = FIXNUM_P(vmode) ? NUM2INT(vmode) : rb_io_mode_modenum(StringValuePtr(vmode));
|
2001-06-05 03:19:39 -04:00
|
|
|
fmode = NIL_P(perm) ? 0666 : NUM2INT(perm);
|
2001-09-03 01:37:42 -04:00
|
|
|
|
2002-05-23 01:35:32 -04:00
|
|
|
rb_file_sysopen_internal(io, path, flags, fmode);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2001-05-02 00:22:21 -04:00
|
|
|
mode = NIL_P(vmode) ? "r" : StringValuePtr(vmode);
|
2002-05-23 01:35:32 -04:00
|
|
|
rb_file_open_internal(io, RSTRING(fname)->ptr, mode);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
2001-06-05 03:19:39 -04:00
|
|
|
return io;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2001-06-05 03:19:39 -04:00
|
|
|
static VALUE
|
2002-01-18 09:24:01 -05:00
|
|
|
rb_io_s_open(argc, argv, klass)
|
2001-06-05 03:19:39 -04:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE klass;
|
|
|
|
{
|
2002-01-18 09:24:01 -05:00
|
|
|
VALUE io = rb_class_new_instance(argc, argv, klass);
|
2001-06-05 03:19:39 -04:00
|
|
|
|
2000-05-24 00:34:26 -04:00
|
|
|
if (rb_block_given_p()) {
|
2003-03-04 02:04:11 -05:00
|
|
|
return rb_ensure(rb_yield, io, io_close, io);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2002-01-18 09:24:01 -05:00
|
|
|
return io;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-05-23 01: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 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_open(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2000-05-24 00:34:26 -04:00
|
|
|
if (argc >= 1) {
|
2001-05-02 00:22:21 -04:00
|
|
|
char *str = StringValuePtr(argv[0]);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-24 00:34:26 -04:00
|
|
|
if (str[0] == '|') {
|
|
|
|
return rb_io_popen(str+1, argc, argv, rb_cIO);
|
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
2002-01-18 09:24:01 -05:00
|
|
|
return rb_io_s_open(argc, argv, rb_cFile);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01: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-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_io_get_io(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
return rb_convert_type(io, T_FILE, "IO", "to_io");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static char*
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_mode_string(fptr)
|
1998-01-16 07:19:22 -05: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 01:45:20 -04:00
|
|
|
static VALUE
|
2000-02-29 03:05:32 -05:00
|
|
|
io_reopen(io, nfile)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE io, nfile;
|
|
|
|
{
|
|
|
|
OpenFile *fptr, *orig;
|
|
|
|
char *mode;
|
|
|
|
int fd;
|
2002-05-29 01:20:39 -04:00
|
|
|
off_t pos = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
nfile = rb_io_get_io(nfile);
|
1999-12-14 01:50:43 -05: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 07:13:05 -05:00
|
|
|
GetOpenFile(nfile, orig);
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (fptr == orig) return io;
|
2000-06-14 04:08:50 -04:00
|
|
|
if (orig->mode & FMODE_READABLE) {
|
2002-10-02 10:59:25 -04:00
|
|
|
pos = io_tell(orig);
|
2000-06-14 04:08:50 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
if (orig->f2) {
|
2002-03-29 09:50:09 -05:00
|
|
|
io_fflush(orig->f2, orig);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (orig->mode & FMODE_WRITABLE) {
|
2002-03-29 09:50:09 -05:00
|
|
|
io_fflush(orig->f, orig);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_thread_fd_close(fileno(fptr->f));
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
mode = rb_io_mode_string(fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
fd = fileno(fptr->f);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (fd < 3) {
|
2000-07-14 03:18:58 -04:00
|
|
|
clearerr(fptr->f);
|
2000-07-17 05:38:10 -04:00
|
|
|
/* need to keep stdio objects */
|
1999-01-19 23:59:39 -05:00
|
|
|
if (dup2(fileno(orig->f), fd) < 0)
|
|
|
|
rb_sys_fail(orig->path);
|
|
|
|
}
|
|
|
|
else {
|
1998-01-16 07:19:22 -05:00
|
|
|
fclose(fptr->f);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (dup2(fileno(orig->f), fd) < 0)
|
|
|
|
rb_sys_fail(orig->path);
|
|
|
|
fptr->f = rb_fdopen(fd, mode);
|
2000-07-17 05:38:10 -04:00
|
|
|
}
|
|
|
|
if ((orig->mode & FMODE_READABLE) && pos >= 0) {
|
2002-10-02 10:59:25 -04:00
|
|
|
io_seek(fptr, pos, SEEK_SET);
|
|
|
|
io_seek(orig, pos, SEEK_SET);
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (fptr->f2) {
|
|
|
|
fd = fileno(fptr->f2);
|
1999-01-19 23:59:39 -05:00
|
|
|
fclose(fptr->f2);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (orig->f2) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (dup2(fileno(orig->f2), fd) < 0)
|
|
|
|
rb_sys_fail(orig->path);
|
|
|
|
fptr->f2 = rb_fdopen(fd, "w");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fptr->f2 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fptr->mode & FMODE_BINMODE) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_binmode(io);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
RBASIC(io)->klass = RBASIC(nfile)->klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
static VALUE
|
2000-02-29 03:05:32 -05:00
|
|
|
rb_io_reopen(argc, argv, file)
|
1999-08-13 01: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 01:48:43 -04:00
|
|
|
if (TYPE(fname) != T_STRING) { /* fname must be IO */
|
2000-02-29 03:05:32 -05:00
|
|
|
return io_reopen(file, fname);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(fname);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (!NIL_P(nmode)) {
|
2001-05-02 00:22:21 -04:00
|
|
|
mode = StringValuePtr(nmode);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = "r";
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(file, fptr);
|
2000-07-06 03:21:26 -04:00
|
|
|
if (fptr->path) {
|
|
|
|
free(fptr->path);
|
|
|
|
fptr->path = 0;
|
|
|
|
}
|
|
|
|
|
1999-08-13 01: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 03:21:26 -04:00
|
|
|
fptr->f2 = 0;
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
2001-05-02 00:22:21 -04:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
if (freopen(RSTRING(fname)->ptr, mode, fptr->f) == 0) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
2001-05-02 00: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 01:45:20 -04:00
|
|
|
if (fptr->f2) {
|
2000-07-06 03:21:26 -04:00
|
|
|
if (freopen(RSTRING(fname)->ptr, "w", fptr->f2) == 0) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_sys_fail(fptr->path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
2002-12-10 01:23:44 -05:00
|
|
|
rb_io_copy_object(dest, io)
|
|
|
|
VALUE dest, io;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
OpenFile *fptr, *orig;
|
|
|
|
int fd;
|
|
|
|
char *mode;
|
|
|
|
|
2002-08-27 04:31:08 -04:00
|
|
|
io = rb_io_get_io(io);
|
2002-12-10 01:23:44 -05:00
|
|
|
if (dest == io) return dest;
|
1998-01-16 07:13:05 -05:00
|
|
|
GetOpenFile(io, orig);
|
2002-12-10 01:23:44 -05:00
|
|
|
MakeOpenFile(dest, fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (orig->f2) {
|
2002-03-29 09:50:09 -05:00
|
|
|
io_fflush(orig->f2, orig);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (orig->mode & FMODE_WRITABLE) {
|
2002-03-29 09:50:09 -05:00
|
|
|
io_fflush(orig->f, orig);
|
1998-01-16 07:13:05 -05: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 11:17:19 -04:00
|
|
|
fd = ruby_dup(fileno(orig->f));
|
1998-01-16 07:13:05 -05:00
|
|
|
fptr->f = rb_fdopen(fd, mode);
|
2002-05-20 00:33:59 -04:00
|
|
|
if (orig->f2) {
|
2001-02-13 00:09:11 -05:00
|
|
|
if (fileno(orig->f) != fileno(orig->f2)) {
|
2001-07-14 11:17:19 -04:00
|
|
|
fd = ruby_dup(fileno(orig->f2));
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
2002-05-20 00:33:59 -04:00
|
|
|
fptr->f2 = rb_fdopen(fd, "w");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
if (fptr->mode & FMODE_BINMODE) {
|
2002-12-10 01:23:44 -05:00
|
|
|
rb_io_binmode(dest);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-12-10 01:23:44 -05:00
|
|
|
return dest;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-02-20 01:35:37 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_printf(argc, argv, out)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE argv[];
|
|
|
|
VALUE out;
|
|
|
|
{
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_io_write(out, rb_f_sprintf(argc, argv));
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-24 04:21:56 -04:00
|
|
|
rb_f_printf(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE argv[];
|
|
|
|
{
|
|
|
|
VALUE out;
|
|
|
|
|
|
|
|
if (argc == 0) return Qnil;
|
|
|
|
if (TYPE(argv[0]) == T_STRING) {
|
|
|
|
out = rb_defout;
|
|
|
|
}
|
1999-08-24 04:21:56 -04:00
|
|
|
else {
|
1998-01-16 07:13:05 -05:00
|
|
|
out = argv[0];
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
}
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_io_write(out, rb_f_sprintf(argc, argv));
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2002-02-20 01:35:37 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_print(argc, argv, out)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE out;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
int i;
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE line;
|
|
|
|
|
|
|
|
/* if no argument given, print `$_' */
|
|
|
|
if (argc == 0) {
|
|
|
|
argc = 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
line = rb_lastline_get();
|
1998-01-16 07:13:05 -05:00
|
|
|
argv = &line;
|
|
|
|
}
|
|
|
|
for (i=0; i<argc; i++) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!NIL_P(rb_output_fs) && i>0) {
|
|
|
|
rb_io_write(out, rb_output_fs);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
switch (TYPE(argv[i])) {
|
|
|
|
case T_NIL:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_write(out, rb_str_new2("nil"));
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_write(out, argv[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!NIL_P(rb_output_rs)) {
|
|
|
|
rb_io_write(out, rb_output_rs);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_print(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_print(argc, argv, rb_defout);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_io_putc(io, ch)
|
|
|
|
VALUE io, ch;
|
|
|
|
{
|
2002-08-21 04:30:09 -04:00
|
|
|
char c = NUM2CHR(ch);
|
|
|
|
|
|
|
|
rb_io_write(io, rb_str_new(&c, 1));
|
1999-01-19 23:59:39 -05:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_putc(recv, ch)
|
|
|
|
VALUE recv, ch;
|
|
|
|
{
|
|
|
|
return rb_io_putc(rb_defout, ch);
|
|
|
|
}
|
|
|
|
|
2001-12-10 02:18:16 -05:00
|
|
|
static VALUE
|
|
|
|
io_puts_ary(ary, out)
|
|
|
|
VALUE ary, out;
|
|
|
|
{
|
|
|
|
VALUE tmp;
|
2002-08-21 11:47:54 -04:00
|
|
|
long i;
|
2001-12-10 02:18:16 -05:00
|
|
|
|
|
|
|
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 01:35:37 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_puts(argc, argv, out)
|
1998-01-16 07:19:22 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE out;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
int i;
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE line;
|
|
|
|
|
|
|
|
/* if no argument given, print newline. */
|
|
|
|
if (argc == 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_write(out, rb_default_rs);
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
for (i=0; i<argc; i++) {
|
2001-10-30 03:43:28 -05:00
|
|
|
if (NIL_P(argv[i])) {
|
1999-01-19 23:59:39 -05:00
|
|
|
line = rb_str_new2("nil");
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
2001-10-30 03:43:28 -05:00
|
|
|
else {
|
2002-12-19 04:20:20 -05:00
|
|
|
line = rb_check_array_type(argv[i]);
|
2001-12-10 02:18:16 -05:00
|
|
|
if (!NIL_P(line)) {
|
|
|
|
rb_protect_inspect(io_puts_ary, line, out);
|
|
|
|
continue;
|
2001-11-19 00:03:03 -05:00
|
|
|
}
|
2001-10-30 03:43:28 -05:00
|
|
|
line = rb_obj_as_string(argv[i]);
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_write(out, line);
|
2002-09-05 21:58:32 -04:00
|
|
|
if (RSTRING(line)->len == 0 ||
|
|
|
|
RSTRING(line)->ptr[RSTRING(line)->len-1] != '\n') {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_write(out, rb_default_rs);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_puts(argc, argv)
|
1998-01-16 07:19:22 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_puts(argc, argv, rb_defout);
|
1998-01-16 07:19:22 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
void
|
|
|
|
rb_p(obj) /* for debug print within C code */
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2000-07-25 03: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-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_p(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
int i;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
rb_p(argv[i]);
|
|
|
|
}
|
2000-12-26 03:32:35 -05:00
|
|
|
if (TYPE(rb_defout) == T_FILE) {
|
2000-12-26 03:08:50 -05:00
|
|
|
rb_io_flush(rb_defout);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_obj_display(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05: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 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static void
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_defset(val, id)
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE val;
|
|
|
|
ID id;
|
|
|
|
{
|
|
|
|
if (!rb_respond_to(val, id_write)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_raise(rb_eTypeError, "$> must have write method, %s given",
|
2003-01-30 23:00:17 -05:00
|
|
|
rb_obj_classname(val));
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
rb_defout = val;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
static void
|
1999-08-13 01:45:20 -04:00
|
|
|
set_stdin(val, id, var)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE val;
|
|
|
|
ID id;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE *var;
|
1998-01-16 07:19:22 -05:00
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (val == *var) return;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (TYPE(val) != T_FILE) {
|
1999-08-13 01:45:20 -04:00
|
|
|
*var = val;
|
|
|
|
return;
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (TYPE(*var) != T_FILE) {
|
|
|
|
*var = orig_stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetOpenFile(val, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
2001-02-26 00:29:06 -05: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 01: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 00:29:06 -05:00
|
|
|
int fd;
|
1999-08-13 01:45:20 -04:00
|
|
|
|
|
|
|
if (val == *var) return;
|
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
if (TYPE(*var) == T_FILE && !rb_io_closed(*var)) {
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_io_flush(*var);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
1999-08-13 01: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 00:29:06 -05: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 01: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 07:19:22 -05:00
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
prep_stdio(f, mode, klass)
|
1998-01-16 07:13:05 -05:00
|
|
|
FILE *f;
|
|
|
|
int mode;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE klass;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
OpenFile *fp;
|
2002-12-20 03:33:17 -05:00
|
|
|
VALUE io = io_alloc(klass);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
MakeOpenFile(io, fp);
|
2003-02-15 13:16:52 -05:00
|
|
|
#ifdef __CYGWIN__
|
2003-03-12 02:59:28 -05:00
|
|
|
if (!isatty(fileno(f))) {
|
|
|
|
mode |= O_BINARY;
|
|
|
|
setmode(fileno(f), O_BINARY);
|
|
|
|
}
|
2003-02-15 13:16:52 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
fp->f = f;
|
|
|
|
fp->mode = mode;
|
|
|
|
|
2002-01-18 09:24:01 -05:00
|
|
|
return io;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2001-01-15 02:01:00 -05:00
|
|
|
static void
|
2000-08-25 04: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);
|
|
|
|
}
|
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
static VALUE
|
|
|
|
rb_io_initialize(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE fnum, mode;
|
2000-03-23 03:37:35 -05:00
|
|
|
OpenFile *fp;
|
2002-04-01 02:39:09 -05:00
|
|
|
int fd, flags;
|
|
|
|
char mbuf[4];
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-01 02:39:09 -05:00
|
|
|
rb_scan_args(argc, argv, "11", &fnum, &mode);
|
|
|
|
fd = NUM2INT(fnum);
|
|
|
|
if (argc == 2) {
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(mode);
|
2002-04-01 03:31:49 -05:00
|
|
|
flags = rb_io_mode_modenum(RSTRING(mode)->ptr);
|
2002-04-01 02:39:09 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if defined(HAVE_FCNTL) && defined(F_GETFL)
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
#else
|
|
|
|
flags = O_RDONLY;
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-03-23 03:37:35 -05:00
|
|
|
MakeOpenFile(io, fp);
|
2002-04-15 03:48:47 -04:00
|
|
|
fp->mode = rb_io_modenum_flags(flags);
|
2002-04-01 02:39:09 -05:00
|
|
|
fp->f = rb_fdopen(fd, rb_io_modenum_mode(flags, mbuf));
|
2000-03-23 03:37:35 -05:00
|
|
|
|
|
|
|
return io;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2000-05-24 00:34:26 -04:00
|
|
|
static VALUE
|
|
|
|
rb_file_initialize(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
2000-07-06 03:21:26 -04:00
|
|
|
if (RFILE(io)->fptr) {
|
|
|
|
rb_io_close_m(io);
|
|
|
|
free(RFILE(io)->fptr);
|
|
|
|
RFILE(io)->fptr = 0;
|
|
|
|
}
|
2002-02-05 02:56:31 -05: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 03:19:39 -04:00
|
|
|
rb_open_file(argc, argv, io);
|
2000-05-24 00:34:26 -04:00
|
|
|
|
2001-06-05 03:19:39 -04:00
|
|
|
return io;
|
2000-05-24 00:34:26 -04:00
|
|
|
}
|
|
|
|
|
2001-02-13 00:09:11 -05:00
|
|
|
static VALUE
|
2002-01-18 09:24:01 -05:00
|
|
|
rb_io_s_new(argc, argv, klass)
|
2001-02-13 00:09:11 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE klass;
|
|
|
|
{
|
2002-01-18 09:24:01 -05:00
|
|
|
if (rb_block_given_p()) {
|
|
|
|
char *cname = rb_class2name(klass);
|
2001-10-03 03:19:19 -04:00
|
|
|
|
2002-04-01 02:39:09 -05:00
|
|
|
rb_warn("%s::new() does not take block; use %s::open() instead",
|
2002-01-18 09:24:01 -05:00
|
|
|
cname, cname);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
2002-01-18 09:24:01 -05:00
|
|
|
return rb_class_new_instance(argc, argv, klass);
|
2001-02-13 00:09:11 -05:00
|
|
|
}
|
|
|
|
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
static int
|
|
|
|
next_argv()
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
extern VALUE rb_argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
char *fn;
|
2003-01-12 04:24:28 -05:00
|
|
|
OpenFile *fptr;
|
|
|
|
int defout_binmode = 0;
|
|
|
|
|
|
|
|
GetOpenFile(rb_defout, fptr);
|
2003-01-13 11:24:40 -05:00
|
|
|
if (fptr->mode & FMODE_BINMODE)
|
2003-01-12 04:24:28 -05:00
|
|
|
defout_binmode = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (init_p == 0) {
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(rb_argv)->len > 0) {
|
1998-01-16 07:13:05 -05:00
|
|
|
next_p = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
next_p = -1;
|
1999-08-13 01:45:20 -04:00
|
|
|
current_file = rb_stdin;
|
2000-08-25 04:26:06 -04:00
|
|
|
filename = rb_str_new2("-");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
init_p = 1;
|
2000-08-15 02:22:49 -04:00
|
|
|
first_p = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
gets_lineno = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (next_p == 1) {
|
|
|
|
next_p = 0;
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RARRAY(rb_argv)->len > 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
filename = rb_ary_shift(rb_argv);
|
2001-05-02 00:22:21 -04:00
|
|
|
fn = StringValuePtr(filename);
|
1999-08-13 01:45:20 -04:00
|
|
|
if (strlen(fn) == 1 && fn[0] == '-') {
|
|
|
|
current_file = rb_stdin;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ruby_inplace_mode) {
|
2000-08-25 04:26:06 -04:00
|
|
|
rb_warn("Can't do inplace edit for stdio");
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_defout = rb_stdout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FILE *fr = rb_fopen(fn, "r");
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ruby_inplace_mode) {
|
1998-01-16 07:13:05 -05:00
|
|
|
struct stat st, st2;
|
|
|
|
VALUE str;
|
|
|
|
FILE *fw;
|
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (TYPE(rb_defout) == T_FILE && rb_defout != rb_stdout) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_close(rb_defout);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
fstat(fileno(fr), &st);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (*ruby_inplace_mode) {
|
|
|
|
str = rb_str_new2(fn);
|
2000-08-25 04:26:06 -04:00
|
|
|
#ifdef NO_LONG_FNAME
|
1999-01-19 23:59:39 -05:00
|
|
|
ruby_add_suffix(str, ruby_inplace_mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
2000-04-10 01:48:43 -04:00
|
|
|
rb_str_cat2(str, ruby_inplace_mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
2000-08-25 04:26:06 -04:00
|
|
|
#ifdef NO_SAFE_RENAME
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
rb_warn("Can't rename %s to %s: %s, skipping file",
|
1998-01-16 07:13:05 -05:00
|
|
|
fn, RSTRING(str)->ptr, strerror(errno));
|
|
|
|
fclose(fr);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
2000-08-25 04:26:06 -04:00
|
|
|
#ifdef NO_SAFE_RENAME
|
|
|
|
rb_fatal("Can't do inplace edit without backup");
|
|
|
|
#else
|
1998-01-16 07:13:05 -05:00
|
|
|
if (unlink(fn) < 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_warn("Can't remove %s: %s, skipping file",
|
|
|
|
fn, strerror(errno));
|
1998-01-16 07:13:05 -05:00
|
|
|
fclose(fr);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
fw = rb_fopen(fn, "w");
|
2000-08-25 04:26:06 -04:00
|
|
|
#ifndef NO_SAFE_RENAME
|
1998-01-16 07:13:05 -05:00
|
|
|
fstat(fileno(fw), &st2);
|
2000-11-13 00:39:35 -05:00
|
|
|
#ifdef HAVE_FCHMOD
|
1998-01-16 07:13:05 -05:00
|
|
|
fchmod(fileno(fw), st.st_mode);
|
2000-11-13 00:39:35 -05:00
|
|
|
#else
|
|
|
|
chmod(fn, st.st_mode);
|
|
|
|
#endif
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
rb_defout = prep_stdio(fw, FMODE_WRITABLE, rb_cFile);
|
2000-08-25 04:26:06 -04:00
|
|
|
prep_path(rb_defout, fn);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
current_file = prep_stdio(fr, FMODE_READABLE, rb_cFile);
|
2000-08-25 04:26:06 -04:00
|
|
|
prep_path(current_file, fn);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
if (binmode) rb_io_binmode(current_file);
|
2003-01-12 04:24:28 -05:00
|
|
|
if (defout_binmode) rb_io_binmode(rb_defout);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
init_p = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qfalse;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qtrue;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-02-06 02:30:13 -05:00
|
|
|
argf_getline(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE line;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (!next_argv()) return Qnil;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
|
|
|
|
}
|
2000-02-08 03:54:01 -05:00
|
|
|
else if (argc == 0 && rb_rs == rb_default_rs) {
|
1999-08-13 01:45:20 -04:00
|
|
|
line = rb_io_gets(current_file);
|
|
|
|
}
|
|
|
|
else {
|
2002-02-06 02:30:13 -05: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 01:45:20 -04:00
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
if (NIL_P(line) && next_p != -1) {
|
2003-03-04 02:04:11 -05:00
|
|
|
io_close(current_file);
|
1998-01-16 07:13:05 -05:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
2003-02-04 02:27:43 -05:00
|
|
|
if (!NIL_P(line)) {
|
|
|
|
gets_lineno++;
|
|
|
|
lineno = INT2FIX(gets_lineno);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
static VALUE
|
|
|
|
rb_f_gets(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2002-02-06 02:30:13 -05:00
|
|
|
VALUE line = argf_getline(argc, argv);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_lastline_set(line);
|
1999-01-19 23:59:39 -05:00
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_gets()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE line;
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
if (rb_rs != rb_default_rs) {
|
|
|
|
return rb_f_gets(0, 0);
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
retry:
|
|
|
|
if (!next_argv()) return Qnil;
|
1999-08-13 01:45:20 -04:00
|
|
|
line = rb_io_gets(current_file);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(line) && next_p != -1) {
|
2003-03-04 02:04:11 -05:00
|
|
|
io_close(current_file);
|
1999-01-19 23:59:39 -05:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_lastline_set(line);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!NIL_P(line)) {
|
|
|
|
gets_lineno++;
|
|
|
|
lineno = INT2FIX(gets_lineno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_readline(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE *argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE line = rb_f_gets(argc, argv);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (NIL_P(line)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_eof_error();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_getc()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_warn("getc is obsolete; use STDIN.getc instead");
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_io_getc(rb_stdin);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_readlines(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE *argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE line, ary;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
ary = rb_ary_new();
|
2002-02-06 02:30:13 -05:00
|
|
|
while (!NIL_P(line = argf_getline(argc, argv))) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(ary, line);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_backquote(obj, str)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE obj, str;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE port, result;
|
2002-03-25 09:50:40 -05:00
|
|
|
OpenFile *fptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(str);
|
1998-01-16 07:19:22 -05:00
|
|
|
port = pipe_open(RSTRING(str)->ptr, "r");
|
1999-08-13 01:45:20 -04:00
|
|
|
if (NIL_P(port)) return rb_str_new(0,0);
|
2002-03-25 09:50:40 -05:00
|
|
|
|
|
|
|
GetOpenFile(port, fptr);
|
2002-12-11 04:32:41 -05:00
|
|
|
result = read_all(fptr, remain_size(fptr), Qnil);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_close(port);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(result)) return rb_str_new(0,0);
|
1998-01-16 07:13:05 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_select(argc, argv, obj)
|
1998-01-16 07:13:05 -05: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;
|
2002-08-21 11:47:54 -04:00
|
|
|
long i;
|
|
|
|
int max = 0, n;
|
1999-01-19 23:59:39 -05:00
|
|
|
int interrupt_flag = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
int pending = 0;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "13", &read, &write, &except, &timeout);
|
|
|
|
if (NIL_P(timeout)) {
|
2000-07-06 03:21:26 -04:00
|
|
|
tp = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 01:45:20 -04:00
|
|
|
timerec = rb_time_interval(timeout);
|
1998-01-16 07:13:05 -05: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 01:45:20 -04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(read)->ptr[i]), fptr);
|
1998-01-16 07:13:05 -05: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 03:21:26 -04:00
|
|
|
rp = 0;
|
1998-01-16 07:13:05 -05: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 01:45:20 -04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(write)->ptr[i]), fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
FD_SET(fileno(fptr->f), wp);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (max < fileno(fptr->f)) max = fileno(fptr->f);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (fptr->f2) {
|
|
|
|
FD_SET(fileno(fptr->f2), wp);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (max < fileno(fptr->f2)) max = fileno(fptr->f2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2000-07-06 03:21:26 -04:00
|
|
|
wp = 0;
|
1998-01-16 07:13:05 -05: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 01:45:20 -04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(except)->ptr[i]), fptr);
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
if (max < fileno(fptr->f2)) max = fileno(fptr->f2);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-07-06 03:21:26 -04:00
|
|
|
else {
|
|
|
|
ep = 0;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
max++;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
n = rb_thread_select(max, rp, wp, ep, tp);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (n < 0) {
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
if (!pending && n == 0) return Qnil; /* returns nil on timeout */
|
|
|
|
|
1999-01-19 23:59:39 -05: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 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (interrupt_flag == 0) {
|
1998-01-16 07:13:05 -05:00
|
|
|
if (rp) {
|
|
|
|
list = RARRAY(res)->ptr[0];
|
|
|
|
for (i=0; i< RARRAY(read)->len; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(read)->ptr[i]), fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (FD_ISSET(fileno(fptr->f), rp)
|
|
|
|
|| FD_ISSET(fileno(fptr->f), &pset)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(list, RARRAY(read)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wp) {
|
|
|
|
list = RARRAY(res)->ptr[1];
|
|
|
|
for (i=0; i< RARRAY(write)->len; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(write)->ptr[i]), fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (FD_ISSET(fileno(fptr->f), wp)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(list, RARRAY(write)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (fptr->f2 && FD_ISSET(fileno(fptr->f2), wp)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(list, RARRAY(write)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ep) {
|
|
|
|
list = RARRAY(res)->ptr[2];
|
|
|
|
for (i=0; i< RARRAY(except)->len; i++) {
|
1999-08-13 01:45:20 -04:00
|
|
|
GetOpenFile(rb_io_get_io(RARRAY(except)->ptr[i]), fptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (FD_ISSET(fileno(fptr->f), ep)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(list, RARRAY(except)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else if (fptr->f2 && FD_ISSET(fileno(fptr->f2), ep)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_ary_push(list, RARRAY(except)->ptr[i]);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res; /* returns an empty array on interrupt */
|
|
|
|
}
|
|
|
|
|
2001-03-28 03:43:25 -05:00
|
|
|
#if !defined(MSDOS) && !defined(__human68k__)
|
2000-11-27 04:23:38 -05:00
|
|
|
static int
|
2001-06-05 03:19:39 -04:00
|
|
|
io_cntl(fd, cmd, narg, io_p)
|
2000-11-27 04:23:38 -05: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 03:43:25 -05:00
|
|
|
#endif
|
2000-11-27 04:23:38 -05:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_ctl(io, req, arg, io_p)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE io, req, arg;
|
1998-01-16 07:13:05 -05:00
|
|
|
int io_p;
|
|
|
|
{
|
|
|
|
#if !defined(MSDOS) && !defined(__human68k__)
|
1999-10-16 06:33:06 -04:00
|
|
|
int cmd = NUM2ULONG(req);
|
1998-01-16 07:13:05 -05:00
|
|
|
OpenFile *fptr;
|
2002-08-21 11:47:54 -04:00
|
|
|
long len = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
long narg = 0;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
rb_secure(2);
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(arg) || arg == Qfalse) {
|
1998-01-16 07:13:05 -05:00
|
|
|
narg = 0;
|
|
|
|
}
|
|
|
|
else if (FIXNUM_P(arg)) {
|
2002-08-21 11:47:54 -04:00
|
|
|
narg = FIX2LONG(arg);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (arg == Qtrue) {
|
1998-01-16 07:13:05 -05:00
|
|
|
narg = 1;
|
|
|
|
}
|
2001-06-05 03:19:39 -04:00
|
|
|
else if (rb_obj_is_kind_of(arg, rb_cInteger)) {
|
|
|
|
narg = NUM2LONG(arg);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
else {
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(arg);
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
rb_str_modify(arg);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1998-01-16 07:19:22 -05:00
|
|
|
if (len <= RSTRING(arg)->len) {
|
|
|
|
len = RSTRING(arg)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1998-01-16 07:19:22 -05:00
|
|
|
if (RSTRING(arg)->len < len) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_str_resize(arg, len+1);
|
1998-01-16 07:19:22 -05:00
|
|
|
}
|
|
|
|
RSTRING(arg)->ptr[len] = 17; /* a little sanity check here */
|
|
|
|
narg = (long)RSTRING(arg)->ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-11-27 04:23:38 -05:00
|
|
|
retval = io_cntl(fileno(fptr->f), cmd, narg, io_p);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (retval < 0) rb_sys_fail(fptr->path);
|
1998-01-16 07:19:22 -05:00
|
|
|
if (TYPE(arg) == T_STRING && RSTRING(arg)->ptr[len] != 17) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "return value overflowed string");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-11-27 04:23:38 -05:00
|
|
|
|
2001-02-13 00:09:11 -05:00
|
|
|
if (fptr->f2 && fileno(fptr->f) != fileno(fptr->f2)) {
|
|
|
|
/* call on f2 too; ignore result */
|
2000-11-27 04:23:38 -05:00
|
|
|
io_cntl(fileno(fptr->f2), cmd, narg, io_p);
|
|
|
|
}
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
return INT2NUM(retval);
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_ioctl(argc, argv, io)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
VALUE req, arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &req, &arg);
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_io_ctl(io, req, arg, 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_fcntl(argc, argv, io)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
#ifdef HAVE_FCNTL
|
|
|
|
VALUE req, arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &req, &arg);
|
1999-01-19 23:59:39 -05:00
|
|
|
return rb_io_ctl(io, req, arg, 0);
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_f_syscall(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2000-08-09 03:35:01 -04:00
|
|
|
#if defined(HAVE_SYSCALL) && !defined(__CHECKER__)
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
if (argc == 0)
|
|
|
|
rb_raise(rb_eArgError, "too few arguments for syscall");
|
2002-08-21 11:47:54 -04:00
|
|
|
arg[0] = NUM2LONG(argv[0]); argv++;
|
1998-01-16 07:13:05 -05:00
|
|
|
while (items--) {
|
|
|
|
if (FIXNUM_P(*argv)) {
|
2002-08-21 11:47:54 -04:00
|
|
|
arg[i] = (unsigned long)NUM2LONG(*argv);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
else {
|
2001-05-02 00:22:21 -04:00
|
|
|
VALUE v = *argv;
|
2001-03-28 03:43:25 -05:00
|
|
|
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(v);
|
2001-03-28 03:43:25 -05:00
|
|
|
rb_str_modify(v);
|
2001-05-02 00:22:21 -04:00
|
|
|
arg[i] = (unsigned long)RSTRING(v)->ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2001-03-28 03:43:25 -05:00
|
|
|
argv++;
|
1998-01-16 07:13:05 -05:00
|
|
|
i++;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
TRAP_BEG;
|
1998-01-16 07:13:05 -05: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-19 23:59:39 -05:00
|
|
|
TRAP_END;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (retval < 0) rb_sys_fail(0);
|
|
|
|
return INT2NUM(retval);
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_s_pipe()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
#ifndef __human68k__
|
|
|
|
int pipes[2];
|
2001-05-16 05:05:54 -04:00
|
|
|
VALUE r, w;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#ifdef _WIN32
|
1998-01-16 07:13:05 -05:00
|
|
|
if (_pipe(pipes, 1024, O_BINARY) == -1)
|
|
|
|
#else
|
|
|
|
if (pipe(pipes) == -1)
|
|
|
|
#endif
|
|
|
|
rb_sys_fail(0);
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
r = prep_stdio(rb_fdopen(pipes[0], "r"), FMODE_READABLE, rb_cIO);
|
2000-05-01 05:42:38 -04:00
|
|
|
w = prep_stdio(rb_fdopen(pipes[1], "w"), FMODE_WRITABLE|FMODE_SYNC, rb_cIO);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-07-04 00:17:26 -04:00
|
|
|
return rb_assoc_new(r, w);
|
1998-01-16 07:13:05 -05:00
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-19 23:59:39 -05:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 07:13:05 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct foreach_arg {
|
|
|
|
int argc;
|
|
|
|
VALUE sep;
|
|
|
|
VALUE io;
|
2002-02-06 02:30:13 -05:00
|
|
|
OpenFile *fptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
2001-01-18 03:43:14 -05:00
|
|
|
io_s_foreach(arg)
|
1998-01-16 07:13:05 -05:00
|
|
|
struct foreach_arg *arg;
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
2002-02-06 02:30:13 -05:00
|
|
|
while (!NIL_P(str = rb_io_getline(arg->sep, arg->fptr))) {
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_yield(str);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2002-02-06 02:30:13 -05:00
|
|
|
rb_io_s_foreach(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
2002-02-06 02:30:13 -05:00
|
|
|
VALUE fname, io;
|
|
|
|
OpenFile *fptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
struct foreach_arg arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &fname, &arg.sep);
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(fname);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-02-06 02:30:13 -05: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 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2001-01-18 03:43:14 -05:00
|
|
|
io_s_readlines(arg)
|
1998-01-16 07:13:05 -05:00
|
|
|
struct foreach_arg *arg;
|
|
|
|
{
|
2001-01-18 03:43:14 -05:00
|
|
|
return rb_io_readlines(arg->argc, &arg->sep, arg->io);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_io_s_readlines(argc, argv, io)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE fname;
|
1998-01-16 07:13:05 -05:00
|
|
|
struct foreach_arg arg;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &fname, &arg.sep);
|
2001-05-02 00:22:21 -04:00
|
|
|
SafeStringValue(fname);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
arg.argc = argc - 1;
|
1999-01-19 23:59:39 -05:00
|
|
|
arg.io = rb_io_open(RSTRING(fname)->ptr, "r");
|
1999-08-13 01:45:20 -04:00
|
|
|
if (NIL_P(arg.io)) return Qnil;
|
2001-01-18 03:43:14 -05: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 00:22:21 -04:00
|
|
|
SafeStringValue(fname);
|
2001-01-18 03:43:14 -05: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 00:45:13 -05:00
|
|
|
rb_io_seek(arg.io, offset, SEEK_SET);
|
2001-01-18 03:43:14 -05:00
|
|
|
}
|
|
|
|
return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_tell()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01: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-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2001-03-13 00:45:13 -05:00
|
|
|
argf_seek_m(argc, argv, self)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
1999-01-19 23:59:39 -05:00
|
|
|
{
|
1999-08-13 01: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 00:45:13 -05:00
|
|
|
return rb_io_seek_m(argc, argv, current_file);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_set_pos(self, offset)
|
|
|
|
VALUE self, offset;
|
|
|
|
{
|
|
|
|
if (!next_argv()) {
|
2001-01-18 03:43:14 -05:00
|
|
|
rb_raise(rb_eArgError, "no stream to set position");
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_to_io()
|
|
|
|
{
|
2003-02-18 09:30:17 -05:00
|
|
|
next_argv();
|
1999-08-13 01:45:20 -04:00
|
|
|
return current_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
argf_read(argc, argv)
|
1998-01-16 07:13:05 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE tmp, str;
|
2002-08-21 11:47:54 -04:00
|
|
|
long len = 0;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-08-21 11:47:54 -04:00
|
|
|
if (argc == 1) len = NUM2LONG(argv[0]);
|
1998-01-16 07:13:05 -05:00
|
|
|
str = Qnil;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (!next_argv()) return str;
|
1999-08-13 01:45:20 -04:00
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
tmp = argf_forward();
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(tmp);
|
1999-08-13 01:45:20 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmp = io_read(argc, argv, current_file);
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
if (NIL_P(tmp) && next_p != -1) {
|
2003-03-04 02:04:11 -05:00
|
|
|
io_close(current_file);
|
1998-01-16 07:13:05 -05:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (NIL_P(tmp) || RSTRING(tmp)->len == 0) return str;
|
1998-01-16 07:13:05 -05:00
|
|
|
else if (NIL_P(str)) str = tmp;
|
2000-04-10 01:48:43 -04:00
|
|
|
else rb_str_append(str, tmp);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (argc == 0) {
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
if (RSTRING(tmp)->len < len) {
|
|
|
|
len -= RSTRING(tmp)->len;
|
2002-08-21 11:47:54 -04:00
|
|
|
argv[0] = LONG2FIX(len);
|
1998-01-16 07:13:05 -05:00
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_getc()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE byte;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if (!next_argv()) return Qnil;
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
if (NIL_P(byte) && next_p != -1) {
|
2003-03-04 02:04:11 -05:00
|
|
|
io_close(current_file);
|
1998-01-16 07:13:05 -05:00
|
|
|
next_p = 1;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_readchar()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
VALUE c = argf_getc();
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (NIL_P(c)) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_eof_error();
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_eof()
|
|
|
|
{
|
2000-08-15 02:22:49 -04:00
|
|
|
if (!next_argv()) return Qtrue;
|
2001-08-23 02:02:15 -04:00
|
|
|
if (next_p == 1) {
|
1999-08-13 01:45:20 -04:00
|
|
|
return Qtrue;
|
2000-08-15 02:22:49 -04:00
|
|
|
}
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
int argc;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE *argv;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
2002-02-06 02:30:13 -05:00
|
|
|
while (RTEST(str = argf_getline(argc, argv))) {
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_yield(str);
|
|
|
|
}
|
1999-08-13 01:45:20 -04:00
|
|
|
return argf;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_each_byte()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE byte;
|
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
while (!NIL_P(byte = argf_getc())) {
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_yield(byte);
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_filename()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-03-28 04:17:54 -05:00
|
|
|
next_argv();
|
1998-01-16 07:13:05 -05:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_file()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2002-03-28 04:17:54 -05:00
|
|
|
next_argv();
|
1999-08-13 01:45:20 -04:00
|
|
|
return current_file;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
2002-03-28 04:17:54 -05: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 07:13:05 -05:00
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_skip()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
if (next_p != -1) {
|
2003-03-04 02:04:11 -05:00
|
|
|
io_close(current_file);
|
1998-01-16 07:13:05 -05:00
|
|
|
next_p = 1;
|
|
|
|
}
|
|
|
|
return argf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_close()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2003-03-04 02:04:11 -05:00
|
|
|
io_close(current_file);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (next_p != -1) {
|
|
|
|
next_p = 1;
|
|
|
|
}
|
|
|
|
gets_lineno = 0;
|
|
|
|
return argf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-08-13 01:45:20 -04:00
|
|
|
argf_closed()
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
1999-08-13 01:45:20 -04:00
|
|
|
if (TYPE(current_file) != T_FILE) {
|
|
|
|
return argf_forward();
|
|
|
|
}
|
|
|
|
return rb_io_closed(current_file);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
opt_i_get()
|
|
|
|
{
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!ruby_inplace_mode) return Qnil;
|
|
|
|
return rb_str_new2(ruby_inplace_mode);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
opt_i_set(val)
|
1998-01-16 07:19:22 -05:00
|
|
|
VALUE val;
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
2001-06-22 05: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 00:22:21 -04:00
|
|
|
if (ruby_inplace_mode) free(ruby_inplace_mode);
|
2001-06-19 00:35:17 -04:00
|
|
|
ruby_inplace_mode = 0;
|
2001-05-02 00:22:21 -04:00
|
|
|
ruby_inplace_mode = strdup(RSTRING(val)->ptr);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_IO()
|
|
|
|
{
|
2003-02-15 13:16:52 -05:00
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#include <sys/cygwin.h>
|
|
|
|
static struct __cygwin_perfile pf[] =
|
|
|
|
{
|
|
|
|
{"", O_RDONLY | O_BINARY},
|
|
|
|
{"", O_WRONLY | O_BINARY},
|
|
|
|
{"", O_RDWR | O_BINARY},
|
|
|
|
{"", O_APPEND | O_BINARY},
|
|
|
|
{NULL, 0}
|
|
|
|
};
|
|
|
|
cygwin_internal(CW_PERFILE, pf);
|
|
|
|
#endif
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_eIOError = rb_define_class("IOError", rb_eStandardError);
|
|
|
|
rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
id_write = rb_intern("write");
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_global_function("syscall", rb_f_syscall, -1);
|
|
|
|
|
|
|
|
rb_define_global_function("open", rb_f_open, -1);
|
1999-08-24 04:21:56 -04:00
|
|
|
rb_define_global_function("printf", rb_f_printf, -1);
|
1999-01-19 23:59:39 -05: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);
|
|
|
|
|
2002-12-20 03:33:17 -05:00
|
|
|
rb_define_alloc_func(rb_cIO, io_alloc);
|
2002-01-18 09:24:01 -05: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 01:35:32 -04:00
|
|
|
rb_define_singleton_method(rb_cIO, "sysopen", rb_io_s_sysopen, -1);
|
2002-01-18 09:24:01 -05:00
|
|
|
rb_define_singleton_method(rb_cIO, "for_fd", rb_class_new_instance, -1);
|
1999-01-19 23:59:39 -05: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 03:43:14 -05:00
|
|
|
rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2002-01-18 09:24:01 -05:00
|
|
|
rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
|
|
|
|
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_output_fs = Qnil;
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
|
|
|
|
|
2001-10-29 00:07:26 -05:00
|
|
|
rb_rs = rb_default_rs = rb_str_new2("\n");
|
|
|
|
rb_output_rs = Qnil;
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_global_variable(&rb_default_rs);
|
2000-01-31 22:12:21 -05:00
|
|
|
OBJ_FREEZE(rb_default_rs); /* avoid modifying RS_default */
|
2001-05-02 00:22:21 -04:00
|
|
|
rb_define_variable("$/", &rb_rs);
|
|
|
|
rb_define_variable("$-0", &rb_rs);
|
|
|
|
rb_define_variable("$\\", &rb_output_rs);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_define_hooked_variable("$.", &lineno, 0, lineno_setter);
|
|
|
|
rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
|
|
|
|
|
2002-12-10 01:23:44 -05:00
|
|
|
rb_define_method(rb_cIO, "copy_object", rb_io_copy_object, 1);
|
2000-02-29 03:05:32 -05:00
|
|
|
rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
|
1999-01-19 23:59:39 -05: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);
|
2002-12-11 04:32:41 -05:00
|
|
|
rb_define_method(rb_cIO, "sysread", rb_io_sysread, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
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 02:30:43 -05:00
|
|
|
rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
|
1999-01-19 23:59:39 -05: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 01:45:20 -04:00
|
|
|
rb_define_method(rb_cIO, "read", io_read, -1);
|
|
|
|
rb_define_method(rb_cIO, "write", io_write, 1);
|
2000-01-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
|
1999-01-19 23:59:39 -05: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 00:45:13 -05:00
|
|
|
rb_define_method(rb_cIO, "seek", rb_io_seek_m, -1);
|
1999-11-04 03:39:57 -05: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-19 23:59:39 -05: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-04 23:41:21 -05:00
|
|
|
rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
|
1999-01-19 23:59:39 -05: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 00:28:00 -05:00
|
|
|
rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
|
|
|
|
rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
|
2000-05-24 00:34:26 -04:00
|
|
|
rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
|
2002-02-22 05:28:47 -05:00
|
|
|
rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
|
1999-01-19 23:59:39 -05:00
|
|
|
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
rb_defout = rb_stdout;
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_define_hooked_variable("$>", &rb_defout, 0, rb_io_defset);
|
1999-11-17 02:30:37 -05:00
|
|
|
rb_define_hooked_variable("$defout", &rb_defout, 0, rb_io_defset);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
rb_define_global_const("STDIN", rb_stdin);
|
|
|
|
rb_define_global_const("STDOUT", rb_stdout);
|
|
|
|
rb_define_global_const("STDERR", rb_stderr);
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
argf = rb_obj_alloc(rb_cObject);
|
|
|
|
rb_extend_object(argf, rb_mEnumerable);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
rb_define_readonly_variable("$<", &argf);
|
|
|
|
rb_define_global_const("ARGF", argf);
|
|
|
|
|
1999-08-13 01: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 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
rb_define_singleton_method(argf, "read", argf_read, -1);
|
1999-01-19 23:59:39 -05: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 01: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 00:45:13 -05:00
|
|
|
rb_define_singleton_method(argf, "seek", argf_seek_m, -1);
|
1999-08-13 01: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-19 23:59:39 -05:00
|
|
|
filename = rb_str_new2("-");
|
1998-01-16 07:13:05 -05:00
|
|
|
rb_define_readonly_variable("$FILENAME", &filename);
|
|
|
|
|
|
|
|
rb_define_virtual_variable("$-i", opt_i_get, opt_i_set);
|
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-14 22:18:08 -05:00
|
|
|
#if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
|
1998-01-16 07:13:05 -05:00
|
|
|
atexit(pipe_atexit);
|
|
|
|
#endif
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
Init_File();
|
1999-08-13 01:45:20 -04:00
|
|
|
|
2000-03-23 03:37:35 -05:00
|
|
|
rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1);
|
1999-08-13 01: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 02:11:22 -05:00
|
|
|
#ifdef O_SYNC
|
2000-02-18 01:59:36 -05:00
|
|
|
rb_file_const("SYNC", INT2FIX(O_SYNC));
|
2000-02-17 02:11:22 -05:00
|
|
|
#endif
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|