2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1996
4 \section[Posix]{Haskell 1.3 POSIX bindings}
50 -- runProcess is our candidate for the high-level OS-independent primitive
51 -- If accepted, it will be moved out of Posix into LibSystem.
53 -- ***NOTE***: make sure you completely force the evaluation of the path
54 -- and arguments to the child before calling runProcess. If you don't do
55 -- this *and* the arguments from runProcess are read in from a file lazily,
56 -- be prepared for some rather weird parent-child file I/O behaviour.
58 -- [If you don't force the args, consider the case where the
59 -- arguments emanate from a file that is read lazily, using hGetContents
60 -- or some such. Since a child of a fork() inherits the opened files of
61 -- the parent, the child can force the evaluation of the arguments and
62 -- read them off the file without any problems. The problem is that
63 -- while the child share a file table with the parent, it has
64 -- separate buffers, so a child may fill up its (copy of) the buffer, but
65 -- only read it partially. When the *parent* tries to read from the shared file again,
66 -- the (shared) file offset will have been stepped on by whatever number of chars
67 -- that was copied into the file buffer of the child. i.e., the unused parts of the
68 -- buffer will *not* be seen, resulting in random/unpredicatable results.
70 -- Based on a true (, debugged :-) story.
73 import Directory ( setCurrentDirectory )
76 runProcess :: FilePath -- Command
77 -> [String] -- Arguments
78 -> Maybe [(String, String)] -- Environment
79 -> Maybe FilePath -- Working directory
80 -> Maybe Handle -- stdin
81 -> Maybe Handle -- stdout
82 -> Maybe Handle -- stderr
84 runProcess path args env dir stdin stdout stderr = do
87 Nothing -> doTheBusiness
90 doTheBusiness :: IO ()
92 maybeChangeWorkingDirectory
96 executeFile path True args env
99 maybeChangeWorkingDirectory :: IO ()
100 maybeChangeWorkingDirectory =
103 Just x -> setCurrentDirectory x
105 maybeDup2 :: Int -> Maybe Handle -> IO ()
107 case h of Nothing -> return ()
110 dupTo src (intToFd dest)