14300befaf9c9edcfb04b8b9efe4003edb2c27fa
[packages/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 -- Executing a command.
12 --
13 -----------------------------------------------------------------------------
14
15 module System.Cmd
16 ( system -- :: String -> IO ExitCode
17 ) where
18
19 import Prelude
20
21 import System.Exit
22 import Foreign.C
23
24 #ifdef __GLASGOW_HASKELL__
25 import GHC.IOBase
26 #endif
27
28 -- ---------------------------------------------------------------------------
29 -- system
30
31 -- Computation `system cmd' returns the exit code
32 -- produced when the operating system processes the command `cmd'.
33
34 -- This computation may fail with
35 -- PermissionDenied
36 -- The process has insufficient privileges to perform the operation.
37 -- ResourceExhausted
38 -- Insufficient resources are available to perform the operation.
39 -- UnsupportedOperation
40 -- The implementation does not support system calls.
41
42 system :: String -> IO ExitCode
43 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
44 system cmd =
45 withCString cmd $ \s -> do
46 status <- throwErrnoIfMinus1 "system" (primSystem s)
47 case status of
48 0 -> return ExitSuccess
49 n -> return (ExitFailure n)
50
51 foreign import ccall unsafe "systemCmd" primSystem :: CString -> IO Int