[project @ 1999-12-20 10:34:27 by simonpj]
[ghc-hetmet.git] / ghc / lib / std / System.lhs
index 0080df6..41373d1 100644 (file)
@@ -25,7 +25,7 @@ import Prelude
 import PrelAddr
 import PrelIOBase      ( IOError(..), IOErrorType(..), constructErrorAndFailWithInfo, stToIO )
 import PrelPack        ( unpackCString, unpackCStringST, packString )
-import PrelArr         ( ByteArray )
+import PrelByteArr     ( ByteArray )
 
 type PrimByteArray  = ByteArray Int
 
@@ -203,12 +203,6 @@ getProgName                  = primGetRawArgs >>= \rawargs ->
 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)
 
@@ -220,6 +214,35 @@ fromExitCode                :: ExitCode -> Int
 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