- errno = 0; /* Just in case fdopen() is lame */
- while ((fp = fdopen(fd, how)) == NULL) {
- if (errno != EINTR) {
- cvtErrno();
- if (created)
- (void) unlink(file);
- (void) close(fd);
- return NULL;
+
+ /* Allocate a IOFileObject to hold the information
+ we need to record per-handle for the various C stubs.
+ This chunk of memory is wrapped up inside a foreign object,
+ so it will be finalised and freed properly when we're
+ through with the handle.
+ */
+ if ((fo = malloc(sizeof(IOFileObject))) == NULL)
+ return NULL;
+
+ fo->fd = fd;
+ fo->buf = NULL;
+ fo->bufWPtr = 0;
+ fo->bufRPtr = 0;
+ fo->flags = flags | ( (how == OPENFILE_READ_ONLY || how == OPENFILE_READ_WRITE) ? FILEOBJ_READ : 0)
+ | ( (how == OPENFILE_APPEND || how == OPENFILE_READ_WRITE) ? FILEOBJ_WRITE : 0);
+ fo->connectedTo = NULL;
+ return fo;
+}
+
+/* `Lock' file descriptor and return file object. */
+IOFileObject*
+openFd(fd,oflags,flags)
+StgInt fd;
+StgInt oflags;
+StgInt flags;
+{
+ int exclusive;
+ FILE* fp;
+ IOFileObject* fo;
+
+ if (lockFile(fd, exclusive) < 0) {
+ cvtErrno();
+ switch (ghc_errno) {
+ default:
+ stdErrno();
+ break;
+ case GHC_EACCES:
+ case GHC_EAGAIN:
+ ghc_errtype = ERR_RESOURCEBUSY;
+ ghc_errstr = "file is locked";
+ break;