summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'io.c')
-rw-r--r--io.c13341
1 files changed, 8602 insertions, 4739 deletions
diff --git a/io.c b/io.c
index 6e3fd85d2b..effcb349c3 100644
--- a/io.c
+++ b/io.c
@@ -11,16 +11,30 @@
**********************************************************************/
-#include "ruby/ruby.h"
-#include "ruby/io.h"
-#include "ruby/thread.h"
-#include "dln.h"
-#include "internal.h"
-#include "id.h"
+#include "ruby/internal/config.h"
+
+#include "ruby/fiber/scheduler.h"
+#include "ruby/io/buffer.h"
+
#include <ctype.h>
#include <errno.h>
-#include "ruby_atomic.h"
+#include <stddef.h>
+
+/* non-Linux poll may not work on all FDs */
+#if defined(HAVE_POLL)
+# if defined(__linux__)
+# define USE_POLL 1
+# endif
+# if defined(__FreeBSD_version) && __FreeBSD_version >= 1100000
+# define USE_POLL 1
+# endif
+#endif
+#ifndef USE_POLL
+# define USE_POLL 0
+#endif
+
+#undef free
#define free(x) xfree(x)
#if defined(DOSISH) || defined(__CYGWIN__)
@@ -31,12 +45,10 @@
#if defined HAVE_NET_SOCKET_H
# include <net/socket.h>
#elif defined HAVE_SYS_SOCKET_H
-# ifndef __native_client__
-# include <sys/socket.h>
-# endif
+# include <sys/socket.h>
#endif
-#if defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32) || defined(__EMX__) || defined(__BEOS__) || defined(__HAIKU__)
+#if defined(__BOW__) || defined(__CYGWIN__) || defined(_WIN32)
# define NO_SAFE_RENAME
#endif
@@ -45,30 +57,26 @@
#endif
#ifdef __QNXNTO__
-#include "unix.h"
+#include <unix.h>
#endif
#include <sys/types.h>
#if defined(HAVE_SYS_IOCTL_H) && !defined(_WIN32)
#include <sys/ioctl.h>
#endif
-#if defined(__native_client__) && defined(NACL_NEWLIB)
-# include "nacl/ioctl.h"
-#endif
#if defined(HAVE_FCNTL_H) || defined(_WIN32)
#include <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
#include <sys/fcntl.h>
#endif
-#if !HAVE_OFF_T && !defined(off_t)
-# define off_t long
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
#endif
#include <sys/stat.h>
-/* EMX has sys/param.h, but.. */
-#if defined(HAVE_SYS_PARAM_H) && !(defined(__EMX__) || defined(__HIUX_MPP__))
+#if defined(HAVE_SYS_PARAM_H) || defined(__HIUX_MPP__)
# include <sys/param.h>
#endif
@@ -86,20 +94,61 @@
#include <sys/syscall.h>
#endif
-#if defined(__BEOS__) || defined(__HAIKU__)
-# ifndef NOFILE
-# define NOFILE (OPEN_MAX)
+#ifdef HAVE_SYS_UIO_H
+#include <sys/uio.h>
+#endif
+
+#ifdef HAVE_SYS_WAIT_H
+# include <sys/wait.h> /* for WNOHANG on BSD */
+#endif
+
+#ifdef HAVE_COPYFILE_H
+# include <copyfile.h>
+
+# ifndef COPYFILE_STATE_COPIED
+/*
+ * Some OSes (e.g., OSX < 10.6) implement fcopyfile() but not
+ * COPYFILE_STATE_COPIED. Since the only use of the former here
+ * requires the latter, we disable the former when the latter is undefined.
+ */
+# undef HAVE_FCOPYFILE
# endif
+
#endif
+#include "ruby/internal/stdbool.h"
+#include "ccan/list/list.h"
+#include "dln.h"
+#include "encindex.h"
+#include "id.h"
+#include "internal.h"
+#include "internal/class.h"
+#include "internal/encoding.h"
+#include "internal/error.h"
+#include "internal/inits.h"
+#include "internal/io.h"
+#include "internal/numeric.h"
+#include "internal/object.h"
+#include "internal/process.h"
+#include "internal/thread.h"
+#include "internal/transcode.h"
+#include "internal/variable.h"
+#include "ruby/io.h"
+#include "ruby/io/buffer.h"
+#include "ruby/missing.h"
+#include "ruby/thread.h"
#include "ruby/util.h"
+#include "ruby_atomic.h"
+#include "ruby/ractor.h"
-#ifndef O_ACCMODE
-#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
+#if !USE_POLL
+# include "vm_core.h"
#endif
-#if SIZEOF_OFF_T > SIZEOF_LONG && !defined(HAVE_LONG_LONG)
-# error off_t is bigger than long, but you have no long long...
+#include "builtin.h"
+
+#ifndef O_ACCMODE
+#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
#endif
#ifndef PIPE_BUF
@@ -124,20 +173,23 @@ off_t __syscall(quad_t number, ...);
#define IO_RBUF_CAPA_FOR(fptr) (NEED_READCONV(fptr) ? IO_CBUF_CAPA_MIN : IO_RBUF_CAPA_MIN)
#define IO_WBUF_CAPA_MIN 8192
+#define IO_MAX_BUFFER_GROWTH 8 * 1024 * 1024 // 8MB
+
/* define system APIs */
#ifdef _WIN32
#undef open
#define open rb_w32_uopen
+#undef rename
+#define rename(f, t) rb_w32_urename((f), (t))
+#include "win32/file.h"
#endif
VALUE rb_cIO;
VALUE rb_eEOFError;
VALUE rb_eIOError;
+VALUE rb_eIOTimeoutError;
VALUE rb_mWaitReadable;
VALUE rb_mWaitWritable;
-extern VALUE rb_eEAGAIN;
-extern VALUE rb_eEWOULDBLOCK;
-extern VALUE rb_eEINPROGRESS;
static VALUE rb_eEAGAINWaitReadable;
static VALUE rb_eEAGAINWaitWritable;
@@ -147,7 +199,6 @@ static VALUE rb_eEINPROGRESSWaitWritable;
static VALUE rb_eEINPROGRESSWaitReadable;
VALUE rb_stdin, rb_stdout, rb_stderr;
-VALUE rb_deferr; /* rescue VIM plugin */
static VALUE orig_stdout, orig_stderr;
VALUE rb_output_fs;
@@ -157,10 +208,11 @@ VALUE rb_default_rs;
static VALUE argf;
-static ID id_write, id_read, id_getc, id_flush, id_readpartial, id_set_encoding;
-static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
-static VALUE sym_textmode, sym_binmode, sym_autoclose, sym_exception;
+static ID id_write, id_read, id_getc, id_flush, id_readpartial, id_set_encoding, id_fileno;
+static VALUE sym_mode, sym_perm, sym_flags, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
+static VALUE sym_textmode, sym_binmode, sym_autoclose;
static VALUE sym_SET, sym_CUR, sym_END;
+static VALUE sym_wait_readable, sym_wait_writable;
#ifdef SEEK_DATA
static VALUE sym_DATA;
#endif
@@ -168,13 +220,26 @@ static VALUE sym_DATA;
static VALUE sym_HOLE;
#endif
+static VALUE prep_io(int fd, enum rb_io_mode fmode, VALUE klass, const char *path);
+
+VALUE
+rb_io_blocking_region_wait(struct rb_io *io, rb_blocking_function_t *function, void *argument, enum rb_io_event events)
+{
+ return rb_thread_io_blocking_call(io, function, argument, events);
+}
+
+VALUE rb_io_blocking_region(struct rb_io *io, rb_blocking_function_t *function, void *argument)
+{
+ return rb_io_blocking_region_wait(io, function, argument, 0);
+}
+
struct argf {
VALUE filename, current_file;
long last_lineno; /* $. */
long lineno;
VALUE argv;
- char *inplace;
- struct rb_io_enc_t encs;
+ VALUE inplace;
+ struct rb_io_encoding encs;
int8_t init_p, next_p, binmode;
};
@@ -182,15 +247,27 @@ static rb_atomic_t max_file_descriptor = NOFILE;
void
rb_update_max_fd(int fd)
{
- struct stat buf;
rb_atomic_t afd = (rb_atomic_t)fd;
+ rb_atomic_t max_fd = max_file_descriptor;
+ int err;
+
+ if (fd < 0 || afd <= max_fd)
+ return;
- if (fstat(fd, &buf) != 0 && errno == EBADF) {
+#if defined(HAVE_FCNTL) && defined(F_GETFL)
+ err = fcntl(fd, F_GETFL) == -1;
+#else
+ {
+ struct stat buf;
+ err = fstat(fd, &buf) != 0;
+ }
+#endif
+ if (err && errno == EBADF) {
rb_bug("rb_update_max_fd: invalid fd (%d) given.", fd);
}
- while (max_file_descriptor < afd) {
- ATOMIC_CAS(max_file_descriptor, max_file_descriptor, afd);
+ while (max_fd < afd) {
+ max_fd = ATOMIC_CAS(max_file_descriptor, max_fd, afd);
}
}
@@ -210,7 +287,7 @@ rb_maygvl_fd_fix_cloexec(int fd)
flags2 = flags | FD_CLOEXEC; /* Set CLOEXEC for non-standard file descriptors: 3, 4, 5, ... */
if (flags != flags2) {
ret = fcntl(fd, F_SETFD, flags2);
- if (ret == -1) {
+ if (ret != 0) {
rb_bug("rb_maygvl_fd_fix_cloexec: fcntl(%d, F_SETFD, %d) failed: %s", fd, flags2, strerror(errno));
}
}
@@ -224,19 +301,65 @@ rb_fd_fix_cloexec(int fd)
rb_update_max_fd(fd);
}
+/* this is only called once */
+static int
+rb_fix_detect_o_cloexec(int fd)
+{
+#if defined(O_CLOEXEC) && defined(F_GETFD)
+ int flags = fcntl(fd, F_GETFD);
+
+ if (flags == -1)
+ rb_bug("rb_fix_detect_o_cloexec: fcntl(%d, F_GETFD) failed: %s", fd, strerror(errno));
+
+ if (flags & FD_CLOEXEC)
+ return 1;
+#endif /* fall through if O_CLOEXEC does not work: */
+ rb_maygvl_fd_fix_cloexec(fd);
+ return 0;
+}
+
+static inline bool
+io_again_p(int e)
+{
+ return (e == EWOULDBLOCK) || (e == EAGAIN);
+}
+
int
rb_cloexec_open(const char *pathname, int flags, mode_t mode)
{
int ret;
+ static int o_cloexec_state = -1; /* <0: unknown, 0: ignored, >0: working */
+
+ static const int retry_interval = 0;
+ static const int retry_max_count = 10000;
+
+ int retry_count = 0;
+
#ifdef O_CLOEXEC
/* O_CLOEXEC is available since Linux 2.6.23. Linux 2.6.18 silently ignore it. */
flags |= O_CLOEXEC;
#elif defined O_NOINHERIT
flags |= O_NOINHERIT;
#endif
- ret = open(pathname, flags, mode);
- if (ret == -1) return -1;
- rb_maygvl_fd_fix_cloexec(ret);
+
+ while ((ret = open(pathname, flags, mode)) == -1) {
+ int e = errno;
+ if (!io_again_p(e)) break;
+ if (retry_count++ >= retry_max_count) break;
+
+ sleep(retry_interval);
+ }
+
+ if (ret < 0) return ret;
+ if (ret <= 2 || o_cloexec_state == 0) {
+ rb_maygvl_fd_fix_cloexec(ret);
+ }
+ else if (o_cloexec_state > 0) {
+ return ret;
+ }
+ else {
+ o_cloexec_state = rb_fix_detect_o_cloexec(ret);
+ }
return ret;
}
@@ -276,47 +399,62 @@ rb_cloexec_dup2(int oldfd, int newfd)
#else
ret = dup2(oldfd, newfd);
#endif
- if (ret == -1) return -1;
+ if (ret < 0) return ret;
}
rb_maygvl_fd_fix_cloexec(ret);
return ret;
}
-int
-rb_cloexec_pipe(int fildes[2])
+static int
+rb_fd_set_nonblock(int fd)
{
- int ret;
+#ifdef _WIN32
+ return rb_w32_set_nonblock(fd);
+#elif defined(F_GETFL)
+ int oflags = fcntl(fd, F_GETFL);
-#if defined(HAVE_PIPE2)
- static int try_pipe2 = 1;
- if (try_pipe2) {
- ret = pipe2(fildes, O_CLOEXEC);
- if (ret != -1)
- return ret;
- /* pipe2 is available since Linux 2.6.27, glibc 2.9. */
- if (errno == ENOSYS) {
- try_pipe2 = 0;
- ret = pipe(fildes);
- }
- }
- else {
- ret = pipe(fildes);
- }
+ if (oflags == -1)
+ return -1;
+ if (oflags & O_NONBLOCK)
+ return 0;
+ oflags |= O_NONBLOCK;
+ return fcntl(fd, F_SETFL, oflags);
+#endif
+ return 0;
+}
+
+int
+rb_cloexec_pipe(int descriptors[2])
+{
+#ifdef HAVE_PIPE2
+ int result = pipe2(descriptors, O_CLOEXEC | O_NONBLOCK);
#else
- ret = pipe(fildes);
+ int result = pipe(descriptors);
#endif
- if (ret == -1) return -1;
+
+ if (result < 0)
+ return result;
+
#ifdef __CYGWIN__
- if (ret == 0 && fildes[1] == -1) {
- close(fildes[0]);
- fildes[0] = -1;
- errno = ENFILE;
- return -1;
+ if (result == 0 && descriptors[1] == -1) {
+ close(descriptors[0]);
+ descriptors[0] = -1;
+ errno = ENFILE;
+ return -1;
}
#endif
- rb_maygvl_fd_fix_cloexec(fildes[0]);
- rb_maygvl_fd_fix_cloexec(fildes[1]);
- return ret;
+
+#ifndef HAVE_PIPE2
+ rb_maygvl_fd_fix_cloexec(descriptors[0]);
+ rb_maygvl_fd_fix_cloexec(descriptors[1]);
+
+#ifndef _WIN32
+ rb_fd_set_nonblock(descriptors[0]);
+ rb_fd_set_nonblock(descriptors[1]);
+#endif
+#endif
+
+ return result;
}
int
@@ -346,40 +484,23 @@ rb_cloexec_fcntl_dupfd(int fd, int minfd)
}
#elif defined(HAVE_FCNTL) && defined(F_DUPFD)
ret = fcntl(fd, F_DUPFD, minfd);
-#elif defined(HAVE_DUP)
+#else
ret = dup(fd);
- if (ret != -1 && ret < minfd) {
+ if (ret >= 0 && ret < minfd) {
const int prev_fd = ret;
ret = rb_cloexec_fcntl_dupfd(fd, minfd);
close(prev_fd);
}
return ret;
-#else
-# error "dup() or fcntl(F_DUPFD) must be supported."
#endif
- if (ret == -1) return -1;
+ if (ret < 0) return ret;
rb_maygvl_fd_fix_cloexec(ret);
return ret;
}
#define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
#define ARGF argf_of(argf)
-
-#ifdef _STDIO_USES_IOSTREAM /* GNU libc */
-# ifdef _IO_fpos_t
-# define STDIO_READ_DATA_PENDING(fp) ((fp)->_IO_read_ptr != (fp)->_IO_read_end)
-# else
-# define STDIO_READ_DATA_PENDING(fp) ((fp)->_gptr < (fp)->_egptr)
-# endif
-#elif defined(FILE_COUNT)
-# define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_COUNT > 0)
-#elif defined(FILE_READEND)
-# define STDIO_READ_DATA_PENDING(fp) ((fp)->FILE_READPTR < (fp)->FILE_READEND)
-#elif defined(__BEOS__) || defined(__HAIKU__)
-# define STDIO_READ_DATA_PENDING(fp) ((fp)->_state._eof == 0)
-#else
-# define STDIO_READ_DATA_PENDING(fp) (!feof(fp))
-#endif
+#define ARGF_SET(field, value) RB_OBJ_WRITE(argf, &ARGF.field, value)
#define GetWriteIO(io) rb_io_get_write_io(io)
@@ -394,16 +515,16 @@ rb_cloexec_fcntl_dupfd(int fd, int minfd)
#if defined(_WIN32)
#define WAIT_FD_IN_WIN32(fptr) \
- (rb_w32_io_cancelable_p((fptr)->fd) ? 0 : rb_thread_wait_fd((fptr)->fd))
+ (rb_w32_io_cancelable_p((fptr)->fd) ? Qnil : rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), RUBY_IO_TIMEOUT_DEFAULT))
#else
#define WAIT_FD_IN_WIN32(fptr)
#endif
#define READ_CHECK(fptr) do {\
if (!READ_DATA_PENDING(fptr)) {\
- WAIT_FD_IN_WIN32(fptr);\
- rb_io_check_closed(fptr);\
- }\
+ WAIT_FD_IN_WIN32(fptr);\
+ rb_io_check_closed(fptr);\
+ }\
} while(0)
#ifndef S_ISSOCK
@@ -421,11 +542,49 @@ rb_cloexec_fcntl_dupfd(int fd, int minfd)
#endif
static int io_fflush(rb_io_t *);
-static rb_io_t *flush_before_seek(rb_io_t *fptr);
+static rb_io_t *flush_before_seek(rb_io_t *fptr, bool discard_rbuf);
+static void clear_codeconv(rb_io_t *fptr);
+
+#define FMODE_SIGNAL_ON_EPIPE (1<<17)
+
+#define fptr_signal_on_epipe(fptr) \
+ (((fptr)->mode & FMODE_SIGNAL_ON_EPIPE) != 0)
+
+#define fptr_set_signal_on_epipe(fptr, flag) \
+ ((flag) ? \
+ (fptr)->mode |= FMODE_SIGNAL_ON_EPIPE : \
+ (fptr)->mode &= ~FMODE_SIGNAL_ON_EPIPE)
+
+extern ID ruby_static_id_signo;
+
+NORETURN(static void rb_sys_fail_on_write(rb_io_t *fptr));
+static void
+rb_sys_fail_on_write(rb_io_t *fptr)
+{
+ int e = errno;
+ VALUE errinfo = rb_syserr_new_path(e, (fptr)->pathv);
+#if defined EPIPE
+ if (fptr_signal_on_epipe(fptr) && (e == EPIPE)) {
+ const VALUE sig =
+# if defined SIGPIPE
+ INT2FIX(SIGPIPE) - INT2FIX(0) +
+# endif
+ INT2FIX(0);
+ rb_ivar_set(errinfo, ruby_static_id_signo, sig);
+ }
+#endif
+ rb_exc_raise(errinfo);
+}
#define NEED_NEWLINE_DECORATOR_ON_READ(fptr) ((fptr)->mode & FMODE_TEXTMODE)
#define NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) ((fptr)->mode & FMODE_TEXTMODE)
#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
+# define RUBY_CRLF_ENVIRONMENT 1
+#else
+# define RUBY_CRLF_ENVIRONMENT 0
+#endif
+
+#if RUBY_CRLF_ENVIRONMENT
/* Windows */
# define DEFAULT_TEXTMODE FMODE_TEXTMODE
# define TEXTMODE_NEWLINE_DECORATOR_ON_WRITE ECONV_CRLF_NEWLINE_DECORATOR
@@ -437,24 +596,31 @@ static rb_io_t *flush_before_seek(rb_io_t *fptr);
* conversion IO process and universal newline decorator by default.
*/
#define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || (fptr)->encs.ecflags & ~ECONV_CRLF_NEWLINE_DECORATOR)
-#define NEED_WRITECONV(fptr) (((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || ((fptr)->encs.ecflags & ((ECONV_DECORATOR_MASK & ~ECONV_CRLF_NEWLINE_DECORATOR)|ECONV_STATEFUL_DECORATOR_MASK)))
+#define WRITECONV_MASK ( \
+ (ECONV_DECORATOR_MASK & ~ECONV_CRLF_NEWLINE_DECORATOR)|\
+ ECONV_STATEFUL_DECORATOR_MASK|\
+ 0)
+#define NEED_WRITECONV(fptr) ( \
+ ((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || \
+ ((fptr)->encs.ecflags & WRITECONV_MASK) || \
+ 0)
#define SET_BINARY_MODE(fptr) setmode((fptr)->fd, O_BINARY)
#define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) do {\
if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {\
- if (((fptr)->mode & FMODE_READABLE) &&\
- !((fptr)->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {\
- setmode((fptr)->fd, O_BINARY);\
- }\
- else {\
- setmode((fptr)->fd, O_TEXT);\
- }\
+ if (((fptr)->mode & FMODE_READABLE) &&\
+ !((fptr)->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {\
+ setmode((fptr)->fd, O_BINARY);\
+ }\
+ else {\
+ setmode((fptr)->fd, O_TEXT);\
+ }\
}\
} while(0)
#define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) do {\
if ((enc2) && ((ecflags) & ECONV_DEFAULT_NEWLINE_DECORATOR)) {\
- (ecflags) |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;\
+ (ecflags) |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;\
}\
} while(0)
@@ -462,9 +628,9 @@ static rb_io_t *flush_before_seek(rb_io_t *fptr);
* IO unread with taking care of removed '\r' in text mode.
*/
static void
-io_unread(rb_io_t *fptr)
+io_unread(rb_io_t *fptr, bool discard_rbuf)
{
- off_t r, pos;
+ rb_off_t r, pos;
ssize_t read_size;
long i;
long newlines = 0;
@@ -474,28 +640,26 @@ io_unread(rb_io_t *fptr)
rb_io_check_closed(fptr);
if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX) {
- return;
+ return;
}
errno = 0;
if (!rb_w32_fd_is_text(fptr->fd)) {
- r = lseek(fptr->fd, -fptr->rbuf.len, SEEK_CUR);
- if (r < 0 && errno) {
- if (errno == ESPIPE)
- fptr->mode |= FMODE_DUPLEX;
- return;
- }
+ r = lseek(fptr->fd, -fptr->rbuf.len, SEEK_CUR);
+ if (r < 0 && errno) {
+ if (errno == ESPIPE)
+ fptr->mode |= FMODE_DUPLEX;
+ if (!discard_rbuf) return;
+ }
- fptr->rbuf.off = 0;
- fptr->rbuf.len = 0;
- return;
+ goto end;
}
pos = lseek(fptr->fd, 0, SEEK_CUR);
if (pos < 0 && errno) {
- if (errno == ESPIPE)
- fptr->mode |= FMODE_DUPLEX;
- return;
+ if (errno == ESPIPE)
+ fptr->mode |= FMODE_DUPLEX;
+ if (!discard_rbuf) goto end;
}
/* add extra offset for removed '\r' in rbuf */
@@ -504,39 +668,42 @@ io_unread(rb_io_t *fptr)
/* if the end of rbuf is '\r', rbuf doesn't have '\r' within rbuf.len */
if (*(fptr->rbuf.ptr + fptr->rbuf.capa - 1) == '\r') {
- newlines++;
+ newlines++;
}
for (i = 0; i < fptr->rbuf.len; i++) {
- if (*p == '\n') newlines++;
- if (extra_max == newlines) break;
- p++;
+ if (*p == '\n') newlines++;
+ if (extra_max == newlines) break;
+ p++;
}
buf = ALLOC_N(char, fptr->rbuf.len + newlines);
while (newlines >= 0) {
- r = lseek(fptr->fd, pos - fptr->rbuf.len - newlines, SEEK_SET);
- if (newlines == 0) break;
- if (r < 0) {
- newlines--;
- continue;
- }
- read_size = _read(fptr->fd, buf, fptr->rbuf.len + newlines);
- if (read_size < 0) {
- free(buf);
- rb_sys_fail_path(fptr->pathv);
- }
- if (read_size == fptr->rbuf.len) {
- lseek(fptr->fd, r, SEEK_SET);
- break;
- }
- else {
- newlines--;
- }
+ r = lseek(fptr->fd, pos - fptr->rbuf.len - newlines, SEEK_SET);
+ if (newlines == 0) break;
+ if (r < 0) {
+ newlines--;
+ continue;
+ }
+ read_size = _read(fptr->fd, buf, fptr->rbuf.len + newlines);
+ if (read_size < 0) {
+ int e = errno;
+ free(buf);
+ rb_syserr_fail_path(e, fptr->pathv);
+ }
+ if (read_size == fptr->rbuf.len) {
+ lseek(fptr->fd, r, SEEK_SET);
+ break;
+ }
+ else {
+ newlines--;
+ }
}
free(buf);
+ end:
fptr->rbuf.off = 0;
fptr->rbuf.len = 0;
+ clear_codeconv(fptr);
return;
}
@@ -553,9 +720,9 @@ set_binary_mode_with_seek_cur(rb_io_t *fptr)
if (!rb_w32_fd_is_text(fptr->fd)) return O_BINARY;
if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX) {
- return setmode(fptr->fd, O_BINARY);
+ return setmode(fptr->fd, O_BINARY);
}
- flush_before_seek(fptr);
+ flush_before_seek(fptr, false);
return setmode(fptr->fd, O_BINARY);
}
#define SET_BINARY_MODE_WITH_SEEK_CUR(fptr) set_binary_mode_with_seek_cur(fptr)
@@ -564,7 +731,11 @@ set_binary_mode_with_seek_cur(rb_io_t *fptr)
/* Unix */
# define DEFAULT_TEXTMODE 0
#define NEED_READCONV(fptr) ((fptr)->encs.enc2 != NULL || NEED_NEWLINE_DECORATOR_ON_READ(fptr))
-#define NEED_WRITECONV(fptr) (((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) || ((fptr)->encs.ecflags & (ECONV_DECORATOR_MASK|ECONV_STATEFUL_DECORATOR_MASK)))
+#define NEED_WRITECONV(fptr) ( \
+ ((fptr)->encs.enc != NULL && (fptr)->encs.enc != rb_ascii8bit_encoding()) || \
+ NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) || \
+ ((fptr)->encs.ecflags & (ECONV_DECORATOR_MASK|ECONV_STATEFUL_DECORATOR_MASK)) || \
+ 0)
#define SET_BINARY_MODE(fptr) (void)(fptr)
#define NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr) (void)(fptr)
#define SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags) ((void)(enc2), (void)(ecflags))
@@ -590,6 +761,17 @@ is_socket(int fd, VALUE path)
}
#endif
+static const char closed_stream[] = "closed stream";
+
+static void
+io_fd_check_closed(int fd)
+{
+ if (fd < 0) {
+ rb_thread_check_ints(); /* check for ruby_error_stream_closed */
+ rb_raise(rb_eIOError, closed_stream);
+ }
+}
+
void
rb_eof_error(void)
{
@@ -607,7 +789,7 @@ void
rb_io_check_initialized(rb_io_t *fptr)
{
if (!fptr) {
- rb_raise(rb_eIOError, "uninitialized stream");
+ rb_raise(rb_eIOError, "uninitialized stream");
}
}
@@ -615,30 +797,34 @@ void
rb_io_check_closed(rb_io_t *fptr)
{
rb_io_check_initialized(fptr);
- if (fptr->fd < 0) {
- rb_raise(rb_eIOError, "closed stream");
- }
+ io_fd_check_closed(fptr->fd);
}
+static rb_io_t *
+rb_io_get_fptr(VALUE io)
+{
+ rb_io_t *fptr = RFILE(io)->fptr;
+ rb_io_check_initialized(fptr);
+ return fptr;
+}
VALUE
rb_io_get_io(VALUE io)
{
- return rb_convert_type(io, T_FILE, "IO", "to_io");
+ return rb_convert_type_with_id(io, T_FILE, "IO", idTo_io);
}
VALUE
rb_io_check_io(VALUE io)
{
- return rb_check_convert_type(io, T_FILE, "IO", "to_io");
+ return rb_check_convert_type_with_id(io, T_FILE, "IO", idTo_io);
}
VALUE
rb_io_get_write_io(VALUE io)
{
VALUE write_io;
- rb_io_check_initialized(RFILE(io)->fptr);
- write_io = RFILE(io)->fptr->tied_io_for_writing;
+ write_io = rb_io_get_fptr(io)->tied_io_for_writing;
if (write_io) {
return write_io;
}
@@ -649,33 +835,79 @@ VALUE
rb_io_set_write_io(VALUE io, VALUE w)
{
VALUE write_io;
- rb_io_check_initialized(RFILE(io)->fptr);
+ rb_io_t *fptr = rb_io_get_fptr(io);
if (!RTEST(w)) {
- w = 0;
+ w = 0;
}
else {
- GetWriteIO(w);
+ GetWriteIO(w);
}
- write_io = RFILE(io)->fptr->tied_io_for_writing;
- RFILE(io)->fptr->tied_io_for_writing = w;
+ write_io = fptr->tied_io_for_writing;
+ fptr->tied_io_for_writing = w;
return write_io ? write_io : Qnil;
}
/*
* call-seq:
- * IO.try_convert(obj) -> io or nil
+ * timeout -> duration or nil
+ *
+ * Get the internal timeout duration or nil if it was not set.
+ *
+ */
+VALUE
+rb_io_timeout(VALUE self)
+{
+ rb_io_t *fptr = rb_io_get_fptr(self);
+
+ return fptr->timeout;
+}
+
+/*
+ * call-seq:
+ * timeout = duration -> duration
+ * timeout = nil -> nil
+ *
+ * Sets the internal timeout to the specified duration or nil. The timeout
+ * applies to all blocking operations where possible.
+ *
+ * When the operation performs longer than the timeout set, IO::TimeoutError
+ * is raised.
*
- * Try to convert <i>obj</i> into an IO, using to_io method.
- * Returns converted IO or nil if <i>obj</i> cannot be converted
- * for any reason.
+ * This affects the following methods (but is not limited to): #gets, #puts,
+ * #read, #write, #wait_readable and #wait_writable. This also affects
+ * blocking socket operations like Socket#accept and Socket#connect.
+ *
+ * Some operations like File#open and IO#close are not affected by the
+ * timeout. A timeout during a write operation may leave the IO in an
+ * inconsistent state, e.g. data was partially written. Generally speaking, a
+ * timeout is a last ditch effort to prevent an application from hanging on
+ * slow I/O operations, such as those that occur during a slowloris attack.
+ */
+VALUE
+rb_io_set_timeout(VALUE self, VALUE timeout)
+{
+ // Validate it:
+ if (RTEST(timeout)) {
+ rb_time_interval(timeout);
+ }
+
+ rb_io_t *fptr = rb_io_get_fptr(self);
+
+ fptr->timeout = timeout;
+
+ return self;
+}
+
+/*
+ * call-seq:
+ * IO.try_convert(object) -> new_io or nil
*
- * IO.try_convert(STDOUT) #=> STDOUT
- * IO.try_convert("STDOUT") #=> nil
+ * Attempts to convert +object+ into an \IO object via method +to_io+;
+ * returns the new \IO object if successful, or +nil+ otherwise:
*
- * require 'zlib'
- * f = open("/tmp/zz.gz") #=> #<File:/tmp/zz.gz>
- * z = Zlib::GzipReader.open(f) #=> #<Zlib::GzipReader:0x81d8744>
- * IO.try_convert(z) #=> #<File:/tmp/zz.gz>
+ * IO.try_convert(STDOUT) # => #<IO:<STDOUT>>
+ * IO.try_convert(ARGF) # => #<IO:<STDIN>>
+ * IO.try_convert('STDOUT') # => nil
*
*/
static VALUE
@@ -684,11 +916,11 @@ rb_io_s_try_convert(VALUE dummy, VALUE io)
return rb_io_check_io(io);
}
-#if !(defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32))
+#if !RUBY_CRLF_ENVIRONMENT
static void
-io_unread(rb_io_t *fptr)
+io_unread(rb_io_t *fptr, bool discard_rbuf)
{
- off_t r;
+ rb_off_t r;
rb_io_check_closed(fptr);
if (fptr->rbuf.len == 0 || fptr->mode & FMODE_DUPLEX)
return;
@@ -698,10 +930,11 @@ io_unread(rb_io_t *fptr)
if (r < 0 && errno) {
if (errno == ESPIPE)
fptr->mode |= FMODE_DUPLEX;
- return;
+ if (!discard_rbuf) return;
}
fptr->rbuf.off = 0;
fptr->rbuf.len = 0;
+ clear_codeconv(fptr);
return;
}
#endif
@@ -718,17 +951,17 @@ io_ungetbyte(VALUE str, rb_io_t *fptr)
fptr->rbuf.off = 0;
fptr->rbuf.len = 0;
#if SIZEOF_LONG > SIZEOF_INT
- if (len > INT_MAX)
- rb_raise(rb_eIOError, "ungetbyte failed");
+ if (len > INT_MAX)
+ rb_raise(rb_eIOError, "ungetbyte failed");
#endif
- if (len > min_capa)
- fptr->rbuf.capa = (int)len;
- else
- fptr->rbuf.capa = min_capa;
+ if (len > min_capa)
+ fptr->rbuf.capa = (int)len;
+ else
+ fptr->rbuf.capa = min_capa;
fptr->rbuf.ptr = ALLOC_N(char, fptr->rbuf.capa);
}
if (fptr->rbuf.capa < len + fptr->rbuf.len) {
- rb_raise(rb_eIOError, "ungetbyte failed");
+ rb_raise(rb_eIOError, "ungetbyte failed");
}
if (fptr->rbuf.off < len) {
MEMMOVE(fptr->rbuf.ptr+fptr->rbuf.capa-fptr->rbuf.len,
@@ -742,17 +975,17 @@ io_ungetbyte(VALUE str, rb_io_t *fptr)
}
static rb_io_t *
-flush_before_seek(rb_io_t *fptr)
+flush_before_seek(rb_io_t *fptr, bool discard_rbuf)
{
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
- io_unread(fptr);
+ rb_sys_fail_on_write(fptr);
+ io_unread(fptr, discard_rbuf);
errno = 0;
return fptr;
}
-#define io_seek(fptr, ofs, whence) (errno = 0, lseek(flush_before_seek(fptr)->fd, (ofs), (whence)))
-#define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR)
+#define io_seek(fptr, ofs, whence) (errno = 0, lseek(flush_before_seek(fptr, true)->fd, (ofs), (whence)))
+#define io_tell(fptr) lseek(flush_before_seek(fptr, false)->fd, 0, SEEK_CUR)
#ifndef SEEK_CUR
# define SEEK_SET 0
@@ -765,17 +998,17 @@ rb_io_check_char_readable(rb_io_t *fptr)
{
rb_io_check_closed(fptr);
if (!(fptr->mode & FMODE_READABLE)) {
- rb_raise(rb_eIOError, "not opened for reading");
+ rb_raise(rb_eIOError, "not opened for reading");
}
if (fptr->wbuf.len) {
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
+ rb_sys_fail_on_write(fptr);
}
if (fptr->tied_io_for_writing) {
- rb_io_t *wfptr;
- GetOpenFile(fptr->tied_io_for_writing, wfptr);
+ rb_io_t *wfptr;
+ GetOpenFile(fptr->tied_io_for_writing, wfptr);
if (io_fflush(wfptr) < 0)
- rb_sys_fail(0);
+ rb_sys_fail_on_write(wfptr);
}
}
@@ -784,7 +1017,7 @@ rb_io_check_byte_readable(rb_io_t *fptr)
{
rb_io_check_char_readable(fptr);
if (READ_CHAR_PENDING(fptr)) {
- rb_raise(rb_eIOError, "byte oriented read for character buffered IO");
+ rb_raise(rb_eIOError, "byte oriented read for character buffered IO");
}
}
@@ -798,7 +1031,7 @@ static rb_encoding*
io_read_encoding(rb_io_t *fptr)
{
if (fptr->encs.enc) {
- return fptr->encs.enc;
+ return fptr->encs.enc;
}
return rb_default_external_encoding();
}
@@ -807,7 +1040,7 @@ static rb_encoding*
io_input_encoding(rb_io_t *fptr)
{
if (fptr->encs.enc2) {
- return fptr->encs.enc2;
+ return fptr->encs.enc2;
}
return io_read_encoding(fptr);
}
@@ -817,10 +1050,10 @@ rb_io_check_writable(rb_io_t *fptr)
{
rb_io_check_closed(fptr);
if (!(fptr->mode & FMODE_WRITABLE)) {
- rb_raise(rb_eIOError, "not opened for writing");
+ rb_raise(rb_eIOError, "not opened for writing");
}
if (fptr->rbuf.len) {
- io_unread(fptr);
+ io_unread(fptr, true);
}
}
@@ -834,36 +1067,42 @@ rb_io_read_pending(rb_io_t *fptr)
}
void
-rb_read_check(FILE *fp)
+rb_io_read_check(rb_io_t *fptr)
{
- if (!STDIO_READ_DATA_PENDING(fp)) {
- rb_thread_wait_fd(fileno(fp));
+ if (!READ_DATA_PENDING(fptr)) {
+ rb_io_wait(fptr->self, RB_INT2NUM(RUBY_IO_READABLE), RUBY_IO_TIMEOUT_DEFAULT);
}
+ return;
}
-void
-rb_io_read_check(rb_io_t *fptr)
+int
+rb_gc_for_fd(int err)
{
- if (!READ_DATA_PENDING(fptr)) {
- rb_thread_wait_fd(fptr->fd);
+ if (err == EMFILE || err == ENFILE || err == ENOMEM) {
+ rb_gc();
+ return 1;
}
- return;
+ return 0;
}
+/* try `expr` upto twice while it returns false and `errno`
+ * is to GC. Each `errno`s are available as `first_errno` and
+ * `retried_errno` respectively */
+#define TRY_WITH_GC(expr) \
+ for (int first_errno, retried_errno = 0, retried = 0; \
+ (!retried && \
+ !(expr) && \
+ (!rb_gc_for_fd(first_errno = errno) || !(expr)) && \
+ (retried_errno = errno, 1)); \
+ (void)retried_errno, retried = 1)
+
static int
ruby_dup(int orig)
{
- int fd;
+ int fd = -1;
- fd = rb_cloexec_dup(orig);
- if (fd < 0) {
- if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
- rb_gc();
- fd = rb_cloexec_dup(orig);
- }
- if (fd < 0) {
- rb_sys_fail(0);
- }
+ TRY_WITH_GC((fd = rb_cloexec_dup(orig)) >= 0) {
+ rb_syserr_fail(first_errno, 0);
}
rb_update_max_fd(fd);
return fd;
@@ -872,7 +1111,7 @@ ruby_dup(int orig)
static VALUE
io_alloc(VALUE klass)
{
- NEWOBJ_OF(io, struct RFile, klass, T_FILE);
+ UNPROTECTED_NEWOBJ_OF(io, struct RFile, klass, T_FILE, sizeof(struct RFile));
io->fptr = 0;
@@ -883,176 +1122,342 @@ io_alloc(VALUE klass)
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif
-static int
-wsplit_p(rb_io_t *fptr)
-{
-#if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
- int r;
-#endif
-
- if (!(fptr->mode & FMODE_WSPLIT_INITIALIZED)) {
- struct stat buf;
- if (fstat(fptr->fd, &buf) == 0 &&
- !S_ISREG(buf.st_mode)
-#if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(O_NONBLOCK)
- && (r = fcntl(fptr->fd, F_GETFL)) != -1 &&
- !(r & O_NONBLOCK)
-#endif
- ) {
- fptr->mode |= FMODE_WSPLIT;
- }
- fptr->mode |= FMODE_WSPLIT_INITIALIZED;
- }
- return fptr->mode & FMODE_WSPLIT;
-}
-
struct io_internal_read_struct {
+ VALUE th;
+ rb_io_t *fptr;
+ int nonblock;
int fd;
+
void *buf;
size_t capa;
+ struct timeval *timeout;
};
struct io_internal_write_struct {
+ VALUE th;
+ rb_io_t *fptr;
+ int nonblock;
int fd;
+
const void *buf;
size_t capa;
+ struct timeval *timeout;
+};
+
+#ifdef HAVE_WRITEV
+struct io_internal_writev_struct {
+ VALUE th;
+ rb_io_t *fptr;
+ int nonblock;
+ int fd;
+
+ int iovcnt;
+ const struct iovec *iov;
+ struct timeval *timeout;
};
+#endif
+
+static int nogvl_wait_for(VALUE th, rb_io_t *fptr, short events, struct timeval *timeout);
+
+/**
+ * Wait for the given events on the given file descriptor.
+ * Returns -1 if an error or timeout occurred. +errno+ will be set.
+ * Returns the event mask if an event occurred.
+ */
+static inline int
+io_internal_wait(VALUE thread, rb_io_t *fptr, int error, int events, struct timeval *timeout)
+{
+ if (!timeout && rb_thread_mn_schedulable(thread)) {
+ RUBY_ASSERT(errno == EWOULDBLOCK || errno == EAGAIN);
+ return -1;
+ }
+
+ int ready = nogvl_wait_for(thread, fptr, events, timeout);
+
+ if (ready > 0) {
+ return ready;
+ }
+ else if (ready == 0) {
+ errno = ETIMEDOUT;
+ return -1;
+ }
+
+ // If there was an error BEFORE we started waiting, return it:
+ if (error) {
+ errno = error;
+ return -1;
+ }
+ else {
+ // Otherwise, whatever error was generated by `nogvl_wait_for` is the one we want:
+ return ready;
+ }
+}
static VALUE
internal_read_func(void *ptr)
{
struct io_internal_read_struct *iis = ptr;
- return read(iis->fd, iis->buf, iis->capa);
+ ssize_t result;
+
+ if (iis->timeout && !iis->nonblock) {
+ if (io_internal_wait(iis->th, iis->fptr, 0, RB_WAITFD_IN, iis->timeout) == -1) {
+ return -1;
+ }
+ }
+
+ retry:
+ result = read(iis->fd, iis->buf, iis->capa);
+
+ if (result < 0 && !iis->nonblock) {
+ if (io_again_p(errno)) {
+ if (io_internal_wait(iis->th, iis->fptr, errno, RB_WAITFD_IN, iis->timeout) == -1) {
+ return -1;
+ }
+ else {
+ goto retry;
+ }
+ }
+ }
+
+ return result;
}
+#if defined __APPLE__
+# define do_write_retry(code) do {result = code;} while (result == -1 && errno == EPROTOTYPE)
+#else
+# define do_write_retry(code) result = code
+#endif
+
static VALUE
internal_write_func(void *ptr)
{
struct io_internal_write_struct *iis = ptr;
- return write(iis->fd, iis->buf, iis->capa);
+ ssize_t result;
+
+ if (iis->timeout && !iis->nonblock) {
+ if (io_internal_wait(iis->th, iis->fptr, 0, RB_WAITFD_OUT, iis->timeout) == -1) {
+ return -1;
+ }
+ }
+
+ retry:
+ do_write_retry(write(iis->fd, iis->buf, iis->capa));
+
+ if (result < 0 && !iis->nonblock) {
+ int e = errno;
+ if (io_again_p(e)) {
+ if (io_internal_wait(iis->th, iis->fptr, errno, RB_WAITFD_OUT, iis->timeout) == -1) {
+ return -1;
+ }
+ else {
+ goto retry;
+ }
+ }
+ }
+
+ return result;
}
-static void*
-internal_write_func2(void *ptr)
+#ifdef HAVE_WRITEV
+static VALUE
+internal_writev_func(void *ptr)
{
- struct io_internal_write_struct *iis = ptr;
- return (void*)(intptr_t)write(iis->fd, iis->buf, iis->capa);
+ struct io_internal_writev_struct *iis = ptr;
+ ssize_t result;
+
+ if (iis->timeout && !iis->nonblock) {
+ if (io_internal_wait(iis->th, iis->fptr, 0, RB_WAITFD_OUT, iis->timeout) == -1) {
+ return -1;
+ }
+ }
+
+ retry:
+ do_write_retry(writev(iis->fd, iis->iov, iis->iovcnt));
+
+ if (result < 0 && !iis->nonblock) {
+ if (io_again_p(errno)) {
+ if (io_internal_wait(iis->th, iis->fptr, errno, RB_WAITFD_OUT, iis->timeout) == -1) {
+ return -1;
+ }
+ else {
+ goto retry;
+ }
+ }
+ }
+
+ return result;
}
+#endif
static ssize_t
-rb_read_internal(int fd, void *buf, size_t count)
+rb_io_read_memory(rb_io_t *fptr, void *buf, size_t count)
{
- struct io_internal_read_struct iis;
- iis.fd = fd;
- iis.buf = buf;
- iis.capa = count;
+ rb_thread_t *th = GET_THREAD();
+ VALUE scheduler = rb_fiber_scheduler_current_for_threadptr(th);
+ if (scheduler != Qnil) {
+ VALUE result = rb_fiber_scheduler_io_read_memory(scheduler, fptr->self, buf, count, 0);
+
+ if (!UNDEF_P(result)) {
+ return rb_fiber_scheduler_io_result_apply(result);
+ }
+ }
+
+ struct io_internal_read_struct iis = {
+ .th = th->self,
+ .fptr = fptr,
+ .nonblock = 0,
+ .fd = fptr->fd,
+
+ .buf = buf,
+ .capa = count,
+ .timeout = NULL,
+ };
+
+ struct timeval timeout_storage;
+
+ if (fptr->timeout != Qnil) {
+ timeout_storage = rb_time_interval(fptr->timeout);
+ iis.timeout = &timeout_storage;
+ }
- return (ssize_t)rb_thread_io_blocking_region(internal_read_func, &iis, fd);
+ return (ssize_t)rb_io_blocking_region_wait(fptr, internal_read_func, &iis, RUBY_IO_READABLE);
}
static ssize_t
-rb_write_internal(int fd, const void *buf, size_t count)
+rb_io_write_memory(rb_io_t *fptr, const void *buf, size_t count)
{
- struct io_internal_write_struct iis;
- iis.fd = fd;
- iis.buf = buf;
- iis.capa = count;
+ rb_thread_t *th = GET_THREAD();
+ VALUE scheduler = rb_fiber_scheduler_current_for_threadptr(th);
+ if (scheduler != Qnil) {
+ VALUE result = rb_fiber_scheduler_io_write_memory(scheduler, fptr->self, buf, count, 0);
- return (ssize_t)rb_thread_io_blocking_region(internal_write_func, &iis, fd);
+ if (!UNDEF_P(result)) {
+ return rb_fiber_scheduler_io_result_apply(result);
+ }
+ }
+
+ struct io_internal_write_struct iis = {
+ .th = th->self,
+ .fptr = fptr,
+ .nonblock = 0,
+ .fd = fptr->fd,
+
+ .buf = buf,
+ .capa = count,
+ .timeout = NULL
+ };
+
+ struct timeval timeout_storage;
+
+ if (fptr->timeout != Qnil) {
+ timeout_storage = rb_time_interval(fptr->timeout);
+ iis.timeout = &timeout_storage;
+ }
+
+ return (ssize_t)rb_io_blocking_region_wait(fptr, internal_write_func, &iis, RUBY_IO_WRITABLE);
}
+#ifdef HAVE_WRITEV
static ssize_t
-rb_write_internal2(int fd, const void *buf, size_t count)
+rb_writev_internal(rb_io_t *fptr, const struct iovec *iov, int iovcnt)
{
- struct io_internal_write_struct iis;
- iis.fd = fd;
- iis.buf = buf;
- iis.capa = count;
+ if (!iovcnt) return 0;
- return (ssize_t)rb_thread_call_without_gvl2(internal_write_func2, &iis,
- RUBY_UBF_IO, NULL);
-}
+ rb_thread_t *th = GET_THREAD();
-static long
-io_writable_length(rb_io_t *fptr, long l)
-{
- if (PIPE_BUF < l &&
- !rb_thread_alone() &&
- wsplit_p(fptr)) {
- l = PIPE_BUF;
+ VALUE scheduler = rb_fiber_scheduler_current_for_threadptr(th);
+ if (scheduler != Qnil) {
+ // This path assumes at least one `iov`:
+ VALUE result = rb_fiber_scheduler_io_write_memory(scheduler, fptr->self, iov[0].iov_base, iov[0].iov_len, 0);
+
+ if (!UNDEF_P(result)) {
+ return rb_fiber_scheduler_io_result_apply(result);
+ }
+ }
+
+ struct io_internal_writev_struct iis = {
+ .th = th->self,
+ .fptr = fptr,
+ .nonblock = 0,
+ .fd = fptr->fd,
+
+ .iov = iov,
+ .iovcnt = iovcnt,
+ .timeout = NULL
+ };
+
+ struct timeval timeout_storage;
+
+ if (fptr->timeout != Qnil) {
+ timeout_storage = rb_time_interval(fptr->timeout);
+ iis.timeout = &timeout_storage;
}
- return l;
+
+ return (ssize_t)rb_io_blocking_region_wait(fptr, internal_writev_func, &iis, RUBY_IO_WRITABLE);
}
+#endif
static VALUE
io_flush_buffer_sync(void *arg)
{
rb_io_t *fptr = arg;
- long l = io_writable_length(fptr, fptr->wbuf.len);
+ long l = fptr->wbuf.len;
ssize_t r = write(fptr->fd, fptr->wbuf.ptr+fptr->wbuf.off, (size_t)l);
if (fptr->wbuf.len <= r) {
- fptr->wbuf.off = 0;
- fptr->wbuf.len = 0;
- return 0;
+ fptr->wbuf.off = 0;
+ fptr->wbuf.len = 0;
+ return 0;
}
+
if (0 <= r) {
- fptr->wbuf.off += (int)r;
- fptr->wbuf.len -= (int)r;
- errno = EAGAIN;
+ fptr->wbuf.off += (int)r;
+ fptr->wbuf.len -= (int)r;
+ errno = EAGAIN;
}
+
return (VALUE)-1;
}
-static void*
-io_flush_buffer_sync2(void *arg)
+static inline VALUE
+io_flush_buffer_fiber_scheduler(VALUE scheduler, rb_io_t *fptr)
{
- VALUE result = io_flush_buffer_sync(arg);
-
- /*
- * rb_thread_call_without_gvl2 uses 0 as interrupted.
- * So, we need to avoid to use 0.
- */
- return !result ? (void*)1 : (void*)result;
+ VALUE ret = rb_fiber_scheduler_io_write_memory(scheduler, fptr->self, fptr->wbuf.ptr+fptr->wbuf.off, fptr->wbuf.len, 0);
+ if (!UNDEF_P(ret)) {
+ ssize_t result = rb_fiber_scheduler_io_result_apply(ret);
+ if (result > 0) {
+ fptr->wbuf.off += result;
+ fptr->wbuf.len -= result;
+ }
+ return result >= 0 ? (VALUE)0 : (VALUE)-1;
+ }
+ return ret;
}
static VALUE
io_flush_buffer_async(VALUE arg)
{
rb_io_t *fptr = (rb_io_t *)arg;
- return rb_thread_io_blocking_region(io_flush_buffer_sync, fptr, fptr->fd);
-}
-
-static VALUE
-io_flush_buffer_async2(VALUE arg)
-{
- rb_io_t *fptr = (rb_io_t *)arg;
- VALUE ret;
- ret = (VALUE)rb_thread_call_without_gvl2(io_flush_buffer_sync2, fptr,
- RUBY_UBF_IO, NULL);
+ VALUE scheduler = rb_fiber_scheduler_current();
+ if (scheduler != Qnil) {
+ VALUE result = io_flush_buffer_fiber_scheduler(scheduler, fptr);
+ if (!UNDEF_P(result)) {
+ return result;
+ }
+ }
- if (!ret) {
- /* pending async interrupt is there. */
- errno = EAGAIN;
- return -1;
- } else if (ret == 1) {
- return 0;
- } else
- return ret;
+ return rb_io_blocking_region_wait(fptr, io_flush_buffer_sync, fptr, RUBY_IO_WRITABLE);
}
static inline int
io_flush_buffer(rb_io_t *fptr)
{
- if (fptr->write_lock) {
- if (rb_mutex_owned_p(fptr->write_lock))
- return (int)io_flush_buffer_async2((VALUE)fptr);
- else
- return (int)rb_mutex_synchronize(fptr->write_lock, io_flush_buffer_async2, (VALUE)fptr);
+ if (!NIL_P(fptr->write_lock) && rb_mutex_owned_p(fptr->write_lock)) {
+ return (int)io_flush_buffer_async((VALUE)fptr);
}
else {
- return (int)io_flush_buffer_async((VALUE)fptr);
+ return (int)rb_mutex_synchronize(fptr->write_lock, io_flush_buffer_async, (VALUE)fptr);
}
}
@@ -1060,78 +1465,250 @@ static int
io_fflush(rb_io_t *fptr)
{
rb_io_check_closed(fptr);
+
if (fptr->wbuf.len == 0)
return 0;
- rb_io_check_closed(fptr);
+
while (fptr->wbuf.len > 0 && io_flush_buffer(fptr) != 0) {
- if (!rb_io_wait_writable(fptr->fd))
- return -1;
+ if (!rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT))
+ return -1;
+
rb_io_check_closed(fptr);
}
+
return 0;
}
+VALUE
+rb_io_wait(VALUE io, VALUE events, VALUE timeout)
+{
+ rb_thread_t *th = GET_THREAD();
+ VALUE scheduler = rb_fiber_scheduler_current_for_threadptr(th);
+
+ if (scheduler != Qnil) {
+ return rb_fiber_scheduler_io_wait(scheduler, io, events, timeout);
+ }
+
+ rb_io_t * fptr = NULL;
+ RB_IO_POINTER(io, fptr);
+
+ struct timeval tv_storage;
+ struct timeval *tv = NULL;
+
+ if (NIL_OR_UNDEF_P(timeout)) {
+ timeout = fptr->timeout;
+ }
+
+ if (timeout != Qnil) {
+ tv_storage = rb_time_interval(timeout);
+ tv = &tv_storage;
+ }
+
+ int ready = rb_thread_io_wait(th, fptr, RB_NUM2INT(events), tv);
+
+ if (ready < 0) {
+ rb_sys_fail(0);
+ }
+
+ // Not sure if this is necessary:
+ rb_io_check_closed(fptr);
+
+ if (ready) {
+ return RB_INT2NUM(ready);
+ }
+ else {
+ return Qfalse;
+ }
+}
+
+static VALUE
+io_from_fd(int fd)
+{
+ return prep_io(fd, FMODE_EXTERNAL, rb_cIO, NULL);
+}
+
+static int
+io_wait_for_single_fd(int fd, int events, struct timeval *timeout, rb_thread_t *th, VALUE scheduler)
+{
+ if (scheduler != Qnil) {
+ return RTEST(
+ rb_fiber_scheduler_io_wait(scheduler, io_from_fd(fd), RB_INT2NUM(events), rb_fiber_scheduler_make_timeout(timeout))
+ );
+ }
+
+ return rb_thread_wait_for_single_fd(th, fd, events, timeout);
+}
+
int
rb_io_wait_readable(int f)
{
- if (f < 0) {
- rb_raise(rb_eIOError, "closed stream");
- }
+ io_fd_check_closed(f);
+
+ rb_thread_t *th = GET_THREAD();
+ VALUE scheduler = rb_fiber_scheduler_current_for_threadptr(th);
+
switch (errno) {
case EINTR:
#if defined(ERESTART)
case ERESTART:
#endif
- rb_thread_check_ints();
- return TRUE;
+ rb_thread_check_ints();
+ return TRUE;
case EAGAIN:
-#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+#if EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
- rb_thread_wait_fd(f);
- return TRUE;
+ if (scheduler != Qnil) {
+ return RTEST(
+ rb_fiber_scheduler_io_wait_readable(scheduler, io_from_fd(f))
+ );
+ }
+ else {
+ io_wait_for_single_fd(f, RUBY_IO_READABLE, NULL, th, scheduler);
+ }
+ return TRUE;
default:
- return FALSE;
+ return FALSE;
}
}
int
rb_io_wait_writable(int f)
{
- if (f < 0) {
- rb_raise(rb_eIOError, "closed stream");
- }
+ io_fd_check_closed(f);
+
+ rb_thread_t *th = GET_THREAD();
+ VALUE scheduler = rb_fiber_scheduler_current_for_threadptr(th);
+
switch (errno) {
case EINTR:
#if defined(ERESTART)
case ERESTART:
#endif
- /*
- * In old Linux, several special files under /proc and /sys don't handle
- * select properly. Thus we need avoid to call if don't use O_NONBLOCK.
- * Otherwise, we face nasty hang up. Sigh.
- * e.g. http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=31b07093c44a7a442394d44423e21d783f5523b8
- * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=31b07093c44a7a442394d44423e21d783f5523b8
- * In EINTR case, we only need to call RUBY_VM_CHECK_INTS_BLOCKING().
- * Then rb_thread_check_ints() is enough.
- */
- rb_thread_check_ints();
- return TRUE;
+ /*
+ * In old Linux, several special files under /proc and /sys don't handle
+ * select properly. Thus we need avoid to call if don't use O_NONBLOCK.
+ * Otherwise, we face nasty hang up. Sigh.
+ * e.g. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=31b07093c44a7a442394d44423e21d783f5523b8
+ * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=31b07093c44a7a442394d44423e21d783f5523b8
+ * In EINTR case, we only need to call RUBY_VM_CHECK_INTS_BLOCKING().
+ * Then rb_thread_check_ints() is enough.
+ */
+ rb_thread_check_ints();
+ return TRUE;
case EAGAIN:
-#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
+#if EWOULDBLOCK != EAGAIN
case EWOULDBLOCK:
#endif
- rb_thread_fd_writable(f);
- return TRUE;
+ if (scheduler != Qnil) {
+ return RTEST(
+ rb_fiber_scheduler_io_wait_writable(scheduler, io_from_fd(f))
+ );
+ }
+ else {
+ io_wait_for_single_fd(f, RUBY_IO_WRITABLE, NULL, th, scheduler);
+ }
+ return TRUE;
default:
- return FALSE;
+ return FALSE;
}
}
+int
+rb_wait_for_single_fd(int fd, int events, struct timeval *timeout)
+{
+ rb_thread_t *th = GET_THREAD();
+ VALUE scheduler = rb_fiber_scheduler_current_for_threadptr(th);
+ return io_wait_for_single_fd(fd, events, timeout, th, scheduler);
+}
+
+int
+rb_thread_wait_fd(int fd)
+{
+ return rb_wait_for_single_fd(fd, RUBY_IO_READABLE, NULL);
+}
+
+int
+rb_thread_fd_writable(int fd)
+{
+ return rb_wait_for_single_fd(fd, RUBY_IO_WRITABLE, NULL);
+}
+
+VALUE
+rb_io_maybe_wait(int error, VALUE io, VALUE events, VALUE timeout)
+{
+ // fptr->fd can be set to -1 at any time by another thread when the GVL is
+ // released. Many code, e.g. `io_bufread` didn't check this correctly and
+ // instead relies on `read(-1) -> -1` which causes this code path. We then
+ // check here whether the IO was in fact closed. Probably it's better to
+ // check that `fptr->fd != -1` before using it in syscall.
+ rb_io_check_closed(RFILE(io)->fptr);
+
+ switch (error) {
+ // In old Linux, several special files under /proc and /sys don't handle
+ // select properly. Thus we need avoid to call if don't use O_NONBLOCK.
+ // Otherwise, we face nasty hang up. Sigh.
+ // e.g. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=31b07093c44a7a442394d44423e21d783f5523b8
+ // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=31b07093c44a7a442394d44423e21d783f5523b8
+ // In EINTR case, we only need to call RUBY_VM_CHECK_INTS_BLOCKING().
+ // Then rb_thread_check_ints() is enough.
+ case EINTR:
+#if defined(ERESTART)
+ case ERESTART:
+#endif
+ // We might have pending interrupts since the previous syscall was interrupted:
+ rb_thread_check_ints();
+
+ // The operation was interrupted, so retry it immediately:
+ return events;
+
+ case EAGAIN:
+#if EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
+#endif
+ // The operation would block, so wait for the specified events:
+ return rb_io_wait(io, events, timeout);
+
+ default:
+ // Non-specific error, no event is ready:
+ return Qnil;
+ }
+}
+
+int
+rb_io_maybe_wait_readable(int error, VALUE io, VALUE timeout)
+{
+ VALUE result = rb_io_maybe_wait(error, io, RB_INT2NUM(RUBY_IO_READABLE), timeout);
+
+ if (RTEST(result)) {
+ return RB_NUM2INT(result);
+ }
+ else if (result == RUBY_Qfalse) {
+ rb_raise(rb_eIOTimeoutError, "Timed out waiting for IO to become readable!");
+ }
+
+ return 0;
+}
+
+int
+rb_io_maybe_wait_writable(int error, VALUE io, VALUE timeout)
+{
+ VALUE result = rb_io_maybe_wait(error, io, RB_INT2NUM(RUBY_IO_WRITABLE), timeout);
+
+ if (RTEST(result)) {
+ return RB_NUM2INT(result);
+ }
+ else if (result == RUBY_Qfalse) {
+ rb_raise(rb_eIOTimeoutError, "Timed out waiting for IO to become writable!");
+ }
+
+ return 0;
+}
+
static void
make_writeconv(rb_io_t *fptr)
{
@@ -1146,7 +1723,7 @@ make_writeconv(rb_io_t *fptr)
ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_READ_MASK;
ecopts = fptr->encs.ecopts;
- if (!fptr->encs.enc || (fptr->encs.enc == rb_ascii8bit_encoding() && !fptr->encs.enc2)) {
+ if (!fptr->encs.enc || (rb_is_ascii8bit_enc(fptr->encs.enc) && !fptr->encs.enc2)) {
/* no encoding conversion */
fptr->writeconv_pre_ecflags = 0;
fptr->writeconv_pre_ecopts = Qnil;
@@ -1190,7 +1767,6 @@ make_writeconv(rb_io_t *fptr)
/* writing functions */
struct binwrite_arg {
rb_io_t *fptr;
- VALUE str;
const char *ptr;
long length;
};
@@ -1201,101 +1777,195 @@ struct write_arg {
int nosync;
};
+#ifdef HAVE_WRITEV
+static ssize_t
+io_binwrite_string_internal(rb_io_t *fptr, const char *ptr, long length)
+{
+ if (fptr->wbuf.len) {
+ struct iovec iov[2];
+
+ iov[0].iov_base = fptr->wbuf.ptr+fptr->wbuf.off;
+ iov[0].iov_len = fptr->wbuf.len;
+ iov[1].iov_base = (void*)ptr;
+ iov[1].iov_len = length;
+
+ ssize_t result = rb_writev_internal(fptr, iov, 2);
+
+ if (result < 0)
+ return result;
+
+ if (result >= fptr->wbuf.len) {
+ // We wrote more than the internal buffer:
+ result -= fptr->wbuf.len;
+ fptr->wbuf.off = 0;
+ fptr->wbuf.len = 0;
+ }
+ else {
+ // We only wrote less data than the internal buffer:
+ fptr->wbuf.off += (int)result;
+ fptr->wbuf.len -= (int)result;
+
+ result = 0;
+ }
+
+ return result;
+ }
+ else {
+ return rb_io_write_memory(fptr, ptr, length);
+ }
+}
+#else
+static ssize_t
+io_binwrite_string_internal(rb_io_t *fptr, const char *ptr, long length)
+{
+ long remaining = length;
+
+ if (fptr->wbuf.len) {
+ if (fptr->wbuf.len+length <= fptr->wbuf.capa) {
+ if (fptr->wbuf.capa < fptr->wbuf.off+fptr->wbuf.len+length) {
+ MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
+ fptr->wbuf.off = 0;
+ }
+
+ MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, ptr, char, length);
+ fptr->wbuf.len += (int)length;
+
+ // We copied the entire incoming data to the internal buffer:
+ remaining = 0;
+ }
+
+ // Flush the internal buffer:
+ if (io_fflush(fptr) < 0) {
+ return -1;
+ }
+
+ // If all the data was buffered, we are done:
+ if (remaining == 0) {
+ return length;
+ }
+ }
+
+ // Otherwise, we should write the data directly:
+ return rb_io_write_memory(fptr, ptr, length);
+}
+#endif
+
static VALUE
io_binwrite_string(VALUE arg)
{
struct binwrite_arg *p = (struct binwrite_arg *)arg;
- long l = io_writable_length(p->fptr, p->length);
- return rb_write_internal2(p->fptr->fd, p->ptr, l);
-}
-static long
-io_binwrite(VALUE str, const char *ptr, long len, rb_io_t *fptr, int nosync)
-{
- long n, r, offset = 0;
+ const char *ptr = p->ptr;
+ size_t remaining = p->length;
- /* don't write anything if current thread has a pending interrupt. */
- rb_thread_check_ints();
+ while (remaining) {
+ // Write as much as possible:
+ ssize_t result = io_binwrite_string_internal(p->fptr, ptr, remaining);
+
+ if (result == 0) {
+ // If only the internal buffer is written, result will be zero [bytes of given data written]. This means we
+ // should try again immediately.
+ }
+ else if (result > 0) {
+ if ((size_t)result == remaining) break;
+ ptr += result;
+ remaining -= result;
+ }
+ // Wait for it to become writable:
+ else if (rb_io_maybe_wait_writable(errno, p->fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
+ rb_io_check_closed(p->fptr);
+ }
+ else {
+ // The error was unrelated to waiting for it to become writable, so we fail:
+ return -1;
+ }
+ }
+
+ return p->length;
+}
- if ((n = len) <= 0) return n;
- if (fptr->wbuf.ptr == NULL && !(!nosync && (fptr->mode & FMODE_SYNC))) {
+inline static void
+io_allocate_write_buffer(rb_io_t *fptr, int sync)
+{
+ if (fptr->wbuf.ptr == NULL && !(sync && (fptr->mode & FMODE_SYNC))) {
fptr->wbuf.off = 0;
fptr->wbuf.len = 0;
fptr->wbuf.capa = IO_WBUF_CAPA_MIN;
fptr->wbuf.ptr = ALLOC_N(char, fptr->wbuf.capa);
- fptr->write_lock = rb_mutex_new();
- rb_mutex_allow_trap(fptr->write_lock, 1);
- }
- if ((!nosync && (fptr->mode & (FMODE_SYNC|FMODE_TTY))) ||
- (fptr->wbuf.ptr && fptr->wbuf.capa <= fptr->wbuf.len + len)) {
- struct binwrite_arg arg;
-
- /*
- * xxx: use writev to avoid double write if available
- * writev may help avoid context switch between "a" and "\n" in
- * STDERR.puts "a" [ruby-dev:25080] (rebroken since native threads
- * introduced in 1.9)
- */
- if (fptr->wbuf.len && fptr->wbuf.len+len <= fptr->wbuf.capa) {
- if (fptr->wbuf.capa < fptr->wbuf.off+fptr->wbuf.len+len) {
- MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
- fptr->wbuf.off = 0;
- }
- MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, ptr+offset, char, len);
- fptr->wbuf.len += (int)len;
- n = 0;
- }
- if (io_fflush(fptr) < 0)
- return -1L;
- if (n == 0)
- return len;
+ }
- rb_io_check_closed(fptr);
- arg.fptr = fptr;
- arg.str = str;
- retry:
- arg.ptr = ptr + offset;
- arg.length = n;
- if (fptr->write_lock) {
- r = rb_mutex_synchronize(fptr->write_lock, io_binwrite_string, (VALUE)&arg);
- }
- else {
- long l = io_writable_length(fptr, n);
- r = rb_write_internal(fptr->fd, ptr+offset, l);
- }
- /* xxx: other threads may modify given string. */
- if (r == n) return len;
- if (0 <= r) {
- offset += r;
- n -= r;
- errno = EAGAIN;
- }
- if (rb_io_wait_writable(fptr->fd)) {
- rb_io_check_closed(fptr);
- if (offset < len)
- goto retry;
+ if (NIL_P(fptr->write_lock)) {
+ fptr->write_lock = rb_mutex_new();
+ rb_mutex_allow_trap(fptr->write_lock, 1);
+ }
+}
+
+static inline int
+io_binwrite_requires_flush_write(rb_io_t *fptr, long len, int nosync)
+{
+ // If the requested operation was synchronous and the output mode is synchronous or a TTY:
+ if (!nosync && (fptr->mode & (FMODE_SYNC|FMODE_TTY)))
+ return 1;
+
+ // If the amount of data we want to write exceeds the internal buffer:
+ if (fptr->wbuf.ptr && fptr->wbuf.capa <= fptr->wbuf.len + len)
+ return 1;
+
+ // Otherwise, we can append to the internal buffer:
+ return 0;
+}
+
+static long
+io_binwrite(const char *ptr, long len, rb_io_t *fptr, int nosync)
+{
+ if (len <= 0) return len;
+
+ // Don't write anything if current thread has a pending interrupt:
+ rb_thread_check_ints();
+
+ io_allocate_write_buffer(fptr, !nosync);
+
+ if (io_binwrite_requires_flush_write(fptr, len, nosync)) {
+ struct binwrite_arg arg;
+
+ arg.fptr = fptr;
+ arg.ptr = ptr;
+ arg.length = len;
+
+ if (!NIL_P(fptr->write_lock)) {
+ return rb_mutex_synchronize(fptr->write_lock, io_binwrite_string, (VALUE)&arg);
+ }
+ else {
+ return io_binwrite_string((VALUE)&arg);
}
- return -1L;
}
+ else {
+ if (fptr->wbuf.off) {
+ if (fptr->wbuf.len)
+ MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
+ fptr->wbuf.off = 0;
+ }
- if (fptr->wbuf.off) {
- if (fptr->wbuf.len)
- MEMMOVE(fptr->wbuf.ptr, fptr->wbuf.ptr+fptr->wbuf.off, char, fptr->wbuf.len);
- fptr->wbuf.off = 0;
+ MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, ptr, char, len);
+ fptr->wbuf.len += (int)len;
+
+ return len;
}
- MEMMOVE(fptr->wbuf.ptr+fptr->wbuf.off+fptr->wbuf.len, ptr+offset, char, len);
- fptr->wbuf.len += (int)len;
- return len;
}
# define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \
(fmode & FMODE_TEXTMODE) ? (c) : (a))
+
+#define MODE_BTXMODE(a, b, c, d, e, f) ((fmode & FMODE_EXCL) ? \
+ MODE_BTMODE(d, e, f) : \
+ MODE_BTMODE(a, b, c))
+
static VALUE
-do_writeconv(VALUE str, rb_io_t *fptr)
+do_writeconv(VALUE str, rb_io_t *fptr, int *converted)
{
if (NEED_WRITECONV(fptr)) {
VALUE common_encoding = Qnil;
- SET_BINARY_MODE(fptr);
+ SET_BINARY_MODE(fptr);
make_writeconv(fptr);
@@ -1319,25 +1989,27 @@ do_writeconv(VALUE str, rb_io_t *fptr)
if (!NIL_P(common_encoding)) {
str = rb_str_encode(str, common_encoding,
fptr->writeconv_pre_ecflags, fptr->writeconv_pre_ecopts);
+ *converted = 1;
}
if (fptr->writeconv) {
str = rb_econv_str_convert(fptr->writeconv, str, ECONV_PARTIAL_INPUT);
+ *converted = 1;
}
}
-#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
+#if RUBY_CRLF_ENVIRONMENT
#define fmode (fptr->mode)
else if (MODE_BTMODE(DEFAULT_TEXTMODE,0,1)) {
- if ((fptr->mode & FMODE_READABLE) &&
- !(fptr->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
- setmode(fptr->fd, O_BINARY);
- }
- else {
- setmode(fptr->fd, O_TEXT);
- }
- if (!rb_enc_asciicompat(rb_enc_get(str))) {
- rb_raise(rb_eArgError, "ASCII incompatible string written for text mode IO without encoding conversion: %s",
- rb_enc_name(rb_enc_get(str)));
+ if ((fptr->mode & FMODE_READABLE) &&
+ !(fptr->encs.ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
+ setmode(fptr->fd, O_BINARY);
+ }
+ else {
+ setmode(fptr->fd, O_TEXT);
+ }
+ if (!rb_enc_asciicompat(rb_enc_get(str))) {
+ rb_raise(rb_eArgError, "ASCII incompatible string written for text mode IO without encoding conversion: %s",
+ rb_enc_name(rb_enc_get(str)));
}
}
#undef fmode
@@ -1348,15 +2020,28 @@ do_writeconv(VALUE str, rb_io_t *fptr)
static long
io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
{
+ int converted = 0;
+ VALUE tmp;
+ long n, len;
+ const char *ptr;
+
#ifdef _WIN32
if (fptr->mode & FMODE_TTY) {
- long len = rb_w32_write_console(str, fptr->fd);
- if (len > 0) return len;
+ long len = rb_w32_write_console(str, fptr->fd);
+ if (len > 0) return len;
}
#endif
- str = do_writeconv(str, fptr);
- return io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str),
- fptr, nosync);
+
+ str = do_writeconv(str, fptr, &converted);
+ if (converted)
+ OBJ_FREEZE(str);
+
+ tmp = rb_str_tmp_frozen_no_embed_acquire(str);
+ RSTRING_GETMEM(tmp, ptr, len);
+ n = io_binwrite(ptr, len, fptr, nosync);
+ rb_str_tmp_frozen_release(str, tmp);
+
+ return n;
}
ssize_t
@@ -1366,7 +2051,7 @@ rb_io_bufwrite(VALUE io, const void *buf, size_t size)
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
- return (ssize_t)io_binwrite(0, buf, (long)size, fptr, 0);
+ return (ssize_t)io_binwrite(buf, (long)size, fptr, 0);
}
static VALUE
@@ -1379,46 +2064,271 @@ io_write(VALUE io, VALUE str, int nosync)
io = GetWriteIO(io);
str = rb_obj_as_string(str);
tmp = rb_io_check_io(io);
+
if (NIL_P(tmp)) {
- /* port is not IO, call write method for it. */
- return rb_funcall(io, id_write, 1, str);
+ /* port is not IO, call write method for it. */
+ return rb_funcall(io, id_write, 1, str);
}
+
io = tmp;
if (RSTRING_LEN(str) == 0) return INT2FIX(0);
- str = rb_str_new_frozen(str);
-
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
n = io_fwrite(str, fptr, nosync);
- if (n == -1L) rb_sys_fail_path(fptr->pathv);
+ if (n < 0L) rb_sys_fail_on_write(fptr);
return LONG2FIX(n);
}
+#ifdef HAVE_WRITEV
+struct binwritev_arg {
+ rb_io_t *fptr;
+ struct iovec *iov;
+ int iovcnt;
+ size_t total;
+};
+
+static VALUE
+io_binwritev_internal(VALUE arg)
+{
+ struct binwritev_arg *p = (struct binwritev_arg *)arg;
+
+ size_t remaining = p->total;
+ size_t offset = 0;
+
+ rb_io_t *fptr = p->fptr;
+ struct iovec *iov = p->iov;
+ int iovcnt = p->iovcnt;
+
+ while (remaining) {
+ long result = rb_writev_internal(fptr, iov, iovcnt);
+
+ if (result >= 0) {
+ offset += result;
+ if (fptr->wbuf.ptr && fptr->wbuf.len) {
+ if (offset < (size_t)fptr->wbuf.len) {
+ fptr->wbuf.off += result;
+ fptr->wbuf.len -= result;
+ }
+ else {
+ offset -= (size_t)fptr->wbuf.len;
+ fptr->wbuf.off = 0;
+ fptr->wbuf.len = 0;
+ }
+ }
+
+ if (offset == p->total) {
+ return p->total;
+ }
+
+ while (result >= (ssize_t)iov->iov_len) {
+ /* iovcnt > 0 */
+ result -= iov->iov_len;
+ iov->iov_len = 0;
+ iov++;
+
+ if (!--iovcnt) {
+ // I don't believe this code path can ever occur.
+ return offset;
+ }
+ }
+
+ iov->iov_base = (char *)iov->iov_base + result;
+ iov->iov_len -= result;
+ }
+ else if (rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
+ rb_io_check_closed(fptr);
+ }
+ else {
+ return -1;
+ }
+ }
+
+ return offset;
+}
+
+static long
+io_binwritev(struct iovec *iov, int iovcnt, rb_io_t *fptr)
+{
+ // Don't write anything if current thread has a pending interrupt:
+ rb_thread_check_ints();
+
+ if (iovcnt == 0) return 0;
+
+ size_t total = 0;
+ for (int i = 1; i < iovcnt; i++) total += iov[i].iov_len;
+
+ io_allocate_write_buffer(fptr, 1);
+
+ if (fptr->wbuf.ptr && fptr->wbuf.len) {
+ // The end of the buffered data:
+ size_t offset = fptr->wbuf.off + fptr->wbuf.len;
+
+ if (offset + total <= (size_t)fptr->wbuf.capa) {
+ for (int i = 1; i < iovcnt; i++) {
+ memcpy(fptr->wbuf.ptr+offset, iov[i].iov_base, iov[i].iov_len);
+ offset += iov[i].iov_len;
+ }
+
+ fptr->wbuf.len += total;
+
+ return total;
+ }
+ else {
+ iov[0].iov_base = fptr->wbuf.ptr + fptr->wbuf.off;
+ iov[0].iov_len = fptr->wbuf.len;
+ }
+ }
+ else {
+ // The first iov is reserved for the internal buffer, and it's empty.
+ iov++;
+
+ if (!--iovcnt) {
+ // If there are no other io vectors we are done.
+ return 0;
+ }
+ }
+
+ struct binwritev_arg arg;
+ arg.fptr = fptr;
+ arg.iov = iov;
+ arg.iovcnt = iovcnt;
+ arg.total = total;
+
+ if (!NIL_P(fptr->write_lock)) {
+ return rb_mutex_synchronize(fptr->write_lock, io_binwritev_internal, (VALUE)&arg);
+ }
+ else {
+ return io_binwritev_internal((VALUE)&arg);
+ }
+}
+
+static long
+io_fwritev(int argc, const VALUE *argv, rb_io_t *fptr)
+{
+ int i, converted, iovcnt = argc + 1;
+ long n;
+ VALUE v1, v2, str, tmp, *tmp_array;
+ struct iovec *iov;
+
+ iov = ALLOCV_N(struct iovec, v1, iovcnt);
+ tmp_array = ALLOCV_N(VALUE, v2, argc);
+
+ for (i = 0; i < argc; i++) {
+ str = rb_obj_as_string(argv[i]);
+ converted = 0;
+ str = do_writeconv(str, fptr, &converted);
+
+ if (converted)
+ OBJ_FREEZE(str);
+
+ tmp = rb_str_tmp_frozen_acquire(str);
+ tmp_array[i] = tmp;
+
+ /* iov[0] is reserved for buffer of fptr */
+ iov[i+1].iov_base = RSTRING_PTR(tmp);
+ iov[i+1].iov_len = RSTRING_LEN(tmp);
+ }
+
+ n = io_binwritev(iov, iovcnt, fptr);
+ if (v1) ALLOCV_END(v1);
+
+ for (i = 0; i < argc; i++) {
+ rb_str_tmp_frozen_release(argv[i], tmp_array[i]);
+ }
+
+ if (v2) ALLOCV_END(v2);
+
+ return n;
+}
+
+static int
+iovcnt_ok(int iovcnt)
+{
+#ifdef IOV_MAX
+ return iovcnt < IOV_MAX;
+#else /* GNU/Hurd has writev, but no IOV_MAX */
+ return 1;
+#endif
+}
+#endif /* HAVE_WRITEV */
+
+static VALUE
+io_writev(int argc, const VALUE *argv, VALUE io)
+{
+ rb_io_t *fptr;
+ long n;
+ VALUE tmp, total = INT2FIX(0);
+ int i, cnt = 1;
+
+ io = GetWriteIO(io);
+ tmp = rb_io_check_io(io);
+
+ if (NIL_P(tmp)) {
+ /* port is not IO, call write method for it. */
+ return rb_funcallv(io, id_write, argc, argv);
+ }
+
+ io = tmp;
+
+ GetOpenFile(io, fptr);
+ rb_io_check_writable(fptr);
+
+ for (i = 0; i < argc; i += cnt) {
+#ifdef HAVE_WRITEV
+ if ((fptr->mode & (FMODE_SYNC|FMODE_TTY)) && iovcnt_ok(cnt = argc - i)) {
+ n = io_fwritev(cnt, &argv[i], fptr);
+ }
+ else
+#endif
+ {
+ cnt = 1;
+ /* sync at last item */
+ n = io_fwrite(rb_obj_as_string(argv[i]), fptr, (i < argc-1));
+ }
+
+ if (n < 0L)
+ rb_sys_fail_on_write(fptr);
+
+ total = rb_fix_plus(LONG2FIX(n), total);
+ }
+
+ return total;
+}
+
/*
* call-seq:
- * ios.write(string) -> integer
+ * write(*objects) -> integer
*
- * Writes the given string to <em>ios</em>. The stream must be opened
- * for writing. If the argument is not a string, it will be converted
- * to a string using <code>to_s</code>. Returns the number of bytes
- * written.
+ * Writes each of the given +objects+ to +self+,
+ * which must be opened for writing
+ * (see {Access Modes}[rdoc-ref:File@Access+Modes]);
+ * returns the total number bytes written;
+ * each of +objects+ that is not a string is converted via method +to_s+:
*
- * count = $stdout.write("This is a test\n")
- * puts "That was #{count} bytes of data"
+ * $stdout.write('Hello', ', ', 'World!', "\n") # => 14
+ * $stdout.write('foo', :bar, 2, "\n") # => 8
*
- * <em>produces:</em>
+ * Output:
*
- * This is a test
- * That was 15 bytes of data
+ * Hello, World!
+ * foobar2
+ *
+ * Related: IO#read.
*/
static VALUE
-io_write_m(VALUE io, VALUE str)
+io_write_m(int argc, VALUE *argv, VALUE io)
{
- return io_write(io, str, 0);
+ if (argc != 1) {
+ return io_writev(argc, argv, io);
+ }
+ else {
+ VALUE str = argv[0];
+ return io_write(io, str, 0);
+ }
}
VALUE
@@ -1427,19 +2337,45 @@ rb_io_write(VALUE io, VALUE str)
return rb_funcallv(io, id_write, 1, &str);
}
+static VALUE
+rb_io_writev(VALUE io, int argc, const VALUE *argv)
+{
+ if (argc > 1 && rb_obj_method_arity(io, id_write) == 1) {
+ if (io != rb_ractor_stderr() && RTEST(ruby_verbose)) {
+ VALUE klass = CLASS_OF(io);
+ char sep = RCLASS_SINGLETON_P(klass) ? (klass = io, '.') : '#';
+ rb_category_warning(
+ RB_WARN_CATEGORY_DEPRECATED, "%+"PRIsVALUE"%c""write is outdated interface"
+ " which accepts just one argument",
+ klass, sep
+ );
+ }
+
+ do rb_io_write(io, *argv++); while (--argc);
+
+ return Qnil;
+ }
+
+ return rb_funcallv(io, id_write, argc, argv);
+}
+
/*
* call-seq:
- * ios << obj -> ios
+ * self << object -> self
*
- * String Output---Writes <i>obj</i> to <em>ios</em>.
- * <i>obj</i> will be converted to a string using
- * <code>to_s</code>.
+ * Writes the given +object+ to +self+,
+ * which must be opened for writing (see {Access Modes}[rdoc-ref:File@Access+Modes]);
+ * returns +self+;
+ * if +object+ is not a string, it is converted via method +to_s+:
*
- * $stdout << "Hello " << "world!\n"
+ * $stdout << 'Hello' << ', ' << 'World!' << "\n"
+ * $stdout << 'foo' << :bar << 2 << "\n"
*
- * <em>produces:</em>
+ * Output:
+ *
+ * Hello, World!
+ * foobar2
*
- * Hello world!
*/
@@ -1456,6 +2392,10 @@ nogvl_fsync(void *ptr)
{
rb_io_t *fptr = ptr;
+#ifdef _WIN32
+ if (GetFileType((HANDLE)rb_w32_get_osfhandle(fptr->fd)) != FILE_TYPE_DISK)
+ return 0;
+#endif
return (VALUE)fsync(fptr->fd);
}
#endif
@@ -1474,15 +2414,10 @@ rb_io_flush_raw(VALUE io, int sync)
if (fptr->mode & FMODE_WRITABLE) {
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
-#ifdef _WIN32
- if (sync && GetFileType((HANDLE)rb_w32_get_osfhandle(fptr->fd)) == FILE_TYPE_DISK) {
- rb_thread_io_blocking_region(nogvl_fsync, fptr, fptr->fd);
- }
-#endif
+ rb_sys_fail_on_write(fptr);
}
if (fptr->mode & FMODE_READABLE) {
- io_unread(fptr);
+ io_unread(fptr, true);
}
return io;
@@ -1490,18 +2425,14 @@ rb_io_flush_raw(VALUE io, int sync)
/*
* call-seq:
- * ios.flush -> ios
+ * flush -> self
*
- * Flushes any buffered data within <em>ios</em> to the underlying
- * operating system (note that this is Ruby internal buffering only;
- * the OS may buffer the data as well).
+ * Flushes data buffered in +self+ to the operating system
+ * (but does not necessarily flush data buffered in the operating system):
*
- * $stdout.print "no newline"
- * $stdout.flush
+ * $stdout.print 'no newline' # Not necessarily flushed.
+ * $stdout.flush # Flushed.
*
- * <em>produces:</em>
- *
- * no newline
*/
VALUE
@@ -1512,22 +2443,25 @@ rb_io_flush(VALUE io)
/*
* call-seq:
- * ios.pos -> integer
- * ios.tell -> integer
+ * tell -> integer
+ *
+ * Returns the current position (in bytes) in +self+
+ * (see {Position}[rdoc-ref:IO@Position]):
*
- * Returns the current offset (in bytes) of <em>ios</em>.
+ * f = File.open('t.txt')
+ * f.tell # => 0
+ * f.gets # => "First line\n"
+ * f.tell # => 12
+ * f.close
*
- * f = File.new("testfile")
- * f.pos #=> 0
- * f.gets #=> "This is line one\n"
- * f.pos #=> 17
+ * Related: IO#pos=, IO#seek.
*/
static VALUE
rb_io_tell(VALUE io)
{
rb_io_t *fptr;
- off_t pos;
+ rb_off_t pos;
GetOpenFile(io, fptr);
pos = io_tell(fptr);
@@ -1540,7 +2474,7 @@ static VALUE
rb_io_seek(VALUE io, VALUE offset, int whence)
{
rb_io_t *fptr;
- off_t pos;
+ rb_off_t pos;
pos = NUM2OFFT(offset);
GetOpenFile(io, fptr);
@@ -1572,23 +2506,49 @@ interpret_seek_whence(VALUE vwhence)
/*
* call-seq:
- * ios.seek(amount, whence=IO::SEEK_SET) -> 0
+ * seek(offset, whence = IO::SEEK_SET) -> 0
*
- * Seeks to a given offset <i>anInteger</i> in the stream according to
- * the value of <i>whence</i>:
+ * Seeks to the position given by integer +offset+
+ * (see {Position}[rdoc-ref:IO@Position])
+ * and constant +whence+, which is one of:
*
- * :CUR or IO::SEEK_CUR | Seeks to _amount_ plus current position
- * ----------------------+--------------------------------------------------
- * :END or IO::SEEK_END | Seeks to _amount_ plus end of stream (you
- * | probably want a negative value for _amount_)
- * ----------------------+--------------------------------------------------
- * :SET or IO::SEEK_SET | Seeks to the absolute location given by _amount_
+ * - +:CUR+ or <tt>IO::SEEK_CUR</tt>:
+ * Repositions the stream to its current position plus the given +offset+:
*
- * Example:
+ * f = File.open('t.txt')
+ * f.tell # => 0
+ * f.seek(20, :CUR) # => 0
+ * f.tell # => 20
+ * f.seek(-10, :CUR) # => 0
+ * f.tell # => 10
+ * f.close
+ *
+ * - +:END+ or <tt>IO::SEEK_END</tt>:
+ * Repositions the stream to its end plus the given +offset+:
+ *
+ * f = File.open('t.txt')
+ * f.tell # => 0
+ * f.seek(0, :END) # => 0 # Repositions to stream end.
+ * f.tell # => 52
+ * f.seek(-20, :END) # => 0
+ * f.tell # => 32
+ * f.seek(-40, :END) # => 0
+ * f.tell # => 12
+ * f.close
+ *
+ * - +:SET+ or <tt>IO::SEEK_SET</tt>:
+ * Repositions the stream to the given +offset+:
+ *
+ * f = File.open('t.txt')
+ * f.tell # => 0
+ * f.seek(20, :SET) # => 0
+ * f.tell # => 20
+ * f.seek(40, :SET) # => 0
+ * f.tell # => 40
+ * f.close
+ *
+ * Related: IO#pos=, IO#tell.
*
- * f = File.new("testfile")
- * f.seek(-13, IO::SEEK_END) #=> 0
- * f.readline #=> "And so on...\n"
*/
static VALUE
@@ -1598,7 +2558,7 @@ rb_io_seek_m(int argc, VALUE *argv, VALUE io)
int whence = SEEK_SET;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
- whence = interpret_seek_whence(ptrname);
+ whence = interpret_seek_whence(ptrname);
}
return rb_io_seek(io, offset, whence);
@@ -1606,22 +2566,26 @@ rb_io_seek_m(int argc, VALUE *argv, VALUE io)
/*
* call-seq:
- * ios.pos = integer -> integer
+ * pos = new_position -> new_position
*
- * Seeks to the given position (in bytes) in <em>ios</em>.
- * It is not guaranteed that seeking to the right position when <em>ios</em>
- * is textmode.
+ * Seeks to the given +new_position+ (in bytes);
+ * see {Position}[rdoc-ref:IO@Position]:
+ *
+ * f = File.open('t.txt')
+ * f.tell # => 0
+ * f.pos = 20 # => 20
+ * f.tell # => 20
+ * f.close
+ *
+ * Related: IO#seek, IO#tell.
*
- * f = File.new("testfile")
- * f.pos = 17
- * f.gets #=> "This is line two\n"
*/
static VALUE
rb_io_set_pos(VALUE io, VALUE offset)
{
rb_io_t *fptr;
- off_t pos;
+ rb_off_t pos;
pos = NUM2OFFT(offset);
GetOpenFile(io, fptr);
@@ -1635,18 +2599,26 @@ static void clear_readconv(rb_io_t *fptr);
/*
* call-seq:
- * ios.rewind -> 0
+ * rewind -> 0
+ *
+ * Repositions the stream to its beginning,
+ * setting both the position and the line number to zero;
+ * see {Position}[rdoc-ref:IO@Position]
+ * and {Line Number}[rdoc-ref:IO@Line+Number]:
*
- * Positions <em>ios</em> to the beginning of input, resetting
- * <code>lineno</code> to zero.
+ * f = File.open('t.txt')
+ * f.tell # => 0
+ * f.lineno # => 0
+ * f.gets # => "First line\n"
+ * f.tell # => 12
+ * f.lineno # => 1
+ * f.rewind # => 0
+ * f.tell # => 0
+ * f.lineno # => 0
+ * f.close
*
- * f = File.new("testfile")
- * f.readline #=> "This is line one\n"
- * f.rewind #=> 0
- * f.lineno #=> 0
- * f.readline #=> "This is line one\n"
+ * Note that this method cannot be used with streams such as pipes, ttys, and sockets.
*
- * Note that it cannot be used with streams such as pipes, ttys, and sockets.
*/
static VALUE
@@ -1657,17 +2629,28 @@ rb_io_rewind(VALUE io)
GetOpenFile(io, fptr);
if (io_seek(fptr, 0L, 0) < 0 && errno) rb_sys_fail_path(fptr->pathv);
if (io == ARGF.current_file) {
- ARGF.lineno -= fptr->lineno;
+ ARGF.lineno -= fptr->lineno;
}
fptr->lineno = 0;
if (fptr->readconv) {
- clear_readconv(fptr);
+ clear_readconv(fptr);
}
return INT2FIX(0);
}
static int
+fptr_wait_readable(rb_io_t *fptr)
+{
+ int result = rb_io_maybe_wait_readable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT);
+
+ if (result)
+ rb_io_check_closed(fptr);
+
+ return result;
+}
+
+static int
io_fillbuf(rb_io_t *fptr)
{
ssize_t r;
@@ -1677,26 +2660,24 @@ io_fillbuf(rb_io_t *fptr)
fptr->rbuf.len = 0;
fptr->rbuf.capa = IO_RBUF_CAPA_FOR(fptr);
fptr->rbuf.ptr = ALLOC_N(char, fptr->rbuf.capa);
-#ifdef _WIN32
- fptr->rbuf.capa--;
-#endif
}
if (fptr->rbuf.len == 0) {
retry:
- {
- r = rb_read_internal(fptr->fd, fptr->rbuf.ptr, fptr->rbuf.capa);
- }
+ r = rb_io_read_memory(fptr, fptr->rbuf.ptr, fptr->rbuf.capa);
+
if (r < 0) {
- if (rb_io_wait_readable(fptr->fd))
+ if (fptr_wait_readable(fptr))
goto retry;
- {
- VALUE path = rb_sprintf("fd:%d ", fptr->fd);
- if (!NIL_P(fptr->pathv)) {
- rb_str_append(path, fptr->pathv);
- }
- rb_sys_fail_path(path);
- }
+
+ int e = errno;
+ VALUE path = rb_sprintf("fd:%d ", fptr->fd);
+ if (!NIL_P(fptr->pathv)) {
+ rb_str_append(path, fptr->pathv);
+ }
+
+ rb_syserr_fail_path(e, path);
}
+ if (r > 0) rb_io_check_closed(fptr);
fptr->rbuf.off = 0;
fptr->rbuf.len = (int)r; /* r should be <= rbuf_capa */
if (r == 0)
@@ -1707,36 +2688,37 @@ io_fillbuf(rb_io_t *fptr)
/*
* call-seq:
- * ios.eof -> true or false
- * ios.eof? -> true or false
+ * eof -> true or false
+ *
+ * Returns +true+ if the stream is positioned at its end, +false+ otherwise;
+ * see {Position}[rdoc-ref:IO@Position]:
*
- * Returns true if <em>ios</em> is at end of file that means
- * there are no more data to read.
- * The stream must be opened for reading or an <code>IOError</code> will be
- * raised.
+ * f = File.open('t.txt')
+ * f.eof # => false
+ * f.seek(0, :END) # => 0
+ * f.eof # => true
+ * f.close
*
- * f = File.new("testfile")
- * dummy = f.readlines
- * f.eof #=> true
+ * Raises an exception unless the stream is opened for reading;
+ * see {Mode}[rdoc-ref:File@Access+Modes].
*
- * If <em>ios</em> is a stream such as pipe or socket, <code>IO#eof?</code>
- * blocks until the other end sends some data or closes it.
+ * If +self+ is a stream such as pipe or socket, this method
+ * blocks until the other end sends some data or closes it:
*
- * r, w = IO.pipe
- * Thread.new { sleep 1; w.close }
- * r.eof? #=> true after 1 second blocking
+ * r, w = IO.pipe
+ * Thread.new { sleep 1; w.close }
+ * r.eof? # => true # After 1-second wait.
*
- * r, w = IO.pipe
- * Thread.new { sleep 1; w.puts "a" }
- * r.eof? #=> false after 1 second blocking
+ * r, w = IO.pipe
+ * Thread.new { sleep 1; w.puts "a" }
+ * r.eof? # => false # After 1-second wait.
*
- * r, w = IO.pipe
- * r.eof? # blocks forever
+ * r, w = IO.pipe
+ * r.eof? # blocks forever
*
- * Note that <code>IO#eof?</code> reads data to the input byte buffer.
- * So <code>IO#sysread</code> may not behave as you intend with
- * <code>IO#eof?</code>, unless you call <code>IO#rewind</code>
- * first (which is not available for some streams).
+ * Note that this method reads data to the input byte buffer. So
+ * IO#sysread may not behave as you intend with IO#eof?, unless you
+ * call IO#rewind first (which is not available for some streams).
*/
VALUE
@@ -1750,28 +2732,28 @@ rb_io_eof(VALUE io)
if (READ_CHAR_PENDING(fptr)) return Qfalse;
if (READ_DATA_PENDING(fptr)) return Qfalse;
READ_CHECK(fptr);
-#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
+#if RUBY_CRLF_ENVIRONMENT
if (!NEED_READCONV(fptr) && NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {
- return eof(fptr->fd) ? Qtrue : Qfalse;
+ return RBOOL(eof(fptr->fd));
}
#endif
- if (io_fillbuf(fptr) < 0) {
- return Qtrue;
- }
- return Qfalse;
+ return RBOOL(io_fillbuf(fptr) < 0);
}
/*
* call-seq:
- * ios.sync -> true or false
+ * sync -> true or false
*
- * Returns the current ``sync mode'' of <em>ios</em>. When sync mode is
- * true, all output is immediately flushed to the underlying operating
- * system and is not buffered by Ruby internally. See also
- * <code>IO#fsync</code>.
+ * Returns the current sync mode of the stream.
+ * When sync mode is true, all output is immediately flushed to the underlying
+ * operating system and is not buffered by Ruby internally. See also #fsync.
+ *
+ * f = File.open('t.tmp', 'w')
+ * f.sync # => false
+ * f.sync = true
+ * f.sync # => true
+ * f.close
*
- * f = File.new("testfile")
- * f.sync #=> false
*/
static VALUE
@@ -1781,24 +2763,34 @@ rb_io_sync(VALUE io)
io = GetWriteIO(io);
GetOpenFile(io, fptr);
- return (fptr->mode & FMODE_SYNC) ? Qtrue : Qfalse;
+ return RBOOL(fptr->mode & FMODE_SYNC);
}
#ifdef HAVE_FSYNC
/*
* call-seq:
- * ios.sync = boolean -> boolean
+ * sync = boolean -> boolean
+ *
+ * Sets the _sync_ _mode_ for the stream to the given value;
+ * returns the given value.
+ *
+ * Values for the sync mode:
*
- * Sets the ``sync mode'' to <code>true</code> or <code>false</code>.
- * When sync mode is true, all output is immediately flushed to the
- * underlying operating system and is not buffered internally. Returns
- * the new state. See also <code>IO#fsync</code>.
+ * - +true+: All output is immediately flushed to the
+ * underlying operating system and is not buffered internally.
+ * - +false+: Output may be buffered internally.
*
- * f = File.new("testfile")
- * f.sync = true
+ * Example;
+ *
+ * f = File.open('t.tmp', 'w')
+ * f.sync # => false
+ * f.sync = true
+ * f.sync # => true
+ * f.close
+ *
+ * Related: IO#fsync.
*
- * <em>(produces no output)</em>
*/
static VALUE
@@ -1809,26 +2801,30 @@ rb_io_set_sync(VALUE io, VALUE sync)
io = GetWriteIO(io);
GetOpenFile(io, fptr);
if (RTEST(sync)) {
- fptr->mode |= FMODE_SYNC;
+ fptr->mode |= FMODE_SYNC;
}
else {
- fptr->mode &= ~FMODE_SYNC;
+ fptr->mode &= ~FMODE_SYNC;
}
return sync;
}
/*
* call-seq:
- * ios.fsync -> 0 or nil
+ * fsync -> 0
+ *
+ * Immediately writes to disk all data buffered in the stream,
+ * via the operating system's <tt>fsync(2)</tt>.
+
+ * Note this difference:
+ *
+ * - IO#sync=: Ensures that data is flushed from the stream's internal buffers,
+ * but does not guarantee that the operating system actually writes the data to disk.
+ * - IO#fsync: Ensures both that data is flushed from internal buffers,
+ * and that data is written to disk.
*
- * Immediately writes all buffered data in <em>ios</em> to disk.
- * Note that <code>fsync</code> differs from
- * using <code>IO#sync=</code>. The latter ensures that data is flushed
- * from Ruby's buffers, but does not guarantee that the underlying
- * operating system actually writes it to disk.
+ * Raises an exception if the operating system does not support <tt>fsync(2)</tt>.
*
- * <code>NotImplementedError</code> is raised
- * if the underlying operating system does not support <em>fsync(2)</em>.
*/
static VALUE
@@ -1840,11 +2836,11 @@ rb_io_fsync(VALUE io)
GetOpenFile(io, fptr);
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
-# ifndef _WIN32 /* already called in io_fflush() */
- if ((int)rb_thread_io_blocking_region(nogvl_fsync, fptr, fptr->fd) < 0)
- rb_sys_fail_path(fptr->pathv);
-# endif
+ rb_sys_fail_on_write(fptr);
+
+ if ((int)rb_io_blocking_region(fptr, nogvl_fsync, fptr))
+ rb_sys_fail_path(fptr->pathv);
+
return INT2FIX(0);
}
#else
@@ -1864,18 +2860,22 @@ nogvl_fdatasync(void *ptr)
{
rb_io_t *fptr = ptr;
+#ifdef _WIN32
+ if (GetFileType((HANDLE)rb_w32_get_osfhandle(fptr->fd)) != FILE_TYPE_DISK)
+ return 0;
+#endif
return (VALUE)fdatasync(fptr->fd);
}
/*
* call-seq:
- * ios.fdatasync -> 0 or nil
+ * fdatasync -> 0
*
- * Immediately writes all buffered data in <em>ios</em> to disk.
+ * Immediately writes to disk all data buffered in the stream,
+ * via the operating system's: <tt>fdatasync(2)</tt>, if supported,
+ * otherwise via <tt>fsync(2)</tt>, if supported;
+ * otherwise raises an exception.
*
- * If the underlying operating system does not support <em>fdatasync(2)</em>,
- * <code>IO#fsync</code> is called instead (which might raise a
- * <code>NotImplementedError</code>).
*/
static VALUE
@@ -1887,10 +2887,10 @@ rb_io_fdatasync(VALUE io)
GetOpenFile(io, fptr);
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
+ rb_sys_fail_on_write(fptr);
- if ((int)rb_thread_io_blocking_region(nogvl_fdatasync, fptr, fptr->fd) == 0)
- return INT2FIX(0);
+ if ((int)rb_io_blocking_region(fptr, nogvl_fdatasync, fptr) == 0)
+ return INT2FIX(0);
/* fall back */
return rb_io_fsync(io);
@@ -1901,46 +2901,77 @@ rb_io_fdatasync(VALUE io)
/*
* call-seq:
- * ios.fileno -> fixnum
- * ios.to_i -> fixnum
+ * fileno -> integer
*
- * Returns an integer representing the numeric file descriptor for
- * <em>ios</em>.
+ * Returns the integer file descriptor for the stream:
+ *
+ * $stdin.fileno # => 0
+ * $stdout.fileno # => 1
+ * $stderr.fileno # => 2
+ * File.open('t.txt').fileno # => 10
+ * f.close
*
- * $stdin.fileno #=> 0
- * $stdout.fileno #=> 1
*/
static VALUE
rb_io_fileno(VALUE io)
{
- rb_io_t *fptr;
+ rb_io_t *fptr = RFILE(io)->fptr;
int fd;
- GetOpenFile(io, fptr);
+ rb_io_check_closed(fptr);
fd = fptr->fd;
return INT2FIX(fd);
}
+int
+rb_io_descriptor(VALUE io)
+{
+ if (RB_TYPE_P(io, T_FILE)) {
+ rb_io_t *fptr = RFILE(io)->fptr;
+ rb_io_check_closed(fptr);
+ return fptr->fd;
+ }
+ else {
+ VALUE fileno = rb_check_funcall(io, id_fileno, 0, NULL);
+ if (!UNDEF_P(fileno)) {
+ return RB_NUM2INT(fileno);
+ }
+ }
+
+ rb_raise(rb_eTypeError, "expected IO or #fileno, %"PRIsVALUE" given", rb_obj_class(io));
+
+ UNREACHABLE_RETURN(-1);
+}
+
+int
+rb_io_mode(VALUE io)
+{
+ rb_io_t *fptr;
+ GetOpenFile(io, fptr);
+ return fptr->mode;
+}
/*
* call-seq:
- * ios.pid -> fixnum
+ * pid -> integer or nil
*
- * Returns the process ID of a child process associated with
- * <em>ios</em>. This will be set by <code>IO.popen</code>.
+ * Returns the process ID of a child process associated with the stream,
+ * which will have been set by IO#popen, or +nil+ if the stream was not
+ * created by IO#popen:
*
- * pipe = IO.popen("-")
- * if pipe
- * $stderr.puts "In parent, child pid is #{pipe.pid}"
- * else
- * $stderr.puts "In child, pid is #{$$}"
- * end
+ * pipe = IO.popen("-")
+ * if pipe
+ * $stderr.puts "In parent, child pid is #{pipe.pid}"
+ * else
+ * $stderr.puts "In child, pid is #{$$}"
+ * end
*
- * <em>produces:</em>
+ * Output:
+ *
+ * In child, pid is 26209
+ * In parent, child pid is 26209
*
- * In child, pid is 26209
- * In parent, child pid is 26209
*/
static VALUE
@@ -1950,16 +2981,44 @@ rb_io_pid(VALUE io)
GetOpenFile(io, fptr);
if (!fptr->pid)
- return Qnil;
+ return Qnil;
return PIDT2NUM(fptr->pid);
}
+/*
+ * call-seq:
+ * path -> string or nil
+ *
+ * Returns the path associated with the IO, or +nil+ if there is no path
+ * associated with the IO. It is not guaranteed that the path exists on
+ * the filesystem.
+ *
+ * $stdin.path # => "<STDIN>"
+ *
+ * File.open("testfile") {|f| f.path} # => "testfile"
+ */
+
+VALUE
+rb_io_path(VALUE io)
+{
+ rb_io_t *fptr = RFILE(io)->fptr;
+
+ if (!fptr)
+ return Qnil;
+
+ return rb_obj_dup(fptr->pathv);
+}
/*
- * call-seq:
- * ios.inspect -> string
+ * call-seq:
+ * inspect -> string
+ *
+ * Returns a string representation of +self+:
+ *
+ * f = File.open('t.txt')
+ * f.inspect # => "#<File:t.txt>"
+ * f.close
*
- * Return a string describing this IO object.
*/
static VALUE
@@ -1969,23 +3028,23 @@ rb_io_inspect(VALUE obj)
VALUE result;
static const char closed[] = " (closed)";
- fptr = RFILE(rb_io_taint_check(obj))->fptr;
+ fptr = RFILE(obj)->fptr;
if (!fptr) return rb_any_to_s(obj);
result = rb_str_new_cstr("#<");
rb_str_append(result, rb_class_name(CLASS_OF(obj)));
rb_str_cat2(result, ":");
if (NIL_P(fptr->pathv)) {
if (fptr->fd < 0) {
- rb_str_cat(result, closed+1, strlen(closed)-1);
+ rb_str_cat(result, closed+1, strlen(closed)-1);
}
else {
- rb_str_catf(result, "fd %d", fptr->fd);
+ rb_str_catf(result, "fd %d", fptr->fd);
}
}
else {
- rb_str_append(result, fptr->pathv);
+ rb_str_append(result, fptr->pathv);
if (fptr->fd < 0) {
- rb_str_cat(result, closed, strlen(closed));
+ rb_str_cat(result, closed, strlen(closed));
}
}
return rb_str_cat2(result, ">");
@@ -1993,9 +3052,10 @@ rb_io_inspect(VALUE obj)
/*
* call-seq:
- * ios.to_io -> ios
+ * to_io -> self
+ *
+ * Returns +self+.
*
- * Returns <em>ios</em>.
*/
static VALUE
@@ -2027,36 +3087,37 @@ io_bufread(char *ptr, long len, rb_io_t *fptr)
long c;
if (READ_DATA_PENDING(fptr) == 0) {
- while (n > 0) {
+ while (n > 0) {
again:
- c = rb_read_internal(fptr->fd, ptr+offset, n);
- if (c == 0) break;
- if (c < 0) {
- if (rb_io_wait_readable(fptr->fd))
+ rb_io_check_closed(fptr);
+ c = rb_io_read_memory(fptr, ptr+offset, n);
+ if (c == 0) break;
+ if (c < 0) {
+ if (fptr_wait_readable(fptr))
goto again;
- return -1;
- }
- offset += c;
- if ((n -= c) <= 0) break;
- }
- return len - n;
+ return -1;
+ }
+ offset += c;
+ if ((n -= c) <= 0) break;
+ }
+ return len - n;
}
while (n > 0) {
- c = read_buffered_data(ptr+offset, n, fptr);
- if (c > 0) {
- offset += c;
- if ((n -= c) <= 0) break;
- }
- rb_io_check_closed(fptr);
- if (io_fillbuf(fptr) < 0) {
- break;
- }
+ c = read_buffered_data(ptr+offset, n, fptr);
+ if (c > 0) {
+ offset += c;
+ if ((n -= c) <= 0) break;
+ }
+ rb_io_check_closed(fptr);
+ if (io_fillbuf(fptr) < 0) {
+ break;
+ }
}
return len - n;
}
-static void io_setstrbuf(VALUE *str, long len);
+static int io_setstrbuf(VALUE *str, long len);
struct bufread_arg {
char *str_ptr;
@@ -2088,43 +3149,31 @@ io_fread(VALUE str, long offset, long size, rb_io_t *fptr)
return len;
}
-ssize_t
-rb_io_bufread(VALUE io, void *buf, size_t size)
-{
- rb_io_t *fptr;
-
- GetOpenFile(io, fptr);
- rb_io_check_readable(fptr);
- return (ssize_t)io_bufread(buf, (long)size, fptr);
-}
-
-#define SMALLBUF 100
-
static long
remain_size(rb_io_t *fptr)
{
struct stat st;
- off_t siz = READ_DATA_PENDING_COUNT(fptr);
- off_t pos;
+ rb_off_t siz = READ_DATA_PENDING_COUNT(fptr);
+ rb_off_t pos;
if (fstat(fptr->fd, &st) == 0 && S_ISREG(st.st_mode)
-#if defined(__BEOS__) || defined(__HAIKU__)
- && (st.st_dev > 3)
+#if defined(__HAIKU__)
+ && (st.st_dev > 3)
#endif
- )
+ )
{
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
- pos = lseek(fptr->fd, 0, SEEK_CUR);
- if (st.st_size >= pos && pos >= 0) {
- siz += st.st_size - pos;
- if (siz > LONG_MAX) {
- rb_raise(rb_eIOError, "file too big for single read");
- }
- }
+ rb_sys_fail_on_write(fptr);
+ pos = lseek(fptr->fd, 0, SEEK_CUR);
+ if (st.st_size >= pos && pos >= 0) {
+ siz += st.st_size - pos;
+ if (siz > LONG_MAX) {
+ rb_raise(rb_eIOError, "file too big for single read");
+ }
+ }
}
else {
- siz += BUFSIZ;
+ siz += BUFSIZ;
}
return (long)siz;
}
@@ -2132,7 +3181,6 @@ remain_size(rb_io_t *fptr)
static VALUE
io_enc_str(VALUE str, rb_io_t *fptr)
{
- OBJ_TAINT(str);
rb_enc_associate(str, io_read_encoding(fptr));
return str;
}
@@ -2148,7 +3196,7 @@ make_readconv(rb_io_t *fptr, int size)
ecopts = fptr->encs.ecopts;
if (fptr->encs.enc2) {
sname = rb_enc_name(fptr->encs.enc2);
- dname = rb_enc_name(fptr->encs.enc);
+ dname = rb_enc_name(io_read_encoding(fptr));
}
else {
sname = dname = "";
@@ -2158,7 +3206,7 @@ make_readconv(rb_io_t *fptr, int size)
rb_exc_raise(rb_econv_open_exc(sname, dname, ecflags));
fptr->cbuf.off = 0;
fptr->cbuf.len = 0;
- if (size < IO_CBUF_CAPA_MIN) size = IO_CBUF_CAPA_MIN;
+ if (size < IO_CBUF_CAPA_MIN) size = IO_CBUF_CAPA_MIN;
fptr->cbuf.capa = size;
fptr->cbuf.ptr = ALLOC_N(char, fptr->cbuf.capa);
}
@@ -2215,27 +3263,27 @@ fill_cbuf(rb_io_t *fptr, int ec_flags)
if (res == econv_finished) {
return MORE_CHAR_FINISHED;
- }
+ }
if (res == econv_source_buffer_empty) {
if (fptr->rbuf.len == 0) {
- READ_CHECK(fptr);
- if (io_fillbuf(fptr) == -1) {
- if (!fptr->readconv) {
- return MORE_CHAR_FINISHED;
- }
+ READ_CHECK(fptr);
+ if (io_fillbuf(fptr) < 0) {
+ if (!fptr->readconv) {
+ return MORE_CHAR_FINISHED;
+ }
ds = dp = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.off + fptr->cbuf.len;
de = (unsigned char *)fptr->cbuf.ptr + fptr->cbuf.capa;
res = rb_econv_convert(fptr->readconv, NULL, NULL, &dp, de, 0);
fptr->cbuf.len += (int)(dp - ds);
rb_econv_check_error(fptr->readconv);
- break;
+ break;
}
}
}
}
if (cbuf_len0 != fptr->cbuf.len)
- return MORE_CHAR_SUSPENDED;
+ return MORE_CHAR_SUSPENDED;
return MORE_CHAR_FINISHED;
}
@@ -2255,15 +3303,14 @@ io_shift_cbuf(rb_io_t *fptr, int len, VALUE *strp)
{
VALUE str = Qnil;
if (strp) {
- str = *strp;
- if (NIL_P(str)) {
- *strp = str = rb_str_new(fptr->cbuf.ptr+fptr->cbuf.off, len);
- }
- else {
- rb_str_cat(str, fptr->cbuf.ptr+fptr->cbuf.off, len);
- }
- OBJ_TAINT(str);
- rb_enc_associate(str, fptr->encs.enc);
+ str = *strp;
+ if (NIL_P(str)) {
+ *strp = str = rb_str_new(fptr->cbuf.ptr+fptr->cbuf.off, len);
+ }
+ else {
+ rb_str_cat(str, fptr->cbuf.ptr+fptr->cbuf.off, len);
+ }
+ rb_enc_associate(str, fptr->encs.enc);
}
fptr->cbuf.off += len;
fptr->cbuf.len -= len;
@@ -2277,36 +3324,45 @@ io_shift_cbuf(rb_io_t *fptr, int len, VALUE *strp)
return str;
}
-static void
+static int
io_setstrbuf(VALUE *str, long len)
{
-#ifdef _WIN32
- len = (len + 1) & ~1L; /* round up for wide char */
-#endif
if (NIL_P(*str)) {
- *str = rb_str_new(0, 0);
+ *str = rb_str_new(0, len);
+ return TRUE;
}
else {
- VALUE s = StringValue(*str);
- long clen = RSTRING_LEN(s);
- if (clen >= len) {
- if (clen != len) {
- rb_str_modify(s);
- rb_str_set_len(s, len);
- }
- return;
- }
- len -= clen;
+ VALUE s = StringValue(*str);
+ rb_str_modify(s);
+
+ long clen = RSTRING_LEN(s);
+ if (clen >= len) {
+ return FALSE;
+ }
+ len -= clen;
+ }
+ if ((rb_str_capacity(*str) - (size_t)RSTRING_LEN(*str)) < (size_t)len) {
+ rb_str_modify_expand(*str, len);
+ }
+ return FALSE;
+}
+
+#define MAX_REALLOC_GAP 4096
+static void
+io_shrink_read_string(VALUE str, long n)
+{
+ if (rb_str_capacity(str) - n > MAX_REALLOC_GAP) {
+ rb_str_resize(str, n);
}
- rb_str_modify_expand(*str, len);
}
static void
-io_set_read_length(VALUE str, long n)
+io_set_read_length(VALUE str, long n, int shrinkable)
{
if (RSTRING_LEN(str) != n) {
- rb_str_modify(str);
- rb_str_set_len(str, n);
+ rb_str_modify(str);
+ rb_str_set_len(str, n);
+ if (shrinkable) io_shrink_read_string(str, n);
}
}
@@ -2318,25 +3374,31 @@ read_all(rb_io_t *fptr, long siz, VALUE str)
long pos;
rb_encoding *enc;
int cr;
+ int shrinkable;
if (NEED_READCONV(fptr)) {
- SET_BINARY_MODE(fptr);
- io_setstrbuf(&str,0);
+ int first = !NIL_P(str);
+ SET_BINARY_MODE(fptr);
+ shrinkable = io_setstrbuf(&str,0);
make_readconv(fptr, 0);
while (1) {
VALUE v;
if (fptr->cbuf.len) {
+ if (first) rb_str_set_len(str, first = 0);
io_shift_cbuf(fptr, fptr->cbuf.len, &str);
}
v = fill_cbuf(fptr, 0);
if (v != MORE_CHAR_SUSPENDED && v != MORE_CHAR_FINISHED) {
if (fptr->cbuf.len) {
+ if (first) rb_str_set_len(str, first = 0);
io_shift_cbuf(fptr, fptr->cbuf.len, &str);
}
rb_exc_raise(v);
}
if (v == MORE_CHAR_FINISHED) {
clear_readconv(fptr);
+ if (first) rb_str_set_len(str, first = 0);
+ if (shrinkable) io_shrink_read_string(str, RSTRING_LEN(str));
return io_enc_str(str, fptr);
}
}
@@ -2350,22 +3412,33 @@ read_all(rb_io_t *fptr, long siz, VALUE str)
cr = 0;
if (siz == 0) siz = BUFSIZ;
- io_setstrbuf(&str,siz);
+ shrinkable = io_setstrbuf(&str, siz);
for (;;) {
- READ_CHECK(fptr);
- n = io_fread(str, bytes, siz - bytes, fptr);
- if (n == 0 && bytes == 0) {
- rb_str_set_len(str, 0);
- break;
- }
- bytes += n;
- rb_str_set_len(str, bytes);
- if (cr != ENC_CODERANGE_BROKEN)
- pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
- if (bytes < siz) break;
- siz += BUFSIZ;
- rb_str_modify_expand(str, BUFSIZ);
+ READ_CHECK(fptr);
+ n = io_fread(str, bytes, siz - bytes, fptr);
+ if (n == 0 && bytes == 0) {
+ rb_str_set_len(str, 0);
+ break;
+ }
+ bytes += n;
+ rb_str_set_len(str, bytes);
+ if (cr != ENC_CODERANGE_BROKEN)
+ pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + bytes, enc, &cr);
+ if (bytes < siz) break;
+ siz += BUFSIZ;
+
+ size_t capa = rb_str_capacity(str);
+ if (capa < (size_t)RSTRING_LEN(str) + BUFSIZ) {
+ if (capa < BUFSIZ) {
+ capa = BUFSIZ;
+ }
+ else if (capa > IO_MAX_BUFFER_GROWTH) {
+ capa = IO_MAX_BUFFER_GROWTH;
+ }
+ rb_str_modify_expand(str, capa);
+ }
}
+ if (shrinkable) io_shrink_read_string(str, RSTRING_LEN(str));
str = io_enc_str(str, fptr);
ENC_CODERANGE_SET(str, cr);
return str;
@@ -2374,62 +3447,66 @@ read_all(rb_io_t *fptr, long siz, VALUE str)
void
rb_io_set_nonblock(rb_io_t *fptr)
{
- int oflags;
-#ifdef F_GETFL
- oflags = fcntl(fptr->fd, F_GETFL);
- if (oflags == -1) {
+ if (rb_fd_set_nonblock(fptr->fd) != 0) {
rb_sys_fail_path(fptr->pathv);
}
-#else
- oflags = 0;
-#endif
- if ((oflags & O_NONBLOCK) == 0) {
- oflags |= O_NONBLOCK;
- if (fcntl(fptr->fd, F_SETFL, oflags) == -1) {
- rb_sys_fail_path(fptr->pathv);
- }
- }
}
-void
-rb_readwrite_sys_fail(int writable, const char *mesg);
+static VALUE
+io_read_memory_call(VALUE arg)
+{
+ struct io_internal_read_struct *iis = (struct io_internal_read_struct *)arg;
-struct read_internal_arg {
- int fd;
- char *str_ptr;
- long len;
-};
+ VALUE scheduler = rb_fiber_scheduler_current();
+ if (scheduler != Qnil) {
+ VALUE result = rb_fiber_scheduler_io_read_memory(scheduler, iis->fptr->self, iis->buf, iis->capa, 0);
-static VALUE
-read_internal_call(VALUE arg)
+ if (!UNDEF_P(result)) {
+ // This is actually returned as a pseudo-VALUE and later cast to a long:
+ return (VALUE)rb_fiber_scheduler_io_result_apply(result);
+ }
+ }
+
+ if (iis->nonblock) {
+ return rb_io_blocking_region(iis->fptr, internal_read_func, iis);
+ }
+ else {
+ return rb_io_blocking_region_wait(iis->fptr, internal_read_func, iis, RUBY_IO_READABLE);
+ }
+}
+
+static long
+io_read_memory_locktmp(VALUE str, struct io_internal_read_struct *iis)
{
- struct read_internal_arg *p = (struct read_internal_arg *)arg;
- p->len = rb_read_internal(p->fd, p->str_ptr, p->len);
- return Qundef;
+ return (long)rb_str_locktmp_ensure(str, io_read_memory_call, (VALUE)iis);
}
+#define no_exception_p(opts) !rb_opts_exception_p((opts), TRUE)
+
static VALUE
-io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock, int no_exception)
+io_getpartial(int argc, VALUE *argv, VALUE io, int no_exception, int nonblock)
{
rb_io_t *fptr;
VALUE length, str;
long n, len;
- struct read_internal_arg arg;
+ struct io_internal_read_struct iis;
+ int shrinkable;
- rb_scan_args(argc, argv, "11:", &length, &str, NULL);
+ rb_scan_args(argc, argv, "11", &length, &str);
if ((len = NUM2LONG(length)) < 0) {
- rb_raise(rb_eArgError, "negative length %ld given", len);
+ rb_raise(rb_eArgError, "negative length %ld given", len);
}
- io_setstrbuf(&str,len);
- OBJ_TAINT(str);
+ shrinkable = io_setstrbuf(&str, len);
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
- if (len == 0)
- return str;
+ if (len == 0) {
+ io_set_read_length(str, 0, shrinkable);
+ return str;
+ }
if (!nonblock)
READ_CHECK(fptr);
@@ -2439,25 +3516,30 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock, int no_exception)
if (nonblock) {
rb_io_set_nonblock(fptr);
}
- io_setstrbuf(&str, len);
- arg.fd = fptr->fd;
- arg.str_ptr = RSTRING_PTR(str);
- arg.len = len;
- rb_str_locktmp_ensure(str, read_internal_call, (VALUE)&arg);
- n = arg.len;
+ io_setstrbuf(&str, len);
+ iis.th = rb_thread_current();
+ iis.fptr = fptr;
+ iis.nonblock = nonblock;
+ iis.fd = fptr->fd;
+ iis.buf = RSTRING_PTR(str);
+ iis.capa = len;
+ iis.timeout = NULL;
+ n = io_read_memory_locktmp(str, &iis);
if (n < 0) {
- if (!nonblock && rb_io_wait_readable(fptr->fd))
+ int e = errno;
+ if (!nonblock && fptr_wait_readable(fptr))
goto again;
- if (nonblock && (errno == EWOULDBLOCK || errno == EAGAIN)) {
+ if (nonblock && (io_again_p(e))) {
if (no_exception)
- return ID2SYM(rb_intern("wait_readable"));
+ return sym_wait_readable;
else
- rb_readwrite_sys_fail(RB_IO_WAIT_READABLE, "read would block");
+ rb_readwrite_syserr_fail(RB_IO_WAIT_READABLE,
+ e, "read would block");
}
- rb_sys_fail_path(fptr->pathv);
+ rb_syserr_fail_path(e, fptr->pathv);
}
}
- io_set_read_length(str, n);
+ io_set_read_length(str, n, shrinkable);
if (n == 0)
return Qnil;
@@ -2467,176 +3549,204 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock, int no_exception)
/*
* call-seq:
- * ios.readpartial(maxlen) -> string
- * ios.readpartial(maxlen, outbuf) -> outbuf
+ * readpartial(maxlen) -> string
+ * readpartial(maxlen, out_string) -> out_string
*
- * Reads at most <i>maxlen</i> bytes from the I/O stream.
- * It blocks only if <em>ios</em> has no data immediately available.
- * It doesn't block if some data available.
- * If the optional <i>outbuf</i> argument is present,
- * it must reference a String, which will receive the data.
- * The <i>outbuf</i> will contain only the received data after the method call
- * even if it is not empty at the beginning.
- * It raises <code>EOFError</code> on end of file.
- *
- * readpartial is designed for streams such as pipe, socket, tty, etc.
- * It blocks only when no data immediately available.
- * This means that it blocks only when following all conditions hold.
- * * the byte buffer in the IO object is empty.
- * * the content of the stream is empty.
- * * the stream is not reached to EOF.
- *
- * When readpartial blocks, it waits data or EOF on the stream.
- * If some data is reached, readpartial returns with the data.
- * If EOF is reached, readpartial raises EOFError.
- *
- * When readpartial doesn't blocks, it returns or raises immediately.
- * If the byte buffer is not empty, it returns the data in the buffer.
- * Otherwise if the stream has some content,
- * it returns the data in the stream.
- * Otherwise if the stream is reached to EOF, it raises EOFError.
- *
- * r, w = IO.pipe # buffer pipe content
- * w << "abc" # "" "abc".
- * r.readpartial(4096) #=> "abc" "" ""
- * r.readpartial(4096) # blocks because buffer and pipe is empty.
- *
- * r, w = IO.pipe # buffer pipe content
- * w << "abc" # "" "abc"
- * w.close # "" "abc" EOF
- * r.readpartial(4096) #=> "abc" "" EOF
- * r.readpartial(4096) # raises EOFError
+ * Reads up to +maxlen+ bytes from the stream;
+ * returns a string (either a new string or the given +out_string+).
+ * Its encoding is:
*
- * r, w = IO.pipe # buffer pipe content
- * w << "abc\ndef\n" # "" "abc\ndef\n"
- * r.gets #=> "abc\n" "def\n" ""
- * w << "ghi\n" # "def\n" "ghi\n"
- * r.readpartial(4096) #=> "def\n" "" "ghi\n"
- * r.readpartial(4096) #=> "ghi\n" "" ""
+ * - The unchanged encoding of +out_string+, if +out_string+ is given.
+ * - ASCII-8BIT, otherwise.
*
- * Note that readpartial behaves similar to sysread.
- * The differences are:
- * * If the byte buffer is not empty, read from the byte buffer instead of "sysread for buffered IO (IOError)".
- * * It doesn't cause Errno::EWOULDBLOCK and Errno::EINTR. When readpartial meets EWOULDBLOCK and EINTR by read system call, readpartial retry the system call.
+ * - Contains +maxlen+ bytes from the stream, if available.
+ * - Otherwise contains all available bytes, if any available.
+ * - Otherwise is an empty string.
*
- * The later means that readpartial is nonblocking-flag insensitive.
- * It blocks on the situation IO#sysread causes Errno::EWOULDBLOCK as if the fd is blocking mode.
+ * With the single non-negative integer argument +maxlen+ given,
+ * returns a new string:
*
- */
-
-static VALUE
-io_readpartial(int argc, VALUE *argv, VALUE io)
-{
- VALUE ret;
-
- ret = io_getpartial(argc, argv, io, 0, 0);
- if (NIL_P(ret))
- rb_eof_error();
- return ret;
-}
-
-/*
- * call-seq:
- * ios.read_nonblock(maxlen) -> string
- * ios.read_nonblock(maxlen, outbuf) -> outbuf
+ * f = File.new('t.txt')
+ * f.readpartial(20) # => "First line\nSecond l"
+ * f.readpartial(20) # => "ine\n\nFourth line\n"
+ * f.readpartial(20) # => "Fifth line\n"
+ * f.readpartial(20) # Raises EOFError.
+ * f.close
*
- * Reads at most <i>maxlen</i> bytes from <em>ios</em> using
- * the read(2) system call after O_NONBLOCK is set for
- * the underlying file descriptor.
+ * With both argument +maxlen+ and string argument +out_string+ given,
+ * returns modified +out_string+:
*
- * If the optional <i>outbuf</i> argument is present,
- * it must reference a String, which will receive the data.
- * The <i>outbuf</i> will contain only the received data after the method call
- * even if it is not empty at the beginning.
+ * f = File.new('t.txt')
+ * s = 'foo'
+ * f.readpartial(20, s) # => "First line\nSecond l"
+ * s = 'bar'
+ * f.readpartial(0, s) # => ""
+ * f.close
*
- * read_nonblock just calls the read(2) system call.
- * It causes all errors the read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc.
- * The caller should care such errors.
+ * This method is useful for a stream such as a pipe, a socket, or a tty.
+ * It blocks only when no data is immediately available.
+ * This means that it blocks only when _all_ of the following are true:
*
- * If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
- * it is extended by IO::WaitReadable.
- * So IO::WaitReadable can be used to rescue the exceptions for retrying read_nonblock.
+ * - The byte buffer in the stream is empty.
+ * - The content of the stream is empty.
+ * - The stream is not at EOF.
*
- * read_nonblock causes EOFError on EOF.
+ * When blocked, the method waits for either more data or EOF on the stream:
*
- * If the read byte buffer is not empty,
- * read_nonblock reads from the buffer like readpartial.
- * In this case, the read(2) system call is not called.
+ * - If more data is read, the method returns the data.
+ * - If EOF is reached, the method raises EOFError.
*
- * When read_nonblock raises an exception kind of IO::WaitReadable,
- * read_nonblock should not be called
- * until io is readable for avoiding busy loop.
- * This can be done as follows.
+ * When not blocked, the method responds immediately:
*
- * # emulates blocking read (readpartial).
- * begin
- * result = io.read_nonblock(maxlen)
- * rescue IO::WaitReadable
- * IO.select([io])
- * retry
- * end
+ * - Returns data from the buffer if there is any.
+ * - Otherwise returns data from the stream if there is any.
+ * - Otherwise raises EOFError if the stream has reached EOF.
+ *
+ * Note that this method is similar to sysread. The differences are:
+ *
+ * - If the byte buffer is not empty, read from the byte buffer
+ * instead of "sysread for buffered IO (IOError)".
+ * - It doesn't cause Errno::EWOULDBLOCK and Errno::EINTR. When
+ * readpartial meets EWOULDBLOCK and EINTR by read system call,
+ * readpartial retries the system call.
+ *
+ * The latter means that readpartial is non-blocking-flag insensitive.
+ * It blocks on the situation IO#sysread causes Errno::EWOULDBLOCK as
+ * if the fd is blocking mode.
+ *
+ * Examples:
*
- * Although IO#read_nonblock doesn't raise IO::WaitWritable.
- * OpenSSL::Buffering#read_nonblock can raise IO::WaitWritable.
- * If IO and SSL should be used polymorphically,
- * IO::WaitWritable should be rescued too.
- * See the document of OpenSSL::Buffering#read_nonblock for sample code.
+ * # # Returned Buffer Content Pipe Content
+ * r, w = IO.pipe #
+ * w << 'abc' # "" "abc".
+ * r.readpartial(4096) # => "abc" "" ""
+ * r.readpartial(4096) # (Blocks because buffer and pipe are empty.)
+ *
+ * # # Returned Buffer Content Pipe Content
+ * r, w = IO.pipe #
+ * w << 'abc' # "" "abc"
+ * w.close # "" "abc" EOF
+ * r.readpartial(4096) # => "abc" "" EOF
+ * r.readpartial(4096) # raises EOFError
+ *
+ * # # Returned Buffer Content Pipe Content
+ * r, w = IO.pipe #
+ * w << "abc\ndef\n" # "" "abc\ndef\n"
+ * r.gets # => "abc\n" "def\n" ""
+ * w << "ghi\n" # "def\n" "ghi\n"
+ * r.readpartial(4096) # => "def\n" "" "ghi\n"
+ * r.readpartial(4096) # => "ghi\n" "" ""
*
- * Note that this method is identical to readpartial
- * except the non-blocking flag is set.
*/
static VALUE
-io_read_nonblock(int argc, VALUE *argv, VALUE io)
+io_readpartial(int argc, VALUE *argv, VALUE io)
{
VALUE ret;
- VALUE opts = Qnil;
- int no_exception = 0;
- rb_scan_args(argc, argv, "11:", NULL, NULL, &opts);
+ ret = io_getpartial(argc, argv, io, Qnil, 0);
+ if (NIL_P(ret))
+ rb_eof_error();
+ return ret;
+}
+
+static VALUE
+io_nonblock_eof(int no_exception)
+{
+ if (!no_exception) {
+ rb_eof_error();
+ }
+ return Qnil;
+}
+
+/* :nodoc: */
+static VALUE
+io_read_nonblock(rb_execution_context_t *ec, VALUE io, VALUE length, VALUE str, VALUE ex)
+{
+ rb_io_t *fptr;
+ long n, len;
+ struct io_internal_read_struct iis;
+ int shrinkable;
+
+ if ((len = NUM2LONG(length)) < 0) {
+ rb_raise(rb_eArgError, "negative length %ld given", len);
+ }
- if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception))
- no_exception = 1;
+ shrinkable = io_setstrbuf(&str, len);
+ rb_bool_expected(ex, "exception", TRUE);
- ret = io_getpartial(argc, argv, io, 1, no_exception);
+ GetOpenFile(io, fptr);
+ rb_io_check_byte_readable(fptr);
- if (NIL_P(ret)) {
- if (no_exception)
- return Qnil;
- else
- rb_eof_error();
+ if (len == 0) {
+ io_set_read_length(str, 0, shrinkable);
+ return str;
}
- return ret;
+
+ n = read_buffered_data(RSTRING_PTR(str), len, fptr);
+ if (n <= 0) {
+ rb_fd_set_nonblock(fptr->fd);
+ shrinkable |= io_setstrbuf(&str, len);
+ iis.fptr = fptr;
+ iis.nonblock = 1;
+ iis.fd = fptr->fd;
+ iis.buf = RSTRING_PTR(str);
+ iis.capa = len;
+ iis.timeout = NULL;
+ n = io_read_memory_locktmp(str, &iis);
+ if (n < 0) {
+ int e = errno;
+ if (io_again_p(e)) {
+ if (!ex) return sym_wait_readable;
+ rb_readwrite_syserr_fail(RB_IO_WAIT_READABLE,
+ e, "read would block");
+ }
+ rb_syserr_fail_path(e, fptr->pathv);
+ }
+ }
+ io_set_read_length(str, n, shrinkable);
+
+ if (n == 0) {
+ if (!ex) return Qnil;
+ rb_eof_error();
+ }
+
+ return str;
}
+/* :nodoc: */
static VALUE
-io_write_nonblock(VALUE io, VALUE str, int no_exception)
+io_write_nonblock(rb_execution_context_t *ec, VALUE io, VALUE str, VALUE ex)
{
rb_io_t *fptr;
long n;
if (!RB_TYPE_P(str, T_STRING))
- str = rb_obj_as_string(str);
+ str = rb_obj_as_string(str);
+ rb_bool_expected(ex, "exception", TRUE);
io = GetWriteIO(io);
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
+ rb_sys_fail_on_write(fptr);
- rb_io_set_nonblock(fptr);
+ rb_fd_set_nonblock(fptr->fd);
n = write(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
+ RB_GC_GUARD(str);
- if (n == -1) {
- if (errno == EWOULDBLOCK || errno == EAGAIN) {
- if (no_exception) {
- return ID2SYM(rb_intern("wait_writable"));
- } else {
- rb_readwrite_sys_fail(RB_IO_WAIT_WRITABLE, "write would block");
- }
- }
- rb_sys_fail_path(fptr->pathv);
+ if (n < 0) {
+ int e = errno;
+ if (io_again_p(e)) {
+ if (!ex) {
+ return sym_wait_writable;
+ }
+ else {
+ rb_readwrite_syserr_fail(RB_IO_WAIT_WRITABLE, e, "write would block");
+ }
+ }
+ rb_syserr_fail_path(e, fptr->pathv);
}
return LONG2FIX(n);
@@ -2644,141 +3754,73 @@ io_write_nonblock(VALUE io, VALUE str, int no_exception)
/*
* call-seq:
- * ios.write_nonblock(string) -> integer
- * ios.write_nonblock(string [, options]) -> integer
- *
- * Writes the given string to <em>ios</em> using
- * the write(2) system call after O_NONBLOCK is set for
- * the underlying file descriptor.
- *
- * It returns the number of bytes written.
- *
- * write_nonblock just calls the write(2) system call.
- * It causes all errors the write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc.
- * The result may also be smaller than string.length (partial write).
- * The caller should care such errors and partial write.
+ * read(maxlen = nil, out_string = nil) -> new_string, out_string, or nil
*
- * If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
- * it is extended by IO::WaitWritable.
- * So IO::WaitWritable can be used to rescue the exceptions for retrying write_nonblock.
+ * Reads bytes from the stream; the stream must be opened for reading
+ * (see {Access Modes}[rdoc-ref:File@Access+Modes]):
*
- * # Creates a pipe.
- * r, w = IO.pipe
- *
- * # write_nonblock writes only 65536 bytes and return 65536.
- * # (The pipe size is 65536 bytes on this environment.)
- * s = "a" * 100000
- * p w.write_nonblock(s) #=> 65536
- *
- * # write_nonblock cannot write a byte and raise EWOULDBLOCK (EAGAIN).
- * p w.write_nonblock("b") # Resource temporarily unavailable (Errno::EAGAIN)
- *
- * If the write buffer is not empty, it is flushed at first.
- *
- * When write_nonblock raises an exception kind of IO::WaitWritable,
- * write_nonblock should not be called
- * until io is writable for avoiding busy loop.
- * This can be done as follows.
- *
- * begin
- * result = io.write_nonblock(string)
- * rescue IO::WaitWritable, Errno::EINTR
- * IO.select(nil, [io])
- * retry
- * end
+ * - If +maxlen+ is +nil+, reads all bytes using the stream's data mode.
+ * - Otherwise reads up to +maxlen+ bytes in binary mode.
*
- * Note that this doesn't guarantee to write all data in string.
- * The length written is reported as result and it should be checked later.
+ * Returns a string (either a new string or the given +out_string+)
+ * containing the bytes read.
+ * The encoding of the string depends on both +maxLen+ and +out_string+:
*
- * On some platforms such as Windows, write_nonblock is not supported
- * according to the kind of the IO object.
- * In such cases, write_nonblock raises <code>Errno::EBADF</code>.
- *
- * By specifying `exception: false`, the options hash allows you to indicate
- * that write_nonblock should not raise an IO::WaitWritable exception, but
- * return the symbol :wait_writable instead.
- *
- */
-
-static VALUE
-rb_io_write_nonblock(int argc, VALUE *argv, VALUE io)
-{
- VALUE str;
- VALUE opts = Qnil;
- int no_exceptions = 0;
-
- rb_scan_args(argc, argv, "10:", &str, &opts);
-
- if (!NIL_P(opts) && Qfalse == rb_hash_aref(opts, sym_exception))
- no_exceptions = 1;
-
- return io_write_nonblock(io, str, no_exceptions);
-}
-
-/*
- * call-seq:
- * ios.read([length [, outbuf]]) -> string, outbuf, or nil
+ * - +maxlen+ is +nil+: uses internal encoding of +self+
+ * (regardless of whether +out_string+ was given).
+ * - +maxlen+ not +nil+:
*
- * Reads <i>length</i> bytes from the I/O stream.
+ * - +out_string+ given: encoding of +out_string+ not modified.
+ * - +out_string+ not given: ASCII-8BIT is used.
*
- * <i>length</i> must be a non-negative integer or <code>nil</code>.
+ * <b>Without Argument +out_string+</b>
*
- * If <i>length</i> is a positive integer,
- * it try to read <i>length</i> bytes without any conversion (binary mode).
- * It returns <code>nil</code> or a string whose length is 1 to <i>length</i> bytes.
- * <code>nil</code> means it met EOF at beginning.
- * The 1 to <i>length</i>-1 bytes string means it met EOF after reading the result.
- * The <i>length</i> bytes string means it doesn't meet EOF.
- * The resulted string is always ASCII-8BIT encoding.
+ * When argument +out_string+ is omitted,
+ * the returned value is a new string:
*
- * If <i>length</i> is omitted or is <code>nil</code>,
- * it reads until EOF and the encoding conversion is applied.
- * It returns a string even if EOF is met at beginning.
+ * f = File.new('t.txt')
+ * f.read
+ * # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * f.rewind
+ * f.read(30) # => "First line\r\nSecond line\r\n\r\nFou"
+ * f.read(30) # => "rth line\r\nFifth line\r\n"
+ * f.read(30) # => nil
+ * f.close
*
- * If <i>length</i> is zero, it returns <code>""</code>.
+ * If +maxlen+ is zero, returns an empty string.
*
- * If the optional <i>outbuf</i> argument is present, it must reference
- * a String, which will receive the data.
- * The <i>outbuf</i> will contain only the received data after the method call
- * even if it is not empty at the beginning.
+ * <b> With Argument +out_string+</b>
*
- * At end of file, it returns <code>nil</code> or <code>""</code>
- * depend on <i>length</i>.
- * <code><i>ios</i>.read()</code> and
- * <code><i>ios</i>.read(nil)</code> returns <code>""</code>.
- * <code><i>ios</i>.read(<i>positive-integer</i>)</code> returns <code>nil</code>.
+ * When argument +out_string+ is given,
+ * the returned value is +out_string+, whose content is replaced:
*
- * f = File.new("testfile")
- * f.read(16) #=> "This is line one"
+ * f = File.new('t.txt')
+ * s = 'foo' # => "foo"
+ * f.read(nil, s) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * s # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * f.rewind
+ * s = 'bar'
+ * f.read(30, s) # => "First line\r\nSecond line\r\n\r\nFou"
+ * s # => "First line\r\nSecond line\r\n\r\nFou"
+ * s = 'baz'
+ * f.read(30, s) # => "rth line\r\nFifth line\r\n"
+ * s # => "rth line\r\nFifth line\r\n"
+ * s = 'bat'
+ * f.read(30, s) # => nil
+ * s # => ""
+ * f.close
*
- * # reads whole file
- * open("file") {|f|
- * data = f.read # This returns a string even if the file is empty.
- * ...
- * }
+ * Note that this method behaves like the fread() function in C.
+ * This means it retries to invoke read(2) system calls to read data
+ * with the specified maxlen (or until EOF).
*
- * # iterate over fixed length records.
- * open("fixed-record-file") {|f|
- * while record = f.read(256)
- * ...
- * end
- * }
+ * This behavior is preserved even if the stream is in non-blocking mode.
+ * (This method is non-blocking-flag insensitive as other methods.)
*
- * # iterate over variable length records.
- * # record is prefixed by 32-bit length.
- * open("variable-record-file") {|f|
- * while len = f.read(4)
- * len = len.unpack("N")[0] # 32-bit length
- * record = f.read(len) # This returns a string even if len is 0.
- * end
- * }
+ * If you need the behavior like a single read(2) system call,
+ * consider #readpartial, #read_nonblock, and #sysread.
*
- * Note that this method behaves like fread() function in C.
- * This means it retry to invoke read(2) system call to read data with the specified length (or until EOF).
- * This behavior is preserved even if <i>ios</i> is non-blocking mode.
- * (This method is non-blocking flag insensitive as other methods.)
- * If you need the behavior like single read(2) system call,
- * consider readpartial, read_nonblock and sysread.
+ * Related: IO#write.
*/
static VALUE
@@ -2787,41 +3829,44 @@ io_read(int argc, VALUE *argv, VALUE io)
rb_io_t *fptr;
long n, len;
VALUE length, str;
-#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
+ int shrinkable;
+#if RUBY_CRLF_ENVIRONMENT
int previous_mode;
#endif
rb_scan_args(argc, argv, "02", &length, &str);
if (NIL_P(length)) {
- GetOpenFile(io, fptr);
- rb_io_check_char_readable(fptr);
- return read_all(fptr, remain_size(fptr), str);
+ GetOpenFile(io, fptr);
+ rb_io_check_char_readable(fptr);
+ return read_all(fptr, remain_size(fptr), str);
}
len = NUM2LONG(length);
if (len < 0) {
- rb_raise(rb_eArgError, "negative length %ld given", len);
+ rb_raise(rb_eArgError, "negative length %ld given", len);
}
- io_setstrbuf(&str,len);
+ shrinkable = io_setstrbuf(&str,len);
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
- if (len == 0) return str;
+ if (len == 0) {
+ io_set_read_length(str, 0, shrinkable);
+ return str;
+ }
READ_CHECK(fptr);
-#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
+#if RUBY_CRLF_ENVIRONMENT
previous_mode = set_binary_mode_with_seek_cur(fptr);
#endif
n = io_fread(str, 0, len, fptr);
- io_set_read_length(str, n);
-#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
+ io_set_read_length(str, n, shrinkable);
+#if RUBY_CRLF_ENVIRONMENT
if (previous_mode == O_TEXT) {
- setmode(fptr->fd, O_TEXT);
+ setmode(fptr->fd, O_TEXT);
}
#endif
if (n == 0) return Qnil;
- OBJ_TAINT(str);
return str;
}
@@ -2831,17 +3876,42 @@ rscheck(const char *rsptr, long rslen, VALUE rs)
{
if (!rs) return;
if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
- rb_raise(rb_eRuntimeError, "rs modified");
+ rb_raise(rb_eRuntimeError, "rs modified");
+}
+
+static const char *
+search_delim(const char *p, long len, int delim, rb_encoding *enc)
+{
+ if (rb_enc_mbminlen(enc) == 1) {
+ p = memchr(p, delim, len);
+ if (p) return p + 1;
+ }
+ else {
+ const char *end = p + len;
+ while (p < end) {
+ int r = rb_enc_precise_mbclen(p, end, enc);
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ p += rb_enc_mbminlen(enc);
+ continue;
+ }
+ int n = MBCLEN_CHARFOUND_LEN(r);
+ if (rb_enc_mbc_to_codepoint(p, end, enc) == (unsigned int)delim) {
+ return p + n;
+ }
+ p += n;
+ }
+ }
+ return NULL;
}
static int
-appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp)
+appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp, rb_encoding *enc)
{
VALUE str = *strp;
long limit = *lp;
if (NEED_READCONV(fptr)) {
- SET_BINARY_MODE(fptr);
+ SET_BINARY_MODE(fptr);
make_readconv(fptr, 0);
do {
const char *p, *e;
@@ -2850,9 +3920,9 @@ appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp)
p = READ_CHAR_PENDING_PTR(fptr);
if (0 < limit && limit < searchlen)
searchlen = (int)limit;
- e = memchr(p, delim, searchlen);
+ e = search_delim(p, searchlen, delim, enc);
if (e) {
- int len = (int)(e-p+1);
+ int len = (int)(e-p);
if (NIL_P(str))
*strp = str = rb_str_new(p, len);
else
@@ -2885,32 +3955,32 @@ appendline(rb_io_t *fptr, int delim, VALUE *strp, long *lp)
NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
do {
- long pending = READ_DATA_PENDING_COUNT(fptr);
- if (pending > 0) {
- const char *p = READ_DATA_PENDING_PTR(fptr);
- const char *e;
- long last;
-
- if (limit > 0 && pending > limit) pending = limit;
- e = memchr(p, delim, pending);
- if (e) pending = e - p + 1;
- if (!NIL_P(str)) {
- last = RSTRING_LEN(str);
- rb_str_resize(str, last + pending);
- }
- else {
+ long pending = READ_DATA_PENDING_COUNT(fptr);
+ if (pending > 0) {
+ const char *p = READ_DATA_PENDING_PTR(fptr);
+ const char *e;
+ long last;
+
+ if (limit > 0 && pending > limit) pending = limit;
+ e = search_delim(p, pending, delim, enc);
+ if (e) pending = e - p;
+ if (!NIL_P(str)) {
+ last = RSTRING_LEN(str);
+ rb_str_resize(str, last + pending);
+ }
+ else {
last = 0;
- *strp = str = rb_str_buf_new(pending);
- rb_str_set_len(str, pending);
- }
- read_buffered_data(RSTRING_PTR(str) + last, pending, fptr); /* must not fail */
- limit -= pending;
- *lp = limit;
- if (e) return delim;
- if (limit == 0)
- return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
- }
- READ_CHECK(fptr);
+ *strp = str = rb_str_buf_new(pending);
+ rb_str_set_len(str, pending);
+ }
+ read_buffered_data(RSTRING_PTR(str) + last, pending, fptr); /* must not fail */
+ limit -= pending;
+ *lp = limit;
+ if (e) return delim;
+ if (limit == 0)
+ return (unsigned char)RSTRING_PTR(str)[RSTRING_LEN(str)-1];
+ }
+ READ_CHECK(fptr);
} while (io_fillbuf(fptr) >= 0);
*lp = limit;
return EOF;
@@ -2920,53 +3990,53 @@ static inline int
swallow(rb_io_t *fptr, int term)
{
if (NEED_READCONV(fptr)) {
- rb_encoding *enc = io_read_encoding(fptr);
- int needconv = rb_enc_mbminlen(enc) != 1;
- SET_BINARY_MODE(fptr);
- make_readconv(fptr, 0);
- do {
- size_t cnt;
- while ((cnt = READ_CHAR_PENDING_COUNT(fptr)) > 0) {
- const char *p = READ_CHAR_PENDING_PTR(fptr);
- int i;
- if (!needconv) {
- if (*p != term) return TRUE;
- i = (int)cnt;
- while (--i && *++p == term);
- }
- else {
- const char *e = p + cnt;
- if (rb_enc_ascget(p, e, &i, enc) != term) return TRUE;
- while ((p += i) < e && rb_enc_ascget(p, e, &i, enc) == term);
- i = (int)(e - p);
- }
- io_shift_cbuf(fptr, (int)cnt - i, NULL);
- }
- } while (more_char(fptr) != MORE_CHAR_FINISHED);
- return FALSE;
+ rb_encoding *enc = io_read_encoding(fptr);
+ int needconv = rb_enc_mbminlen(enc) != 1;
+ SET_BINARY_MODE(fptr);
+ make_readconv(fptr, 0);
+ do {
+ size_t cnt;
+ while ((cnt = READ_CHAR_PENDING_COUNT(fptr)) > 0) {
+ const char *p = READ_CHAR_PENDING_PTR(fptr);
+ int i;
+ if (!needconv) {
+ if (*p != term) return TRUE;
+ i = (int)cnt;
+ while (--i && *++p == term);
+ }
+ else {
+ const char *e = p + cnt;
+ if (rb_enc_ascget(p, e, &i, enc) != term) return TRUE;
+ while ((p += i) < e && rb_enc_ascget(p, e, &i, enc) == term);
+ i = (int)(e - p);
+ }
+ io_shift_cbuf(fptr, (int)cnt - i, NULL);
+ }
+ } while (more_char(fptr) != MORE_CHAR_FINISHED);
+ return FALSE;
}
NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
do {
- size_t cnt;
- while ((cnt = READ_DATA_PENDING_COUNT(fptr)) > 0) {
- char buf[1024];
- const char *p = READ_DATA_PENDING_PTR(fptr);
- int i;
- if (cnt > sizeof buf) cnt = sizeof buf;
- if (*p != term) return TRUE;
- i = (int)cnt;
- while (--i && *++p == term);
- if (!read_buffered_data(buf, cnt - i, fptr)) /* must not fail */
- rb_sys_fail_path(fptr->pathv);
- }
- READ_CHECK(fptr);
+ size_t cnt;
+ while ((cnt = READ_DATA_PENDING_COUNT(fptr)) > 0) {
+ char buf[1024];
+ const char *p = READ_DATA_PENDING_PTR(fptr);
+ int i;
+ if (cnt > sizeof buf) cnt = sizeof buf;
+ if (*p != term) return TRUE;
+ i = (int)cnt;
+ while (--i && *++p == term);
+ if (!read_buffered_data(buf, cnt - i, fptr)) /* must not fail */
+ rb_sys_fail_path(fptr->pathv);
+ }
+ READ_CHECK(fptr);
} while (io_fillbuf(fptr) == 0);
return FALSE;
}
static VALUE
-rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, VALUE io)
+rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, int chomp)
{
VALUE str = Qnil;
int len = 0;
@@ -2974,53 +4044,80 @@ rb_io_getline_fast(rb_io_t *fptr, rb_encoding *enc, VALUE io)
int cr = 0;
do {
- int pending = READ_DATA_PENDING_COUNT(fptr);
+ int pending = READ_DATA_PENDING_COUNT(fptr);
- if (pending > 0) {
- const char *p = READ_DATA_PENDING_PTR(fptr);
- const char *e;
+ if (pending > 0) {
+ const char *p = READ_DATA_PENDING_PTR(fptr);
+ const char *e;
+ int chomplen = 0;
- e = memchr(p, '\n', pending);
- if (e) {
+ e = memchr(p, '\n', pending);
+ if (e) {
pending = (int)(e - p + 1);
- }
- if (NIL_P(str)) {
- str = rb_str_new(p, pending);
- fptr->rbuf.off += pending;
- fptr->rbuf.len -= pending;
- }
- else {
- rb_str_resize(str, len + pending);
- read_buffered_data(RSTRING_PTR(str)+len, pending, fptr);
- }
- len += pending;
- if (cr != ENC_CODERANGE_BROKEN)
- pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + len, enc, &cr);
- if (e) break;
- }
- READ_CHECK(fptr);
+ if (chomp) {
+ chomplen = (pending > 1 && *(e-1) == '\r') + 1;
+ }
+ }
+ if (NIL_P(str)) {
+ str = rb_str_new(p, pending - chomplen);
+ fptr->rbuf.off += pending;
+ fptr->rbuf.len -= pending;
+ }
+ else {
+ rb_str_resize(str, len + pending - chomplen);
+ read_buffered_data(RSTRING_PTR(str)+len, pending - chomplen, fptr);
+ fptr->rbuf.off += chomplen;
+ fptr->rbuf.len -= chomplen;
+ if (pending == 1 && chomplen == 1 && len > 0) {
+ if (RSTRING_PTR(str)[len-1] == '\r') {
+ rb_str_resize(str, --len);
+ break;
+ }
+ }
+ }
+ len += pending - chomplen;
+ if (cr != ENC_CODERANGE_BROKEN)
+ pos += rb_str_coderange_scan_restartable(RSTRING_PTR(str) + pos, RSTRING_PTR(str) + len, enc, &cr);
+ if (e) break;
+ }
+ READ_CHECK(fptr);
} while (io_fillbuf(fptr) >= 0);
if (NIL_P(str)) return Qnil;
str = io_enc_str(str, fptr);
ENC_CODERANGE_SET(str, cr);
fptr->lineno++;
- if (io == ARGF.current_file) {
- ARGF.lineno++;
- ARGF.last_lineno = ARGF.lineno;
- }
- else {
- ARGF.last_lineno = fptr->lineno;
- }
return str;
}
+struct getline_arg {
+ VALUE io;
+ VALUE rs;
+ long limit;
+ unsigned int chomp: 1;
+};
+
+static void
+extract_getline_opts(VALUE opts, struct getline_arg *args)
+{
+ int chomp = FALSE;
+ if (!NIL_P(opts)) {
+ static ID kwds[1];
+ VALUE vchomp;
+ if (!kwds[0]) {
+ kwds[0] = rb_intern_const("chomp");
+ }
+ rb_get_kwargs(opts, kwds, 0, -2, &vchomp);
+ chomp = (!UNDEF_P(vchomp)) && RTEST(vchomp);
+ }
+ args->chomp = chomp;
+}
+
static void
-prepare_getline_args(int argc, VALUE *argv, VALUE *rsp, long *limit, VALUE io)
+extract_getline_args(int argc, VALUE *argv, struct getline_arg *args)
{
VALUE rs = rb_rs, lim = Qnil;
- rb_io_t *fptr;
if (argc == 1) {
VALUE tmp = Qnil;
@@ -3033,107 +4130,141 @@ prepare_getline_args(int argc, VALUE *argv, VALUE *rsp, long *limit, VALUE io)
}
}
else if (2 <= argc) {
- rb_scan_args(argc, argv, "2", &rs, &lim);
+ rs = argv[0], lim = argv[1];
if (!NIL_P(rs))
StringValue(rs);
}
+ args->rs = rs;
+ args->limit = NIL_P(lim) ? -1L : NUM2LONG(lim);
+}
+
+static void
+check_getline_args(VALUE *rsp, long *limit, VALUE io)
+{
+ rb_io_t *fptr;
+ VALUE rs = *rsp;
+
if (!NIL_P(rs)) {
- rb_encoding *enc_rs, *enc_io;
-
- GetOpenFile(io, fptr);
- enc_rs = rb_enc_get(rs);
- enc_io = io_read_encoding(fptr);
- if (enc_io != enc_rs &&
- (rb_enc_str_coderange(rs) != ENC_CODERANGE_7BIT ||
- (RSTRING_LEN(rs) > 0 && !rb_enc_asciicompat(enc_io)))) {
+ rb_encoding *enc_rs, *enc_io;
+
+ GetOpenFile(io, fptr);
+ enc_rs = rb_enc_get(rs);
+ enc_io = io_read_encoding(fptr);
+ if (enc_io != enc_rs &&
+ (!is_ascii_string(rs) ||
+ (RSTRING_LEN(rs) > 0 && !rb_enc_asciicompat(enc_io)))) {
if (rs == rb_default_rs) {
rs = rb_enc_str_new(0, 0, enc_io);
rb_str_buf_cat_ascii(rs, "\n");
+ *rsp = rs;
}
else {
rb_raise(rb_eArgError, "encoding mismatch: %s IO with %s RS",
rb_enc_name(enc_io),
rb_enc_name(enc_rs));
}
- }
+ }
}
- *rsp = rs;
- *limit = NIL_P(lim) ? -1L : NUM2LONG(lim);
+}
+
+static void
+prepare_getline_args(int argc, VALUE *argv, struct getline_arg *args, VALUE io)
+{
+ VALUE opts;
+ argc = rb_scan_args(argc, argv, "02:", NULL, NULL, &opts);
+ extract_getline_args(argc, argv, args);
+ extract_getline_opts(opts, args);
+ check_getline_args(&args->rs, &args->limit, io);
}
static VALUE
-rb_io_getline_1(VALUE rs, long limit, VALUE io)
+rb_io_getline_0(VALUE rs, long limit, int chomp, rb_io_t *fptr)
{
VALUE str = Qnil;
- rb_io_t *fptr;
int nolimit = 0;
rb_encoding *enc;
- GetOpenFile(io, fptr);
rb_io_check_char_readable(fptr);
if (NIL_P(rs) && limit < 0) {
- str = read_all(fptr, 0, Qnil);
- if (RSTRING_LEN(str) == 0) return Qnil;
+ str = read_all(fptr, 0, Qnil);
+ if (RSTRING_LEN(str) == 0) return Qnil;
}
else if (limit == 0) {
- return rb_enc_str_new(0, 0, io_read_encoding(fptr));
+ return rb_enc_str_new(0, 0, io_read_encoding(fptr));
}
else if (rs == rb_default_rs && limit < 0 && !NEED_READCONV(fptr) &&
rb_enc_asciicompat(enc = io_read_encoding(fptr))) {
- NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
- return rb_io_getline_fast(fptr, enc, io);
+ NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
+ return rb_io_getline_fast(fptr, enc, chomp);
}
else {
- int c, newline = -1;
- const char *rsptr = 0;
- long rslen = 0;
- int rspara = 0;
+ int c, newline = -1;
+ const char *rsptr = 0;
+ long rslen = 0;
+ int rspara = 0;
int extra_limit = 16;
+ int chomp_cr = chomp;
- SET_BINARY_MODE(fptr);
+ SET_BINARY_MODE(fptr);
enc = io_read_encoding(fptr);
- if (!NIL_P(rs)) {
- rslen = RSTRING_LEN(rs);
- if (rslen == 0) {
- rsptr = "\n\n";
- rslen = 2;
- rspara = 1;
- swallow(fptr, '\n');
- rs = 0;
- if (!rb_enc_asciicompat(enc)) {
- rs = rb_usascii_str_new(rsptr, rslen);
- rs = rb_str_encode(rs, rb_enc_from_encoding(enc), 0, Qnil);
- OBJ_FREEZE(rs);
- rsptr = RSTRING_PTR(rs);
- rslen = RSTRING_LEN(rs);
- }
- }
- else {
- rsptr = RSTRING_PTR(rs);
- }
- newline = (unsigned char)rsptr[rslen - 1];
- }
-
- /* MS - Optimisation */
- while ((c = appendline(fptr, newline, &str, &limit)) != EOF) {
+ if (!NIL_P(rs)) {
+ rslen = RSTRING_LEN(rs);
+ if (rslen == 0) {
+ rsptr = "\n\n";
+ rslen = 2;
+ rspara = 1;
+ swallow(fptr, '\n');
+ rs = 0;
+ if (!rb_enc_asciicompat(enc)) {
+ rs = rb_usascii_str_new(rsptr, rslen);
+ rs = rb_str_conv_enc(rs, 0, enc);
+ OBJ_FREEZE(rs);
+ rsptr = RSTRING_PTR(rs);
+ rslen = RSTRING_LEN(rs);
+ }
+ newline = '\n';
+ }
+ else if (rb_enc_mbminlen(enc) == 1) {
+ rsptr = RSTRING_PTR(rs);
+ newline = (unsigned char)rsptr[rslen - 1];
+ }
+ else {
+ rs = rb_str_conv_enc(rs, 0, enc);
+ rsptr = RSTRING_PTR(rs);
+ const char *e = rsptr + rslen;
+ const char *last = rb_enc_prev_char(rsptr, e, e, enc);
+ int n;
+ newline = rb_enc_codepoint_len(last, e, &n, enc);
+ if (last + n != e) rb_raise(rb_eArgError, "broken separator");
+ }
+ chomp_cr = chomp && newline == '\n' && rslen == rb_enc_mbminlen(enc);
+ }
+
+ /* MS - Optimization */
+ while ((c = appendline(fptr, newline, &str, &limit, enc)) != EOF) {
const char *s, *p, *pp, *e;
- if (c == newline) {
- if (RSTRING_LEN(str) < rslen) continue;
- s = RSTRING_PTR(str);
+ if (c == newline) {
+ if (RSTRING_LEN(str) < rslen) continue;
+ s = RSTRING_PTR(str);
e = RSTRING_END(str);
- p = e - rslen;
- pp = rb_enc_left_char_head(s, p, e, enc);
- if (pp != p) continue;
- if (!rspara) rscheck(rsptr, rslen, rs);
- if (memcmp(p, rsptr, rslen) == 0) break;
- }
- if (limit == 0) {
- s = RSTRING_PTR(str);
- p = RSTRING_END(str);
- pp = rb_enc_left_char_head(s, p-1, p, enc);
- if (extra_limit &&
+ p = e - rslen;
+ if (!at_char_boundary(s, p, e, enc)) continue;
+ if (!rspara) rscheck(rsptr, rslen, rs);
+ if (memcmp(p, rsptr, rslen) == 0) {
+ if (chomp) {
+ if (chomp_cr && p > s && *(p-1) == '\r') --p;
+ rb_str_set_len(str, p - s);
+ }
+ break;
+ }
+ }
+ if (limit == 0) {
+ s = RSTRING_PTR(str);
+ p = RSTRING_END(str);
+ pp = rb_enc_prev_char(s, p, p, enc);
+ if (extra_limit && pp &&
MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(pp, p, enc))) {
/* relax the limit while incomplete character.
* extra_limit limits the relax length */
@@ -3144,24 +4275,40 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io)
nolimit = 1;
break;
}
- }
- }
+ }
+ }
- if (rspara && c != EOF)
- swallow(fptr, '\n');
- if (!NIL_P(str))
+ if (rspara && c != EOF)
+ swallow(fptr, '\n');
+ if (!NIL_P(str))
str = io_enc_str(str, fptr);
}
if (!NIL_P(str) && !nolimit) {
- fptr->lineno++;
- if (io == ARGF.current_file) {
- ARGF.lineno++;
- ARGF.last_lineno = ARGF.lineno;
- }
- else {
- ARGF.last_lineno = fptr->lineno;
- }
+ fptr->lineno++;
+ }
+
+ return str;
+}
+
+static VALUE
+rb_io_getline_1(VALUE rs, long limit, int chomp, VALUE io)
+{
+ rb_io_t *fptr;
+ int old_lineno, new_lineno;
+ VALUE str;
+
+ GetOpenFile(io, fptr);
+ old_lineno = fptr->lineno;
+ str = rb_io_getline_0(rs, limit, chomp, fptr);
+ if (!NIL_P(str) && (new_lineno = fptr->lineno) != old_lineno) {
+ if (io == ARGF.current_file) {
+ ARGF.lineno += new_lineno - old_lineno;
+ ARGF.last_lineno = ARGF.lineno;
+ }
+ else {
+ ARGF.last_lineno = new_lineno;
+ }
}
return str;
@@ -3170,38 +4317,102 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io)
static VALUE
rb_io_getline(int argc, VALUE *argv, VALUE io)
{
- VALUE rs;
- long limit;
+ struct getline_arg args;
- prepare_getline_args(argc, argv, &rs, &limit, io);
- return rb_io_getline_1(rs, limit, io);
+ prepare_getline_args(argc, argv, &args, io);
+ return rb_io_getline_1(args.rs, args.limit, args.chomp, io);
}
VALUE
rb_io_gets(VALUE io)
{
- return rb_io_getline_1(rb_default_rs, -1, io);
+ return rb_io_getline_1(rb_default_rs, -1, FALSE, io);
+}
+
+VALUE
+rb_io_gets_limit_internal(VALUE io, long limit)
+{
+ rb_io_t *fptr;
+ GetOpenFile(io, fptr);
+ return rb_io_getline_0(rb_default_rs, limit, FALSE, fptr);
+}
+
+VALUE
+rb_io_gets_internal(VALUE io)
+{
+ return rb_io_gets_limit_internal(io, -1);
}
/*
* call-seq:
- * ios.gets(sep=$/) -> string or nil
- * ios.gets(limit) -> string or nil
- * ios.gets(sep, limit) -> string or nil
- *
- * Reads the next ``line'' from the I/O stream; lines are separated by
- * <i>sep</i>. A separator of <code>nil</code> reads the entire
- * contents, and a zero-length separator reads the input a paragraph at
- * a time (two successive newlines in the input separate paragraphs).
- * The stream must be opened for reading or an <code>IOError</code>
- * will be raised. The line read in will be returned and also assigned
- * to <code>$_</code>. Returns <code>nil</code> if called at end of
- * file. If the first argument is an integer, or optional second
- * argument is given, the returning string would not be longer than the
- * given value in bytes.
+ * gets(sep = $/, chomp: false) -> string or nil
+ * gets(limit, chomp: false) -> string or nil
+ * gets(sep, limit, chomp: false) -> string or nil
+ *
+ * Reads and returns a line from the stream;
+ * assigns the return value to <tt>$_</tt>.
+ * See {Line IO}[rdoc-ref:IO@Line+IO].
+ *
+ * With no arguments given, returns the next line
+ * as determined by line separator <tt>$/</tt>, or +nil+ if none:
+ *
+ * f = File.open('t.txt')
+ * f.gets # => "First line\n"
+ * $_ # => "First line\n"
+ * f.gets # => "\n"
+ * f.gets # => "Fourth line\n"
+ * f.gets # => "Fifth line\n"
+ * f.gets # => nil
+ * f.close
+ *
+ * With only string argument +sep+ given,
+ * returns the next line as determined by line separator +sep+,
+ * or +nil+ if none;
+ * see {Line Separator}[rdoc-ref:IO@Line+Separator]:
+ *
+ * f = File.new('t.txt')
+ * f.gets('l') # => "First l"
+ * f.gets('li') # => "ine\nSecond li"
+ * f.gets('lin') # => "ne\n\nFourth lin"
+ * f.gets # => "e\n"
+ * f.close
+ *
+ * The two special values for +sep+ are honored:
+ *
+ * f = File.new('t.txt')
+ * # Get all.
+ * f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * f.rewind
+ * # Get paragraph (up to two line separators).
+ * f.gets('') # => "First line\nSecond line\n\n"
+ * f.close
+ *
+ * With only integer argument +limit+ given,
+ * limits the number of bytes in the line;
+ * see {Line Limit}[rdoc-ref:IO@Line+Limit]:
+ *
+ * # No more than one line.
+ * File.open('t.txt') {|f| f.gets(10) } # => "First line"
+ * File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
+ * File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
+ *
+ * With arguments +sep+ and +limit+ given,
+ * combines the two behaviors
+ * (see {Line Separator and Line Limit}[rdoc-ref:IO@Line+Separator+and+Line+Limit]).
+ *
+ * Optional keyword argument +chomp+ specifies whether line separators
+ * are to be omitted:
+ *
+ * f = File.open('t.txt')
+ * # Chomp the lines.
+ * f.gets(chomp: true) # => "First line"
+ * f.gets(chomp: true) # => "Second line"
+ * f.gets(chomp: true) # => ""
+ * f.gets(chomp: true) # => "Fourth line"
+ * f.gets(chomp: true) # => "Fifth line"
+ * f.gets(chomp: true) # => nil
+ * f.close
*
- * File.new("testfile").gets #=> "This is line one\n"
- * $_ #=> "This is line one\n"
*/
static VALUE
@@ -3217,24 +4428,11 @@ rb_io_gets_m(int argc, VALUE *argv, VALUE io)
/*
* call-seq:
- * ios.lineno -> integer
+ * lineno -> integer
*
- * Returns the current line number in <em>ios</em>. The stream must be
- * opened for reading. <code>lineno</code> counts the number of times
- * #gets is called rather than the number of newlines encountered. The two
- * values will differ if #gets is called with a separator other than newline.
+ * Returns the current line number for the stream;
+ * see {Line Number}[rdoc-ref:IO@Line+Number].
*
- * Methods that use <code>$/</code> like #each, #lines and #readline will
- * also increment <code>lineno</code>.
- *
- * See also the <code>$.</code> variable.
- *
- * f = File.new("testfile")
- * f.lineno #=> 0
- * f.gets #=> "This is line one\n"
- * f.lineno #=> 1
- * f.gets #=> "This is line two\n"
- * f.lineno #=> 2
*/
static VALUE
@@ -3249,19 +4447,11 @@ rb_io_lineno(VALUE io)
/*
* call-seq:
- * ios.lineno = integer -> integer
+ * lineno = integer -> integer
*
- * Manually sets the current line number to the given value.
- * <code>$.</code> is updated only on the next read.
+ * Sets and returns the line number for the stream;
+ * see {Line Number}[rdoc-ref:IO@Line+Number].
*
- * f = File.new("testfile")
- * f.gets #=> "This is line one\n"
- * $. #=> 1
- * f.lineno = 1000
- * f.lineno #=> 1000
- * $. #=> 1 # lineno of last read
- * f.gets #=> "This is line two\n"
- * $. #=> 1001 # lineno of last read
*/
static VALUE
@@ -3275,134 +4465,273 @@ rb_io_set_lineno(VALUE io, VALUE lineno)
return lineno;
}
-/*
- * call-seq:
- * ios.readline(sep=$/) -> string
- * ios.readline(limit) -> string
- * ios.readline(sep, limit) -> string
- *
- * Reads a line as with <code>IO#gets</code>, but raises an
- * <code>EOFError</code> on end of file.
- */
-
+/* :nodoc: */
static VALUE
-rb_io_readline(int argc, VALUE *argv, VALUE io)
+io_readline(rb_execution_context_t *ec, VALUE io, VALUE sep, VALUE lim, VALUE chomp)
{
- VALUE line = rb_io_gets_m(argc, argv, io);
+ long limit = -1;
+ if (NIL_P(lim)) {
+ VALUE tmp = Qnil;
+ // If sep is specified, but it's not a string and not nil, then assume
+ // it's the limit (it should be an integer)
+ if (!NIL_P(sep) && NIL_P(tmp = rb_check_string_type(sep))) {
+ // If the user has specified a non-nil / non-string value
+ // for the separator, we assume it's the limit and set the
+ // separator to default: rb_rs.
+ lim = sep;
+ limit = NUM2LONG(lim);
+ sep = rb_rs;
+ }
+ else {
+ sep = tmp;
+ }
+ }
+ else {
+ if (!NIL_P(sep)) StringValue(sep);
+ limit = NUM2LONG(lim);
+ }
+
+ check_getline_args(&sep, &limit, io);
+
+ VALUE line = rb_io_getline_1(sep, limit, RTEST(chomp), io);
+ rb_lastline_set_up(line, 1);
if (NIL_P(line)) {
- rb_eof_error();
+ rb_eof_error();
}
return line;
}
+static VALUE io_readlines(const struct getline_arg *arg, VALUE io);
+
/*
* call-seq:
- * ios.readlines(sep=$/) -> array
- * ios.readlines(limit) -> array
- * ios.readlines(sep, limit) -> array
+ * readlines(sep = $/, chomp: false) -> array
+ * readlines(limit, chomp: false) -> array
+ * readlines(sep, limit, chomp: false) -> array
+ *
+ * Reads and returns all remaining line from the stream;
+ * does not modify <tt>$_</tt>.
+ * See {Line IO}[rdoc-ref:IO@Line+IO].
+ *
+ * With no arguments given, returns lines
+ * as determined by line separator <tt>$/</tt>, or +nil+ if none:
+ *
+ * f = File.new('t.txt')
+ * f.readlines
+ * # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
+ * f.readlines # => []
+ * f.close
+ *
+ * With only string argument +sep+ given,
+ * returns lines as determined by line separator +sep+,
+ * or +nil+ if none;
+ * see {Line Separator}[rdoc-ref:IO@Line+Separator]:
+ *
+ * f = File.new('t.txt')
+ * f.readlines('li')
+ * # => ["First li", "ne\nSecond li", "ne\n\nFourth li", "ne\nFifth li", "ne\n"]
+ * f.close
*
- * Reads all of the lines in <em>ios</em>, and returns them in
- * <i>anArray</i>. Lines are separated by the optional <i>sep</i>. If
- * <i>sep</i> is <code>nil</code>, the rest of the stream is returned
- * as a single record. If the first argument is an integer, or
- * optional second argument is given, the returning string would not be
- * longer than the given value in bytes. The stream must be opened for
- * reading or an <code>IOError</code> will be raised.
+ * The two special values for +sep+ are honored:
+ *
+ * f = File.new('t.txt')
+ * # Get all into one string.
+ * f.readlines(nil)
+ * # => ["First line\nSecond line\n\nFourth line\nFifth line\n"]
+ * # Get paragraphs (up to two line separators).
+ * f.rewind
+ * f.readlines('')
+ * # => ["First line\nSecond line\n\n", "Fourth line\nFifth line\n"]
+ * f.close
+ *
+ * With only integer argument +limit+ given,
+ * limits the number of bytes in each line;
+ * see {Line Limit}[rdoc-ref:IO@Line+Limit]:
+ *
+ * f = File.new('t.txt')
+ * f.readlines(8)
+ * # => ["First li", "ne\n", "Second l", "ine\n", "\n", "Fourth l", "ine\n", "Fifth li", "ne\n"]
+ * f.close
+ *
+ * With arguments +sep+ and +limit+ given,
+ * combines the two behaviors
+ * (see {Line Separator and Line Limit}[rdoc-ref:IO@Line+Separator+and+Line+Limit]).
+ *
+ * Optional keyword argument +chomp+ specifies whether line separators
+ * are to be omitted:
+ *
+ * f = File.new('t.txt')
+ * f.readlines(chomp: true)
+ * # => ["First line", "Second line", "", "Fourth line", "Fifth line"]
+ * f.close
*
- * f = File.new("testfile")
- * f.readlines[0] #=> "This is line one\n"
*/
static VALUE
rb_io_readlines(int argc, VALUE *argv, VALUE io)
{
- VALUE line, ary, rs;
- long limit;
+ struct getline_arg args;
+
+ prepare_getline_args(argc, argv, &args, io);
+ return io_readlines(&args, io);
+}
+
+static VALUE
+io_readlines(const struct getline_arg *arg, VALUE io)
+{
+ VALUE line, ary;
- prepare_getline_args(argc, argv, &rs, &limit, io);
- if (limit == 0)
- rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
+ if (arg->limit == 0)
+ rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
ary = rb_ary_new();
- while (!NIL_P(line = rb_io_getline_1(rs, limit, io))) {
- rb_ary_push(ary, line);
+ while (!NIL_P(line = rb_io_getline_1(arg->rs, arg->limit, arg->chomp, io))) {
+ rb_ary_push(ary, line);
}
return ary;
}
/*
* call-seq:
- * ios.each(sep=$/) {|line| block } -> ios
- * ios.each(limit) {|line| block } -> ios
- * ios.each(sep,limit) {|line| block } -> ios
- * ios.each(...) -> an_enumerator
+ * each_line(sep = $/, chomp: false) {|line| ... } -> self
+ * each_line(limit, chomp: false) {|line| ... } -> self
+ * each_line(sep, limit, chomp: false) {|line| ... } -> self
+ * each_line -> enumerator
*
- * ios.each_line(sep=$/) {|line| block } -> ios
- * ios.each_line(limit) {|line| block } -> ios
- * ios.each_line(sep,limit) {|line| block } -> ios
- * ios.each_line(...) -> an_enumerator
+ * Calls the block with each remaining line read from the stream;
+ * returns +self+.
+ * Does nothing if already at end-of-stream;
+ * See {Line IO}[rdoc-ref:IO@Line+IO].
*
- * Executes the block for every line in <em>ios</em>, where lines are
- * separated by <i>sep</i>. <em>ios</em> must be opened for
- * reading or an <code>IOError</code> will be raised.
+ * With no arguments given, reads lines
+ * as determined by line separator <tt>$/</tt>:
*
- * If no block is given, an enumerator is returned instead.
+ * f = File.new('t.txt')
+ * f.each_line {|line| p line }
+ * f.each_line {|line| fail 'Cannot happen' }
+ * f.close
*
- * f = File.new("testfile")
- * f.each {|line| puts "#{f.lineno}: #{line}" }
+ * Output:
*
- * <em>produces:</em>
+ * "First line\n"
+ * "Second line\n"
+ * "\n"
+ * "Fourth line\n"
+ * "Fifth line\n"
+ *
+ * With only string argument +sep+ given,
+ * reads lines as determined by line separator +sep+;
+ * see {Line Separator}[rdoc-ref:IO@Line+Separator]:
+ *
+ * f = File.new('t.txt')
+ * f.each_line('li') {|line| p line }
+ * f.close
+ *
+ * Output:
+ *
+ * "First li"
+ * "ne\nSecond li"
+ * "ne\n\nFourth li"
+ * "ne\nFifth li"
+ * "ne\n"
+ *
+ * The two special values for +sep+ are honored:
+ *
+ * f = File.new('t.txt')
+ * # Get all into one string.
+ * f.each_line(nil) {|line| p line }
+ * f.close
+ *
+ * Output:
+ *
+ * "First line\nSecond line\n\nFourth line\nFifth line\n"
+ *
+ * f.rewind
+ * # Get paragraphs (up to two line separators).
+ * f.each_line('') {|line| p line }
+ *
+ * Output:
*
- * 1: This is line one
- * 2: This is line two
- * 3: This is line three
- * 4: And so on...
+ * "First line\nSecond line\n\n"
+ * "Fourth line\nFifth line\n"
+ *
+ * With only integer argument +limit+ given,
+ * limits the number of bytes in each line;
+ * see {Line Limit}[rdoc-ref:IO@Line+Limit]:
+ *
+ * f = File.new('t.txt')
+ * f.each_line(8) {|line| p line }
+ * f.close
+ *
+ * Output:
+ *
+ * "First li"
+ * "ne\n"
+ * "Second l"
+ * "ine\n"
+ * "\n"
+ * "Fourth l"
+ * "ine\n"
+ * "Fifth li"
+ * "ne\n"
+ *
+ * With arguments +sep+ and +limit+ given,
+ * combines the two behaviors
+ * (see {Line Separator and Line Limit}[rdoc-ref:IO@Line+Separator+and+Line+Limit]).
+ *
+ * Optional keyword argument +chomp+ specifies whether line separators
+ * are to be omitted:
+ *
+ * f = File.new('t.txt')
+ * f.each_line(chomp: true) {|line| p line }
+ * f.close
+ *
+ * Output:
+ *
+ * "First line"
+ * "Second line"
+ * ""
+ * "Fourth line"
+ * "Fifth line"
+ *
+ * Returns an Enumerator if no block is given.
*/
static VALUE
rb_io_each_line(int argc, VALUE *argv, VALUE io)
{
- VALUE str, rs;
- long limit;
+ VALUE str;
+ struct getline_arg args;
RETURN_ENUMERATOR(io, argc, argv);
- prepare_getline_args(argc, argv, &rs, &limit, io);
- if (limit == 0)
- rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
- while (!NIL_P(str = rb_io_getline_1(rs, limit, io))) {
- rb_yield(str);
+ prepare_getline_args(argc, argv, &args, io);
+ if (args.limit == 0)
+ rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
+ while (!NIL_P(str = rb_io_getline_1(args.rs, args.limit, args.chomp, io))) {
+ rb_yield(str);
}
return io;
}
/*
- * This is a deprecated alias for <code>each_line</code>.
- */
-
-static VALUE
-rb_io_lines(int argc, VALUE *argv, VALUE io)
-{
- rb_warn("IO#lines is deprecated; use #each_line instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(io, ID2SYM(rb_intern("each_line")), argc, argv);
- return rb_io_each_line(argc, argv, io);
-}
-
-/*
* call-seq:
- * ios.each_byte {|byte| block } -> ios
- * ios.each_byte -> an_enumerator
+ * each_byte {|byte| ... } -> self
+ * each_byte -> enumerator
*
- * Calls the given block once for each byte (0..255) in <em>ios</em>,
- * passing the byte as an argument. The stream must be opened for
- * reading or an <code>IOError</code> will be raised.
+ * Calls the given block with each byte (0..255) in the stream; returns +self+.
+ * See {Byte IO}[rdoc-ref:IO@Byte+IO].
*
- * If no block is given, an enumerator is returned instead.
+ * File.read('t.ja') # => "こんにちは"
+ * f = File.new('t.ja')
+ * a = []
+ * f.each_byte {|b| a << b }
+ * a # => [227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175]
+ * f.close
+ *
+ * Returns an Enumerator if no block is given.
+ *
+ * Related: IO#each_char, IO#each_codepoint.
*
- * f = File.new("testfile")
- * checksum = 0
- * f.each_byte {|x| checksum ^= x } #=> #<File:testfile>
- * checksum #=> 12
*/
static VALUE
@@ -3414,31 +4743,18 @@ rb_io_each_byte(VALUE io)
GetOpenFile(io, fptr);
do {
- while (fptr->rbuf.len > 0) {
- char *p = fptr->rbuf.ptr + fptr->rbuf.off++;
- fptr->rbuf.len--;
- rb_yield(INT2FIX(*p & 0xff));
- errno = 0;
- }
- rb_io_check_byte_readable(fptr);
- READ_CHECK(fptr);
+ while (fptr->rbuf.len > 0) {
+ char *p = fptr->rbuf.ptr + fptr->rbuf.off++;
+ fptr->rbuf.len--;
+ rb_yield(INT2FIX(*p & 0xff));
+ rb_io_check_byte_readable(fptr);
+ errno = 0;
+ }
+ READ_CHECK(fptr);
} while (io_fillbuf(fptr) >= 0);
return io;
}
-/*
- * This is a deprecated alias for <code>each_byte</code>.
- */
-
-static VALUE
-rb_io_bytes(VALUE io)
-{
- rb_warn("IO#bytes is deprecated; use #each_byte instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(io, ID2SYM(rb_intern("each_byte")), 0, 0);
- return rb_io_each_byte(io);
-}
-
static VALUE
io_getc(rb_io_t *fptr, rb_encoding *enc)
{
@@ -3446,17 +4762,17 @@ io_getc(rb_io_t *fptr, rb_encoding *enc)
VALUE str;
if (NEED_READCONV(fptr)) {
- VALUE str = Qnil;
- rb_encoding *read_enc = io_read_encoding(fptr);
+ rb_encoding *read_enc = io_read_encoding(fptr);
- SET_BINARY_MODE(fptr);
+ str = Qnil;
+ SET_BINARY_MODE(fptr);
make_readconv(fptr, 0);
while (1) {
if (fptr->cbuf.len) {
- r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
- fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
- read_enc);
+ r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
+ fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
+ read_enc);
if (!MBCLEN_NEEDMORE_P(r))
break;
if (fptr->cbuf.len == fptr->cbuf.capa) {
@@ -3466,16 +4782,16 @@ io_getc(rb_io_t *fptr, rb_encoding *enc)
if (more_char(fptr) == MORE_CHAR_FINISHED) {
if (fptr->cbuf.len == 0) {
- clear_readconv(fptr);
- return Qnil;
- }
+ clear_readconv(fptr);
+ return Qnil;
+ }
/* return an unit of an incomplete character just before EOF */
- str = rb_enc_str_new(fptr->cbuf.ptr+fptr->cbuf.off, 1, read_enc);
- fptr->cbuf.off += 1;
- fptr->cbuf.len -= 1;
+ str = rb_enc_str_new(fptr->cbuf.ptr+fptr->cbuf.off, 1, read_enc);
+ fptr->cbuf.off += 1;
+ fptr->cbuf.len -= 1;
if (fptr->cbuf.len == 0) clear_readconv(fptr);
- ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
- return str;
+ ENC_CODERANGE_SET(str, ENC_CODERANGE_BROKEN);
+ return str;
}
}
if (MBCLEN_INVALID_P(r)) {
@@ -3483,62 +4799,62 @@ io_getc(rb_io_t *fptr, rb_encoding *enc)
fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
read_enc);
io_shift_cbuf(fptr, r, &str);
- cr = ENC_CODERANGE_BROKEN;
- }
- else {
- io_shift_cbuf(fptr, MBCLEN_CHARFOUND_LEN(r), &str);
- cr = ENC_CODERANGE_VALID;
- if (MBCLEN_CHARFOUND_LEN(r) == 1 && rb_enc_asciicompat(read_enc) &&
- ISASCII(RSTRING_PTR(str)[0])) {
- cr = ENC_CODERANGE_7BIT;
- }
- }
- str = io_enc_str(str, fptr);
- ENC_CODERANGE_SET(str, cr);
- return str;
+ cr = ENC_CODERANGE_BROKEN;
+ }
+ else {
+ io_shift_cbuf(fptr, MBCLEN_CHARFOUND_LEN(r), &str);
+ cr = ENC_CODERANGE_VALID;
+ if (MBCLEN_CHARFOUND_LEN(r) == 1 && rb_enc_asciicompat(read_enc) &&
+ ISASCII(RSTRING_PTR(str)[0])) {
+ cr = ENC_CODERANGE_7BIT;
+ }
+ }
+ str = io_enc_str(str, fptr);
+ ENC_CODERANGE_SET(str, cr);
+ return str;
}
NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
if (io_fillbuf(fptr) < 0) {
- return Qnil;
+ return Qnil;
}
if (rb_enc_asciicompat(enc) && ISASCII(fptr->rbuf.ptr[fptr->rbuf.off])) {
- str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
- fptr->rbuf.off += 1;
- fptr->rbuf.len -= 1;
- cr = ENC_CODERANGE_7BIT;
+ str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
+ fptr->rbuf.off += 1;
+ fptr->rbuf.len -= 1;
+ cr = ENC_CODERANGE_7BIT;
}
else {
- r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
- if (MBCLEN_CHARFOUND_P(r) &&
- (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
- str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, n);
- fptr->rbuf.off += n;
- fptr->rbuf.len -= n;
- cr = ENC_CODERANGE_VALID;
- }
- else if (MBCLEN_NEEDMORE_P(r)) {
- str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.len);
- fptr->rbuf.len = 0;
- getc_needmore:
- if (io_fillbuf(fptr) != -1) {
- rb_str_cat(str, fptr->rbuf.ptr+fptr->rbuf.off, 1);
- fptr->rbuf.off++;
- fptr->rbuf.len--;
- r = rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_PTR(str)+RSTRING_LEN(str), enc);
- if (MBCLEN_NEEDMORE_P(r)) {
- goto getc_needmore;
- }
- else if (MBCLEN_CHARFOUND_P(r)) {
- cr = ENC_CODERANGE_VALID;
- }
- }
- }
- else {
- str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
- fptr->rbuf.off++;
- fptr->rbuf.len--;
- }
+ r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
+ if (MBCLEN_CHARFOUND_P(r) &&
+ (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
+ str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, n);
+ fptr->rbuf.off += n;
+ fptr->rbuf.len -= n;
+ cr = ENC_CODERANGE_VALID;
+ }
+ else if (MBCLEN_NEEDMORE_P(r)) {
+ str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, fptr->rbuf.len);
+ fptr->rbuf.len = 0;
+ getc_needmore:
+ if (io_fillbuf(fptr) != -1) {
+ rb_str_cat(str, fptr->rbuf.ptr+fptr->rbuf.off, 1);
+ fptr->rbuf.off++;
+ fptr->rbuf.len--;
+ r = rb_enc_precise_mbclen(RSTRING_PTR(str), RSTRING_PTR(str)+RSTRING_LEN(str), enc);
+ if (MBCLEN_NEEDMORE_P(r)) {
+ goto getc_needmore;
+ }
+ else if (MBCLEN_CHARFOUND_P(r)) {
+ cr = ENC_CODERANGE_VALID;
+ }
+ }
+ }
+ else {
+ str = rb_str_new(fptr->rbuf.ptr+fptr->rbuf.off, 1);
+ fptr->rbuf.off++;
+ fptr->rbuf.len--;
+ }
}
if (!cr) cr = ENC_CODERANGE_BROKEN;
str = io_enc_str(str, fptr);
@@ -3548,17 +4864,23 @@ io_getc(rb_io_t *fptr, rb_encoding *enc)
/*
* call-seq:
- * ios.each_char {|c| block } -> ios
- * ios.each_char -> an_enumerator
+ * each_char {|c| ... } -> self
+ * each_char -> enumerator
*
- * Calls the given block once for each character in <em>ios</em>,
- * passing the character as an argument. The stream must be opened for
- * reading or an <code>IOError</code> will be raised.
+ * Calls the given block with each character in the stream; returns +self+.
+ * See {Character IO}[rdoc-ref:IO@Character+IO].
*
- * If no block is given, an enumerator is returned instead.
+ * File.read('t.ja') # => "こんにちは"
+ * f = File.new('t.ja')
+ * a = []
+ * f.each_char {|c| a << c.ord }
+ * a # => [12371, 12435, 12395, 12385, 12399]
+ * f.close
+ *
+ * Returns an Enumerator if no block is given.
+ *
+ * Related: IO#each_byte, IO#each_codepoint.
*
- * f = File.new("testfile")
- * f.each_char {|c| print c, ' ' } #=> #<File:testfile>
*/
static VALUE
@@ -3581,31 +4903,22 @@ rb_io_each_char(VALUE io)
}
/*
- * This is a deprecated alias for <code>each_char</code>.
- */
-
-static VALUE
-rb_io_chars(VALUE io)
-{
- rb_warn("IO#chars is deprecated; use #each_char instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(io, ID2SYM(rb_intern("each_char")), 0, 0);
- return rb_io_each_char(io);
-}
-
-
-/*
* call-seq:
- * ios.each_codepoint {|c| block } -> ios
- * ios.codepoints {|c| block } -> ios
- * ios.each_codepoint -> an_enumerator
- * ios.codepoints -> an_enumerator
+ * each_codepoint {|c| ... } -> self
+ * each_codepoint -> enumerator
*
- * Passes the <code>Integer</code> ordinal of each character in <i>ios</i>,
- * passing the codepoint as an argument. The stream must be opened for
- * reading or an <code>IOError</code> will be raised.
+ * Calls the given block with each codepoint in the stream; returns +self+:
*
- * If no block is given, an enumerator is returned instead.
+ * File.read('t.ja') # => "こんにちは"
+ * f = File.new('t.ja')
+ * a = []
+ * f.each_codepoint {|c| a << c }
+ * a # => [12371, 12435, 12395, 12385, 12399]
+ * f.close
+ *
+ * Returns an Enumerator if no block is given.
+ *
+ * Related: IO#each_byte, IO#each_char.
*
*/
@@ -3622,95 +4935,105 @@ rb_io_each_codepoint(VALUE io)
rb_io_check_char_readable(fptr);
READ_CHECK(fptr);
+ enc = io_read_encoding(fptr);
if (NEED_READCONV(fptr)) {
- SET_BINARY_MODE(fptr);
- for (;;) {
- make_readconv(fptr, 0);
- for (;;) {
- if (fptr->cbuf.len) {
- if (fptr->encs.enc)
- r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
- fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
- fptr->encs.enc);
- else
- r = ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(1);
- if (!MBCLEN_NEEDMORE_P(r))
- break;
- if (fptr->cbuf.len == fptr->cbuf.capa) {
- rb_raise(rb_eIOError, "too long character");
- }
- }
- if (more_char(fptr) == MORE_CHAR_FINISHED) {
+ SET_BINARY_MODE(fptr);
+ r = 1; /* no invalid char yet */
+ for (;;) {
+ make_readconv(fptr, 0);
+ for (;;) {
+ if (fptr->cbuf.len) {
+ r = rb_enc_precise_mbclen(fptr->cbuf.ptr+fptr->cbuf.off,
+ fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
+ enc);
+ if (!MBCLEN_NEEDMORE_P(r))
+ break;
+ if (fptr->cbuf.len == fptr->cbuf.capa) {
+ rb_raise(rb_eIOError, "too long character");
+ }
+ }
+ if (more_char(fptr) == MORE_CHAR_FINISHED) {
clear_readconv(fptr);
- /* ignore an incomplete character before EOF */
- return io;
- }
- }
- if (MBCLEN_INVALID_P(r)) {
- rb_raise(rb_eArgError, "invalid byte sequence in %s",
- rb_enc_name(fptr->encs.enc));
- }
- n = MBCLEN_CHARFOUND_LEN(r);
- if (fptr->encs.enc) {
- c = rb_enc_codepoint(fptr->cbuf.ptr+fptr->cbuf.off,
- fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
- fptr->encs.enc);
- }
- else {
- c = (unsigned char)fptr->cbuf.ptr[fptr->cbuf.off];
- }
- fptr->cbuf.off += n;
- fptr->cbuf.len -= n;
- rb_yield(UINT2NUM(c));
- }
+ if (!MBCLEN_CHARFOUND_P(r)) {
+ goto invalid;
+ }
+ return io;
+ }
+ }
+ if (MBCLEN_INVALID_P(r)) {
+ goto invalid;
+ }
+ n = MBCLEN_CHARFOUND_LEN(r);
+ c = rb_enc_codepoint(fptr->cbuf.ptr+fptr->cbuf.off,
+ fptr->cbuf.ptr+fptr->cbuf.off+fptr->cbuf.len,
+ enc);
+ fptr->cbuf.off += n;
+ fptr->cbuf.len -= n;
+ rb_yield(UINT2NUM(c));
+ rb_io_check_char_readable(fptr);
+ }
}
NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
- enc = io_input_encoding(fptr);
while (io_fillbuf(fptr) >= 0) {
- r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off,
- fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
- if (MBCLEN_CHARFOUND_P(r) &&
- (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
- c = rb_enc_codepoint(fptr->rbuf.ptr+fptr->rbuf.off,
- fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
- fptr->rbuf.off += n;
- fptr->rbuf.len -= n;
- rb_yield(UINT2NUM(c));
- }
- else if (MBCLEN_INVALID_P(r)) {
- rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
- }
- else {
- continue;
- }
+ r = rb_enc_precise_mbclen(fptr->rbuf.ptr+fptr->rbuf.off,
+ fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
+ if (MBCLEN_CHARFOUND_P(r) &&
+ (n = MBCLEN_CHARFOUND_LEN(r)) <= fptr->rbuf.len) {
+ c = rb_enc_codepoint(fptr->rbuf.ptr+fptr->rbuf.off,
+ fptr->rbuf.ptr+fptr->rbuf.off+fptr->rbuf.len, enc);
+ fptr->rbuf.off += n;
+ fptr->rbuf.len -= n;
+ rb_yield(UINT2NUM(c));
+ }
+ else if (MBCLEN_INVALID_P(r)) {
+ goto invalid;
+ }
+ else if (MBCLEN_NEEDMORE_P(r)) {
+ char cbuf[8], *p = cbuf;
+ int more = MBCLEN_NEEDMORE_LEN(r);
+ if (more > numberof(cbuf)) goto invalid;
+ more += n = fptr->rbuf.len;
+ if (more > numberof(cbuf)) goto invalid;
+ while ((n = (int)read_buffered_data(p, more, fptr)) > 0 &&
+ (p += n, (more -= n) > 0)) {
+ if (io_fillbuf(fptr) < 0) goto invalid;
+ if ((n = fptr->rbuf.len) > more) n = more;
+ }
+ r = rb_enc_precise_mbclen(cbuf, p, enc);
+ if (!MBCLEN_CHARFOUND_P(r)) goto invalid;
+ c = rb_enc_codepoint(cbuf, p, enc);
+ rb_yield(UINT2NUM(c));
+ }
+ else {
+ continue;
+ }
+ rb_io_check_byte_readable(fptr);
}
return io;
-}
-
-/*
- * This is a deprecated alias for <code>each_codepoint</code>.
- */
-static VALUE
-rb_io_codepoints(VALUE io)
-{
- rb_warn("IO#codepoints is deprecated; use #each_codepoint instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(io, ID2SYM(rb_intern("each_codepoint")), 0, 0);
- return rb_io_each_codepoint(io);
+ invalid:
+ rb_raise(rb_eArgError, "invalid byte sequence in %s", rb_enc_name(enc));
+ UNREACHABLE_RETURN(Qundef);
}
-
/*
* call-seq:
- * ios.getc -> string or nil
+ * getc -> character or nil
+ *
+ * Reads and returns the next 1-character string from the stream;
+ * returns +nil+ if already at end-of-stream.
+ * See {Character IO}[rdoc-ref:IO@Character+IO].
*
- * Reads a one-character string from <em>ios</em>. Returns
- * <code>nil</code> if called at end of file.
+ * f = File.open('t.txt')
+ * f.getc # => "F"
+ * f.close
+ * File.read('t.ja') # => "こんにちは"
+ * f = File.open('t.ja')
+ * f.getc.ord # => 12371
+ * f.close
+ *
+ * Related: IO#readchar (may raise EOFError).
*
- * f = File.new("testfile")
- * f.getc #=> "h"
- * f.getc #=> "e"
*/
static VALUE
@@ -3729,14 +5052,22 @@ rb_io_getc(VALUE io)
/*
* call-seq:
- * ios.readchar -> string
+ * readchar -> string
+ *
+ * Reads and returns the next 1-character string from the stream;
+ * raises EOFError if already at end-of-stream.
+ * See {Character IO}[rdoc-ref:IO@Character+IO].
+ *
+ * f = File.open('t.txt')
+ * f.readchar # => "F"
+ * f.close
+ * File.read('t.ja') # => "こんにちは"
+ * f = File.open('t.ja')
+ * f.readchar.ord # => 12371
+ * f.close
*
- * Reads a one-character string from <em>ios</em>. Raises an
- * <code>EOFError</code> on end of file.
+ * Related: IO#getc (will not raise EOFError).
*
- * f = File.new("testfile")
- * f.readchar #=> "h"
- * f.readchar #=> "e"
*/
static VALUE
@@ -3745,21 +5076,28 @@ rb_io_readchar(VALUE io)
VALUE c = rb_io_getc(io);
if (NIL_P(c)) {
- rb_eof_error();
+ rb_eof_error();
}
return c;
}
/*
* call-seq:
- * ios.getbyte -> fixnum or nil
+ * getbyte -> integer or nil
+ *
+ * Reads and returns the next byte (in range 0..255) from the stream;
+ * returns +nil+ if already at end-of-stream.
+ * See {Byte IO}[rdoc-ref:IO@Byte+IO].
*
- * Gets the next 8-bit byte (0..255) from <em>ios</em>. Returns
- * <code>nil</code> if called at end of file.
+ * f = File.open('t.txt')
+ * f.getbyte # => 70
+ * f.close
+ * File.read('t.ja') # => "こんにちは"
+ * f = File.open('t.ja')
+ * f.getbyte # => 227
+ * f.close
*
- * f = File.new("testfile")
- * f.getbyte #=> 84
- * f.getbyte #=> 104
+ * Related: IO#readbyte (may raise EOFError).
*/
VALUE
@@ -3771,15 +5109,16 @@ rb_io_getbyte(VALUE io)
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
READ_CHECK(fptr);
- if (fptr->fd == 0 && (fptr->mode & FMODE_TTY) && RB_TYPE_P(rb_stdout, T_FILE)) {
+ VALUE r_stdout = rb_ractor_stdout();
+ if (fptr->fd == 0 && (fptr->mode & FMODE_TTY) && RB_TYPE_P(r_stdout, T_FILE)) {
rb_io_t *ofp;
- GetOpenFile(rb_stdout, ofp);
+ GetOpenFile(r_stdout, ofp);
if (ofp->mode & FMODE_TTY) {
- rb_io_flush(rb_stdout);
+ rb_io_flush(r_stdout);
}
}
if (io_fillbuf(fptr) < 0) {
- return Qnil;
+ return Qnil;
}
fptr->rbuf.off++;
fptr->rbuf.len--;
@@ -3789,10 +5128,22 @@ rb_io_getbyte(VALUE io)
/*
* call-seq:
- * ios.readbyte -> fixnum
+ * readbyte -> integer
+ *
+ * Reads and returns the next byte (in range 0..255) from the stream;
+ * raises EOFError if already at end-of-stream.
+ * See {Byte IO}[rdoc-ref:IO@Byte+IO].
+ *
+ * f = File.open('t.txt')
+ * f.readbyte # => 70
+ * f.close
+ * File.read('t.ja') # => "こんにちは"
+ * f = File.open('t.ja')
+ * f.readbyte # => 227
+ * f.close
+ *
+ * Related: IO#getbyte (will not raise EOFError).
*
- * Reads a byte as with <code>IO#getbyte</code>, but raises an
- * <code>EOFError</code> on end of file.
*/
static VALUE
@@ -3801,26 +5152,47 @@ rb_io_readbyte(VALUE io)
VALUE c = rb_io_getbyte(io);
if (NIL_P(c)) {
- rb_eof_error();
+ rb_eof_error();
}
return c;
}
/*
* call-seq:
- * ios.ungetbyte(string) -> nil
- * ios.ungetbyte(integer) -> nil
+ * ungetbyte(integer) -> nil
+ * ungetbyte(string) -> nil
+ *
+ * Pushes back ("unshifts") the given data onto the stream's buffer,
+ * placing the data so that it is next to be read; returns +nil+.
+ * See {Byte IO}[rdoc-ref:IO@Byte+IO].
+ *
+ * Note that:
*
- * Pushes back bytes (passed as a parameter) onto <em>ios</em>,
- * such that a subsequent buffered read will return it. Only one byte
- * may be pushed back before a subsequent read operation (that is,
- * you will be able to read only the last of several bytes that have been pushed
- * back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
+ * - Calling the method has no effect with unbuffered reads (such as IO#sysread).
+ * - Calling #rewind on the stream discards the pushed-back data.
+ *
+ * When argument +integer+ is given, uses only its low-order byte:
+ *
+ * File.write('t.tmp', '012')
+ * f = File.open('t.tmp')
+ * f.ungetbyte(0x41) # => nil
+ * f.read # => "A012"
+ * f.rewind
+ * f.ungetbyte(0x4243) # => nil
+ * f.read # => "C012"
+ * f.close
+ *
+ * When argument +string+ is given, uses all bytes:
+ *
+ * File.write('t.tmp', '012')
+ * f = File.open('t.tmp')
+ * f.ungetbyte('A') # => nil
+ * f.read # => "A012"
+ * f.rewind
+ * f.ungetbyte('BCDE') # => nil
+ * f.read # => "BCDE012"
+ * f.close
*
- * f = File.new("testfile") #=> #<File:testfile>
- * b = f.getbyte #=> 0x38
- * f.ungetbyte(b) #=> nil
- * f.getbyte #=> 0x38
*/
VALUE
@@ -3830,13 +5202,17 @@ rb_io_ungetbyte(VALUE io, VALUE b)
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
- if (NIL_P(b)) return Qnil;
- if (FIXNUM_P(b)) {
- char cc = FIX2INT(b);
- b = rb_str_new(&cc, 1);
- }
- else {
- SafeStringValue(b);
+ switch (TYPE(b)) {
+ case T_NIL:
+ return Qnil;
+ case T_FIXNUM:
+ case T_BIGNUM: ;
+ VALUE v = rb_int_modulo(b, INT2FIX(256));
+ unsigned char c = NUM2INT(v) & 0xFF;
+ b = rb_str_new((const char *)&c, 1);
+ break;
+ default:
+ StringValue(b);
}
io_ungetbyte(b, fptr);
return Qnil;
@@ -3844,18 +5220,43 @@ rb_io_ungetbyte(VALUE io, VALUE b)
/*
* call-seq:
- * ios.ungetc(string) -> nil
+ * ungetc(integer) -> nil
+ * ungetc(string) -> nil
+ *
+ * Pushes back ("unshifts") the given data onto the stream's buffer,
+ * placing the data so that it is next to be read; returns +nil+.
+ * See {Character IO}[rdoc-ref:IO@Character+IO].
+ *
+ * Note that:
+ *
+ * - Calling the method has no effect with unbuffered reads (such as IO#sysread).
+ * - Calling #rewind on the stream discards the pushed-back data.
*
- * Pushes back one character (passed as a parameter) onto <em>ios</em>,
- * such that a subsequent buffered character read will return it. Only one character
- * may be pushed back before a subsequent read operation (that is,
- * you will be able to read only the last of several characters that have been pushed
- * back). Has no effect with unbuffered reads (such as <code>IO#sysread</code>).
+ * When argument +integer+ is given, interprets the integer as a character:
+ *
+ * File.write('t.tmp', '012')
+ * f = File.open('t.tmp')
+ * f.ungetc(0x41) # => nil
+ * f.read # => "A012"
+ * f.rewind
+ * f.ungetc(0x0442) # => nil
+ * f.getc.ord # => 1090
+ * f.close
+ *
+ * When argument +string+ is given, uses all characters:
+ *
+ * File.write('t.tmp', '012')
+ * f = File.open('t.tmp')
+ * f.ungetc('A') # => nil
+ * f.read # => "A012"
+ * f.rewind
+ * f.ungetc("\u0442\u0435\u0441\u0442") # => nil
+ * f.getc.ord # => 1090
+ * f.getc.ord # => 1077
+ * f.getc.ord # => 1089
+ * f.getc.ord # => 1090
+ * f.close
*
- * f = File.new("testfile") #=> #<File:testfile>
- * c = f.getc #=> "8"
- * f.ungetc(c) #=> nil
- * f.getc #=> "8"
*/
VALUE
@@ -3866,22 +5267,21 @@ rb_io_ungetc(VALUE io, VALUE c)
GetOpenFile(io, fptr);
rb_io_check_char_readable(fptr);
- if (NIL_P(c)) return Qnil;
if (FIXNUM_P(c)) {
- c = rb_enc_uint_chr(FIX2UINT(c), io_read_encoding(fptr));
+ c = rb_enc_uint_chr(FIX2UINT(c), io_read_encoding(fptr));
}
- else if (RB_TYPE_P(c, T_BIGNUM)) {
- c = rb_enc_uint_chr(NUM2UINT(c), io_read_encoding(fptr));
+ else if (RB_BIGNUM_TYPE_P(c)) {
+ c = rb_enc_uint_chr(NUM2UINT(c), io_read_encoding(fptr));
}
else {
- SafeStringValue(c);
+ StringValue(c);
}
if (NEED_READCONV(fptr)) {
- SET_BINARY_MODE(fptr);
+ SET_BINARY_MODE(fptr);
len = RSTRING_LEN(c);
#if SIZEOF_LONG > SIZEOF_INT
- if (len > INT_MAX)
- rb_raise(rb_eIOError, "ungetc failed");
+ if (len > INT_MAX)
+ rb_raise(rb_eIOError, "ungetc failed");
#endif
make_readconv(fptr, (int)len);
if (fptr->cbuf.capa - fptr->cbuf.len < len)
@@ -3897,7 +5297,7 @@ rb_io_ungetc(VALUE io, VALUE c)
MEMMOVE(fptr->cbuf.ptr+fptr->cbuf.off, RSTRING_PTR(c), char, len);
}
else {
- NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
+ NEED_NEWLINE_DECORATOR_ON_READ_CHECK(fptr);
io_ungetbyte(c, fptr);
}
return Qnil;
@@ -3905,14 +5305,16 @@ rb_io_ungetc(VALUE io, VALUE c)
/*
* call-seq:
- * ios.isatty -> true or false
- * ios.tty? -> true or false
+ * isatty -> true or false
+ *
+ * Returns +true+ if the stream is associated with a terminal device (tty),
+ * +false+ otherwise:
*
- * Returns <code>true</code> if <em>ios</em> is associated with a
- * terminal device (tty), <code>false</code> otherwise.
+ * f = File.new('t.txt').isatty #=> false
+ * f.close
+ * f = File.new('/dev/tty').isatty #=> true
+ * f.close
*
- * File.new("testfile").isatty #=> false
- * File.new("/dev/tty").isatty #=> true
*/
static VALUE
@@ -3921,24 +5323,22 @@ rb_io_isatty(VALUE io)
rb_io_t *fptr;
GetOpenFile(io, fptr);
- if (isatty(fptr->fd) == 0)
- return Qfalse;
- return Qtrue;
+ return RBOOL(isatty(fptr->fd) != 0);
}
#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
/*
* call-seq:
- * ios.close_on_exec? -> true or false
+ * close_on_exec? -> true or false
*
- * Returns <code>true</code> if <em>ios</em> will be closed on exec.
+ * Returns +true+ if the stream will be closed on exec, +false+ otherwise:
+ *
+ * f = File.open('t.txt')
+ * f.close_on_exec? # => true
+ * f.close_on_exec = false
+ * f.close_on_exec? # => false
+ * f.close
*
- * f = open("/dev/null")
- * f.close_on_exec? #=> false
- * f.close_on_exec = true
- * f.close_on_exec? #=> true
- * f.close_on_exec = false
- * f.close_on_exec? #=> false
*/
static VALUE
@@ -3971,11 +5371,11 @@ rb_io_close_on_exec_p(VALUE io)
#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
/*
* call-seq:
- * ios.close_on_exec = bool -> true or false
+ * self.close_on_exec = bool -> true or false
*
* Sets a close-on-exec flag.
*
- * f = open("/dev/null")
+ * f = File.open(File::NULL)
* f.close_on_exec = true
* system("cat", "/proc/self/fd/#{f.fileno}") # cat: /proc/self/fd/3: No such file or directory
* f.closed? #=> false
@@ -4005,7 +5405,7 @@ rb_io_set_close_on_exec(VALUE io, VALUE arg)
if ((ret & FD_CLOEXEC) != flag) {
ret = (ret & ~FD_CLOEXEC) | flag;
ret = fcntl(fd, F_SETFD, ret);
- if (ret == -1) rb_sys_fail_path(fptr->pathv);
+ if (ret != 0) rb_sys_fail_path(fptr->pathv);
}
}
@@ -4017,7 +5417,7 @@ rb_io_set_close_on_exec(VALUE io, VALUE arg)
if ((ret & FD_CLOEXEC) != flag) {
ret = (ret & ~FD_CLOEXEC) | flag;
ret = fcntl(fd, F_SETFD, ret);
- if (ret == -1) rb_sys_fail_path(fptr->pathv);
+ if (ret != 0) rb_sys_fail_path(fptr->pathv);
}
}
return Qnil;
@@ -4026,8 +5426,7 @@ rb_io_set_close_on_exec(VALUE io, VALUE arg)
#define rb_io_set_close_on_exec rb_f_notimplement
#endif
-#define FMODE_PREP (1<<16)
-#define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
+#define RUBY_IO_EXTERNAL_P(f) ((f)->mode & FMODE_EXTERNAL)
#define PREP_STDIO_NAME(f) (RSTRING_PTR((f)->pathv))
static VALUE
@@ -4038,7 +5437,6 @@ finish_writeconv(rb_io_t *fptr, int noalloc)
if (!fptr->wbuf.ptr) {
unsigned char buf[1024];
- long r;
res = econv_destination_buffer_full;
while (res == econv_destination_buffer_full) {
@@ -4046,22 +5444,20 @@ finish_writeconv(rb_io_t *fptr, int noalloc)
de = buf + sizeof(buf);
res = rb_econv_convert(fptr->writeconv, NULL, NULL, &dp, de, 0);
while (dp-ds) {
- retry:
- if (fptr->write_lock && rb_mutex_owned_p(fptr->write_lock))
- r = rb_write_internal2(fptr->fd, ds, dp-ds);
- else
- r = rb_write_internal(fptr->fd, ds, dp-ds);
- if (r == dp-ds)
- break;
- if (0 <= r) {
- ds += r;
+ size_t remaining = dp-ds;
+ long result = rb_io_write_memory(fptr, ds, remaining);
+
+ if (result > 0) {
+ ds += result;
+ if ((size_t)result == remaining) break;
}
- if (rb_io_wait_writable(fptr->fd)) {
+ else if (rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT)) {
if (fptr->fd < 0)
- return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr("closed stream"));
- goto retry;
+ return noalloc ? Qtrue : rb_exc_new3(rb_eIOError, rb_str_new_cstr(closed_stream));
+ }
+ else {
+ return noalloc ? Qtrue : INT2NUM(errno);
}
- return noalloc ? Qtrue : INT2NUM(errno);
}
if (res == econv_invalid_byte_sequence ||
res == econv_incomplete_input ||
@@ -4076,8 +5472,9 @@ finish_writeconv(rb_io_t *fptr, int noalloc)
res = econv_destination_buffer_full;
while (res == econv_destination_buffer_full) {
if (fptr->wbuf.len == fptr->wbuf.capa) {
- if (io_fflush(fptr) < 0)
+ if (io_fflush(fptr) < 0) {
return noalloc ? Qtrue : INT2NUM(errno);
+ }
}
ds = dp = (unsigned char *)fptr->wbuf.ptr + fptr->wbuf.off + fptr->wbuf.len;
@@ -4117,13 +5514,13 @@ static int
maygvl_close(int fd, int keepgvl)
{
if (keepgvl)
- return close(fd);
+ return close(fd);
/*
* close() may block for certain file types (NFS, SO_LINGER sockets,
* inotify), so let other threads run.
*/
- return (int)(intptr_t)rb_thread_call_without_gvl(nogvl_close, &fd, RUBY_UBF_IO, 0);
+ return IO_WITHOUT_GVL_INT(nogvl_close, &fd);
}
static void*
@@ -4138,82 +5535,126 @@ static int
maygvl_fclose(FILE *file, int keepgvl)
{
if (keepgvl)
- return fclose(file);
+ return fclose(file);
- return (int)(intptr_t)rb_thread_call_without_gvl(nogvl_fclose, file, RUBY_UBF_IO, 0);
+ return IO_WITHOUT_GVL_INT(nogvl_fclose, file);
}
+static void free_io_buffer(rb_io_buffer_t *buf);
+
static void
-fptr_finalize(rb_io_t *fptr, int noraise)
+fptr_finalize_flush(rb_io_t *fptr, int noraise, int keepgvl)
{
- VALUE err = Qnil;
+ VALUE error = Qnil;
int fd = fptr->fd;
FILE *stdio_file = fptr->stdio_file;
+ int mode = fptr->mode;
if (fptr->writeconv) {
- if (fptr->write_lock && !noraise) {
+ if (!NIL_P(fptr->write_lock) && !noraise) {
struct finish_writeconv_arg arg;
arg.fptr = fptr;
arg.noalloc = noraise;
- err = rb_mutex_synchronize(fptr->write_lock, finish_writeconv_sync, (VALUE)&arg);
- }
- else {
- err = finish_writeconv(fptr, noraise);
- }
+ error = rb_mutex_synchronize(fptr->write_lock, finish_writeconv_sync, (VALUE)&arg);
+ }
+ else {
+ error = finish_writeconv(fptr, noraise);
+ }
}
if (fptr->wbuf.len) {
- if (noraise) {
- if ((int)io_flush_buffer_sync(fptr) < 0 && NIL_P(err))
- err = Qtrue;
- }
- else {
- if (io_fflush(fptr) < 0 && NIL_P(err))
- err = INT2NUM(errno);
- }
+ if (noraise) {
+ io_flush_buffer_sync(fptr);
+ }
+ else {
+ if (io_fflush(fptr) < 0 && NIL_P(error)) {
+ error = INT2NUM(errno);
+ }
+ }
+ }
+
+ int done = 0;
+
+ if (RUBY_IO_EXTERNAL_P(fptr) || fd <= 2) {
+ // Need to keep FILE objects of stdin, stdout and stderr, so we are done:
+ done = 1;
}
fptr->fd = -1;
fptr->stdio_file = 0;
fptr->mode &= ~(FMODE_READABLE|FMODE_WRITABLE);
- if (IS_PREP_STDIO(fptr) || fd <= 2) {
- /* need to keep FILE objects of stdin, stdout and stderr */
- }
- else if (stdio_file) {
- /* stdio_file is deallocated anyway
- * even if fclose failed. */
- if ((maygvl_fclose(stdio_file, noraise) < 0) && NIL_P(err))
- err = noraise ? Qtrue : INT2NUM(errno);
+ // Wait for blocking operations to ensure they do not hit EBADF:
+ rb_thread_io_close_wait(fptr);
+
+ if (!done && stdio_file) {
+ // stdio_file is deallocated anyway even if fclose failed.
+ if ((maygvl_fclose(stdio_file, noraise) < 0) && NIL_P(error)) {
+ if (!noraise) {
+ error = INT2NUM(errno);
+ }
+ }
+
+ done = 1;
}
- else if (0 <= fd) {
- /* fptr->fd may be closed even if close fails.
- * POSIX doesn't specify it.
- * We assumes it is closed. */
- if ((maygvl_close(fd, noraise) < 0) && NIL_P(err))
- err = noraise ? Qtrue : INT2NUM(errno);
+
+ VALUE scheduler = rb_fiber_scheduler_current();
+ if (!done && fd >= 0 && scheduler != Qnil) {
+ VALUE result = rb_fiber_scheduler_io_close(scheduler, RB_INT2NUM(fd));
+
+ if (!UNDEF_P(result)) {
+ done = RTEST(result);
+ }
}
- if (!NIL_P(err) && !noraise) {
- switch (TYPE(err)) {
- case T_FIXNUM:
- case T_BIGNUM:
- errno = NUM2INT(err);
- rb_sys_fail_path(fptr->pathv);
+ if (!done && fd >= 0) {
+ // fptr->fd may be closed even if close fails. POSIX doesn't specify it.
+ // We assumes it is closed.
- default:
- rb_exc_raise(err);
+ keepgvl |= !(mode & FMODE_WRITABLE);
+ keepgvl |= noraise;
+ if ((maygvl_close(fd, keepgvl) < 0) && NIL_P(error)) {
+ if (!noraise) {
+ error = INT2NUM(errno);
+ }
}
+
+ done = 1;
}
+
+ if (!NIL_P(error) && !noraise) {
+ if (RB_INTEGER_TYPE_P(error))
+ rb_syserr_fail_path(NUM2INT(error), fptr->pathv);
+ else
+ rb_exc_raise(error);
+ }
+}
+
+static void
+fptr_finalize(rb_io_t *fptr, int noraise)
+{
+ fptr_finalize_flush(fptr, noraise, FALSE);
+ free_io_buffer(&fptr->rbuf);
+ free_io_buffer(&fptr->wbuf);
+ clear_codeconv(fptr);
}
static void
rb_io_fptr_cleanup(rb_io_t *fptr, int noraise)
{
if (fptr->finalize) {
- (*fptr->finalize)(fptr, noraise);
+ (*fptr->finalize)(fptr, noraise);
}
else {
- fptr_finalize(fptr, noraise);
+ fptr_finalize(fptr, noraise);
+ }
+}
+
+static void
+free_io_buffer(rb_io_buffer_t *buf)
+{
+ if (buf->ptr) {
+ ruby_xfree_sized(buf->ptr, (size_t)buf->capa);
+ buf->ptr = NULL;
}
}
@@ -4224,10 +5665,7 @@ clear_readconv(rb_io_t *fptr)
rb_econv_close(fptr->readconv);
fptr->readconv = NULL;
}
- if (fptr->cbuf.ptr) {
- free(fptr->cbuf.ptr);
- fptr->cbuf.ptr = NULL;
- }
+ free_io_buffer(&fptr->cbuf);
}
static void
@@ -4247,46 +5685,71 @@ clear_codeconv(rb_io_t *fptr)
clear_writeconv(fptr);
}
-int
-rb_io_fptr_finalize(rb_io_t *fptr)
+static void
+rb_io_fptr_cleanup_all(rb_io_t *fptr)
{
- if (!fptr) return 0;
fptr->pathv = Qnil;
if (0 <= fptr->fd)
- rb_io_fptr_cleanup(fptr, TRUE);
- fptr->write_lock = 0;
- if (fptr->rbuf.ptr) {
- free(fptr->rbuf.ptr);
- fptr->rbuf.ptr = 0;
- }
- if (fptr->wbuf.ptr) {
- free(fptr->wbuf.ptr);
- fptr->wbuf.ptr = 0;
- }
+ rb_io_fptr_cleanup(fptr, TRUE);
+ fptr->write_lock = Qnil;
+ free_io_buffer(&fptr->rbuf);
+ free_io_buffer(&fptr->wbuf);
clear_codeconv(fptr);
- free(fptr);
+}
+
+int
+rb_io_fptr_finalize(struct rb_io *io)
+{
+ if (!io) return 0;
+ rb_io_fptr_cleanup_all(io);
+ free(io);
+
return 1;
}
-size_t rb_econv_memsize(rb_econv_t *);
+bool
+rb_io_fptr_finalize_closed(struct rb_io *io)
+{
+ if (!io) return true;
+ if (io->fd >= 0) return false;
+ rb_io_fptr_finalize(io);
+ return true;
+}
-RUBY_FUNC_EXPORTED size_t
-rb_io_memsize(const rb_io_t *fptr)
+size_t
+rb_io_memsize(const rb_io_t *io)
{
size_t size = sizeof(rb_io_t);
- size += fptr->rbuf.capa;
- size += fptr->wbuf.capa;
- size += fptr->cbuf.capa;
- if (fptr->readconv) size += rb_econv_memsize(fptr->readconv);
- if (fptr->writeconv) size += rb_econv_memsize(fptr->writeconv);
+ size += io->rbuf.capa;
+ size += io->wbuf.capa;
+ size += io->cbuf.capa;
+ if (io->readconv) size += rb_econv_memsize(io->readconv);
+ if (io->writeconv) size += rb_econv_memsize(io->writeconv);
+
+ struct rb_io_blocking_operation *blocking_operation = 0;
+
+ // Validate the fork generation of the IO object. If the IO object fork generation is different, the list of blocking operations is not valid memory. See `rb_io_blocking_operations` for the exact semantics.
+ rb_serial_t fork_generation = GET_VM()->fork_gen;
+ if (io->fork_generation == fork_generation) {
+ ccan_list_for_each(&io->blocking_operations, blocking_operation, list) {
+ size += sizeof(struct rb_io_blocking_operation);
+ }
+ }
+
return size;
}
-VALUE
-rb_io_close(VALUE io)
+#ifdef _WIN32
+/* keep GVL while closing to prevent crash on Windows */
+# define KEEPGVL TRUE
+#else
+# define KEEPGVL FALSE
+#endif
+
+static rb_io_t *
+io_close_fptr(VALUE io)
{
rb_io_t *fptr;
- int fd;
VALUE write_io;
rb_io_t *write_fptr;
@@ -4299,40 +5762,80 @@ rb_io_close(VALUE io)
}
fptr = RFILE(io)->fptr;
- if (!fptr) return Qnil;
- if (fptr->fd < 0) return Qnil;
+ if (!fptr) return 0;
+ if (fptr->fd < 0) return 0;
+
+ // This guards against multiple threads closing the same IO object:
+ if (rb_thread_io_close_interrupt(fptr)) {
+ /* calls close(fptr->fd): */
+ fptr_finalize_flush(fptr, FALSE, KEEPGVL);
+ }
- fd = fptr->fd;
- rb_thread_fd_close(fd);
rb_io_fptr_cleanup(fptr, FALSE);
+ return fptr;
+}
+static void
+fptr_waitpid(rb_io_t *fptr, int nohang)
+{
+ int status;
if (fptr->pid) {
rb_last_status_clear();
- rb_syswait(fptr->pid);
- fptr->pid = 0;
+ rb_waitpid(fptr->pid, &status, nohang ? WNOHANG : 0);
+ fptr->pid = 0;
}
+}
+VALUE
+rb_io_close(VALUE io)
+{
+ rb_io_t *fptr = io_close_fptr(io);
+ if (fptr) fptr_waitpid(fptr, 0);
return Qnil;
}
/*
* call-seq:
- * ios.close -> nil
+ * close -> nil
+ *
+ * Closes the stream for both reading and writing
+ * if open for either or both; returns +nil+.
+ * See {Open and Closed Streams}[rdoc-ref:IO@Open+and+Closed+Streams].
+ *
+ * If the stream is open for writing, flushes any buffered writes
+ * to the operating system before closing.
+ *
+ * If the stream was opened by IO.popen, sets global variable <tt>$?</tt>
+ * (child exit status).
+ *
+ * It is not an error to close an IO object that has already been closed.
+ * It just returns nil.
+ *
+ * Example:
*
- * Closes <em>ios</em> and flushes any pending writes to the operating
- * system. The stream is unavailable for any further data operations;
- * an <code>IOError</code> is raised if such an attempt is made. I/O
- * streams are automatically closed when they are claimed by the
- * garbage collector.
+ * IO.popen('ruby', 'r+') do |pipe|
+ * puts pipe.closed?
+ * pipe.close
+ * puts $?
+ * puts pipe.closed?
+ * end
+ *
+ * Output:
+ *
+ * false
+ * pid 13760 exit 0
+ * true
*
- * If <em>ios</em> is opened by <code>IO.popen</code>,
- * <code>close</code> sets <code>$?</code>.
+ * Related: IO#close_read, IO#close_write, IO#closed?.
*/
static VALUE
rb_io_close_m(VALUE io)
{
- rb_io_check_closed(RFILE(io)->fptr);
+ rb_io_t *fptr = rb_io_get_fptr(io);
+ if (fptr->fd < 0) {
+ return Qnil;
+ }
rb_io_close(io);
return Qnil;
}
@@ -4340,36 +5843,59 @@ rb_io_close_m(VALUE io)
static VALUE
io_call_close(VALUE io)
{
- return rb_funcall(io, rb_intern("close"), 0, 0);
+ rb_check_funcall(io, rb_intern("close"), 0, 0);
+ return io;
+}
+
+static VALUE
+ignore_closed_stream(VALUE io, VALUE exc)
+{
+ enum {mesg_len = sizeof(closed_stream)-1};
+ VALUE mesg = rb_attr_get(exc, idMesg);
+ if (!RB_TYPE_P(mesg, T_STRING) ||
+ RSTRING_LEN(mesg) != mesg_len ||
+ memcmp(RSTRING_PTR(mesg), closed_stream, mesg_len)) {
+ rb_exc_raise(exc);
+ }
+ return io;
}
static VALUE
io_close(VALUE io)
{
- return rb_rescue(io_call_close, io, 0, 0);
+ VALUE closed = rb_check_funcall(io, rb_intern("closed?"), 0, 0);
+ if (!UNDEF_P(closed) && RTEST(closed)) return io;
+ rb_rescue2(io_call_close, io, ignore_closed_stream, io,
+ rb_eIOError, (VALUE)0);
+ return io;
}
/*
* call-seq:
- * ios.closed? -> true or false
+ * closed? -> true or false
+ *
+ * Returns +true+ if the stream is closed for both reading and writing,
+ * +false+ otherwise.
+ * See {Open and Closed Streams}[rdoc-ref:IO@Open+and+Closed+Streams].
+ *
+ * IO.popen('ruby', 'r+') do |pipe|
+ * puts pipe.closed?
+ * pipe.close_read
+ * puts pipe.closed?
+ * pipe.close_write
+ * puts pipe.closed?
+ * end
+ *
+ * Output:
*
- * Returns <code>true</code> if <em>ios</em> is completely closed (for
- * duplex streams, both reader and writer), <code>false</code>
- * otherwise.
+ * false
+ * false
+ * true
*
- * f = File.new("testfile")
- * f.close #=> nil
- * f.closed? #=> true
- * f = IO.popen("/bin/sh","r+")
- * f.close_write #=> nil
- * f.closed? #=> false
- * f.close_read #=> nil
- * f.closed? #=> true
+ * Related: IO#close_read, IO#close_write, IO#close.
*/
-
-
-static VALUE
-rb_io_closed(VALUE io)
+VALUE
+rb_io_closed_p(VALUE io)
{
rb_io_t *fptr;
VALUE write_io;
@@ -4383,27 +5909,40 @@ rb_io_closed(VALUE io)
}
}
- fptr = RFILE(io)->fptr;
- rb_io_check_initialized(fptr);
- return 0 <= fptr->fd ? Qfalse : Qtrue;
+ fptr = rb_io_get_fptr(io);
+ return RBOOL(0 > fptr->fd);
}
/*
* call-seq:
- * ios.close_read -> nil
+ * close_read -> nil
*
- * Closes the read end of a duplex I/O stream (i.e., one that contains
- * both a read and a write stream, such as a pipe). Will raise an
- * <code>IOError</code> if the stream is not duplexed.
+ * Closes the stream for reading if open for reading;
+ * returns +nil+.
+ * See {Open and Closed Streams}[rdoc-ref:IO@Open+and+Closed+Streams].
*
- * f = IO.popen("/bin/sh","r+")
- * f.close_read
- * f.readlines
+ * If the stream was opened by IO.popen and is also closed for writing,
+ * sets global variable <tt>$?</tt> (child exit status).
*
- * <em>produces:</em>
+ * Example:
+ *
+ * IO.popen('ruby', 'r+') do |pipe|
+ * puts pipe.closed?
+ * pipe.close_write
+ * puts pipe.closed?
+ * pipe.close_read
+ * puts $?
+ * puts pipe.closed?
+ * end
*
- * prog.rb:3:in `readlines': not opened for reading (IOError)
- * from prog.rb:3
+ * Output:
+ *
+ * false
+ * false
+ * pid 14748 exit 0
+ * true
+ *
+ * Related: IO#close, IO#close_write, IO#closed?.
*/
static VALUE
@@ -4412,7 +5951,8 @@ rb_io_close_read(VALUE io)
rb_io_t *fptr;
VALUE write_io;
- GetOpenFile(io, fptr);
+ fptr = rb_io_get_fptr(rb_io_taint_check(io));
+ if (fptr->fd < 0) return Qnil;
if (is_socket(fptr->fd, fptr->pathv)) {
#ifndef SHUT_RD
# define SHUT_RD 0
@@ -4427,43 +5967,55 @@ rb_io_close_read(VALUE io)
write_io = GetWriteIO(io);
if (io != write_io) {
- rb_io_t *wfptr;
- GetOpenFile(write_io, wfptr);
- wfptr->pid = fptr->pid;
- fptr->pid = 0;
+ rb_io_t *wfptr;
+ wfptr = rb_io_get_fptr(rb_io_taint_check(write_io));
+ wfptr->pid = fptr->pid;
+ fptr->pid = 0;
RFILE(io)->fptr = wfptr;
- /* bind to write_io temporarily to get rid of memory/fd leak */
- fptr->tied_io_for_writing = 0;
- fptr->mode &= ~FMODE_DUPLEX;
- RFILE(write_io)->fptr = fptr;
- rb_io_fptr_cleanup(fptr, FALSE);
- /* should not finalize fptr because another thread may be reading it */
+ /* bind to write_io temporarily to get rid of memory/fd leak */
+ fptr->tied_io_for_writing = 0;
+ RFILE(write_io)->fptr = fptr;
+ rb_io_fptr_cleanup(fptr, FALSE);
+ /* should not finalize fptr because another thread may be reading it */
return Qnil;
}
- if (fptr->mode & FMODE_WRITABLE) {
- rb_raise(rb_eIOError, "closing non-duplex IO for reading");
+ if ((fptr->mode & (FMODE_DUPLEX|FMODE_WRITABLE)) == FMODE_WRITABLE) {
+ rb_raise(rb_eIOError, "closing non-duplex IO for reading");
}
return rb_io_close(io);
}
/*
* call-seq:
- * ios.close_write -> nil
+ * close_write -> nil
*
- * Closes the write end of a duplex I/O stream (i.e., one that contains
- * both a read and a write stream, such as a pipe). Will raise an
- * <code>IOError</code> if the stream is not duplexed.
+ * Closes the stream for writing if open for writing;
+ * returns +nil+.
+ * See {Open and Closed Streams}[rdoc-ref:IO@Open+and+Closed+Streams].
*
- * f = IO.popen("/bin/sh","r+")
- * f.close_write
- * f.print "nowhere"
+ * Flushes any buffered writes to the operating system before closing.
*
- * <em>produces:</em>
+ * If the stream was opened by IO.popen and is also closed for reading,
+ * sets global variable <tt>$?</tt> (child exit status).
+ *
+ * IO.popen('ruby', 'r+') do |pipe|
+ * puts pipe.closed?
+ * pipe.close_read
+ * puts pipe.closed?
+ * pipe.close_write
+ * puts $?
+ * puts pipe.closed?
+ * end
+ *
+ * Output:
*
- * prog.rb:3:in `write': not opened for writing (IOError)
- * from prog.rb:3:in `print'
- * from prog.rb:3
+ * false
+ * false
+ * pid 15044 exit 0
+ * true
+ *
+ * Related: IO#close, IO#close_read, IO#closed?.
*/
static VALUE
@@ -4473,7 +6025,8 @@ rb_io_close_write(VALUE io)
VALUE write_io;
write_io = GetWriteIO(io);
- GetOpenFile(write_io, fptr);
+ fptr = rb_io_get_fptr(rb_io_taint_check(write_io));
+ if (fptr->fd < 0) return Qnil;
if (is_socket(fptr->fd, fptr->pathv)) {
#ifndef SHUT_WR
# define SHUT_WR 1
@@ -4482,18 +6035,17 @@ rb_io_close_write(VALUE io)
rb_sys_fail_path(fptr->pathv);
fptr->mode &= ~FMODE_WRITABLE;
if (!(fptr->mode & FMODE_READABLE))
- return rb_io_close(write_io);
+ return rb_io_close(write_io);
return Qnil;
}
- if (fptr->mode & FMODE_READABLE) {
- rb_raise(rb_eIOError, "closing non-duplex IO for writing");
+ if ((fptr->mode & (FMODE_DUPLEX|FMODE_READABLE)) == FMODE_READABLE) {
+ rb_raise(rb_eIOError, "closing non-duplex IO for writing");
}
if (io != write_io) {
- GetOpenFile(io, fptr);
- fptr->tied_io_for_writing = 0;
- fptr->mode &= ~FMODE_DUPLEX;
+ fptr = rb_io_get_fptr(rb_io_taint_check(io));
+ fptr->tied_io_for_writing = 0;
}
rb_io_close(write_io);
return Qnil;
@@ -4501,15 +6053,13 @@ rb_io_close_write(VALUE io)
/*
* call-seq:
- * ios.sysseek(offset, whence=IO::SEEK_SET) -> integer
+ * sysseek(offset, whence = IO::SEEK_SET) -> integer
*
- * Seeks to a given <i>offset</i> in the stream according to the value
- * of <i>whence</i> (see <code>IO#seek</code> for values of
- * <i>whence</i>). Returns the new offset into the file.
+ * Behaves like IO#seek, except that it:
+ *
+ * - Uses low-level system functions.
+ * - Returns the new position.
*
- * f = File.new("testfile")
- * f.sysseek(-13, IO::SEEK_END) #=> 53
- * f.sysread(10) #=> "And so on."
*/
static VALUE
@@ -4518,83 +6068,82 @@ rb_io_sysseek(int argc, VALUE *argv, VALUE io)
VALUE offset, ptrname;
int whence = SEEK_SET;
rb_io_t *fptr;
- off_t pos;
+ rb_off_t pos;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
- whence = interpret_seek_whence(ptrname);
+ whence = interpret_seek_whence(ptrname);
}
pos = NUM2OFFT(offset);
GetOpenFile(io, fptr);
if ((fptr->mode & FMODE_READABLE) &&
(READ_DATA_BUFFERED(fptr) || READ_CHAR_PENDING(fptr))) {
- rb_raise(rb_eIOError, "sysseek for buffered IO");
+ rb_raise(rb_eIOError, "sysseek for buffered IO");
}
if ((fptr->mode & FMODE_WRITABLE) && fptr->wbuf.len) {
- rb_warn("sysseek for buffered IO");
+ rb_warn("sysseek for buffered IO");
}
errno = 0;
pos = lseek(fptr->fd, pos, whence);
- if (pos == -1 && errno) rb_sys_fail_path(fptr->pathv);
+ if (pos < 0 && errno) rb_sys_fail_path(fptr->pathv);
return OFFT2NUM(pos);
}
/*
* call-seq:
- * ios.syswrite(string) -> integer
+ * syswrite(object) -> integer
+ *
+ * Writes the given +object+ to self, which must be opened for writing (see Modes);
+ * returns the number bytes written.
+ * If +object+ is not a string is converted via method to_s:
*
- * Writes the given string to <em>ios</em> using a low-level write.
- * Returns the number of bytes written. Do not mix with other methods
- * that write to <em>ios</em> or you may get unpredictable results.
- * Raises <code>SystemCallError</code> on error.
+ * f = File.new('t.tmp', 'w')
+ * f.syswrite('foo') # => 3
+ * f.syswrite(30) # => 2
+ * f.syswrite(:foo) # => 3
+ * f.close
+ *
+ * This methods should not be used with other stream-writer methods.
*
- * f = File.new("out", "w")
- * f.syswrite("ABCDEF") #=> 6
*/
static VALUE
rb_io_syswrite(VALUE io, VALUE str)
{
+ VALUE tmp;
rb_io_t *fptr;
- long n;
+ long n, len;
+ const char *ptr;
if (!RB_TYPE_P(str, T_STRING))
- str = rb_obj_as_string(str);
+ str = rb_obj_as_string(str);
io = GetWriteIO(io);
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
- str = rb_str_new_frozen(str);
-
if (fptr->wbuf.len) {
- rb_warn("syswrite for buffered IO");
+ rb_warn("syswrite for buffered IO");
}
- n = rb_write_internal(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
- RB_GC_GUARD(str);
-
- if (n == -1) rb_sys_fail_path(fptr->pathv);
+ tmp = rb_str_tmp_frozen_acquire(str);
+ RSTRING_GETMEM(tmp, ptr, len);
+ n = rb_io_write_memory(fptr, ptr, len);
+ if (n < 0) rb_sys_fail_path(fptr->pathv);
+ rb_str_tmp_frozen_release(str, tmp);
return LONG2FIX(n);
}
/*
* call-seq:
- * ios.sysread(maxlen[, outbuf]) -> string
+ * sysread(maxlen) -> string
+ * sysread(maxlen, out_string) -> string
*
- * Reads <i>maxlen</i> bytes from <em>ios</em> using a low-level
- * read and returns them as a string. Do not mix with other methods
- * that read from <em>ios</em> or you may get unpredictable results.
- * If the optional <i>outbuf</i> argument is present, it must reference
- * a String, which will receive the data.
- * The <i>outbuf</i> will contain only the received data after the method call
- * even if it is not empty at the beginning.
- * Raises <code>SystemCallError</code> on error and
- * <code>EOFError</code> at end of file.
+ * Behaves like IO#readpartial, except that it uses low-level system functions.
+ *
+ * This method should not be used with other stream-reader methods.
*
- * f = File.new("testfile")
- * f.sysread(16) #=> "This is line one"
*/
static VALUE
@@ -4603,54 +6152,227 @@ rb_io_sysread(int argc, VALUE *argv, VALUE io)
VALUE len, str;
rb_io_t *fptr;
long n, ilen;
- struct read_internal_arg arg;
+ struct io_internal_read_struct iis;
+ int shrinkable;
rb_scan_args(argc, argv, "11", &len, &str);
ilen = NUM2LONG(len);
- io_setstrbuf(&str,ilen);
+ shrinkable = io_setstrbuf(&str, ilen);
if (ilen == 0) return str;
GetOpenFile(io, fptr);
rb_io_check_byte_readable(fptr);
if (READ_DATA_BUFFERED(fptr)) {
- rb_raise(rb_eIOError, "sysread for buffered IO");
+ rb_raise(rb_eIOError, "sysread for buffered IO");
}
- n = fptr->fd;
+ rb_io_check_closed(fptr);
+
+ io_setstrbuf(&str, ilen);
+ iis.th = rb_thread_current();
+ iis.fptr = fptr;
+ iis.nonblock = 0;
+ iis.fd = fptr->fd;
+ iis.buf = RSTRING_PTR(str);
+ iis.capa = ilen;
+ iis.timeout = NULL;
+ n = io_read_memory_locktmp(str, &iis);
- /*
- * FIXME: removing rb_thread_wait_fd() here changes sysread semantics
- * on non-blocking IOs. However, it's still currently possible
- * for sysread to raise Errno::EAGAIN if another thread read()s
- * the IO after we return from rb_thread_wait_fd() but before
- * we call read()
- */
- rb_thread_wait_fd(fptr->fd);
+ if (n < 0) {
+ rb_sys_fail_path(fptr->pathv);
+ }
+
+ io_set_read_length(str, n, shrinkable);
+
+ if (n == 0 && ilen > 0) {
+ rb_eof_error();
+ }
+
+ return str;
+}
+
+struct prdwr_internal_arg {
+ struct rb_io *io;
+ int fd;
+ void *buf;
+ size_t count;
+ rb_off_t offset;
+};
+
+static VALUE
+internal_pread_func(void *_arg)
+{
+ struct prdwr_internal_arg *arg = _arg;
+
+ return (VALUE)pread(arg->fd, arg->buf, arg->count, arg->offset);
+}
+
+static VALUE
+pread_internal_call(VALUE _arg)
+{
+ struct prdwr_internal_arg *arg = (struct prdwr_internal_arg *)_arg;
+
+ VALUE scheduler = rb_fiber_scheduler_current();
+ if (scheduler != Qnil) {
+ VALUE result = rb_fiber_scheduler_io_pread_memory(scheduler, arg->io->self, arg->offset, arg->buf, arg->count, 0);
+
+ if (!UNDEF_P(result)) {
+ return rb_fiber_scheduler_io_result_apply(result);
+ }
+ }
+
+ return rb_io_blocking_region_wait(arg->io, internal_pread_func, arg, RUBY_IO_READABLE);
+}
+
+/*
+ * call-seq:
+ * pread(maxlen, offset) -> string
+ * pread(maxlen, offset, out_string) -> string
+ *
+ * Behaves like IO#readpartial, except that it:
+ *
+ * - Reads at the given +offset+ (in bytes).
+ * - Disregards, and does not modify, the stream's position
+ * (see {Position}[rdoc-ref:IO@Position]).
+ * - Bypasses any user space buffering in the stream.
+ *
+ * Because this method does not disturb the stream's state
+ * (its position, in particular), +pread+ allows multiple threads and processes
+ * to use the same \IO object for reading at various offsets.
+ *
+ * f = File.open('t.txt')
+ * f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * f.pos # => 52
+ * # Read 12 bytes at offset 0.
+ * f.pread(12, 0) # => "First line\n"
+ * # Read 9 bytes at offset 8.
+ * f.pread(9, 8) # => "ne\nSecon"
+ * f.close
+ *
+ * Not available on some platforms.
+ *
+ */
+static VALUE
+rb_io_pread(int argc, VALUE *argv, VALUE io)
+{
+ VALUE len, offset, str;
+ rb_io_t *fptr;
+ ssize_t n;
+ struct prdwr_internal_arg arg;
+ int shrinkable;
+
+ rb_scan_args(argc, argv, "21", &len, &offset, &str);
+ arg.count = NUM2SIZET(len);
+ arg.offset = NUM2OFFT(offset);
+
+ shrinkable = io_setstrbuf(&str, (long)arg.count);
+ if (arg.count == 0) return str;
+ arg.buf = RSTRING_PTR(str);
+ GetOpenFile(io, fptr);
+ rb_io_check_byte_readable(fptr);
+
+ arg.io = fptr;
+ arg.fd = fptr->fd;
rb_io_check_closed(fptr);
- io_setstrbuf(&str, ilen);
rb_str_locktmp(str);
- arg.fd = fptr->fd;
- arg.str_ptr = RSTRING_PTR(str);
- arg.len = ilen;
- rb_ensure(read_internal_call, (VALUE)&arg, rb_str_unlocktmp, str);
- n = arg.len;
+ n = (ssize_t)rb_ensure(pread_internal_call, (VALUE)&arg, rb_str_unlocktmp, str);
- if (n == -1) {
- rb_sys_fail_path(fptr->pathv);
+ if (n < 0) {
+ rb_sys_fail_path(fptr->pathv);
}
- io_set_read_length(str, n);
- if (n == 0 && ilen > 0) {
- rb_eof_error();
+ io_set_read_length(str, n, shrinkable);
+ if (n == 0 && arg.count > 0) {
+ rb_eof_error();
}
- OBJ_TAINT(str);
return str;
}
+static VALUE
+internal_pwrite_func(void *_arg)
+{
+ struct prdwr_internal_arg *arg = _arg;
+
+ return (VALUE)pwrite(arg->fd, arg->buf, arg->count, arg->offset);
+}
+
+static VALUE
+pwrite_internal_call(VALUE _arg)
+{
+ struct prdwr_internal_arg *arg = (struct prdwr_internal_arg *)_arg;
+
+ VALUE scheduler = rb_fiber_scheduler_current();
+ if (scheduler != Qnil) {
+ VALUE result = rb_fiber_scheduler_io_pwrite_memory(scheduler, arg->io->self, arg->offset, arg->buf, arg->count, 0);
+
+ if (!UNDEF_P(result)) {
+ return rb_fiber_scheduler_io_result_apply(result);
+ }
+ }
+
+ return rb_io_blocking_region_wait(arg->io, internal_pwrite_func, arg, RUBY_IO_WRITABLE);
+}
+
+/*
+ * call-seq:
+ * pwrite(object, offset) -> integer
+ *
+ * Behaves like IO#write, except that it:
+ *
+ * - Writes at the given +offset+ (in bytes).
+ * - Disregards, and does not modify, the stream's position
+ * (see {Position}[rdoc-ref:IO@Position]).
+ * - Bypasses any user space buffering in the stream.
+ *
+ * Because this method does not disturb the stream's state
+ * (its position, in particular), +pwrite+ allows multiple threads and processes
+ * to use the same \IO object for writing at various offsets.
+ *
+ * f = File.open('t.tmp', 'w+')
+ * # Write 6 bytes at offset 3.
+ * f.pwrite('ABCDEF', 3) # => 6
+ * f.rewind
+ * f.read # => "\u0000\u0000\u0000ABCDEF"
+ * f.close
+ *
+ * Not available on some platforms.
+ *
+ */
+static VALUE
+rb_io_pwrite(VALUE io, VALUE str, VALUE offset)
+{
+ rb_io_t *fptr;
+ ssize_t n;
+ struct prdwr_internal_arg arg;
+ VALUE tmp;
+
+ if (!RB_TYPE_P(str, T_STRING))
+ str = rb_obj_as_string(str);
+
+ arg.offset = NUM2OFFT(offset);
+
+ io = GetWriteIO(io);
+ GetOpenFile(io, fptr);
+ rb_io_check_writable(fptr);
+
+ arg.io = fptr;
+ arg.fd = fptr->fd;
+
+ tmp = rb_str_tmp_frozen_acquire(str);
+ arg.buf = RSTRING_PTR(tmp);
+ arg.count = (size_t)RSTRING_LEN(tmp);
+
+ n = (ssize_t)pwrite_internal_call((VALUE)&arg);
+ if (n < 0) rb_sys_fail_path(fptr->pathv);
+ rb_str_tmp_frozen_release(str, tmp);
+
+ return SSIZET2NUM(n);
+}
+
VALUE
rb_io_binmode(VALUE io)
{
@@ -4666,10 +6388,10 @@ rb_io_binmode(VALUE io)
fptr->writeconv_pre_ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
#ifdef O_BINARY
if (!fptr->readconv) {
- SET_BINARY_MODE_WITH_SEEK_CUR(fptr);
+ SET_BINARY_MODE_WITH_SEEK_CUR(fptr);
}
else {
- setmode(fptr->fd, O_BINARY);
+ setmode(fptr->fd, O_BINARY);
}
#endif
return io;
@@ -4710,14 +6432,12 @@ rb_io_ascii8bit_binmode(VALUE io)
/*
* call-seq:
- * ios.binmode -> ios
+ * binmode -> self
*
- * Puts <em>ios</em> into binary mode.
- * Once a stream is in binary mode, it cannot be reset to nonbinary mode.
+ * Sets the stream's data mode as binary
+ * (see {Data Mode}[rdoc-ref:File@Data+Mode]).
*
- * - newline conversion disabled
- * - encoding conversion disabled
- * - content is treated as ASCII-8BIT
+ * A stream's data mode may not be changed from binary to text.
*
*/
@@ -4736,90 +6456,97 @@ rb_io_binmode_m(VALUE io)
/*
* call-seq:
- * ios.binmode? -> true or false
+ * binmode? -> true or false
+ *
+ * Returns +true+ if the stream is on binary mode, +false+ otherwise.
+ * See {Data Mode}[rdoc-ref:File@Data+Mode].
*
- * Returns <code>true</code> if <em>ios</em> is binmode.
*/
static VALUE
rb_io_binmode_p(VALUE io)
{
rb_io_t *fptr;
GetOpenFile(io, fptr);
- return fptr->mode & FMODE_BINMODE ? Qtrue : Qfalse;
+ return RBOOL(fptr->mode & FMODE_BINMODE);
}
static const char*
-rb_io_fmode_modestr(int fmode)
+rb_io_fmode_modestr(enum rb_io_mode fmode)
{
if (fmode & FMODE_APPEND) {
- if ((fmode & FMODE_READWRITE) == FMODE_READWRITE) {
- return MODE_BTMODE("a+", "ab+", "at+");
- }
- return MODE_BTMODE("a", "ab", "at");
+ if ((fmode & FMODE_READWRITE) == FMODE_READWRITE) {
+ return MODE_BTMODE("a+", "ab+", "at+");
+ }
+ return MODE_BTMODE("a", "ab", "at");
}
switch (fmode & FMODE_READWRITE) {
default:
- rb_raise(rb_eArgError, "invalid access fmode 0x%x", fmode);
+ rb_raise(rb_eArgError, "invalid access fmode 0x%x", fmode);
case FMODE_READABLE:
- return MODE_BTMODE("r", "rb", "rt");
+ return MODE_BTMODE("r", "rb", "rt");
case FMODE_WRITABLE:
- return MODE_BTMODE("w", "wb", "wt");
+ return MODE_BTXMODE("w", "wb", "wt", "wx", "wbx", "wtx");
case FMODE_READWRITE:
- if (fmode & FMODE_CREATE) {
- return MODE_BTMODE("w+", "wb+", "wt+");
- }
- return MODE_BTMODE("r+", "rb+", "rt+");
+ if (fmode & FMODE_CREATE) {
+ return MODE_BTXMODE("w+", "wb+", "wt+", "w+x", "wb+x", "wt+x");
+ }
+ return MODE_BTMODE("r+", "rb+", "rt+");
}
}
+static const char bom_prefix[] = "bom|";
+static const char utf_prefix[] = "utf-";
+enum {bom_prefix_len = (int)sizeof(bom_prefix) - 1};
+enum {utf_prefix_len = (int)sizeof(utf_prefix) - 1};
+
static int
io_encname_bom_p(const char *name, long len)
{
- static const char bom_prefix[] = "bom|utf-";
- enum {bom_prefix_len = (int)sizeof(bom_prefix) - 1};
- if (!len) {
- const char *p = strchr(name, ':');
- len = p ? (long)(p - name) : (long)strlen(name);
- }
return len > bom_prefix_len && STRNCASECMP(name, bom_prefix, bom_prefix_len) == 0;
}
-int
+enum rb_io_mode
rb_io_modestr_fmode(const char *modestr)
{
- int fmode = 0;
+ enum rb_io_mode fmode = 0;
const char *m = modestr, *p = NULL;
switch (*m++) {
case 'r':
- fmode |= FMODE_READABLE;
- break;
+ fmode |= FMODE_READABLE;
+ break;
case 'w':
- fmode |= FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE;
- break;
+ fmode |= FMODE_WRITABLE | FMODE_TRUNC | FMODE_CREATE;
+ break;
case 'a':
- fmode |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE;
- break;
+ fmode |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE;
+ break;
default:
- error:
- rb_raise(rb_eArgError, "invalid access mode %s", modestr);
+ goto error;
}
while (*m) {
switch (*m++) {
- case 'b':
+ case 'b':
fmode |= FMODE_BINMODE;
break;
- case 't':
+ case 't':
fmode |= FMODE_TEXTMODE;
break;
- case '+':
+ case '+':
fmode |= FMODE_READWRITE;
break;
- default:
+ case 'x':
+ if (modestr[0] != 'w')
+ goto error;
+ fmode |= FMODE_EXCL;
+ break;
+ default:
goto error;
- case ':':
- p = m;
+ case ':':
+ p = strchr(m, ':');
+ if (io_encname_bom_p(m, p ? (long)(p - m) : (long)strlen(m)))
+ fmode |= FMODE_SETENC_BY_BOM;
goto finished;
}
}
@@ -4827,41 +6554,46 @@ rb_io_modestr_fmode(const char *modestr)
finished:
if ((fmode & FMODE_BINMODE) && (fmode & FMODE_TEXTMODE))
goto error;
- if (p && io_encname_bom_p(p, 0))
- fmode |= FMODE_SETENC_BY_BOM;
return fmode;
+
+ error:
+ rb_raise(rb_eArgError, "invalid access mode %s", modestr);
+ UNREACHABLE_RETURN(Qundef);
}
int
rb_io_oflags_fmode(int oflags)
{
- int fmode = 0;
+ enum rb_io_mode fmode = 0;
- switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
+ switch (oflags & O_ACCMODE) {
case O_RDONLY:
- fmode = FMODE_READABLE;
- break;
+ fmode = FMODE_READABLE;
+ break;
case O_WRONLY:
- fmode = FMODE_WRITABLE;
- break;
+ fmode = FMODE_WRITABLE;
+ break;
case O_RDWR:
- fmode = FMODE_READWRITE;
- break;
+ fmode = FMODE_READWRITE;
+ break;
}
if (oflags & O_APPEND) {
- fmode |= FMODE_APPEND;
+ fmode |= FMODE_APPEND;
}
if (oflags & O_TRUNC) {
- fmode |= FMODE_TRUNC;
+ fmode |= FMODE_TRUNC;
}
if (oflags & O_CREAT) {
- fmode |= FMODE_CREATE;
+ fmode |= FMODE_CREATE;
+ }
+ if (oflags & O_EXCL) {
+ fmode |= FMODE_EXCL;
}
#ifdef O_BINARY
if (oflags & O_BINARY) {
- fmode |= FMODE_BINMODE;
+ fmode |= FMODE_BINMODE;
}
#endif
@@ -4869,7 +6601,7 @@ rb_io_oflags_fmode(int oflags)
}
static int
-rb_io_fmode_oflags(int fmode)
+rb_io_fmode_oflags(enum rb_io_mode fmode)
{
int oflags = 0;
@@ -4894,6 +6626,9 @@ rb_io_fmode_oflags(int fmode)
if (fmode & FMODE_CREATE) {
oflags |= O_CREAT;
}
+ if (fmode & FMODE_EXCL) {
+ oflags |= O_EXCL;
+ }
#ifdef O_BINARY
if (fmode & FMODE_BINMODE) {
oflags |= O_BINARY;
@@ -4917,24 +6652,31 @@ rb_io_oflags_modestr(int oflags)
#else
# define MODE_BINARY(a,b) (a)
#endif
- int accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR);
+ int accmode;
+ if (oflags & O_EXCL) {
+ rb_raise(rb_eArgError, "exclusive access mode is not supported");
+ }
+ accmode = oflags & (O_RDONLY|O_WRONLY|O_RDWR);
if (oflags & O_APPEND) {
- if (accmode == O_WRONLY) {
- return MODE_BINARY("a", "ab");
- }
- if (accmode == O_RDWR) {
- return MODE_BINARY("a+", "ab+");
- }
- }
- switch (oflags & (O_RDONLY|O_WRONLY|O_RDWR)) {
+ if (accmode == O_WRONLY) {
+ return MODE_BINARY("a", "ab");
+ }
+ if (accmode == O_RDWR) {
+ return MODE_BINARY("a+", "ab+");
+ }
+ }
+ switch (accmode) {
default:
- rb_raise(rb_eArgError, "invalid access oflags 0x%x", oflags);
+ rb_raise(rb_eArgError, "invalid access oflags 0x%x", oflags);
case O_RDONLY:
- return MODE_BINARY("r", "rb");
+ return MODE_BINARY("r", "rb");
case O_WRONLY:
- return MODE_BINARY("w", "wb");
+ return MODE_BINARY("w", "wb");
case O_RDWR:
- return MODE_BINARY("r+", "rb+");
+ if (oflags & O_TRUNC) {
+ return MODE_BINARY("w+", "wb+");
+ }
+ return MODE_BINARY("r+", "rb+");
}
}
@@ -4944,112 +6686,109 @@ rb_io_oflags_modestr(int oflags)
* Qnil => no encoding specified (internal only)
*/
static void
-rb_io_ext_int_to_encs(rb_encoding *ext, rb_encoding *intern, rb_encoding **enc, rb_encoding **enc2, int fmode)
+rb_io_ext_int_to_encs(rb_encoding *ext, rb_encoding *intern, rb_encoding **enc, rb_encoding **enc2, enum rb_io_mode fmode)
{
int default_ext = 0;
if (ext == NULL) {
- ext = rb_default_external_encoding();
- default_ext = 1;
+ ext = rb_default_external_encoding();
+ default_ext = 1;
}
- if (ext == rb_ascii8bit_encoding()) {
- /* If external is ASCII-8BIT, no transcoding */
- intern = NULL;
+ if (rb_is_ascii8bit_enc(ext)) {
+ /* If external is ASCII-8BIT, no transcoding */
+ intern = NULL;
}
else if (intern == NULL) {
- intern = rb_default_internal_encoding();
+ intern = rb_default_internal_encoding();
}
if (intern == NULL || intern == (rb_encoding *)Qnil ||
- (!(fmode & FMODE_SETENC_BY_BOM) && (intern == ext))) {
- /* No internal encoding => use external + no transcoding */
- *enc = (default_ext && intern != ext) ? NULL : ext;
- *enc2 = NULL;
+ (!(fmode & FMODE_SETENC_BY_BOM) && (intern == ext))) {
+ /* No internal encoding => use external + no transcoding */
+ *enc = (default_ext && intern != ext) ? NULL : ext;
+ *enc2 = NULL;
}
else {
- *enc = intern;
- *enc2 = ext;
+ *enc = intern;
+ *enc2 = ext;
}
}
static void
-unsupported_encoding(const char *name)
+unsupported_encoding(const char *name, rb_encoding *enc)
{
- rb_warn("Unsupported encoding %s ignored", name);
+ rb_enc_warn(enc, "Unsupported encoding %s ignored", name);
}
static void
-parse_mode_enc(const char *estr, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
+parse_mode_enc(const char *estr, rb_encoding *estr_enc,
+ rb_encoding **enc_p, rb_encoding **enc2_p, enum rb_io_mode *fmode_p)
{
const char *p;
char encname[ENCODING_MAXNAMELEN+1];
int idx, idx2;
- int fmode = fmode_p ? *fmode_p : 0;
+ enum rb_io_mode fmode = fmode_p ? *fmode_p : 0;
rb_encoding *ext_enc, *int_enc;
+ long len;
/* parse estr as "enc" or "enc2:enc" or "enc:-" */
p = strrchr(estr, ':');
- if (p) {
- long len = (p++) - estr;
- if (len == 0 || len > ENCODING_MAXNAMELEN)
- idx = -1;
- else {
- if (io_encname_bom_p(estr, len)) {
- fmode |= FMODE_SETENC_BY_BOM;
- estr += 4;
- len -= 4;
- }
- memcpy(encname, estr, len);
- encname[len] = '\0';
- estr = encname;
- idx = rb_enc_find_index(encname);
- }
+ len = p ? (p++ - estr) : (long)strlen(estr);
+ if ((fmode & FMODE_SETENC_BY_BOM) || io_encname_bom_p(estr, len)) {
+ estr += bom_prefix_len;
+ len -= bom_prefix_len;
+ if (!STRNCASECMP(estr, utf_prefix, utf_prefix_len)) {
+ fmode |= FMODE_SETENC_BY_BOM;
+ }
+ else {
+ rb_enc_warn(estr_enc, "BOM with non-UTF encoding %s is nonsense", estr);
+ fmode &= ~FMODE_SETENC_BY_BOM;
+ }
+ }
+ if (len == 0 || len > ENCODING_MAXNAMELEN) {
+ idx = -1;
}
else {
- long len = strlen(estr);
- if (io_encname_bom_p(estr, len)) {
- fmode |= FMODE_SETENC_BY_BOM;
- estr += 4;
- len -= 4;
- memcpy(encname, estr, len);
- encname[len] = '\0';
- estr = encname;
- }
- idx = rb_enc_find_index(estr);
+ if (p) {
+ memcpy(encname, estr, len);
+ encname[len] = '\0';
+ estr = encname;
+ }
+ idx = rb_enc_find_index(estr);
}
if (fmode_p) *fmode_p = fmode;
if (idx >= 0)
- ext_enc = rb_enc_from_index(idx);
+ ext_enc = rb_enc_from_index(idx);
else {
- if (idx != -2)
- unsupported_encoding(estr);
- ext_enc = NULL;
+ if (idx != -2)
+ unsupported_encoding(estr, estr_enc);
+ ext_enc = NULL;
}
int_enc = NULL;
if (p) {
- if (*p == '-' && *(p+1) == '\0') {
- /* Special case - "-" => no transcoding */
- int_enc = (rb_encoding *)Qnil;
- }
- else {
- idx2 = rb_enc_find_index(p);
- if (idx2 < 0)
- unsupported_encoding(p);
- else if (!(fmode & FMODE_SETENC_BY_BOM) && (idx2 == idx)) {
- int_enc = (rb_encoding *)Qnil;
- }
- else
- int_enc = rb_enc_from_index(idx2);
- }
+ if (*p == '-' && *(p+1) == '\0') {
+ /* Special case - "-" => no transcoding */
+ int_enc = (rb_encoding *)Qnil;
+ }
+ else {
+ idx2 = rb_enc_find_index(p);
+ if (idx2 < 0)
+ unsupported_encoding(p, estr_enc);
+ else if (!(fmode & FMODE_SETENC_BY_BOM) && (idx2 == idx)) {
+ int_enc = (rb_encoding *)Qnil;
+ }
+ else
+ int_enc = rb_enc_from_index(idx2);
+ }
}
rb_io_ext_int_to_encs(ext_enc, int_enc, enc_p, enc2_p, fmode);
}
int
-rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
+rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, enum rb_io_mode *fmode_p)
{
VALUE encoding=Qnil, extenc=Qundef, intenc=Qundef, tmp;
int extracted = 0;
@@ -5057,71 +6796,70 @@ rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2
rb_encoding *intencoding = NULL;
if (!NIL_P(opt)) {
- VALUE v;
- v = rb_hash_lookup2(opt, sym_encoding, Qnil);
- if (v != Qnil) encoding = v;
- v = rb_hash_lookup2(opt, sym_extenc, Qundef);
- if (v != Qnil) extenc = v;
- v = rb_hash_lookup2(opt, sym_intenc, Qundef);
- if (v != Qundef) intenc = v;
- }
- if ((extenc != Qundef || intenc != Qundef) && !NIL_P(encoding)) {
- if (!NIL_P(ruby_verbose)) {
- int idx = rb_to_encoding_index(encoding);
- rb_warn("Ignoring encoding parameter '%s': %s_encoding is used",
- idx < 0 ? StringValueCStr(encoding) : rb_enc_name(rb_enc_from_index(idx)),
- extenc == Qundef ? "internal" : "external");
- }
- encoding = Qnil;
- }
- if (extenc != Qundef && !NIL_P(extenc)) {
- extencoding = rb_to_encoding(extenc);
- }
- if (intenc != Qundef) {
- if (NIL_P(intenc)) {
- /* internal_encoding: nil => no transcoding */
- intencoding = (rb_encoding *)Qnil;
- }
- else if (!NIL_P(tmp = rb_check_string_type(intenc))) {
- char *p = StringValueCStr(tmp);
-
- if (*p == '-' && *(p+1) == '\0') {
- /* Special case - "-" => no transcoding */
- intencoding = (rb_encoding *)Qnil;
- }
- else {
- intencoding = rb_to_encoding(intenc);
- }
- }
- else {
- intencoding = rb_to_encoding(intenc);
- }
- if (extencoding == intencoding) {
- intencoding = (rb_encoding *)Qnil;
- }
+ VALUE v;
+ v = rb_hash_lookup2(opt, sym_encoding, Qnil);
+ if (v != Qnil) encoding = v;
+ v = rb_hash_lookup2(opt, sym_extenc, Qundef);
+ if (v != Qnil) extenc = v;
+ v = rb_hash_lookup2(opt, sym_intenc, Qundef);
+ if (!UNDEF_P(v)) intenc = v;
+ }
+ if ((!UNDEF_P(extenc) || !UNDEF_P(intenc)) && !NIL_P(encoding)) {
+ if (!NIL_P(ruby_verbose)) {
+ int idx = rb_to_encoding_index(encoding);
+ if (idx >= 0) encoding = rb_enc_from_encoding(rb_enc_from_index(idx));
+ rb_warn("Ignoring encoding parameter '%"PRIsVALUE"': %s_encoding is used",
+ encoding, UNDEF_P(extenc) ? "internal" : "external");
+ }
+ encoding = Qnil;
+ }
+ if (!UNDEF_P(extenc) && !NIL_P(extenc)) {
+ extencoding = rb_to_encoding(extenc);
+ }
+ if (!UNDEF_P(intenc)) {
+ if (NIL_P(intenc)) {
+ /* internal_encoding: nil => no transcoding */
+ intencoding = (rb_encoding *)Qnil;
+ }
+ else if (!NIL_P(tmp = rb_check_string_type(intenc))) {
+ char *p = StringValueCStr(tmp);
+
+ if (*p == '-' && *(p+1) == '\0') {
+ /* Special case - "-" => no transcoding */
+ intencoding = (rb_encoding *)Qnil;
+ }
+ else {
+ intencoding = rb_to_encoding(intenc);
+ }
+ }
+ else {
+ intencoding = rb_to_encoding(intenc);
+ }
+ if (extencoding == intencoding) {
+ intencoding = (rb_encoding *)Qnil;
+ }
}
if (!NIL_P(encoding)) {
- extracted = 1;
- if (!NIL_P(tmp = rb_check_string_type(encoding))) {
- parse_mode_enc(StringValueCStr(tmp), enc_p, enc2_p, fmode_p);
- }
- else {
- rb_io_ext_int_to_encs(rb_to_encoding(encoding), NULL, enc_p, enc2_p, 0);
- }
- }
- else if (extenc != Qundef || intenc != Qundef) {
extracted = 1;
- rb_io_ext_int_to_encs(extencoding, intencoding, enc_p, enc2_p, 0);
+ if (!NIL_P(tmp = rb_check_string_type(encoding))) {
+ parse_mode_enc(StringValueCStr(tmp), rb_enc_get(tmp),
+ enc_p, enc2_p, fmode_p);
+ }
+ else {
+ rb_io_ext_int_to_encs(rb_to_encoding(encoding), NULL, enc_p, enc2_p, 0);
+ }
+ }
+ else if (!UNDEF_P(extenc) || !UNDEF_P(intenc)) {
+ extracted = 1;
+ rb_io_ext_int_to_encs(extencoding, intencoding, enc_p, enc2_p, 0);
}
return extracted;
}
-typedef struct rb_io_enc_t convconfig_t;
-
static void
-validate_enc_binmode(int *fmode_p, int ecflags, rb_encoding *enc, rb_encoding *enc2)
+validate_enc_binmode(enum rb_io_mode *fmode_p, int ecflags, rb_encoding *enc, rb_encoding *enc2)
{
- int fmode = *fmode_p;
+ enum rb_io_mode fmode = *fmode_p;
if ((fmode & FMODE_READABLE) &&
!enc2 &&
@@ -5129,54 +6867,58 @@ validate_enc_binmode(int *fmode_p, int ecflags, rb_encoding *enc, rb_encoding *e
!rb_enc_asciicompat(enc ? enc : rb_default_external_encoding()))
rb_raise(rb_eArgError, "ASCII incompatible encoding needs binmode");
+ if ((fmode & FMODE_BINMODE) && (ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
+ rb_raise(rb_eArgError, "newline decorator with binary mode");
+ }
if (!(fmode & FMODE_BINMODE) &&
- (DEFAULT_TEXTMODE || (ecflags & ECONV_NEWLINE_DECORATOR_MASK))) {
- fmode |= DEFAULT_TEXTMODE;
- *fmode_p = fmode;
+ (DEFAULT_TEXTMODE || (ecflags & ECONV_NEWLINE_DECORATOR_MASK))) {
+ fmode |= FMODE_TEXTMODE;
+ *fmode_p = fmode;
}
#if !DEFAULT_TEXTMODE
else if (!(ecflags & ECONV_NEWLINE_DECORATOR_MASK)) {
- fmode &= ~FMODE_TEXTMODE;
- *fmode_p = fmode;
+ fmode &= ~FMODE_TEXTMODE;
+ *fmode_p = fmode;
}
#endif
}
static void
-extract_binmode(VALUE opthash, int *fmode)
+extract_binmode(VALUE opthash, enum rb_io_mode *fmode)
{
if (!NIL_P(opthash)) {
- VALUE v;
- v = rb_hash_aref(opthash, sym_textmode);
- if (!NIL_P(v)) {
- if (*fmode & FMODE_TEXTMODE)
- rb_raise(rb_eArgError, "textmode specified twice");
- if (*fmode & FMODE_BINMODE)
- rb_raise(rb_eArgError, "both textmode and binmode specified");
- if (RTEST(v))
- *fmode |= FMODE_TEXTMODE;
- }
- v = rb_hash_aref(opthash, sym_binmode);
- if (!NIL_P(v)) {
- if (*fmode & FMODE_BINMODE)
- rb_raise(rb_eArgError, "binmode specified twice");
- if (*fmode & FMODE_TEXTMODE)
- rb_raise(rb_eArgError, "both textmode and binmode specified");
- if (RTEST(v))
- *fmode |= FMODE_BINMODE;
- }
-
- if ((*fmode & FMODE_BINMODE) && (*fmode & FMODE_TEXTMODE))
- rb_raise(rb_eArgError, "both textmode and binmode specified");
+ VALUE v;
+ v = rb_hash_aref(opthash, sym_textmode);
+ if (!NIL_P(v)) {
+ if (*fmode & FMODE_TEXTMODE)
+ rb_raise(rb_eArgError, "textmode specified twice");
+ if (*fmode & FMODE_BINMODE)
+ rb_raise(rb_eArgError, "both textmode and binmode specified");
+ if (RTEST(v))
+ *fmode |= FMODE_TEXTMODE;
+ }
+ v = rb_hash_aref(opthash, sym_binmode);
+ if (!NIL_P(v)) {
+ if (*fmode & FMODE_BINMODE)
+ rb_raise(rb_eArgError, "binmode specified twice");
+ if (*fmode & FMODE_TEXTMODE)
+ rb_raise(rb_eArgError, "both textmode and binmode specified");
+ if (RTEST(v))
+ *fmode |= FMODE_BINMODE;
+ }
+
+ if ((*fmode & FMODE_BINMODE) && (*fmode & FMODE_TEXTMODE))
+ rb_raise(rb_eArgError, "both textmode and binmode specified");
}
}
-static void
+void
rb_io_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
- int *oflags_p, int *fmode_p, convconfig_t *convconfig_p)
+ int *oflags_p, enum rb_io_mode *fmode_p, struct rb_io_encoding *convconfig_p)
{
VALUE vmode;
- int oflags, fmode;
+ int oflags;
+ enum rb_io_mode fmode;
rb_encoding *enc, *enc2;
int ecflags;
VALUE ecopts;
@@ -5201,80 +6943,99 @@ rb_io_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
else {
const char *p;
- SafeStringValue(vmode);
+ StringValue(vmode);
p = StringValueCStr(vmode);
fmode = rb_io_modestr_fmode(p);
oflags = rb_io_fmode_oflags(fmode);
p = strchr(p, ':');
if (p) {
has_enc = 1;
- parse_mode_enc(p+1, &enc, &enc2, &fmode);
+ parse_mode_enc(p+1, rb_enc_get(vmode), &enc, &enc2, &fmode);
}
- else {
- rb_encoding *e;
+ else {
+ rb_encoding *e;
- e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
- rb_io_ext_int_to_encs(e, NULL, &enc, &enc2, fmode);
- }
+ e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
+ rb_io_ext_int_to_encs(e, NULL, &enc, &enc2, fmode);
+ }
}
if (NIL_P(opthash)) {
- ecflags = (fmode & FMODE_READABLE) ?
- MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
- 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
+ ecflags = (fmode & FMODE_READABLE) ?
+ MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
+ 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
- ecflags |= (fmode & FMODE_WRITABLE) ?
- MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
- 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
+ ecflags |= (fmode & FMODE_WRITABLE) ?
+ MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
+ 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
#endif
- SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
ecopts = Qnil;
+ if (fmode & FMODE_BINMODE) {
+#ifdef O_BINARY
+ oflags |= O_BINARY;
+#endif
+ if (!has_enc)
+ rb_io_ext_int_to_encs(rb_ascii8bit_encoding(), NULL, &enc, &enc2, fmode);
+ }
+#if DEFAULT_TEXTMODE
+ else if (NIL_P(vmode)) {
+ fmode |= DEFAULT_TEXTMODE;
+ }
+#endif
}
else {
- VALUE v;
- extract_binmode(opthash, &fmode);
- if (fmode & FMODE_BINMODE) {
+ VALUE v;
+ if (!has_vmode) {
+ v = rb_hash_aref(opthash, sym_mode);
+ if (!NIL_P(v)) {
+ if (!NIL_P(vmode)) {
+ rb_raise(rb_eArgError, "mode specified twice");
+ }
+ has_vmode = 1;
+ vmode = v;
+ goto vmode_handle;
+ }
+ }
+ v = rb_hash_aref(opthash, sym_flags);
+ if (!NIL_P(v)) {
+ v = rb_to_int(v);
+ oflags |= NUM2INT(v);
+ vmode = INT2NUM(oflags);
+ fmode = rb_io_oflags_fmode(oflags);
+ }
+ extract_binmode(opthash, &fmode);
+ if (fmode & FMODE_BINMODE) {
#ifdef O_BINARY
oflags |= O_BINARY;
#endif
- if (!has_enc)
- rb_io_ext_int_to_encs(rb_ascii8bit_encoding(), NULL, &enc, &enc2, fmode);
- }
+ if (!has_enc)
+ rb_io_ext_int_to_encs(rb_ascii8bit_encoding(), NULL, &enc, &enc2, fmode);
+ }
#if DEFAULT_TEXTMODE
- else if (NIL_P(vmode)) {
- fmode |= DEFAULT_TEXTMODE;
- }
-#endif
- if (!has_vmode) {
- v = rb_hash_aref(opthash, sym_mode);
- if (!NIL_P(v)) {
- if (!NIL_P(vmode)) {
- rb_raise(rb_eArgError, "mode specified twice");
- }
- has_vmode = 1;
- vmode = v;
- goto vmode_handle;
- }
- }
- v = rb_hash_aref(opthash, sym_perm);
- if (!NIL_P(v)) {
- if (vperm_p) {
- if (!NIL_P(*vperm_p)) {
- rb_raise(rb_eArgError, "perm specified twice");
- }
- *vperm_p = v;
- }
- else {
- /* perm no use, just ignore */
- }
- }
- ecflags = (fmode & FMODE_READABLE) ?
- MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
- 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
+ else if (NIL_P(vmode)) {
+ fmode |= DEFAULT_TEXTMODE;
+ }
+#endif
+ v = rb_hash_aref(opthash, sym_perm);
+ if (!NIL_P(v)) {
+ if (vperm_p) {
+ if (!NIL_P(*vperm_p)) {
+ rb_raise(rb_eArgError, "perm specified twice");
+ }
+ *vperm_p = v;
+ }
+ else {
+ /* perm no use, just ignore */
+ }
+ }
+ ecflags = (fmode & FMODE_READABLE) ?
+ MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
+ 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
- ecflags |= (fmode & FMODE_WRITABLE) ?
- MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
- 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
+ ecflags |= (fmode & FMODE_WRITABLE) ?
+ MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
+ 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
#endif
if (rb_io_extract_encoding_option(opthash, &enc, &enc2, &fmode)) {
@@ -5282,8 +7043,8 @@ rb_io_extract_modeenc(VALUE *vmode_p, VALUE *vperm_p, VALUE opthash,
rb_raise(rb_eArgError, "encoding specified twice");
}
}
- SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
- ecflags = rb_econv_prepare_options(opthash, &ecopts, ecflags);
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
+ ecflags = rb_econv_prepare_options(opthash, &ecopts, ecflags);
}
validate_enc_binmode(&fmode, ecflags, enc, enc2);
@@ -5316,7 +7077,9 @@ static inline int
rb_sysopen_internal(struct sysopen_struct *data)
{
int fd;
- fd = (int)(VALUE)rb_thread_call_without_gvl(sysopen_func, data, RUBY_UBF_IO, 0);
+ do {
+ fd = IO_WITHOUT_GVL_INT(sysopen_func, data);
+ } while (fd < 0 && errno == EINTR);
if (0 <= fd)
rb_update_max_fd(fd);
return fd;
@@ -5325,28 +7088,22 @@ rb_sysopen_internal(struct sysopen_struct *data)
static int
rb_sysopen(VALUE fname, int oflags, mode_t perm)
{
- int fd;
+ int fd = -1;
struct sysopen_struct data;
data.fname = rb_str_encode_ospath(fname);
+ StringValueCStr(data.fname);
data.oflags = oflags;
data.perm = perm;
- fd = rb_sysopen_internal(&data);
- if (fd < 0) {
- if (errno == EMFILE || errno == ENFILE) {
- rb_gc();
- fd = rb_sysopen_internal(&data);
- }
- if (fd < 0) {
- rb_sys_fail_path(fname);
- }
+ TRY_WITH_GC((fd = rb_sysopen_internal(&data)) >= 0) {
+ rb_syserr_fail_path(first_errno, fname);
}
return fd;
}
-FILE *
-rb_fdopen(int fd, const char *modestr)
+static inline FILE *
+fdopen_internal(int fd, const char *modestr)
{
FILE *file;
@@ -5355,40 +7112,39 @@ rb_fdopen(int fd, const char *modestr)
#endif
file = fdopen(fd, modestr);
if (!file) {
- if (
-#if defined(__sun)
- errno == 0 ||
-#endif
- errno == EMFILE || errno == ENFILE) {
- rb_gc();
-#if defined(__sun)
- errno = 0;
-#endif
- file = fdopen(fd, modestr);
- }
- if (!file) {
#ifdef _WIN32
- if (errno == 0) errno = EINVAL;
+ if (errno == 0) errno = EINVAL;
#elif defined(__sun)
- if (errno == 0) errno = EMFILE;
+ if (errno == 0) errno = EMFILE;
#endif
- rb_sys_fail(0);
- }
+ }
+ return file;
+}
+
+FILE *
+rb_fdopen(int fd, const char *modestr)
+{
+ FILE *file = 0;
+
+ TRY_WITH_GC((file = fdopen_internal(fd, modestr)) != 0) {
+ rb_syserr_fail(first_errno, 0);
}
/* xxx: should be _IONBF? A buffer in FILE may have trouble. */
#ifdef USE_SETVBUF
if (setvbuf(file, NULL, _IOFBF, 0) != 0)
- rb_warn("setvbuf() can't be honoured (fd=%d)", fd);
+ rb_warn("setvbuf() can't be honoured (fd=%d)", fd);
#endif
return file;
}
-static void
+static int
io_check_tty(rb_io_t *fptr)
{
- if (isatty(fptr->fd))
+ int t = isatty(fptr->fd);
+ if (t)
fptr->mode |= FMODE_TTY|FMODE_DUPLEX;
+ return t;
}
static VALUE rb_io_internal_encoding(VALUE);
@@ -5398,101 +7154,113 @@ static int
io_strip_bom(VALUE io)
{
VALUE b1, b2, b3, b4;
+ rb_io_t *fptr;
+ GetOpenFile(io, fptr);
+ if (!(fptr->mode & FMODE_READABLE)) return 0;
if (NIL_P(b1 = rb_io_getbyte(io))) return 0;
switch (b1) {
case INT2FIX(0xEF):
- if (NIL_P(b2 = rb_io_getbyte(io))) break;
- if (b2 == INT2FIX(0xBB) && !NIL_P(b3 = rb_io_getbyte(io))) {
- if (b3 == INT2FIX(0xBF)) {
- return rb_utf8_encindex();
- }
- rb_io_ungetbyte(io, b3);
- }
- rb_io_ungetbyte(io, b2);
- break;
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0xBB) && !NIL_P(b3 = rb_io_getbyte(io))) {
+ if (b3 == INT2FIX(0xBF)) {
+ return rb_utf8_encindex();
+ }
+ rb_io_ungetbyte(io, b3);
+ }
+ rb_io_ungetbyte(io, b2);
+ break;
case INT2FIX(0xFE):
- if (NIL_P(b2 = rb_io_getbyte(io))) break;
- if (b2 == INT2FIX(0xFF)) {
- return ENCINDEX_UTF_16BE;
- }
- rb_io_ungetbyte(io, b2);
- break;
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0xFF)) {
+ return ENCINDEX_UTF_16BE;
+ }
+ rb_io_ungetbyte(io, b2);
+ break;
case INT2FIX(0xFF):
- if (NIL_P(b2 = rb_io_getbyte(io))) break;
- if (b2 == INT2FIX(0xFE)) {
- b3 = rb_io_getbyte(io);
- if (b3 == INT2FIX(0) && !NIL_P(b4 = rb_io_getbyte(io))) {
- if (b4 == INT2FIX(0)) {
- return ENCINDEX_UTF_32LE;
- }
- rb_io_ungetbyte(io, b4);
- rb_io_ungetbyte(io, b3);
- }
- else {
- rb_io_ungetbyte(io, b3);
- return ENCINDEX_UTF_16LE;
- }
- }
- rb_io_ungetbyte(io, b2);
- break;
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0xFE)) {
+ b3 = rb_io_getbyte(io);
+ if (b3 == INT2FIX(0) && !NIL_P(b4 = rb_io_getbyte(io))) {
+ if (b4 == INT2FIX(0)) {
+ return ENCINDEX_UTF_32LE;
+ }
+ rb_io_ungetbyte(io, b4);
+ }
+ rb_io_ungetbyte(io, b3);
+ return ENCINDEX_UTF_16LE;
+ }
+ rb_io_ungetbyte(io, b2);
+ break;
case INT2FIX(0):
- if (NIL_P(b2 = rb_io_getbyte(io))) break;
- if (b2 == INT2FIX(0) && !NIL_P(b3 = rb_io_getbyte(io))) {
- if (b3 == INT2FIX(0xFE) && !NIL_P(b4 = rb_io_getbyte(io))) {
- if (b4 == INT2FIX(0xFF)) {
- return ENCINDEX_UTF_32BE;
- }
- rb_io_ungetbyte(io, b4);
- }
- rb_io_ungetbyte(io, b3);
- }
- rb_io_ungetbyte(io, b2);
- break;
+ if (NIL_P(b2 = rb_io_getbyte(io))) break;
+ if (b2 == INT2FIX(0) && !NIL_P(b3 = rb_io_getbyte(io))) {
+ if (b3 == INT2FIX(0xFE) && !NIL_P(b4 = rb_io_getbyte(io))) {
+ if (b4 == INT2FIX(0xFF)) {
+ return ENCINDEX_UTF_32BE;
+ }
+ rb_io_ungetbyte(io, b4);
+ }
+ rb_io_ungetbyte(io, b3);
+ }
+ rb_io_ungetbyte(io, b2);
+ break;
}
rb_io_ungetbyte(io, b1);
return 0;
}
-static void
+static rb_encoding *
io_set_encoding_by_bom(VALUE io)
{
int idx = io_strip_bom(io);
rb_io_t *fptr;
+ rb_encoding *extenc = NULL;
GetOpenFile(io, fptr);
if (idx) {
- io_encoding_set(fptr, rb_enc_from_encoding(rb_enc_from_index(idx)),
- rb_io_internal_encoding(io), Qnil);
+ extenc = rb_enc_from_index(idx);
+ io_encoding_set(fptr, rb_enc_from_encoding(extenc),
+ rb_io_internal_encoding(io), Qnil);
}
else {
- fptr->encs.enc2 = NULL;
+ fptr->encs.enc2 = NULL;
}
+ return extenc;
}
static VALUE
-rb_file_open_generic(VALUE io, VALUE filename, int oflags, int fmode, convconfig_t *convconfig, mode_t perm)
+rb_file_open_generic(VALUE io, VALUE filename, int oflags, enum rb_io_mode fmode,
+ const struct rb_io_encoding *convconfig, mode_t perm)
{
+ VALUE pathv;
rb_io_t *fptr;
- convconfig_t cc;
+ struct rb_io_encoding cc;
if (!convconfig) {
- /* Set to default encodings */
- rb_io_ext_int_to_encs(NULL, NULL, &cc.enc, &cc.enc2, fmode);
+ /* Set to default encodings */
+ rb_io_ext_int_to_encs(NULL, NULL, &cc.enc, &cc.enc2, fmode);
cc.ecflags = 0;
cc.ecopts = Qnil;
convconfig = &cc;
}
validate_enc_binmode(&fmode, convconfig->ecflags,
- convconfig->enc, convconfig->enc2);
+ convconfig->enc, convconfig->enc2);
MakeOpenFile(io, fptr);
fptr->mode = fmode;
fptr->encs = *convconfig;
- fptr->pathv = rb_str_new_frozen(filename);
- fptr->fd = rb_sysopen(fptr->pathv, oflags, perm);
+ pathv = rb_str_new_frozen(filename);
+#ifdef O_TMPFILE
+ if (!(oflags & O_TMPFILE)) {
+ fptr->pathv = pathv;
+ }
+#else
+ fptr->pathv = pathv;
+#endif
+ fptr->fd = rb_sysopen(pathv, oflags, perm);
io_check_tty(fptr);
if (fmode & FMODE_SETENC_BY_BOM) io_set_encoding_by_bom(io);
@@ -5502,23 +7270,33 @@ rb_file_open_generic(VALUE io, VALUE filename, int oflags, int fmode, convconfig
static VALUE
rb_file_open_internal(VALUE io, VALUE filename, const char *modestr)
{
- int fmode = rb_io_modestr_fmode(modestr);
+ enum rb_io_mode fmode = rb_io_modestr_fmode(modestr);
const char *p = strchr(modestr, ':');
- convconfig_t convconfig;
+ struct rb_io_encoding convconfig;
if (p) {
- parse_mode_enc(p+1, &convconfig.enc, &convconfig.enc2, &fmode);
+ parse_mode_enc(p+1, rb_usascii_encoding(),
+ &convconfig.enc, &convconfig.enc2, &fmode);
}
else {
- rb_encoding *e;
- /* Set to default encodings */
+ rb_encoding *e;
+ /* Set to default encodings */
- e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
- rb_io_ext_int_to_encs(e, NULL, &convconfig.enc, &convconfig.enc2, fmode);
- convconfig.ecflags = 0;
- convconfig.ecopts = Qnil;
+ e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
+ rb_io_ext_int_to_encs(e, NULL, &convconfig.enc, &convconfig.enc2, fmode);
}
+ convconfig.ecflags = (fmode & FMODE_READABLE) ?
+ MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
+ 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
+#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
+ convconfig.ecflags |= (fmode & FMODE_WRITABLE) ?
+ MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
+ 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
+#endif
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(convconfig.enc2, convconfig.ecflags);
+ convconfig.ecopts = Qnil;
+
return rb_file_open_generic(io, filename,
rb_io_fmode_oflags(fmode),
fmode,
@@ -5539,7 +7317,7 @@ rb_file_open(const char *fname, const char *modestr)
return rb_file_open_internal(io_alloc(rb_cFile), rb_str_new_cstr(fname), modestr);
}
-#if defined(__CYGWIN__) || !defined(HAVE_FORK)
+#if defined(__CYGWIN__) || !defined(HAVE_WORKING_FORK)
static struct pipe_list {
rb_io_t *fptr;
struct pipe_list *next;
@@ -5559,26 +7337,20 @@ pipe_add_fptr(rb_io_t *fptr)
static void
pipe_del_fptr(rb_io_t *fptr)
{
- struct pipe_list *list = pipe_list;
+ struct pipe_list **prev = &pipe_list;
struct pipe_list *tmp;
- if (list->fptr == fptr) {
- pipe_list = list->next;
- free(list);
- return;
- }
-
- while (list->next) {
- if (list->next->fptr == fptr) {
- tmp = list->next;
- list->next = list->next->next;
- free(tmp);
- return;
- }
- list = list->next;
+ while ((tmp = *prev) != 0) {
+ if (tmp->fptr == fptr) {
+ *prev = tmp->next;
+ free(tmp);
+ return;
+ }
+ prev = &tmp->next;
}
}
+#if defined (_WIN32) || defined(__CYGWIN__)
static void
pipe_atexit(void)
{
@@ -5586,19 +7358,20 @@ pipe_atexit(void)
struct pipe_list *tmp;
while (list) {
- tmp = list->next;
- rb_io_fptr_finalize(list->fptr);
- list = tmp;
+ tmp = list->next;
+ rb_io_fptr_finalize(list->fptr);
+ list = tmp;
}
}
+#endif
static void
pipe_finalize(rb_io_t *fptr, int noraise)
{
-#if !defined(HAVE_FORK) && !defined(_WIN32)
+#if !defined(HAVE_WORKING_FORK) && !defined(_WIN32)
int status = 0;
if (fptr->stdio_file) {
- status = pclose(fptr->stdio_file);
+ status = pclose(fptr->stdio_file);
}
fptr->fd = -1;
fptr->stdio_file = 0;
@@ -5610,6 +7383,31 @@ pipe_finalize(rb_io_t *fptr, int noraise)
}
#endif
+static void
+fptr_copy_finalizer(rb_io_t *fptr, const rb_io_t *orig)
+{
+#if defined(__CYGWIN__) || !defined(HAVE_WORKING_FORK)
+ void (*const old_finalize)(struct rb_io*,int) = fptr->finalize;
+
+ if (old_finalize == orig->finalize) return;
+#endif
+
+ fptr->finalize = orig->finalize;
+
+#if defined(__CYGWIN__) || !defined(HAVE_WORKING_FORK)
+ if (old_finalize != pipe_finalize) {
+ struct pipe_list *list;
+ for (list = pipe_list; list; list = list->next) {
+ if (list->fptr == fptr) break;
+ }
+ if (!list) pipe_add_fptr(fptr);
+ }
+ else {
+ pipe_del_fptr(fptr);
+ }
+#endif
+}
+
void
rb_io_synchronized(rb_io_t *fptr)
{
@@ -5627,13 +7425,7 @@ int
rb_pipe(int *pipes)
{
int ret;
- ret = rb_cloexec_pipe(pipes);
- if (ret == -1) {
- if (errno == EMFILE || errno == ENFILE) {
- rb_gc();
- ret = rb_cloexec_pipe(pipes);
- }
- }
+ TRY_WITH_GC((ret = rb_cloexec_pipe(pipes)) >= 0);
if (ret == 0) {
rb_update_max_fd(pipes[0]);
rb_update_max_fd(pipes[1]);
@@ -5647,7 +7439,7 @@ rb_pipe(int *pipes)
#define spawn(mode, cmd) rb_w32_uspawn((mode), (cmd), 0)
#endif
-#if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
+#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
struct popen_arg {
VALUE execarg_obj;
struct rb_execarg *eargp;
@@ -5657,7 +7449,8 @@ struct popen_arg {
};
#endif
-#ifdef HAVE_FORK
+#ifdef HAVE_WORKING_FORK
+# ifndef __EMSCRIPTEN__
static void
popen_redirect(struct popen_arg *p)
{
@@ -5688,6 +7481,7 @@ popen_redirect(struct popen_arg *p)
}
}
}
+# endif
#if defined(__linux__)
/* Linux /proc/self/status contains a line: "FDSize:\t<nnn>\n"
@@ -5706,9 +7500,9 @@ linux_get_maxfd(void)
char buf[4096], *p, *np, *e;
ssize_t ss;
fd = rb_cloexec_open("/proc/self/status", O_RDONLY|O_NOCTTY, 0);
- if (fd == -1) return -1;
+ if (fd < 0) return fd;
ss = read(fd, buf, sizeof(buf));
- if (ss == -1) goto err;
+ if (ss < 0) goto err;
p = buf;
e = buf + ss;
while ((int)sizeof("FDSize:\t0\n")-1 <= e-p &&
@@ -5727,7 +7521,7 @@ linux_get_maxfd(void)
err:
close(fd);
- return -1;
+ return (int)ss;
}
#endif
@@ -5735,37 +7529,40 @@ linux_get_maxfd(void)
void
rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
{
+#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
int fd, ret;
int max = (int)max_file_descriptor;
-#ifdef F_MAXFD
+# ifdef F_MAXFD
/* F_MAXFD is available since NetBSD 2.0. */
ret = fcntl(0, F_MAXFD); /* async-signal-safe */
if (ret != -1)
maxhint = max = ret;
-#elif defined(__linux__)
+# elif defined(__linux__)
ret = linux_get_maxfd();
if (maxhint < ret)
maxhint = ret;
/* maxhint = max = ret; if (ret == -1) abort(); // test */
-#endif
+# endif
if (max < maxhint)
max = maxhint;
for (fd = lowfd; fd <= max; fd++) {
if (!NIL_P(noclose_fds) &&
RTEST(rb_hash_lookup(noclose_fds, INT2FIX(fd)))) /* async-signal-safe */
continue;
- ret = fcntl(fd, F_GETFD); /* async-signal-safe */
- if (ret != -1 && !(ret & FD_CLOEXEC)) {
+ ret = fcntl(fd, F_GETFD); /* async-signal-safe */
+ if (ret != -1 && !(ret & FD_CLOEXEC)) {
fcntl(fd, F_SETFD, ret|FD_CLOEXEC); /* async-signal-safe */
}
-#define CONTIGUOUS_CLOSED_FDS 20
+# define CONTIGUOUS_CLOSED_FDS 20
if (ret != -1) {
- if (max < fd + CONTIGUOUS_CLOSED_FDS)
- max = fd + CONTIGUOUS_CLOSED_FDS;
- }
+ if (max < fd + CONTIGUOUS_CLOSED_FDS)
+ max = fd + CONTIGUOUS_CLOSED_FDS;
+ }
}
+#endif
}
+# ifndef __EMSCRIPTEN__
static int
popen_exec(void *pp, char *errmsg, size_t errmsg_len)
{
@@ -5773,10 +7570,24 @@ popen_exec(void *pp, char *errmsg, size_t errmsg_len)
return rb_exec_async_signal_safe(p->eargp, errmsg, errmsg_len);
}
+# endif
+#endif
+
+#if (defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)) && !defined __EMSCRIPTEN__
+static VALUE
+rb_execarg_fixup_v(VALUE execarg_obj)
+{
+ rb_execarg_parent_start(execarg_obj);
+ return Qnil;
+}
+#else
+char *rb_execarg_commandline(const struct rb_execarg *eargp, VALUE *prog);
#endif
+#ifndef __EMSCRIPTEN__
static VALUE
-pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convconfig)
+pipe_open(VALUE execarg_obj, const char *modestr, enum rb_io_mode fmode,
+ const struct rb_io_encoding *convconfig)
{
struct rb_execarg *eargp = NIL_P(execarg_obj) ? NULL : rb_execarg_get(execarg_obj);
VALUE prog = eargp ? (eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name) : Qfalse ;
@@ -5785,55 +7596,52 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convc
VALUE port;
rb_io_t *write_fptr;
VALUE write_port;
-#if defined(HAVE_FORK)
+#if defined(HAVE_WORKING_FORK)
int status;
char errmsg[80] = { '\0' };
#endif
-#if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
+#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
+ int state;
struct popen_arg arg;
- int e = 0;
#endif
+ int e = 0;
#if defined(HAVE_SPAWNV)
# if defined(HAVE_SPAWNVE)
# define DO_SPAWN(cmd, args, envp) ((args) ? \
- spawnve(P_NOWAIT, (cmd), (args), (envp)) : \
- spawne(P_NOWAIT, (cmd), (envp)))
+ spawnve(P_NOWAIT, (cmd), (args), (envp)) : \
+ spawne(P_NOWAIT, (cmd), (envp)))
# else
# define DO_SPAWN(cmd, args, envp) ((args) ? \
- spawnv(P_NOWAIT, (cmd), (args)) : \
- spawn(P_NOWAIT, (cmd)))
+ spawnv(P_NOWAIT, (cmd), (args)) : \
+ spawn(P_NOWAIT, (cmd)))
# endif
-# if !defined(HAVE_FORK)
+# if !defined(HAVE_WORKING_FORK)
char **args = NULL;
# if defined(HAVE_SPAWNVE)
char **envp = NULL;
# endif
# endif
#endif
-#if !defined(HAVE_FORK)
+#if !defined(HAVE_WORKING_FORK)
struct rb_execarg sarg, *sargp = &sarg;
#endif
FILE *fp = 0;
int fd = -1;
int write_fd = -1;
-#if !defined(HAVE_FORK)
+#if !defined(HAVE_WORKING_FORK)
const char *cmd = 0;
-#if !defined(HAVE_SPAWNV)
- int argc;
- VALUE *argv;
-#endif
if (prog)
cmd = StringValueCStr(prog);
#endif
-#if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
+#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
arg.execarg_obj = execarg_obj;
arg.eargp = eargp;
arg.modef = fmode;
arg.pair[0] = arg.pair[1] = -1;
arg.write_pair[0] = arg.write_pair[1] = -1;
-# if !defined(HAVE_FORK)
+# if !defined(HAVE_WORKING_FORK)
if (eargp && !eargp->use_shell) {
args = ARGVSTR2ARGV(eargp->invoke.cmd.argv_str);
}
@@ -5843,89 +7651,96 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convc
if (rb_pipe(arg.write_pair) < 0)
rb_sys_fail_str(prog);
if (rb_pipe(arg.pair) < 0) {
- int e = errno;
+ e = errno;
close(arg.write_pair[0]);
close(arg.write_pair[1]);
- errno = e;
- rb_sys_fail_str(prog);
+ rb_syserr_fail_str(e, prog);
}
if (eargp) {
rb_execarg_addopt(execarg_obj, INT2FIX(0), INT2FIX(arg.write_pair[0]));
rb_execarg_addopt(execarg_obj, INT2FIX(1), INT2FIX(arg.pair[1]));
}
- break;
+ break;
case FMODE_READABLE:
if (rb_pipe(arg.pair) < 0)
rb_sys_fail_str(prog);
if (eargp)
rb_execarg_addopt(execarg_obj, INT2FIX(1), INT2FIX(arg.pair[1]));
- break;
+ break;
case FMODE_WRITABLE:
if (rb_pipe(arg.pair) < 0)
rb_sys_fail_str(prog);
if (eargp)
rb_execarg_addopt(execarg_obj, INT2FIX(0), INT2FIX(arg.pair[0]));
- break;
+ break;
default:
rb_sys_fail_str(prog);
}
if (!NIL_P(execarg_obj)) {
- rb_execarg_fixup(execarg_obj);
-# if defined(HAVE_FORK)
- pid = rb_fork_async_signal_safe(&status, popen_exec, &arg, arg.eargp->redirect_fds, errmsg, sizeof(errmsg));
+ rb_protect(rb_execarg_fixup_v, execarg_obj, &state);
+ if (state) {
+ if (0 <= arg.write_pair[0]) close(arg.write_pair[0]);
+ if (0 <= arg.write_pair[1]) close(arg.write_pair[1]);
+ if (0 <= arg.pair[0]) close(arg.pair[0]);
+ if (0 <= arg.pair[1]) close(arg.pair[1]);
+ rb_execarg_parent_end(execarg_obj);
+ rb_jump_tag(state);
+ }
+
+# if defined(HAVE_WORKING_FORK)
+ pid = rb_fork_async_signal_safe(&status, popen_exec, &arg, arg.eargp->redirect_fds, errmsg, sizeof(errmsg));
# else
- rb_execarg_run_options(eargp, sargp, NULL, 0);
+ rb_execarg_run_options(eargp, sargp, NULL, 0);
# if defined(HAVE_SPAWNVE)
- if (eargp->envp_str) envp = (char **)RSTRING_PTR(eargp->envp_str);
+ if (eargp->envp_str) envp = (char **)RSTRING_PTR(eargp->envp_str);
# endif
- while ((pid = DO_SPAWN(cmd, args, envp)) == -1) {
- /* exec failed */
- switch (e = errno) {
- case EAGAIN:
-# if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
- case EWOULDBLOCK:
+ while ((pid = DO_SPAWN(cmd, args, envp)) < 0) {
+ /* exec failed */
+ switch (e = errno) {
+ case EAGAIN:
+# if EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
# endif
- rb_thread_sleep(1);
- continue;
- }
- break;
- }
- if (eargp)
- rb_execarg_run_options(sargp, NULL, NULL, 0);
+ rb_thread_sleep(1);
+ continue;
+ }
+ break;
+ }
+ if (eargp)
+ rb_execarg_run_options(sargp, NULL, NULL, 0);
# endif
+ rb_execarg_parent_end(execarg_obj);
}
else {
-# if defined(HAVE_FORK)
- pid = rb_fork_ruby(&status);
- if (pid == 0) { /* child */
- rb_thread_atfork();
- popen_redirect(&arg);
- rb_io_synchronized(RFILE(orig_stdout)->fptr);
- rb_io_synchronized(RFILE(orig_stderr)->fptr);
- return Qnil;
- }
+# if defined(HAVE_WORKING_FORK)
+ pid = rb_call_proc__fork();
+ if (pid == 0) { /* child */
+ popen_redirect(&arg);
+ rb_io_synchronized(RFILE(orig_stdout)->fptr);
+ rb_io_synchronized(RFILE(orig_stderr)->fptr);
+ return Qnil;
+ }
# else
- rb_notimplement();
+ rb_notimplement();
# endif
}
/* parent */
- if (pid == -1) {
-# if defined(HAVE_FORK)
- e = errno;
+ if (pid < 0) {
+# if defined(HAVE_WORKING_FORK)
+ e = errno;
# endif
- close(arg.pair[0]);
- close(arg.pair[1]);
+ close(arg.pair[0]);
+ close(arg.pair[1]);
if ((fmode & (FMODE_READABLE|FMODE_WRITABLE)) == (FMODE_READABLE|FMODE_WRITABLE)) {
close(arg.write_pair[0]);
close(arg.write_pair[1]);
}
- errno = e;
-# if defined(HAVE_FORK)
+# if defined(HAVE_WORKING_FORK)
if (errmsg[0])
- rb_sys_fail(errmsg);
+ rb_syserr_fail(e, errmsg);
# endif
- rb_sys_fail_str(prog);
+ rb_syserr_fail_str(e, prog);
}
if ((fmode & FMODE_READABLE) && (fmode & FMODE_WRITABLE)) {
close(arg.pair[1]);
@@ -5942,18 +7757,18 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convc
fd = arg.pair[1];
}
#else
- if (argc) {
- prog = rb_ary_join(rb_ary_new4(argc, argv), rb_str_new2(" "));
- cmd = StringValueCStr(prog);
- }
+ cmd = rb_execarg_commandline(eargp, &prog);
if (!NIL_P(execarg_obj)) {
- rb_execarg_fixup(execarg_obj);
- rb_execarg_run_options(eargp, sargp, NULL, 0);
+ rb_execarg_parent_start(execarg_obj);
+ rb_execarg_run_options(eargp, sargp, NULL, 0);
}
fp = popen(cmd, modestr);
- if (eargp)
- rb_execarg_run_options(sargp, NULL, NULL, 0);
- if (!fp) rb_sys_fail_path(prog);
+ e = errno;
+ if (eargp) {
+ rb_execarg_parent_end(execarg_obj);
+ rb_execarg_run_options(sargp, NULL, NULL, 0);
+ }
+ if (!fp) rb_syserr_fail_path(e, prog);
fd = fileno(fp);
#endif
@@ -5964,20 +7779,20 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convc
fptr->mode = fmode | FMODE_SYNC|FMODE_DUPLEX;
if (convconfig) {
fptr->encs = *convconfig;
-#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
- if (fptr->encs.ecflags & ECONV_DEFAULT_NEWLINE_DECORATOR) {
- fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
- }
+#if RUBY_CRLF_ENVIRONMENT
+ if (fptr->encs.ecflags & ECONV_DEFAULT_NEWLINE_DECORATOR) {
+ fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
+ }
#endif
}
else {
- if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {
- fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
- }
+ if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) {
+ fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
+ }
#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
- if (NEED_NEWLINE_DECORATOR_ON_WRITE(fptr)) {
- fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
- }
+ if (NEED_NEWLINE_DECORATOR_ON_WRITE(fptr)) {
+ fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
+ }
#endif
}
fptr->pid = pid;
@@ -5992,117 +7807,206 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convc
rb_ivar_set(port, rb_intern("@tied_io_for_writing"), write_port);
}
-#if defined (__CYGWIN__) || !defined(HAVE_FORK)
+#if defined (__CYGWIN__) || !defined(HAVE_WORKING_FORK)
fptr->finalize = pipe_finalize;
pipe_add_fptr(fptr);
#endif
return port;
}
+#else
+static VALUE
+pipe_open(VALUE execarg_obj, const char *modestr, enum rb_io_mode fmode,
+ const struct rb_io_encoding *convconfig)
+{
+ rb_raise(rb_eNotImpError, "popen() is not available");
+}
+#endif
static int
is_popen_fork(VALUE prog)
{
if (RSTRING_LEN(prog) == 1 && RSTRING_PTR(prog)[0] == '-') {
-#if !defined(HAVE_FORK)
- rb_raise(rb_eNotImpError,
- "fork() function is unimplemented on this machine");
+#if !defined(HAVE_WORKING_FORK)
+ rb_raise(rb_eNotImpError,
+ "fork() function is unimplemented on this machine");
#else
- return TRUE;
+ return TRUE;
#endif
}
return FALSE;
}
static VALUE
-pipe_open_s(VALUE prog, const char *modestr, int fmode, convconfig_t *convconfig)
+pipe_open_s(VALUE prog, const char *modestr, enum rb_io_mode fmode,
+ const struct rb_io_encoding *convconfig)
{
int argc = 1;
VALUE *argv = &prog;
VALUE execarg_obj = Qnil;
if (!is_popen_fork(prog))
- execarg_obj = rb_execarg_new(argc, argv, TRUE);
+ execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
return pipe_open(execarg_obj, modestr, fmode, convconfig);
}
+static VALUE
+pipe_close(VALUE io)
+{
+ rb_io_t *fptr = io_close_fptr(io);
+ if (fptr) {
+ fptr_waitpid(fptr, rb_thread_to_be_killed(rb_thread_current()));
+ }
+ return Qnil;
+}
+
+static VALUE popen_finish(VALUE port, VALUE klass);
+
/*
* call-seq:
- * IO.popen([env,] cmd, mode="r" [, opt]) -> io
- * IO.popen([env,] cmd, mode="r" [, opt]) {|io| block } -> obj
+ * IO.popen(env = {}, cmd, mode = 'r', **opts) -> io
+ * IO.popen(env = {}, cmd, mode = 'r', **opts) {|io| ... } -> object
*
- * Runs the specified command as a subprocess; the subprocess's
- * standard input and output will be connected to the returned
- * <code>IO</code> object.
+ * Executes the given command +cmd+ as a subprocess
+ * whose $stdin and $stdout are connected to a new stream +io+.
*
- * The PID of the started process can be obtained by IO#pid method.
+ * This method has potential security vulnerabilities if called with untrusted input;
+ * see {Command Injection}[rdoc-ref:security/command_injection.rdoc].
*
- * _cmd_ is a string or an array as follows.
+ * If no block is given, returns the new stream,
+ * which depending on given +mode+ may be open for reading, writing, or both.
+ * The stream should be explicitly closed (eventually) to avoid resource leaks.
*
- * cmd:
- * "-" : fork
- * commandline : command line string which is passed to a shell
- * [env, cmdname, arg1, ..., opts] : command name and zero or more arguments (no shell)
- * [env, [cmdname, argv0], arg1, ..., opts] : command name, argv[0] and zero or more arguments (no shell)
- * (env and opts are optional.)
+ * If a block is given, the stream is passed to the block
+ * (again, open for reading, writing, or both);
+ * when the block exits, the stream is closed,
+ * the block's value is returned,
+ * and the global variable <tt>$?</tt> is set to the child's exit status.
*
- * If _cmd_ is a +String+ ``<code>-</code>'',
- * then a new instance of Ruby is started as the subprocess.
+ * Optional argument +mode+ may be any valid \IO mode.
+ * See {Access Modes}[rdoc-ref:File@Access+Modes].
*
- * If <i>cmd</i> is an +Array+ of +String+,
- * then it will be used as the subprocess's +argv+ bypassing a shell.
- * The array can contains a hash at first for environments and
- * a hash at last for options similar to <code>spawn</code>.
+ * Required argument +cmd+ determines which of the following occurs:
*
- * The default mode for the new file object is ``r'',
- * but <i>mode</i> may be set to any of the modes listed in the description for class IO.
- * The last argument <i>opt</i> qualifies <i>mode</i>.
+ * - The process forks.
+ * - A specified program runs in a shell.
+ * - A specified program runs with specified arguments.
+ * - A specified program runs with specified arguments and a specified +argv0+.
*
- * # set IO encoding
- * IO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io|
- * euc_jp_string = nkf_io.read
- * }
+ * Each of these is detailed below.
*
- * # merge standard output and standard error using
- * # spawn option. See the document of Kernel.spawn.
- * IO.popen(["ls", "/", :err=>[:child, :out]]) {|ls_io|
- * ls_result_with_error = ls_io.read
- * }
+ * The optional hash argument +env+ specifies name/value pairs that are to be added
+ * to the environment variables for the subprocess:
*
- * # spawn options can be mixed with IO options
- * IO.popen(["ls", "/"], :err=>[:child, :out]) {|ls_io|
- * ls_result_with_error = ls_io.read
- * }
+ * IO.popen({'FOO' => 'bar'}, 'ruby', 'r+') do |pipe|
+ * pipe.puts 'puts ENV["FOO"]'
+ * pipe.close_write
+ * pipe.gets
+ * end => "bar\n"
+ *
+ * Optional keyword arguments +opts+ specify:
+ *
+ * - {Open options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
+ * - Options for Kernel#spawn.
+ *
+ * <b>Forked Process</b>
+ *
+ * When argument +cmd+ is the 1-character string <tt>'-'</tt>, causes the process to fork:
+ * IO.popen('-') do |pipe|
+ * if pipe
+ * $stderr.puts "In parent, child pid is #{pipe.pid}\n"
+ * else
+ * $stderr.puts "In child, pid is #{$$}\n"
+ * end
+ * end
+ *
+ * Output:
+ *
+ * In parent, child pid is 26253
+ * In child, pid is 26253
+ *
+ * Note that this is not supported on all platforms.
+ *
+ * <b>Shell Subprocess</b>
*
- * Raises exceptions which <code>IO.pipe</code> and
- * <code>Kernel.spawn</code> raise.
+ * When argument +cmd+ is a single string (but not <tt>'-'</tt>),
+ * the program named +cmd+ is run as a shell command:
*
- * If a block is given, Ruby will run the command as a child connected
- * to Ruby with a pipe. Ruby's end of the pipe will be passed as a
- * parameter to the block.
- * At the end of block, Ruby close the pipe and sets <code>$?</code>.
- * In this case <code>IO.popen</code> returns
- * the value of the block.
+ * IO.popen('uname') do |pipe|
+ * pipe.readlines
+ * end
+ *
+ * Output:
+ *
+ * ["Linux\n"]
+ *
+ * Another example:
+ *
+ * IO.popen('/bin/sh', 'r+') do |pipe|
+ * pipe.puts('ls')
+ * pipe.close_write
+ * $stderr.puts pipe.readlines.size
+ * end
+ *
+ * Output:
+ *
+ * 213
+ *
+ * <b>Program Subprocess</b>
+ *
+ * When argument +cmd+ is an array of strings,
+ * the program named <tt>cmd[0]</tt> is run with all elements of +cmd+ as its arguments:
+ *
+ * IO.popen(['du', '..', '.']) do |pipe|
+ * $stderr.puts pipe.readlines.size
+ * end
+ *
+ * Output:
+ *
+ * 1111
+ *
+ * <b>Program Subprocess with <tt>argv0</tt></b>
+ *
+ * When argument +cmd+ is an array whose first element is a 2-element string array
+ * and whose remaining elements (if any) are strings:
+ *
+ * - <tt>cmd[0][0]</tt> (the first string in the nested array) is the name of a program that is run.
+ * - <tt>cmd[0][1]</tt> (the second string in the nested array) is set as the program's <tt>argv[0]</tt>.
+ * - <tt>cmd[1..-1]</tt> (the strings in the outer array) are the program's arguments.
+ *
+ * Example (sets <tt>$0</tt> to 'foo'):
+ *
+ * IO.popen([['/bin/sh', 'foo'], '-c', 'echo $0']).read # => "foo\n"
+ *
+ * <b>Some Special Examples</b>
+ *
+ * # Set IO encoding.
+ * IO.popen("nkf -e filename", :external_encoding=>"EUC-JP") {|nkf_io|
+ * euc_jp_string = nkf_io.read
+ * }
*
- * If a block is given with a _cmd_ of ``<code>-</code>'',
- * the block will be run in two separate processes: once in the parent,
- * and once in a child. The parent process will be passed the pipe
- * object as a parameter to the block, the child version of the block
- * will be passed <code>nil</code>, and the child's standard in and
- * standard out will be connected to the parent through the pipe. Not
- * available on all platforms.
+ * # Merge standard output and standard error using Kernel#spawn option. See Kernel#spawn.
+ * IO.popen(["ls", "/", :err=>[:child, :out]]) do |io|
+ * ls_result_with_error = io.read
+ * end
+ *
+ * # Use mixture of spawn options and IO options.
+ * IO.popen(["ls", "/"], :err=>[:child, :out]) do |io|
+ * ls_result_with_error = io.read
+ * end
*
* f = IO.popen("uname")
* p f.readlines
* f.close
* puts "Parent is #{Process.pid}"
- * IO.popen("date") { |f| puts f.gets }
+ * IO.popen("date") {|f| puts f.gets }
* IO.popen("-") {|f| $stderr.puts "#{Process.pid} is here, f is #{f.inspect}"}
* p $?
* IO.popen(%w"sed -e s|^|<foo>| -e s&$&;zot;&", "r+") {|f|
* f.puts "bar"; f.close_write; puts f.gets
* }
*
- * <em>produces:</em>
+ * Output (from last section):
*
* ["Linux\n"]
* Parent is 21346
@@ -6111,157 +8015,207 @@ pipe_open_s(VALUE prog, const char *modestr, int fmode, convconfig_t *convconfig
* 21352 is here, f is nil
* #<Process::Status: pid 21352 exit 0>
* <foo>bar;zot;
+ *
+ * Raises exceptions that IO.pipe and Kernel.spawn raise.
+ *
*/
static VALUE
rb_io_s_popen(int argc, VALUE *argv, VALUE klass)
{
- const char *modestr;
- VALUE pname, pmode = Qnil, port, tmp, opt = Qnil, env = Qnil, execarg_obj = Qnil;
- int oflags, fmode;
- convconfig_t convconfig;
+ VALUE pname, pmode = Qnil, opt = Qnil, env = Qnil;
if (argc > 1 && !NIL_P(opt = rb_check_hash_type(argv[argc-1]))) --argc;
if (argc > 1 && !NIL_P(env = rb_check_hash_type(argv[0]))) --argc, ++argv;
switch (argc) {
case 2:
- pmode = argv[1];
+ pmode = argv[1];
case 1:
- pname = argv[0];
- break;
+ pname = argv[0];
+ break;
default:
- {
- int ex = !NIL_P(opt);
- rb_error_arity(argc + ex, 1 + ex, 2 + ex);
- }
+ {
+ int ex = !NIL_P(opt);
+ rb_error_arity(argc + ex, 1 + ex, 2 + ex);
+ }
}
+ return popen_finish(rb_io_popen(pname, pmode, env, opt), klass);
+}
+
+VALUE
+rb_io_popen(VALUE pname, VALUE pmode, VALUE env, VALUE opt)
+{
+ const char *modestr;
+ VALUE tmp, execarg_obj = Qnil;
+ int oflags;
+ enum rb_io_mode fmode;
+ struct rb_io_encoding convconfig;
tmp = rb_check_array_type(pname);
if (!NIL_P(tmp)) {
- long len = RARRAY_LEN(tmp);
+ long len = RARRAY_LEN(tmp);
#if SIZEOF_LONG > SIZEOF_INT
- if (len > INT_MAX) {
- rb_raise(rb_eArgError, "too many arguments");
- }
+ if (len > INT_MAX) {
+ rb_raise(rb_eArgError, "too many arguments");
+ }
#endif
- tmp = rb_ary_dup(tmp);
- RBASIC_CLEAR_CLASS(tmp);
- execarg_obj = rb_execarg_new((int)len, RARRAY_PTR(tmp), FALSE);
- rb_ary_clear(tmp);
+ execarg_obj = rb_execarg_new((int)len, RARRAY_CONST_PTR(tmp), FALSE, FALSE);
+ RB_GC_GUARD(tmp);
}
else {
- SafeStringValue(pname);
- execarg_obj = Qnil;
- if (!is_popen_fork(pname))
- execarg_obj = rb_execarg_new(1, &pname, TRUE);
+ StringValue(pname);
+ execarg_obj = Qnil;
+ if (!is_popen_fork(pname))
+ execarg_obj = rb_execarg_new(1, &pname, TRUE, FALSE);
}
if (!NIL_P(execarg_obj)) {
- if (!NIL_P(opt))
- opt = rb_execarg_extract_options(execarg_obj, opt);
- if (!NIL_P(env))
- rb_execarg_setenv(execarg_obj, env);
+ if (!NIL_P(opt))
+ opt = rb_execarg_extract_options(execarg_obj, opt);
+ if (!NIL_P(env))
+ rb_execarg_setenv(execarg_obj, env);
}
rb_io_extract_modeenc(&pmode, 0, opt, &oflags, &fmode, &convconfig);
modestr = rb_io_oflags_modestr(oflags);
- port = pipe_open(execarg_obj, modestr, fmode, &convconfig);
+ return pipe_open(execarg_obj, modestr, fmode, &convconfig);
+}
+
+static VALUE
+popen_finish(VALUE port, VALUE klass)
+{
if (NIL_P(port)) {
- /* child */
- if (rb_block_given_p()) {
- rb_yield(Qnil);
- rb_io_flush(rb_stdout);
- rb_io_flush(rb_stderr);
- _exit(0);
- }
- return Qnil;
+ /* child */
+ if (rb_block_given_p()) {
+ rb_protect(rb_yield, Qnil, NULL);
+ rb_io_flush(rb_ractor_stdout());
+ rb_io_flush(rb_ractor_stderr());
+ _exit(EXIT_SUCCESS);
+ }
+ return Qnil;
}
RBASIC_SET_CLASS(port, klass);
if (rb_block_given_p()) {
- return rb_ensure(rb_yield, port, io_close, port);
+ return rb_ensure(rb_yield, port, pipe_close, port);
}
return port;
}
-static void
-rb_scan_open_args(int argc, const VALUE *argv,
- VALUE *fname_p, int *oflags_p, int *fmode_p,
- convconfig_t *convconfig_p, mode_t *perm_p)
-{
- VALUE opt, fname, vmode, vperm;
- int oflags, fmode;
- mode_t perm;
+#if defined(HAVE_WORKING_FORK) && !defined(__EMSCRIPTEN__)
+struct popen_writer_arg {
+ char *const *argv;
+ struct popen_arg popen;
+};
- argc = rb_scan_args(argc, argv, "12:", &fname, &vmode, &vperm, &opt);
- FilePathValue(fname);
+static int
+exec_popen_writer(void *arg, char *errmsg, size_t buflen)
+{
+ struct popen_writer_arg *pw = arg;
+ pw->popen.modef = FMODE_WRITABLE;
+ popen_redirect(&pw->popen);
+ execv(pw->argv[0], pw->argv);
+ strlcpy(errmsg, strerror(errno), buflen);
+ return -1;
+}
+#endif
- rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, convconfig_p);
+FILE *
+ruby_popen_writer(char *const *argv, rb_pid_t *pid)
+{
+#if (defined(HAVE_WORKING_FORK) && !defined(__EMSCRIPTEN__)) || defined(_WIN32)
+# ifdef HAVE_WORKING_FORK
+ struct popen_writer_arg pw;
+ int *const write_pair = pw.popen.pair;
+# else
+ int write_pair[2];
+# endif
- perm = NIL_P(vperm) ? 0666 : NUM2MODET(vperm);
+#ifdef HAVE_PIPE2
+ int result = pipe2(write_pair, O_CLOEXEC);
+#else
+ int result = pipe(write_pair);
+#endif
- *fname_p = fname;
- *oflags_p = oflags;
- *fmode_p = fmode;
- *perm_p = perm;
+ *pid = -1;
+ if (result == 0) {
+# ifdef HAVE_WORKING_FORK
+ pw.argv = argv;
+ int status;
+ char errmsg[80] = {'\0'};
+ *pid = rb_fork_async_signal_safe(&status, exec_popen_writer, &pw, Qnil, errmsg, sizeof(errmsg));
+# else
+ *pid = rb_w32_uspawn_process(P_NOWAIT, argv[0], argv, write_pair[0], -1, -1, 0);
+ const char *errmsg = (*pid < 0) ? strerror(errno) : NULL;
+# endif
+ close(write_pair[0]);
+ if (*pid < 0) {
+ close(write_pair[1]);
+ fprintf(stderr, "ruby_popen_writer(%s): %s\n", argv[0], errmsg);
+ }
+ else {
+ return fdopen(write_pair[1], "w");
+ }
+ }
+#endif
+ return NULL;
}
static VALUE
-rb_open_file(int argc, const VALUE *argv, VALUE io)
+rb_open_file(VALUE io, VALUE fname, VALUE vmode, VALUE vperm, VALUE opt)
{
- VALUE fname;
- int oflags, fmode;
- convconfig_t convconfig;
+ int oflags;
+ enum rb_io_mode fmode;
+ struct rb_io_encoding convconfig;
mode_t perm;
- rb_scan_open_args(argc, argv, &fname, &oflags, &fmode, &convconfig, &perm);
+ FilePathValue(fname);
+
+ rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, &convconfig);
+ perm = NIL_P(vperm) ? 0666 : NUM2MODET(vperm);
+
rb_file_open_generic(io, fname, oflags, fmode, &convconfig, perm);
return io;
}
-
/*
* Document-method: File::open
*
* call-seq:
- * File.open(filename, mode="r" [, opt]) -> file
- * File.open(filename [, mode [, perm]] [, opt]) -> file
- * File.open(filename, mode="r" [, opt]) {|file| block } -> obj
- * File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj
+ * File.open(path, mode = 'r', perm = 0666, **opts) -> file
+ * File.open(path, mode = 'r', perm = 0666, **opts) {|f| ... } -> object
*
- * With no associated block, <code>File.open</code> is a synonym for
- * File.new. If the optional code block is given, it will
- * be passed the opened +file+ as an argument and the File object will
- * automatically be closed when the block terminates. The value of the block
- * will be returned from <code>File.open</code>.
+ * Creates a new File object, via File.new with the given arguments.
*
- * If a file is being created, its initial permissions may be set using the
- * +perm+ parameter. See File.new for further discussion.
+ * With no block given, returns the File object.
+ *
+ * With a block given, calls the block with the File object
+ * and returns the block's value.
*
- * See IO.new for a description of the +mode+ and +opt+ parameters.
*/
/*
* Document-method: IO::open
*
* call-seq:
- * IO.open(fd, mode="r" [, opt]) -> io
- * IO.open(fd, mode="r" [, opt]) { |io| block } -> obj
+ * IO.open(fd, mode = 'r', **opts) -> io
+ * IO.open(fd, mode = 'r', **opts) {|io| ... } -> object
+ *
+ * Creates a new \IO object, via IO.new with the given arguments.
+ *
+ * With no block given, returns the \IO object.
*
- * With no associated block, <code>IO.open</code> is a synonym for IO.new. If
- * the optional code block is given, it will be passed +io+ as an argument,
- * and the IO object will automatically be closed when the block terminates.
- * In this instance, IO.open returns the value of the block.
+ * With a block given, calls the block with the \IO object
+ * and returns the block's value.
*
- * See IO.new for a description of the +fd+, +mode+ and +opt+ parameters.
*/
static VALUE
rb_io_s_open(int argc, VALUE *argv, VALUE klass)
{
- VALUE io = rb_class_new_instance(argc, argv, klass);
+ VALUE io = rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
if (rb_block_given_p()) {
- return rb_ensure(rb_yield, io, io_close, io);
+ return rb_ensure(rb_yield, io, io_close, io);
}
return io;
@@ -6269,16 +8223,24 @@ rb_io_s_open(int argc, VALUE *argv, VALUE klass)
/*
* call-seq:
- * IO.sysopen(path, [mode, [perm]]) -> fixnum
+ * IO.sysopen(path, mode = 'r', perm = 0666) -> integer
+ *
+ * Opens the file at the given path with the given mode and permissions;
+ * returns the integer file descriptor.
+ *
+ * If the file is to be readable, it must exist;
+ * if the file is to be writable and does not exist,
+ * it is created with the given permissions:
+ *
+ * File.write('t.tmp', '') # => 0
+ * IO.sysopen('t.tmp') # => 8
+ * IO.sysopen('t.tmp', 'w') # => 9
*
- * Opens the given path, returning the underlying file descriptor as a
- * <code>Fixnum</code>.
*
- * IO.sysopen("testfile") #=> 3
*/
static VALUE
-rb_io_s_sysopen(int argc, VALUE *argv)
+rb_io_s_sysopen(int argc, VALUE *argv, VALUE _)
{
VALUE fname, vmode, vperm;
VALUE intmode;
@@ -6293,8 +8255,8 @@ rb_io_s_sysopen(int argc, VALUE *argv)
else if (!NIL_P(intmode = rb_check_to_integer(vmode, "to_int")))
oflags = NUM2INT(intmode);
else {
- SafeStringValue(vmode);
- oflags = rb_io_modestr_oflags(StringValueCStr(vmode));
+ StringValue(vmode);
+ oflags = rb_io_modestr_oflags(StringValueCStr(vmode));
}
if (NIL_P(vperm)) perm = 0666;
else perm = NUM2MODET(vperm);
@@ -6304,185 +8266,82 @@ rb_io_s_sysopen(int argc, VALUE *argv)
return INT2NUM(fd);
}
-static VALUE
-check_pipe_command(VALUE filename_or_command)
-{
- char *s = RSTRING_PTR(filename_or_command);
- long l = RSTRING_LEN(filename_or_command);
- char *e = s + l;
- int chlen;
-
- if (rb_enc_ascget(s, e, &chlen, rb_enc_get(filename_or_command)) == '|') {
- VALUE cmd = rb_str_new(s+chlen, l-chlen);
- OBJ_INFECT(cmd, filename_or_command);
- return cmd;
- }
- return Qnil;
-}
-
/*
* call-seq:
- * open(path [, mode [, perm]] [, opt]) -> io or nil
- * open(path [, mode [, perm]] [, opt]) {|io| block } -> obj
- *
- * Creates an IO object connected to the given stream, file, or subprocess.
- *
- * If +path+ does not start with a pipe character (<code>|</code>), treat it
- * as the name of a file to open using the specified mode (defaulting to
- * "r").
+ * open(path, mode = 'r', perm = 0666, **opts) -> io or nil
+ * open(path, mode = 'r', perm = 0666, **opts) {|io| ... } -> obj
*
- * The +mode+ is either a string or an integer. If it is an integer, it
- * must be bitwise-or of open(2) flags, such as File::RDWR or File::EXCL. If
- * it is a string, it is either "fmode", "fmode:ext_enc", or
- * "fmode:ext_enc:int_enc".
+ * Creates an IO object connected to the given file.
*
- * See the documentation of IO.new for full documentation of the +mode+ string
- * directives.
+ * With no block given, file stream is returned:
*
- * If a file is being created, its initial permissions may be set using the
- * +perm+ parameter. See File.new and the open(2) and chmod(2) man pages for
- * a description of permissions.
+ * open('t.txt') # => #<File:t.txt>
*
- * If a block is specified, it will be invoked with the IO object as a
- * parameter, and the IO will be automatically closed when the block
- * terminates. The call returns the value of the block.
+ * With a block given, calls the block with the open file stream,
+ * then closes the stream:
*
- * If +path+ starts with a pipe character (<code>"|"</code>), a subprocess is
- * created, connected to the caller by a pair of pipes. The returned IO
- * object may be used to write to the standard input and read from the
- * standard output of this subprocess.
+ * open('t.txt') {|f| p f } # => #<File:t.txt (closed)>
*
- * If the command following the pipe is a single minus sign
- * (<code>"|-"</code>), Ruby forks, and this subprocess is connected to the
- * parent. If the command is not <code>"-"</code>, the subprocess runs the
- * command.
+ * Output:
*
- * When the subprocess is ruby (opened via <code>"|-"</code>), the +open+
- * call returns +nil+. If a block is associated with the open call, that
- * block will run twice --- once in the parent and once in the child.
+ * #<File:t.txt>
*
- * The block parameter will be an IO object in the parent and +nil+ in the
- * child. The parent's +IO+ object will be connected to the child's $stdin
- * and $stdout. The subprocess will be terminated at the end of the block.
+ * See File.open for details.
*
- * === Examples
- *
- * Reading from "testfile":
- *
- * open("testfile") do |f|
- * print f.gets
- * end
- *
- * Produces:
- *
- * This is line one
- *
- * Open a subprocess and read its output:
- *
- * cmd = open("|date")
- * print cmd.gets
- * cmd.close
- *
- * Produces:
- *
- * Wed Apr 9 08:56:31 CDT 2003
- *
- * Open a subprocess running the same Ruby program:
- *
- * f = open("|-", "w+")
- * if f == nil
- * puts "in Child"
- * exit
- * else
- * puts "Got: #{f.gets}"
- * end
- *
- * Produces:
- *
- * Got: in Child
- *
- * Open a subprocess using a block to receive the IO object:
- *
- * open "|-" do |f|
- * if f then
- * # parent process
- * puts "Got: #{f.gets}"
- * else
- * # child process
- * puts "in Child"
- * end
- * end
- *
- * Produces:
- *
- * Got: in Child
*/
static VALUE
-rb_f_open(int argc, VALUE *argv)
+rb_f_open(int argc, VALUE *argv, VALUE _)
{
ID to_open = 0;
int redirect = FALSE;
if (argc >= 1) {
- CONST_ID(to_open, "to_open");
- if (rb_respond_to(argv[0], to_open)) {
- redirect = TRUE;
- }
- else {
- VALUE tmp = argv[0];
- FilePathValue(tmp);
- if (NIL_P(tmp)) {
- redirect = TRUE;
- }
- else {
- VALUE cmd = check_pipe_command(tmp);
- if (!NIL_P(cmd)) {
- argv[0] = cmd;
- return rb_io_s_popen(argc, argv, rb_cIO);
- }
- }
- }
+ CONST_ID(to_open, "to_open");
+ if (rb_respond_to(argv[0], to_open)) {
+ redirect = TRUE;
+ }
+ else {
+ VALUE tmp = argv[0];
+ FilePathValue(tmp);
+ if (NIL_P(tmp)) {
+ redirect = TRUE;
+ }
+ else {
+ argv[0] = tmp;
+ }
+ }
}
if (redirect) {
- VALUE io = rb_funcall2(argv[0], to_open, argc-1, argv+1);
+ VALUE io = rb_funcallv_kw(argv[0], to_open, argc-1, argv+1, RB_PASS_CALLED_KEYWORDS);
- if (rb_block_given_p()) {
- return rb_ensure(rb_yield, io, io_close, io);
- }
- return io;
+ if (rb_block_given_p()) {
+ return rb_ensure(rb_yield, io, io_close, io);
+ }
+ return io;
}
return rb_io_s_open(argc, argv, rb_cFile);
}
static VALUE
-rb_io_open(VALUE filename, VALUE vmode, VALUE vperm, VALUE opt)
+rb_io_open_generic(VALUE klass, VALUE filename, int oflags, enum rb_io_mode fmode,
+ const struct rb_io_encoding *convconfig, mode_t perm)
{
- VALUE cmd;
- int oflags, fmode;
- convconfig_t convconfig;
- mode_t perm;
-
- rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, &convconfig);
- perm = NIL_P(vperm) ? 0666 : NUM2MODET(vperm);
-
- if (!NIL_P(cmd = check_pipe_command(filename))) {
- return pipe_open_s(cmd, rb_io_oflags_modestr(oflags), fmode, &convconfig);
- }
- else {
- return rb_file_open_generic(io_alloc(rb_cFile), filename,
- oflags, fmode, &convconfig, perm);
- }
+ return rb_file_open_generic(io_alloc(klass), filename,
+ oflags, fmode, convconfig, perm);
}
static VALUE
-rb_io_open_with_args(int argc, const VALUE *argv)
+rb_io_open(VALUE io, VALUE filename, VALUE vmode, VALUE vperm, VALUE opt)
{
- VALUE io;
+ int oflags;
+ enum rb_io_mode fmode;
+ struct rb_io_encoding convconfig;
+ mode_t perm;
- io = io_alloc(rb_cFile);
- rb_open_file(argc, argv, io);
- return io;
+ rb_io_extract_modeenc(&vmode, &vperm, opt, &oflags, &fmode, &convconfig);
+ perm = NIL_P(vperm) ? 0666 : NUM2MODET(vperm);
+ return rb_io_open_generic(io, filename, oflags, fmode, &convconfig, perm);
}
static VALUE
@@ -6490,60 +8349,61 @@ io_reopen(VALUE io, VALUE nfile)
{
rb_io_t *fptr, *orig;
int fd, fd2;
- off_t pos = 0;
+ rb_off_t pos = 0;
nfile = rb_io_get_io(nfile);
GetOpenFile(io, fptr);
GetOpenFile(nfile, orig);
if (fptr == orig) return io;
- if (IS_PREP_STDIO(fptr)) {
+ if (RUBY_IO_EXTERNAL_P(fptr)) {
if ((fptr->stdio_file == stdin && !(orig->mode & FMODE_READABLE)) ||
(fptr->stdio_file == stdout && !(orig->mode & FMODE_WRITABLE)) ||
(fptr->stdio_file == stderr && !(orig->mode & FMODE_WRITABLE))) {
- rb_raise(rb_eArgError,
- "%s can't change access mode from \"%s\" to \"%s\"",
- PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
- rb_io_fmode_modestr(orig->mode));
- }
+ rb_raise(rb_eArgError,
+ "%s can't change access mode from \"%s\" to \"%s\"",
+ PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
+ rb_io_fmode_modestr(orig->mode));
+ }
}
if (fptr->mode & FMODE_WRITABLE) {
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
+ rb_sys_fail_on_write(fptr);
}
else {
- io_tell(fptr);
+ flush_before_seek(fptr, true);
}
if (orig->mode & FMODE_READABLE) {
- pos = io_tell(orig);
+ pos = io_tell(orig);
}
if (orig->mode & FMODE_WRITABLE) {
if (io_fflush(orig) < 0)
- rb_sys_fail(0);
+ rb_sys_fail_on_write(fptr);
}
/* copy rb_io_t structure */
- fptr->mode = orig->mode | (fptr->mode & FMODE_PREP);
+ fptr->mode = orig->mode | (fptr->mode & FMODE_EXTERNAL);
+ fptr->encs = orig->encs;
fptr->pid = orig->pid;
fptr->lineno = orig->lineno;
if (RTEST(orig->pathv)) fptr->pathv = orig->pathv;
- else if (!IS_PREP_STDIO(fptr)) fptr->pathv = Qnil;
- fptr->finalize = orig->finalize;
-#if defined (__CYGWIN__) || !defined(HAVE_FORK)
- if (fptr->finalize == pipe_finalize)
- pipe_add_fptr(fptr);
-#endif
+ else if (!RUBY_IO_EXTERNAL_P(fptr)) fptr->pathv = Qnil;
+ fptr_copy_finalizer(fptr, orig);
fd = fptr->fd;
fd2 = orig->fd;
if (fd != fd2) {
- if (IS_PREP_STDIO(fptr) || fd <= 2 || !fptr->stdio_file) {
- /* need to keep FILE objects of stdin, stdout and stderr */
- if (rb_cloexec_dup2(fd2, fd) < 0)
- rb_sys_fail_path(orig->pathv);
+ // Interrupt all usage of the old file descriptor:
+ rb_thread_io_close_interrupt(fptr);
+ rb_thread_io_close_wait(fptr);
+
+ if (RUBY_IO_EXTERNAL_P(fptr) || fd <= 2 || !fptr->stdio_file) {
+ /* need to keep FILE objects of stdin, stdout and stderr */
+ if (rb_cloexec_dup2(fd2, fd) < 0)
+ rb_sys_fail_path(orig->pathv);
rb_update_max_fd(fd);
- }
- else {
+ }
+ else {
fclose(fptr->stdio_file);
fptr->stdio_file = 0;
fptr->fd = -1;
@@ -6551,40 +8411,72 @@ io_reopen(VALUE io, VALUE nfile)
rb_sys_fail_path(orig->pathv);
rb_update_max_fd(fd);
fptr->fd = fd;
- }
- rb_thread_fd_close(fd);
- if ((orig->mode & FMODE_READABLE) && pos >= 0) {
- if (io_seek(fptr, pos, SEEK_SET) < 0 && errno) {
- rb_sys_fail_path(fptr->pathv);
- }
- if (io_seek(orig, pos, SEEK_SET) < 0 && errno) {
- rb_sys_fail_path(orig->pathv);
- }
- }
+ }
+
+ if ((orig->mode & FMODE_READABLE) && pos >= 0) {
+ if (io_seek(fptr, pos, SEEK_SET) < 0 && errno) {
+ rb_sys_fail_path(fptr->pathv);
+ }
+ if (io_seek(orig, pos, SEEK_SET) < 0 && errno) {
+ rb_sys_fail_path(orig->pathv);
+ }
+ }
}
if (fptr->mode & FMODE_BINMODE) {
- rb_io_binmode(io);
+ rb_io_binmode(io);
}
RBASIC_SET_CLASS(io, rb_obj_class(nfile));
return io;
}
+#ifdef _WIN32
+int rb_freopen(VALUE fname, const char *mode, FILE *fp);
+#else
+static int
+rb_freopen(VALUE fname, const char *mode, FILE *fp)
+{
+ if (!freopen(RSTRING_PTR(fname), mode, fp)) {
+ RB_GC_GUARD(fname);
+ return errno;
+ }
+ return 0;
+}
+#endif
+
/*
* call-seq:
- * ios.reopen(other_IO) -> ios
- * ios.reopen(path, mode_str) -> ios
+ * reopen(other_io) -> self
+ * reopen(path, mode = 'r', **opts) -> self
+ *
+ * Reassociates the stream with another stream,
+ * which may be of a different class.
+ * This method may be used to redirect an existing stream
+ * to a new destination.
+ *
+ * With argument +other_io+ given, reassociates with that stream:
+ *
+ * # Redirect $stdin from a file.
+ * f = File.open('t.txt')
+ * $stdin.reopen(f)
+ * f.close
+ *
+ * # Redirect $stdout to a file.
+ * f = File.open('t.tmp', 'w')
+ * $stdout.reopen(f)
+ * f.close
+ *
+ * With argument +path+ given, reassociates with a new stream to that file path:
+ *
+ * $stdin.reopen('t.txt')
+ * $stdout.reopen('t.tmp', 'w')
+ *
+ * Optional keyword arguments +opts+ specify:
*
- * Reassociates <em>ios</em> with the I/O stream given in
- * <i>other_IO</i> or to a new stream opened on <i>path</i>. This may
- * dynamically change the actual class of this stream.
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
*
- * f1 = File.new("testfile")
- * f2 = File.new("testfile")
- * f2.readlines[0] #=> "This is line one\n"
- * f2.reopen(f1) #=> #<File:testfile>
- * f2.readlines[0] #=> "This is line one\n"
*/
static VALUE
@@ -6595,57 +8487,57 @@ rb_io_reopen(int argc, VALUE *argv, VALUE file)
rb_io_t *fptr;
if (rb_scan_args(argc, argv, "11:", &fname, &nmode, &opt) == 1) {
- VALUE tmp = rb_io_check_io(fname);
- if (!NIL_P(tmp)) {
- return io_reopen(file, tmp);
- }
+ VALUE tmp = rb_io_check_io(fname);
+ if (!NIL_P(tmp)) {
+ return io_reopen(file, tmp);
+ }
}
FilePathValue(fname);
rb_io_taint_check(file);
fptr = RFILE(file)->fptr;
if (!fptr) {
- fptr = RFILE(file)->fptr = ALLOC(rb_io_t);
- MEMZERO(fptr, rb_io_t, 1);
+ fptr = RFILE(file)->fptr = ZALLOC(rb_io_t);
}
if (!NIL_P(nmode) || !NIL_P(opt)) {
- int fmode;
- convconfig_t convconfig;
+ enum rb_io_mode fmode;
+ struct rb_io_encoding convconfig;
- rb_io_extract_modeenc(&nmode, 0, opt, &oflags, &fmode, &convconfig);
- if (IS_PREP_STDIO(fptr) &&
+ rb_io_extract_modeenc(&nmode, 0, opt, &oflags, &fmode, &convconfig);
+ if (RUBY_IO_EXTERNAL_P(fptr) &&
((fptr->mode & FMODE_READWRITE) & (fmode & FMODE_READWRITE)) !=
(fptr->mode & FMODE_READWRITE)) {
- rb_raise(rb_eArgError,
- "%s can't change access mode from \"%s\" to \"%s\"",
- PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
- rb_io_fmode_modestr(fmode));
- }
- fptr->mode = fmode;
- fptr->encs = convconfig;
+ rb_raise(rb_eArgError,
+ "%s can't change access mode from \"%s\" to \"%s\"",
+ PREP_STDIO_NAME(fptr), rb_io_fmode_modestr(fptr->mode),
+ rb_io_fmode_modestr(fmode));
+ }
+ fptr->mode = fmode;
+ fptr->encs = convconfig;
}
else {
- oflags = rb_io_fmode_oflags(fptr->mode);
+ oflags = rb_io_fmode_oflags(fptr->mode);
}
- fptr->pathv = rb_str_new_frozen(fname);
+ fptr->pathv = fname;
if (fptr->fd < 0) {
fptr->fd = rb_sysopen(fptr->pathv, oflags, 0666);
- fptr->stdio_file = 0;
- return file;
+ fptr->stdio_file = 0;
+ return file;
}
if (fptr->mode & FMODE_WRITABLE) {
if (io_fflush(fptr) < 0)
- rb_sys_fail(0);
+ rb_sys_fail_on_write(fptr);
}
fptr->rbuf.off = fptr->rbuf.len = 0;
if (fptr->stdio_file) {
- if (freopen(RSTRING_PTR(fptr->pathv), rb_io_oflags_modestr(oflags), fptr->stdio_file) == 0) {
- rb_sys_fail_path(fptr->pathv);
- }
+ int e = rb_freopen(rb_str_encode_ospath(fptr->pathv),
+ rb_io_oflags_modestr(oflags),
+ fptr->stdio_file);
+ if (e) rb_syserr_fail_path(e, fptr->pathv);
fptr->fd = fileno(fptr->stdio_file);
rb_fd_fix_cloexec(fptr->fd);
#ifdef USE_SETVBUF
@@ -6662,14 +8554,14 @@ rb_io_reopen(int argc, VALUE *argv, VALUE file)
}
}
else {
- int tmpfd = rb_sysopen(fptr->pathv, oflags, 0666);
- int err = 0;
- if (rb_cloexec_dup2(tmpfd, fptr->fd) < 0)
- err = errno;
- (void)close(tmpfd);
- if (err) {
- rb_syserr_fail_path(err, fptr->pathv);
- }
+ int tmpfd = rb_sysopen(fptr->pathv, oflags, 0666);
+ int err = 0;
+ if (rb_cloexec_dup2(tmpfd, fptr->fd) < 0)
+ err = errno;
+ (void)close(tmpfd);
+ if (err) {
+ rb_syserr_fail_path(err, fptr->pathv);
+ }
}
return file;
@@ -6682,7 +8574,7 @@ rb_io_init_copy(VALUE dest, VALUE io)
rb_io_t *fptr, *orig;
int fd;
VALUE write_io;
- off_t pos;
+ rb_off_t pos;
io = rb_io_get_io(io);
if (!OBJ_INIT_COPY(dest, io)) return dest;
@@ -6692,16 +8584,19 @@ rb_io_init_copy(VALUE dest, VALUE io)
rb_io_flush(io);
/* copy rb_io_t structure */
- fptr->mode = orig->mode & ~FMODE_PREP;
+ fptr->mode = orig->mode & ~FMODE_EXTERNAL;
fptr->encs = orig->encs;
fptr->pid = orig->pid;
fptr->lineno = orig->lineno;
+ fptr->timeout = orig->timeout;
+
+ ccan_list_head_init(&fptr->blocking_operations);
+ fptr->closing_ec = NULL;
+ fptr->wakeup_mutex = Qnil;
+ fptr->fork_generation = GET_VM()->fork_gen;
+
if (!NIL_P(orig->pathv)) fptr->pathv = orig->pathv;
- fptr->finalize = orig->finalize;
-#if defined (__CYGWIN__) || !defined(HAVE_FORK)
- if (fptr->finalize == pipe_finalize)
- pipe_add_fptr(fptr);
-#endif
+ fptr_copy_finalizer(fptr, orig);
fd = ruby_dup(orig->fd);
fptr->fd = fd;
@@ -6709,7 +8604,7 @@ rb_io_init_copy(VALUE dest, VALUE io)
if (0 <= pos)
io_seek(fptr, pos, SEEK_SET);
if (fptr->mode & FMODE_BINMODE) {
- rb_io_binmode(dest);
+ rb_io_binmode(dest);
}
write_io = GetWriteIO(io);
@@ -6724,15 +8619,17 @@ rb_io_init_copy(VALUE dest, VALUE io)
/*
* call-seq:
- * ios.printf(format_string [, obj, ...]) -> nil
+ * printf(format_string, *objects) -> nil
+ *
+ * Formats and writes +objects+ to the stream.
+ *
+ * For details on +format_string+, see
+ * {Format Specifications}[rdoc-ref:language/format_specifications.rdoc].
*
- * Formats and writes to <em>ios</em>, converting parameters under
- * control of the format string. See <code>Kernel#sprintf</code>
- * for details.
*/
VALUE
-rb_io_printf(int argc, VALUE *argv, VALUE out)
+rb_io_printf(int argc, const VALUE *argv, VALUE out)
{
rb_io_write(out, rb_f_sprintf(argc, argv));
return Qnil;
@@ -6740,76 +8637,153 @@ rb_io_printf(int argc, VALUE *argv, VALUE out)
/*
* call-seq:
- * printf(io, string [, obj ... ]) -> nil
- * printf(string [, obj ... ]) -> nil
+ * printf(format_string, *objects) -> nil
+ * printf(io, format_string, *objects) -> nil
*
* Equivalent to:
- * io.write(sprintf(string, obj, ...))
- * or
- * $stdout.write(sprintf(string, obj, ...))
+ *
+ * io.write(sprintf(format_string, *objects))
+ *
+ * For details on +format_string+, see
+ * {Format Specifications}[rdoc-ref:language/format_specifications.rdoc].
+ *
+ * With the single argument +format_string+, formats +objects+ into the string,
+ * then writes the formatted string to $stdout:
+ *
+ * printf('%4.4d %10s %2.2f', 24, 24, 24.0)
+ *
+ * Output (on $stdout):
+ *
+ * 0024 24 24.00#
+ *
+ * With arguments +io+ and +format_string+, formats +objects+ into the string,
+ * then writes the formatted string to +io+:
+ *
+ * printf($stderr, '%4.4d %10s %2.2f', 24, 24, 24.0)
+ *
+ * Output (on $stderr):
+ *
+ * 0024 24 24.00# => nil
+ *
+ * With no arguments, does nothing.
+ *
*/
static VALUE
-rb_f_printf(int argc, VALUE *argv)
+rb_f_printf(int argc, VALUE *argv, VALUE _)
{
VALUE out;
if (argc == 0) return Qnil;
if (RB_TYPE_P(argv[0], T_STRING)) {
- out = rb_stdout;
+ out = rb_ractor_stdout();
}
else {
- out = argv[0];
- argv++;
- argc--;
+ out = argv[0];
+ argv++;
+ argc--;
}
rb_io_write(out, rb_f_sprintf(argc, argv));
return Qnil;
}
+extern void rb_deprecated_str_setter(VALUE val, ID id, VALUE *var);
+
+static void
+deprecated_rs_setter(VALUE val, ID id, VALUE *var)
+{
+ rb_deprecated_str_setter(val, id, &val);
+ if (!NIL_P(val)) {
+ if (rb_str_equal(val, rb_default_rs)) {
+ val = rb_default_rs;
+ }
+ else {
+ val = rb_str_frozen_bare_string(val);
+ }
+ }
+ *var = val;
+}
+
/*
* call-seq:
- * ios.print() -> nil
- * ios.print(obj, ...) -> nil
+ * print(*objects) -> nil
*
- * Writes the given object(s) to <em>ios</em>. The stream must be
- * opened for writing. If the output field separator (<code>$,</code>)
- * is not <code>nil</code>, it will be inserted between each object.
- * If the output record separator (<code>$\\</code>)
- * is not <code>nil</code>, it will be appended to the output. If no
- * arguments are given, prints <code>$_</code>. Objects that aren't
- * strings will be converted by calling their <code>to_s</code> method.
- * With no argument, prints the contents of the variable <code>$_</code>.
- * Returns <code>nil</code>.
+ * Writes the given objects to the stream; returns +nil+.
+ * Appends the output record separator <tt>$OUTPUT_RECORD_SEPARATOR</tt>
+ * (<tt>$\\</tt>), if it is not +nil+.
+ * See {Line IO}[rdoc-ref:IO@Line+IO].
*
- * $stdout.print("This is ", 100, " percent.\n")
+ * With argument +objects+ given, for each object:
*
- * <em>produces:</em>
+ * - Converts via its method +to_s+ if not a string.
+ * - Writes to the stream.
+ * - If not the last object, writes the output field separator
+ * <tt>$OUTPUT_FIELD_SEPARATOR</tt> (<tt>$,</tt>) if it is not +nil+.
+ *
+ * With default separators:
+ *
+ * f = File.open('t.tmp', 'w+')
+ * objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero']
+ * p $OUTPUT_RECORD_SEPARATOR
+ * p $OUTPUT_FIELD_SEPARATOR
+ * f.print(*objects)
+ * f.rewind
+ * p f.read
+ * f.close
+ *
+ * Output:
+ *
+ * nil
+ * nil
+ * "00.00/10+0izerozero"
+ *
+ * With specified separators:
+ *
+ * $\ = "\n"
+ * $, = ','
+ * f.rewind
+ * f.print(*objects)
+ * f.rewind
+ * p f.read
+ *
+ * Output:
+ *
+ * "0,0.0,0/1,0+0i,zero,zero\n"
+ *
+ * With no argument given, writes the content of <tt>$_</tt>
+ * (which is usually the most recent user input):
+ *
+ * f = File.open('t.tmp', 'w+')
+ * gets # Sets $_ to the most recent user input.
+ * f.print
+ * f.close
*
- * This is 100 percent.
*/
VALUE
-rb_io_print(int argc, VALUE *argv, VALUE out)
+rb_io_print(int argc, const VALUE *argv, VALUE out)
{
int i;
VALUE line;
/* if no argument given, print `$_' */
if (argc == 0) {
- argc = 1;
- line = rb_lastline_get();
- argv = &line;
+ argc = 1;
+ line = rb_lastline_get();
+ argv = &line;
+ }
+ if (argc > 1 && !NIL_P(rb_output_fs)) {
+ rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "$, is set to non-nil value");
}
for (i=0; i<argc; i++) {
- if (!NIL_P(rb_output_fs) && i>0) {
- rb_io_write(out, rb_output_fs);
- }
- rb_io_write(out, argv[i]);
+ if (!NIL_P(rb_output_fs) && i>0) {
+ rb_io_write(out, rb_output_fs);
+ }
+ rb_io_write(out, argv[i]);
}
if (argc > 0 && !NIL_P(rb_output_rs)) {
- rb_io_write(out, rb_output_rs);
+ rb_io_write(out, rb_output_rs);
}
return Qnil;
@@ -6817,50 +8791,79 @@ rb_io_print(int argc, VALUE *argv, VALUE out)
/*
* call-seq:
- * print(obj, ...) -> nil
+ * print(*objects) -> nil
*
- * Prints each object in turn to <code>$stdout</code>. If the output
- * field separator (<code>$,</code>) is not +nil+, its
- * contents will appear between each field. If the output record
- * separator (<code>$\\</code>) is not +nil+, it will be
- * appended to the output. If no arguments are given, prints
- * <code>$_</code>. Objects that aren't strings will be converted by
- * calling their <code>to_s</code> method.
+ * Equivalent to <tt>$stdout.print(*objects)</tt>,
+ * this method is the straightforward way to write to <tt>$stdout</tt>.
*
- * print "cat", [1,2,3], 99, "\n"
- * $, = ", "
- * $\ = "\n"
- * print "cat", [1,2,3], 99
+ * Writes the given objects to <tt>$stdout</tt>; returns +nil+.
+ * Appends the output record separator <tt>$OUTPUT_RECORD_SEPARATOR</tt>
+ * <tt>$\\</tt>), if it is not +nil+.
*
- * <em>produces:</em>
+ * With argument +objects+ given, for each object:
+ *
+ * - Converts via its method +to_s+ if not a string.
+ * - Writes to <tt>stdout</tt>.
+ * - If not the last object, writes the output field separator
+ * <tt>$OUTPUT_FIELD_SEPARATOR</tt> (<tt>$,</tt> if it is not +nil+.
+ *
+ * With default separators:
+ *
+ * objects = [0, 0.0, Rational(0, 1), Complex(0, 0), :zero, 'zero']
+ * $OUTPUT_RECORD_SEPARATOR
+ * $OUTPUT_FIELD_SEPARATOR
+ * print(*objects)
+ *
+ * Output:
+ *
+ * nil
+ * nil
+ * 00.00/10+0izerozero
+ *
+ * With specified separators:
+ *
+ * $OUTPUT_RECORD_SEPARATOR = "\n"
+ * $OUTPUT_FIELD_SEPARATOR = ','
+ * print(*objects)
+ *
+ * Output:
+ *
+ * 0,0.0,0/1,0+0i,zero,zero
+ *
+ * With no argument given, writes the content of <tt>$_</tt>
+ * (which is usually the most recent user input):
+ *
+ * gets # Sets $_ to the most recent user input.
+ * print # Prints $_.
*
- * cat12399
- * cat, 1, 2, 3, 99
*/
static VALUE
-rb_f_print(int argc, VALUE *argv)
+rb_f_print(int argc, const VALUE *argv, VALUE _)
{
- rb_io_print(argc, argv, rb_stdout);
+ rb_io_print(argc, argv, rb_ractor_stdout());
return Qnil;
}
/*
* call-seq:
- * ios.putc(obj) -> obj
+ * putc(object) -> object
*
- * If <i>obj</i> is <code>Numeric</code>, write the character whose code is
- * the least-significant byte of <i>obj</i>, otherwise write the first byte
- * of the string representation of <i>obj</i> to <em>ios</em>. Note: This
- * method is not safe for use with multi-byte characters as it will truncate
- * them.
+ * Writes a character to the stream.
+ * See {Character IO}[rdoc-ref:IO@Character+IO].
*
- * $stdout.putc "A"
- * $stdout.putc 65
+ * If +object+ is numeric, converts to integer if necessary,
+ * then writes the character whose code is the
+ * least significant byte;
+ * if +object+ is a string, writes the first character:
*
- * <em>produces:</em>
+ * $stdout.putc "A"
+ * $stdout.putc 65
+ *
+ * Output:
*
* AA
+ *
*/
static VALUE
@@ -6868,40 +8871,48 @@ rb_io_putc(VALUE io, VALUE ch)
{
VALUE str;
if (RB_TYPE_P(ch, T_STRING)) {
- str = rb_str_substr(ch, 0, 1);
+ str = rb_str_substr(ch, 0, 1);
}
else {
- char c = NUM2CHR(ch);
- str = rb_str_new(&c, 1);
+ char c = NUM2CHR(ch);
+ str = rb_str_new(&c, 1);
}
rb_io_write(io, str);
return ch;
}
+#define forward(obj, id, argc, argv) \
+ rb_funcallv_kw(obj, id, argc, argv, RB_PASS_CALLED_KEYWORDS)
+#define forward_public(obj, id, argc, argv) \
+ rb_funcallv_public_kw(obj, id, argc, argv, RB_PASS_CALLED_KEYWORDS)
+#define forward_current(id, argc, argv) \
+ forward_public(ARGF.current_file, id, argc, argv)
+
/*
* call-seq:
- * putc(int) -> int
+ * putc(int) -> int
*
* Equivalent to:
*
* $stdout.putc(int)
*
- * Refer to the documentation for IO#putc for important information regarding
- * multi-byte characters.
+ * See IO#putc for important information regarding multi-byte characters.
+ *
*/
static VALUE
rb_f_putc(VALUE recv, VALUE ch)
{
- if (recv == rb_stdout) {
- return rb_io_putc(recv, ch);
+ VALUE r_stdout = rb_ractor_stdout();
+ if (recv == r_stdout) {
+ return rb_io_putc(recv, ch);
}
- return rb_funcall2(rb_stdout, rb_intern("putc"), 1, &ch);
+ return forward(r_stdout, rb_intern("putc"), 1, &ch);
}
-static int
-str_end_with_asciichar(VALUE str, int c)
+int
+rb_str_end_with_asciichar(VALUE str, int c)
{
long len = RSTRING_LEN(str);
const char *ptr = RSTRING_PTR(str);
@@ -6910,7 +8921,7 @@ str_end_with_asciichar(VALUE str, int c)
if (len == 0) return 0;
if ((n = rb_enc_mbminlen(enc)) == 1) {
- return ptr[len - 1] == c;
+ return ptr[len - 1] == c;
}
return rb_enc_ascget(ptr + ((len - 1) / n) * n, ptr + len, &n, enc) == c;
}
@@ -6922,65 +8933,101 @@ io_puts_ary(VALUE ary, VALUE out, int recur)
long i;
if (recur) {
- tmp = rb_str_new2("[...]");
- rb_io_puts(1, &tmp, out);
- return Qtrue;
+ tmp = rb_str_new2("[...]");
+ rb_io_puts(1, &tmp, out);
+ return Qtrue;
}
ary = rb_check_array_type(ary);
if (NIL_P(ary)) return Qfalse;
for (i=0; i<RARRAY_LEN(ary); i++) {
- tmp = RARRAY_AREF(ary, i);
- rb_io_puts(1, &tmp, out);
+ tmp = RARRAY_AREF(ary, i);
+ rb_io_puts(1, &tmp, out);
}
return Qtrue;
}
/*
* call-seq:
- * ios.puts(obj, ...) -> nil
+ * puts(*objects) -> nil
*
- * Writes the given objects to <em>ios</em> as with
- * <code>IO#print</code>. Writes a record separator (typically a
- * newline) after any that do not already end with a newline sequence.
- * If called with an array argument, writes each element on a new line.
- * If called without arguments, outputs a single record separator.
+ * Writes the given +objects+ to the stream, which must be open for writing;
+ * returns +nil+.\
+ * Writes a newline after each that does not already end with a newline sequence.
+ * If called without arguments, writes a newline.
+ * See {Line IO}[rdoc-ref:IO@Line+IO].
*
- * $stdout.puts("this", "is", "a", "test")
+ * Note that each added newline is the character <tt>"\n"<//tt>,
+ * not the output record separator (<tt>$\\</tt>).
*
- * <em>produces:</em>
+ * Treatment for each object:
+ *
+ * - String: writes the string.
+ * - Neither string nor array: writes <tt>object.to_s</tt>.
+ * - Array: writes each element of the array; arrays may be nested.
+ *
+ * To keep these examples brief, we define this helper method:
+ *
+ * def show(*objects)
+ * # Puts objects to file.
+ * f = File.new('t.tmp', 'w+')
+ * f.puts(objects)
+ * # Return file content.
+ * f.rewind
+ * p f.read
+ * f.close
+ * end
+ *
+ * # Strings without newlines.
+ * show('foo', 'bar', 'baz') # => "foo\nbar\nbaz\n"
+ * # Strings, some with newlines.
+ * show("foo\n", 'bar', "baz\n") # => "foo\nbar\nbaz\n"
+ *
+ * # Neither strings nor arrays:
+ * show(0, 0.0, Rational(0, 1), Complex(9, 0), :zero)
+ * # => "0\n0.0\n0/1\n9+0i\nzero\n"
+ *
+ * # Array of strings.
+ * show(['foo', "bar\n", 'baz']) # => "foo\nbar\nbaz\n"
+ * # Nested arrays.
+ * show([[[0, 1], 2, 3], 4, 5]) # => "0\n1\n2\n3\n4\n5\n"
*
- * this
- * is
- * a
- * test
*/
VALUE
-rb_io_puts(int argc, VALUE *argv, VALUE out)
+rb_io_puts(int argc, const VALUE *argv, VALUE out)
{
- int i;
- VALUE line;
+ VALUE line, args[2];
/* if no argument given, print newline. */
if (argc == 0) {
- rb_io_write(out, rb_default_rs);
- return Qnil;
+ rb_io_write(out, rb_default_rs);
+ return Qnil;
}
- for (i=0; i<argc; i++) {
- if (RB_TYPE_P(argv[i], T_STRING)) {
- line = argv[i];
- goto string;
- }
- if (rb_exec_recursive(io_puts_ary, argv[i], out)) {
- continue;
- }
- line = rb_obj_as_string(argv[i]);
- string:
- rb_io_write(out, line);
- if (RSTRING_LEN(line) == 0 ||
- !str_end_with_asciichar(line, '\n')) {
- rb_io_write(out, rb_default_rs);
- }
+ for (int i = 0; i < argc; i++) {
+ // Convert the argument to a string:
+ if (RB_TYPE_P(argv[i], T_STRING)) {
+ line = argv[i];
+ }
+ else if (rb_exec_recursive(io_puts_ary, argv[i], out)) {
+ continue;
+ }
+ else {
+ line = rb_obj_as_string(argv[i]);
+ }
+
+ // Write the line:
+ int n = 0;
+ if (RSTRING_LEN(line) == 0) {
+ args[n++] = rb_default_rs;
+ }
+ else {
+ args[n++] = line;
+ if (!rb_str_end_with_asciichar(line, '\n')) {
+ args[n++] = rb_default_rs;
+ }
+ }
+
+ rb_io_writev(out, n, args);
}
return Qnil;
@@ -6988,115 +9035,122 @@ rb_io_puts(int argc, VALUE *argv, VALUE out)
/*
* call-seq:
- * puts(obj, ...) -> nil
+ * puts(*objects) -> nil
*
* Equivalent to
*
- * $stdout.puts(obj, ...)
+ * $stdout.puts(objects)
*/
static VALUE
rb_f_puts(int argc, VALUE *argv, VALUE recv)
{
- if (recv == rb_stdout) {
- return rb_io_puts(argc, argv, recv);
+ VALUE r_stdout = rb_ractor_stdout();
+ if (recv == r_stdout) {
+ return rb_io_puts(argc, argv, recv);
}
- return rb_funcall2(rb_stdout, rb_intern("puts"), argc, argv);
+ return forward(r_stdout, rb_intern("puts"), argc, argv);
}
-void
-rb_p(VALUE obj) /* for debug print within C code */
+static VALUE
+rb_p_write(VALUE str)
{
- VALUE str = rb_obj_as_string(rb_inspect(obj));
- if (RB_TYPE_P(rb_stdout, T_FILE) &&
- rb_method_basic_definition_p(CLASS_OF(rb_stdout), id_write)) {
- io_write(rb_stdout, str, 1);
- io_write(rb_stdout, rb_default_rs, 0);
+ VALUE args[2];
+ args[0] = str;
+ args[1] = rb_default_rs;
+ VALUE r_stdout = rb_ractor_stdout();
+ if (RB_TYPE_P(r_stdout, T_FILE) &&
+ rb_method_basic_definition_p(CLASS_OF(r_stdout), id_write)) {
+ io_writev(2, args, r_stdout);
}
else {
- rb_io_write(rb_stdout, str);
- rb_io_write(rb_stdout, rb_default_rs);
+ rb_io_writev(r_stdout, 2, args);
}
+ return Qnil;
}
-struct rb_f_p_arg {
- int argc;
- VALUE *argv;
-};
+void
+rb_p(VALUE obj) /* for debug print within C code */
+{
+ rb_p_write(rb_obj_as_string(rb_inspect(obj)));
+}
static VALUE
-rb_f_p_internal(VALUE arg)
+rb_p_result(int argc, const VALUE *argv)
{
- struct rb_f_p_arg *arg1 = (struct rb_f_p_arg*)arg;
- int argc = arg1->argc;
- VALUE *argv = arg1->argv;
- int i;
VALUE ret = Qnil;
- for (i=0; i<argc; i++) {
- rb_p(argv[i]);
- }
if (argc == 1) {
- ret = argv[0];
+ ret = argv[0];
}
else if (argc > 1) {
- ret = rb_ary_new4(argc, argv);
+ ret = rb_ary_new4(argc, argv);
}
- if (RB_TYPE_P(rb_stdout, T_FILE)) {
- rb_io_flush(rb_stdout);
+ VALUE r_stdout = rb_ractor_stdout();
+ if (RB_TYPE_P(r_stdout, T_FILE)) {
+ rb_uninterruptible(rb_io_flush, r_stdout);
}
return ret;
}
/*
* call-seq:
- * p(obj) -> obj
- * p(obj1, obj2, ...) -> [obj, ...]
- * p() -> nil
+ * p(object) -> obj
+ * p(*objects) -> array of objects
+ * p -> nil
*
- * For each object, directly writes _obj_.+inspect+ followed by a
- * newline to the program's standard output.
+ * For each object +obj+, executes:
*
- * S = Struct.new(:name, :state)
- * s = S['dave', 'TX']
- * p s
+ * $stdout.write(obj.inspect, "\n")
*
- * <em>produces:</em>
+ * With one object given, returns the object;
+ * with multiple objects given, returns an array containing the objects;
+ * with no object given, returns +nil+.
*
- * #<S name="dave", state="TX">
+ * Examples:
+ *
+ * r = Range.new(0, 4)
+ * p r # => 0..4
+ * p [r, r, r] # => [0..4, 0..4, 0..4]
+ * p # => nil
+ *
+ * Output:
+ *
+ * 0..4
+ * [0..4, 0..4, 0..4]
+ *
+ * Kernel#p is designed for debugging purposes.
+ * Ruby implementations may define Kernel#p to be uninterruptible
+ * in whole or in part.
+ * On CRuby, Kernel#p's writing of data is uninterruptible.
*/
static VALUE
rb_f_p(int argc, VALUE *argv, VALUE self)
{
- struct rb_f_p_arg arg;
- arg.argc = argc;
- arg.argv = argv;
-
- return rb_uninterruptible(rb_f_p_internal, (VALUE)&arg);
+ int i;
+ for (i=0; i<argc; i++) {
+ VALUE inspected = rb_obj_as_string(rb_inspect(argv[i]));
+ rb_uninterruptible(rb_p_write, inspected);
+ }
+ return rb_p_result(argc, argv);
}
/*
* call-seq:
- * obj.display(port=$>) -> nil
- *
- * Prints <i>obj</i> on the given port (default <code>$></code>).
- * Equivalent to:
- *
- * def display(port=$>)
- * port.write self
- * end
+ * display(port = $>) -> nil
*
- * For example:
+ * Writes +self+ on the given port:
*
* 1.display
* "cat".display
* [ 4, 5, 6 ].display
* puts
*
- * <em>produces:</em>
+ * Output:
+ *
+ * 1cat[4, 5, 6]
*
- * 1cat456
*/
static VALUE
@@ -7104,28 +9158,35 @@ rb_obj_display(int argc, VALUE *argv, VALUE self)
{
VALUE out;
- if (argc == 0) {
- out = rb_stdout;
- }
- else {
- rb_scan_args(argc, argv, "01", &out);
- }
+ out = (!rb_check_arity(argc, 0, 1) ? rb_ractor_stdout() : argv[0]);
rb_io_write(out, self);
return Qnil;
}
+static int
+rb_stderr_to_original_p(VALUE err)
+{
+ return (err == orig_stderr || RFILE(orig_stderr)->fptr->fd < 0);
+}
+
void
rb_write_error2(const char *mesg, long len)
{
- if (rb_stderr == orig_stderr || RFILE(orig_stderr)->fptr->fd < 0) {
- if (fwrite(mesg, sizeof(char), (size_t)len, stderr) < (size_t)len) {
- /* failed to write to stderr, what can we do? */
- return;
- }
+ VALUE out = rb_ractor_stderr();
+ if (rb_stderr_to_original_p(out)) {
+#ifdef _WIN32
+ if (isatty(fileno(stderr))) {
+ if (rb_w32_write_console(rb_str_new(mesg, len), fileno(stderr)) > 0) return;
+ }
+#endif
+ if (fwrite(mesg, sizeof(char), (size_t)len, stderr) < (size_t)len) {
+ /* failed to write to stderr, what can we do? */
+ return;
+ }
}
else {
- rb_io_write(rb_stderr, rb_str_new(mesg, len));
+ rb_io_write(out, rb_str_new(mesg, len));
}
}
@@ -7138,57 +9199,176 @@ rb_write_error(const char *mesg)
void
rb_write_error_str(VALUE mesg)
{
+ VALUE out = rb_ractor_stderr();
/* a stopgap measure for the time being */
- if (rb_stderr == orig_stderr || RFILE(orig_stderr)->fptr->fd < 0) {
- size_t len = (size_t)RSTRING_LEN(mesg);
- if (fwrite(RSTRING_PTR(mesg), sizeof(char), len, stderr) < len) {
- RB_GC_GUARD(mesg);
- return;
- }
+ if (rb_stderr_to_original_p(out)) {
+ size_t len = (size_t)RSTRING_LEN(mesg);
+#ifdef _WIN32
+ if (isatty(fileno(stderr))) {
+ if (rb_w32_write_console(mesg, fileno(stderr)) > 0) return;
+ }
+#endif
+ if (fwrite(RSTRING_PTR(mesg), sizeof(char), len, stderr) < len) {
+ RB_GC_GUARD(mesg);
+ return;
+ }
}
else {
- /* may unlock GVL, and */
- rb_io_write(rb_stderr, mesg);
+ /* may unlock GVL, and */
+ rb_io_write(out, mesg);
}
}
+int
+rb_stderr_tty_p(void)
+{
+ if (rb_stderr_to_original_p(rb_ractor_stderr()))
+ return isatty(fileno(stderr));
+ return 0;
+}
+
static void
must_respond_to(ID mid, VALUE val, ID id)
{
if (!rb_respond_to(val, mid)) {
- rb_raise(rb_eTypeError, "%s must have %s method, %s given",
- rb_id2name(id), rb_id2name(mid),
- rb_obj_classname(val));
+ rb_raise(rb_eTypeError, "%"PRIsVALUE" must have %"PRIsVALUE" method, %"PRIsVALUE" given",
+ rb_id2str(id), rb_id2str(mid),
+ rb_obj_class(val));
}
}
static void
-stdout_setter(VALUE val, ID id, VALUE *variable)
+stdin_setter(VALUE val, ID id, VALUE *ptr)
+{
+ rb_ractor_stdin_set(val);
+}
+
+static VALUE
+stdin_getter(ID id, VALUE *ptr)
+{
+ return rb_ractor_stdin();
+}
+
+static void
+stdout_setter(VALUE val, ID id, VALUE *ptr)
{
must_respond_to(id_write, val, id);
- *variable = val;
+ rb_ractor_stdout_set(val);
}
static VALUE
-prep_io(int fd, int fmode, VALUE klass, const char *path)
+stdout_getter(ID id, VALUE *ptr)
{
- rb_io_t *fp;
- VALUE io = io_alloc(klass);
+ return rb_ractor_stdout();
+}
- MakeOpenFile(io, fp);
- fp->fd = fd;
-#ifdef __CYGWIN__
- if (!isatty(fd)) {
- fmode |= FMODE_BINMODE;
- setmode(fd, O_BINARY);
+static void
+stderr_setter(VALUE val, ID id, VALUE *ptr)
+{
+ must_respond_to(id_write, val, id);
+ rb_ractor_stderr_set(val);
+}
+
+static VALUE
+stderr_getter(ID id, VALUE *ptr)
+{
+ return rb_ractor_stderr();
+}
+
+static VALUE
+allocate_and_open_new_file(VALUE klass)
+{
+ VALUE self = io_alloc(klass);
+ rb_io_make_open_file(self);
+ return self;
+}
+
+VALUE
+rb_io_open_descriptor(VALUE klass, int descriptor, int mode, VALUE path, VALUE timeout, struct rb_io_encoding *encoding)
+{
+ int state;
+ VALUE self = rb_protect(allocate_and_open_new_file, klass, &state);
+ if (state) {
+ /* if we raised an exception allocating an IO object, but the caller
+ intended to transfer ownership of this FD to us, close the fd before
+ raising the exception. Otherwise, we would leak a FD - the caller
+ expects GC to close the file, but we never got around to assigning
+ it to a rb_io. */
+ if (!(mode & FMODE_EXTERNAL)) {
+ maygvl_close(descriptor, 0);
+ }
+ rb_jump_tag(state);
+ }
+
+
+ rb_io_t *io = RFILE(self)->fptr;
+ io->self = self;
+ io->fd = descriptor;
+ io->mode = mode;
+
+ /* At this point, Ruby fully owns the descriptor, and will close it when
+ the IO gets GC'd (unless FMODE_EXTERNAL was set), no matter what happens
+ in the rest of this method. */
+
+ if (NIL_P(path)) {
+ io->pathv = Qnil;
+ }
+ else {
+ StringValue(path);
+ io->pathv = rb_str_new_frozen(path);
+ }
+
+ io->timeout = timeout;
+
+ ccan_list_head_init(&io->blocking_operations);
+ io->closing_ec = NULL;
+ io->wakeup_mutex = Qnil;
+ io->fork_generation = GET_VM()->fork_gen;
+
+ if (encoding) {
+ io->encs = *encoding;
+ }
+
+ rb_update_max_fd(descriptor);
+
+ return self;
+}
+
+static VALUE
+prep_io(int fd, enum rb_io_mode fmode, VALUE klass, const char *path)
+{
+ VALUE path_value = Qnil;
+ rb_encoding *e;
+ struct rb_io_encoding convconfig;
+
+ if (path) {
+ path_value = rb_obj_freeze(rb_str_new_cstr(path));
}
+
+ e = (fmode & FMODE_BINMODE) ? rb_ascii8bit_encoding() : NULL;
+ rb_io_ext_int_to_encs(e, NULL, &convconfig.enc, &convconfig.enc2, fmode);
+ convconfig.ecflags = (fmode & FMODE_READABLE) ?
+ MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR,
+ 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0;
+#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
+ convconfig.ecflags |= (fmode & FMODE_WRITABLE) ?
+ MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE,
+ 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0;
#endif
- fp->mode = fmode;
- io_check_tty(fp);
- if (path) fp->pathv = rb_obj_freeze(rb_str_new_cstr(path));
- rb_update_max_fd(fd);
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(convconfig.enc2, convconfig.ecflags);
+ convconfig.ecopts = Qnil;
- return io;
+ VALUE self = rb_io_open_descriptor(klass, fd, fmode, path_value, Qnil, &convconfig);
+ rb_io_t*io = RFILE(self)->fptr;
+
+ if (!io_check_tty(io)) {
+#ifdef __CYGWIN__
+ io->mode |= FMODE_BINMODE;
+ setmode(fd, O_BINARY);
+#endif
+ }
+
+ return self;
}
VALUE
@@ -7201,17 +9381,17 @@ rb_io_fdopen(int fd, int oflags, const char *path)
}
static VALUE
-prep_stdio(FILE *f, int fmode, VALUE klass, const char *path)
+prep_stdio(FILE *f, enum rb_io_mode fmode, VALUE klass, const char *path)
{
rb_io_t *fptr;
- VALUE io = prep_io(fileno(f), fmode|FMODE_PREP|DEFAULT_TEXTMODE, klass, path);
+ VALUE io = prep_io(fileno(f), fmode|FMODE_EXTERNAL|DEFAULT_TEXTMODE, klass, path);
GetOpenFile(io, fptr);
fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR;
#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
if (fmode & FMODE_READABLE) {
- fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
+ fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
}
#endif
fptr->stdio_file = f;
@@ -7219,216 +9399,218 @@ prep_stdio(FILE *f, int fmode, VALUE klass, const char *path)
return io;
}
+VALUE
+rb_io_prep_stdin(void)
+{
+ return prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");
+}
+
+VALUE
+rb_io_prep_stdout(void)
+{
+ return prep_stdio(stdout, FMODE_WRITABLE|FMODE_SIGNAL_ON_EPIPE, rb_cIO, "<STDOUT>");
+}
+
+VALUE
+rb_io_prep_stderr(void)
+{
+ return prep_stdio(stderr, FMODE_WRITABLE|FMODE_SYNC, rb_cIO, "<STDERR>");
+}
+
FILE *
rb_io_stdio_file(rb_io_t *fptr)
{
if (!fptr->stdio_file) {
- int oflags = rb_io_fmode_oflags(fptr->mode);
+ int oflags = rb_io_fmode_oflags(fptr->mode) & ~O_EXCL;
fptr->stdio_file = rb_fdopen(fptr->fd, rb_io_oflags_modestr(oflags));
}
return fptr->stdio_file;
}
+static inline void
+rb_io_buffer_init(struct rb_io_internal_buffer *buf)
+{
+ buf->ptr = NULL;
+ buf->off = 0;
+ buf->len = 0;
+ buf->capa = 0;
+}
+
+static inline rb_io_t *
+rb_io_fptr_new(void)
+{
+ rb_io_t *fp = ALLOC(rb_io_t);
+ fp->self = Qnil;
+ fp->fd = -1;
+ fp->stdio_file = NULL;
+ fp->mode = 0;
+ fp->pid = 0;
+ fp->lineno = 0;
+ fp->pathv = Qnil;
+ fp->finalize = 0;
+ rb_io_buffer_init(&fp->wbuf);
+ rb_io_buffer_init(&fp->rbuf);
+ rb_io_buffer_init(&fp->cbuf);
+ fp->readconv = NULL;
+ fp->writeconv = NULL;
+ fp->writeconv_asciicompat = Qnil;
+ fp->writeconv_pre_ecflags = 0;
+ fp->writeconv_pre_ecopts = Qnil;
+ fp->writeconv_initialized = 0;
+ fp->tied_io_for_writing = 0;
+ fp->encs.enc = NULL;
+ fp->encs.enc2 = NULL;
+ fp->encs.ecflags = 0;
+ fp->encs.ecopts = Qnil;
+ fp->write_lock = Qnil;
+ fp->timeout = Qnil;
+ ccan_list_head_init(&fp->blocking_operations);
+ fp->closing_ec = NULL;
+ fp->wakeup_mutex = Qnil;
+ fp->fork_generation = GET_VM()->fork_gen;
+ return fp;
+}
+
+rb_io_t *
+rb_io_make_open_file(VALUE obj)
+{
+ rb_io_t *fp = 0;
+
+ Check_Type(obj, T_FILE);
+ if (RFILE(obj)->fptr) {
+ rb_io_close(obj);
+ rb_io_fptr_finalize(RFILE(obj)->fptr);
+ RFILE(obj)->fptr = 0;
+ }
+ fp = rb_io_fptr_new();
+ fp->self = obj;
+ RFILE(obj)->fptr = fp;
+ return fp;
+}
+
+static VALUE io_initialize(VALUE io, VALUE fnum, VALUE vmode, VALUE opt);
+
/*
* call-seq:
- * IO.new(fd [, mode] [, opt]) -> io
+ * IO.new(fd, mode = 'r', **opts) -> io
*
- * Returns a new IO object (a stream) for the given integer file descriptor
- * +fd+ and +mode+ string. +opt+ may be used to specify parts of +mode+ in a
- * more readable fashion. See also IO.sysopen and IO.for_fd.
+ * Creates and returns a new \IO object (file stream) from a file descriptor.
*
- * IO.new is called by various File and IO opening methods such as IO::open,
- * Kernel#open, and File::open.
+ * \IO.new may be useful for interaction with low-level libraries.
+ * For higher-level interactions, it may be simpler to create
+ * the file stream using File.open.
*
- * === Open Mode
+ * Argument +fd+ must be a valid file descriptor (integer):
*
- * When +mode+ is an integer it must be combination of the modes defined in
- * File::Constants (+File::RDONLY+, +File::WRONLY | File::CREAT+). See the
- * open(2) man page for more information.
+ * path = 't.tmp'
+ * fd = IO.sysopen(path) # => 3
+ * IO.new(fd) # => #<IO:fd 3>
*
- * When +mode+ is a string it must be in one of the following forms:
+ * The new \IO object does not inherit encoding
+ * (because the integer file descriptor does not have an encoding):
*
- * fmode
- * fmode ":" ext_enc
- * fmode ":" ext_enc ":" int_enc
- * fmode ":" "BOM|UTF-*"
+ * File.read('t.ja') # => "こんにちは"
+ * fd = IO.sysopen('t.ja', 'rb')
+ * io = IO.new(fd)
+ * io.external_encoding # => #<Encoding:UTF-8> # Not ASCII-8BIT.
*
- * +fmode+ is an IO open mode string, +ext_enc+ is the external encoding for
- * the IO and +int_enc+ is the internal encoding.
+ * Optional argument +mode+ (defaults to 'r') must specify a valid mode;
+ * see {Access Modes}[rdoc-ref:File@Access+Modes]:
*
- * ==== IO Open Mode
+ * IO.new(fd, 'w') # => #<IO:fd 3>
+ * IO.new(fd, File::WRONLY) # => #<IO:fd 3>
*
- * Ruby allows the following open modes:
+ * Optional keyword arguments +opts+ specify:
*
- * "r" Read-only, starts at beginning of file (default mode).
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
*
- * "r+" Read-write, starts at beginning of file.
- *
- * "w" Write-only, truncates existing file
- * to zero length or creates a new file for writing.
- *
- * "w+" Read-write, truncates existing file to zero length
- * or creates a new file for reading and writing.
- *
- * "a" Write-only, starts at end of file if file exists,
- * otherwise creates a new file for writing.
- *
- * "a+" Read-write, starts at end of file if file exists,
- * otherwise creates a new file for reading and
- * writing.
- *
- * The following modes must be used separately, and along with one or more of
- * the modes seen above.
- *
- * "b" Binary file mode
- * Suppresses EOL <-> CRLF conversion on Windows. And
- * sets external encoding to ASCII-8BIT unless explicitly
- * specified.
- *
- * "t" Text file mode
- *
- * When the open mode of original IO is read only, the mode cannot be
- * changed to be writable. Similarly, the open mode cannot be changed from
- * write only to readable.
- *
- * When such a change is attempted the error is raised in different locations
- * according to the platform.
- *
- * === IO Encoding
- *
- * When +ext_enc+ is specified, strings read will be tagged by the encoding
- * when reading, and strings output will be converted to the specified
- * encoding when writing.
- *
- * When +ext_enc+ and +int_enc+ are specified read strings will be converted
- * from +ext_enc+ to +int_enc+ upon input, and written strings will be
- * converted from +int_enc+ to +ext_enc+ upon output. See Encoding for
- * further details of transcoding on input and output.
- *
- * If "BOM|UTF-8", "BOM|UTF-16LE" or "BOM|UTF16-BE" are used, ruby checks for
- * a Unicode BOM in the input document to help determine the encoding. For
- * UTF-16 encodings the file open mode must be binary. When present, the BOM
- * is stripped and the external encoding from the BOM is used. When the BOM
- * is missing the given Unicode encoding is used as +ext_enc+. (The BOM-set
- * encoding option is case insensitive, so "bom|utf-8" is also valid.)
- *
- * === Options
- *
- * +opt+ can be used instead of +mode+ for improved readability. The
- * following keys are supported:
- *
- * :mode ::
- * Same as +mode+ parameter
- *
- * :\external_encoding ::
- * External encoding for the IO. "-" is a synonym for the default external
- * encoding.
- *
- * :\internal_encoding ::
- * Internal encoding for the IO. "-" is a synonym for the default internal
- * encoding.
- *
- * If the value is nil no conversion occurs.
- *
- * :encoding ::
- * Specifies external and internal encodings as "extern:intern".
- *
- * :textmode ::
- * If the value is truth value, same as "t" in argument +mode+.
- *
- * :binmode ::
- * If the value is truth value, same as "b" in argument +mode+.
- *
- * :autoclose ::
- * If the value is +false+, the +fd+ will be kept open after this IO
- * instance gets finalized.
- *
- * Also, +opt+ can have same keys in String#encode for controlling conversion
- * between the external encoding and the internal encoding.
- *
- * === Example 1
- *
- * fd = IO.sysopen("/dev/tty", "w")
- * a = IO.new(fd,"w")
- * $stderr.puts "Hello"
- * a.puts "World"
- *
- * Produces:
- *
- * Hello
- * World
- *
- * === Example 2
- *
- * require 'fcntl'
- *
- * fd = STDERR.fcntl(Fcntl::F_DUPFD)
- * io = IO.new(fd, mode: 'w:UTF-16LE', cr_newline: true)
- * io.puts "Hello, World!"
+ * Examples:
*
- * fd = STDERR.fcntl(Fcntl::F_DUPFD)
- * io = IO.new(fd, mode: 'w', cr_newline: true,
- * external_encoding: Encoding::UTF_16LE)
- * io.puts "Hello, World!"
+ * IO.new(fd, internal_encoding: nil) # => #<IO:fd 3>
+ * IO.new(fd, autoclose: true) # => #<IO:fd 3>
*
- * Both of above print "Hello, World!" in UTF-16LE to standard error output
- * with converting EOL generated by <code>puts</code> to CR.
*/
static VALUE
rb_io_initialize(int argc, VALUE *argv, VALUE io)
{
VALUE fnum, vmode;
- rb_io_t *fp;
- int fd, fmode, oflags = O_RDONLY;
- convconfig_t convconfig;
VALUE opt;
+
+ rb_scan_args(argc, argv, "11:", &fnum, &vmode, &opt);
+ return io_initialize(io, fnum, vmode, opt);
+}
+
+static VALUE
+io_initialize(VALUE io, VALUE fnum, VALUE vmode, VALUE opt)
+{
+ rb_io_t *fp;
+ int fd, oflags = O_RDONLY;
+ enum rb_io_mode fmode;
+ struct rb_io_encoding convconfig;
#if defined(HAVE_FCNTL) && defined(F_GETFL)
int ofmode;
#else
struct stat st;
#endif
-
- argc = rb_scan_args(argc, argv, "11:", &fnum, &vmode, &opt);
rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &fmode, &convconfig);
fd = NUM2INT(fnum);
if (rb_reserved_fd_p(fd)) {
- rb_raise(rb_eArgError, "The given fd is not accessible because RubyVM reserves it");
+ rb_raise(rb_eArgError, "The given fd is not accessible because RubyVM reserves it");
}
#if defined(HAVE_FCNTL) && defined(F_GETFL)
oflags = fcntl(fd, F_GETFL);
if (oflags == -1) rb_sys_fail(0);
#else
- if (fstat(fd, &st) == -1) rb_sys_fail(0);
+ if (fstat(fd, &st) < 0) rb_sys_fail(0);
#endif
rb_update_max_fd(fd);
#if defined(HAVE_FCNTL) && defined(F_GETFL)
ofmode = rb_io_oflags_fmode(oflags);
if (NIL_P(vmode)) {
- fmode = ofmode;
+ fmode = ofmode;
}
else if ((~ofmode & fmode) & FMODE_READWRITE) {
- VALUE error = INT2FIX(EINVAL);
- rb_exc_raise(rb_class_new_instance(1, &error, rb_eSystemCallError));
+ VALUE error = INT2FIX(EINVAL);
+ rb_exc_raise(rb_class_new_instance(1, &error, rb_eSystemCallError));
}
#endif
- if (!NIL_P(opt) && rb_hash_aref(opt, sym_autoclose) == Qfalse) {
- fmode |= FMODE_PREP;
+ VALUE path = Qnil;
+
+ if (!NIL_P(opt)) {
+ if (rb_hash_aref(opt, sym_autoclose) == Qfalse) {
+ fmode |= FMODE_EXTERNAL;
+ }
+
+ path = rb_hash_aref(opt, RB_ID2SYM(idPath));
+ if (!NIL_P(path)) {
+ StringValue(path);
+ path = rb_str_new_frozen(path);
+ }
}
+
MakeOpenFile(io, fp);
+ fp->self = io;
fp->fd = fd;
fp->mode = fmode;
fp->encs = convconfig;
+ fp->pathv = path;
+ fp->timeout = Qnil;
+ ccan_list_head_init(&fp->blocking_operations);
+ fp->closing_ec = NULL;
+ fp->wakeup_mutex = Qnil;
+ fp->fork_generation = GET_VM()->fork_gen;
clear_codeconv(fp);
io_check_tty(fp);
if (fileno(stdin) == fd)
- fp->stdio_file = stdin;
+ fp->stdio_file = stdin;
else if (fileno(stdout) == fd)
- fp->stdio_file = stdout;
+ fp->stdio_file = stdout;
else if (fileno(stderr) == fd)
- fp->stdio_file = stderr;
+ fp->stdio_file = stderr;
if (fmode & FMODE_SETENC_BY_BOM) io_set_encoding_by_bom(io);
return io;
@@ -7436,42 +9618,105 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io)
/*
* call-seq:
- * File.new(filename, mode="r" [, opt]) -> file
- * File.new(filename [, mode [, perm]] [, opt]) -> file
+ * set_encoding_by_bom -> encoding or nil
+ *
+ * If the stream begins with a BOM
+ * ({byte order marker}[https://en.wikipedia.org/wiki/Byte_order_mark]),
+ * consumes the BOM and sets the external encoding accordingly;
+ * returns the result encoding if found, or +nil+ otherwise:
+ *
+ * File.write('t.tmp', "\u{FEFF}abc")
+ * io = File.open('t.tmp', 'rb')
+ * io.set_encoding_by_bom # => #<Encoding:UTF-8>
+ * io.close
+ *
+ * File.write('t.tmp', 'abc')
+ * io = File.open('t.tmp', 'rb')
+ * io.set_encoding_by_bom # => nil
+ * io.close
+ *
+ * Raises an exception if the stream is not binmode
+ * or its encoding has already been set.
+ *
+ */
+
+static VALUE
+rb_io_set_encoding_by_bom(VALUE io)
+{
+ rb_io_t *fptr;
+
+ GetOpenFile(io, fptr);
+ if (!(fptr->mode & FMODE_BINMODE)) {
+ rb_raise(rb_eArgError, "ASCII incompatible encoding needs binmode");
+ }
+ if (fptr->encs.enc2) {
+ rb_raise(rb_eArgError, "encoding conversion is set");
+ }
+ else if (fptr->encs.enc && fptr->encs.enc != rb_ascii8bit_encoding()) {
+ rb_raise(rb_eArgError, "encoding is set to %s already",
+ rb_enc_name(fptr->encs.enc));
+ }
+ if (!io_set_encoding_by_bom(io)) return Qnil;
+ return rb_enc_from_encoding(fptr->encs.enc);
+}
+
+/*
+ * call-seq:
+ * File.new(path, mode = 'r', perm = 0666, **opts) -> file
+ *
+ * Opens the file at the given +path+ according to the given +mode+;
+ * creates and returns a new File object for that file.
+ *
+ * The new File object is buffered mode (or non-sync mode), unless
+ * +filename+ is a tty.
+ * See IO#flush, IO#fsync, IO#fdatasync, and IO#sync=.
+ *
+ * Argument +path+ must be a valid file path:
+ *
+ * f = File.new('/etc/fstab')
+ * f.close
+ * f = File.new('t.txt')
+ * f.close
+ *
+ * Optional argument +mode+ (defaults to 'r') must specify a valid mode;
+ * see {Access Modes}[rdoc-ref:File@Access+Modes]:
*
- * Opens the file named by +filename+ according to the given +mode+ and
- * returns a new File object.
+ * f = File.new('t.tmp', 'w')
+ * f.close
+ * f = File.new('t.tmp', File::RDONLY)
+ * f.close
*
- * See IO.new for a description of +mode+ and +opt+.
+ * Optional argument +perm+ (defaults to 0666) must specify valid permissions
+ * see {File Permissions}[rdoc-ref:File@File+Permissions]:
*
- * If a file is being created, permission bits may be given in +perm+. These
- * mode and permission bits are platform dependent; on Unix systems, see
- * open(2) and chmod(2) man pages for details.
+ * f = File.new('t.tmp', File::CREAT, 0644)
+ * f.close
+ * f = File.new('t.tmp', File::CREAT, 0444)
+ * f.close
*
- * === Examples
+ * Optional keyword arguments +opts+ specify:
+ *
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
*
- * f = File.new("testfile", "r")
- * f = File.new("newfile", "w+")
- * f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)
*/
static VALUE
rb_file_initialize(int argc, VALUE *argv, VALUE io)
{
if (RFILE(io)->fptr) {
- rb_raise(rb_eRuntimeError, "reinitializing File");
+ rb_raise(rb_eRuntimeError, "reinitializing File");
}
- if (0 < argc && argc < 3) {
- VALUE fd = rb_check_convert_type(argv[0], T_FIXNUM, "Fixnum", "to_int");
+ VALUE fname, vmode, vperm, opt;
+ int posargc = rb_scan_args(argc, argv, "12:", &fname, &vmode, &vperm, &opt);
+ if (posargc < 3) { /* perm is File only */
+ VALUE fd = rb_check_to_int(fname);
- if (!NIL_P(fd)) {
- argv[0] = fd;
- return rb_io_initialize(argc, argv, io);
- }
+ if (!NIL_P(fd)) {
+ return io_initialize(io, fd, vmode, opt);
+ }
}
- rb_open_file(argc, argv, io);
-
- return io;
+ return rb_open_file(io, fname, vmode, vperm, opt);
}
/* :nodoc: */
@@ -7479,20 +9724,20 @@ static VALUE
rb_io_s_new(int argc, VALUE *argv, VALUE klass)
{
if (rb_block_given_p()) {
- const char *cname = rb_class2name(klass);
+ VALUE cname = rb_obj_as_string(klass);
- rb_warn("%s::new() does not take block; use %s::open() instead",
- cname, cname);
+ rb_warn("%"PRIsVALUE"::new() does not take block; use %"PRIsVALUE"::open() instead",
+ cname, cname);
}
- return rb_class_new_instance(argc, argv, klass);
+ return rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
}
/*
* call-seq:
- * IO.for_fd(fd, mode [, opt]) -> io
+ * IO.for_fd(fd, mode = 'r', **opts) -> io
*
- * Synonym for <code>IO.new</code>.
+ * Synonym for IO.new.
*
*/
@@ -7509,15 +9754,15 @@ rb_io_s_for_fd(int argc, VALUE *argv, VALUE klass)
* ios.autoclose? -> true or false
*
* Returns +true+ if the underlying file descriptor of _ios_ will be
- * closed automatically at its finalization, otherwise +false+.
+ * closed at its finalization or at calling #close, otherwise +false+.
*/
static VALUE
rb_io_autoclose_p(VALUE io)
{
- rb_io_t *fptr;
- GetOpenFile(io, fptr);
- return (fptr->mode & FMODE_PREP) ? Qfalse : Qtrue;
+ rb_io_t *fptr = RFILE(io)->fptr;
+ rb_io_check_closed(fptr);
+ return RBOOL(!(fptr->mode & FMODE_EXTERNAL));
}
/*
@@ -7526,15 +9771,15 @@ rb_io_autoclose_p(VALUE io)
*
* Sets auto-close flag.
*
- * f = open("/dev/null")
- * IO.for_fd(f.fileno)
- * # ...
- * f.gets # may cause IOError
+ * f = File.open(File::NULL)
+ * IO.for_fd(f.fileno).close
+ * f.gets # raises Errno::EBADF
*
- * f = open("/dev/null")
- * IO.for_fd(f.fileno).autoclose = true
- * # ...
- * f.gets # won't cause IOError
+ * f = File.open(File::NULL)
+ * g = IO.for_fd(f.fileno)
+ * g.autoclose = false
+ * g.close
+ * f.gets # won't cause Errno::EBADF
*/
static VALUE
@@ -7543,28 +9788,232 @@ rb_io_set_autoclose(VALUE io, VALUE autoclose)
rb_io_t *fptr;
GetOpenFile(io, fptr);
if (!RTEST(autoclose))
- fptr->mode |= FMODE_PREP;
+ fptr->mode |= FMODE_EXTERNAL;
else
- fptr->mode &= ~FMODE_PREP;
- return io;
+ fptr->mode &= ~FMODE_EXTERNAL;
+ return autoclose;
}
-static void
-argf_mark(void *ptr)
+static VALUE
+io_wait_event(VALUE io, int event, VALUE timeout, int return_io)
{
- struct argf *p = ptr;
- rb_gc_mark(p->filename);
- rb_gc_mark(p->current_file);
- rb_gc_mark(p->argv);
- rb_gc_mark(p->encs.ecopts);
+ VALUE result = rb_io_wait(io, RB_INT2NUM(event), timeout);
+
+ if (!RB_TEST(result)) {
+ return Qnil;
+ }
+
+ int mask = RB_NUM2INT(result);
+
+ if (mask & event) {
+ if (return_io)
+ return io;
+ else
+ return result;
+ }
+ else {
+ return Qfalse;
+ }
+}
+
+/*
+ * call-seq:
+ * io.wait_readable -> truthy or falsy
+ * io.wait_readable(timeout) -> truthy or falsy
+ *
+ * Waits until IO is readable and returns a truthy value, or a falsy
+ * value when times out. Returns a truthy value immediately when
+ * buffered data is available.
+ */
+
+static VALUE
+io_wait_readable(int argc, VALUE *argv, VALUE io)
+{
+ rb_io_t *fptr;
+
+ RB_IO_POINTER(io, fptr);
+ rb_io_check_char_readable(fptr);
+
+ if (rb_io_read_pending(fptr)) return Qtrue;
+
+ rb_check_arity(argc, 0, 1);
+ VALUE timeout = (argc == 1 ? argv[0] : Qnil);
+
+ return io_wait_event(io, RUBY_IO_READABLE, timeout, 1);
+}
+
+/*
+ * call-seq:
+ * io.wait_writable -> truthy or falsy
+ * io.wait_writable(timeout) -> truthy or falsy
+ *
+ * Waits until IO is writable and returns a truthy value or a falsy
+ * value when times out.
+ */
+static VALUE
+io_wait_writable(int argc, VALUE *argv, VALUE io)
+{
+ rb_io_t *fptr;
+
+ RB_IO_POINTER(io, fptr);
+ rb_io_check_writable(fptr);
+
+ rb_check_arity(argc, 0, 1);
+ VALUE timeout = (argc == 1 ? argv[0] : Qnil);
+
+ return io_wait_event(io, RUBY_IO_WRITABLE, timeout, 1);
+}
+
+/*
+ * call-seq:
+ * io.wait_priority -> truthy or falsy
+ * io.wait_priority(timeout) -> truthy or falsy
+ *
+ * Waits until IO is priority and returns a truthy value or a falsy
+ * value when times out. Priority data is sent and received using
+ * the Socket::MSG_OOB flag and is typically limited to streams.
+ */
+static VALUE
+io_wait_priority(int argc, VALUE *argv, VALUE io)
+{
+ rb_io_t *fptr = NULL;
+
+ RB_IO_POINTER(io, fptr);
+ rb_io_check_char_readable(fptr);
+
+ if (rb_io_read_pending(fptr)) return Qtrue;
+
+ rb_check_arity(argc, 0, 1);
+ VALUE timeout = argc == 1 ? argv[0] : Qnil;
+
+ return io_wait_event(io, RUBY_IO_PRIORITY, timeout, 1);
+}
+
+static int
+wait_mode_sym(VALUE mode)
+{
+ if (mode == ID2SYM(rb_intern("r"))) {
+ return RB_WAITFD_IN;
+ }
+ if (mode == ID2SYM(rb_intern("read"))) {
+ return RB_WAITFD_IN;
+ }
+ if (mode == ID2SYM(rb_intern("readable"))) {
+ return RB_WAITFD_IN;
+ }
+ if (mode == ID2SYM(rb_intern("w"))) {
+ return RB_WAITFD_OUT;
+ }
+ if (mode == ID2SYM(rb_intern("write"))) {
+ return RB_WAITFD_OUT;
+ }
+ if (mode == ID2SYM(rb_intern("writable"))) {
+ return RB_WAITFD_OUT;
+ }
+ if (mode == ID2SYM(rb_intern("rw"))) {
+ return RB_WAITFD_IN|RB_WAITFD_OUT;
+ }
+ if (mode == ID2SYM(rb_intern("read_write"))) {
+ return RB_WAITFD_IN|RB_WAITFD_OUT;
+ }
+ if (mode == ID2SYM(rb_intern("readable_writable"))) {
+ return RB_WAITFD_IN|RB_WAITFD_OUT;
+ }
+
+ rb_raise(rb_eArgError, "unsupported mode: %"PRIsVALUE, mode);
+}
+
+static inline enum rb_io_event
+io_event_from_value(VALUE value)
+{
+ int events = RB_NUM2INT(value);
+
+ if (events <= 0) rb_raise(rb_eArgError, "Events must be positive integer!");
+
+ return events;
+}
+
+/*
+ * call-seq:
+ * io.wait(events, timeout) -> event mask, false or nil
+ * io.wait(*event_symbols[, timeout]) -> self, true, or false
+ *
+ * Waits until the IO becomes ready for the specified events and returns the
+ * subset of events that become ready, or a falsy value when times out.
+ *
+ * The events can be a bit mask of +IO::READABLE+, +IO::WRITABLE+ or
+ * +IO::PRIORITY+.
+ *
+ * Returns an event mask (truthy value) immediately when buffered data is
+ * available.
+ *
+ * The second form: if one or more event symbols (+:read+, +:write+, or
+ * +:read_write+) are passed, the event mask is the bit OR of the bitmask
+ * corresponding to those symbols. In this form, +timeout+ is optional, the
+ * order of the arguments is arbitrary, and returns +io+ if any of the
+ * events is ready.
+ */
+
+static VALUE
+io_wait(int argc, VALUE *argv, VALUE io)
+{
+ VALUE timeout = Qundef;
+ enum rb_io_event events = 0;
+ int return_io = 0;
+
+ if (argc != 2 || (RB_SYMBOL_P(argv[0]) || RB_SYMBOL_P(argv[1]))) {
+ // We'd prefer to return the actual mask, but this form would return the io itself:
+ return_io = 1;
+
+ // Slow/messy path:
+ for (int i = 0; i < argc; i += 1) {
+ if (RB_SYMBOL_P(argv[i])) {
+ events |= wait_mode_sym(argv[i]);
+ }
+ else if (UNDEF_P(timeout)) {
+ rb_time_interval(timeout = argv[i]);
+ }
+ else {
+ rb_raise(rb_eArgError, "timeout given more than once");
+ }
+ }
+
+ if (UNDEF_P(timeout)) timeout = Qnil;
+
+ if (events == 0) {
+ events = RUBY_IO_READABLE;
+ }
+ }
+ else /* argc == 2 and neither are symbols */ {
+ // This is the fast path:
+ events = io_event_from_value(argv[0]);
+ timeout = argv[1];
+ }
+
+ if (events & RUBY_IO_READABLE) {
+ rb_io_t *fptr = NULL;
+ RB_IO_POINTER(io, fptr);
+
+ if (rb_io_read_pending(fptr)) {
+ // This was the original behaviour:
+ if (return_io) return Qtrue;
+ // New behaviour always returns an event mask:
+ else return RB_INT2NUM(RUBY_IO_READABLE);
+ }
+ }
+
+ return io_wait_event(io, events, timeout, return_io);
}
static void
-argf_free(void *ptr)
+argf_mark_and_move(void *ptr)
{
struct argf *p = ptr;
- xfree(p->inplace);
- xfree(p);
+ rb_gc_mark_and_move(&p->filename);
+ rb_gc_mark_and_move(&p->current_file);
+ rb_gc_mark_and_move(&p->argv);
+ rb_gc_mark_and_move(&p->inplace);
+ rb_gc_mark_and_move(&p->encs.ecopts);
}
static size_t
@@ -7572,24 +10021,22 @@ argf_memsize(const void *ptr)
{
const struct argf *p = ptr;
size_t size = sizeof(*p);
- if (!ptr) return 0;
- if (p->inplace) size += strlen(p->inplace) + 1;
return size;
}
static const rb_data_type_t argf_type = {
"ARGF",
- {argf_mark, argf_free, argf_memsize},
- NULL, NULL, RUBY_TYPED_FREE_IMMEDIATELY
+ {argf_mark_and_move, RUBY_TYPED_DEFAULT_FREE, argf_memsize, argf_mark_and_move},
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
};
static inline void
-argf_init(struct argf *p, VALUE v)
+argf_init(VALUE argf, struct argf *p, VALUE v)
{
p->filename = Qnil;
p->current_file = Qnil;
p->lineno = 0;
- p->argv = v;
+ RB_OBJ_WRITE(argf, &p->argv, v);
}
static VALUE
@@ -7598,7 +10045,7 @@ argf_alloc(VALUE klass)
struct argf *p;
VALUE argf = TypedData_Make_Struct(klass, struct argf, &argf_type, p);
- argf_init(p, Qnil);
+ argf_init(argf, p, Qnil);
return argf;
}
@@ -7609,7 +10056,7 @@ static VALUE
argf_initialize(VALUE argf, VALUE argv)
{
memset(&ARGF, 0, sizeof(ARGF));
- argf_init(&ARGF, argv);
+ argf_init(argf, &ARGF, argv);
return argf;
}
@@ -7620,12 +10067,8 @@ argf_initialize_copy(VALUE argf, VALUE orig)
{
if (!OBJ_INIT_COPY(argf, orig)) return argf;
ARGF = argf_of(orig);
- ARGF.argv = rb_obj_dup(ARGF.argv);
- if (ARGF.inplace) {
- const char *inplace = ARGF.inplace;
- ARGF.inplace = 0;
- ARGF.inplace = ruby_strdup(inplace);
- }
+ rb_gc_writebarrier_remember(argf);
+ ARGF_SET(argv, rb_obj_dup(ARGF.argv));
return argf;
}
@@ -7633,11 +10076,11 @@ argf_initialize_copy(VALUE argf, VALUE orig)
* call-seq:
* ARGF.lineno = integer -> integer
*
- * Sets the line number of +ARGF+ as a whole to the given +Integer+.
+ * Sets the line number of ARGF as a whole to the given Integer.
*
- * +ARGF+ sets the line number automatically as you read data, so normally
+ * ARGF sets the line number automatically as you read data, so normally
* you will not need to set it explicitly. To access the current line number
- * use +ARGF.lineno+.
+ * use ARGF.lineno.
*
* For example:
*
@@ -7652,15 +10095,15 @@ argf_set_lineno(VALUE argf, VALUE val)
{
ARGF.lineno = NUM2INT(val);
ARGF.last_lineno = ARGF.lineno;
- return Qnil;
+ return val;
}
/*
* call-seq:
- * ARGF.lineno -> integer
+ * ARGF.lineno -> integer
*
* Returns the current line number of ARGF as a whole. This value
- * can be set manually with +ARGF.lineno=+.
+ * can be set manually with ARGF.lineno=.
*
* For example:
*
@@ -7677,7 +10120,7 @@ argf_lineno(VALUE argf)
static VALUE
argf_forward(int argc, VALUE *argv, VALUE argf)
{
- return rb_funcall3(ARGF.current_file, rb_frame_this_func(), argc, argv);
+ return forward_current(rb_frame_this_func(), argc, argv);
}
#define next_argv() argf_next_argv(argf)
@@ -7685,7 +10128,7 @@ argf_forward(int argc, VALUE *argv, VALUE argf)
(ARGF.current_file == rb_stdin && !RB_TYPE_P(ARGF.current_file, T_FILE))
#define ARGF_FORWARD(argc, argv) do {\
if (ARGF_GENERIC_INPUT_P())\
- return argf_forward((argc), (argv), argf);\
+ return argf_forward((argc), (argv), argf);\
} while (0)
#define NEXT_ARGF_FORWARD(argc, argv) do {\
if (!next_argv()) return Qnil;\
@@ -7698,9 +10141,9 @@ argf_close(VALUE argf)
VALUE file = ARGF.current_file;
if (file == rb_stdin) return;
if (RB_TYPE_P(file, T_FILE)) {
- rb_io_set_write_io(file, Qnil);
+ rb_io_set_write_io(file, Qnil);
}
- rb_funcall3(file, rb_intern("close"), 0, 0);
+ io_close(file);
ARGF.init_p = -1;
}
@@ -7710,161 +10153,174 @@ argf_next_argv(VALUE argf)
char *fn;
rb_io_t *fptr;
int stdout_binmode = 0;
- int fmode;
+ enum rb_io_mode fmode;
+
+ VALUE r_stdout = rb_ractor_stdout();
- if (RB_TYPE_P(rb_stdout, T_FILE)) {
- GetOpenFile(rb_stdout, fptr);
+ if (RB_TYPE_P(r_stdout, T_FILE)) {
+ GetOpenFile(r_stdout, fptr);
if (fptr->mode & FMODE_BINMODE)
stdout_binmode = 1;
}
if (ARGF.init_p == 0) {
- if (!NIL_P(ARGF.argv) && RARRAY_LEN(ARGF.argv) > 0) {
- ARGF.next_p = 1;
- }
- else {
- ARGF.next_p = -1;
- }
- ARGF.init_p = 1;
+ if (!NIL_P(ARGF.argv) && RARRAY_LEN(ARGF.argv) > 0) {
+ ARGF.next_p = 1;
+ }
+ else {
+ ARGF.next_p = -1;
+ }
+ ARGF.init_p = 1;
}
else {
- if (NIL_P(ARGF.argv)) {
- ARGF.next_p = -1;
- }
- else if (ARGF.next_p == -1 && RARRAY_LEN(ARGF.argv) > 0) {
- ARGF.next_p = 1;
- }
+ if (NIL_P(ARGF.argv)) {
+ ARGF.next_p = -1;
+ }
+ else if (ARGF.next_p == -1 && RARRAY_LEN(ARGF.argv) > 0) {
+ ARGF.next_p = 1;
+ }
}
if (ARGF.next_p == 1) {
+ if (ARGF.init_p == 1) argf_close(argf);
retry:
- if (RARRAY_LEN(ARGF.argv) > 0) {
- ARGF.filename = rb_ary_shift(ARGF.argv);
- fn = StringValueCStr(ARGF.filename);
- if (strlen(fn) == 1 && fn[0] == '-') {
- ARGF.current_file = rb_stdin;
- if (ARGF.inplace) {
- rb_warn("Can't do inplace edit for stdio; skipping");
- goto retry;
- }
- }
- else {
- VALUE write_io = Qnil;
- int fr = rb_sysopen(ARGF.filename, O_RDONLY, 0);
-
- if (ARGF.inplace) {
- struct stat st;
+ if (RARRAY_LEN(ARGF.argv) > 0) {
+ VALUE filename = rb_ary_shift(ARGF.argv);
+ FilePathValue(filename);
+ ARGF_SET(filename, filename);
+ filename = rb_str_encode_ospath(filename);
+ fn = StringValueCStr(filename);
+ if (RSTRING_LEN(filename) == 1 && fn[0] == '-') {
+ ARGF_SET(current_file, rb_stdin);
+ if (ARGF.inplace) {
+ rb_warn("Can't do inplace edit for stdio; skipping");
+ goto retry;
+ }
+ }
+ else {
+ VALUE write_io = Qnil;
+ int fr = rb_sysopen(filename, O_RDONLY, 0);
+
+ if (ARGF.inplace) {
+ struct stat st;
#ifndef NO_SAFE_RENAME
- struct stat st2;
-#endif
- VALUE str;
- int fw;
-
- if (RB_TYPE_P(rb_stdout, T_FILE) && rb_stdout != orig_stdout) {
- rb_io_close(rb_stdout);
- }
- fstat(fr, &st);
- if (*ARGF.inplace) {
- str = rb_str_new2(fn);
- rb_str_cat2(str, ARGF.inplace);
+ struct stat st2;
+#endif
+ VALUE str;
+ int fw;
+
+ if (RB_TYPE_P(r_stdout, T_FILE) && r_stdout != orig_stdout) {
+ rb_io_close(r_stdout);
+ }
+ fstat(fr, &st);
+ str = filename;
+ if (!NIL_P(ARGF.inplace)) {
+ VALUE suffix = ARGF.inplace;
+ str = rb_str_dup(str);
+ if (NIL_P(rb_str_cat_conv_enc_opts(str, RSTRING_LEN(str),
+ RSTRING_PTR(suffix), RSTRING_LEN(suffix),
+ rb_enc_get(suffix), 0, Qnil))) {
+ rb_str_append(str, suffix);
+ }
#ifdef NO_SAFE_RENAME
- (void)close(fr);
- (void)unlink(RSTRING_PTR(str));
- if (rename(fn, RSTRING_PTR(str)) < 0) {
- rb_warn("Can't rename %s to %s: %s, skipping file",
- fn, RSTRING_PTR(str), strerror(errno));
- goto retry;
- }
- fr = rb_sysopen(str, O_RDONLY, 0);
+ (void)close(fr);
+ (void)unlink(RSTRING_PTR(str));
+ if (rename(fn, RSTRING_PTR(str)) < 0) {
+ rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
+ filename, str, strerror(errno));
+ goto retry;
+ }
+ fr = rb_sysopen(str, O_RDONLY, 0);
#else
- if (rename(fn, RSTRING_PTR(str)) < 0) {
- rb_warn("Can't rename %s to %s: %s, skipping file",
- fn, RSTRING_PTR(str), strerror(errno));
- close(fr);
- goto retry;
- }
-#endif
- }
- else {
+ if (rename(fn, RSTRING_PTR(str)) < 0) {
+ rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file",
+ filename, str, strerror(errno));
+ close(fr);
+ goto retry;
+ }
+#endif
+ }
+ else {
#ifdef NO_SAFE_RENAME
- rb_fatal("Can't do inplace edit without backup");
+ rb_fatal("Can't do inplace edit without backup");
#else
- if (unlink(fn) < 0) {
- rb_warn("Can't remove %s: %s, skipping file",
- fn, strerror(errno));
- close(fr);
- goto retry;
- }
-#endif
- }
- fw = rb_sysopen(ARGF.filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
+ if (unlink(fn) < 0) {
+ rb_warn("Can't remove %"PRIsVALUE": %s, skipping file",
+ filename, strerror(errno));
+ close(fr);
+ goto retry;
+ }
+#endif
+ }
+ fw = rb_sysopen(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
#ifndef NO_SAFE_RENAME
- fstat(fw, &st2);
+ fstat(fw, &st2);
#ifdef HAVE_FCHMOD
- fchmod(fw, st.st_mode);
+ fchmod(fw, st.st_mode);
#else
- chmod(fn, st.st_mode);
+ chmod(fn, st.st_mode);
#endif
- if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
- int err;
+ if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
+ int err;
#ifdef HAVE_FCHOWN
- err = fchown(fw, st.st_uid, st.st_gid);
+ err = fchown(fw, st.st_uid, st.st_gid);
#else
- err = chown(fn, st.st_uid, st.st_gid);
-#endif
- if (err && getuid() == 0 && st2.st_uid == 0) {
- const char *wkfn = RSTRING_PTR(ARGF.filename);
- rb_warn("Can't set owner/group of %s to same as %s: %s, skipping file",
- wkfn, fn, strerror(errno));
- (void)close(fr);
- (void)close(fw);
- (void)unlink(wkfn);
- goto retry;
- }
- }
-#endif
- write_io = prep_io(fw, FMODE_WRITABLE, rb_cFile, fn);
- rb_stdout = write_io;
- if (stdout_binmode) rb_io_binmode(rb_stdout);
- }
- fmode = FMODE_READABLE;
- if (!ARGF.binmode) {
- fmode |= DEFAULT_TEXTMODE;
- }
- ARGF.current_file = prep_io(fr, fmode, rb_cFile, fn);
- if (!NIL_P(write_io)) {
- rb_io_set_write_io(ARGF.current_file, write_io);
- }
- }
- if (ARGF.binmode) rb_io_ascii8bit_binmode(ARGF.current_file);
- GetOpenFile(ARGF.current_file, fptr);
- if (ARGF.encs.enc) {
- fptr->encs = ARGF.encs;
+ err = chown(fn, st.st_uid, st.st_gid);
+#endif
+ if (err && getuid() == 0 && st2.st_uid == 0) {
+ const char *wkfn = RSTRING_PTR(filename);
+ rb_warn("Can't set owner/group of %"PRIsVALUE" to same as %"PRIsVALUE": %s, skipping file",
+ filename, str, strerror(errno));
+ (void)close(fr);
+ (void)close(fw);
+ (void)unlink(wkfn);
+ goto retry;
+ }
+ }
+#endif
+ write_io = prep_io(fw, FMODE_WRITABLE, rb_cFile, fn);
+ rb_ractor_stdout_set(write_io);
+ if (stdout_binmode) rb_io_binmode(rb_stdout);
+ }
+ fmode = FMODE_READABLE;
+ if (!ARGF.binmode) {
+ fmode |= DEFAULT_TEXTMODE;
+ }
+ ARGF_SET(current_file, prep_io(fr, fmode, rb_cFile, fn));
+ if (!NIL_P(write_io)) {
+ rb_io_set_write_io(ARGF.current_file, write_io);
+ }
+ RB_GC_GUARD(filename);
+ }
+ if (ARGF.binmode) rb_io_ascii8bit_binmode(ARGF.current_file);
+ GetOpenFile(ARGF.current_file, fptr);
+ if (ARGF.encs.enc) {
+ fptr->encs = ARGF.encs;
clear_codeconv(fptr);
- }
- else {
- fptr->encs.ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
- if (!ARGF.binmode) {
- fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR;
+ }
+ else {
+ fptr->encs.ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK;
+ if (!ARGF.binmode) {
+ fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR;
#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE
- fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
+ fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE;
#endif
- }
- }
- ARGF.next_p = 0;
- }
- else {
- ARGF.next_p = 1;
- return FALSE;
- }
+ }
+ }
+ ARGF.next_p = 0;
+ }
+ else {
+ ARGF.next_p = 1;
+ return FALSE;
+ }
}
else if (ARGF.next_p == -1) {
- ARGF.current_file = rb_stdin;
- ARGF.filename = rb_str_new2("-");
- if (ARGF.inplace) {
- rb_warn("Can't do inplace edit for stdio");
- rb_stdout = orig_stdout;
- }
+ ARGF_SET(current_file, rb_stdin);
+ ARGF_SET(filename, rb_str_new2("-"));
+ if (ARGF.inplace) {
+ rb_warn("Can't do inplace edit for stdio");
+ rb_ractor_stdout_set(orig_stdout);
+ }
}
if (ARGF.init_p == -1) ARGF.init_p = 1;
return TRUE;
@@ -7879,24 +10335,24 @@ argf_getline(int argc, VALUE *argv, VALUE argf)
retry:
if (!next_argv()) return Qnil;
if (ARGF_GENERIC_INPUT_P()) {
- line = rb_funcall3(ARGF.current_file, idGets, argc, argv);
+ line = forward_current(idGets, argc, argv);
}
else {
- if (argc == 0 && rb_rs == rb_default_rs) {
- line = rb_io_gets(ARGF.current_file);
- }
- else {
- line = rb_io_getline(argc, argv, ARGF.current_file);
- }
- if (NIL_P(line) && ARGF.next_p != -1) {
- argf_close(argf);
- ARGF.next_p = 1;
- goto retry;
- }
+ if (argc == 0 && rb_rs == rb_default_rs) {
+ line = rb_io_gets(ARGF.current_file);
+ }
+ else {
+ line = rb_io_getline(argc, argv, ARGF.current_file);
+ }
+ if (NIL_P(line) && ARGF.next_p != -1) {
+ argf_close(argf);
+ ARGF.next_p = 1;
+ goto retry;
+ }
}
if (!NIL_P(line)) {
- ARGF.lineno = ++lineno;
- ARGF.last_lineno = ARGF.lineno;
+ ARGF.lineno = ++lineno;
+ ARGF.last_lineno = ARGF.lineno;
}
return line;
}
@@ -7916,13 +10372,19 @@ argf_lineno_setter(VALUE val, ID id, VALUE *var)
ARGF.last_lineno = ARGF.lineno = n;
}
+void
+rb_reset_argf_lineno(long n)
+{
+ ARGF.last_lineno = ARGF.lineno = n;
+}
+
static VALUE argf_gets(int, VALUE *, VALUE);
/*
* call-seq:
- * gets(sep=$/) -> string or nil
- * gets(limit) -> string or nil
- * gets(sep,limit) -> string or nil
+ * gets(sep=$/ [, getline_args]) -> string or nil
+ * gets(limit [, getline_args]) -> string or nil
+ * gets(sep, limit [, getline_args]) -> string or nil
*
* Returns (and assigns to <code>$_</code>) the next line from the list
* of files in +ARGV+ (or <code>$*</code>), or from standard input if
@@ -7934,8 +10396,8 @@ static VALUE argf_gets(int, VALUE *, VALUE);
* divided by two consecutive newlines. If the first argument is an
* integer, or optional second argument is given, the returning string
* would not be longer than the given value in bytes. If multiple
- * filenames are present in +ARGV+, +gets(nil)+ will read the contents
- * one file at a time.
+ * filenames are present in +ARGV+, <code>gets(nil)</code> will read
+ * the contents one file at a time.
*
* ARGV << "testfile"
* print while gets
@@ -7955,25 +10417,28 @@ static VALUE
rb_f_gets(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
- return argf_gets(argc, argv, argf);
+ return argf_gets(argc, argv, argf);
}
- return rb_funcall2(argf, idGets, argc, argv);
+ return forward(argf, idGets, argc, argv);
}
/*
* call-seq:
- * ARGF.gets(sep=$/) -> string or nil
- * ARGF.gets(limit) -> string or nil
- * ARGF.gets(sep, limit) -> string or nil
+ * ARGF.gets(sep=$/ [, getline_args]) -> string or nil
+ * ARGF.gets(limit [, getline_args]) -> string or nil
+ * ARGF.gets(sep, limit [, getline_args]) -> string or nil
*
- * Returns the next line from the current file in +ARGF+.
+ * Returns the next line from the current file in ARGF.
*
- * By default lines are assumed to be separated by +$/+; to use a different
- * character as a separator, supply it as a +String+ for the _sep_ argument.
+ * By default lines are assumed to be separated by <code>$/</code>;
+ * to use a different character as a separator, supply it as a String
+ * for the _sep_ argument.
*
* The optional _limit_ argument specifies how many characters of each line
* to return. By default all characters are returned.
*
+ * See IO.readlines for details about getline_args.
+ *
*/
static VALUE
argf_gets(int argc, VALUE *argv, VALUE argf)
@@ -7992,21 +10457,21 @@ rb_gets(void)
VALUE line;
if (rb_rs != rb_default_rs) {
- return rb_f_gets(0, 0, argf);
+ return rb_f_gets(0, 0, argf);
}
retry:
if (!next_argv()) return Qnil;
line = rb_io_gets(ARGF.current_file);
if (NIL_P(line) && ARGF.next_p != -1) {
- rb_io_close(ARGF.current_file);
- ARGF.next_p = 1;
- goto retry;
+ rb_io_close(ARGF.current_file);
+ ARGF.next_p = 1;
+ goto retry;
}
rb_lastline_set(line);
if (!NIL_P(line)) {
- ARGF.lineno++;
- ARGF.last_lineno = ARGF.lineno;
+ ARGF.lineno++;
+ ARGF.last_lineno = ARGF.lineno;
}
return line;
@@ -8016,21 +10481,28 @@ static VALUE argf_readline(int, VALUE *, VALUE);
/*
* call-seq:
- * readline(sep=$/) -> string
- * readline(limit) -> string
- * readline(sep, limit) -> string
+ * readline(sep = $/, chomp: false) -> string
+ * readline(limit, chomp: false) -> string
+ * readline(sep, limit, chomp: false) -> string
*
- * Equivalent to <code>Kernel::gets</code>, except
- * +readline+ raises +EOFError+ at end of file.
+ * Equivalent to method Kernel#gets, except that it raises an exception
+ * if called at end-of-stream:
+ *
+ * $ cat t.txt | ruby -e "p readlines; readline"
+ * ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
+ * in `readline': end of file reached (EOFError)
+ *
+ * Optional keyword argument +chomp+ specifies whether line separators
+ * are to be omitted.
*/
static VALUE
rb_f_readline(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
- return argf_readline(argc, argv, argf);
+ return argf_readline(argc, argv, argf);
}
- return rb_funcall2(argf, rb_intern("readline"), argc, argv);
+ return forward(argf, rb_intern("readline"), argc, argv);
}
@@ -8040,15 +10512,16 @@ rb_f_readline(int argc, VALUE *argv, VALUE recv)
* ARGF.readline(limit) -> string
* ARGF.readline(sep, limit) -> string
*
- * Returns the next line from the current file in +ARGF+.
+ * Returns the next line from the current file in ARGF.
*
- * By default lines are assumed to be separated by +$/+; to use a different
- * character as a separator, supply it as a +String+ for the _sep_ argument.
+ * By default lines are assumed to be separated by <code>$/</code>;
+ * to use a different character as a separator, supply it as a String
+ * for the _sep_ argument.
*
- * The optional _limit_ argument specifies how many characters of each line
+ * The optional _limit_ argument specifies how many characters of each line
* to return. By default all characters are returned.
*
- * An +EOFError+ is raised at the end of the file.
+ * An EOFError is raised at the end of the file.
*/
static VALUE
argf_readline(int argc, VALUE *argv, VALUE argf)
@@ -8059,7 +10532,7 @@ argf_readline(int argc, VALUE *argv, VALUE argf)
ARGF_FORWARD(argc, argv);
line = argf_gets(argc, argv, argf);
if (NIL_P(line)) {
- rb_eof_error();
+ rb_eof_error();
}
return line;
@@ -8069,38 +10542,89 @@ static VALUE argf_readlines(int, VALUE *, VALUE);
/*
* call-seq:
- * readlines(sep=$/) -> array
- * readlines(limit) -> array
- * readlines(sep,limit) -> array
+ * readlines(sep = $/, chomp: false, **enc_opts) -> array
+ * readlines(limit, chomp: false, **enc_opts) -> array
+ * readlines(sep, limit, chomp: false, **enc_opts) -> array
*
* Returns an array containing the lines returned by calling
- * <code>Kernel.gets(<i>sep</i>)</code> until the end of file.
+ * Kernel#gets until the end-of-stream is reached;
+ * (see {Line IO}[rdoc-ref:IO@Line+IO]).
+ *
+ * With only string argument +sep+ given,
+ * returns the remaining lines as determined by line separator +sep+,
+ * or +nil+ if none;
+ * see {Line Separator}[rdoc-ref:IO@Line+Separator]:
+ *
+ * # Default separator.
+ * $ cat t.txt | ruby -e "p readlines"
+ * ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
+ *
+ * # Specified separator.
+ * $ cat t.txt | ruby -e "p readlines 'li'"
+ * ["First li", "ne\nSecond li", "ne\n\nFourth li", "ne\nFifth li", "ne\n"]
+ *
+ * # Get-all separator.
+ * $ cat t.txt | ruby -e "p readlines nil"
+ * ["First line\nSecond line\n\nFourth line\nFifth line\n"]
+ *
+ * # Get-paragraph separator.
+ * $ cat t.txt | ruby -e "p readlines ''"
+ * ["First line\nSecond line\n\n", "Fourth line\nFifth line\n"]
+ *
+ * With only integer argument +limit+ given,
+ * limits the number of bytes in the line;
+ * see {Line Limit}[rdoc-ref:IO@Line+Limit]:
+ *
+ * $cat t.txt | ruby -e "p readlines 10"
+ * ["First line", "\n", "Second lin", "e\n", "\n", "Fourth lin", "e\n", "Fifth line", "\n"]
+ *
+ * $cat t.txt | ruby -e "p readlines 11"
+ * ["First line\n", "Second line", "\n", "\n", "Fourth line", "\n", "Fifth line\n"]
+ *
+ * $cat t.txt | ruby -e "p readlines 12"
+ * ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
+ *
+ * With arguments +sep+ and +limit+ given,
+ * combines the two behaviors
+ * (see {Line Separator and Line Limit}[rdoc-ref:IO@Line+Separator+and+Line+Limit]).
+ *
+ * Optional keyword argument +chomp+ specifies whether line separators
+ * are to be omitted:
+ *
+ * $ cat t.txt | ruby -e "p readlines(chomp: true)"
+ * ["First line", "Second line", "", "Fourth line", "Fifth line"]
+ *
+ * Optional keyword arguments +enc_opts+ specify encoding options;
+ * see {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
+ *
*/
static VALUE
rb_f_readlines(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
- return argf_readlines(argc, argv, argf);
+ return argf_readlines(argc, argv, argf);
}
- return rb_funcall2(argf, rb_intern("readlines"), argc, argv);
+ return forward(argf, rb_intern("readlines"), argc, argv);
}
/*
* call-seq:
- * ARGF.readlines(sep=$/) -> array
- * ARGF.readlines(limit) -> array
- * ARGF.readlines(sep, limit) -> array
+ * ARGF.readlines(sep = $/, chomp: false) -> array
+ * ARGF.readlines(limit, chomp: false) -> array
+ * ARGF.readlines(sep, limit, chomp: false) -> array
*
- * ARGF.to_a(sep=$/) -> array
- * ARGF.to_a(limit) -> array
- * ARGF.to_a(sep, limit) -> array
+ * ARGF.to_a(sep = $/, chomp: false) -> array
+ * ARGF.to_a(limit, chomp: false) -> array
+ * ARGF.to_a(sep, limit, chomp: false) -> array
*
- * Reads +ARGF+'s current file in its entirety, returning an +Array+ of its
- * lines, one line per element. Lines are assumed to be separated by _sep_.
+ * Reads each file in ARGF in its entirety, returning an Array containing
+ * lines from the files. Lines are assumed to be separated by _sep_.
*
* lines = ARGF.readlines
* lines[0] #=> "This is line one\n"
+ *
+ * See +IO.readlines+ for a full description of all options.
*/
static VALUE
argf_readlines(int argc, VALUE *argv, VALUE argf)
@@ -8110,17 +10634,17 @@ argf_readlines(int argc, VALUE *argv, VALUE argf)
ary = rb_ary_new();
while (next_argv()) {
- if (ARGF_GENERIC_INPUT_P()) {
- lines = rb_funcall3(ARGF.current_file, rb_intern("readlines"), argc, argv);
- }
- else {
- lines = rb_io_readlines(argc, argv, ARGF.current_file);
- argf_close(argf);
- }
- ARGF.next_p = 1;
- rb_ary_concat(ary, lines);
- ARGF.lineno = lineno + RARRAY_LEN(ary);
- ARGF.last_lineno = ARGF.lineno;
+ if (ARGF_GENERIC_INPUT_P()) {
+ lines = forward_current(rb_intern("readlines"), argc, argv);
+ }
+ else {
+ lines = rb_io_readlines(argc, argv, ARGF.current_file);
+ argf_close(argf);
+ }
+ ARGF.next_p = 1;
+ rb_ary_concat(ary, lines);
+ ARGF.lineno = lineno + RARRAY_LEN(ary);
+ ARGF.last_lineno = ARGF.lineno;
}
ARGF.init_p = 0;
return ary;
@@ -8128,26 +10652,33 @@ argf_readlines(int argc, VALUE *argv, VALUE argf)
/*
* call-seq:
- * `cmd` -> string
+ * `command` -> string
+ *
+ * Returns the <tt>$stdout</tt> output from running +command+ in a subshell;
+ * sets global variable <tt>$?</tt> to the process status.
*
- * Returns the standard output of running _cmd_ in a subshell.
- * The built-in syntax <code>%x{...}</code> uses
- * this method. Sets <code>$?</code> to the process status.
+ * This method has potential security vulnerabilities if called with untrusted input;
+ * see {Command Injection}[rdoc-ref:security/command_injection.rdoc].
+ *
+ * Examples:
+ *
+ * $ `date` # => "Wed Apr 9 08:56:30 CDT 2003\n"
+ * $ `echo oops && exit 99` # => "oops\n"
+ * $ $? # => #<Process::Status: pid 17088 exit 99>
+ * $ $?.exitstatus # => 99
+ *
+ * The built-in syntax <tt>%x{...}</tt> uses this method.
*
- * `date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
- * `ls testdir`.split[1] #=> "main.rb"
- * `echo oops && exit 99` #=> "oops\n"
- * $?.exitstatus #=> 99
*/
static VALUE
rb_f_backquote(VALUE obj, VALUE str)
{
- volatile VALUE port;
+ VALUE port;
VALUE result;
rb_io_t *fptr;
- SafeStringValue(str);
+ StringValue(str);
rb_last_status_clear();
port = pipe_open_s(str, "r", FMODE_READABLE|DEFAULT_TEXTMODE, NULL);
if (NIL_P(port)) return rb_str_new(0,0);
@@ -8155,6 +10686,8 @@ rb_f_backquote(VALUE obj, VALUE str)
GetOpenFile(port, fptr);
result = read_all(fptr, remain_size(fptr), Qnil);
rb_io_close(port);
+ rb_io_fptr_cleanup_all(fptr);
+ RB_GC_GUARD(port);
return result;
}
@@ -8175,114 +10708,114 @@ select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fd
struct timeval timerec;
if (!NIL_P(read)) {
- Check_Type(read, T_ARRAY);
- for (i=0; i<RARRAY_LEN(read); i++) {
- GetOpenFile(rb_io_get_io(RARRAY_AREF(read, i)), fptr);
- rb_fd_set(fptr->fd, &fds[0]);
- if (READ_DATA_PENDING(fptr) || READ_CHAR_PENDING(fptr)) { /* check for buffered data */
- pending++;
- rb_fd_set(fptr->fd, &fds[3]);
- }
- if (max < fptr->fd) max = fptr->fd;
- }
- if (pending) { /* no blocking if there's buffered data */
- timerec.tv_sec = timerec.tv_usec = 0;
- tp = &timerec;
- }
- rp = &fds[0];
+ Check_Type(read, T_ARRAY);
+ for (i=0; i<RARRAY_LEN(read); i++) {
+ GetOpenFile(rb_io_get_io(RARRAY_AREF(read, i)), fptr);
+ rb_fd_set(fptr->fd, &fds[0]);
+ if (READ_DATA_PENDING(fptr) || READ_CHAR_PENDING(fptr)) { /* check for buffered data */
+ pending++;
+ rb_fd_set(fptr->fd, &fds[3]);
+ }
+ if (max < fptr->fd) max = fptr->fd;
+ }
+ if (pending) { /* no blocking if there's buffered data */
+ timerec.tv_sec = timerec.tv_usec = 0;
+ tp = &timerec;
+ }
+ rp = &fds[0];
}
else
- rp = 0;
+ rp = 0;
if (!NIL_P(write)) {
- Check_Type(write, T_ARRAY);
- for (i=0; i<RARRAY_LEN(write); i++) {
+ Check_Type(write, T_ARRAY);
+ for (i=0; i<RARRAY_LEN(write); i++) {
VALUE write_io = GetWriteIO(rb_io_get_io(RARRAY_AREF(write, i)));
- GetOpenFile(write_io, fptr);
- rb_fd_set(fptr->fd, &fds[1]);
- if (max < fptr->fd) max = fptr->fd;
- }
- wp = &fds[1];
+ GetOpenFile(write_io, fptr);
+ rb_fd_set(fptr->fd, &fds[1]);
+ if (max < fptr->fd) max = fptr->fd;
+ }
+ wp = &fds[1];
}
else
- wp = 0;
+ wp = 0;
if (!NIL_P(except)) {
- Check_Type(except, T_ARRAY);
- for (i=0; i<RARRAY_LEN(except); i++) {
+ Check_Type(except, T_ARRAY);
+ for (i=0; i<RARRAY_LEN(except); i++) {
VALUE io = rb_io_get_io(RARRAY_AREF(except, i));
VALUE write_io = GetWriteIO(io);
- GetOpenFile(io, fptr);
- rb_fd_set(fptr->fd, &fds[2]);
- if (max < fptr->fd) max = fptr->fd;
+ GetOpenFile(io, fptr);
+ rb_fd_set(fptr->fd, &fds[2]);
+ if (max < fptr->fd) max = fptr->fd;
if (io != write_io) {
GetOpenFile(write_io, fptr);
rb_fd_set(fptr->fd, &fds[2]);
if (max < fptr->fd) max = fptr->fd;
}
- }
- ep = &fds[2];
+ }
+ ep = &fds[2];
}
else {
- ep = 0;
+ ep = 0;
}
max++;
n = rb_thread_fd_select(max, rp, wp, ep, tp);
if (n < 0) {
- rb_sys_fail(0);
+ rb_sys_fail(0);
}
if (!pending && n == 0) return Qnil; /* returns nil on timeout */
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));
+ rb_ary_push(res, rp ? rb_ary_new_capa(RARRAY_LEN(read)) : rb_ary_new());
+ rb_ary_push(res, wp ? rb_ary_new_capa(RARRAY_LEN(write)) : rb_ary_new());
+ rb_ary_push(res, ep ? rb_ary_new_capa(RARRAY_LEN(except)) : rb_ary_new());
if (rp) {
- list = RARRAY_AREF(res, 0);
- for (i=0; i< RARRAY_LEN(read); i++) {
- VALUE obj = rb_ary_entry(read, i);
- VALUE io = rb_io_get_io(obj);
- GetOpenFile(io, fptr);
- if (rb_fd_isset(fptr->fd, &fds[0]) ||
- rb_fd_isset(fptr->fd, &fds[3])) {
- rb_ary_push(list, obj);
- }
- }
+ list = RARRAY_AREF(res, 0);
+ for (i=0; i< RARRAY_LEN(read); i++) {
+ VALUE obj = rb_ary_entry(read, i);
+ VALUE io = rb_io_get_io(obj);
+ GetOpenFile(io, fptr);
+ if (rb_fd_isset(fptr->fd, &fds[0]) ||
+ rb_fd_isset(fptr->fd, &fds[3])) {
+ rb_ary_push(list, obj);
+ }
+ }
}
if (wp) {
- list = RARRAY_AREF(res, 1);
- for (i=0; i< RARRAY_LEN(write); i++) {
- VALUE obj = rb_ary_entry(write, i);
- VALUE io = rb_io_get_io(obj);
- VALUE write_io = GetWriteIO(io);
- GetOpenFile(write_io, fptr);
- if (rb_fd_isset(fptr->fd, &fds[1])) {
- rb_ary_push(list, obj);
- }
- }
+ list = RARRAY_AREF(res, 1);
+ for (i=0; i< RARRAY_LEN(write); i++) {
+ VALUE obj = rb_ary_entry(write, i);
+ VALUE io = rb_io_get_io(obj);
+ VALUE write_io = GetWriteIO(io);
+ GetOpenFile(write_io, fptr);
+ if (rb_fd_isset(fptr->fd, &fds[1])) {
+ rb_ary_push(list, obj);
+ }
+ }
}
if (ep) {
- list = RARRAY_AREF(res, 2);
- for (i=0; i< RARRAY_LEN(except); i++) {
- VALUE obj = rb_ary_entry(except, i);
- VALUE io = rb_io_get_io(obj);
- VALUE write_io = GetWriteIO(io);
- GetOpenFile(io, fptr);
- if (rb_fd_isset(fptr->fd, &fds[2])) {
- rb_ary_push(list, obj);
- }
- else if (io != write_io) {
- GetOpenFile(write_io, fptr);
- if (rb_fd_isset(fptr->fd, &fds[2])) {
- rb_ary_push(list, obj);
- }
- }
- }
+ list = RARRAY_AREF(res, 2);
+ for (i=0; i< RARRAY_LEN(except); i++) {
+ VALUE obj = rb_ary_entry(except, i);
+ VALUE io = rb_io_get_io(obj);
+ VALUE write_io = GetWriteIO(io);
+ GetOpenFile(io, fptr);
+ if (rb_fd_isset(fptr->fd, &fds[2])) {
+ rb_ary_push(list, obj);
+ }
+ else if (io != write_io) {
+ GetOpenFile(write_io, fptr);
+ if (rb_fd_isset(fptr->fd, &fds[2])) {
+ rb_ary_push(list, obj);
+ }
+ }
+ }
}
return res; /* returns an empty array on interrupt */
@@ -8309,7 +10842,7 @@ select_end(VALUE arg)
int i;
for (i = 0; i < numberof(p->fdsets); ++i)
- rb_fd_term(&p->fdsets[i]);
+ rb_fd_term(&p->fdsets[i]);
return Qnil;
}
@@ -8319,9 +10852,9 @@ static VALUE sym_normal, sym_sequential, sym_random,
#ifdef HAVE_POSIX_FADVISE
struct io_advise_struct {
int fd;
- off_t offset;
- off_t len;
int advice;
+ rb_off_t offset;
+ rb_off_t len;
};
static VALUE
@@ -8336,39 +10869,39 @@ io_advise_sym_to_const(VALUE sym)
{
#ifdef POSIX_FADV_NORMAL
if (sym == sym_normal)
- return INT2NUM(POSIX_FADV_NORMAL);
+ return INT2NUM(POSIX_FADV_NORMAL);
#endif
#ifdef POSIX_FADV_RANDOM
if (sym == sym_random)
- return INT2NUM(POSIX_FADV_RANDOM);
+ return INT2NUM(POSIX_FADV_RANDOM);
#endif
#ifdef POSIX_FADV_SEQUENTIAL
if (sym == sym_sequential)
- return INT2NUM(POSIX_FADV_SEQUENTIAL);
+ return INT2NUM(POSIX_FADV_SEQUENTIAL);
#endif
#ifdef POSIX_FADV_WILLNEED
if (sym == sym_willneed)
- return INT2NUM(POSIX_FADV_WILLNEED);
+ return INT2NUM(POSIX_FADV_WILLNEED);
#endif
#ifdef POSIX_FADV_DONTNEED
if (sym == sym_dontneed)
- return INT2NUM(POSIX_FADV_DONTNEED);
+ return INT2NUM(POSIX_FADV_DONTNEED);
#endif
#ifdef POSIX_FADV_NOREUSE
if (sym == sym_noreuse)
- return INT2NUM(POSIX_FADV_NOREUSE);
+ return INT2NUM(POSIX_FADV_NOREUSE);
#endif
return Qnil;
}
static VALUE
-do_io_advise(rb_io_t *fptr, VALUE advice, off_t offset, off_t len)
+do_io_advise(rb_io_t *fptr, VALUE advice, rb_off_t offset, rb_off_t len)
{
int rv;
struct io_advise_struct ias;
@@ -8381,18 +10914,23 @@ do_io_advise(rb_io_t *fptr, VALUE advice, off_t offset, off_t len)
* silently ignore it. Because IO::advise is only hint.
*/
if (NIL_P(num_adv))
- return Qnil;
+ return Qnil;
ias.fd = fptr->fd;
ias.advice = NUM2INT(num_adv);
ias.offset = offset;
ias.len = len;
- rv = (int)rb_thread_io_blocking_region(io_advise_internal, &ias, fptr->fd);
- if (rv) {
- /* posix_fadvise(2) doesn't set errno. On success it returns 0; otherwise
- it returns the error code. */
- rb_syserr_fail_str(rv, fptr->pathv);
+ rv = (int)rb_io_blocking_region(fptr, io_advise_internal, &ias);
+ if (rv && rv != ENOSYS) {
+ /* posix_fadvise(2) doesn't set errno. On success it returns 0; otherwise
+ it returns the error code. */
+ VALUE message = rb_sprintf("%"PRIsVALUE" "
+ "(%"PRI_OFFT_PREFIX"d, "
+ "%"PRI_OFFT_PREFIX"d, "
+ "%"PRIsVALUE")",
+ fptr->pathv, offset, len, advice);
+ rb_syserr_fail_str(rv, message);
}
return Qnil;
@@ -8404,67 +10942,56 @@ static void
advice_arg_check(VALUE advice)
{
if (!SYMBOL_P(advice))
- rb_raise(rb_eTypeError, "advice must be a Symbol");
+ rb_raise(rb_eTypeError, "advice must be a Symbol");
if (advice != sym_normal &&
- advice != sym_sequential &&
- advice != sym_random &&
- advice != sym_willneed &&
- advice != sym_dontneed &&
- advice != sym_noreuse) {
- VALUE symname = rb_inspect(advice);
- rb_raise(rb_eNotImpError, "Unsupported advice: %s",
- StringValuePtr(symname));
+ advice != sym_sequential &&
+ advice != sym_random &&
+ advice != sym_willneed &&
+ advice != sym_dontneed &&
+ advice != sym_noreuse) {
+ rb_raise(rb_eNotImpError, "Unsupported advice: %+"PRIsVALUE, advice);
}
}
/*
* call-seq:
- * ios.advise(advice, offset=0, len=0) -> nil
+ * advise(advice, offset = 0, len = 0) -> nil
*
- * Announce an intention to access data from the current file in a
- * specific pattern. On platforms that do not support the
- * <em>posix_fadvise(2)</em> system call, this method is a no-op.
+ * Invokes Posix system call
+ * {posix_fadvise(2)}[https://man7.org/linux/man-pages/man2/posix_fadvise.2.html],
+ * which announces an intention to access data from the current file
+ * in a particular manner.
*
- * _advice_ is one of the following symbols:
+ * The arguments and results are platform-dependent.
*
- * :normal:: No advice to give; the default assumption for an open file.
- * :sequential:: The data will be accessed sequentially
- * with lower offsets read before higher ones.
- * :random:: The data will be accessed in random order.
- * :willneed:: The data will be accessed in the near future.
- * :dontneed:: The data will not be accessed in the near future.
- * :noreuse:: The data will only be accessed once.
+ * The relevant data is specified by:
*
- * The semantics of a piece of advice are platform-dependent. See
- * <em>man 2 posix_fadvise</em> for details.
+ * - +offset+: The offset of the first byte of data.
+ * - +len+: The number of bytes to be accessed;
+ * if +len+ is zero, or is larger than the number of bytes remaining,
+ * all remaining bytes will be accessed.
*
- * "data" means the region of the current file that begins at
- * _offset_ and extends for _len_ bytes. If _len_ is 0, the region
- * ends at the last byte of the file. By default, both _offset_ and
- * _len_ are 0, meaning that the advice applies to the entire file.
+ * Argument +advice+ is one of the following symbols:
*
- * If an error occurs, one of the following exceptions will be raised:
+ * - +:normal+: The application has no advice to give
+ * about its access pattern for the specified data.
+ * If no advice is given for an open file, this is the default assumption.
+ * - +:sequential+: The application expects to access the specified data sequentially
+ * (with lower offsets read before higher ones).
+ * - +:random+: The specified data will be accessed in random order.
+ * - +:noreuse+: The specified data will be accessed only once.
+ * - +:willneed+: The specified data will be accessed in the near future.
+ * - +:dontneed+: The specified data will not be accessed in the near future.
*
- * <code>IOError</code>:: The <code>IO</code> stream is closed.
- * <code>Errno::EBADF</code>::
- * The file descriptor of the current file is invalid.
- * <code>Errno::EINVAL</code>:: An invalid value for _advice_ was given.
- * <code>Errno::ESPIPE</code>::
- * The file descriptor of the current file refers to a FIFO or
- * pipe. (Linux raises <code>Errno::EINVAL</code> in this case).
- * <code>TypeError</code>::
- * Either _advice_ was not a Symbol, or one of the
- * other arguments was not an <code>Integer</code>.
- * <code>RangeError</code>:: One of the arguments given was too big/small.
+ * Not implemented on all platforms.
*
- * This list is not exhaustive; other Errno:: exceptions are also possible.
*/
static VALUE
rb_io_advise(int argc, VALUE *argv, VALUE io)
{
VALUE advice, offset, len;
- off_t off, l;
+ rb_off_t off, l;
rb_io_t *fptr;
rb_scan_args(argc, argv, "12", &advice, &offset, &len);
@@ -8484,37 +11011,60 @@ rb_io_advise(int argc, VALUE *argv, VALUE io)
#endif
}
+static int
+is_pos_inf(VALUE x)
+{
+ double f;
+ if (!RB_FLOAT_TYPE_P(x))
+ return 0;
+ f = RFLOAT_VALUE(x);
+ return isinf(f) && 0 < f;
+}
+
/*
* call-seq:
- * IO.select(read_array
- * [, write_array
- * [, error_array
- * [, timeout]]]) -> array or nil
+ * IO.select(read_ios, write_ios = [], error_ios = [], timeout = nil) -> array or nil
+ *
+ * Invokes system call {select(2)}[https://man7.org/linux/man-pages/man2/select.2.html],
+ * which monitors multiple file descriptors,
+ * waiting until one or more of the file descriptors
+ * becomes ready for some class of I/O operation.
+ *
+ * Not implemented on all platforms.
+ *
+ * Each of the arguments +read_ios+, +write_ios+, and +error_ios+
+ * is an array of IO objects.
+ *
+ * Argument +timeout+ is a numeric value (such as integer or float) timeout
+ * interval in seconds.
+ * +timeout+ can also be +nil+ or +Float::INFINITY+.
+ * +nil+ and +Float::INFINITY+ means no timeout.
*
- * Calls select(2) system call.
- * It monitors given arrays of <code>IO</code> objects, waits one or more
- * of <code>IO</code> objects ready for reading, are ready for writing,
- * and have pending exceptions respectively, and returns an array that
- * contains arrays of those IO objects. It will return <code>nil</code>
- * if optional <i>timeout</i> value is given and no <code>IO</code> object
- * is ready in <i>timeout</i> seconds.
+ * The method monitors the \IO objects given in all three arrays,
+ * waiting for some to be ready;
+ * returns a 3-element array whose elements are:
*
- * <code>IO.select</code> peeks the buffer of <code>IO</code> objects for testing readability.
- * If the <code>IO</code> buffer is not empty,
- * <code>IO.select</code> immediately notify readability.
- * This "peek" is only happen for <code>IO</code> objects.
- * It is not happen for IO-like objects such as OpenSSL::SSL::SSLSocket.
+ * - An array of the objects in +read_ios+ that are ready for reading.
+ * - An array of the objects in +write_ios+ that are ready for writing.
+ * - An array of the objects in +error_ios+ have pending exceptions.
*
- * The best way to use <code>IO.select</code> is invoking it
- * after nonblocking methods such as <code>read_nonblock</code>, <code>write_nonblock</code>, etc.
- * The methods raises an exception which is extended by
- * <code>IO::WaitReadable</code> or <code>IO::WaitWritable</code>.
- * The modules notify how the caller should wait with <code>IO.select</code>.
- * If <code>IO::WaitReadable</code> is raised, the caller should wait for reading.
- * If <code>IO::WaitWritable</code> is raised, the caller should wait for writing.
+ * If no object becomes ready within the given +timeout+, +nil+ is returned.
*
- * So, blocking read (<code>readpartial</code>) can be emulated using
- * <code>read_nonblock</code> and <code>IO.select</code> as follows:
+ * \IO.select peeks the buffer of \IO objects for testing readability.
+ * If the \IO buffer is not empty, \IO.select immediately notifies
+ * readability. This "peek" only happens for \IO objects. It does not
+ * happen for IO-like objects such as OpenSSL::SSL::SSLSocket.
+ *
+ * The best way to use \IO.select is invoking it after non-blocking
+ * methods such as #read_nonblock, #write_nonblock, etc. The methods
+ * raise an exception which is extended by IO::WaitReadable or
+ * IO::WaitWritable. The modules notify how the caller should wait
+ * with \IO.select. If IO::WaitReadable is raised, the caller should
+ * wait for reading. If IO::WaitWritable is raised, the caller should
+ * wait for writing.
+ *
+ * So, blocking read (#readpartial) can be emulated using
+ * #read_nonblock and \IO.select as follows:
*
* begin
* result = io_like.read_nonblock(maxlen)
@@ -8526,57 +11076,57 @@ rb_io_advise(int argc, VALUE *argv, VALUE io)
* retry
* end
*
- * Especially, the combination of nonblocking methods and
- * <code>IO.select</code> is preferred for <code>IO</code> like
- * objects such as <code>OpenSSL::SSL::SSLSocket</code>.
- * It has <code>to_io</code> method to return underlying <code>IO</code> object.
- * <code>IO.select</code> calls <code>to_io</code> to obtain the file descriptor to wait.
+ * Especially, the combination of non-blocking methods and \IO.select is
+ * preferred for IO like objects such as OpenSSL::SSL::SSLSocket. It
+ * has #to_io method to return underlying IO object. IO.select calls
+ * #to_io to obtain the file descriptor to wait.
*
- * This means that readability notified by <code>IO.select</code> doesn't mean
- * readability from <code>OpenSSL::SSL::SSLSocket</code> object.
+ * This means that readability notified by \IO.select doesn't mean
+ * readability from OpenSSL::SSL::SSLSocket object.
*
- * Most possible situation is <code>OpenSSL::SSL::SSLSocket</code> buffers some data.
- * <code>IO.select</code> doesn't see the buffer.
- * So <code>IO.select</code> can block when <code>OpenSSL::SSL::SSLSocket#readpartial</code> doesn't block.
+ * The most likely situation is that OpenSSL::SSL::SSLSocket buffers
+ * some data. \IO.select doesn't see the buffer. So \IO.select can
+ * block when OpenSSL::SSL::SSLSocket#readpartial doesn't block.
*
- * However several more complicated situation exists.
+ * However, several more complicated situations exist.
*
* SSL is a protocol which is sequence of records.
- * The record consists multiple bytes.
- * So, the remote side of SSL sends a partial record,
- * <code>IO.select</code> notifies readability but
- * <code>OpenSSL::SSL::SSLSocket</code> cannot decrypt a byte and
- * <code>OpenSSL::SSL::SSLSocket#readpartial</code> will blocks.
+ * The record consists of multiple bytes.
+ * So, the remote side of SSL sends a partial record, IO.select
+ * notifies readability but OpenSSL::SSL::SSLSocket cannot decrypt a
+ * byte and OpenSSL::SSL::SSLSocket#readpartial will block.
*
* Also, the remote side can request SSL renegotiation which forces
- * the local SSL engine writes some data.
- * This means <code>OpenSSL::SSL::SSLSocket#readpartial</code> may
- * invoke <code>write</code> system call and it can block.
- * In such situation, <code>OpenSSL::SSL::SSLSocket#read_nonblock</code>
- * raises IO::WaitWritable instead of blocking.
- * So, the caller should wait for ready for writability as above example.
- *
- * The combination of nonblocking methods and <code>IO.select</code> is
- * also useful for streams such as tty, pipe socket socket when
- * multiple process read form a stream.
- *
- * Finally, Linux kernel developers doesn't guarantee that
+ * the local SSL engine to write some data.
+ * This means OpenSSL::SSL::SSLSocket#readpartial may invoke #write
+ * system call and it can block.
+ * In such a situation, OpenSSL::SSL::SSLSocket#read_nonblock raises
+ * IO::WaitWritable instead of blocking.
+ * So, the caller should wait for ready for writability as above
+ * example.
+ *
+ * The combination of non-blocking methods and \IO.select is also useful
+ * for streams such as tty, pipe socket socket when multiple processes
+ * read from a stream.
+ *
+ * Finally, Linux kernel developers don't guarantee that
* readability of select(2) means readability of following read(2) even
- * for single process.
- * See select(2) manual on GNU/Linux system.
+ * for a single process;
+ * see {select(2)}[https://man7.org/linux/man-pages/man2/select.2.html]
*
- * Invoking <code>IO.select</code> before <code>IO#readpartial</code> works well in usual.
- * However it is not the best way to use <code>IO.select</code>.
+ * Invoking \IO.select before IO#readpartial works well as usual.
+ * However it is not the best way to use \IO.select.
*
* The writability notified by select(2) doesn't show
- * how many bytes writable.
- * <code>IO#write</code> method blocks until given whole string is written.
- * So, <code>IO#write(two or more bytes)</code> can block after writability is notified by <code>IO.select</code>.
- * <code>IO#write_nonblock</code> is required to avoid the blocking.
+ * how many bytes are writable.
+ * IO#write method blocks until given whole string is written.
+ * So, <tt>IO#write(two or more bytes)</tt> can block after
+ * writability is notified by \IO.select. IO#write_nonblock is required
+ * to avoid the blocking.
*
- * Blocking write (<code>write</code>) can be emulated using
- * <code>write_nonblock</code> and <code>IO.select</code> as follows:
- * IO::WaitReadable should also be rescued for SSL renegotiation in <code>OpenSSL::SSL::SSLSocket</code>.
+ * Blocking write (#write) can be emulated using #write_nonblock and
+ * IO.select as follows: IO::WaitReadable should also be rescued for
+ * SSL renegotiation in OpenSSL::SSL::SSLSocket.
*
* while 0 < string.bytesize
* begin
@@ -8591,13 +11141,7 @@ rb_io_advise(int argc, VALUE *argv, VALUE io)
* string = string.byteslice(written..-1)
* end
*
- * === Parameters
- * read_array:: an array of <code>IO</code> objects that wait until ready for read
- * write_array:: an array of <code>IO</code> objects that wait until ready for write
- * error_array:: an array of <code>IO</code> objects that wait for exceptions
- * timeout:: a numeric value in second
- *
- * === Example
+ * Example:
*
* rp, wp = IO.pipe
* mesg = "ping "
@@ -8619,46 +11163,54 @@ rb_io_advise(int argc, VALUE *argv, VALUE io)
* end
* }
*
- * <em>produces:</em>
+ * Output:
*
* ping pong
* ping pong
* ping pong
* (snipped)
* ping
+ *
*/
static VALUE
rb_f_select(int argc, VALUE *argv, VALUE obj)
{
+ VALUE scheduler = rb_fiber_scheduler_current();
+ if (scheduler != Qnil) {
+ // It's optionally supported.
+ VALUE result = rb_fiber_scheduler_io_selectv(scheduler, argc, argv);
+ if (!UNDEF_P(result)) return result;
+ }
+
VALUE timeout;
struct select_args args;
struct timeval timerec;
int i;
rb_scan_args(argc, argv, "13", &args.read, &args.write, &args.except, &timeout);
- if (NIL_P(timeout)) {
- args.timeout = 0;
+ if (NIL_P(timeout) || is_pos_inf(timeout)) {
+ args.timeout = 0;
}
else {
- timerec = rb_time_interval(timeout);
- args.timeout = &timerec;
+ timerec = rb_time_interval(timeout);
+ args.timeout = &timerec;
}
for (i = 0; i < numberof(args.fdsets); ++i)
- rb_fd_init(&args.fdsets[i]);
+ rb_fd_init(&args.fdsets[i]);
return rb_ensure(select_call, (VALUE)&args, select_end, (VALUE)&args);
}
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
- typedef unsigned long ioctl_req_t;
-# define NUM2IOCTLREQ(num) NUM2ULONG(num)
+#ifdef IOCTL_REQ_TYPE
+ typedef IOCTL_REQ_TYPE ioctl_req_t;
#else
typedef int ioctl_req_t;
-# define NUM2IOCTLREQ(num) NUM2INT(num)
+# define NUM2IOCTLREQ(num) ((int)NUM2LONG(num))
#endif
+#ifdef HAVE_IOCTL
struct ioctl_arg {
int fd;
ioctl_req_t cmd;
@@ -8674,21 +11226,22 @@ nogvl_ioctl(void *ptr)
}
static int
-do_ioctl(int fd, ioctl_req_t cmd, long narg)
+do_ioctl(struct rb_io *io, ioctl_req_t cmd, long narg)
{
int retval;
struct ioctl_arg arg;
- arg.fd = fd;
+ arg.fd = io->fd;
arg.cmd = cmd;
arg.narg = narg;
- retval = (int)rb_thread_io_blocking_region(nogvl_ioctl, &arg, fd);
+ retval = (int)rb_io_blocking_region(io, nogvl_ioctl, &arg);
return retval;
}
+#endif
-#define DEFULT_IOCTL_NARG_LEN (256)
+#define DEFAULT_IOCTL_NARG_LEN (256)
#if defined(__linux__) && defined(_IOC_SIZE)
static long
@@ -8697,20 +11250,21 @@ linux_iocparm_len(ioctl_req_t cmd)
long len;
if ((cmd & 0xFFFF0000) == 0) {
- /* legacy and unstructured ioctl number. */
- return DEFULT_IOCTL_NARG_LEN;
+ /* legacy and unstructured ioctl number. */
+ return DEFAULT_IOCTL_NARG_LEN;
}
len = _IOC_SIZE(cmd);
/* paranoia check for silly drivers which don't keep ioctl convention */
- if (len < DEFULT_IOCTL_NARG_LEN)
- len = DEFULT_IOCTL_NARG_LEN;
+ if (len < DEFAULT_IOCTL_NARG_LEN)
+ len = DEFAULT_IOCTL_NARG_LEN;
return len;
}
#endif
+#ifdef HAVE_IOCTL
static long
ioctl_narg_len(ioctl_req_t cmd)
{
@@ -8727,11 +11281,12 @@ ioctl_narg_len(ioctl_req_t cmd)
len = linux_iocparm_len(cmd);
#else
/* otherwise guess at what's safe */
- len = DEFULT_IOCTL_NARG_LEN;
+ len = DEFAULT_IOCTL_NARG_LEN;
#endif
return len;
}
+#endif
#ifdef HAVE_FCNTL
#ifdef __linux__
@@ -8742,177 +11297,198 @@ typedef int fcntl_arg_t;
#endif
static long
-fcntl_narg_len(int cmd)
+fcntl_narg_len(ioctl_req_t cmd)
{
long len;
switch (cmd) {
#ifdef F_DUPFD
case F_DUPFD:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
#ifdef F_DUP2FD /* bsd specific */
case F_DUP2FD:
- len = sizeof(int);
- break;
+ len = sizeof(int);
+ break;
#endif
#ifdef F_DUPFD_CLOEXEC /* linux specific */
case F_DUPFD_CLOEXEC:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
#ifdef F_GETFD
case F_GETFD:
- len = 1;
- break;
+ len = 1;
+ break;
#endif
#ifdef F_SETFD
case F_SETFD:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
#ifdef F_GETFL
case F_GETFL:
- len = 1;
- break;
+ len = 1;
+ break;
#endif
#ifdef F_SETFL
case F_SETFL:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
#ifdef F_GETOWN
case F_GETOWN:
- len = 1;
- break;
+ len = 1;
+ break;
#endif
#ifdef F_SETOWN
case F_SETOWN:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
#ifdef F_GETOWN_EX /* linux specific */
case F_GETOWN_EX:
- len = sizeof(struct f_owner_ex);
- break;
+ len = sizeof(struct f_owner_ex);
+ break;
#endif
#ifdef F_SETOWN_EX /* linux specific */
case F_SETOWN_EX:
- len = sizeof(struct f_owner_ex);
- break;
+ len = sizeof(struct f_owner_ex);
+ break;
#endif
#ifdef F_GETLK
case F_GETLK:
- len = sizeof(struct flock);
- break;
+ len = sizeof(struct flock);
+ break;
#endif
#ifdef F_SETLK
case F_SETLK:
- len = sizeof(struct flock);
- break;
+ len = sizeof(struct flock);
+ break;
#endif
#ifdef F_SETLKW
case F_SETLKW:
- len = sizeof(struct flock);
- break;
+ len = sizeof(struct flock);
+ break;
#endif
#ifdef F_READAHEAD /* bsd specific */
case F_READAHEAD:
- len = sizeof(int);
- break;
+ len = sizeof(int);
+ break;
#endif
#ifdef F_RDAHEAD /* Darwin specific */
case F_RDAHEAD:
- len = sizeof(int);
- break;
+ len = sizeof(int);
+ break;
#endif
#ifdef F_GETSIG /* linux specific */
case F_GETSIG:
- len = 1;
- break;
+ len = 1;
+ break;
#endif
#ifdef F_SETSIG /* linux specific */
case F_SETSIG:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
#ifdef F_GETLEASE /* linux specific */
case F_GETLEASE:
- len = 1;
- break;
+ len = 1;
+ break;
#endif
#ifdef F_SETLEASE /* linux specific */
case F_SETLEASE:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
#ifdef F_NOTIFY /* linux specific */
case F_NOTIFY:
- len = sizeof(fcntl_arg_t);
- break;
+ len = sizeof(fcntl_arg_t);
+ break;
#endif
default:
- len = 256;
- break;
+ len = 256;
+ break;
}
return len;
}
#else /* HAVE_FCNTL */
static long
-fcntl_narg_len(int cmd)
+fcntl_narg_len(ioctl_req_t cmd)
{
return 0;
}
#endif /* HAVE_FCNTL */
+#define NARG_SENTINEL 17
+
static long
-setup_narg(ioctl_req_t cmd, VALUE *argp, int io_p)
+setup_narg(ioctl_req_t cmd, VALUE *argp, long (*narg_len)(ioctl_req_t))
{
long narg = 0;
VALUE arg = *argp;
- if (NIL_P(arg) || arg == Qfalse) {
- narg = 0;
+ if (!RTEST(arg)) {
+ narg = 0;
}
else if (FIXNUM_P(arg)) {
- narg = FIX2LONG(arg);
+ narg = FIX2LONG(arg);
}
else if (arg == Qtrue) {
- narg = 1;
+ narg = 1;
}
else {
- VALUE tmp = rb_check_string_type(arg);
-
- if (NIL_P(tmp)) {
- narg = NUM2LONG(arg);
- }
- else {
- long len;
-
- *argp = arg = tmp;
- if (io_p)
- len = ioctl_narg_len(cmd);
- else
- len = fcntl_narg_len((int)cmd);
- rb_str_modify(arg);
-
- /* expand for data + sentinel. */
- if (RSTRING_LEN(arg) < len+1) {
- rb_str_resize(arg, len+1);
- }
- /* a little sanity check here */
- RSTRING_PTR(arg)[RSTRING_LEN(arg) - 1] = 17;
- narg = (long)(SIGNED_VALUE)RSTRING_PTR(arg);
- }
+ VALUE tmp = rb_check_string_type(arg);
+
+ if (NIL_P(tmp)) {
+ narg = NUM2LONG(arg);
+ }
+ else {
+ char *ptr;
+ long len, slen;
+
+ *argp = arg = tmp;
+ len = narg_len(cmd);
+ rb_str_modify(arg);
+
+ slen = RSTRING_LEN(arg);
+ /* expand for data + sentinel. */
+ if (slen < len+1) {
+ rb_str_resize(arg, len+1);
+ MEMZERO(RSTRING_PTR(arg)+slen, char, len-slen);
+ slen = len+1;
+ }
+ /* a little sanity check here */
+ ptr = RSTRING_PTR(arg);
+ ptr[slen - 1] = NARG_SENTINEL;
+ narg = (long)(SIGNED_VALUE)ptr;
+ }
}
return narg;
}
static VALUE
+finish_narg(int retval, VALUE arg, const rb_io_t *fptr)
+{
+ if (retval < 0) rb_sys_fail_path(fptr->pathv);
+ if (RB_TYPE_P(arg, T_STRING)) {
+ char *ptr;
+ long slen;
+ RSTRING_GETMEM(arg, ptr, slen);
+ if (ptr[slen-1] != NARG_SENTINEL)
+ rb_raise(rb_eArgError, "return value overflowed string");
+ ptr[slen-1] = '\0';
+ }
+
+ return INT2NUM(retval);
+}
+
+#ifdef HAVE_IOCTL
+static VALUE
rb_ioctl(VALUE io, VALUE req, VALUE arg)
{
ioctl_req_t cmd = NUM2IOCTLREQ(req);
@@ -8920,31 +11496,28 @@ rb_ioctl(VALUE io, VALUE req, VALUE arg)
long narg;
int retval;
- rb_secure(2);
-
- narg = setup_narg(cmd, &arg, 1);
+ narg = setup_narg(cmd, &arg, ioctl_narg_len);
GetOpenFile(io, fptr);
- retval = do_ioctl(fptr->fd, cmd, narg);
- if (retval < 0) rb_sys_fail_path(fptr->pathv);
- if (RB_TYPE_P(arg, T_STRING)) {
- if (RSTRING_PTR(arg)[RSTRING_LEN(arg)-1] != 17)
- rb_raise(rb_eArgError, "return value overflowed string");
- RSTRING_PTR(arg)[RSTRING_LEN(arg)-1] = '\0';
- }
-
- return INT2NUM(retval);
+ retval = do_ioctl(fptr, cmd, narg);
+ return finish_narg(retval, arg, fptr);
}
/*
* call-seq:
- * ios.ioctl(integer_cmd, arg) -> integer
+ * ioctl(integer_cmd, argument) -> integer
+ *
+ * Invokes Posix system call {ioctl(2)}[https://man7.org/linux/man-pages/man2/ioctl.2.html],
+ * which issues a low-level command to an I/O device.
+ *
+ * Issues a low-level command to an I/O device.
+ * The arguments and returned value are platform-dependent.
+ * The effect of the call is platform-dependent.
+ *
+ * If argument +argument+ is an integer, it is passed directly;
+ * if it is a string, it is interpreted as a binary sequence of bytes.
+ *
+ * Not implemented on all platforms.
*
- * Provides a mechanism for issuing low-level commands to control or
- * query I/O devices. Arguments and results are platform dependent. If
- * <i>arg</i> is a number, its value is passed directly. If it is a
- * string, it is interpreted as a binary sequence of bytes. On Unix
- * platforms, see <code>ioctl(2)</code> for details. Not implemented on
- * all platforms.
*/
static VALUE
@@ -8955,6 +11528,9 @@ rb_io_ioctl(int argc, VALUE *argv, VALUE io)
rb_scan_args(argc, argv, "11", &req, &arg);
return rb_ioctl(io, req, arg);
}
+#else
+#define rb_io_ioctl rb_f_notimplement
+#endif
#ifdef HAVE_FCNTL
struct fcntl_arg {
@@ -8970,27 +11546,33 @@ nogvl_fcntl(void *ptr)
#if defined(F_DUPFD)
if (arg->cmd == F_DUPFD)
- return (VALUE)rb_cloexec_fcntl_dupfd(arg->fd, (int)arg->narg);
+ return (VALUE)rb_cloexec_fcntl_dupfd(arg->fd, (int)arg->narg);
#endif
return (VALUE)fcntl(arg->fd, arg->cmd, arg->narg);
}
static int
-do_fcntl(int fd, int cmd, long narg)
+do_fcntl(struct rb_io *io, int cmd, long narg)
{
int retval;
struct fcntl_arg arg;
- arg.fd = fd;
+ arg.fd = io->fd;
arg.cmd = cmd;
arg.narg = narg;
- retval = (int)rb_thread_io_blocking_region(nogvl_fcntl, &arg, fd);
+ retval = (int)rb_io_blocking_region(io, nogvl_fcntl, &arg);
+ if (retval != -1) {
+ switch (cmd) {
#if defined(F_DUPFD)
- if (retval != -1 && cmd == F_DUPFD) {
- rb_update_max_fd(retval);
- }
+ case F_DUPFD:
+#endif
+#if defined(F_DUPFD_CLOEXEC)
+ case F_DUPFD_CLOEXEC:
#endif
+ rb_update_max_fd(retval);
+ }
+ }
return retval;
}
@@ -9003,42 +11585,27 @@ rb_fcntl(VALUE io, VALUE req, VALUE arg)
long narg;
int retval;
- rb_secure(2);
-
- narg = setup_narg(cmd, &arg, 0);
+ narg = setup_narg(cmd, &arg, fcntl_narg_len);
GetOpenFile(io, fptr);
- retval = do_fcntl(fptr->fd, cmd, narg);
- if (retval < 0) rb_sys_fail_path(fptr->pathv);
- if (RB_TYPE_P(arg, T_STRING)) {
- if (RSTRING_PTR(arg)[RSTRING_LEN(arg)-1] != 17)
- rb_raise(rb_eArgError, "return value overflowed string");
- RSTRING_PTR(arg)[RSTRING_LEN(arg)-1] = '\0';
- }
-
- if (cmd == F_SETFL) {
- if (narg & O_NONBLOCK) {
- fptr->mode |= FMODE_WSPLIT_INITIALIZED;
- fptr->mode &= ~FMODE_WSPLIT;
- }
- else {
- fptr->mode &= ~(FMODE_WSPLIT_INITIALIZED|FMODE_WSPLIT);
- }
- }
-
- return INT2NUM(retval);
+ retval = do_fcntl(fptr, cmd, narg);
+ return finish_narg(retval, arg, fptr);
}
/*
* call-seq:
- * ios.fcntl(integer_cmd, arg) -> integer
+ * fcntl(integer_cmd, argument) -> integer
+ *
+ * Invokes Posix system call {fcntl(2)}[https://man7.org/linux/man-pages/man2/fcntl.2.html],
+ * which provides a mechanism for issuing low-level commands to control or query
+ * a file-oriented I/O stream. Arguments and results are platform
+ * dependent.
+ *
+ * If +argument+ is a number, its value is passed directly;
+ * if it is a string, it is interpreted as a binary sequence of bytes.
+ * (Array#pack might be a useful way to build this string.)
*
- * Provides a mechanism for issuing low-level commands to control or
- * query file-oriented I/O streams. Arguments and results are platform
- * dependent. If <i>arg</i> is a number, its value is passed
- * directly. If it is a string, it is interpreted as a binary sequence
- * of bytes (<code>Array#pack</code> might be a useful way to build this
- * string). On Unix platforms, see <code>fcntl(2)</code> for details.
* Not implemented on all platforms.
+ *
*/
static VALUE
@@ -9056,45 +11623,35 @@ rb_io_fcntl(int argc, VALUE *argv, VALUE io)
#if defined(HAVE_SYSCALL) || defined(HAVE___SYSCALL)
/*
* call-seq:
- * syscall(num [, args...]) -> integer
+ * syscall(integer_callno, *arguments) -> integer
*
- * Calls the operating system function identified by _num_ and
- * returns the result of the function or raises SystemCallError if
- * it failed.
+ * Invokes Posix system call {syscall(2)}[https://man7.org/linux/man-pages/man2/syscall.2.html],
+ * which calls a specified function.
*
- * Arguments for the function can follow _num_. They must be either
- * +String+ objects or +Integer+ objects. A +String+ object is passed
- * as a pointer to the byte sequence. An +Integer+ object is passed
- * as an integer whose bit size is same as a pointer.
- * Up to nine parameters may be passed (14 on the Atari-ST).
+ * Calls the operating system function identified by +integer_callno+;
+ * returns the result of the function or raises SystemCallError if it failed.
+ * The effect of the call is platform-dependent.
+ * The arguments and returned value are platform-dependent.
*
- * The function identified by _num_ is system
- * dependent. On some Unix systems, the numbers may be obtained from a
- * header file called <code>syscall.h</code>.
+ * For each of +arguments+: if it is an integer, it is passed directly;
+ * if it is a string, it is interpreted as a binary sequence of bytes.
+ * There may be as many as nine such arguments.
*
- * syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
- *
- * <em>produces:</em>
- *
- * hello
+ * Arguments +integer_callno+ and +argument+, as well as the returned value,
+ * are platform-dependent.
*
+ * Note: Method +syscall+ is essentially unsafe and unportable.
+ * The DL (Fiddle) library is preferred for safer and a bit
+ * more portable programming.
*
- * Calling +syscall+ on a platform which does not have any way to
- * an arbitrary system function just fails with NotImplementedError.
+ * Not implemented on all platforms.
*
- * Note::
- * +syscall+ is essentially unsafe and unportable. Feel free to shoot your foot.
- * DL (Fiddle) library is preferred for safer and a bit more portable programming.
*/
static VALUE
-rb_f_syscall(int argc, VALUE *argv)
+rb_f_syscall(int argc, VALUE *argv, VALUE _)
{
-#ifdef atarist
- VALUE arg[13]; /* yes, we really need that many ! */
-#else
VALUE arg[8];
-#endif
#if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8 /* mainly *BSD */
# define SYSCALL __syscall
# define NUM2SYSCALLID(x) NUM2LONG(x)
@@ -9127,83 +11684,57 @@ rb_f_syscall(int argc, VALUE *argv)
int i;
if (RTEST(ruby_verbose)) {
- rb_warning("We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
+ rb_category_warning(RB_WARN_CATEGORY_DEPRECATED,
+ "We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
}
- rb_secure(2);
if (argc == 0)
- rb_raise(rb_eArgError, "too few arguments for syscall");
+ rb_raise(rb_eArgError, "too few arguments for syscall");
if (argc > numberof(arg))
- rb_raise(rb_eArgError, "too many arguments for syscall");
+ rb_raise(rb_eArgError, "too many arguments for syscall");
num = NUM2SYSCALLID(argv[0]); ++argv;
for (i = argc - 1; i--; ) {
- VALUE v = rb_check_string_type(argv[i]);
+ VALUE v = rb_check_string_type(argv[i]);
- if (!NIL_P(v)) {
- SafeStringValue(v);
- rb_str_modify(v);
- arg[i] = (VALUE)StringValueCStr(v);
- }
- else {
- arg[i] = (VALUE)NUM2LONG(argv[i]);
- }
+ if (!NIL_P(v)) {
+ StringValue(v);
+ rb_str_modify(v);
+ arg[i] = (VALUE)StringValueCStr(v);
+ }
+ else {
+ arg[i] = (VALUE)NUM2LONG(argv[i]);
+ }
}
switch (argc) {
case 1:
- retval = SYSCALL(num);
- break;
+ retval = SYSCALL(num);
+ break;
case 2:
- retval = SYSCALL(num, arg[0]);
- break;
+ retval = SYSCALL(num, arg[0]);
+ break;
case 3:
- retval = SYSCALL(num, arg[0],arg[1]);
- break;
+ retval = SYSCALL(num, arg[0],arg[1]);
+ break;
case 4:
- retval = SYSCALL(num, arg[0],arg[1],arg[2]);
- break;
+ retval = SYSCALL(num, arg[0],arg[1],arg[2]);
+ break;
case 5:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
- break;
+ retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
+ break;
case 6:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
- break;
+ retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
+ break;
case 7:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
- break;
+ retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
+ break;
case 8:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
- break;
-#ifdef atarist
- case 9:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
- arg[7]);
- break;
- case 10:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
- arg[7], arg[8]);
- break;
- case 11:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
- arg[7], arg[8], arg[9]);
- break;
- case 12:
- retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],
- arg[7], arg[8], arg[9], arg[10]);
- break;
- case 13:
- retval = SYSCALL(num, 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 14:
- retval = SYSCALL(num, 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]);
+ retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
break;
-#endif
}
if (retval == -1)
- rb_sys_fail(0);
+ rb_sys_fail(0);
return RETVAL2NUM(retval);
#undef SYSCALL
#undef NUM2SYSCALLID
@@ -9223,7 +11754,7 @@ static rb_encoding *
find_encoding(VALUE v)
{
rb_encoding *enc = rb_find_encoding(v);
- if (!enc) unsupported_encoding(StringValueCStr(v));
+ if (!enc) rb_warn("Unsupported encoding %"PRIsVALUE" ignored", v);
return enc;
}
@@ -9235,51 +11766,56 @@ io_encoding_set(rb_io_t *fptr, VALUE v1, VALUE v2, VALUE opt)
VALUE ecopts, tmp;
if (!NIL_P(v2)) {
- enc2 = find_encoding(v1);
- tmp = rb_check_string_type(v2);
- if (!NIL_P(tmp)) {
- if (RSTRING_LEN(tmp) == 1 && RSTRING_PTR(tmp)[0] == '-') {
- /* Special case - "-" => no transcoding */
- enc = enc2;
- enc2 = NULL;
- }
- else
- enc = find_encoding(v2);
- if (enc == enc2) {
- /* Special case - "-" => no transcoding */
- enc2 = NULL;
- }
- }
- else {
- enc = find_encoding(v2);
- if (enc == enc2) {
- /* Special case - "-" => no transcoding */
- enc2 = NULL;
- }
- }
- SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
- ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
+ enc2 = find_encoding(v1);
+ tmp = rb_check_string_type(v2);
+ if (!NIL_P(tmp)) {
+ if (RSTRING_LEN(tmp) == 1 && RSTRING_PTR(tmp)[0] == '-') {
+ /* Special case - "-" => no transcoding */
+ enc = enc2;
+ enc2 = NULL;
+ }
+ else
+ enc = find_encoding(v2);
+ if (enc == enc2) {
+ /* Special case - "-" => no transcoding */
+ enc2 = NULL;
+ }
+ }
+ else {
+ enc = find_encoding(v2);
+ if (enc == enc2) {
+ /* Special case - "-" => no transcoding */
+ enc2 = NULL;
+ }
+ }
+ if (enc2 == rb_ascii8bit_encoding()) {
+ /* If external is ASCII-8BIT, no transcoding */
+ enc = enc2;
+ enc2 = NULL;
+ }
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
+ ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
}
else {
- if (NIL_P(v1)) {
- /* Set to default encodings */
- rb_io_ext_int_to_encs(NULL, NULL, &enc, &enc2, 0);
- SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
+ if (NIL_P(v1)) {
+ /* Set to default encodings */
+ rb_io_ext_int_to_encs(NULL, NULL, &enc, &enc2, 0);
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
ecopts = Qnil;
- }
- else {
- tmp = rb_check_string_type(v1);
- if (!NIL_P(tmp) && rb_enc_asciicompat(rb_enc_get(tmp))) {
- parse_mode_enc(RSTRING_PTR(tmp), &enc, &enc2, NULL);
- SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
+ }
+ else {
+ tmp = rb_check_string_type(v1);
+ if (!NIL_P(tmp) && rb_enc_asciicompat(enc = rb_enc_get(tmp))) {
+ parse_mode_enc(RSTRING_PTR(tmp), enc, &enc, &enc2, NULL);
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
- }
- else {
- rb_io_ext_int_to_encs(find_encoding(v1), NULL, &enc, &enc2, 0);
- SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
+ }
+ else {
+ rb_io_ext_int_to_encs(find_encoding(v1), NULL, &enc, &enc2, 0);
+ SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
ecopts = Qnil;
- }
- }
+ }
+ }
}
validate_enc_binmode(&fptr->mode, ecflags, enc, enc2);
fptr->encs.enc = enc;
@@ -9290,6 +11826,21 @@ io_encoding_set(rb_io_t *fptr, VALUE v1, VALUE v2, VALUE opt)
}
+struct io_encoding_set_args {
+ rb_io_t *fptr;
+ VALUE v1;
+ VALUE v2;
+ VALUE opt;
+};
+
+static VALUE
+io_encoding_set_v(VALUE v)
+{
+ struct io_encoding_set_args *arg = (struct io_encoding_set_args *)v;
+ io_encoding_set(arg->fptr, arg->v1, arg->v2, arg->opt);
+ return Qnil;
+}
+
static VALUE
pipe_pair_close(VALUE rw)
{
@@ -9299,62 +11850,81 @@ pipe_pair_close(VALUE rw)
/*
* call-seq:
- * IO.pipe -> [read_io, write_io]
- * IO.pipe(ext_enc) -> [read_io, write_io]
- * IO.pipe("ext_enc:int_enc" [, opt]) -> [read_io, write_io]
- * IO.pipe(ext_enc, int_enc [, opt]) -> [read_io, write_io]
+ * IO.pipe(**opts) -> [read_io, write_io]
+ * IO.pipe(enc, **opts) -> [read_io, write_io]
+ * IO.pipe(ext_enc, int_enc, **opts) -> [read_io, write_io]
+ * IO.pipe(**opts) {|read_io, write_io| ...} -> object
+ * IO.pipe(enc, **opts) {|read_io, write_io| ...} -> object
+ * IO.pipe(ext_enc, int_enc, **opts) {|read_io, write_io| ...} -> object
*
- * IO.pipe(...) {|read_io, write_io| ... }
+ * Creates a pair of pipe endpoints, +read_io+ and +write_io+,
+ * connected to each other.
*
- * Creates a pair of pipe endpoints (connected to each other) and
- * returns them as a two-element array of <code>IO</code> objects:
- * <code>[</code> <i>read_io</i>, <i>write_io</i> <code>]</code>.
+ * If argument +enc_string+ is given, it must be a string containing one of:
*
- * If a block is given, the block is called and
- * returns the value of the block.
- * <i>read_io</i> and <i>write_io</i> are sent to the block as arguments.
- * If read_io and write_io are not closed when the block exits, they are closed.
- * i.e. closing read_io and/or write_io doesn't cause an error.
+ * - The name of the encoding to be used as the external encoding.
+ * - The colon-separated names of two encodings to be used as the external
+ * and internal encodings.
*
- * Not available on all platforms.
+ * If argument +int_enc+ is given, it must be an Encoding object
+ * or encoding name string that specifies the internal encoding to be used;
+ * if argument +ext_enc+ is also given, it must be an Encoding object
+ * or encoding name string that specifies the external encoding to be used.
+ *
+ * The string read from +read_io+ is tagged with the external encoding;
+ * if an internal encoding is also specified, the string is converted
+ * to, and tagged with, that encoding.
+ *
+ * If any encoding is specified,
+ * optional hash arguments specify the conversion option.
+ *
+ * Optional keyword arguments +opts+ specify:
+ *
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding Options}[rdoc-ref:encodings.rdoc@Encoding+Options].
+ *
+ * With no block given, returns the two endpoints in an array:
+ *
+ * IO.pipe # => [#<IO:fd 4>, #<IO:fd 5>]
*
- * If an encoding (encoding name or encoding object) is specified as an optional argument,
- * read string from pipe is tagged with the encoding specified.
- * If the argument is a colon separated two encoding names "A:B",
- * the read string is converted from encoding A (external encoding)
- * to encoding B (internal encoding), then tagged with B.
- * If two optional arguments are specified, those must be
- * encoding objects or encoding names,
- * and the first one is the external encoding,
- * and the second one is the internal encoding.
- * If the external encoding and the internal encoding is specified,
- * optional hash argument specify the conversion option.
+ * With a block given, calls the block with the two endpoints;
+ * closes both endpoints and returns the value of the block:
+ *
+ * IO.pipe {|read_io, write_io| p read_io; p write_io }
+ *
+ * Output:
+ *
+ * #<IO:fd 6>
+ * #<IO:fd 7>
+ *
+ * Not available on all platforms.
*
* In the example below, the two processes close the ends of the pipe
* that they are not using. This is not just a cosmetic nicety. The
* read end of a pipe will not generate an end of file condition if
* there are any writers with the pipe still open. In the case of the
- * parent process, the <code>rd.read</code> will never return if it
- * does not first issue a <code>wr.close</code>.
- *
- * rd, wr = IO.pipe
- *
- * if fork
- * wr.close
- * puts "Parent got: <#{rd.read}>"
- * rd.close
- * Process.wait
- * else
- * rd.close
- * puts "Sending message to parent"
- * wr.write "Hi Dad"
- * wr.close
- * end
+ * parent process, the <tt>rd.read</tt> will never return if it
+ * does not first issue a <tt>wr.close</tt>:
+ *
+ * rd, wr = IO.pipe
+ *
+ * if fork
+ * wr.close
+ * puts "Parent got: <#{rd.read}>"
+ * rd.close
+ * Process.wait
+ * else
+ * rd.close
+ * puts 'Sending message to parent'
+ * wr.write "Hi Dad"
+ * wr.close
+ * end
*
* <em>produces:</em>
*
* Sending message to parent
* Parent got: <Hi Dad>
+ *
*/
static VALUE
@@ -9364,11 +11934,12 @@ rb_io_s_pipe(int argc, VALUE *argv, VALUE klass)
VALUE r, w, args[3], v1, v2;
VALUE opt;
rb_io_t *fptr, *fptr2;
- int fmode = 0;
+ struct io_encoding_set_args ies_args;
+ enum rb_io_mode fmode = 0;
VALUE ret;
argc = rb_scan_args(argc, argv, "02:", &v1, &v2, &opt);
- if (rb_pipe(pipes) == -1)
+ if (rb_pipe(pipes) < 0)
rb_sys_fail(0);
args[0] = klass;
@@ -9376,50 +11947,67 @@ rb_io_s_pipe(int argc, VALUE *argv, VALUE klass)
args[2] = INT2FIX(O_RDONLY);
r = rb_protect(io_new_instance, (VALUE)args, &state);
if (state) {
- close(pipes[0]);
- close(pipes[1]);
- rb_jump_tag(state);
+ close(pipes[0]);
+ close(pipes[1]);
+ rb_jump_tag(state);
}
GetOpenFile(r, fptr);
- io_encoding_set(fptr, v1, v2, opt);
+
+ ies_args.fptr = fptr;
+ ies_args.v1 = v1;
+ ies_args.v2 = v2;
+ ies_args.opt = opt;
+ rb_protect(io_encoding_set_v, (VALUE)&ies_args, &state);
+ if (state) {
+ close(pipes[1]);
+ io_close(r);
+ rb_jump_tag(state);
+ }
+
args[1] = INT2NUM(pipes[1]);
args[2] = INT2FIX(O_WRONLY);
w = rb_protect(io_new_instance, (VALUE)args, &state);
if (state) {
- close(pipes[1]);
- if (!NIL_P(r)) rb_io_close(r);
- rb_jump_tag(state);
+ close(pipes[1]);
+ if (!NIL_P(r)) rb_io_close(r);
+ rb_jump_tag(state);
}
GetOpenFile(w, fptr2);
rb_io_synchronized(fptr2);
extract_binmode(opt, &fmode);
+
+ if ((fmode & FMODE_BINMODE) && NIL_P(v1)) {
+ rb_io_ascii8bit_binmode(r);
+ rb_io_ascii8bit_binmode(w);
+ }
+
#if DEFAULT_TEXTMODE
if ((fptr->mode & FMODE_TEXTMODE) && (fmode & FMODE_BINMODE)) {
- fptr->mode &= ~FMODE_TEXTMODE;
- setmode(fptr->fd, O_BINARY);
+ fptr->mode &= ~FMODE_TEXTMODE;
+ setmode(fptr->fd, O_BINARY);
}
-#if defined(RUBY_TEST_CRLF_ENVIRONMENT) || defined(_WIN32)
+#if RUBY_CRLF_ENVIRONMENT
if (fptr->encs.ecflags & ECONV_DEFAULT_NEWLINE_DECORATOR) {
- fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
+ fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR;
}
#endif
#endif
fptr->mode |= fmode;
#if DEFAULT_TEXTMODE
if ((fptr2->mode & FMODE_TEXTMODE) && (fmode & FMODE_BINMODE)) {
- fptr2->mode &= ~FMODE_TEXTMODE;
- setmode(fptr2->fd, O_BINARY);
+ fptr2->mode &= ~FMODE_TEXTMODE;
+ setmode(fptr2->fd, O_BINARY);
}
#endif
fptr2->mode |= fmode;
ret = rb_assoc_new(r, w);
if (rb_block_given_p()) {
- VALUE rw[2];
- rw[0] = r;
- rw[1] = w;
- return rb_ensure(rb_yield, ret, pipe_pair_close, (VALUE)rw);
+ VALUE rw[2];
+ rw[0] = r;
+ rw[1] = w;
+ return rb_ensure(rb_yield, ret, pipe_pair_close, (VALUE)rw);
}
return ret;
}
@@ -9431,9 +12019,10 @@ struct foreach_arg {
};
static void
-open_key_args(int argc, VALUE *argv, VALUE opt, struct foreach_arg *arg)
+open_key_args(VALUE klass, int argc, VALUE *argv, VALUE opt, struct foreach_arg *arg)
{
VALUE path, v;
+ VALUE vmode = Qnil, vperm = Qnil;
path = *argv++;
argc--;
@@ -9442,65 +12031,108 @@ open_key_args(int argc, VALUE *argv, VALUE opt, struct foreach_arg *arg)
arg->argc = argc;
arg->argv = argv;
if (NIL_P(opt)) {
- arg->io = rb_io_open(path, INT2NUM(O_RDONLY), INT2FIX(0666), Qnil);
- return;
+ vmode = INT2NUM(O_RDONLY);
+ vperm = INT2FIX(0666);
}
- v = rb_hash_aref(opt, sym_open_args);
- if (!NIL_P(v)) {
- VALUE args;
- long n;
+ else if (!NIL_P(v = rb_hash_aref(opt, sym_open_args))) {
+ int n;
- v = rb_convert_type(v, T_ARRAY, "Array", "to_ary");
- n = RARRAY_LEN(v) + 1;
-#if SIZEOF_LONG > SIZEOF_INT
- if (n > INT_MAX) {
- rb_raise(rb_eArgError, "too many arguments");
- }
-#endif
- args = rb_ary_tmp_new(n);
- rb_ary_push(args, path);
- rb_ary_concat(args, v);
- arg->io = rb_io_open_with_args((int)n, RARRAY_CONST_PTR(args));
- rb_ary_clear(args); /* prevent from GC */
- return;
+ v = rb_to_array_type(v);
+ n = RARRAY_LENINT(v);
+ rb_check_arity(n, 0, 3); /* rb_io_open */
+ rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, n, RARRAY_CONST_PTR(v), "02:", &vmode, &vperm, &opt);
}
- arg->io = rb_io_open(path, Qnil, Qnil, opt);
+ arg->io = rb_io_open(klass, path, vmode, vperm, opt);
}
static VALUE
-io_s_foreach(struct foreach_arg *arg)
+io_s_foreach(VALUE v)
{
+ struct getline_arg *arg = (void *)v;
VALUE str;
- while (!NIL_P(str = rb_io_gets_m(arg->argc, arg->argv, arg->io))) {
- rb_yield(str);
+ if (arg->limit == 0)
+ rb_raise(rb_eArgError, "invalid limit: 0 for foreach");
+ while (!NIL_P(str = rb_io_getline_1(arg->rs, arg->limit, arg->chomp, arg->io))) {
+ rb_lastline_set(str);
+ rb_yield(str);
}
+ rb_lastline_set(Qnil);
return Qnil;
}
/*
* call-seq:
- * IO.foreach(name, sep=$/ [, open_args]) {|line| block } -> nil
- * IO.foreach(name, limit [, open_args]) {|line| block } -> nil
- * IO.foreach(name, sep, limit [, open_args]) {|line| block } -> nil
- * IO.foreach(...) -> an_enumerator
+ * IO.foreach(path, sep = $/, **opts) {|line| block } -> nil
+ * IO.foreach(path, limit, **opts) {|line| block } -> nil
+ * IO.foreach(path, sep, limit, **opts) {|line| block } -> nil
+ * IO.foreach(...) -> an_enumerator
*
- * Executes the block for every line in the named I/O port, where lines
- * are separated by <em>sep</em>.
+ * Calls the block with each successive line read from the stream.
*
- * If no block is given, an enumerator is returned instead.
+ * The first argument must be a string that is the path to a file.
*
- * IO.foreach("testfile") {|x| print "GOT ", x }
+ * With only argument +path+ given, parses lines from the file at the given +path+,
+ * as determined by the default line separator,
+ * and calls the block with each successive line:
*
- * <em>produces:</em>
+ * File.foreach('t.txt') {|line| p line }
+ *
+ * Output: the same as above.
+ *
+ * For both forms, command and path, the remaining arguments are the same.
+ *
+ * With argument +sep+ given, parses lines as determined by that line separator
+ * (see {Line Separator}[rdoc-ref:IO@Line+Separator]):
+ *
+ * File.foreach('t.txt', 'li') {|line| p line }
+ *
+ * Output:
+ *
+ * "First li"
+ * "ne\nSecond li"
+ * "ne\n\nThird li"
+ * "ne\nFourth li"
+ * "ne\n"
+ *
+ * Each paragraph:
+ *
+ * File.foreach('t.txt', '') {|paragraph| p paragraph }
+ *
+ * Output:
+ *
+ * "First line\nSecond line\n\n"
+ * "Third line\nFourth line\n"
+ *
+ * With argument +limit+ given, parses lines as determined by the default
+ * line separator and the given line-length limit
+ * (see {Line Separator}[rdoc-ref:IO@Line+Separator] and {Line Limit}[rdoc-ref:IO@Line+Limit]):
+ *
+ * File.foreach('t.txt', 7) {|line| p line }
+ *
+ * Output:
+ *
+ * "First l"
+ * "ine\n"
+ * "Second "
+ * "line\n"
+ * "\n"
+ * "Third l"
+ * "ine\n"
+ * "Fourth l"
+ * "line\n"
+ *
+ * With arguments +sep+ and +limit+ given,
+ * combines the two behaviors
+ * (see {Line Separator and Line Limit}[rdoc-ref:IO@Line+Separator+and+Line+Limit]).
*
- * GOT This is line one
- * GOT This is line two
- * GOT This is line three
- * GOT And so on...
+ * Optional keyword arguments +opts+ specify:
*
- * If the last argument is a hash, it's the keyword argument to open.
- * See <code>IO.read</code> for detail.
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
+ * - {Line Options}[rdoc-ref:IO@Line+IO].
+ *
+ * Returns an Enumerator if no block is given.
*
*/
@@ -9510,35 +12142,71 @@ rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
VALUE opt;
int orig_argc = argc;
struct foreach_arg arg;
+ struct getline_arg garg;
- argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
+ argc = rb_scan_args(argc, argv, "12:", NULL, NULL, NULL, &opt);
RETURN_ENUMERATOR(self, orig_argc, argv);
- open_key_args(argc, argv, opt, &arg);
+ extract_getline_args(argc-1, argv+1, &garg);
+ open_key_args(self, argc, argv, opt, &arg);
if (NIL_P(arg.io)) return Qnil;
- return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, arg.io);
+ extract_getline_opts(opt, &garg);
+ check_getline_args(&garg.rs, &garg.limit, garg.io = arg.io);
+ return rb_ensure(io_s_foreach, (VALUE)&garg, rb_io_close, arg.io);
}
static VALUE
-io_s_readlines(struct foreach_arg *arg)
+io_s_readlines(VALUE v)
{
- return rb_io_readlines(arg->argc, arg->argv, arg->io);
+ struct getline_arg *arg = (void *)v;
+ return io_readlines(arg, arg->io);
}
/*
* call-seq:
- * IO.readlines(name, sep=$/ [, open_args]) -> array
- * IO.readlines(name, limit [, open_args]) -> array
- * IO.readlines(name, sep, limit [, open_args]) -> array
+ * IO.readlines(path, sep = $/, **opts) -> array
+ * IO.readlines(path, limit, **opts) -> array
+ * IO.readlines(path, sep, limit, **opts) -> array
+ *
+ * Returns an array of all lines read from the stream.
+ *
+ * The first argument must be a string that is the path to a file.
+ *
+ * With only argument +path+ given, parses lines from the file at the given +path+,
+ * as determined by the default line separator,
+ * and returns those lines in an array:
+ *
+ * IO.readlines('t.txt')
+ * # => ["First line\n", "Second line\n", "\n", "Third line\n", "Fourth line\n"]
+ *
+ * With argument +sep+ given, parses lines as determined by that line separator
+ * (see {Line Separator}[rdoc-ref:IO@Line+Separator]):
*
- * Reads the entire file specified by <i>name</i> as individual
- * lines, and returns those lines in an array. Lines are separated by
- * <i>sep</i>.
+ * # Ordinary separator.
+ * IO.readlines('t.txt', 'li')
+ * # =>["First li", "ne\nSecond li", "ne\n\nThird li", "ne\nFourth li", "ne\n"]
+ * # Get-paragraphs separator.
+ * IO.readlines('t.txt', '')
+ * # => ["First line\nSecond line\n\n", "Third line\nFourth line\n"]
+ * # Get-all separator.
+ * IO.readlines('t.txt', nil)
+ * # => ["First line\nSecond line\n\nThird line\nFourth line\n"]
*
- * a = IO.readlines("testfile")
- * a[0] #=> "This is line one\n"
+ * With argument +limit+ given, parses lines as determined by the default
+ * line separator and the given line-length limit
+ * (see {Line Separator}[rdoc-ref:IO@Line+Separator] and {Line Limit}[rdoc-ref:IO@Line+Limit]:
*
- * If the last argument is a hash, it's the keyword argument to open.
- * See <code>IO.read</code> for detail.
+ * IO.readlines('t.txt', 7)
+ * # => ["First l", "ine\n", "Second ", "line\n", "\n", "Third l", "ine\n", "Fourth ", "line\n"]
+ *
+ * With arguments +sep+ and +limit+ given,
+ * combines the two behaviors
+ * (see {Line Separator and Line Limit}[rdoc-ref:IO@Line+Separator+and+Line+Limit]).
+ *
+ * Optional keyword arguments +opts+ specify:
+ *
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
+ * - {Line Options}[rdoc-ref:IO@Line+IO].
*
*/
@@ -9547,16 +12215,21 @@ rb_io_s_readlines(int argc, VALUE *argv, VALUE io)
{
VALUE opt;
struct foreach_arg arg;
+ struct getline_arg garg;
- argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
- open_key_args(argc, argv, opt, &arg);
+ argc = rb_scan_args(argc, argv, "12:", NULL, NULL, NULL, &opt);
+ extract_getline_args(argc-1, argv+1, &garg);
+ open_key_args(io, argc, argv, opt, &arg);
if (NIL_P(arg.io)) return Qnil;
- return rb_ensure(io_s_readlines, (VALUE)&arg, rb_io_close, arg.io);
+ extract_getline_opts(opt, &garg);
+ check_getline_args(&garg.rs, &garg.limit, garg.io = arg.io);
+ return rb_ensure(io_s_readlines, (VALUE)&garg, rb_io_close, arg.io);
}
static VALUE
-io_s_read(struct foreach_arg *arg)
+io_s_read(VALUE v)
{
+ struct foreach_arg *arg = (void *)v;
return io_read(arg->argc, arg->argv, arg->io);
}
@@ -9576,77 +12249,103 @@ seek_before_access(VALUE argp)
/*
* call-seq:
- * IO.read(name, [length [, offset]] ) -> string
- * IO.read(name, [length [, offset]], open_args) -> string
+ * IO.read(path, length = nil, offset = 0, **opts) -> string or nil
*
- * Opens the file, optionally seeks to the given +offset+, then returns
- * +length+ bytes (defaulting to the rest of the file). <code>read</code>
- * ensures the file is closed before returning.
+ * Opens the stream, reads and returns some or all of its content,
+ * and closes the stream; returns +nil+ if no bytes were read.
*
- * If the last argument is a hash, it specifies option for internal
- * open(). The key would be the following. open_args: is exclusive
- * to others.
+ * The first argument must be a string that is the path to a file.
*
- * encoding::
- * string or encoding
+ * With only argument +path+ given, reads in text mode and returns the entire content
+ * of the file at the given path:
*
- * specifies encoding of the read string. +encoding+ will be ignored
- * if length is specified.
+ * File.read('t.txt')
+ * # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * File.read('t.ja')
+ * # => "こんにちは"
+ * File.read('t.dat')
+ * # => "\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"
*
- * mode::
- * string
+ * On Windows, text mode can terminate reading and leave bytes in the file
+ * unread when encountering certain special bytes. Consider using
+ * IO.binread if all bytes in the file should be read.
*
- * specifies mode argument for open(). It should start with "r"
- * otherwise it will cause an error.
+ * With argument +length+, returns +length+ bytes if available:
*
- * open_args:: array of strings
+ * File.read('t.txt', 7)
+ * # => "First l"
+ * File.read('t.ja', 7)
+ * # => "\xE3\x81\x93\xE3\x82\x93\xE3"
+ * File.read('t.dat', 7)
+ * # => "\xFE\xFF\x99\x90\x99\x91\x99"
*
- * specifies arguments for open() as an array.
+ * Returns all bytes if +length+ is larger than the files size:
*
- * Examples:
+ * File.read('t.txt', 700)
+ * # => "First line\r\nSecond line\r\n\r\nFourth line\r\nFifth line\r\n"
+ * File.read('t.ja', 700)
+ * # => "\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF"
+ * File.read('t.dat', 700)
+ * # => "\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"
+ *
+ * With arguments +length+ and +offset+, returns +length+ bytes
+ * if available, beginning at the given +offset+:
+ *
+ * File.read('t.txt', 10, 2)
+ * # => "rst line\r\n"
+ * File.read('t.ja', 10, 2)
+ * # => "\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1"
+ * File.read('t.dat', 10, 2)
+ * # => "\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"
+ *
+ * Returns +nil+ if +offset+ is past the end of the stream:
+ *
+ * File.read('t.txt', 10, 200)
+ * # => nil
+ *
+ * Optional keyword arguments +opts+ specify:
+ *
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
*
- * IO.read("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
- * IO.read("testfile", 20) #=> "This is line one\nThi"
- * IO.read("testfile", 20, 10) #=> "ne one\nThis is line "
*/
static VALUE
rb_io_s_read(int argc, VALUE *argv, VALUE io)
{
VALUE opt, offset;
+ long off;
struct foreach_arg arg;
argc = rb_scan_args(argc, argv, "13:", NULL, NULL, &offset, NULL, &opt);
- open_key_args(argc, argv, opt, &arg);
+ if (!NIL_P(offset) && (off = NUM2LONG(offset)) < 0) {
+ rb_raise(rb_eArgError, "negative offset %ld given", off);
+ }
+ open_key_args(io, argc, argv, opt, &arg);
if (NIL_P(arg.io)) return Qnil;
if (!NIL_P(offset)) {
- struct seek_arg sarg;
- int state = 0;
- sarg.io = arg.io;
- sarg.offset = offset;
- sarg.mode = SEEK_SET;
- rb_protect(seek_before_access, (VALUE)&sarg, &state);
- if (state) {
- rb_io_close(arg.io);
- rb_jump_tag(state);
- }
- if (arg.argc == 2) arg.argc = 1;
+ struct seek_arg sarg;
+ int state = 0;
+ sarg.io = arg.io;
+ sarg.offset = offset;
+ sarg.mode = SEEK_SET;
+ rb_protect(seek_before_access, (VALUE)&sarg, &state);
+ if (state) {
+ rb_io_close(arg.io);
+ rb_jump_tag(state);
+ }
+ if (arg.argc == 2) arg.argc = 1;
}
return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
}
/*
* call-seq:
- * IO.binread(name, [length [, offset]] ) -> string
+ * IO.binread(path, length = nil, offset = 0) -> string or nil
*
- * Opens the file, optionally seeks to the given <i>offset</i>, then returns
- * <i>length</i> bytes (defaulting to the rest of the file).
- * <code>binread</code> ensures the file is closed before returning.
- * The open mode would be "rb:ASCII-8BIT".
+ * Behaves like IO.read, except that the stream is opened in binary mode
+ * with ASCII-8BIT encoding.
*
- * IO.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
- * IO.binread("testfile", 20) #=> "This is line one\nThi"
- * IO.binread("testfile", 20, 10) #=> "ne one\nThis is line "
*/
static VALUE
@@ -9654,27 +12353,46 @@ rb_io_s_binread(int argc, VALUE *argv, VALUE io)
{
VALUE offset;
struct foreach_arg arg;
+ enum rb_io_mode fmode = FMODE_READABLE|FMODE_BINMODE;
+ enum {
+ oflags = O_RDONLY
+#ifdef O_BINARY
+ |O_BINARY
+#endif
+ };
+ struct rb_io_encoding convconfig = {NULL, NULL, 0, Qnil};
rb_scan_args(argc, argv, "12", NULL, NULL, &offset);
FilePathValue(argv[0]);
- arg.io = rb_io_open(argv[0], rb_str_new_cstr("rb:ASCII-8BIT"), Qnil, Qnil);
+ convconfig.enc = rb_ascii8bit_encoding();
+ arg.io = rb_io_open_generic(io, argv[0], oflags, fmode, &convconfig, 0);
if (NIL_P(arg.io)) return Qnil;
arg.argv = argv+1;
arg.argc = (argc > 1) ? 1 : 0;
if (!NIL_P(offset)) {
- rb_io_seek(arg.io, offset, SEEK_SET);
+ struct seek_arg sarg;
+ int state = 0;
+ sarg.io = arg.io;
+ sarg.offset = offset;
+ sarg.mode = SEEK_SET;
+ rb_protect(seek_before_access, (VALUE)&sarg, &state);
+ if (state) {
+ rb_io_close(arg.io);
+ rb_jump_tag(state);
+ }
}
return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
}
static VALUE
-io_s_write0(struct write_arg *arg)
+io_s_write0(VALUE v)
{
+ struct write_arg *arg = (void *)v;
return io_write(arg->io,arg->str,arg->nosync);
}
static VALUE
-io_s_write(int argc, VALUE *argv, int binary)
+io_s_write(int argc, VALUE *argv, VALUE klass, int binary)
{
VALUE string, offset, opt;
struct foreach_arg arg;
@@ -9694,7 +12412,7 @@ io_s_write(int argc, VALUE *argv, int binary)
if (NIL_P(offset)) mode |= O_TRUNC;
rb_hash_aset(opt,sym_mode,INT2NUM(mode));
}
- open_key_args(argc,argv,opt,&arg);
+ open_key_args(klass, argc, argv, opt, &arg);
#ifndef O_BINARY
if (binary) rb_io_binmode_m(arg.io);
@@ -9723,81 +12441,99 @@ io_s_write(int argc, VALUE *argv, int binary)
/*
* call-seq:
- * IO.write(name, string, [offset] ) => fixnum
- * IO.write(name, string, [offset], open_args ) => fixnum
+ * IO.write(path, data, offset = 0, **opts) -> nonnegative_integer
+ *
+ * Opens the stream, writes the given +data+ to it,
+ * and closes the stream; returns the number of bytes written.
+ *
+ * The first argument must be a string that is the path to a file.
*
- * Opens the file, optionally seeks to the given <i>offset</i>, writes
- * <i>string</i>, then returns the length written.
- * <code>write</code> ensures the file is closed before returning.
- * If <i>offset</i> is not given, the file is truncated. Otherwise,
- * it is not truncated.
+ * With only arguments +path+ and +data+ given,
+ * writes the given data to the file at that path:
*
- * If the last argument is a hash, it specifies option for internal
- * open(). The key would be the following. open_args: is exclusive
- * to others.
+ * path = 't.tmp'
+ * File.write(path, "First line\nSecond line\n\nFourth line\nFifth line\n") # => 47
+ * File.write(path, 'こんにちは') # => 15
+ * File.write(path, "\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94") # => 12
*
- * encoding: string or encoding
+ * When +offset+ is zero (the default), the entire file content is overwritten:
*
- * specifies encoding of the read string. encoding will be ignored
- * if length is specified.
+ * File.read(path) # => "\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"
+ * File.write(path, 'foo')
+ * File.read(path) # => "foo"
*
- * mode: string
+ * When +offset+ in within the file content, the file content is partly overwritten,
+ * beginning at byte +offset+:
*
- * specifies mode argument for open(). it should start with "w" or "a" or "r+"
- * otherwise it would cause error.
+ * File.write(path, "First line\nSecond line\n\nFourth line\nFifth line\n")
+ * File.write(path, 'LINE', 6)
+ * File.read(path) # => "First LINE\nSecond line\n\nFourth line\nFifth line\n"
*
- * perm: fixnum
+ * When the file contains multi-byte characters,
+ * the effect of writing may disturb some characters:
*
- * specifies perm argument for open().
+ * File.write(path, "こんにちは")
+ * File.write(path, 'FOO', 3) # Replace one 3-byte character.
+ * File.read(path) # => "こFOOにちは"
+ * File.write(path, 'BAR', 7) # Replace bytes in two different 3-byte characters.
+ * File.read(path) # => "こFOO\xE3BAR\x81\xA1は"
*
- * open_args: array
+ * If +offset+ is outside the file content,
+ * the file is padded with null characters <tt>"\u0000"</tt>:
*
- * specifies arguments for open() as an array.
+ * File.write(path, "First line\nSecond line\n\nFourth line\nFifth line\n")
+ * File.write(path, 'FOO', 55)
+ * File.read(path)
+ * # => "First line\nSecond line\n\nFourth line\nFifth line\n\u0000\u0000\u0000FOO"
+ *
+ * Optional keyword arguments +opts+ specify:
+ *
+ * - {Open Options}[rdoc-ref:IO@Open+Options].
+ * - {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
*
- * IO.write("testfile", "0123456789", 20) # => 10
- * # File could contain: "This is line one\nThi0123456789two\nThis is line three\nAnd so on...\n"
- * IO.write("testfile", "0123456789") #=> 10
- * # File would now read: "0123456789"
*/
static VALUE
rb_io_s_write(int argc, VALUE *argv, VALUE io)
{
- return io_s_write(argc, argv, 0);
+ return io_s_write(argc, argv, io, 0);
}
/*
* call-seq:
- * IO.binwrite(name, string, [offset] ) => fixnum
- * IO.binwrite(name, string, [offset], open_args ) => fixnum
+ * IO.binwrite(path, string, offset = 0, **opts) -> integer
*
- * Same as <code>IO.write</code> except opening the file in binary mode
- * and ASCII-8BIT encoding ("wb:ASCII-8BIT").
+ * Behaves like IO.write, except that the stream is opened in binary mode
+ * with ASCII-8BIT encoding.
*
*/
static VALUE
rb_io_s_binwrite(int argc, VALUE *argv, VALUE io)
{
- return io_s_write(argc, argv, 1);
+ return io_s_write(argc, argv, io, 1);
}
struct copy_stream_struct {
VALUE src;
VALUE dst;
- off_t copy_length; /* (off_t)-1 if not specified */
- off_t src_offset; /* (off_t)-1 if not specified */
-
- int src_fd;
- int dst_fd;
- int close_src;
- int close_dst;
- off_t total;
- const char *syserr;
+ rb_off_t copy_length; /* (rb_off_t)-1 if not specified */
+ rb_off_t src_offset; /* (rb_off_t)-1 if not specified */
+
+ rb_io_t *src_fptr;
+ rb_io_t *dst_fptr;
+ unsigned close_src : 1;
+ unsigned close_dst : 1;
int error_no;
+ rb_off_t total;
+ const char *syserr;
const char *notimp;
- rb_fdset_t fds;
VALUE th;
+ struct stat src_stat;
+ struct stat dst_stat;
+#ifdef HAVE_FCOPYFILE
+ copyfile_state_t copyfile_state;
+#endif
};
static void *
@@ -9821,25 +12557,108 @@ maygvl_copy_stream_continue_p(int has_gvl, struct copy_stream_struct *stp)
#if defined(ERESTART)
case ERESTART:
#endif
- if (rb_thread_interrupted(stp->th)) {
+ if (rb_thread_interrupted(stp->th)) {
if (has_gvl)
rb_thread_execute_interrupts(stp->th);
else
rb_thread_call_with_gvl(exec_interrupts, (void *)stp->th);
}
- return TRUE;
+ return TRUE;
}
return FALSE;
}
+struct fiber_scheduler_wait_for_arguments {
+ VALUE scheduler;
+
+ rb_io_t *fptr;
+ short events;
+
+ VALUE result;
+};
+
+static void *
+fiber_scheduler_wait_for(void * _arguments)
+{
+ struct fiber_scheduler_wait_for_arguments *arguments = (struct fiber_scheduler_wait_for_arguments *)_arguments;
+
+ arguments->result = rb_fiber_scheduler_io_wait(arguments->scheduler, arguments->fptr->self, INT2NUM(arguments->events), RUBY_IO_TIMEOUT_DEFAULT);
+
+ return NULL;
+}
+
+#if USE_POLL
+# define IOWAIT_SYSCALL "poll"
+STATIC_ASSERT(pollin_expected, POLLIN == RB_WAITFD_IN);
+STATIC_ASSERT(pollout_expected, POLLOUT == RB_WAITFD_OUT);
static int
-maygvl_select(int has_gvl, int n, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout)
+nogvl_wait_for(VALUE th, rb_io_t *fptr, short events, struct timeval *timeout)
{
- if (has_gvl)
- return rb_thread_fd_select(n, rfds, wfds, efds, timeout);
- else
- return rb_fd_select(n, rfds, wfds, efds, timeout);
+ VALUE scheduler = rb_fiber_scheduler_current_for_thread(th);
+ if (scheduler != Qnil) {
+ struct fiber_scheduler_wait_for_arguments args = {.scheduler = scheduler, .fptr = fptr, .events = events};
+ rb_thread_call_with_gvl(fiber_scheduler_wait_for, &args);
+ return RTEST(args.result);
+ }
+
+ int fd = fptr->fd;
+ if (fd == -1) return 0;
+
+ struct pollfd fds;
+
+ fds.fd = fd;
+ fds.events = events;
+
+ int timeout_milliseconds = -1;
+
+ if (timeout) {
+ timeout_milliseconds = (int)(timeout->tv_sec * 1000) + (int)(timeout->tv_usec / 1000);
+ }
+
+ return poll(&fds, 1, timeout_milliseconds);
+}
+#else /* !USE_POLL */
+# define IOWAIT_SYSCALL "select"
+static int
+nogvl_wait_for(VALUE th, rb_io_t *fptr, short events, struct timeval *timeout)
+{
+ VALUE scheduler = rb_fiber_scheduler_current_for_thread(th);
+ if (scheduler != Qnil) {
+ struct fiber_scheduler_wait_for_arguments args = {.scheduler = scheduler, .fptr = fptr, .events = events};
+ rb_thread_call_with_gvl(fiber_scheduler_wait_for, &args);
+ return RTEST(args.result);
+ }
+
+ int fd = fptr->fd;
+
+ if (fd == -1) {
+ errno = EBADF;
+ return -1;
+ }
+
+ rb_fdset_t fds;
+ int ret;
+
+ rb_fd_init(&fds);
+ rb_fd_set(fd, &fds);
+
+ switch (events) {
+ case RB_WAITFD_IN:
+ ret = rb_fd_select(fd + 1, &fds, 0, 0, timeout);
+ break;
+ case RB_WAITFD_OUT:
+ ret = rb_fd_select(fd + 1, 0, &fds, 0, timeout);
+ break;
+ default:
+ VM_UNREACHABLE(nogvl_wait_for);
+ }
+
+ rb_fd_term(&fds);
+
+ // On timeout, this returns 0.
+ return ret;
}
+#endif /* !USE_POLL */
static int
maygvl_copy_stream_wait_read(int has_gvl, struct copy_stream_struct *stp)
@@ -9847,15 +12666,18 @@ maygvl_copy_stream_wait_read(int has_gvl, struct copy_stream_struct *stp)
int ret;
do {
- rb_fd_zero(&stp->fds);
- rb_fd_set(stp->src_fd, &stp->fds);
- ret = maygvl_select(has_gvl, rb_fd_max(&stp->fds), &stp->fds, NULL, NULL, NULL);
- } while (ret == -1 && maygvl_copy_stream_continue_p(has_gvl, stp));
+ if (has_gvl) {
+ ret = RB_NUM2INT(rb_io_wait(stp->src, RB_INT2NUM(RUBY_IO_READABLE), Qnil));
+ }
+ else {
+ ret = nogvl_wait_for(stp->th, stp->src_fptr, RB_WAITFD_IN, NULL);
+ }
+ } while (ret < 0 && maygvl_copy_stream_continue_p(has_gvl, stp));
- if (ret == -1) {
- stp->syserr = "select";
+ if (ret < 0) {
+ stp->syserr = IOWAIT_SYSCALL;
stp->error_no = errno;
- return -1;
+ return ret;
}
return 0;
}
@@ -9866,19 +12688,203 @@ nogvl_copy_stream_wait_write(struct copy_stream_struct *stp)
int ret;
do {
- rb_fd_zero(&stp->fds);
- rb_fd_set(stp->dst_fd, &stp->fds);
- ret = rb_fd_select(rb_fd_max(&stp->fds), NULL, &stp->fds, NULL, NULL);
- } while (ret == -1 && maygvl_copy_stream_continue_p(0, stp));
+ ret = nogvl_wait_for(stp->th, stp->dst_fptr, RB_WAITFD_OUT, NULL);
+ } while (ret < 0 && maygvl_copy_stream_continue_p(0, stp));
- if (ret == -1) {
- stp->syserr = "select";
+ if (ret < 0) {
+ stp->syserr = IOWAIT_SYSCALL;
stp->error_no = errno;
- return -1;
+ return ret;
}
return 0;
}
+#ifdef USE_COPY_FILE_RANGE
+
+static ssize_t
+simple_copy_file_range(int in_fd, rb_off_t *in_offset, int out_fd, rb_off_t *out_offset, size_t count, unsigned int flags)
+{
+#ifdef HAVE_COPY_FILE_RANGE
+ return copy_file_range(in_fd, in_offset, out_fd, out_offset, count, flags);
+#else
+ return syscall(__NR_copy_file_range, in_fd, in_offset, out_fd, out_offset, count, flags);
+#endif
+}
+
+static int
+nogvl_copy_file_range(struct copy_stream_struct *stp)
+{
+ ssize_t ss;
+ rb_off_t src_size;
+ rb_off_t copy_length, src_offset, *src_offset_ptr;
+
+ if (!S_ISREG(stp->src_stat.st_mode))
+ return 0;
+
+ src_size = stp->src_stat.st_size;
+ src_offset = stp->src_offset;
+ if (src_offset >= (rb_off_t)0) {
+ src_offset_ptr = &src_offset;
+ }
+ else {
+ src_offset_ptr = NULL; /* if src_offset_ptr is NULL, then bytes are read from in_fd starting from the file offset */
+ }
+
+ copy_length = stp->copy_length;
+ if (copy_length < (rb_off_t)0) {
+ if (src_offset < (rb_off_t)0) {
+ rb_off_t current_offset;
+ errno = 0;
+ current_offset = lseek(stp->src_fptr->fd, 0, SEEK_CUR);
+ if (current_offset < (rb_off_t)0 && errno) {
+ stp->syserr = "lseek";
+ stp->error_no = errno;
+ return (int)current_offset;
+ }
+ copy_length = src_size - current_offset;
+ }
+ else {
+ copy_length = src_size - src_offset;
+ }
+ }
+
+ retry_copy_file_range:
+# if SIZEOF_OFF_T > SIZEOF_SIZE_T
+ /* we are limited by the 32-bit ssize_t return value on 32-bit */
+ ss = (copy_length > (rb_off_t)SSIZE_MAX) ? SSIZE_MAX : (ssize_t)copy_length;
+# else
+ ss = (ssize_t)copy_length;
+# endif
+ ss = simple_copy_file_range(stp->src_fptr->fd, src_offset_ptr, stp->dst_fptr->fd, NULL, ss, 0);
+ if (0 < ss) {
+ stp->total += ss;
+ copy_length -= ss;
+ if (0 < copy_length) {
+ goto retry_copy_file_range;
+ }
+ }
+ if (ss < 0) {
+ if (maygvl_copy_stream_continue_p(0, stp)) {
+ goto retry_copy_file_range;
+ }
+ switch (errno) {
+ case EINVAL:
+ case EPERM: /* copy_file_range(2) doesn't exist (may happen in
+ docker container) */
+#ifdef ENOSYS
+ case ENOSYS:
+#endif
+#ifdef EXDEV
+ case EXDEV: /* in_fd and out_fd are not on the same filesystem */
+#endif
+ return 0;
+ case EAGAIN:
+#if EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
+#endif
+ {
+ int ret = nogvl_copy_stream_wait_write(stp);
+ if (ret < 0) return ret;
+ }
+ goto retry_copy_file_range;
+ case EBADF:
+ {
+ int e = errno;
+ int flags = fcntl(stp->dst_fptr->fd, F_GETFL);
+
+ if (flags != -1 && flags & O_APPEND) {
+ return 0;
+ }
+ errno = e;
+ }
+ }
+ stp->syserr = "copy_file_range";
+ stp->error_no = errno;
+ return (int)ss;
+ }
+ return 1;
+}
+#endif
+
+#ifdef HAVE_FCOPYFILE
+static int
+nogvl_fcopyfile(struct copy_stream_struct *stp)
+{
+ rb_off_t cur, ss = 0;
+ const rb_off_t src_offset = stp->src_offset;
+ int ret;
+
+ if (stp->copy_length >= (rb_off_t)0) {
+ /* copy_length can't be specified in fcopyfile(3) */
+ return 0;
+ }
+
+ if (!S_ISREG(stp->src_stat.st_mode))
+ return 0;
+
+ if (!S_ISREG(stp->dst_stat.st_mode))
+ return 0;
+ if (lseek(stp->dst_fptr->fd, 0, SEEK_CUR) > (rb_off_t)0) /* if dst IO was already written */
+ return 0;
+ if (fcntl(stp->dst_fptr->fd, F_GETFL) & O_APPEND) {
+ /* fcopyfile(3) appends src IO to dst IO and then truncates
+ * dst IO to src IO's original size. */
+ rb_off_t end = lseek(stp->dst_fptr->fd, 0, SEEK_END);
+ lseek(stp->dst_fptr->fd, 0, SEEK_SET);
+ if (end > (rb_off_t)0) return 0;
+ }
+
+ if (src_offset > (rb_off_t)0) {
+ rb_off_t r;
+
+ /* get current offset */
+ errno = 0;
+ cur = lseek(stp->src_fptr->fd, 0, SEEK_CUR);
+ if (cur < (rb_off_t)0 && errno) {
+ stp->error_no = errno;
+ return 1;
+ }
+
+ errno = 0;
+ r = lseek(stp->src_fptr->fd, src_offset, SEEK_SET);
+ if (r < (rb_off_t)0 && errno) {
+ stp->error_no = errno;
+ return 1;
+ }
+ }
+
+ stp->copyfile_state = copyfile_state_alloc(); /* this will be freed by copy_stream_finalize() */
+ ret = fcopyfile(stp->src_fptr->fd, stp->dst_fptr->fd, stp->copyfile_state, COPYFILE_DATA);
+ copyfile_state_get(stp->copyfile_state, COPYFILE_STATE_COPIED, &ss); /* get copied bytes */
+
+ if (ret == 0) { /* success */
+ stp->total = ss;
+ if (src_offset > (rb_off_t)0) {
+ rb_off_t r;
+ errno = 0;
+ /* reset offset */
+ r = lseek(stp->src_fptr->fd, cur, SEEK_SET);
+ if (r < (rb_off_t)0 && errno) {
+ stp->error_no = errno;
+ return 1;
+ }
+ }
+ }
+ else {
+ switch (errno) {
+ case ENOTSUP:
+ case EPERM:
+ case EINVAL:
+ return 0;
+ }
+ stp->syserr = "fcopyfile";
+ stp->error_no = errno;
+ return (int)ret;
+ }
+ return 1;
+}
+#endif
+
#ifdef HAVE_SENDFILE
# ifdef __linux__
@@ -9889,7 +12895,7 @@ nogvl_copy_stream_wait_write(struct copy_stream_struct *stp)
# endif
static ssize_t
-simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
+simple_sendfile(int out_fd, int in_fd, rb_off_t *offset, rb_off_t count)
{
return sendfile(out_fd, in_fd, offset, (size_t)count);
}
@@ -9900,28 +12906,24 @@ simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
*/
# define USE_SENDFILE
-# ifdef HAVE_SYS_UIO_H
-# include <sys/uio.h>
-# endif
-
static ssize_t
-simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
+simple_sendfile(int out_fd, int in_fd, rb_off_t *offset, rb_off_t count)
{
int r;
- off_t pos = offset ? *offset : lseek(in_fd, 0, SEEK_CUR);
- off_t sbytes;
+ rb_off_t pos = offset ? *offset : lseek(in_fd, 0, SEEK_CUR);
+ rb_off_t sbytes;
# ifdef __APPLE__
r = sendfile(in_fd, out_fd, pos, &count, NULL, 0);
sbytes = count;
# else
r = sendfile(in_fd, out_fd, pos, (size_t)count, NULL, &sbytes, 0);
# endif
- if (r != 0 && sbytes == 0) return -1;
+ if (r != 0 && sbytes == 0) return r;
if (offset) {
- *offset += sbytes;
+ *offset += sbytes;
}
else {
- lseek(in_fd, sbytes, SEEK_CUR);
+ lseek(in_fd, sbytes, SEEK_CUR);
}
return (ssize_t)sbytes;
}
@@ -9934,64 +12936,53 @@ simple_sendfile(int out_fd, int in_fd, off_t *offset, off_t count)
static int
nogvl_copy_stream_sendfile(struct copy_stream_struct *stp)
{
- struct stat src_stat, dst_stat;
ssize_t ss;
- int ret;
-
- off_t copy_length;
- off_t src_offset;
+ rb_off_t src_size;
+ rb_off_t copy_length;
+ rb_off_t src_offset;
int use_pread;
- ret = fstat(stp->src_fd, &src_stat);
- if (ret == -1) {
- stp->syserr = "fstat";
- stp->error_no = errno;
- return -1;
- }
- if (!S_ISREG(src_stat.st_mode))
+ if (!S_ISREG(stp->src_stat.st_mode))
return 0;
- ret = fstat(stp->dst_fd, &dst_stat);
- if (ret == -1) {
- stp->syserr = "fstat";
- stp->error_no = errno;
- return -1;
- }
- if ((dst_stat.st_mode & S_IFMT) != S_IFSOCK)
+ src_size = stp->src_stat.st_size;
+#ifndef __linux__
+ if ((stp->dst_stat.st_mode & S_IFMT) != S_IFSOCK)
return 0;
+#endif
src_offset = stp->src_offset;
- use_pread = src_offset != (off_t)-1;
+ use_pread = src_offset >= (rb_off_t)0;
copy_length = stp->copy_length;
- if (copy_length == (off_t)-1) {
+ if (copy_length < (rb_off_t)0) {
if (use_pread)
- copy_length = src_stat.st_size - src_offset;
+ copy_length = src_size - src_offset;
else {
- off_t cur;
+ rb_off_t cur;
errno = 0;
- cur = lseek(stp->src_fd, 0, SEEK_CUR);
- if (cur == (off_t)-1 && errno) {
+ cur = lseek(stp->src_fptr->fd, 0, SEEK_CUR);
+ if (cur < (rb_off_t)0 && errno) {
stp->syserr = "lseek";
stp->error_no = errno;
- return -1;
+ return (int)cur;
}
- copy_length = src_stat.st_size - cur;
+ copy_length = src_size - cur;
}
}
retry_sendfile:
# if SIZEOF_OFF_T > SIZEOF_SIZE_T
/* we are limited by the 32-bit ssize_t return value on 32-bit */
- ss = (copy_length > (off_t)SSIZE_MAX) ? SSIZE_MAX : (ssize_t)copy_length;
+ ss = (copy_length > (rb_off_t)SSIZE_MAX) ? SSIZE_MAX : (ssize_t)copy_length;
# else
ss = (ssize_t)copy_length;
# endif
if (use_pread) {
- ss = simple_sendfile(stp->dst_fd, stp->src_fd, &src_offset, ss);
+ ss = simple_sendfile(stp->dst_fptr->fd, stp->src_fptr->fd, &src_offset, ss);
}
else {
- ss = simple_sendfile(stp->dst_fd, stp->src_fd, NULL, ss);
+ ss = simple_sendfile(stp->dst_fptr->fd, stp->src_fptr->fd, NULL, ss);
}
if (0 < ss) {
stp->total += ss;
@@ -10000,90 +12991,94 @@ nogvl_copy_stream_sendfile(struct copy_stream_struct *stp)
goto retry_sendfile;
}
}
- if (ss == -1) {
- if (maygvl_copy_stream_continue_p(0, stp))
- goto retry_sendfile;
+ if (ss < 0) {
+ if (maygvl_copy_stream_continue_p(0, stp))
+ goto retry_sendfile;
switch (errno) {
- case EINVAL:
+ case EINVAL:
#ifdef ENOSYS
- case ENOSYS:
+ case ENOSYS:
+#endif
+#ifdef EOPNOTSUP
+ /* some RedHat kernels may return EOPNOTSUP on an NFS mount.
+ see also: [Feature #16965] */
+ case EOPNOTSUP:
#endif
return 0;
- case EAGAIN:
-#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
- case EWOULDBLOCK:
+ case EAGAIN:
+#if EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
#endif
+ {
+ int ret;
#ifndef __linux__
- /*
- * Linux requires stp->src_fd to be a mmap-able (regular) file,
- * select() reports regular files to always be "ready", so
- * there is no need to select() on it.
- * Other OSes may have the same limitation for sendfile() which
- * allow us to bypass maygvl_copy_stream_wait_read()...
- */
- if (maygvl_copy_stream_wait_read(0, stp) == -1)
- return -1;
-#endif
- if (nogvl_copy_stream_wait_write(stp) == -1)
- return -1;
+ /*
+ * Linux requires stp->src_fptr->fd to be a mmap-able (regular) file,
+ * select() reports regular files to always be "ready", so
+ * there is no need to select() on it.
+ * Other OSes may have the same limitation for sendfile() which
+ * allow us to bypass maygvl_copy_stream_wait_read()...
+ */
+ ret = maygvl_copy_stream_wait_read(0, stp);
+ if (ret < 0) return ret;
+#endif
+ ret = nogvl_copy_stream_wait_write(stp);
+ if (ret < 0) return ret;
+ }
goto retry_sendfile;
}
stp->syserr = "sendfile";
stp->error_no = errno;
- return -1;
+ return (int)ss;
}
return 1;
}
#endif
static ssize_t
-maygvl_read(int has_gvl, int fd, void *buf, size_t count)
+maygvl_read(int has_gvl, rb_io_t *fptr, void *buf, size_t count)
{
if (has_gvl)
- return rb_read_internal(fd, buf, count);
+ return rb_io_read_memory(fptr, buf, count);
else
- return read(fd, buf, count);
+ return read(fptr->fd, buf, count);
}
static ssize_t
-maygvl_copy_stream_read(int has_gvl, struct copy_stream_struct *stp, char *buf, size_t len, off_t offset)
+maygvl_copy_stream_read(int has_gvl, struct copy_stream_struct *stp, char *buf, size_t len, rb_off_t offset)
{
ssize_t ss;
retry_read:
- if (offset == (off_t)-1) {
- ss = maygvl_read(has_gvl, stp->src_fd, buf, len);
+ if (offset < (rb_off_t)0) {
+ ss = maygvl_read(has_gvl, stp->src_fptr, buf, len);
}
else {
-#ifdef HAVE_PREAD
- ss = pread(stp->src_fd, buf, len, offset);
-#else
- stp->notimp = "pread";
- return -1;
-#endif
+ ss = pread(stp->src_fptr->fd, buf, len, offset);
}
if (ss == 0) {
return 0;
}
- if (ss == -1) {
- if (maygvl_copy_stream_continue_p(has_gvl, stp))
- goto retry_read;
+ if (ss < 0) {
+ if (maygvl_copy_stream_continue_p(has_gvl, stp))
+ goto retry_read;
switch (errno) {
- case EAGAIN:
-#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
- case EWOULDBLOCK:
+ case EAGAIN:
+#if EWOULDBLOCK != EAGAIN
+ case EWOULDBLOCK:
#endif
- if (maygvl_copy_stream_wait_read(has_gvl, stp) == -1)
- return -1;
+ {
+ int ret = maygvl_copy_stream_wait_read(has_gvl, stp);
+ if (ret < 0) return ret;
+ }
goto retry_read;
#ifdef ENOSYS
- case ENOSYS:
-#endif
+ case ENOSYS:
stp->notimp = "pread";
- return -1;
+ return ss;
+#endif
}
- stp->syserr = offset == (off_t)-1 ? "read" : "pread";
+ stp->syserr = offset < (rb_off_t)0 ? "read" : "pread";
stp->error_no = errno;
- return -1;
}
return ss;
}
@@ -10094,18 +13089,18 @@ nogvl_copy_stream_write(struct copy_stream_struct *stp, char *buf, size_t len)
ssize_t ss;
int off = 0;
while (len) {
- ss = write(stp->dst_fd, buf+off, len);
- if (ss == -1) {
- if (maygvl_copy_stream_continue_p(0, stp))
- continue;
- if (errno == EAGAIN || errno == EWOULDBLOCK) {
- if (nogvl_copy_stream_wait_write(stp) == -1)
- return -1;
+ ss = write(stp->dst_fptr->fd, buf+off, len);
+ if (ss < 0) {
+ if (maygvl_copy_stream_continue_p(0, stp))
+ continue;
+ if (io_again_p(errno)) {
+ int ret = nogvl_copy_stream_wait_write(stp);
+ if (ret < 0) return ret;
continue;
}
stp->syserr = "write";
stp->error_no = errno;
- return -1;
+ return (int)ss;
}
off += (int)ss;
len -= (int)ss;
@@ -10121,31 +13116,31 @@ nogvl_copy_stream_read_write(struct copy_stream_struct *stp)
size_t len;
ssize_t ss;
int ret;
- off_t copy_length;
+ rb_off_t copy_length;
+ rb_off_t src_offset;
int use_eof;
- off_t src_offset;
int use_pread;
copy_length = stp->copy_length;
- use_eof = copy_length == (off_t)-1;
+ use_eof = copy_length < (rb_off_t)0;
src_offset = stp->src_offset;
- use_pread = src_offset != (off_t)-1;
+ use_pread = src_offset >= (rb_off_t)0;
if (use_pread && stp->close_src) {
- off_t r;
- errno = 0;
- r = lseek(stp->src_fd, src_offset, SEEK_SET);
- if (r == (off_t)-1 && errno) {
+ rb_off_t r;
+ errno = 0;
+ r = lseek(stp->src_fptr->fd, src_offset, SEEK_SET);
+ if (r < (rb_off_t)0 && errno) {
stp->syserr = "lseek";
stp->error_no = errno;
return;
}
- src_offset = (off_t)-1;
+ src_offset = (rb_off_t)-1;
use_pread = 0;
}
while (use_eof || 0 < copy_length) {
- if (!use_eof && copy_length < (off_t)sizeof(buf)) {
+ if (!use_eof && copy_length < (rb_off_t)sizeof(buf)) {
len = (size_t)copy_length;
}
else {
@@ -10157,7 +13152,7 @@ nogvl_copy_stream_read_write(struct copy_stream_struct *stp)
src_offset += ss;
}
else {
- ss = maygvl_copy_stream_read(0, stp, buf, len, (off_t)-1);
+ ss = maygvl_copy_stream_read(0, stp, buf, len, (rb_off_t)-1);
}
if (ss <= 0) /* EOF or error */
return;
@@ -10175,10 +13170,22 @@ static void *
nogvl_copy_stream_func(void *arg)
{
struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
-#ifdef USE_SENDFILE
+#if defined(USE_SENDFILE) || defined(USE_COPY_FILE_RANGE) || defined(HAVE_FCOPYFILE)
int ret;
#endif
+#ifdef USE_COPY_FILE_RANGE
+ ret = nogvl_copy_file_range(stp);
+ if (ret != 0)
+ goto finish; /* error or success */
+#endif
+
+#ifdef HAVE_FCOPYFILE
+ ret = nogvl_fcopyfile(stp);
+ if (ret != 0)
+ goto finish; /* error or success */
+#endif
+
#ifdef USE_SENDFILE
ret = nogvl_copy_stream_sendfile(stp);
if (ret != 0)
@@ -10187,7 +13194,7 @@ nogvl_copy_stream_func(void *arg)
nogvl_copy_stream_read_write(stp);
-#ifdef USE_SENDFILE
+#if defined(USE_SENDFILE) || defined(USE_COPY_FILE_RANGE) || defined(HAVE_FCOPYFILE)
finish:
#endif
return 0;
@@ -10200,28 +13207,31 @@ copy_stream_fallback_body(VALUE arg)
const int buflen = 16*1024;
VALUE n;
VALUE buf = rb_str_buf_new(buflen);
- off_t rest = stp->copy_length;
- off_t off = stp->src_offset;
+ rb_off_t rest = stp->copy_length;
+ rb_off_t off = stp->src_offset;
ID read_method = id_readpartial;
- if (stp->src_fd == -1) {
- if (!rb_respond_to(stp->src, read_method)) {
- read_method = id_read;
- }
+ if (!stp->src_fptr) {
+ if (!rb_respond_to(stp->src, read_method)) {
+ read_method = id_read;
+ }
}
while (1) {
long numwrote;
long l;
- if (stp->copy_length == (off_t)-1) {
+ rb_str_make_independent(buf);
+ if (stp->copy_length < (rb_off_t)0) {
l = buflen;
}
else {
- if (rest == 0)
+ if (rest == 0) {
+ rb_str_resize(buf, 0);
break;
+ }
l = buflen < rest ? buflen : (long)rest;
}
- if (stp->src_fd == -1) {
+ if (!stp->src_fptr) {
VALUE rc = rb_funcall(stp->src, read_method, 2, INT2FIX(l), buf);
if (read_method == id_read && NIL_P(rc))
@@ -10231,21 +13241,21 @@ copy_stream_fallback_body(VALUE arg)
ssize_t ss;
rb_str_resize(buf, buflen);
ss = maygvl_copy_stream_read(1, stp, RSTRING_PTR(buf), l, off);
- if (ss == -1)
+ rb_str_resize(buf, ss > 0 ? ss : 0);
+ if (ss < 0)
return Qnil;
if (ss == 0)
rb_eof_error();
- rb_str_resize(buf, ss);
- if (off != (off_t)-1)
+ if (off >= (rb_off_t)0)
off += ss;
}
n = rb_io_write(stp->dst, buf);
numwrote = NUM2LONG(n);
stp->total += numwrote;
rest -= numwrote;
- if (read_method == id_read && RSTRING_LEN(buf) == 0) {
- break;
- }
+ if (read_method == id_read && RSTRING_LEN(buf) == 0) {
+ break;
+ }
}
return Qnil;
@@ -10254,11 +13264,11 @@ copy_stream_fallback_body(VALUE arg)
static VALUE
copy_stream_fallback(struct copy_stream_struct *stp)
{
- if (stp->src_fd == -1 && stp->src_offset != (off_t)-1) {
- rb_raise(rb_eArgError, "cannot specify src_offset for non-IO");
+ if (!stp->src_fptr && stp->src_offset >= (rb_off_t)0) {
+ rb_raise(rb_eArgError, "cannot specify src_offset for non-IO");
}
rb_rescue2(copy_stream_fallback_body, (VALUE)stp,
- (VALUE (*) (ANYARGS))0, (VALUE)0,
+ (VALUE (*) (VALUE, VALUE))0, (VALUE)0,
rb_eEOFError, (VALUE)0);
return Qnil;
}
@@ -10268,13 +13278,11 @@ copy_stream_body(VALUE arg)
{
struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
VALUE src_io = stp->src, dst_io = stp->dst;
- rb_io_t *src_fptr = 0, *dst_fptr = 0;
- int src_fd, dst_fd;
const int common_oflags = 0
#ifdef O_NOCTTY
- | O_NOCTTY
+ | O_NOCTTY
#endif
- ;
+ ;
stp->th = rb_thread_current();
@@ -10284,36 +13292,51 @@ copy_stream_body(VALUE arg)
!(RB_TYPE_P(src_io, T_FILE) ||
RB_TYPE_P(src_io, T_STRING) ||
rb_respond_to(src_io, rb_intern("to_path")))) {
- src_fd = -1;
+ stp->src_fptr = NULL;
}
else {
- if (!RB_TYPE_P(src_io, T_FILE)) {
+ int stat_ret;
+ VALUE tmp_io = rb_io_check_io(src_io);
+ if (!NIL_P(tmp_io)) {
+ src_io = tmp_io;
+ }
+ else if (!RB_TYPE_P(src_io, T_FILE)) {
VALUE args[2];
- FilePathValue(src_io);
- args[0] = src_io;
- args[1] = INT2NUM(O_RDONLY|common_oflags);
+ FilePathValue(src_io);
+ args[0] = src_io;
+ args[1] = INT2NUM(O_RDONLY|common_oflags);
src_io = rb_class_new_instance(2, args, rb_cFile);
stp->src = src_io;
stp->close_src = 1;
}
- GetOpenFile(src_io, src_fptr);
- rb_io_check_byte_readable(src_fptr);
- src_fd = src_fptr->fd;
+ RB_IO_POINTER(src_io, stp->src_fptr);
+ rb_io_check_byte_readable(stp->src_fptr);
+
+ stat_ret = fstat(stp->src_fptr->fd, &stp->src_stat);
+ if (stat_ret < 0) {
+ stp->syserr = "fstat";
+ stp->error_no = errno;
+ return Qnil;
+ }
}
- stp->src_fd = src_fd;
if (dst_io == argf ||
!(RB_TYPE_P(dst_io, T_FILE) ||
RB_TYPE_P(dst_io, T_STRING) ||
rb_respond_to(dst_io, rb_intern("to_path")))) {
- dst_fd = -1;
+ stp->dst_fptr = NULL;
}
else {
- if (!RB_TYPE_P(dst_io, T_FILE)) {
+ int stat_ret;
+ VALUE tmp_io = rb_io_check_io(dst_io);
+ if (!NIL_P(tmp_io)) {
+ dst_io = GetWriteIO(tmp_io);
+ }
+ else if (!RB_TYPE_P(dst_io, T_FILE)) {
VALUE args[3];
- FilePathValue(dst_io);
- args[0] = dst_io;
- args[1] = INT2NUM(O_WRONLY|O_CREAT|O_TRUNC|common_oflags);
+ FilePathValue(dst_io);
+ args[0] = dst_io;
+ args[1] = INT2NUM(O_WRONLY|O_CREAT|O_TRUNC|common_oflags);
args[2] = INT2FIX(0666);
dst_io = rb_class_new_instance(3, args, rb_cFile);
stp->dst = dst_io;
@@ -10323,54 +13346,57 @@ copy_stream_body(VALUE arg)
dst_io = GetWriteIO(dst_io);
stp->dst = dst_io;
}
- GetOpenFile(dst_io, dst_fptr);
- rb_io_check_writable(dst_fptr);
- dst_fd = dst_fptr->fd;
+ RB_IO_POINTER(dst_io, stp->dst_fptr);
+ rb_io_check_writable(stp->dst_fptr);
+
+ stat_ret = fstat(stp->dst_fptr->fd, &stp->dst_stat);
+ if (stat_ret < 0) {
+ stp->syserr = "fstat";
+ stp->error_no = errno;
+ return Qnil;
+ }
}
- stp->dst_fd = dst_fd;
#ifdef O_BINARY
- if (src_fptr)
- SET_BINARY_MODE_WITH_SEEK_CUR(src_fptr);
+ if (stp->src_fptr)
+ SET_BINARY_MODE_WITH_SEEK_CUR(stp->src_fptr);
#endif
- if (dst_fptr)
- io_ascii8bit_binmode(dst_fptr);
+ if (stp->dst_fptr)
+ io_ascii8bit_binmode(stp->dst_fptr);
- if (stp->src_offset == (off_t)-1 && src_fptr && src_fptr->rbuf.len) {
- size_t len = src_fptr->rbuf.len;
+ if (stp->src_offset < (rb_off_t)0 && stp->src_fptr && stp->src_fptr->rbuf.len) {
+ size_t len = stp->src_fptr->rbuf.len;
VALUE str;
- if (stp->copy_length != (off_t)-1 && stp->copy_length < (off_t)len) {
+ if (stp->copy_length >= (rb_off_t)0 && stp->copy_length < (rb_off_t)len) {
len = (size_t)stp->copy_length;
}
str = rb_str_buf_new(len);
rb_str_resize(str,len);
- read_buffered_data(RSTRING_PTR(str), len, src_fptr);
- if (dst_fptr) { /* IO or filename */
- if (io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str), dst_fptr, 0) < 0)
- rb_sys_fail(0);
+ read_buffered_data(RSTRING_PTR(str), len, stp->src_fptr);
+ if (stp->dst_fptr) { /* IO or filename */
+ if (io_binwrite(RSTRING_PTR(str), RSTRING_LEN(str), stp->dst_fptr, 0) < 0)
+ rb_sys_fail_on_write(stp->dst_fptr);
}
else /* others such as StringIO */
- rb_io_write(dst_io, str);
+ rb_io_write(dst_io, str);
+ rb_str_resize(str, 0);
stp->total += len;
- if (stp->copy_length != (off_t)-1)
+ if (stp->copy_length >= (rb_off_t)0)
stp->copy_length -= len;
}
- if (dst_fptr && io_fflush(dst_fptr) < 0) {
- rb_raise(rb_eIOError, "flush failed");
+ if (stp->dst_fptr && io_fflush(stp->dst_fptr) < 0) {
+ rb_raise(rb_eIOError, "flush failed");
}
if (stp->copy_length == 0)
return Qnil;
- if (src_fd == -1 || dst_fd == -1) {
+ if (stp->src_fptr == NULL || stp->dst_fptr == NULL) {
return copy_stream_fallback(stp);
}
- rb_fd_set(src_fd, &stp->fds);
- rb_fd_set(dst_fd, &stp->fds);
-
- rb_thread_call_without_gvl(nogvl_copy_stream_func, (void*)stp, RUBY_UBF_IO, 0);
+ IO_WITHOUT_GVL(nogvl_copy_stream_func, stp);
return Qnil;
}
@@ -10378,49 +13404,77 @@ static VALUE
copy_stream_finalize(VALUE arg)
{
struct copy_stream_struct *stp = (struct copy_stream_struct *)arg;
+
+#ifdef HAVE_FCOPYFILE
+ if (stp->copyfile_state) {
+ copyfile_state_free(stp->copyfile_state);
+ }
+#endif
+
if (stp->close_src) {
rb_io_close_m(stp->src);
}
if (stp->close_dst) {
rb_io_close_m(stp->dst);
}
- rb_fd_term(&stp->fds);
if (stp->syserr) {
- errno = stp->error_no;
- rb_sys_fail(stp->syserr);
+ rb_syserr_fail(stp->error_no, stp->syserr);
}
if (stp->notimp) {
- rb_raise(rb_eNotImpError, "%s() not implemented", stp->notimp);
+ rb_raise(rb_eNotImpError, "%s() not implemented", stp->notimp);
}
return Qnil;
}
/*
* call-seq:
- * IO.copy_stream(src, dst)
- * IO.copy_stream(src, dst, copy_length)
- * IO.copy_stream(src, dst, copy_length, src_offset)
+ * IO.copy_stream(src, dst, src_length = nil, src_offset = 0) -> integer
+ *
+ * Copies from the given +src+ to the given +dst+,
+ * returning the number of bytes copied.
+ *
+ * - The given +src+ must be one of the following:
+ *
+ * - The path to a readable file, from which source data is to be read.
+ * - An \IO-like object, opened for reading and capable of responding
+ * to method +:readpartial+ or method +:read+.
+ *
+ * - The given +dst+ must be one of the following:
+ *
+ * - The path to a writable file, to which data is to be written.
+ * - An \IO-like object, opened for writing and capable of responding
+ * to method +:write+.
+ *
+ * The examples here use file <tt>t.txt</tt> as source:
*
- * IO.copy_stream copies <i>src</i> to <i>dst</i>.
- * <i>src</i> and <i>dst</i> is either a filename or an IO.
+ * File.read('t.txt')
+ * # => "First line\nSecond line\n\nThird line\nFourth line\n"
+ * File.read('t.txt').size # => 47
*
- * This method returns the number of bytes copied.
+ * If only arguments +src+ and +dst+ are given,
+ * the entire source stream is copied:
*
- * If optional arguments are not given,
- * the start position of the copy is
- * the beginning of the filename or
- * the current file offset of the IO.
- * The end position of the copy is the end of file.
+ * # Paths.
+ * IO.copy_stream('t.txt', 't.tmp') # => 47
*
- * If <i>copy_length</i> is given,
- * No more than <i>copy_length</i> bytes are copied.
+ * # IOs (recall that a File is also an IO).
+ * src_io = File.open('t.txt', 'r') # => #<File:t.txt>
+ * dst_io = File.open('t.tmp', 'w') # => #<File:t.tmp>
+ * IO.copy_stream(src_io, dst_io) # => 47
+ * src_io.close
+ * dst_io.close
*
- * If <i>src_offset</i> is given,
- * it specifies the start position of the copy.
+ * With argument +src_length+ a non-negative integer,
+ * no more than that many bytes are copied:
*
- * When <i>src_offset</i> is specified and
- * <i>src</i> is an IO,
- * IO.copy_stream doesn't move the current file offset.
+ * IO.copy_stream('t.txt', 't.tmp', 10) # => 10
+ * File.read('t.tmp') # => "First line"
+ *
+ * With argument +src_offset+ also given,
+ * the source stream is read beginning at that offset:
+ *
+ * IO.copy_stream('t.txt', 't.tmp', 11, 11) # => 11
+ * IO.read('t.tmp') # => "Second line"
*
*/
static VALUE
@@ -10436,17 +13490,19 @@ rb_io_s_copy_stream(int argc, VALUE *argv, VALUE io)
st.src = src;
st.dst = dst;
+ st.src_fptr = NULL;
+ st.dst_fptr = NULL;
+
if (NIL_P(length))
- st.copy_length = (off_t)-1;
+ st.copy_length = (rb_off_t)-1;
else
st.copy_length = NUM2OFFT(length);
if (NIL_P(src_offset))
- st.src_offset = (off_t)-1;
+ st.src_offset = (rb_off_t)-1;
else
st.src_offset = NUM2OFFT(src_offset);
- rb_fd_init(&st.fds);
rb_ensure(copy_stream_body, (VALUE)&st, copy_stream_finalize, (VALUE)&st);
return OFFT2NUM(st.total);
@@ -10454,64 +13510,80 @@ rb_io_s_copy_stream(int argc, VALUE *argv, VALUE io)
/*
* call-seq:
- * io.external_encoding -> encoding
+ * external_encoding -> encoding or nil
+ *
+ * Returns the Encoding object that represents the encoding of the stream,
+ * or +nil+ if the stream is in write mode and no encoding is specified.
+ *
+ * See {Encodings}[rdoc-ref:File@Encodings].
*
- * Returns the Encoding object that represents the encoding of the file.
- * If io is write mode and no encoding is specified, returns <code>nil</code>.
*/
static VALUE
rb_io_external_encoding(VALUE io)
{
- rb_io_t *fptr;
+ rb_io_t *fptr = RFILE(rb_io_taint_check(io))->fptr;
- GetOpenFile(io, fptr);
if (fptr->encs.enc2) {
- return rb_enc_from_encoding(fptr->encs.enc2);
+ return rb_enc_from_encoding(fptr->encs.enc2);
}
if (fptr->mode & FMODE_WRITABLE) {
- if (fptr->encs.enc)
- return rb_enc_from_encoding(fptr->encs.enc);
- return Qnil;
+ if (fptr->encs.enc)
+ return rb_enc_from_encoding(fptr->encs.enc);
+ return Qnil;
}
return rb_enc_from_encoding(io_read_encoding(fptr));
}
/*
* call-seq:
- * io.internal_encoding -> encoding
+ * internal_encoding -> encoding or nil
+ *
+ * Returns the Encoding object that represents the encoding of the internal string,
+ * if conversion is specified,
+ * or +nil+ otherwise.
+ *
+ * See {Encodings}[rdoc-ref:File@Encodings].
*
- * Returns the Encoding of the internal string if conversion is
- * specified. Otherwise returns nil.
*/
static VALUE
rb_io_internal_encoding(VALUE io)
{
- rb_io_t *fptr;
+ rb_io_t *fptr = RFILE(rb_io_taint_check(io))->fptr;
- GetOpenFile(io, fptr);
if (!fptr->encs.enc2) return Qnil;
return rb_enc_from_encoding(io_read_encoding(fptr));
}
/*
* call-seq:
- * io.set_encoding(ext_enc) -> io
- * io.set_encoding("ext_enc:int_enc") -> io
- * io.set_encoding(ext_enc, int_enc) -> io
- * io.set_encoding("ext_enc:int_enc", opt) -> io
- * io.set_encoding(ext_enc, int_enc, opt) -> io
+ * set_encoding(ext_enc) -> self
+ * set_encoding(ext_enc, int_enc, **enc_opts) -> self
+ * set_encoding('ext_enc:int_enc', **enc_opts) -> self
+ *
+ * See {Encodings}[rdoc-ref:File@Encodings].
+ *
+ * Argument +ext_enc+, if given, must be an Encoding object
+ * or a String with the encoding name;
+ * it is assigned as the encoding for the stream.
+ *
+ * Argument +int_enc+, if given, must be an Encoding object
+ * or a String with the encoding name;
+ * it is assigned as the encoding for the internal string.
+ *
+ * Argument <tt>'ext_enc:int_enc'</tt>, if given, is a string
+ * containing two colon-separated encoding names;
+ * corresponding Encoding objects are assigned as the external
+ * and internal encodings for the stream.
+ *
+ * If the external encoding of a string is binary/ASCII-8BIT,
+ * the internal encoding of the string is set to nil, since no
+ * transcoding is needed.
+ *
+ * Optional keyword arguments +enc_opts+ specify
+ * {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options].
*
- * If single argument is specified, read string from io is tagged
- * with the encoding specified. If encoding is a colon separated two
- * encoding names "A:B", the read string is converted from encoding A
- * (external encoding) to encoding B (internal encoding), then tagged
- * with B. If two arguments are specified, those must be encoding
- * objects or encoding names, and the first one is the external encoding, and the
- * second one is the internal encoding.
- * If the external encoding and the internal encoding is specified,
- * optional hash argument specify the conversion option.
*/
static VALUE
@@ -10521,7 +13593,7 @@ rb_io_set_encoding(int argc, VALUE *argv, VALUE io)
VALUE v1, v2, opt;
if (!RB_TYPE_P(io, T_FILE)) {
- return rb_funcall2(io, id_set_encoding, argc, argv);
+ return forward(io, id_set_encoding, argc, argv);
}
argc = rb_scan_args(argc, argv, "11:", &v1, &v2, &opt);
@@ -10533,26 +13605,54 @@ rb_io_set_encoding(int argc, VALUE *argv, VALUE io)
void
rb_stdio_set_default_encoding(void)
{
- extern VALUE rb_stdin, rb_stdout, rb_stderr;
VALUE val = Qnil;
+#ifdef _WIN32
+ if (isatty(fileno(stdin))) {
+ rb_encoding *external = rb_locale_encoding();
+ rb_encoding *internal = rb_default_internal_encoding();
+ if (!internal) internal = rb_default_external_encoding();
+ io_encoding_set(RFILE(rb_stdin)->fptr,
+ rb_enc_from_encoding(external),
+ rb_enc_from_encoding(internal),
+ Qnil);
+ }
+ else
+#endif
rb_io_set_encoding(1, &val, rb_stdin);
rb_io_set_encoding(1, &val, rb_stdout);
rb_io_set_encoding(1, &val, rb_stderr);
}
+static inline int
+global_argf_p(VALUE arg)
+{
+ return arg == argf;
+}
+
+typedef VALUE (*argf_encoding_func)(VALUE io);
+
+static VALUE
+argf_encoding(VALUE argf, argf_encoding_func func)
+{
+ if (!RTEST(ARGF.current_file)) {
+ return rb_enc_default_external();
+ }
+ return func(rb_io_check_io(ARGF.current_file));
+}
+
/*
* call-seq:
* ARGF.external_encoding -> encoding
*
- * Returns the external encoding for files read from +ARGF+ as an +Encoding+
+ * Returns the external encoding for files read from ARGF as an Encoding
* object. The external encoding is the encoding of the text as stored in a
- * file. Contrast with +ARGF.internal_encoding+, which is the encoding used
- * to represent this text within Ruby.
+ * file. Contrast with ARGF.internal_encoding, which is the encoding used to
+ * represent this text within Ruby.
*
- * To set the external encoding use +ARGF.set_encoding+.
+ * To set the external encoding use ARGF.set_encoding.
*
- * For example:
+ * For example:
*
* ARGF.external_encoding #=> #<Encoding:UTF-8>
*
@@ -10560,32 +13660,26 @@ rb_stdio_set_default_encoding(void)
static VALUE
argf_external_encoding(VALUE argf)
{
- if (!RTEST(ARGF.current_file)) {
- return rb_enc_from_encoding(rb_default_external_encoding());
- }
- return rb_io_external_encoding(rb_io_check_io(ARGF.current_file));
+ return argf_encoding(argf, rb_io_external_encoding);
}
/*
* call-seq:
* ARGF.internal_encoding -> encoding
*
- * Returns the internal encoding for strings read from +ARGF+ as an
- * +Encoding+ object.
+ * Returns the internal encoding for strings read from ARGF as an
+ * Encoding object.
*
- * If +ARGF.set_encoding+ has been called with two encoding names, the second
+ * If ARGF.set_encoding has been called with two encoding names, the second
* is returned. Otherwise, if +Encoding.default_external+ has been set, that
* value is returned. Failing that, if a default external encoding was
* specified on the command-line, that value is used. If the encoding is
- * unknown, nil is returned.
+ * unknown, +nil+ is returned.
*/
static VALUE
argf_internal_encoding(VALUE argf)
{
- if (!RTEST(ARGF.current_file)) {
- return rb_enc_from_encoding(rb_default_external_encoding());
- }
- return rb_io_internal_encoding(rb_io_check_io(ARGF.current_file));
+ return argf_encoding(argf, rb_io_internal_encoding);
}
/*
@@ -10609,8 +13703,8 @@ argf_internal_encoding(VALUE argf)
* specifies the internal encoding.
*
* If the external encoding and the internal encoding are specified, the
- * optional +Hash+ argument can be used to adjust the conversion process. The
- * structure of this hash is explained in the +String#encode+ documentation.
+ * optional Hash argument can be used to adjust the conversion process. The
+ * structure of this hash is explained in the String#encode documentation.
*
* For example:
*
@@ -10625,11 +13719,12 @@ argf_set_encoding(int argc, VALUE *argv, VALUE argf)
rb_io_t *fptr;
if (!next_argv()) {
- rb_raise(rb_eArgError, "no stream to set encoding");
+ rb_raise(rb_eArgError, "no stream to set encoding");
}
rb_io_set_encoding(argc, argv, ARGF.current_file);
GetOpenFile(ARGF.current_file, fptr);
ARGF.encs = fptr->encs;
+ RB_OBJ_WRITTEN(argf, Qundef, ARGF.encs.ecopts);
return argf;
}
@@ -10638,7 +13733,7 @@ argf_set_encoding(int argc, VALUE *argv, VALUE argf)
* ARGF.tell -> Integer
* ARGF.pos -> Integer
*
- * Returns the current offset (in bytes) of the current file in +ARGF+.
+ * Returns the current offset (in bytes) of the current file in ARGF.
*
* ARGF.pos #=> 0
* ARGF.gets #=> "This is line one\n"
@@ -10649,7 +13744,7 @@ static VALUE
argf_tell(VALUE argf)
{
if (!next_argv()) {
- rb_raise(rb_eArgError, "no stream to tell");
+ rb_raise(rb_eArgError, "no stream to tell");
}
ARGF_FORWARD(0, 0);
return rb_io_tell(ARGF.current_file);
@@ -10657,16 +13752,16 @@ argf_tell(VALUE argf)
/*
* call-seq:
- * ARGF.seek(amount, whence=IO::SEEK_SET) -> 0
+ * ARGF.seek(amount, whence=IO::SEEK_SET) -> 0
*
- * Seeks to offset _amount_ (an +Integer+) in the +ARGF+ stream according to
- * the value of _whence_. See +IO#seek+ for further details.
+ * Seeks to offset _amount_ (an Integer) in the ARGF stream according to
+ * the value of _whence_. See IO#seek for further details.
*/
static VALUE
argf_seek_m(int argc, VALUE *argv, VALUE argf)
{
if (!next_argv()) {
- rb_raise(rb_eArgError, "no stream to seek");
+ rb_raise(rb_eArgError, "no stream to seek");
}
ARGF_FORWARD(argc, argv);
return rb_io_seek_m(argc, argv, ARGF.current_file);
@@ -10676,7 +13771,7 @@ argf_seek_m(int argc, VALUE *argv, VALUE argf)
* call-seq:
* ARGF.pos = position -> Integer
*
- * Seeks to the position given by _position_ (in bytes) in +ARGF+.
+ * Seeks to the position given by _position_ (in bytes) in ARGF.
*
* For example:
*
@@ -10687,7 +13782,7 @@ static VALUE
argf_set_pos(VALUE argf, VALUE offset)
{
if (!next_argv()) {
- rb_raise(rb_eArgError, "no stream to set position");
+ rb_raise(rb_eArgError, "no stream to set position");
}
ARGF_FORWARD(1, &offset);
return rb_io_set_pos(ARGF.current_file, offset);
@@ -10698,7 +13793,7 @@ argf_set_pos(VALUE argf, VALUE offset)
* ARGF.rewind -> 0
*
* Positions the current file to the beginning of input, resetting
- * +ARGF.lineno+ to zero.
+ * ARGF.lineno to zero.
*
* ARGF.readline #=> "This is line one\n"
* ARGF.rewind #=> 0
@@ -10708,20 +13803,28 @@ argf_set_pos(VALUE argf, VALUE offset)
static VALUE
argf_rewind(VALUE argf)
{
+ VALUE ret;
+ int old_lineno;
+
if (!next_argv()) {
- rb_raise(rb_eArgError, "no stream to rewind");
+ rb_raise(rb_eArgError, "no stream to rewind");
}
ARGF_FORWARD(0, 0);
- return rb_io_rewind(ARGF.current_file);
+ old_lineno = RFILE(ARGF.current_file)->fptr->lineno;
+ ret = rb_io_rewind(ARGF.current_file);
+ if (!global_argf_p(argf)) {
+ ARGF.last_lineno = ARGF.lineno -= old_lineno;
+ }
+ return ret;
}
/*
* call-seq:
- * ARGF.fileno -> fixnum
- * ARGF.to_i -> fixnum
+ * ARGF.fileno -> integer
+ * ARGF.to_i -> integer
*
* Returns an integer representing the numeric file descriptor for
- * the current file. Raises an +ArgumentError+ if there isn't a current file.
+ * the current file. Raises an ArgumentError if there isn't a current file.
*
* ARGF.fileno #=> 3
*/
@@ -10729,7 +13832,7 @@ static VALUE
argf_fileno(VALUE argf)
{
if (!next_argv()) {
- rb_raise(rb_eArgError, "no stream");
+ rb_raise(rb_eArgError, "no stream");
}
ARGF_FORWARD(0, 0);
return rb_io_fileno(ARGF.current_file);
@@ -10739,8 +13842,8 @@ argf_fileno(VALUE argf)
* call-seq:
* ARGF.to_io -> IO
*
- * Returns an +IO+ object representing the current file. This will be a
- * +File+ object unless the current file is a stream such as STDIN.
+ * Returns an IO object representing the current file. This will be a
+ * File object unless the current file is a stream such as STDIN.
*
* For example:
*
@@ -10760,8 +13863,8 @@ argf_to_io(VALUE argf)
* ARGF.eof? -> true or false
* ARGF.eof -> true or false
*
- * Returns true if the current file in +ARGF+ is at end of file, i.e. it has
- * no data to read. The stream must be opened for reading or an +IOError+
+ * Returns true if the current file in ARGF is at end of file, i.e. it has
+ * no data to read. The stream must be opened for reading or an IOError
* will be raised.
*
* $ echo "eof" | ruby argf.rb
@@ -10778,12 +13881,12 @@ argf_eof(VALUE argf)
{
next_argv();
if (RTEST(ARGF.current_file)) {
- if (ARGF.init_p == 0) return Qtrue;
- next_argv();
- ARGF_FORWARD(0, 0);
- if (rb_io_eof(ARGF.current_file)) {
- return Qtrue;
- }
+ if (ARGF.init_p == 0) return Qtrue;
+ next_argv();
+ ARGF_FORWARD(0, 0);
+ if (rb_io_eof(ARGF.current_file)) {
+ return Qtrue;
+ }
}
return Qfalse;
}
@@ -10797,22 +13900,28 @@ argf_eof(VALUE argf)
* called without arguments the contents of this pseudo file are returned in
* their entirety.
*
- * _length_ must be a non-negative integer or nil. If it is a positive
- * integer, +read+ tries to read at most _length_ bytes. It returns nil
- * if an EOF was encountered before anything could be read. Fewer than
- * _length_ bytes may be returned if an EOF is encountered during the read.
+ * _length_ must be a non-negative integer or +nil+.
+ *
+ * If _length_ is a positive integer, +read+ tries to read
+ * _length_ bytes without any conversion (binary mode).
+ * It returns +nil+ if an EOF is encountered before anything can be read.
+ * Fewer than _length_ bytes are returned if an EOF is encountered during
+ * the read.
+ * In the case of an integer _length_, the resulting string is always
+ * in ASCII-8BIT encoding.
*
- * If _length_ is omitted or is _nil_, it reads until EOF. A String is
- * returned even if EOF is encountered before any data is read.
+ * If _length_ is omitted or is +nil+, it reads until EOF
+ * and the encoding conversion is applied, if applicable.
+ * A string is returned even if EOF is encountered before any data is read.
*
- * If _length_ is zero, it returns _""_.
+ * If _length_ is zero, it returns an empty string (<code>""</code>).
*
- * If the optional _outbuf_ argument is present, it must reference a String,
- * which will receive the data.
- * The <i>outbuf</i> will contain only the received data after the method call
+ * If the optional _outbuf_ argument is present,
+ * it must reference a String, which will receive the data.
+ * The _outbuf_ will contain only the received data after the method call
* even if it is not empty at the beginning.
*
- * For example:
+ * For example:
*
* $ echo "small" > small.txt
* $ echo "large" > large.txt
@@ -10823,8 +13932,11 @@ argf_eof(VALUE argf)
* ARGF.read(2) #=> "sm"
* ARGF.read(0) #=> ""
*
- * Note that this method behaves like fread() function in C. If you need the
- * behavior like read(2) system call, consider +ARGF.readpartial+.
+ * Note that this method behaves like the fread() function in C.
+ * This means it retries to invoke read(2) system calls to read data
+ * with the specified length.
+ * If you need the behavior like a single read(2) system call,
+ * consider ARGF#readpartial or ARGF#read_nonblock.
*/
static VALUE
@@ -10835,39 +13947,39 @@ argf_read(int argc, VALUE *argv, VALUE argf)
rb_scan_args(argc, argv, "02", &length, &str);
if (!NIL_P(length)) {
- len = NUM2LONG(argv[0]);
+ len = NUM2LONG(argv[0]);
}
if (!NIL_P(str)) {
- StringValue(str);
- rb_str_resize(str,0);
- argv[1] = Qnil;
+ StringValue(str);
+ rb_str_resize(str,0);
+ argv[1] = Qnil;
}
retry:
if (!next_argv()) {
- return str;
+ return str;
}
if (ARGF_GENERIC_INPUT_P()) {
- tmp = argf_forward(argc, argv, argf);
+ tmp = argf_forward(argc, argv, argf);
}
else {
- tmp = io_read(argc, argv, ARGF.current_file);
+ tmp = io_read(argc, argv, ARGF.current_file);
}
if (NIL_P(str)) str = tmp;
else if (!NIL_P(tmp)) rb_str_append(str, tmp);
if (NIL_P(tmp) || NIL_P(length)) {
- if (ARGF.next_p != -1) {
- argf_close(argf);
- ARGF.next_p = 1;
- goto retry;
- }
+ if (ARGF.next_p != -1) {
+ argf_close(argf);
+ ARGF.next_p = 1;
+ goto retry;
+ }
}
else if (argc >= 1) {
- if (RSTRING_LEN(str) < len) {
- len -= RSTRING_LEN(str);
- argv[0] = INT2NUM(len);
- goto retry;
- }
+ long slen = RSTRING_LEN(str);
+ if (slen < len) {
+ argv[0] = LONG2NUM(len - slen);
+ goto retry;
+ }
}
return str;
}
@@ -10886,7 +13998,8 @@ argf_forward_call(VALUE arg)
return Qnil;
}
-static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock);
+static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts,
+ int nonblock);
/*
* call-seq:
@@ -10897,27 +14010,27 @@ static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock);
*
* If the optional _outbuf_ argument is present,
* it must reference a String, which will receive the data.
- * The <i>outbuf</i> will contain only the received data after the method call
+ * The _outbuf_ will contain only the received data after the method call
* even if it is not empty at the beginning.
*
- * It raises <code>EOFError</code> on end of ARGF stream.
+ * It raises EOFError on end of ARGF stream.
* Since ARGF stream is a concatenation of multiple files,
* internally EOF is occur for each file.
* ARGF.readpartial returns empty strings for EOFs except the last one and
- * raises <code>EOFError</code> for the last one.
+ * raises EOFError for the last one.
*
*/
static VALUE
argf_readpartial(int argc, VALUE *argv, VALUE argf)
{
- return argf_getpartial(argc, argv, argf, 0);
+ return argf_getpartial(argc, argv, argf, Qnil, 0);
}
/*
* call-seq:
- * ARGF.read_nonblock(maxlen) -> string
- * ARGF.read_nonblock(maxlen, outbuf) -> outbuf
+ * ARGF.read_nonblock(maxlen[, options]) -> string
+ * ARGF.read_nonblock(maxlen, outbuf[, options]) -> outbuf
*
* Reads at most _maxlen_ bytes from the ARGF stream in non-blocking mode.
*/
@@ -10925,43 +14038,56 @@ argf_readpartial(int argc, VALUE *argv, VALUE argf)
static VALUE
argf_read_nonblock(int argc, VALUE *argv, VALUE argf)
{
- return argf_getpartial(argc, argv, argf, 1);
+ VALUE opts;
+
+ rb_scan_args(argc, argv, "11:", NULL, NULL, &opts);
+
+ if (!NIL_P(opts))
+ argc--;
+
+ return argf_getpartial(argc, argv, argf, opts, 1);
}
static VALUE
-argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock)
+argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts, int nonblock)
{
VALUE tmp, str, length;
+ int no_exception;
rb_scan_args(argc, argv, "11", &length, &str);
if (!NIL_P(str)) {
StringValue(str);
argv[1] = str;
}
+ no_exception = no_exception_p(opts);
if (!next_argv()) {
- rb_str_resize(str, 0);
+ if (!NIL_P(str)) {
+ rb_str_resize(str, 0);
+ }
rb_eof_error();
}
if (ARGF_GENERIC_INPUT_P()) {
- struct argf_call_arg arg;
- arg.argc = argc;
- arg.argv = argv;
- arg.argf = argf;
- tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
- RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
+ VALUE (*const rescue_does_nothing)(VALUE, VALUE) = 0;
+ struct argf_call_arg arg;
+ arg.argc = argc;
+ arg.argv = argv;
+ arg.argf = argf;
+ tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
+ rescue_does_nothing, Qnil, rb_eEOFError, (VALUE)0);
}
else {
- tmp = io_getpartial(argc, argv, ARGF.current_file, nonblock, 0);
+ tmp = io_getpartial(argc, argv, ARGF.current_file, no_exception, nonblock);
}
if (NIL_P(tmp)) {
if (ARGF.next_p == -1) {
- rb_eof_error();
+ return io_nonblock_eof(no_exception);
}
argf_close(argf);
ARGF.next_p = 1;
- if (RARRAY_LEN(ARGF.argv) == 0)
- rb_eof_error();
+ if (RARRAY_LEN(ARGF.argv) == 0) {
+ return io_nonblock_eof(no_exception);
+ }
if (NIL_P(str))
str = rb_str_new(NULL, 0);
return str;
@@ -10973,10 +14099,10 @@ argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock)
* call-seq:
* ARGF.getc -> String or nil
*
- * Reads the next character from +ARGF+ and returns it as a +String+. Returns
+ * Reads the next character from ARGF and returns it as a String. Returns
* +nil+ at the end of the stream.
*
- * +ARGF+ treats the files named on the command line as a single file created
+ * ARGF treats the files named on the command line as a single file created
* by concatenating their contents. After returning the last character of the
* first file, it returns the first character of the second file, and so on.
*
@@ -11000,15 +14126,15 @@ argf_getc(VALUE argf)
retry:
if (!next_argv()) return Qnil;
if (ARGF_GENERIC_INPUT_P()) {
- ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
+ ch = forward_current(rb_intern("getc"), 0, 0);
}
else {
- ch = rb_io_getc(ARGF.current_file);
+ ch = rb_io_getc(ARGF.current_file);
}
if (NIL_P(ch) && ARGF.next_p != -1) {
- argf_close(argf);
- ARGF.next_p = 1;
- goto retry;
+ argf_close(argf);
+ ARGF.next_p = 1;
+ goto retry;
}
return ch;
@@ -11016,9 +14142,9 @@ argf_getc(VALUE argf)
/*
* call-seq:
- * ARGF.getbyte -> Fixnum or nil
+ * ARGF.getbyte -> Integer or nil
*
- * Gets the next 8-bit byte (0..255) from +ARGF+. Returns +nil+ if called at
+ * Gets the next 8-bit byte (0..255) from ARGF. Returns +nil+ if called at
* the end of the stream.
*
* For example:
@@ -11040,15 +14166,15 @@ argf_getbyte(VALUE argf)
retry:
if (!next_argv()) return Qnil;
if (!RB_TYPE_P(ARGF.current_file, T_FILE)) {
- ch = rb_funcall3(ARGF.current_file, rb_intern("getbyte"), 0, 0);
+ ch = forward_current(rb_intern("getbyte"), 0, 0);
}
else {
- ch = rb_io_getbyte(ARGF.current_file);
+ ch = rb_io_getbyte(ARGF.current_file);
}
if (NIL_P(ch) && ARGF.next_p != -1) {
- argf_close(argf);
- ARGF.next_p = 1;
- goto retry;
+ argf_close(argf);
+ ARGF.next_p = 1;
+ goto retry;
}
return ch;
@@ -11058,8 +14184,8 @@ argf_getbyte(VALUE argf)
* call-seq:
* ARGF.readchar -> String or nil
*
- * Reads the next character from +ARGF+ and returns it as a +String+. Raises
- * an +EOFError+ after the last character of the last file has been read.
+ * Reads the next character from ARGF and returns it as a String. Raises
+ * an EOFError after the last character of the last file has been read.
*
* For example:
*
@@ -11080,15 +14206,15 @@ argf_readchar(VALUE argf)
retry:
if (!next_argv()) rb_eof_error();
if (!RB_TYPE_P(ARGF.current_file, T_FILE)) {
- ch = rb_funcall3(ARGF.current_file, rb_intern("getc"), 0, 0);
+ ch = forward_current(rb_intern("getc"), 0, 0);
}
else {
- ch = rb_io_getc(ARGF.current_file);
+ ch = rb_io_getc(ARGF.current_file);
}
if (NIL_P(ch) && ARGF.next_p != -1) {
- argf_close(argf);
- ARGF.next_p = 1;
- goto retry;
+ argf_close(argf);
+ ARGF.next_p = 1;
+ goto retry;
}
return ch;
@@ -11096,10 +14222,10 @@ argf_readchar(VALUE argf)
/*
* call-seq:
- * ARGF.readbyte -> Fixnum
+ * ARGF.readbyte -> Integer
*
- * Reads the next 8-bit byte from ARGF and returns it as a +Fixnum+. Raises
- * an +EOFError+ after the last byte of the last file has been read.
+ * Reads the next 8-bit byte from ARGF and returns it as an Integer. Raises
+ * an EOFError after the last byte of the last file has been read.
*
* For example:
*
@@ -11120,7 +14246,7 @@ argf_readbyte(VALUE argf)
NEXT_ARGF_FORWARD(0, 0);
c = argf_getbyte(argf);
if (NIL_P(c)) {
- rb_eof_error();
+ rb_eof_error();
}
return c;
}
@@ -11133,46 +14259,74 @@ argf_block_call_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, argf))
const VALUE current = ARGF.current_file;
rb_yield_values2(argc, argv);
if (ARGF.init_p == -1 || current != ARGF.current_file) {
- rb_iter_break_value(Qundef);
+ rb_iter_break_value(Qundef);
}
return Qnil;
}
+#define ARGF_block_call(mid, argc, argv, func, argf) \
+ rb_block_call_kw(ARGF.current_file, mid, argc, argv, \
+ func, argf, rb_keyword_given_p())
+
static void
argf_block_call(ID mid, int argc, VALUE *argv, VALUE argf)
{
- VALUE ret = rb_block_call(ARGF.current_file, mid, argc, argv, argf_block_call_i, argf);
- if (ret != Qundef) ARGF.next_p = 1;
+ VALUE ret = ARGF_block_call(mid, argc, argv, argf_block_call_i, argf);
+ if (!UNDEF_P(ret)) ARGF.next_p = 1;
+}
+
+static VALUE
+argf_block_call_line_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, argf))
+{
+ if (!global_argf_p(argf)) {
+ ARGF.last_lineno = ++ARGF.lineno;
+ }
+ return argf_block_call_i(i, argf, argc, argv, blockarg);
+}
+
+static void
+argf_block_call_line(ID mid, int argc, VALUE *argv, VALUE argf)
+{
+ VALUE ret = ARGF_block_call(mid, argc, argv, argf_block_call_line_i, argf);
+ if (!UNDEF_P(ret)) ARGF.next_p = 1;
}
/*
* call-seq:
- * ARGF.each(sep=$/) {|line| block } -> ARGF
- * ARGF.each(sep=$/,limit) {|line| block } -> ARGF
- * ARGF.each(...) -> an_enumerator
+ * ARGF.each(sep=$/) {|line| block } -> ARGF
+ * ARGF.each(sep=$/, limit) {|line| block } -> ARGF
+ * ARGF.each(...) -> an_enumerator
*
- * ARGF.each_line(sep=$/) {|line| block } -> ARGF
- * ARGF.each_line(sep=$/,limit) {|line| block } -> ARGF
- * ARGF.each_line(...) -> an_enumerator
+ * ARGF.each_line(sep=$/) {|line| block } -> ARGF
+ * ARGF.each_line(sep=$/, limit) {|line| block } -> ARGF
+ * ARGF.each_line(...) -> an_enumerator
*
* Returns an enumerator which iterates over each line (separated by _sep_,
* which defaults to your platform's newline character) of each file in
* +ARGV+. If a block is supplied, each line in turn will be yielded to the
* block, otherwise an enumerator is returned.
- * The optional _limit_ argument is a +Fixnum+ specifying the maximum
+ * The optional _limit_ argument is an Integer specifying the maximum
* length of each line; longer lines will be split according to this limit.
*
* This method allows you to treat the files supplied on the command line as
* a single file consisting of the concatenation of each named file. After
* the last line of the first file has been returned, the first line of the
- * second file is returned. The +ARGF.filename+ and +ARGF.lineno+ methods can
- * be used to determine the filename and line number, respectively, of the
- * current line.
+ * second file is returned. The ARGF.filename and ARGF.lineno methods can be
+ * used to determine the filename of the current line and line number of the
+ * whole input, respectively.
*
* For example, the following code prints out each line of each named file
* prefixed with its line number, displaying the filename once per file:
*
- * ARGF.lines do |line|
+ * ARGF.each_line do |line|
+ * puts ARGF.filename if ARGF.file.lineno == 1
+ * puts "#{ARGF.file.lineno}: #{line}"
+ * end
+ *
+ * While the following code prints only the first file's name at first, and
+ * the contents with line number counted through all named files.
+ *
+ * ARGF.each_line do |line|
* puts ARGF.filename if ARGF.lineno == 1
* puts "#{ARGF.lineno}: #{line}"
* end
@@ -11182,44 +14336,28 @@ argf_each_line(int argc, VALUE *argv, VALUE argf)
{
RETURN_ENUMERATOR(argf, argc, argv);
FOREACH_ARGF() {
- argf_block_call(rb_intern("each_line"), argc, argv, argf);
+ argf_block_call_line(rb_intern("each_line"), argc, argv, argf);
}
return argf;
}
/*
- * This is a deprecated alias for <code>each_line</code>.
- */
-
-static VALUE
-argf_lines(int argc, VALUE *argv, VALUE argf)
-{
- rb_warn("ARGF#lines is deprecated; use #each_line instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(argf, ID2SYM(rb_intern("each_line")), argc, argv);
- return argf_each_line(argc, argv, argf);
-}
-
-/*
* call-seq:
- * ARGF.bytes {|byte| block } -> ARGF
- * ARGF.bytes -> an_enumerator
- *
* ARGF.each_byte {|byte| block } -> ARGF
* ARGF.each_byte -> an_enumerator
*
* Iterates over each byte of each file in +ARGV+.
- * A byte is returned as a +Fixnum+ in the range 0..255.
+ * A byte is returned as an Integer in the range 0..255.
*
* This method allows you to treat the files supplied on the command line as
* a single file consisting of the concatenation of each named file. After
* the last byte of the first file has been returned, the first byte of the
- * second file is returned. The +ARGF.filename+ method can be used to
+ * second file is returned. The ARGF.filename method can be used to
* determine the filename of the current byte.
*
* If no block is given, an enumerator is returned instead.
*
- * For example:
+ * For example:
*
* ARGF.bytes.to_a #=> [35, 32, ... 95, 10]
*
@@ -11229,35 +14367,22 @@ argf_each_byte(VALUE argf)
{
RETURN_ENUMERATOR(argf, 0, 0);
FOREACH_ARGF() {
- argf_block_call(rb_intern("each_byte"), 0, 0, argf);
+ argf_block_call(rb_intern("each_byte"), 0, 0, argf);
}
return argf;
}
/*
- * This is a deprecated alias for <code>each_byte</code>.
- */
-
-static VALUE
-argf_bytes(VALUE argf)
-{
- rb_warn("ARGF#bytes is deprecated; use #each_byte instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(argf, ID2SYM(rb_intern("each_byte")), 0, 0);
- return argf_each_byte(argf);
-}
-
-/*
* call-seq:
- * ARGF.each_char {|char| block } -> ARGF
- * ARGF.each_char -> an_enumerator
+ * ARGF.each_char {|char| block } -> ARGF
+ * ARGF.each_char -> an_enumerator
*
- * Iterates over each character of each file in +ARGF+.
+ * Iterates over each character of each file in ARGF.
*
* This method allows you to treat the files supplied on the command line as
* a single file consisting of the concatenation of each named file. After
* the last character of the first file has been returned, the first
- * character of the second file is returned. The +ARGF.filename+ method can
+ * character of the second file is returned. The ARGF.filename method can
* be used to determine the name of the file in which the current character
* appears.
*
@@ -11268,35 +14393,22 @@ argf_each_char(VALUE argf)
{
RETURN_ENUMERATOR(argf, 0, 0);
FOREACH_ARGF() {
- argf_block_call(rb_intern("each_char"), 0, 0, argf);
+ argf_block_call(rb_intern("each_char"), 0, 0, argf);
}
return argf;
}
/*
- * This is a deprecated alias for <code>each_char</code>.
- */
-
-static VALUE
-argf_chars(VALUE argf)
-{
- rb_warn("ARGF#chars is deprecated; use #each_char instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(argf, ID2SYM(rb_intern("each_char")), 0, 0);
- return argf_each_char(argf);
-}
-
-/*
* call-seq:
- * ARGF.each_codepoint {|codepoint| block } -> ARGF
- * ARGF.each_codepoint -> an_enumerator
+ * ARGF.each_codepoint {|codepoint| block } -> ARGF
+ * ARGF.each_codepoint -> an_enumerator
*
- * Iterates over each codepoint of each file in +ARGF+.
+ * Iterates over each codepoint of each file in ARGF.
*
* This method allows you to treat the files supplied on the command line as
* a single file consisting of the concatenation of each named file. After
* the last codepoint of the first file has been returned, the first
- * codepoint of the second file is returned. The +ARGF.filename+ method can
+ * codepoint of the second file is returned. The ARGF.filename method can
* be used to determine the name of the file in which the current codepoint
* appears.
*
@@ -11307,25 +14419,12 @@ argf_each_codepoint(VALUE argf)
{
RETURN_ENUMERATOR(argf, 0, 0);
FOREACH_ARGF() {
- argf_block_call(rb_intern("each_codepoint"), 0, 0, argf);
+ argf_block_call(rb_intern("each_codepoint"), 0, 0, argf);
}
return argf;
}
/*
- * This is a deprecated alias for <code>each_codepoint</code>.
- */
-
-static VALUE
-argf_codepoints(VALUE argf)
-{
- rb_warn("ARGF#codepoints is deprecated; use #each_codepoint instead");
- if (!rb_block_given_p())
- return rb_enumeratorize(argf, ID2SYM(rb_intern("each_codepoint")), 0, 0);
- return argf_each_codepoint(argf);
-}
-
-/*
* call-seq:
* ARGF.filename -> String
* ARGF.path -> String
@@ -11364,8 +14463,8 @@ argf_filename_getter(ID id, VALUE *var)
* call-seq:
* ARGF.file -> IO or File object
*
- * Returns the current file as an +IO+ or +File+ object. #<IO:<STDIN>> is
- * returned when the current file is STDIN.
+ * Returns the current file as an IO or File object.
+ * <code>$stdin</code> is returned when the current file is STDIN.
*
* For example:
*
@@ -11389,7 +14488,7 @@ argf_file(VALUE argf)
* call-seq:
* ARGF.binmode -> ARGF
*
- * Puts +ARGF+ into binary mode. Once a stream is in binary mode, it cannot
+ * Puts ARGF into binary mode. Once a stream is in binary mode, it cannot
* be reset to non-binary mode. This option has the following effects:
*
* * Newline conversion is disabled.
@@ -11410,10 +14509,10 @@ argf_binmode_m(VALUE argf)
* call-seq:
* ARGF.binmode? -> true or false
*
- * Returns true if +ARGF+ is being read in binary mode; false otherwise. (To
- * enable binary mode use +ARGF.binmode+.
+ * Returns true if ARGF is being read in binary mode; false otherwise.
+ * To enable binary mode use ARGF.binmode.
*
- * For example:
+ * For example:
*
* ARGF.binmode? #=> false
* ARGF.binmode
@@ -11422,7 +14521,7 @@ argf_binmode_m(VALUE argf)
static VALUE
argf_binmode_p(VALUE argf)
{
- return ARGF.binmode ? Qtrue : Qfalse;
+ return RBOOL(ARGF.binmode);
}
/*
@@ -11432,7 +14531,7 @@ argf_binmode_p(VALUE argf)
* Sets the current file to the next file in ARGV. If there aren't any more
* files it has no effect.
*
- * For example:
+ * For example:
*
* $ ruby argf.rb foo bar
* ARGF.filename #=> "foo"
@@ -11443,8 +14542,8 @@ static VALUE
argf_skip(VALUE argf)
{
if (ARGF.init_p && ARGF.next_p == 0) {
- argf_close(argf);
- ARGF.next_p = 1;
+ argf_close(argf);
+ ARGF.next_p = 1;
}
return argf;
}
@@ -11453,11 +14552,11 @@ argf_skip(VALUE argf)
* call-seq:
* ARGF.close -> ARGF
*
- * Closes the current file and skips to the next in the stream. Trying to
- * close a file that has already been closed causes an +IOError+ to be
- * raised.
+ * Closes the current file and skips to the next file in ARGV. If there are
+ * no more files to open, just closes the current file. STDIN will not be
+ * closed.
*
- * For example:
+ * For example:
*
* $ ruby argf.rb foo bar
*
@@ -11465,7 +14564,6 @@ argf_skip(VALUE argf)
* ARGF.close
* ARGF.filename #=> "bar"
* ARGF.close
- * ARGF.close #=> closed stream (IOError)
*/
static VALUE
argf_close_m(VALUE argf)
@@ -11473,7 +14571,7 @@ argf_close_m(VALUE argf)
next_argv();
argf_close(argf);
if (ARGF.next_p != -1) {
- ARGF.next_p = 1;
+ ARGF.next_p = 1;
}
ARGF.lineno = 0;
return argf;
@@ -11484,14 +14582,14 @@ argf_close_m(VALUE argf)
* ARGF.closed? -> true or false
*
* Returns _true_ if the current file has been closed; _false_ otherwise. Use
- * +ARGF.close+ to actually close the current file.
+ * ARGF.close to actually close the current file.
*/
static VALUE
argf_closed(VALUE argf)
{
next_argv();
ARGF_FORWARD(0, 0);
- return rb_io_closed(ARGF.current_file);
+ return rb_io_closed_p(ARGF.current_file);
}
/*
@@ -11510,15 +14608,16 @@ argf_to_s(VALUE argf)
* call-seq:
* ARGF.inplace_mode -> String
*
- * Returns the file extension appended to the names of modified files under
- * inplace-edit mode. This value can be set using +ARGF.inplace_mode=+ or
- * passing the +-i+ switch to the Ruby binary.
+ * Returns the file extension appended to the names of backup copies of
+ * modified files under in-place edit mode. This value can be set using
+ * ARGF.inplace_mode= or passing the +-i+ switch to the Ruby binary.
*/
static VALUE
argf_inplace_mode_get(VALUE argf)
{
if (!ARGF.inplace) return Qnil;
- return rb_str_new2(ARGF.inplace);
+ if (NIL_P(ARGF.inplace)) return rb_str_new(0, 0);
+ return rb_str_dup(ARGF.inplace);
}
static VALUE
@@ -11531,37 +14630,34 @@ opt_i_get(ID id, VALUE *var)
* call-seq:
* ARGF.inplace_mode = ext -> ARGF
*
- * Sets the filename extension for inplace editing mode to the given String.
- * Each file being edited has this value appended to its filename. The
- * modified file is saved under this new name.
+ * Sets the filename extension for in-place editing mode to the given String.
+ * The backup copy of each file being edited has this value appended to its
+ * filename.
*
* For example:
*
* $ ruby argf.rb file.txt
*
* ARGF.inplace_mode = '.bak'
- * ARGF.lines do |line|
+ * ARGF.each_line do |line|
* print line.sub("foo","bar")
* end
*
- * Each line of _file.txt_ has the first occurrence of "foo" replaced with
- * "bar", then the new line is written out to _file.txt.bak_.
+ * First, _file.txt.bak_ is created as a backup copy of _file.txt_.
+ * Then, each line of _file.txt_ has the first occurrence of "foo" replaced with
+ * "bar".
*/
static VALUE
argf_inplace_mode_set(VALUE argf, VALUE val)
{
- if (rb_safe_level() >= 1 && OBJ_TAINTED(val))
- rb_insecure_operation();
-
if (!RTEST(val)) {
- if (ARGF.inplace) free(ARGF.inplace);
- ARGF.inplace = 0;
+ ARGF.inplace = Qfalse;
+ }
+ else if (StringValueCStr(val), !RSTRING_LEN(val)) {
+ ARGF.inplace = Qnil;
}
else {
- StringValue(val);
- if (ARGF.inplace) free(ARGF.inplace);
- ARGF.inplace = 0;
- ARGF.inplace = strdup(RSTRING_PTR(val));
+ ARGF_SET(inplace, rb_str_new_frozen(val));
}
return argf;
}
@@ -11572,18 +14668,10 @@ opt_i_set(VALUE val, ID id, VALUE *var)
argf_inplace_mode_set(*var, val);
}
-const char *
-ruby_get_inplace_mode(void)
-{
- return ARGF.inplace;
-}
-
void
ruby_set_inplace_mode(const char *suffix)
{
- if (ARGF.inplace) free(ARGF.inplace);
- ARGF.inplace = 0;
- if (suffix) ARGF.inplace = strdup(suffix);
+ ARGF_SET(inplace, !suffix ? Qfalse : !*suffix ? Qnil : rb_str_new(suffix, strlen(suffix)));
}
/*
@@ -11629,66 +14717,85 @@ static VALUE
argf_write_io(VALUE argf)
{
if (!RTEST(ARGF.current_file)) {
- rb_raise(rb_eIOError, "not opened for writing");
+ rb_raise(rb_eIOError, "not opened for writing");
}
return GetWriteIO(ARGF.current_file);
}
/*
* call-seq:
- * ARGF.write(string) -> integer
+ * ARGF.write(*objects) -> integer
*
- * Writes _string_ if inplace mode.
+ * Writes each of the given +objects+ if inplace mode.
*/
static VALUE
-argf_write(VALUE argf, VALUE str)
+argf_write(int argc, VALUE *argv, VALUE argf)
+{
+ return rb_io_writev(argf_write_io(argf), argc, argv);
+}
+
+void
+rb_readwrite_sys_fail(enum rb_io_wait_readwrite waiting, const char *mesg)
{
- return rb_io_write(argf_write_io(argf), str);
+ rb_readwrite_syserr_fail(waiting, errno, mesg);
}
void
-rb_readwrite_sys_fail(int writable, const char *mesg)
+rb_readwrite_syserr_fail(enum rb_io_wait_readwrite waiting, int n, const char *mesg)
{
- VALUE arg;
- int n = errno;
+ VALUE arg, c = Qnil;
arg = mesg ? rb_str_new2(mesg) : Qnil;
- if (writable == RB_IO_WAIT_WRITABLE) {
- switch (n) {
- case EAGAIN:
- rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEAGAINWaitWritable));
- break;
+ switch (waiting) {
+ case RB_IO_WAIT_WRITABLE:
+ switch (n) {
+ case EAGAIN:
+ c = rb_eEAGAINWaitWritable;
+ break;
#if EAGAIN != EWOULDBLOCK
- case EWOULDBLOCK:
- rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEWOULDBLOCKWaitWritable));
- break;
-#endif
- case EINPROGRESS:
- rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEINPROGRESSWaitWritable));
- break;
- default:
- rb_mod_sys_fail_str(rb_mWaitWritable, arg);
- }
- }
- else if (writable == RB_IO_WAIT_READABLE) {
- switch (n) {
- case EAGAIN:
- rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEAGAINWaitReadable));
- break;
+ case EWOULDBLOCK:
+ c = rb_eEWOULDBLOCKWaitWritable;
+ break;
+#endif
+ case EINPROGRESS:
+ c = rb_eEINPROGRESSWaitWritable;
+ break;
+ default:
+ rb_mod_syserr_fail_str(rb_mWaitWritable, n, arg);
+ }
+ break;
+ case RB_IO_WAIT_READABLE:
+ switch (n) {
+ case EAGAIN:
+ c = rb_eEAGAINWaitReadable;
+ break;
#if EAGAIN != EWOULDBLOCK
- case EWOULDBLOCK:
- rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEWOULDBLOCKWaitReadable));
- break;
+ case EWOULDBLOCK:
+ c = rb_eEWOULDBLOCKWaitReadable;
+ break;
#endif
- case EINPROGRESS:
- rb_exc_raise(rb_class_new_instance(1, &arg, rb_eEINPROGRESSWaitReadable));
- break;
- default:
- rb_mod_sys_fail_str(rb_mWaitReadable, arg);
- }
- }
- else {
- rb_bug("invalid read/write type passed to rb_readwrite_sys_fail: %d", writable);
+ case EINPROGRESS:
+ c = rb_eEINPROGRESSWaitReadable;
+ break;
+ default:
+ rb_mod_syserr_fail_str(rb_mWaitReadable, n, arg);
+ }
+ break;
+ default:
+ rb_bug("invalid read/write type passed to rb_readwrite_sys_fail: %d", waiting);
}
+ rb_exc_raise(rb_class_new_instance(1, &arg, c));
+}
+
+static VALUE
+get_LAST_READ_LINE(ID _x, VALUE *_y)
+{
+ return rb_lastline_get();
+}
+
+static void
+set_LAST_READ_LINE(VALUE val, ID _x, VALUE *_y)
+{
+ rb_lastline_set(val);
}
/*
@@ -11702,8 +14809,8 @@ rb_readwrite_sys_fail(int writable, const char *mesg)
* File.open("/etc/hosts") {|f| f.close; f.read }
* #=> IOError: closed stream
*
- * Note that some IO failures raise +SystemCallError+s and these are not
- * subclasses of IOError:
+ * Note that some IO failures raise <code>SystemCallError</code>s
+ * and these are not subclasses of IOError:
*
* File.open("does/not/exist")
* #=> Errno::ENOENT: No such file or directory - does/not/exist
@@ -11716,152 +14823,863 @@ rb_readwrite_sys_fail(int writable, const char *mesg)
* methods exist in two forms,
*
* one that returns +nil+ when the end of file is reached, the other
- * raises EOFError +EOFError+.
+ * raises EOFError.
*
- * +EOFError+ is a subclass of +IOError+.
+ * EOFError is a subclass of IOError.
*
* file = File.open("/etc/hosts")
* file.read
* file.gets #=> nil
* file.readline #=> EOFError: end of file reached
+ * file.close
*/
/*
* Document-class: ARGF
*
- * +ARGF+ is a stream designed for use in scripts that process files given as
- * command-line arguments or passed in via STDIN.
+ * == \ARGF and +ARGV+
+ *
+ * The \ARGF object works with the array at global variable +ARGV+
+ * to make <tt>$stdin</tt> and file streams available in the Ruby program:
+ *
+ * - **ARGV** may be thought of as the <b>argument vector</b> array.
+ *
+ * Initially, it contains the command-line arguments and options
+ * that are passed to the Ruby program;
+ * the program can modify that array as it likes.
+ *
+ * - **ARGF** may be thought of as the <b>argument files</b> object.
+ *
+ * It can access file streams and/or the <tt>$stdin</tt> stream,
+ * based on what it finds in +ARGV+.
+ * This provides a convenient way for the command line
+ * to specify streams for a Ruby program to read.
+ *
+ * == Reading
+ *
+ * \ARGF may read from _source_ streams,
+ * which at any particular time are determined by the content of +ARGV+.
+ *
+ * === Simplest Case
+ *
+ * When the <i>very first</i> \ARGF read occurs with an empty +ARGV+ (<tt>[]</tt>),
+ * the source is <tt>$stdin</tt>:
+ *
+ * - \File +t.rb+:
+ *
+ * p ['ARGV', ARGV]
+ * p ['ARGF.read', ARGF.read]
+ *
+ * - Commands and outputs
+ * (see below for the content of files +foo.txt+ and +bar.txt+):
+ *
+ * $ echo "Open the pod bay doors, Hal." | ruby t.rb
+ * ["ARGV", []]
+ * ["ARGF.read", "Open the pod bay doors, Hal.\n"]
+ *
+ * $ cat foo.txt bar.txt | ruby t.rb
+ * ["ARGV", []]
+ * ["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"]
+ *
+ * === About the Examples
+ *
+ * Many examples here assume the existence of files +foo.txt+ and +bar.txt+:
+ *
+ * $ cat foo.txt
+ * Foo 0
+ * Foo 1
+ * $ cat bar.txt
+ * Bar 0
+ * Bar 1
+ * Bar 2
+ * Bar 3
+ *
+ * === Sources in +ARGV+
+ *
+ * For any \ARGF read _except_ the {simplest case}[rdoc-ref:ARGF@Simplest+Case]
+ * (that is, _except_ for the <i>very first</i> \ARGF read with an empty +ARGV+),
+ * the sources are found in +ARGV+.
+ *
+ * \ARGF assumes that each element in array +ARGV+ is a potential source,
+ * and is one of:
+ *
+ * - The string path to a file that may be opened as a stream.
+ * - The character <tt>'-'</tt>, meaning stream <tt>$stdin</tt>.
+ *
+ * Each element that is _not_ one of these
+ * should be removed from +ARGV+ before \ARGF accesses that source.
+ *
+ * In the following example:
+ *
+ * - Filepaths +foo.txt+ and +bar.txt+ may be retained as potential sources.
+ * - Options <tt>--xyzzy</tt> and <tt>--mojo</tt> should be removed.
+ *
+ * Example:
+ *
+ * - \File +t.rb+:
+ *
+ * # Print arguments (and options, if any) found on command line.
+ * p ['ARGV', ARGV]
+ *
+ * - Command and output:
+ *
+ * $ ruby t.rb --xyzzy --mojo foo.txt bar.txt
+ * ["ARGV", ["--xyzzy", "--mojo", "foo.txt", "bar.txt"]]
+ *
+ * \ARGF's stream access considers the elements of +ARGV+, left to right:
+ *
+ * - \File +t.rb+:
+ *
+ * p "ARGV: #{ARGV}"
+ * p "Read: #{ARGF.read}" # Read everything from all specified streams.
+ *
+ * - Command and output:
+ *
+ * $ ruby t.rb foo.txt bar.txt
+ * "ARGV: [\"foo.txt\", \"bar.txt\"]"
+ * "Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"
+ *
+ * Because the value at +ARGV+ is an ordinary array,
+ * you can manipulate it to control which sources \ARGF considers:
+ *
+ * - If you remove an element from +ARGV+, \ARGF will not consider the corresponding source.
+ * - If you add an element to +ARGV+, \ARGF will consider the corresponding source.
+ *
+ * Each element in +ARGV+ is removed when its corresponding source is accessed;
+ * when all sources have been accessed, the array is empty:
+ *
+ * - \File +t.rb+:
+ *
+ * until ARGV.empty? && ARGF.eof?
+ * p "ARGV: #{ARGV}"
+ * p "Line: #{ARGF.readline}" # Read each line from each specified stream.
+ * end
+ *
+ * - Command and output:
+ *
+ * $ ruby t.rb foo.txt bar.txt
+ * "ARGV: [\"foo.txt\", \"bar.txt\"]"
+ * "Line: Foo 0\n"
+ * "ARGV: [\"bar.txt\"]"
+ * "Line: Foo 1\n"
+ * "ARGV: [\"bar.txt\"]"
+ * "Line: Bar 0\n"
+ * "ARGV: []"
+ * "Line: Bar 1\n"
+ * "ARGV: []"
+ * "Line: Bar 2\n"
+ * "ARGV: []"
+ * "Line: Bar 3\n"
+ *
+ * ==== Filepaths in +ARGV+
+ *
+ * The +ARGV+ array may contain filepaths the specify sources for \ARGF reading.
+ *
+ * This program prints what it reads from files at the paths specified
+ * on the command line:
*
- * The arguments passed to your script are stored in the +ARGV+ Array, one
- * argument per element. +ARGF+ assumes that any arguments that aren't
- * filenames have been removed from +ARGV+. For example:
+ * - \File +t.rb+:
*
- * $ ruby argf.rb --verbose file1 file2
+ * p ['ARGV', ARGV]
+ * # Read and print all content from the specified sources.
+ * p ['ARGF.read', ARGF.read]
*
- * ARGV #=> ["--verbose", "file1", "file2"]
- * option = ARGV.shift #=> "--verbose"
- * ARGV #=> ["file1", "file2"]
+ * - Command and output:
*
- * You can now use +ARGF+ to work with a concatenation of each of these named
- * files. For instance, +ARGF.read+ will return the contents of _file1_
- * followed by the contents of _file2_.
+ * $ ruby t.rb foo.txt bar.txt
+ * ["ARGV", [foo.txt, bar.txt]
+ * ["ARGF.read", "Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"]
*
- * After a file in +ARGV+ has been read +ARGF+ removes it from the Array.
- * Thus, after all files have been read +ARGV+ will be empty.
+ * ==== Specifying <tt>$stdin</tt> in +ARGV+
*
- * You can manipulate +ARGV+ yourself to control what +ARGF+ operates on. If
- * you remove a file from +ARGV+, it is ignored by +ARGF+; if you add files to
- * +ARGV+, they are treated as if they were named on the command line. For
- * example:
+ * To specify stream <tt>$stdin</tt> in +ARGV+, us the character <tt>'-'</tt>:
*
- * ARGV.replace ["file1"]
- * ARGF.readlines # Returns the contents of file1 as an Array
- * ARGV #=> []
- * ARGV.replace ["file2", "file3"]
- * ARGF.read # Returns the contents of file2 and file3
+ * - \File +t.rb+:
*
- * If +ARGV+ is empty, +ARGF+ acts as if it contained STDIN, i.e. the data
- * piped to your script. For example:
+ * p ['ARGV', ARGV]
+ * p ['ARGF.read', ARGF.read]
+ *
+ * - Command and output:
+ *
+ * $ echo "Open the pod bay doors, Hal." | ruby t.rb -
+ * ["ARGV", ["-"]]
+ * ["ARGF.read", "Open the pod bay doors, Hal.\n"]
+ *
+ * When no character <tt>'-'</tt> is given, stream <tt>$stdin</tt> is ignored.
+ *
+ * - Command and output:
+ *
+ * $ echo "Open the pod bay doors, Hal." | ruby t.rb foo.txt bar.txt
+ * "ARGV: [\"foo.txt\", \"bar.txt\"]"
+ * "Read: Foo 0\nFoo 1\nBar 0\nBar 1\nBar 2\nBar 3\n"
+ *
+ * ==== Mixtures and Repetitions in +ARGV+
+ *
+ * For an \ARGF reader, +ARGV+ may contain any mixture of filepaths
+ * and character <tt>'-'</tt>, including repetitions.
+ *
+ * ==== Modifications to +ARGV+
+ *
+ * The running Ruby program may make any modifications to the +ARGV+ array;
+ * the current value of +ARGV+ affects \ARGF reading.
+ *
+ * ==== Empty +ARGV+
+ *
+ * For an empty +ARGV+, an \ARGF read method either returns +nil+
+ * or raises an exception, depending on the specific method.
+ *
+ * === More Read Methods
+ *
+ * As seen above, method ARGF#read reads the content of all sources
+ * into a single string.
+ * Other \ARGF methods provide other ways to access that content;
+ * these include:
+ *
+ * - Byte access: #each_byte, #getbyte, #readbyte.
+ * - Character access: #each_char, #getc, #readchar.
+ * - Codepoint access: #each_codepoint.
+ * - Line access: #each_line, #gets, #readline, #readlines.
+ * - Source access: #read, #read_nonblock, #readpartial.
+ *
+ * === About \Enumerable
+ *
+ * \ARGF includes module Enumerable.
+ * Virtually all methods in \Enumerable call method <tt>#each</tt> in the including class.
+ *
+ * <b>Note well</b>: In \ARGF, method #each returns data from the _sources_,
+ * _not_ from +ARGV+;
+ * therefore, for example, <tt>ARGF#entries</tt> returns an array of lines from the sources,
+ * not an array of the strings from +ARGV+:
+ *
+ * - \File +t.rb+:
+ *
+ * p ['ARGV', ARGV]
+ * p ['ARGF.entries', ARGF.entries]
+ *
+ * - Command and output:
+ *
+ * $ ruby t.rb foo.txt bar.txt
+ * ["ARGV", ["foo.txt", "bar.txt"]]
+ * ["ARGF.entries", ["Foo 0\n", "Foo 1\n", "Bar 0\n", "Bar 1\n", "Bar 2\n", "Bar 3\n"]]
+ *
+ * == Writing
+ *
+ * If <i>inplace mode</i> is in effect,
+ * \ARGF may write to target streams,
+ * which at any particular time are determined by the content of ARGV.
+ *
+ * Methods about inplace mode:
+ *
+ * - #inplace_mode
+ * - #inplace_mode=
+ * - #to_write_io
+ *
+ * Methods for writing:
+ *
+ * - #print
+ * - #printf
+ * - #putc
+ * - #puts
+ * - #write
*
- * $ echo "glark" | ruby -e 'p ARGF.read'
- * "glark\n"
*/
/*
- * The IO class is the basis for all input and output in Ruby.
- * An I/O stream may be <em>duplexed</em> (that is, bidirectional), and
- * so may use more than one native operating system stream.
+ * An instance of class \IO (commonly called a _stream_)
+ * represents an input/output stream in the underlying operating system.
+ * Class \IO is the basis for input and output in Ruby.
*
- * Many of the examples in this section use the File class, the only standard
- * subclass of IO. The two classes are closely associated. Like the File
- * class, the Socket library subclasses from IO (such as TCPSocket or
- * UDPSocket).
+ * Class File is the only class in the Ruby core that is a subclass of \IO.
+ * Some classes in the Ruby standard library are also subclasses of \IO;
+ * these include TCPSocket and UDPSocket.
*
- * The Kernel#open method can create an IO (or File) object for these types
- * of arguments:
+ * The global constant ARGF (also accessible as <tt>$<</tt>)
+ * provides an IO-like stream that allows access to all file paths
+ * found in ARGV (or found in STDIN if ARGV is empty).
+ * ARGF is not itself a subclass of \IO.
*
- * * A plain string represents a filename suitable for the underlying
- * operating system.
+ * Class StringIO provides an IO-like stream that handles a String.
+ * StringIO is not itself a subclass of \IO.
+ *
+ * Important objects based on \IO include:
+ *
+ * - $stdin.
+ * - $stdout.
+ * - $stderr.
+ * - Instances of class File.
+ *
+ * An instance of \IO may be created using:
+ *
+ * - IO.new: returns a new \IO object for the given integer file descriptor.
+ * - IO.open: passes a new \IO object to the given block.
+ * - IO.popen: returns a new \IO object that is connected to the $stdin and $stdout
+ * of a newly-launched subprocess.
+ * - Kernel#open: Returns a new \IO object connected to a given source:
+ * stream, file, or subprocess.
+ *
+ * Like a File stream, an \IO stream has:
+ *
+ * - A read/write mode, which may be read-only, write-only, or read/write;
+ * see {Read/Write Mode}[rdoc-ref:File@ReadWrite+Mode].
+ * - A data mode, which may be text-only or binary;
+ * see {Data Mode}[rdoc-ref:File@Data+Mode].
+ * - Internal and external encodings;
+ * see {Encodings}[rdoc-ref:File@Encodings].
+ *
+ * And like other \IO streams, it has:
+ *
+ * - A position, which determines where in the stream the next
+ * read or write is to occur;
+ * see {Position}[rdoc-ref:IO@Position].
+ * - A line number, which is a special, line-oriented, "position"
+ * (different from the position mentioned above);
+ * see {Line Number}[rdoc-ref:IO@Line+Number].
+ *
+ * == Extension <tt>io/console</tt>
+ *
+ * Extension <tt>io/console</tt> provides numerous methods
+ * for interacting with the console;
+ * requiring it adds numerous methods to class \IO.
+ *
+ * == Example Files
+ *
+ * Many examples here use these variables:
+ *
+ * :include: doc/examples/files.rdoc
+ *
+ * == Open Options
+ *
+ * A number of \IO methods accept optional keyword arguments
+ * that determine how a new stream is to be opened:
+ *
+ * - +:mode+: Stream mode.
+ * - +:flags+: Integer file open flags;
+ * If +mode+ is also given, the two are bitwise-ORed.
+ * - +:external_encoding+: External encoding for the stream.
+ * - +:internal_encoding+: Internal encoding for the stream.
+ * <tt>'-'</tt> is a synonym for the default internal encoding.
+ * If the value is +nil+ no conversion occurs.
+ * - +:encoding+: Specifies external and internal encodings as <tt>'extern:intern'</tt>.
+ * - +:textmode+: If a truthy value, specifies the mode as text-only, binary otherwise.
+ * - +:binmode+: If a truthy value, specifies the mode as binary, text-only otherwise.
+ * - +:autoclose+: If a truthy value, specifies that the +fd+ will close
+ * when the stream closes; otherwise it remains open.
+ * - +:path+: If a string value is provided, it is used in #inspect and is available as
+ * #path method.
+ *
+ * Also available are the options offered in String#encode,
+ * which may control conversion between external and internal encoding.
+ *
+ * == Basic \IO
+ *
+ * You can perform basic stream \IO with these methods,
+ * which typically operate on multi-byte strings:
+ *
+ * - IO#read: Reads and returns some or all of the remaining bytes from the stream.
+ * - IO#write: Writes zero or more strings to the stream;
+ * each given object that is not already a string is converted via +to_s+.
*
- * * A string starting with <code>"|"</code> indicates a subprocess.
- * The remainder of the string following the <code>"|"</code> is
- * invoked as a process with appropriate input/output channels
- * connected to it.
- *
- * * A string equal to <code>"|-"</code> will create another Ruby
- * instance as a subprocess.
- *
- * The IO may be opened with different file modes (read-only, write-only) and
- * encodings for proper conversion. See IO.new for these options. See
- * Kernel#open for details of the various command formats described above.
- *
- * IO.popen, the Open3 library, or Process#spawn may also be used to
- * communicate with subprocesses through an IO.
- *
- * Ruby will convert pathnames between different operating system
- * conventions if possible. For instance, on a Windows system the
- * filename <code>"/gumby/ruby/test.rb"</code> will be opened as
- * <code>"\gumby\ruby\test.rb"</code>. When specifying a Windows-style
- * filename in a Ruby string, remember to escape the backslashes:
- *
- * "c:\\gumby\\ruby\\test.rb"
- *
- * Our examples here will use the Unix-style forward slashes;
- * File::ALT_SEPARATOR can be used to get the platform-specific separator
- * character.
- *
- * The global constant ARGF (also accessible as $<) provides an
- * IO-like stream which allows access to all files mentioned on the
- * command line (or STDIN if no files are mentioned). ARGF#path and its alias
- * ARGF#filename are provided to access the name of the file currently being
- * read.
- *
- * == io/console
- *
- * The io/console extension provides methods for interacting with the
- * console. The console can be accessed from IO.console or the standard
- * input/output/error IO objects.
- *
- * Requiring io/console adds the following methods:
- *
- * * IO::console
- * * IO#raw
- * * IO#raw!
- * * IO#cooked
- * * IO#cooked!
- * * IO#getch
- * * IO#echo=
- * * IO#echo?
- * * IO#noecho
- * * IO#winsize
- * * IO#winsize=
- * * IO#iflush
- * * IO#ioflush
- * * IO#oflush
+ * === Position
+ *
+ * An \IO stream has a nonnegative integer _position_,
+ * which is the byte offset at which the next read or write is to occur.
+ * A new stream has position zero (and line number zero);
+ * method +rewind+ resets the position (and line number) to zero.
+ *
+ * These methods discard {buffers}[rdoc-ref:IO@Buffering] and the
+ * Encoding::Converter instances used for that \IO.
+ *
+ * The relevant methods:
+ *
+ * - IO#tell (aliased as +#pos+): Returns the current position (in bytes) in the stream.
+ * - IO#pos=: Sets the position of the stream to a given integer +new_position+ (in bytes).
+ * - IO#seek: Sets the position of the stream to a given integer +offset+ (in bytes),
+ * relative to a given position +whence+
+ * (indicating the beginning, end, or current position).
+ * - IO#rewind: Positions the stream at the beginning (also resetting the line number).
+ *
+ * === Open and Closed Streams
+ *
+ * A new \IO stream may be open for reading, open for writing, or both.
+ *
+ * A stream is automatically closed when claimed by the garbage collector.
+ *
+ * Attempted reading or writing on a closed stream raises an exception.
+ *
+ * The relevant methods:
+ *
+ * - IO#close: Closes the stream for both reading and writing.
+ * - IO#close_read: Closes the stream for reading.
+ * - IO#close_write: Closes the stream for writing.
+ * - IO#closed?: Returns whether the stream is closed.
+ *
+ * === End-of-Stream
+ *
+ * You can query whether a stream is positioned at its end:
+ *
+ * - IO#eof? (also aliased as +#eof+): Returns whether the stream is at end-of-stream.
+ *
+ * You can reposition to end-of-stream by using method IO#seek:
+ *
+ * f = File.new('t.txt')
+ * f.eof? # => false
+ * f.seek(0, :END)
+ * f.eof? # => true
+ * f.close
+ *
+ * Or by reading all stream content (which is slower than using IO#seek):
+ *
+ * f.rewind
+ * f.eof? # => false
+ * f.read # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * f.eof? # => true
+ *
+ * == Line \IO
+ *
+ * Class \IO supports line-oriented
+ * {input}[rdoc-ref:IO@Line+Input] and {output}[rdoc-ref:IO@Line+Output]
+ *
+ * === Line Input
+ *
+ * Class \IO supports line-oriented input for
+ * {files}[rdoc-ref:IO@File+Line+Input] and {IO streams}[rdoc-ref:IO@Stream+Line+Input]
+ *
+ * ==== \File Line Input
+ *
+ * You can read lines from a file using these methods:
+ *
+ * - IO.foreach: Reads each line and passes it to the given block.
+ * - IO.readlines: Reads and returns all lines in an array.
+ *
+ * For each of these methods:
+ *
+ * - You can specify {open options}[rdoc-ref:IO@Open+Options].
+ * - Line parsing depends on the effective <i>line separator</i>;
+ * see {Line Separator}[rdoc-ref:IO@Line+Separator].
+ * - The length of each returned line depends on the effective <i>line limit</i>;
+ * see {Line Limit}[rdoc-ref:IO@Line+Limit].
+ *
+ * ==== Stream Line Input
+ *
+ * You can read lines from an \IO stream using these methods:
+ *
+ * - IO#each_line: Reads each remaining line, passing it to the given block.
+ * - IO#gets: Returns the next line.
+ * - IO#readline: Like #gets, but raises an exception at end-of-stream.
+ * - IO#readlines: Returns all remaining lines in an array.
+ *
+ * For each of these methods:
+ *
+ * - Reading may begin mid-line,
+ * depending on the stream's _position_;
+ * see {Position}[rdoc-ref:IO@Position].
+ * - Line parsing depends on the effective <i>line separator</i>;
+ * see {Line Separator}[rdoc-ref:IO@Line+Separator].
+ * - The length of each returned line depends on the effective <i>line limit</i>;
+ * see {Line Limit}[rdoc-ref:IO@Line+Limit].
+ *
+ * ===== Line Separator
+ *
+ * Each of the {line input methods}[rdoc-ref:IO@Line+Input] uses a <i>line separator</i>:
+ * the string that determines what is considered a line;
+ * it is sometimes called the <i>input record separator</i>.
+ *
+ * The default line separator is taken from global variable <tt>$/</tt>,
+ * whose initial value is <tt>"\n"</tt>.
+ *
+ * Generally, the line to be read next is all data
+ * from the current {position}[rdoc-ref:IO@Position]
+ * to the next line separator
+ * (but see {Special Line Separator Values}[rdoc-ref:IO@Special+Line+Separator+Values]):
+ *
+ * f = File.new('t.txt')
+ * # Method gets with no sep argument returns the next line, according to $/.
+ * f.gets # => "First line\n"
+ * f.gets # => "Second line\n"
+ * f.gets # => "\n"
+ * f.gets # => "Fourth line\n"
+ * f.gets # => "Fifth line\n"
+ * f.close
+ *
+ * You can use a different line separator by passing argument +sep+:
+ *
+ * f = File.new('t.txt')
+ * f.gets('l') # => "First l"
+ * f.gets('li') # => "ine\nSecond li"
+ * f.gets('lin') # => "ne\n\nFourth lin"
+ * f.gets # => "e\n"
+ * f.close
+ *
+ * Or by setting global variable <tt>$/</tt>:
+ *
+ * f = File.new('t.txt')
+ * $/ = 'l'
+ * f.gets # => "First l"
+ * f.gets # => "ine\nSecond l"
+ * f.gets # => "ine\n\nFourth l"
+ * f.close
+ *
+ * ===== Special Line Separator Values
+ *
+ * Each of the {line input methods}[rdoc-ref:IO@Line+Input]
+ * accepts two special values for parameter +sep+:
+ *
+ * - +nil+: The entire stream is to be read ("slurped") into a single string:
+ *
+ * f = File.new('t.txt')
+ * f.gets(nil) # => "First line\nSecond line\n\nFourth line\nFifth line\n"
+ * f.close
+ *
+ * - <tt>''</tt> (the empty string): The next "paragraph" is to be read
+ * (paragraphs being separated by two consecutive line separators):
+ *
+ * f = File.new('t.txt')
+ * f.gets('') # => "First line\nSecond line\n\n"
+ * f.gets('') # => "Fourth line\nFifth line\n"
+ * f.close
+ *
+ * ===== Line Limit
+ *
+ * Each of the {line input methods}[rdoc-ref:IO@Line+Input]
+ * uses an integer <i>line limit</i>,
+ * which restricts the number of bytes that may be returned.
+ * (A multi-byte character will not be split, and so a returned line may be slightly longer
+ * than the limit).
+ *
+ * The default limit value is <tt>-1</tt>;
+ * any negative limit value means that there is no limit.
+ *
+ * If there is no limit, the line is determined only by +sep+.
+ *
+ * # Text with 1-byte characters.
+ * File.open('t.txt') {|f| f.gets(1) } # => "F"
+ * File.open('t.txt') {|f| f.gets(2) } # => "Fi"
+ * File.open('t.txt') {|f| f.gets(3) } # => "Fir"
+ * File.open('t.txt') {|f| f.gets(4) } # => "Firs"
+ * # No more than one line.
+ * File.open('t.txt') {|f| f.gets(10) } # => "First line"
+ * File.open('t.txt') {|f| f.gets(11) } # => "First line\n"
+ * File.open('t.txt') {|f| f.gets(12) } # => "First line\n"
+ *
+ * # Text with 3-byte characters, which will not be split.
+ * File.read('t.ja') # => "こんにちは"
+ * File.open('t.ja') {|f| f.gets(1).size } # => 1
+ * File.open('t.ja') {|f| f.gets(2).size } # => 1
+ * File.open('t.ja') {|f| f.gets(3).size } # => 1
+ * File.open('t.ja') {|f| f.gets(4).size } # => 2
+ * File.open('t.ja') {|f| f.gets(5).size } # => 2
+ *
+ * ===== Line Separator and Line Limit
+ *
+ * With arguments +sep+ and +limit+ given, combines the two behaviors:
+ *
+ * - Returns the next line as determined by line separator +sep+.
+ * - But returns no more bytes than are allowed by the limit +limit+.
*
* Example:
*
- * require 'io/console'
- * rows, columns = $stdin.winsize
- * puts "Your screen is #{columns} wide and #{rows} tall"
+ * File.open('t.txt') {|f| f.gets('li', 20) } # => "First li"
+ * File.open('t.txt') {|f| f.gets('li', 2) } # => "Fi"
+ *
+ * ===== Line Number
+ *
+ * A readable \IO stream has a non-negative integer <i>line number</i>:
+ *
+ * - IO#lineno: Returns the line number.
+ * - IO#lineno=: Resets and returns the line number.
+ *
+ * Unless modified by a call to method IO#lineno=,
+ * the line number is the number of lines read
+ * by certain line-oriented methods,
+ * according to the effective {line separator}[rdoc-ref:IO@Line+Separator]:
+ *
+ * - IO.foreach: Increments the line number on each call to the block.
+ * - IO#each_line: Increments the line number on each call to the block.
+ * - IO#gets: Increments the line number.
+ * - IO#readline: Increments the line number.
+ * - IO#readlines: Increments the line number for each line read.
+ *
+ * A new stream is initially has line number zero (and position zero);
+ * method +rewind+ resets the line number (and position) to zero:
+ *
+ * f = File.new('t.txt')
+ * f.lineno # => 0
+ * f.gets # => "First line\n"
+ * f.lineno # => 1
+ * f.rewind
+ * f.lineno # => 0
+ * f.close
+ *
+ * Reading lines from a stream usually changes its line number:
+ *
+ * f = File.new('t.txt', 'r')
+ * f.lineno # => 0
+ * f.readline # => "This is line one.\n"
+ * f.lineno # => 1
+ * f.readline # => "This is the second line.\n"
+ * f.lineno # => 2
+ * f.readline # => "Here's the third line.\n"
+ * f.lineno # => 3
+ * f.eof? # => true
+ * f.close
+ *
+ * Iterating over lines in a stream usually changes its line number:
+ *
+ * File.open('t.txt') do |f|
+ * f.each_line do |line|
+ * p "position=#{f.pos} eof?=#{f.eof?} lineno=#{f.lineno}"
+ * end
+ * end
+ *
+ * Output:
+ *
+ * "position=11 eof?=false lineno=1"
+ * "position=23 eof?=false lineno=2"
+ * "position=24 eof?=false lineno=3"
+ * "position=36 eof?=false lineno=4"
+ * "position=47 eof?=true lineno=5"
+ *
+ * Unlike the stream's {position}[rdoc-ref:IO@Position],
+ * the line number does not affect where the next read or write will occur:
+ *
+ * f = File.new('t.txt')
+ * f.lineno = 1000
+ * f.lineno # => 1000
+ * f.gets # => "First line\n"
+ * f.lineno # => 1001
+ * f.close
+ *
+ * Associated with the line number is the global variable <tt>$.</tt>:
+ *
+ * - When a stream is opened, <tt>$.</tt> is not set;
+ * its value is left over from previous activity in the process:
+ *
+ * $. = 41
+ * f = File.new('t.txt')
+ * $. = 41
+ * # => 41
+ * f.close
+ *
+ * - When a stream is read, <tt>$.</tt> is set to the line number for that stream:
+ *
+ * f0 = File.new('t.txt')
+ * f1 = File.new('t.dat')
+ * f0.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
+ * $. # => 5
+ * f1.readlines # => ["\xFE\xFF\x99\x90\x99\x91\x99\x92\x99\x93\x99\x94"]
+ * $. # => 1
+ * f0.close
+ * f1.close
+ *
+ * - Methods IO#rewind and IO#seek do not affect <tt>$.</tt>:
+ *
+ * f = File.new('t.txt')
+ * f.readlines # => ["First line\n", "Second line\n", "\n", "Fourth line\n", "Fifth line\n"]
+ * $. # => 5
+ * f.rewind
+ * f.seek(0, :SET)
+ * $. # => 5
+ * f.close
+ *
+ * === Line Output
+ *
+ * You can write to an \IO stream line-by-line using this method:
+ *
+ * - IO#puts: Writes objects to the stream.
+ *
+ * == Character \IO
+ *
+ * You can process an \IO stream character-by-character using these methods:
+ *
+ * - IO#getc: Reads and returns the next character from the stream.
+ * - IO#readchar: Like #getc, but raises an exception at end-of-stream.
+ * - IO#ungetc: Pushes back ("unshifts") a character or integer onto the stream.
+ * - IO#putc: Writes a character to the stream.
+ * - IO#each_char: Reads each remaining character in the stream,
+ * passing the character to the given block.
+ *
+ * == Byte \IO
+ *
+ * You can process an \IO stream byte-by-byte using these methods:
+ *
+ * - IO#getbyte: Returns the next 8-bit byte as an integer in range 0..255.
+ * - IO#readbyte: Like #getbyte, but raises an exception if at end-of-stream.
+ * - IO#ungetbyte: Pushes back ("unshifts") a byte back onto the stream.
+ * - IO#each_byte: Reads each remaining byte in the stream,
+ * passing the byte to the given block.
+ *
+ * == Codepoint \IO
+ *
+ * You can process an \IO stream codepoint-by-codepoint:
+ *
+ * - IO#each_codepoint: Reads each remaining codepoint, passing it to the given block.
+ *
+ * == What's Here
+ *
+ * First, what's elsewhere. Class \IO:
+ *
+ * - Inherits from {class Object}[rdoc-ref:Object@Whats+Here].
+ * - Includes {module Enumerable}[rdoc-ref:Enumerable@Whats+Here],
+ * which provides dozens of additional methods.
+ *
+ * Here, class \IO provides methods that are useful for:
+ *
+ * - {Creating}[rdoc-ref:IO@Creating]
+ * - {Reading}[rdoc-ref:IO@Reading]
+ * - {Writing}[rdoc-ref:IO@Writing]
+ * - {Positioning}[rdoc-ref:IO@Positioning]
+ * - {Iterating}[rdoc-ref:IO@Iterating]
+ * - {Settings}[rdoc-ref:IO@Settings]
+ * - {Querying}[rdoc-ref:IO@Querying]
+ * - {Buffering}[rdoc-ref:IO@Buffering]
+ * - {Low-Level Access}[rdoc-ref:IO@Low-Level+Access]
+ * - {Other}[rdoc-ref:IO@Other]
+ *
+ * === Creating
+ *
+ * - ::new (aliased as ::for_fd): Creates and returns a new \IO object for the given
+ * integer file descriptor.
+ * - ::open: Creates a new \IO object.
+ * - ::pipe: Creates a connected pair of reader and writer \IO objects.
+ * - ::popen: Creates an \IO object to interact with a subprocess.
+ * - ::select: Selects which given \IO instances are ready for reading,
+ * writing, or have pending exceptions.
+ *
+ * === Reading
+ *
+ * - ::binread: Returns a binary string with all or a subset of bytes
+ * from the given file.
+ * - ::read: Returns a string with all or a subset of bytes from the given file.
+ * - ::readlines: Returns an array of strings, which are the lines from the given file.
+ * - #getbyte: Returns the next 8-bit byte read from +self+ as an integer.
+ * - #getc: Returns the next character read from +self+ as a string.
+ * - #gets: Returns the line read from +self+.
+ * - #pread: Returns all or the next _n_ bytes read from +self+,
+ * not updating the receiver's offset.
+ * - #read: Returns all remaining or the next _n_ bytes read from +self+
+ * for a given _n_.
+ * - #read_nonblock: the next _n_ bytes read from +self+ for a given _n_,
+ * in non-block mode.
+ * - #readbyte: Returns the next byte read from +self+;
+ * same as #getbyte, but raises an exception on end-of-stream.
+ * - #readchar: Returns the next character read from +self+;
+ * same as #getc, but raises an exception on end-of-stream.
+ * - #readline: Returns the next line read from +self+;
+ * same as #getline, but raises an exception of end-of-stream.
+ * - #readlines: Returns an array of all lines read read from +self+.
+ * - #readpartial: Returns up to the given number of bytes from +self+.
+ *
+ * === Writing
+ *
+ * - ::binwrite: Writes the given string to the file at the given filepath,
+ * in binary mode.
+ * - ::write: Writes the given string to +self+.
+ * - #<<: Appends the given string to +self+.
+ * - #print: Prints last read line or given objects to +self+.
+ * - #printf: Writes to +self+ based on the given format string and objects.
+ * - #putc: Writes a character to +self+.
+ * - #puts: Writes lines to +self+, making sure line ends with a newline.
+ * - #pwrite: Writes the given string at the given offset,
+ * not updating the receiver's offset.
+ * - #write: Writes one or more given strings to +self+.
+ * - #write_nonblock: Writes one or more given strings to +self+ in non-blocking mode.
+ *
+ * === Positioning
+ *
+ * - #lineno: Returns the current line number in +self+.
+ * - #lineno=: Sets the line number is +self+.
+ * - #pos (aliased as #tell): Returns the current byte offset in +self+.
+ * - #pos=: Sets the byte offset in +self+.
+ * - #reopen: Reassociates +self+ with a new or existing \IO stream.
+ * - #rewind: Positions +self+ to the beginning of input.
+ * - #seek: Sets the offset for +self+ relative to given position.
+ *
+ * === Iterating
+ *
+ * - ::foreach: Yields each line of given file to the block.
+ * - #each (aliased as #each_line): Calls the given block
+ * with each successive line in +self+.
+ * - #each_byte: Calls the given block with each successive byte in +self+
+ * as an integer.
+ * - #each_char: Calls the given block with each successive character in +self+
+ * as a string.
+ * - #each_codepoint: Calls the given block with each successive codepoint in +self+
+ * as an integer.
+ *
+ * === Settings
+ *
+ * - #autoclose=: Sets whether +self+ auto-closes.
+ * - #binmode: Sets +self+ to binary mode.
+ * - #close: Closes +self+.
+ * - #close_on_exec=: Sets the close-on-exec flag.
+ * - #close_read: Closes +self+ for reading.
+ * - #close_write: Closes +self+ for writing.
+ * - #set_encoding: Sets the encoding for +self+.
+ * - #set_encoding_by_bom: Sets the encoding for +self+, based on its
+ * Unicode byte-order-mark.
+ * - #sync=: Sets the sync-mode to the given value.
+ *
+ * === Querying
+ *
+ * - #autoclose?: Returns whether +self+ auto-closes.
+ * - #binmode?: Returns whether +self+ is in binary mode.
+ * - #close_on_exec?: Returns the close-on-exec flag for +self+.
+ * - #closed?: Returns whether +self+ is closed.
+ * - #eof? (aliased as #eof): Returns whether +self+ is at end-of-stream.
+ * - #external_encoding: Returns the external encoding object for +self+.
+ * - #fileno (aliased as #to_i): Returns the integer file descriptor for +self+
+ * - #internal_encoding: Returns the internal encoding object for +self+.
+ * - #pid: Returns the process ID of a child process associated with +self+,
+ * if +self+ was created by ::popen.
+ * - #stat: Returns the File::Stat object containing status information for +self+.
+ * - #sync: Returns whether +self+ is in sync-mode.
+ * - #tty? (aliased as #isatty): Returns whether +self+ is a terminal.
+ *
+ * === Buffering
+ *
+ * - #fdatasync: Immediately writes all buffered data in +self+ to disk.
+ * - #flush: Flushes any buffered data within +self+ to the underlying
+ * operating system.
+ * - #fsync: Immediately writes all buffered data and attributes in +self+ to disk.
+ * - #ungetbyte: Prepends buffer for +self+ with given integer byte or string.
+ * - #ungetc: Prepends buffer for +self+ with given string.
+ *
+ * === Low-Level Access
+ *
+ * - ::sysopen: Opens the file given by its path,
+ * returning the integer file descriptor.
+ * - #advise: Announces the intention to access data from +self+ in a specific way.
+ * - #fcntl: Passes a low-level command to the file specified
+ * by the given file descriptor.
+ * - #ioctl: Passes a low-level command to the device specified
+ * by the given file descriptor.
+ * - #sysread: Returns up to the next _n_ bytes read from self using a low-level read.
+ * - #sysseek: Sets the offset for +self+.
+ * - #syswrite: Writes the given string to +self+ using a low-level write.
+ *
+ * === Other
+ *
+ * - ::copy_stream: Copies data from a source to a destination,
+ * each of which is a filepath or an \IO-like object.
+ * - ::try_convert: Returns a new \IO object resulting from converting
+ * the given object.
+ * - #inspect: Returns the string representation of +self+.
+ *
*/
void
Init_IO(void)
{
-#undef rb_intern
-#define rb_intern(str) rb_intern_const(str)
-
VALUE rb_cARGF;
#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}
+ {"", O_RDONLY | O_BINARY},
+ {"", O_WRONLY | O_BINARY},
+ {"", O_RDWR | O_BINARY},
+ {"", O_APPEND | O_BINARY},
+ {NULL, 0}
};
cygwin_internal(CW_PERFILE, pf);
#endif
@@ -11869,12 +15687,13 @@ Init_IO(void)
rb_eIOError = rb_define_class("IOError", rb_eStandardError);
rb_eEOFError = rb_define_class("EOFError", rb_eIOError);
- id_write = rb_intern("write");
- id_read = rb_intern("read");
- id_getc = rb_intern("getc");
- id_flush = rb_intern("flush");
- id_readpartial = rb_intern("readpartial");
- id_set_encoding = rb_intern("set_encoding");
+ id_write = rb_intern_const("write");
+ id_read = rb_intern_const("read");
+ id_getc = rb_intern_const("getc");
+ id_flush = rb_intern_const("flush");
+ id_readpartial = rb_intern_const("readpartial");
+ id_set_encoding = rb_intern_const("set_encoding");
+ id_fileno = rb_intern_const("fileno");
rb_define_global_function("syscall", rb_f_syscall, -1);
@@ -11897,10 +15716,24 @@ Init_IO(void)
rb_cIO = rb_define_class("IO", rb_cObject);
rb_include_module(rb_cIO, rb_mEnumerable);
+ /* Can be raised by IO operations when IO#timeout= is set. */
+ rb_eIOTimeoutError = rb_define_class_under(rb_cIO, "TimeoutError", rb_eIOError);
+
+ /* Readable event mask for IO#wait. */
+ rb_define_const(rb_cIO, "READABLE", INT2NUM(RUBY_IO_READABLE));
+ /* Writable event mask for IO#wait. */
+ rb_define_const(rb_cIO, "WRITABLE", INT2NUM(RUBY_IO_WRITABLE));
+ /* Priority event mask for IO#wait. */
+ rb_define_const(rb_cIO, "PRIORITY", INT2NUM(RUBY_IO_PRIORITY));
+
+ /* exception to wait for reading. see IO.select. */
rb_mWaitReadable = rb_define_module_under(rb_cIO, "WaitReadable");
+ /* exception to wait for writing. see IO.select. */
rb_mWaitWritable = rb_define_module_under(rb_cIO, "WaitWritable");
+ /* exception to wait for reading by EAGAIN. see IO.select. */
rb_eEAGAINWaitReadable = rb_define_class_under(rb_cIO, "EAGAINWaitReadable", rb_eEAGAIN);
rb_include_module(rb_eEAGAINWaitReadable, rb_mWaitReadable);
+ /* exception to wait for writing by EAGAIN. see IO.select. */
rb_eEAGAINWaitWritable = rb_define_class_under(rb_cIO, "EAGAINWaitWritable", rb_eEAGAIN);
rb_include_module(rb_eEAGAINWaitWritable, rb_mWaitWritable);
#if EAGAIN == EWOULDBLOCK
@@ -11911,13 +15744,17 @@ Init_IO(void)
/* same as IO::EAGAINWaitWritable */
rb_define_const(rb_cIO, "EWOULDBLOCKWaitWritable", rb_eEAGAINWaitWritable);
#else
+ /* exception to wait for reading by EWOULDBLOCK. see IO.select. */
rb_eEWOULDBLOCKWaitReadable = rb_define_class_under(rb_cIO, "EWOULDBLOCKWaitReadable", rb_eEWOULDBLOCK);
rb_include_module(rb_eEWOULDBLOCKWaitReadable, rb_mWaitReadable);
+ /* exception to wait for writing by EWOULDBLOCK. see IO.select. */
rb_eEWOULDBLOCKWaitWritable = rb_define_class_under(rb_cIO, "EWOULDBLOCKWaitWritable", rb_eEWOULDBLOCK);
rb_include_module(rb_eEWOULDBLOCKWaitWritable, rb_mWaitWritable);
#endif
+ /* exception to wait for reading by EINPROGRESS. see IO.select. */
rb_eEINPROGRESSWaitReadable = rb_define_class_under(rb_cIO, "EINPROGRESSWaitReadable", rb_eEINPROGRESS);
rb_include_module(rb_eEINPROGRESSWaitReadable, rb_mWaitReadable);
+ /* exception to wait for writing by EINPROGRESS. see IO.select. */
rb_eEINPROGRESSWaitWritable = rb_define_class_under(rb_cIO, "EINPROGRESSWaitWritable", rb_eEINPROGRESS);
rb_include_module(rb_eEINPROGRESSWaitWritable, rb_mWaitWritable);
@@ -11946,17 +15783,21 @@ Init_IO(void)
rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
rb_output_fs = Qnil;
- rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_str_setter);
+ rb_define_hooked_variable("$,", &rb_output_fs, 0, rb_deprecated_str_setter);
- rb_rs = rb_default_rs = rb_usascii_str_new2("\n");
- rb_gc_register_mark_object(rb_default_rs);
+ rb_default_rs = rb_fstring_lit("\n"); /* avoid modifying RS_default */
+ rb_vm_register_global_object(rb_default_rs);
+ rb_rs = rb_default_rs;
rb_output_rs = Qnil;
- OBJ_FREEZE(rb_default_rs); /* avoid modifying RS_default */
- rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);
- rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
- rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
+ rb_define_hooked_variable("$/", &rb_rs, 0, deprecated_rs_setter);
+ rb_gvar_ractor_local("$/"); // not local but ractor safe
+ rb_define_hooked_variable("$-0", &rb_rs, 0, deprecated_rs_setter);
+ rb_gvar_ractor_local("$-0"); // not local but ractor safe
+ rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_deprecated_str_setter);
- rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
+ rb_define_virtual_variable("$_", get_LAST_READ_LINE, set_LAST_READ_LINE);
+ rb_gvar_ractor_local("$_");
+ rb_gvar_box_dynamic("$_");
rb_define_method(rb_cIO, "initialize_copy", rb_io_init_copy, 1);
rb_define_method(rb_cIO, "reopen", rb_io_reopen, -1);
@@ -11971,37 +15812,36 @@ Init_IO(void)
rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0);
rb_define_method(rb_cIO, "each_char", rb_io_each_char, 0);
rb_define_method(rb_cIO, "each_codepoint", rb_io_each_codepoint, 0);
- rb_define_method(rb_cIO, "lines", rb_io_lines, -1);
- rb_define_method(rb_cIO, "bytes", rb_io_bytes, 0);
- rb_define_method(rb_cIO, "chars", rb_io_chars, 0);
- rb_define_method(rb_cIO, "codepoints", rb_io_codepoints, 0);
rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
rb_define_method(rb_cIO, "sysread", rb_io_sysread, -1);
+ rb_define_method(rb_cIO, "pread", rb_io_pread, -1);
+ rb_define_method(rb_cIO, "pwrite", rb_io_pwrite, 2);
+
rb_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
rb_define_alias(rb_cIO, "to_i", "fileno");
rb_define_method(rb_cIO, "to_io", rb_io_to_io, 0);
- rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
- rb_define_method(rb_cIO, "fdatasync", rb_io_fdatasync, 0);
- rb_define_method(rb_cIO, "sync", rb_io_sync, 0);
- rb_define_method(rb_cIO, "sync=", rb_io_set_sync, 1);
-
- rb_define_method(rb_cIO, "lineno", rb_io_lineno, 0);
- rb_define_method(rb_cIO, "lineno=", rb_io_set_lineno, 1);
-
- rb_define_method(rb_cIO, "readlines", rb_io_readlines, -1);
-
- rb_define_method(rb_cIO, "read_nonblock", io_read_nonblock, -1);
- rb_define_method(rb_cIO, "write_nonblock", rb_io_write_nonblock, -1);
- rb_define_method(rb_cIO, "readpartial", io_readpartial, -1);
- rb_define_method(rb_cIO, "read", io_read, -1);
- rb_define_method(rb_cIO, "write", io_write_m, 1);
- rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
- rb_define_method(rb_cIO, "readline", rb_io_readline, -1);
- rb_define_method(rb_cIO, "getc", rb_io_getc, 0);
- rb_define_method(rb_cIO, "getbyte", rb_io_getbyte, 0);
+ rb_define_method(rb_cIO, "timeout", rb_io_timeout, 0);
+ rb_define_method(rb_cIO, "timeout=", rb_io_set_timeout, 1);
+
+ rb_define_method(rb_cIO, "fsync", rb_io_fsync, 0);
+ rb_define_method(rb_cIO, "fdatasync", rb_io_fdatasync, 0);
+ rb_define_method(rb_cIO, "sync", rb_io_sync, 0);
+ rb_define_method(rb_cIO, "sync=", rb_io_set_sync, 1);
+
+ rb_define_method(rb_cIO, "lineno", rb_io_lineno, 0);
+ rb_define_method(rb_cIO, "lineno=", rb_io_set_lineno, 1);
+
+ rb_define_method(rb_cIO, "readlines", rb_io_readlines, -1);
+
+ rb_define_method(rb_cIO, "readpartial", io_readpartial, -1);
+ rb_define_method(rb_cIO, "read", io_read, -1);
+ rb_define_method(rb_cIO, "write", io_write_m, -1);
+ rb_define_method(rb_cIO, "gets", rb_io_gets_m, -1);
+ rb_define_method(rb_cIO, "getc", rb_io_getc, 0);
+ rb_define_method(rb_cIO, "getbyte", rb_io_getbyte, 0);
rb_define_method(rb_cIO, "readchar", rb_io_readchar, 0);
rb_define_method(rb_cIO, "readbyte", rb_io_readbyte, 0);
rb_define_method(rb_cIO, "ungetbyte",rb_io_ungetbyte, 1);
@@ -12034,7 +15874,7 @@ Init_IO(void)
rb_define_method(rb_cIO, "close_on_exec=", rb_io_set_close_on_exec, 1);
rb_define_method(rb_cIO, "close", rb_io_close_m, 0);
- rb_define_method(rb_cIO, "closed?", rb_io_closed, 0);
+ rb_define_method(rb_cIO, "closed?", rb_io_closed_p, 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);
@@ -12048,24 +15888,45 @@ Init_IO(void)
rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
rb_define_method(rb_cIO, "pid", rb_io_pid, 0);
+
+ rb_define_method(rb_cIO, "path", rb_io_path, 0);
+ rb_define_method(rb_cIO, "to_path", rb_io_path, 0);
+
rb_define_method(rb_cIO, "inspect", rb_io_inspect, 0);
rb_define_method(rb_cIO, "external_encoding", rb_io_external_encoding, 0);
rb_define_method(rb_cIO, "internal_encoding", rb_io_internal_encoding, 0);
rb_define_method(rb_cIO, "set_encoding", rb_io_set_encoding, -1);
+ rb_define_method(rb_cIO, "set_encoding_by_bom", rb_io_set_encoding_by_bom, 0);
rb_define_method(rb_cIO, "autoclose?", rb_io_autoclose_p, 0);
rb_define_method(rb_cIO, "autoclose=", rb_io_set_autoclose, 1);
- rb_define_variable("$stdin", &rb_stdin);
- rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");
- rb_define_hooked_variable("$stdout", &rb_stdout, 0, stdout_setter);
- rb_stdout = prep_stdio(stdout, FMODE_WRITABLE, rb_cIO, "<STDOUT>");
- rb_define_hooked_variable("$stderr", &rb_stderr, 0, stdout_setter);
- rb_stderr = prep_stdio(stderr, FMODE_WRITABLE|FMODE_SYNC, rb_cIO, "<STDERR>");
- rb_define_hooked_variable("$>", &rb_stdout, 0, stdout_setter);
+ rb_define_method(rb_cIO, "wait", io_wait, -1);
+
+ rb_define_method(rb_cIO, "wait_readable", io_wait_readable, -1);
+ rb_define_method(rb_cIO, "wait_writable", io_wait_writable, -1);
+ rb_define_method(rb_cIO, "wait_priority", io_wait_priority, -1);
+
+ rb_define_virtual_variable("$stdin", stdin_getter, stdin_setter);
+ rb_define_virtual_variable("$stdout", stdout_getter, stdout_setter);
+ rb_define_virtual_variable("$>", stdout_getter, stdout_setter);
+ rb_define_virtual_variable("$stderr", stderr_getter, stderr_setter);
+
+ rb_gvar_ractor_local("$stdin");
+ rb_gvar_ractor_local("$stdout");
+ rb_gvar_ractor_local("$>");
+ rb_gvar_ractor_local("$stderr");
+
+ rb_global_variable(&rb_stdin);
+ rb_stdin = rb_io_prep_stdin();
+ rb_global_variable(&rb_stdout);
+ rb_stdout = rb_io_prep_stdout();
+ rb_global_variable(&rb_stderr);
+ rb_stderr = rb_io_prep_stderr();
+
orig_stdout = rb_stdout;
- rb_deferr = orig_stderr = rb_stderr;
+ orig_stderr = rb_stderr;
/* Holds the original stdin */
rb_define_global_const("STDIN", rb_stdin);
@@ -12100,10 +15961,6 @@ Init_IO(void)
rb_define_method(rb_cARGF, "each_byte", argf_each_byte, 0);
rb_define_method(rb_cARGF, "each_char", argf_each_char, 0);
rb_define_method(rb_cARGF, "each_codepoint", argf_each_codepoint, 0);
- rb_define_method(rb_cARGF, "lines", argf_lines, -1);
- rb_define_method(rb_cARGF, "bytes", argf_bytes, 0);
- rb_define_method(rb_cARGF, "chars", argf_chars, 0);
- rb_define_method(rb_cARGF, "codepoints", argf_codepoints, 0);
rb_define_method(rb_cARGF, "read", argf_read, -1);
rb_define_method(rb_cARGF, "readpartial", argf_readpartial, -1);
@@ -12126,7 +15983,7 @@ Init_IO(void)
rb_define_method(rb_cARGF, "binmode", argf_binmode_m, 0);
rb_define_method(rb_cARGF, "binmode?", argf_binmode_p, 0);
- rb_define_method(rb_cARGF, "write", argf_write, 1);
+ rb_define_method(rb_cARGF, "write", argf_write, -1);
rb_define_method(rb_cARGF, "print", rb_io_print, -1);
rb_define_method(rb_cARGF, "putc", rb_io_putc, 1);
rb_define_method(rb_cARGF, "puts", rb_io_puts, -1);
@@ -12162,9 +16019,11 @@ Init_IO(void)
rb_define_hooked_variable("$.", &argf, argf_lineno_getter, argf_lineno_setter);
rb_define_hooked_variable("$FILENAME", &argf, argf_filename_getter, rb_gvar_readonly_setter);
- ARGF.filename = rb_str_new2("-");
+ ARGF_SET(filename, rb_str_new2("-"));
rb_define_hooked_variable("$-i", &argf, opt_i_get, opt_i_set);
+ rb_gvar_ractor_local("$-i");
+
rb_define_hooked_variable("$*", &argf, argf_argv_getter, rb_gvar_readonly_setter);
#if defined (_WIN32) || defined(__CYGWIN__)
@@ -12175,29 +16034,33 @@ Init_IO(void)
rb_define_method(rb_cFile, "initialize", rb_file_initialize, -1);
- sym_mode = ID2SYM(rb_intern("mode"));
- sym_perm = ID2SYM(rb_intern("perm"));
- sym_extenc = ID2SYM(rb_intern("external_encoding"));
- sym_intenc = ID2SYM(rb_intern("internal_encoding"));
- sym_encoding = ID2SYM(rb_intern("encoding"));
- sym_open_args = ID2SYM(rb_intern("open_args"));
- sym_textmode = ID2SYM(rb_intern("textmode"));
- sym_binmode = ID2SYM(rb_intern("binmode"));
- sym_autoclose = ID2SYM(rb_intern("autoclose"));
- sym_normal = ID2SYM(rb_intern("normal"));
- sym_sequential = ID2SYM(rb_intern("sequential"));
- sym_random = ID2SYM(rb_intern("random"));
- sym_willneed = ID2SYM(rb_intern("willneed"));
- sym_dontneed = ID2SYM(rb_intern("dontneed"));
- sym_noreuse = ID2SYM(rb_intern("noreuse"));
- sym_SET = ID2SYM(rb_intern("SET"));
- sym_CUR = ID2SYM(rb_intern("CUR"));
- sym_END = ID2SYM(rb_intern("END"));
+ sym_mode = ID2SYM(rb_intern_const("mode"));
+ sym_perm = ID2SYM(rb_intern_const("perm"));
+ sym_flags = ID2SYM(rb_intern_const("flags"));
+ sym_extenc = ID2SYM(rb_intern_const("external_encoding"));
+ sym_intenc = ID2SYM(rb_intern_const("internal_encoding"));
+ sym_encoding = ID2SYM(rb_id_encoding());
+ sym_open_args = ID2SYM(rb_intern_const("open_args"));
+ sym_textmode = ID2SYM(rb_intern_const("textmode"));
+ sym_binmode = ID2SYM(rb_intern_const("binmode"));
+ sym_autoclose = ID2SYM(rb_intern_const("autoclose"));
+ sym_normal = ID2SYM(rb_intern_const("normal"));
+ sym_sequential = ID2SYM(rb_intern_const("sequential"));
+ sym_random = ID2SYM(rb_intern_const("random"));
+ sym_willneed = ID2SYM(rb_intern_const("willneed"));
+ sym_dontneed = ID2SYM(rb_intern_const("dontneed"));
+ sym_noreuse = ID2SYM(rb_intern_const("noreuse"));
+ sym_SET = ID2SYM(rb_intern_const("SET"));
+ sym_CUR = ID2SYM(rb_intern_const("CUR"));
+ sym_END = ID2SYM(rb_intern_const("END"));
#ifdef SEEK_DATA
- sym_DATA = ID2SYM(rb_intern("DATA"));
+ sym_DATA = ID2SYM(rb_intern_const("DATA"));
#endif
#ifdef SEEK_HOLE
- sym_HOLE = ID2SYM(rb_intern("HOLE"));
+ sym_HOLE = ID2SYM(rb_intern_const("HOLE"));
#endif
- sym_exception = ID2SYM(rb_intern("exception"));
+ sym_wait_readable = ID2SYM(rb_intern_const("wait_readable"));
+ sym_wait_writable = ID2SYM(rb_intern_const("wait_writable"));
}
+
+#include "io.rbinc"