@@ -6824,7 +6824,7 @@ open_key_args(int argc, VALUE *argv, struct foreach_arg *arg)
6824
6824
arg -> io = rb_io_open_with_args (RARRAY_LEN (args ), RARRAY_PTR (args ));
6825
6825
return ;
6826
6826
}
6827
- arg -> io = rb_io_open (argv [0 ], Qnil , INT2NUM ( O_RDONLY ) , opt );
6827
+ arg -> io = rb_io_open (argv [0 ], Qnil , Qnil , opt );
6828
6828
}
6829
6829
6830
6830
static VALUE
@@ -6963,6 +6963,37 @@ rb_io_s_read(int argc, VALUE *argv, VALUE io)
6963
6963
return rb_ensure (io_s_read , (VALUE )& arg , rb_io_close , arg .io );
6964
6964
}
6965
6965
6966
+ /*
6967
+ * call-seq:
6968
+ * IO.binread(name, [length [, offset]] ) => string
6969
+ *
6970
+ * Opens the file, optionally seeks to the given offset, then returns
6971
+ * <i>length</i> bytes (defaulting to the rest of the file).
6972
+ * <code>read</code> ensures the file is closed before returning.
6973
+ * The open mode would be "rb:ASCII-8BIT".
6974
+ *
6975
+ * IO.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
6976
+ * IO.binread("testfile", 20) #=> "This is line one\nThi"
6977
+ * IO.binread("testfile", 20, 10) #=> "ne one\nThis is line "
6978
+ */
6979
+
6980
+ static VALUE
6981
+ rb_io_s_binread (int argc , VALUE * argv , VALUE io )
6982
+ {
6983
+ VALUE offset ;
6984
+ struct foreach_arg arg ;
6985
+
6986
+ rb_scan_args (argc , argv , "12" , NULL , NULL , & offset );
6987
+ arg .io = rb_io_open (argv [0 ], rb_str_new_cstr ("rb:ASCII-8BIT" ), Qnil , Qnil );
6988
+ if (NIL_P (arg .io )) return Qnil ;
6989
+ arg .argv = argv + 1 ;
6990
+ arg .argc = (argc > 2 ) ? 2 : argc ;
6991
+ if (!NIL_P (offset )) {
6992
+ rb_io_seek (arg .io , offset , SEEK_SET );
6993
+ }
6994
+ return rb_ensure (io_s_read , (VALUE )& arg , rb_io_close , arg .io );
6995
+ }
6996
+
6966
6997
struct copy_stream_struct {
6967
6998
VALUE src ;
6968
6999
VALUE dst ;
@@ -8185,6 +8216,7 @@ Init_IO(void)
8185
8216
rb_define_singleton_method (rb_cIO , "foreach" , rb_io_s_foreach , -1 );
8186
8217
rb_define_singleton_method (rb_cIO , "readlines" , rb_io_s_readlines , -1 );
8187
8218
rb_define_singleton_method (rb_cIO , "read" , rb_io_s_read , -1 );
8219
+ rb_define_singleton_method (rb_cIO , "binread" , rb_io_s_binread , -1 );
8188
8220
rb_define_singleton_method (rb_cIO , "select" , rb_f_select , -1 );
8189
8221
rb_define_singleton_method (rb_cIO , "pipe" , rb_io_s_pipe , -1 );
8190
8222
rb_define_singleton_method (rb_cIO , "try_convert" , rb_io_s_try_convert , 1 );
0 commit comments