diff --git a/io.c b/io.c index 0713f35..8db6174 100644 --- a/io.c +++ b/io.c @@ -2482,6 +2482,11 @@ io_read(int argc, VALUE *argv, VALUE io) if (len == 0) return str; READ_CHECK(fptr); +#ifdef O_BINARY + if (!(fptr->mode & FMODE_BINMODE)) { + SET_BINARY_MODE_WITH_SEEK_CUR(fptr); + } +#endif n = io_fread(str, 0, fptr); if (n == 0) { if (fptr->fd < 0) return Qnil; diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 6f823c8..fd7b9fe 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -2222,4 +2222,15 @@ EOT end end end if /mswin|mingw/ =~ RUBY_PLATFORM + + def test_read_with_length + with_tmpdir { + str = "ab\r\nc" + generate_file('tmp', str) + s = open("tmp", "r") {|f| + # read with length should be binary mode + assert_equal(str, f.read(5)) + } + } + end if /mswin|mingw/ =~ RUBY_PLATFORM end