Hostname: Difference between revisions

776 bytes added ,  10 years ago
(Added example)
Line 195:
main = do hostName <- getHostName
putStrLn hostName</lang>
 
Or if you don't want to depend on the network package being installed, you can implement it on your own (this implementation is based on the implementation in the network package).
 
 
<lang 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
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Anonymous user