[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / misc / examples / posix / po012 / Main.hs
1 import LibPosix
2
3 main =
4     createFile "po012.out" stdFileMode >>= \ fd ->
5     installHandler processStatusChanged (Catch (reap1 fd)) Nothing >>
6     ls >>
7     awaitSignal Nothing
8
9 ls =
10     runProcess "ls" ["-l","po012.out"] Nothing Nothing Nothing Nothing Nothing
11
12 reap1 fd =
13     getAnyProcessStatus True False >>
14     installHandler processStatusChanged (Catch (reap2 fd)) Nothing >>
15     writeChannel fd (take 666 (repeat 'x')) >>
16     ls >>
17     awaitSignal Nothing
18     
19 reap2 fd =
20     getAnyProcessStatus True False >>
21     installHandler processStatusChanged (Catch (reap3 fd)) Nothing >>
22     setFileMode "po012.out" 
23         (foldr1 unionFileModes [ownerReadMode,ownerWriteMode,groupReadMode,otherReadMode]) >>
24     ls >>
25     awaitSignal Nothing
26     
27 reap3 fd =
28     getAnyProcessStatus True False >>
29     installHandler processStatusChanged (Catch (reap4 fd)) Nothing >>
30     setFileTimes "po012.out" 0 0 >>
31     ls >>
32     awaitSignal Nothing
33     
34 reap4 fd =
35     getAnyProcessStatus True False >>
36     installHandler processStatusChanged (Catch (reap5 fd)) Nothing >>
37     removeLink "po012.out" >>
38     ls >>
39     awaitSignal Nothing
40
41 reap5 fd =
42     getAnyProcessStatus True False >>
43     seekChannel fd SeekFromEnd 0 >>= \ bytes ->
44     if bytes == 666 then
45         seekChannel fd AbsoluteSeek 0 >>
46         readChannel fd 1024 >>= \ (str, _) ->
47         if str == (take 666 (repeat 'x')) then
48             putStr "Okay\n"
49         else
50             putStr "Read failed\n"
51     else
52         putStr "Seek returned wrong size\n"