about summary refs log tree commit homepage
path: root/ext/raindrops/my_fileno.h
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-02-22 19:24:54 -0800
committerEric Wong <normalperson@yhbt.net>2011-02-23 13:14:27 -0800
commitdb192979f096d0153ad14e530e1e2e193289c7e0 (patch)
tree356c4a8bf64f30dd6c4886adc92f253bd4bf831d /ext/raindrops/my_fileno.h
parentebc2093847705c382b4d83ed5120e44b9afad3c0 (diff)
downloadraindrops-db192979f096d0153ad14e530e1e2e193289c7e0.tar.gz
This returns a Raindrops::TCP_Info object
that wraps a tcp_info struct.
Diffstat (limited to 'ext/raindrops/my_fileno.h')
-rw-r--r--ext/raindrops/my_fileno.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/ext/raindrops/my_fileno.h b/ext/raindrops/my_fileno.h
new file mode 100644
index 0000000..bdf1a5f
--- /dev/null
+++ b/ext/raindrops/my_fileno.h
@@ -0,0 +1,36 @@
+#include <ruby.h>
+#ifdef HAVE_RUBY_IO_H
+#  include <ruby/io.h>
+#else
+#  include <stdio.h>
+#  include <rubyio.h>
+#endif
+
+#if ! HAVE_RB_IO_T
+#  define rb_io_t OpenFile
+#endif
+
+#ifdef GetReadFile
+#  define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
+#else
+#  if !HAVE_RB_IO_T || (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8)
+#    define FPTR_TO_FD(fptr) fileno(fptr->f)
+#  else
+#    define FPTR_TO_FD(fptr) fptr->fd
+#  endif
+#endif
+
+static int my_fileno(VALUE io)
+{
+        rb_io_t *fptr;
+        int fd;
+
+        if (TYPE(io) != T_FILE)
+                io = rb_convert_type(io, T_FILE, "IO", "to_io");
+        GetOpenFile(io, fptr);
+        fd = FPTR_TO_FD(fptr);
+
+        if (fd < 0)
+                rb_raise(rb_eIOError, "closed stream");
+        return fd;
+}