re-opening a closed stdin?

Simon Marlow simonmar@microsoft.com
Thu, 21 Nov 2002 11:57:29 -0000


This is a multi-part message in MIME format.

------_=_NextPart_001_01C29155.2AAA3C44
Content-Type: text/plain;
	charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

> > You can call 'System.Posix.IO.dup stdin' and save this value.
> > However, I think you then need to explicitely read from this fd as
> > it is not possible to reset what GHC thinks stdin is currently to
> > this new fd (I'll dig into this and maybe we'll get a
> >    setStdin :: Fd -> IO ()
> > from this, IIRC somebody else was asking for this, too).
>=20
> How does this interact with Simon's proposal for hDuplicate?

If you actually want to change what stdin/stdout/stderr refer to, or
re-open one of these file descriptors after it has been closed, then you
need hDuplicateTo.

I've enclosed a version of these functions which works with GHC 5.04.1.

Cheers,
	Simon

------_=_NextPart_001_01C29155.2AAA3C44
Content-Type: application/octet-stream;
	name="hdup.hs"
Content-Transfer-Encoding: base64
Content-Description: hdup.hs
Content-Disposition: attachment;
	filename="hdup.hs"

bW9kdWxlIEhEdXBsaWNhdGUgKGhEdXBsaWNhdGUsIGhEdXBsaWNhdGVUbykgd2hlcmUKCmltcG9y
dCBHSEMuSU9CYXNlCmltcG9ydCBHSEMuQ29uYwppbXBvcnQgR0hDLkhhbmRsZQppbXBvcnQgR0hD
LlBvc2l4CgotLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQotLSBEdXBsaWNhdGluZyBhIEhhbmRsZQoK
LS0gfFJldHVybnMgYSBkdXBsaWNhdGUgb2YgdGhlIG9yaWdpbmFsIGhhbmRsZSwgd2l0aCBpdHMg
b3duIGJ1ZmZlcgotLSBhbmQgZmlsZSBwb2ludGVyLiAgVGhlIG9yaWdpbmFsIGhhbmRsZSdzIGJ1
ZmZlciBpcyBmbHVzaGVkLCBpbmNsdWRpbmcKLS0gZGlzY2FyZGluZyBhbnkgaW5wdXQgZGF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