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

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Mon Aug 29 22:08:39 EDT 2005

Hi everyone,


I'm starting to play with the foreign function interface; the FFI paper:

    http://repository.readscheme.org/ftp/papers/sw2004/barzilay.pdf

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)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

which is lovely.  But, unfortunately, I'm not quite sure how to call this.
*grin* Basically, I wanted to do something like:

    (c-read (current-input-port) (make-string 20))

but this didn't work, of course:

##################################
> (define buf (make-string 20))
> (procedure-arity c-read)
> (c-read (current-input-port) buf)
Scheme->C: expects argument of type <int32>; given #<input-port:stdin>
##################################


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'm
assuming that this is non-obvious because:

    http://list.cs.brown.edu/pipermail/plt-scheme/2003-October/003588.html

uses "cffi.ss" to call fileno:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define stdin-fileno (c-lambda () int
  "___result = fileno(stdin);
  "))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


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.  But I'm wondering if there's a way to get at the fileno of an
arbitrary file input port without having to invoke the compiler: I'd like
to avoid cffi if that's possible.

The closest I've been able to find in the Mzscheme manual is the function
port-file-identity, which isn't quite right, but is close.


Thanks for any help!



Posted on the users mailing list.