import PrelAddr
import PrelIOBase ( IOError(..), IOErrorType(..), constructErrorAndFailWithInfo, stToIO )
import PrelPack ( unpackCString, unpackCStringST, packString )
-import PrelArr ( ByteArray )
+import PrelByteArr ( ByteArray )
type PrimByteArray = ByteArray Int
getEnv :: String -> IO String
getEnv = primGetEnv
-system :: String -> IO ExitCode
-system s = error "System.system unimplemented"
-
-exitWith :: ExitCode -> IO a
-exitWith c = error "System.exitWith unimplemented"
-
exitFailure :: IO a
exitFailure = exitWith (ExitFailure 1)
fromExitCode ExitSuccess = 0
fromExitCode (ExitFailure n) = n
+-- see comment in Prelude.hs near primRunIO_hugs_toplevel
+exitWith :: ExitCode -> IO a
+exitWith c
+ = do cleanup_action <- readIORef prelCleanupAfterRunAction
+ case cleanup_action of
+ Just xx -> xx
+ Nothing -> return ()
+ nh_stderr >>= nh_flush
+ nh_stdout >>= nh_flush
+ nh_stdin >>= nh_close
+ nh_exitwith (fromExitCode c)
+ (ioError.IOError) "System.exitWith: should not return"
+
+system :: String -> IO ExitCode
+system cmd
+ | null cmd
+ = (ioError.IOError) "System.system: null command"
+ | otherwise
+ = do str <- copy_String_to_cstring cmd
+ status <- nh_system str
+ nh_free str
+ case status of
+ 0 -> return ExitSuccess
+ n -> return (ExitFailure n)
+
+getPID :: IO Int
+getPID
+ = nh_getPID
+
-----------------------------------------------------------------------------
\end{code}
#endif