Skip to content

Commit

Permalink
IO.new will be able to decide an access mode from a given descriptor.
Browse files Browse the repository at this point in the history
Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

fd = IO.sysopen("/tmp/test.txt", "w", 0666)

f = IO.new(fd)
f.write("foo\nbar\nbaz")
f.close

File.open("/tmp/test.txt") {|f|
  assert_equal("foo\nbar\nbaz", f.read)
}

assert_raise(Errno::EBADF){ IO.new(-1) }
assert_raise(Errno::EBADF){ IO.new(32767) }

puts :ok
}}}
  • Loading branch information
Watson1978 committed May 5, 2011
1 parent fc0895b commit 5912f76
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions io.c
Expand Up @@ -3183,7 +3183,15 @@ rb_io_initialize(VALUE io, SEL sel, int argc, VALUE *argv)
}

if (NIL_P(mode)) {
#ifdef F_GETFL
const int oflags = fcntl(fd, F_GETFL);
if (oflags == -1) {
rb_sys_fail("fcntl(2) failed");
}
mode_flags = convert_oflags_to_fmode(oflags);
#else
mode_flags = FMODE_READABLE;
#endif
}
else if (TYPE(mode) == T_STRING) {
mode_flags = convert_mode_string_to_fmode(mode);
Expand Down

0 comments on commit 5912f76

Please sign in to comment.