[project @ 2001-05-18 14:18:34 by simonmar]
[ghc-hetmet.git] / ghc / tests / io / should_run / io008.hs
1 -- !!! Test file positioning
2
3 module Main(main) where
4
5 import IO
6 import Monad
7
8 import Directory (removeFile, doesFileExist)
9
10 main = do
11   hIn <- openFile "io008.in" ReadMode
12   f <- doesFileExist "io008.out"
13   when f (removeFile "io008.out")
14   hOut <- openFile "io008.out" ReadWriteMode
15   bof <- hGetPosn hIn
16   copy hIn hOut
17   hSetPosn bof
18   copy hIn hOut
19   hSeek hOut AbsoluteSeek 0
20   stuff <- hGetContents hOut
21   putStr stuff
22
23 copy :: Handle -> Handle -> IO ()
24 copy hIn hOut =
25     try (hGetChar hIn) >>=
26     either (\ err -> if isEOFError err then return () else error "copy")
27            ( \ x -> hPutChar hOut x >> copy hIn hOut)