Skip to content

Commit

Permalink
ext/io/wait: update with CRuby1.9.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Watson1978 committed Dec 8, 2011
1 parent f7f88c3 commit 7bfae05
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
5 changes: 3 additions & 2 deletions ext/io/wait/extconf.rb
Expand Up @@ -2,8 +2,9 @@
target = "io/wait"

unless macro_defined?("DOSISH", "#include <ruby.h>")
fionread = %w[sys/ioctl.h sys/filio.h].find do |h|
have_macro("FIONREAD", h)
have_header(ioctl_h = "sys/ioctl.h") or ioctl_h = nil
fionread = %w[sys/ioctl.h sys/filio.h sys/socket.h].find do |h|
have_macro("FIONREAD", [h, ioctl_h].compact)
end
if fionread
$defs << "-DFIONREAD_HEADER=\"<#{fionread}>\""
Expand Down
23 changes: 0 additions & 23 deletions ext/io/wait/lib/nonblock.rb

This file was deleted.

44 changes: 37 additions & 7 deletions ext/io/wait/wait.c
Expand Up @@ -11,9 +11,12 @@
**********************************************************************/

#include "ruby.h"
#include "rubyio.h"
#include "ruby/io.h"

#include <sys/types.h>
#if defined(HAVE_SYS_IOCTL_H)
#include <sys/ioctl.h>
#endif
#if defined(FIONREAD_HEADER)
#include FIONREAD_HEADER
#endif
Expand All @@ -30,7 +33,7 @@
#ifdef HAVE_RB_W32_IS_SOCKET
#define FIONREAD_POSSIBLE_P(fd) rb_w32_is_socket(fd)
#else
#define FIONREAD_POSSIBLE_P(fd) ((fd),Qtrue)
#define FIONREAD_POSSIBLE_P(fd) ((void)(fd),Qtrue)
#endif

static VALUE io_ready_p _((VALUE io));
Expand All @@ -39,11 +42,37 @@ void Init_wait _((void));

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 non-nil if input available without blocking, or nil.
* Returns true if input available without blocking, or false.
* Returns nil if no information available.
*/

static VALUE
Expand All @@ -55,10 +84,10 @@ io_ready_p(VALUE io)
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
if (rb_io_read_pending(fptr)) return Qtrue;
if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse;
if (ioctl(fptr->fd, FIONREAD, &n)) rb_sys_fail(0);
if (n > 0) return ioctl_arg2num(n);
return Qnil;
if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qnil;
if (ioctl(fptr->fd, FIONREAD, &n)) return Qnil;
if (n > 0) return Qtrue;
return Qfalse;
}

struct wait_readable_arg {
Expand Down Expand Up @@ -133,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);
}

0 comments on commit 7bfae05

Please sign in to comment.