[project @ 2001-08-17 12:50:34 by simonmar]
[ghc-base.git] / System / Cmd.hsc
1 -----------------------------------------------------------------------------
2 -- 
3 -- Module      :  System.Cmd
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/core/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  portable
10 --
11 -- $Id: Cmd.hsc,v 1.2 2001/08/17 12:50:34 simonmar Exp $
12 --
13 -- Executing a command.
14 --
15 -----------------------------------------------------------------------------
16
17 module System.Cmd
18     ( system        -- :: String -> IO ExitCode
19     ) where
20
21 import Prelude
22
23 import System.Exit
24 import Foreign.C
25
26 #ifdef __GLASGOW_HASKELL__
27 import GHC.IOBase
28 #endif
29
30 #include "HsCore.h"
31
32 -- ---------------------------------------------------------------------------
33 -- system
34
35 -- Computation `system cmd' returns the exit code
36 -- produced when the operating system processes the command `cmd'.
37
38 -- This computation may fail with
39 --   PermissionDenied 
40 --      The process has insufficient privileges to perform the operation.
41 --   ResourceExhausted
42 --      Insufficient resources are available to perform the operation.  
43 --   UnsupportedOperation
44 --      The implementation does not support system calls.
45
46 system :: String -> IO ExitCode
47 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
48 system cmd =
49   withCString cmd $ \s -> do
50     status <- throwErrnoIfMinus1 "system" (primSystem s)
51     case status of
52         0  -> return ExitSuccess
53         n  -> return (ExitFailure n)
54
55 foreign import ccall "systemCmd" unsafe primSystem :: CString -> IO Int