/*
* (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
*
- * $Id: openFile.c,v 1.9 1999/09/19 19:15:26 sof Exp $
+ * $Id: openFile.c,v 1.18 2000/05/15 09:20:11 simonmar Exp $
*
* openFile Runtime Support
*/
#include "Rts.h"
#include "stgio.h"
-#include "fileObject.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#include <fcntl.h>
#endif
-#ifdef mingw32_TARGET_OS
+#if defined(mingw32_TARGET_OS) && !defined(O_NOCTTY)
#define O_NOCTTY 0
#endif
IOFileObject*
-openStdFile(fd,rd)
-StgInt fd;
-StgInt rd;
+openStdFile(StgInt fd, StgInt rd)
{
IOFileObject* fo;
long fd_flags;
fo->buf = NULL;
fo->bufWPtr = 0;
fo->bufRPtr = 0;
- fo->flags = FILEOBJ_STD | ( rd ? FILEOBJ_READ : (FILEOBJ_WRITE | FILEOBJ_FLUSH));
+ fo->flags = FILEOBJ_STD | ( rd ? FILEOBJ_READ : FILEOBJ_WRITE);
fo->connectedTo = NULL;
- /* MS Win32 CRT doesn't support fcntl() -- the workaround is to
- start using 'completion ports', but I'm punting on implementing
- support for using those.
- */
#if !defined(_WIN32) || defined(__CYGWIN__) || defined(__CYGWIN32__)
- /* set the non-blocking flag on this file descriptor */
- fd_flags = fcntl(fd, F_GETFL);
- fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
+ /* Set the non-blocking flag on this file descriptor.
+ *
+ * Don't do it for stdout and stderr: some shells (actually most)
+ * don't reset the nonblocking flag after running a program, and
+ * this causes all sorts of problems. --SDM (12/99)
+ *
+ * MS Win32 CRT doesn't support fcntl() -- the workaround is to
+ * start using 'completion ports', but I'm punting on implementing
+ * support for using those.
+ */
+ if (fd != 1 && fd != 2) {
+ fd_flags = fcntl(fd, F_GETFL);
+ fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK);
+ }
#endif
return fo;
#define OPENFILE_READ_WRITE 3
IOFileObject*
-openFile(file, how, binary)
-StgByteArray file;
-StgInt how;
-StgInt binary;
+openFile(StgByteArray file, StgInt how, StgInt binary)
{
- FILE *fp;
int fd;
int oflags;
int for_writing;
case OPENFILE_APPEND:
oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY | O_APPEND;
for_writing = 1;
- flags |= FILEOBJ_WRITE | FILEOBJ_FLUSH;
+ flags |= FILEOBJ_WRITE;
break;
case OPENFILE_WRITE:
oflags = O_NONBLOCK | O_WRONLY | O_NOCTTY;
- flags |= FILEOBJ_WRITE | FILEOBJ_FLUSH;
+ flags |= FILEOBJ_WRITE;
for_writing = 1;
break;
case OPENFILE_READ_ONLY:
break;
case OPENFILE_READ_WRITE:
oflags = O_NONBLOCK | O_RDWR | O_NOCTTY;
- flags |= FILEOBJ_READ | FILEOBJ_WRITE | FILEOBJ_FLUSH;
+ flags |= FILEOBJ_READ | FILEOBJ_WRITE;
for_writing = 1;
break;
default:
/* `Lock' file descriptor and return file object. */
IOFileObject*
-openFd(fd,oflags,flags)
-StgInt fd;
-StgInt oflags;
-StgInt flags;
+openFd(StgInt fd, StgInt oflags, StgInt flags)
{
int for_writing;
- FILE* fp;
IOFileObject* fo;
for_writing = ( ((oflags & O_WRONLY) || (oflags & O_RDWR)) ? 1 : 0);
/* See openFileObject() comment */
if ((fo = malloc(sizeof(IOFileObject))) == NULL)
return NULL;
- fo->fd = fd;
- fo->buf = NULL;
- fo->bufWPtr = 0;
- fo->bufRPtr = 0;
- fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
+ fo->fd = fd;
+ fo->buf = NULL;
+ fo->bufWPtr = 0;
+ fo->bufRPtr = 0;
+ fo->flags = flags | ( oflags & O_RDONLY ? FILEOBJ_READ
: oflags & O_RDWR ? FILEOBJ_READ
: 0)
| ( oflags & O_WRONLY ? FILEOBJ_WRITE