/*
* (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
*
- * $Id: closeFile.c,v 1.3 1998/12/02 13:27:14 simonm Exp $
+ * $Id: closeFile.c,v 1.10 2000/09/25 10:48:50 simonmar Exp $
*
* hClose Runtime Support
*/
#include "Rts.h"
#include "stgio.h"
+#include <errno.h>
+
+#if defined(HAVE_WINSOCK_H) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
+#define USE_WINSOCK
+#endif
+
+#ifdef USE_WINSOCK
+#include <winsock.h>
+#endif
StgInt __really_close_stdfiles=1;
StgInt
-closeFile(ptr,flush_buf)
-StgForeignPtr ptr;
-StgInt flush_buf;
+closeFile(StgForeignPtr ptr, StgInt flush_buf)
{
IOFileObject* fo = (IOFileObject*)ptr;
int rc = 0;
return 0;
}
- if ( flush_buf != 0 && (fo->flags & FILEOBJ_FLUSH) ) {
- writeFileObject(ptr,fo->bufWPtr);
+ /* Flush buffer if we have unwritten data */
+ if ( flush_buf != 0 ) {
+ flushBuffer(fo);
}
/* If the flush failed, we ignore this and soldier on.. */
}
+ /* Free the buffer straight away. We can't free the file object
+ * itself until the finalizer runs.
+ */
+ if ( fo->buf != NULL ) {
+ free(fo->buf);
+ fo->buf = NULL;
+ }
+
/* Closing file descriptors that refer to standard channels
is problematic, so we back off from doing this by default,
just closing them at the Handle level. If you insist on
} else {
/* Regardless of success or otherwise, the fd field gets smashed. */
- while ( (rc = close(fo->fd)) != 0 ) {
+ while ( (rc =
+ (
+#ifdef USE_WINSOCK
+ fo->flags & FILEOBJ_WINSOCK ?
+ closesocket(fo->fd) :
+ close(fo->fd))) != 0 ) {
+#else
+ close(fo->fd))) != 0 ) {
+#endif
/* See above unlockFile() comment */
if ( errno != EINTR && (!unlocked && errno != EBADF ) ) {
cvtErrno();
}
}
}
+
fo->fd = -1;
+
return 0;
}