Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created July 3, 2014 11:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tarrasch/0fdbf3d2bb824ac5ca23 to your computer and use it in GitHub Desktop.
Save Tarrasch/0fdbf3d2bb824ac5ca23 to your computer and use it in GitHub Desktop.
Minimal retrieving hostname in Haskell
module GetHostName where
import Foreign.Marshal.Array ( allocaArray0, peekArray0 )
import Foreign.C.Types ( CInt(..), CSize(..) )
import Foreign.C.String ( CString, peekCString )
import Foreign.C.Error ( throwErrnoIfMinus1_ )
getHostName :: IO String
getHostName = do
let size = 256
allocaArray0 size $ \ cstr -> do
throwErrnoIfMinus1_ "getHostName" $ c_gethostname cstr (fromIntegral size)
peekCString cstr
foreign import ccall "gethostname"
c_gethostname :: CString -> CSize -> IO CInt
main = do hostName <- getHostName
putStrLn hostName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment