summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'io.c')
-rw-r--r--io.c62
1 files changed, 37 insertions, 25 deletions
diff --git a/io.c b/io.c
index c1c7d511db..1ea7df8c74 100644
--- a/io.c
+++ b/io.c
@@ -6,7 +6,7 @@
$Date$
created at: Fri Oct 15 18:08:59 JST 1993
- Copyright (C) 1993-1999 Yukihiro Matsumoto
+ Copyright (C) 1993-2000 Yukihiro Matsumoto
************************************************/
@@ -426,7 +426,7 @@ read_all(port)
TRAP_BEG;
n = fread(RSTRING(str)->ptr+bytes, 1, siz-bytes, fptr->f);
TRAP_END;
- if (n == 0) {
+ if (n == 0 && bytes == 0) {
if (feof(fptr->f)) return Qnil;
rb_sys_fail(fptr->path);
}
@@ -452,15 +452,17 @@ io_fread(ptr, len, f)
int c;
while (n--) {
+ if (!READ_DATA_PENDING(f)) {
+ rb_thread_wait_fd(fileno(f));
+ }
+ TRAP_BEG;
c = getc(f);
+ TRAP_END;
if (c == EOF) {
*ptr = '\0';
break;
}
*ptr++ = c;
- if (!READ_DATA_PENDING(f)) {
- rb_thread_wait_fd(fileno(f));
- }
}
return len - n - 1;
@@ -482,11 +484,15 @@ io_read(argc, argv, io)
}
len = NUM2INT(length);
+ if (len < 0) {
+ rb_raise(rb_eArgError, "negative length %d given", len);
+ }
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (feof(fptr->f)) return Qnil;
str = rb_str_new(0, len);
+ if (len == 0) return str;
READ_CHECK(fptr->f);
n = io_fread(RSTRING(str)->ptr, len, fptr->f);
@@ -951,7 +957,7 @@ rb_io_close(io)
{
OpenFile *fptr;
- GetOpenFile(io, fptr);
+ fptr = RFILE(io)->fptr;
rb_io_fptr_close(fptr);
if (fptr->pid) {
rb_syswait(fptr->pid);
@@ -1088,7 +1094,7 @@ VALUE
rb_io_binmode(io)
VALUE io;
{
-#if defined(NT) || defined(DJGPP) || defined(__CYGWIN32__)\
+#if defined(NT) || defined(DJGPP) || defined(__CYGWIN__)\
|| defined(__human68k__) || defined(USE_CWGUSI) || defined(__EMX__)
OpenFile *fptr;
@@ -1250,6 +1256,9 @@ rb_fopen(fname, mode)
rb_sys_fail(fname);
}
}
+#ifdef __human68k__
+ fmode(file, _IOTEXT);
+#endif
return file;
}
@@ -1334,7 +1343,7 @@ rb_file_sysopen(fname, flags, mode)
return rb_file_sysopen_internal(rb_cFile, fname, flags, mode);
}
-#if defined (NT) || defined(DJGPP) || defined(__CYGWIN32__) || defined(__human68k__)
+#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
static struct pipe_list {
OpenFile *fptr;
struct pipe_list *next;
@@ -1393,7 +1402,7 @@ static void
pipe_finalize(fptr)
OpenFile *fptr;
{
-#if !defined (__CYGWIN32__)
+#if !defined (__CYGWIN__)
if (fptr->f != NULL) {
pclose(fptr->f);
}
@@ -1444,7 +1453,8 @@ pipe_open(pname, mode)
pipe_add_fptr(fptr);
if (modef & FMODE_READABLE) fptr->f = f;
if (modef & FMODE_WRITABLE) {
- fptr->f2 = f;
+ if (fptr->f) fptr->f2 = f;
+ else fptr->f = f;
rb_io_synchronized(fptr);
}
return (VALUE)port;
@@ -1524,7 +1534,7 @@ pipe_open(pname, mode)
if (fptr->f) fptr->f2 = f;
else fptr->f = f;
}
-#if defined (__CYGWIN32__)
+#if defined (__CYGWIN__)
fptr->finalize = pipe_finalize;
pipe_add_fptr(fptr);
#endif
@@ -1578,8 +1588,8 @@ rb_file_s_open(argc, argv, klass)
path = RSTRING(fname)->ptr;
if (FIXNUM_P(vmode)) {
- int flags = FIX2INT(vmode);
- int fmode = NIL_P(perm) ? 0666 : FIX2INT(perm);
+ int flags = NUM2INT(vmode);
+ int fmode = NIL_P(perm) ? 0666 : NUM2INT(perm);
file = rb_file_sysopen_internal(klass, path, flags, fmode);
}
@@ -1619,7 +1629,7 @@ rb_f_open(argc, argv)
mode = "r";
}
else if (FIXNUM_P(pmode)) {
- mode = rb_io_flags_mode(FIX2INT(pmode));
+ mode = rb_io_flags_mode(NUM2INT(pmode));
}
else {
int len;
@@ -2283,13 +2293,13 @@ next_argv()
fstat(fileno(fr), &st);
if (*ruby_inplace_mode) {
str = rb_str_new2(fn);
-#if defined(MSDOS) || defined(__CYGWIN32__) || defined(NT)
+#if defined(MSDOS) || defined(__CYGWIN__) || defined(NT)
ruby_add_suffix(str, ruby_inplace_mode);
#else
rb_str_cat(str, ruby_inplace_mode,
strlen(ruby_inplace_mode));
#endif
-#if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN32__) || defined(NT) || defined(__human68k__) || defined(__EMX__)
+#if defined(MSDOS) || defined(__BOW__) || defined(__CYGWIN__) || defined(NT) || defined(__human68k__) || defined(__EMX__)
(void)fclose(fr);
(void)unlink(RSTRING(str)->ptr);
(void)rename(fn, RSTRING(str)->ptr);
@@ -2304,7 +2314,7 @@ next_argv()
#endif
}
else {
-#if !defined(MSDOS) && !defined(__BOW__) && !defined(__CYGWIN32__) && !defined(NT) && !defined(__human68k__)
+#if !defined(MSDOS) && !defined(__BOW__) && !defined(__CYGWIN__) && !defined(NT) && !defined(__human68k__)
if (unlink(fn) < 0) {
rb_warn("Can't remove %s: %s, skipping file",
fn, strerror(errno));
@@ -2316,7 +2326,7 @@ next_argv()
#endif
}
fw = rb_fopen(fn, "w");
-#if !defined(MSDOS) && !defined(__CYGWIN32__) && !(NT) && !defined(__human68k__) && !defined(USE_CWGUSI) && !defined(__BEOS__) && !defined(__EMX__)
+#if !defined(MSDOS) && !defined(__CYGWIN__) && !(NT) && !defined(__human68k__) && !defined(USE_CWGUSI) && !defined(__BEOS__) && !defined(__EMX__)
fstat(fileno(fw), &st2);
fchmod(fileno(fw), st.st_mode);
if (st.st_uid!=st2.st_uid || st.st_gid!=st2.st_gid) {
@@ -2359,7 +2369,7 @@ rb_f_gets_internal(argc, argv)
if (TYPE(current_file) != T_FILE) {
line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
}
- if (argc == 0 && rb_rs == rb_default_rs) {
+ else if (argc == 0 && rb_rs == rb_default_rs) {
line = rb_io_gets(current_file);
}
else {
@@ -2623,7 +2633,7 @@ rb_io_ctl(io, req, arg, io_p)
int io_p;
{
#if !defined(MSDOS) && !defined(__human68k__)
- int cmd = NUM2INT(req);
+ int cmd = NUM2ULONG(req);
OpenFile *fptr;
int len = 0;
int fd;
@@ -2669,7 +2679,7 @@ rb_io_ctl(io, req, arg, io_p)
fd = fileno(fptr->f);
#ifdef HAVE_FCNTL
TRAP_BEG;
-# ifdef USE_CWGUSI
+# if defined(USE_CWGUSI) || defined(__CYGWIN__)
retval = io_p?ioctl(fd, cmd, (void*) narg):fcntl(fd, cmd, narg);
# else
retval = io_p?ioctl(fd, cmd, narg):fcntl(fd, cmd, narg);
@@ -3066,6 +3076,8 @@ argf_eof()
{
if (init_p == 0 && !next_argv())
return Qtrue;
+ if (next_p == -1)
+ return Qtrue;
if (TYPE(current_file) != T_FILE) {
return argf_forward();
}
@@ -3250,9 +3262,9 @@ Init_IO()
rb_define_method(rb_cIO, "flush", rb_io_flush, 0);
rb_define_method(rb_cIO, "tell", rb_io_tell, 0);
rb_define_method(rb_cIO, "seek", rb_io_seek, 2);
- rb_define_const(rb_cIO, "SEEK_SET", SEEK_SET);
- rb_define_const(rb_cIO, "SEEK_CUR", SEEK_CUR);
- rb_define_const(rb_cIO, "SEEK_END", SEEK_END);
+ rb_define_const(rb_cIO, "SEEK_SET", INT2FIX(SEEK_SET));
+ rb_define_const(rb_cIO, "SEEK_CUR", INT2FIX(SEEK_CUR));
+ rb_define_const(rb_cIO, "SEEK_END", INT2FIX(SEEK_END));
rb_define_method(rb_cIO, "rewind", rb_io_rewind, 0);
rb_define_method(rb_cIO, "pos", rb_io_tell, 0);
rb_define_method(rb_cIO, "pos=", rb_io_set_pos, 1);
@@ -3330,7 +3342,7 @@ Init_IO()
rb_define_virtual_variable("$-i", opt_i_get, opt_i_set);
-#if defined (NT) || defined(DJGPP) || defined(__CYGWIN32__) || defined(__human68k__)
+#if defined (NT) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
atexit(pipe_atexit);
#endif