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