1 -----------------------------------------------------------------------------
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- Executing an external command.
13 -----------------------------------------------------------------------------
16 ( system, -- :: String -> IO ExitCode
17 #ifdef __GLASGOW_HASKELL__
18 rawSystem, -- :: FilePath -> [String] -> IO ExitCode
24 #ifdef __GLASGOW_HASKELL__
37 import System (system)
40 -- ---------------------------------------------------------------------------
44 Computation @system cmd@ returns the exit code
45 produced when the operating system processes the command @cmd@.
47 This computation may fail with
49 * @PermissionDenied@: The process has insufficient privileges to
50 perform the operation.
52 * @ResourceExhausted@: Insufficient resources are available to
53 perform the operation.
55 * @UnsupportedOperation@: The implementation does not support
58 On Windows, 'system' is implemented using Windows's native system
59 call, which ignores the @SHELL@ environment variable, and always
60 passes the command to the Windows command interpreter (@CMD.EXE@ or
61 @COMMAND.COM@), hence Unixy shell tricks will not work.
63 #ifdef __GLASGOW_HASKELL__
64 system :: String -> IO ExitCode
65 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
67 withCString cmd $ \s -> do
68 status <- throwErrnoIfMinus1 "system" (primSystem s)
70 0 -> return ExitSuccess
71 n -> return (ExitFailure n)
73 foreign import ccall unsafe "systemCmd" primSystem :: CString -> IO Int
76 The same as 'system', but bypasses the shell (GHC only).
77 Will behave more portably between systems,
78 because there is no interpretation of shell metasyntax.
81 rawSystem :: FilePath -> [String] -> IO ExitCode
83 #ifndef mingw32_TARGET_OS
86 withCString cmd $ \pcmd ->
87 withMany withCString (cmd:args) $ \cstrs ->
88 withArray0 nullPtr cstrs $ \arr -> do
89 status <- throwErrnoIfMinus1 "rawSystem" (c_rawSystem pcmd arr)
91 0 -> return ExitSuccess
92 n -> return (ExitFailure n)
94 foreign import ccall unsafe "rawSystem"
95 c_rawSystem :: CString -> Ptr CString -> IO Int
99 -- On Windows, the command line is passed to the operating system as
100 -- a single string. Command-line parsing is done by the executable
102 rawSystem cmd args = do
103 let cmdline = translate cmd ++ concat (map ((' ':) . translate) args)
104 withCString cmdline $ \pcmdline -> do
105 status <- throwErrnoIfMinus1 "rawSystem" (c_rawSystem pcmdline)
107 0 -> return ExitSuccess
108 n -> return (ExitFailure n)
110 translate :: String -> String
111 translate str = '"' : foldr escape "\"" str
112 where escape '"' str = '\\' : '"' : str
113 escape '\\' str = '\\' : '\\' : str
114 escape c str = c : str
116 foreign import ccall unsafe "rawSystem"
117 c_rawSystem :: CString -> IO Int
121 #endif /* __GLASGOW_HASKELL__ */