module System.PosixCompat.Unistd
(
SystemID(..)
, getSystemID
, sleep
, usleep
, nanosleep
) where
#ifdef UNIX_IMPL
import System.Posix.Unistd
#else
import Control.Concurrent (threadDelay)
import Foreign.C.String (CString, peekCString)
import Foreign.C.Types (CInt, CSize)
import Foreign.Marshal.Array (allocaArray)
data SystemID = SystemID {
systemName :: String
, nodeName :: String
, release :: String
, version :: String
, machine :: String
} deriving (Eq, Read, Show)
getSystemID :: IO SystemID
getSystemID = do
let bufSize = 256
let call f = allocaArray bufSize $ \buf -> do
ok <- f buf (fromIntegral bufSize)
if ok == 1
then peekCString buf
else return ""
display <- call c_HsOSDisplayString
vers <- call c_HsOSVersionString
arch <- call c_HsOSArchString
node <- call c_HsOSNodeName
return SystemID {
systemName = "Windows"
, nodeName = node
, release = display
, version = vers
, machine = arch
}
sleep :: Int -> IO Int
sleep secs = threadDelay (secs * 1000000) >> return 0
usleep :: Int -> IO ()
usleep = threadDelay
nanosleep :: Integer -> IO ()
nanosleep nsecs = threadDelay (round (fromIntegral nsecs / 1000 :: Double))
foreign import ccall "HsOSDisplayString" c_HsOSDisplayString
:: CString -> CSize -> IO CInt
foreign import ccall "HsOSVersionString" c_HsOSVersionString
:: CString -> CSize -> IO CInt
foreign import ccall "HsOSArchString" c_HsOSArchString
:: CString -> CSize -> IO CInt
foreign import ccall "HsOSNodeName" c_HsOSNodeName
:: CString -> CSize -> IO CInt
#endif