Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created July 3, 2014 11:06
Show Gist options
  • Save Tarrasch/0fdbf3d2bb824ac5ca23 to your computer and use it in GitHub Desktop.
Save Tarrasch/0fdbf3d2bb824ac5ca23 to your computer and use it in GitHub Desktop.

Revisions

  1. Tarrasch created this gist Jul 3, 2014.
    19 changes: 19 additions & 0 deletions GetHostName.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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