7f7ca2094b98b20c063c45039fdb6a734fda0a7e
[ghc-base.git] / System / Cmd.hs
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.hs,v 1.2 2002/04/24 16:09:12 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 -- ---------------------------------------------------------------------------
31 -- system
32
33 -- Computation `system cmd' returns the exit code
34 -- produced when the operating system processes the command `cmd'.
35
36 -- This computation may fail with
37 --   PermissionDenied 
38 --      The process has insufficient privileges to perform the operation.
39 --   ResourceExhausted
40 --      Insufficient resources are available to perform the operation.  
41 --   UnsupportedOperation
42 --      The implementation does not support system calls.
43
44 system :: String -> IO ExitCode
45 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
46 system cmd =
47   withCString cmd $ \s -> do
48     status <- throwErrnoIfMinus1 "system" (primSystem s)
49     case status of
50         0  -> return ExitSuccess
51         n  -> return (ExitFailure n)
52
53 foreign import ccall unsafe "systemCmd" primSystem :: CString -> IO Int