[project @ 2002-07-02 10:31:35 by simonmar]
[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/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  portable
10 --
11 -- Executing an external 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 {-| 
32 Computation @system cmd@ returns the exit code
33 produced when the operating system processes the command @cmd@.
34
35 This computation may fail with
36
37    * @PermissionDenied@: The process has insufficient privileges to
38      perform the operation.
39
40    * @ResourceExhausted@: Insufficient resources are available to
41      perform the operation.
42
43    * @UnsupportedOperation@: The implementation does not support
44      system calls.
45
46 On Windows, 'system' is implemented using Windows's native system
47 call, which ignores the @SHELL@ environment variable, and always
48 passes the command to the Windows command interpreter (@CMD.EXE@ or
49 @COMMAND.COM@), hence Unixy shell tricks will not work.
50 -}
51 system :: String -> IO ExitCode
52 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
53 system cmd =
54   withCString cmd $ \s -> do
55     status <- throwErrnoIfMinus1 "system" (primSystem s)
56     case status of
57         0  -> return ExitSuccess
58         n  -> return (ExitFailure n)
59
60 foreign import ccall unsafe "systemCmd" primSystem :: CString -> IO Int