watcher: RDoc examples point to the demo
[raindrops.git] / ext / raindrops / my_fileno.h
blobbdf1a5f6ae3eda7193feb3b3176891b22094d060
1 #include <ruby.h>
2 #ifdef HAVE_RUBY_IO_H
3 # include <ruby/io.h>
4 #else
5 # include <stdio.h>
6 # include <rubyio.h>
7 #endif
9 #if ! HAVE_RB_IO_T
10 # define rb_io_t OpenFile
11 #endif
13 #ifdef GetReadFile
14 # define FPTR_TO_FD(fptr) (fileno(GetReadFile(fptr)))
15 #else
16 # if !HAVE_RB_IO_T || (RUBY_VERSION_MAJOR == 1 && RUBY_VERSION_MINOR == 8)
17 # define FPTR_TO_FD(fptr) fileno(fptr->f)
18 # else
19 # define FPTR_TO_FD(fptr) fptr->fd
20 # endif
21 #endif
23 static int my_fileno(VALUE io)
25 rb_io_t *fptr;
26 int fd;
28 if (TYPE(io) != T_FILE)
29 io = rb_convert_type(io, T_FILE, "IO", "to_io");
30 GetOpenFile(io, fptr);
31 fd = FPTR_TO_FD(fptr);
33 if (fd < 0)
34 rb_raise(rb_eIOError, "closed stream");
35 return fd;