Created
July 3, 2014 11:06
-
-
Save Tarrasch/0fdbf3d2bb824ac5ca23 to your computer and use it in GitHub Desktop.
Minimal retrieving hostname in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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