2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: setBuffering.c,v 1.7 2000/03/28 08:48:44 simonmar Exp $
6 * hSetBuffering Runtime Support
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
16 #ifdef HAVE_SYS_STAT_H
33 setBuffering(StgForeignPtr ptr, StgInt size)
35 IOFileObject* fo = (IOFileObject*)ptr;
42 /* First off, flush old buffer.. */
43 if ( (fo->flags & FILEOBJ_WRITE) ) {
44 rc = flushBuffer(ptr);
48 /* Let go of old buffer, and reset buffer pointers. */
49 if ( fo->buf != NULL ) {
57 #ifndef mingw32_TARGET_OS
58 while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
66 input = flags == O_RDONLY || flags == O_RDWR;
68 isaterm = input && isatty(fo->fd);
73 fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;
75 #ifndef mingw32_TARGET_OS
77 /* Switch over to canonical mode. */
78 if (tcgetattr(fo->fd, &tio) < 0) {
83 tio.c_lflag &= ~ICANON;
86 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
95 fo->flags &= ~FILEOBJ_BB;
96 fo->flags |= FILEOBJ_LB;
101 #ifdef HAVE_ST_BLKSIZE
102 while (fstat(fo->fd, &sb) < 0) {
103 /* not very likely.. */
104 if ( errno != EINTR ) {
110 size = sb.st_blksize;
114 fo->flags &= ~FILEOBJ_LB;
115 fo->flags |= FILEOBJ_BB;
122 fo->buf = malloc(size*sizeof(char));
123 if (fo->buf == NULL) {
128 #ifndef mingw32_TARGET_OS
132 * Try to switch back to cooked mode.
135 if (tcgetattr(fo->fd, &tio) < 0) {
140 tio.c_lflag |= ICANON;
141 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
151 StgInt const_BUFSIZ() { return BUFSIZ; }