diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-29 09:07:20 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-29 09:07:20 +0000 |
commit | d33298e52459045ddde4bae619a8da3c3cf7dbd6 (patch) | |
tree | c14bd8d6c2aec5109888c3410d888f997d7c18c0 /ext/io/wait/wait.c | |
parent | 95327358ce9a2c7f2c2aab68cae4446c281a655e (diff) | |
download | ruby-d33298e52459045ddde4bae619a8da3c3cf7dbd6.tar.gz |
* ext/io/wait/wait.c (io_nread): returns number of bytes available
for read. response to feature request #936 in [ruby-core:20917].
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21169 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/io/wait/wait.c')
-rw-r--r-- | ext/io/wait/wait.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/ext/io/wait/wait.c b/ext/io/wait/wait.c index 24e4141162..b73c9af1ce 100644 --- a/ext/io/wait/wait.c +++ b/ext/io/wait/wait.c @@ -44,6 +44,31 @@ EXTERN struct timeval rb_time_interval _((VALUE time)); /* * call-seq: + * io.nread -> int + * + * Returns number of bytes that can be read without blocking. + * Returns zero if no information available. + */ + +static VALUE +io_nread(VALUE io) +{ + rb_io_t *fptr; + int len; + ioctl_arg n; + + GetOpenFile(io, fptr); + rb_io_check_readable(fptr); + len = rb_io_read_pending(fptr); + if (len > 0) return len; + if (!FIONREAD_POSSIBLE_P(fptr->fd)) return INT2FIX(0); + if (ioctl(fptr->fd, FIONREAD, &n)) return INT2FIX(0); + if (n > 0) return ioctl_arg2num(n); + return INT2FIX(0); +} + +/* + * call-seq: * io.ready? -> true, false or nil * * Returns true if input available without blocking, or false. @@ -137,6 +162,7 @@ io_wait(int argc, VALUE *argv, VALUE io) void Init_wait() { + rb_define_method(rb_cIO, "nread", io_nread, 0); rb_define_method(rb_cIO, "ready?", io_ready_p, 0); rb_define_method(rb_cIO, "wait", io_wait, -1); } |