-- -----------------------------------------------------------------------------
--- $Id: System.lhs,v 1.31 2001/05/22 15:06:47 simonmar Exp $
+-- $Id: System.lhs,v 1.33 2001/08/14 17:14:22 sof Exp $
--
-- (c) The University of Glasgow, 1994-2000
--
import PrelCString
import PrelCTypes
import PrelMarshalArray
+import PrelMarshalAlloc
import PrelPtr
import PrelStorable
import PrelIOBase
-- line arguments (not including the program name).
getArgs :: IO [String]
-getArgs = do
- argv <- peek prog_argv_label
- argc <- peek prog_argc_label
- peekArray (fromIntegral argc - 1) (advancePtr argv 1) >>= mapM peekCString
-
-foreign label "prog_argv" prog_argv_label :: Ptr (Ptr (Ptr CChar))
-foreign label "prog_argc" prog_argc_label :: Ptr CInt
+getArgs =
+ alloca $ \ p_argc ->
+ alloca $ \ p_argv -> do
+ getProgArgv p_argc p_argv
+ p <- peek p_argc
+ argv <- peek p_argv
+ peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
+
+
+foreign import "getProgArgv" getProgArgv :: Ptr Int -> Ptr (Ptr CString) -> IO ()
-- Computation `getProgName' returns the name of the program
-- as it was invoked.
getProgName :: IO String
-getProgName = do
- argv <- peek prog_argv_label
- unpackProgName argv
+getProgName =
+ alloca $ \ p_argc ->
+ alloca $ \ p_argv -> do
+ getProgArgv p_argc p_argv
+ argv <- peek p_argv
+ unpackProgName argv
-- Computation `getEnv var' returns the value
-- of the environment variable {\em var}.
getEnv :: String -> IO String
getEnv name =
- withUnsafeCString name $ \s -> do
+ withCString name $ \s -> do
litstring <- _getenv s
if litstring /= nullPtr
then peekCString litstring
else ioException (IOError Nothing NoSuchThing "getEnv"
"no environment variable" (Just name))
-foreign import ccall "getenv" unsafe _getenv :: UnsafeCString -> IO (Ptr CChar)
+foreign import ccall "getenv" unsafe _getenv :: CString -> IO (Ptr CChar)
-- ---------------------------------------------------------------------------
-- system
system :: String -> IO ExitCode
system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
system cmd =
- withUnsafeCString cmd $ \s -> do
+ withCString cmd $ \s -> do
status <- throwErrnoIfMinus1 "system" (primSystem s)
case status of
0 -> return ExitSuccess
n -> return (ExitFailure n)
-foreign import ccall "systemCmd" unsafe primSystem :: UnsafeCString -> IO Int
+foreign import ccall "systemCmd" unsafe primSystem :: CString -> IO Int
-- ---------------------------------------------------------------------------
-- exitWith