[Haskell-cafe] question concerning ANSI "void *"

Adam Langley agl at imperialviolet.org
Fri Feb 8 15:38:44 EST 2008


On Feb 8, 2008 10:57 AM, Galchin Vasili <vigalchin at gmail.com> wrote:
> basically I am trying to implement ioctl for the Posix library .. so a
> possible signtaure would be:
>
> fdIoctl :: Fd -> Int -> Ptr Word 8 -> IO( Ptr Word8) ????

Ah, ok. You could cover many of the ioctls (the ones which only take a
single primitive) by using the Storable class and the types CInt etc.
Define the FFI call to ioctl with a type of CInt -> Int -> Ptr () ->
IO CInt and then

ioctlCInt :: CInt -> CInt -> CInt -> IO CInt
ioctlCInt fd call arg = do
  allocaBytes $ \ptr -> do
    poke ptr arg
    result <- ioctl fd call (cast ptr)
    when (result < 0) $ fail "ioctl error"
    peek ptr

(untested, might work ;)

... and likewise for the other C types. However, for those ioctls which take a
complex structure (e.g. many of the networking ones), you'll need to marshal
yourself:

data SomeIOCtlStruct = CInt CInt CInt

ioctlSomeIOCtlStruct :: CInt -> CInt -> SomeIOCtlStruct -> IO ()
ioctlSomeIOCtlStruct = do
  ...  (see the above linked to pointers to hsc2hs and c2hs about how to write
  this function)



AGL

--
Adam Langley                                      agl at imperialviolet.org
http://www.imperialviolet.org                       650-283-9641


More information about the Haskell-Cafe mailing list