[plt-scheme] FFI stuff: getting a fileno file descriptor from an input port?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon Aug 29 22:26:34 EDT 2005

At Mon, 29 Aug 2005 19:08:39 -0700 (PDT), Danny Yoo wrote:
> mentions an implementation of c-read:
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (define c-read
>  (get-ffi-obj "read" "libc.so.6"
>    (_fun _int
>       (buf : _string)
>       (_int : (string-length buf))
>       -> _int)))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> [...]
> The first argument should be a file descriptor integer, but I haven't
> figured out a clean way to get a file descriptor from a file port. [...]
> I've been staring at src/mzscheme/src/port.c, and see that it has a FILE*
> structure, so I'm pretty sure that I can use cffi to access that
> structure.

FILE* ports and file-descriptor ports are different. FILE* ports
aren't used unless some extension or embedding program creates them.

Extracting the file descriptor for a port is not easy. You'd have
to go through the `port_data' field of an import port or output port;
it will refer to a Scheme_FD struct, which contains an fd field. The
Scheme_FD struct is private to "port.c".

If it's useful, we could add a scheme_get_port_file_descriptor()
function to MzScheme. You want to do more than just call read() I take
it (since MzScheme already provides `read-bytes!')?


Posted on the users mailing list.