System.Process

Glynn Clements glynn.clements at virgin.net
Thu Jul 8 02:06:01 EDT 2004


Peter Simons wrote:

> I have tried to use the various POpen-like implementations
> out there, but _none_ of them works with the most current
> GHC. Is there any way to communicate with external programs
> at the moment? Posix-only would be suffice at the moment.

There's the DIY solution, i.e. a direct translation of how popen() is
implemented in C, e.g.:

> module Main where
> 
> import IO
> import System
> import System.Posix
> 
> main = do
> 	(rfd, wfd) <- createPipe
> 	pid <- forkProcess (proc rfd wfd)
> 	closeFd wfd
> 	hRead <- fdToHandle rfd
> 	str <- hGetContents hRead
> 	putStr str
> 	stat <- getProcessStatus True False pid
> 	case stat of
> 		(Just (Exited code)) -> exitWith code
> 		_ -> exitWith (ExitFailure 127)
> 	where proc rfd wfd = do
> 		closeFd rfd
> 		dupTo wfd stdOutput
> 		executeFile "/bin/ls" False ["-l"] Nothing

The above has been (briefly) tested with ghc 6.2.1.

-- 
Glynn Clements <glynn.clements at virgin.net>


More information about the Libraries mailing list