2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: setBuffering.c,v 1.11 2001/03/01 12:25:33 rrt 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;
38 #ifndef mingw32_TARGET_OS
43 /* First off, flush old buffer.. */
44 if ( (fo->flags & FILEOBJ_WRITE) ) {
45 rc = flushBuffer(ptr);
49 /* Let go of old buffer, and reset buffer pointers. */
50 if ( fo->buf != NULL ) {
58 #ifndef mingw32_TARGET_OS
59 while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
67 input = flags == O_RDONLY || flags == O_RDWR;
69 isaterm = input && isatty(fo->fd);
74 fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;
76 #ifndef mingw32_TARGET_OS
78 /* Switch over to canonical mode. */
79 if (tcgetattr(fo->fd, &tio) < 0) {
84 tio.c_lflag &= ~ICANON;
87 if (tcSetAttr(fo->fd, TCSANOW, &tio) < 0) {
96 fo->flags &= ~FILEOBJ_BB;
97 fo->flags |= FILEOBJ_LB;
102 #ifdef HAVE_ST_BLKSIZE
103 while (Fstat(fo->fd, &sb) < 0) {
104 /* not very likely.. */
105 if ( errno != EINTR ) {
111 size = sb.st_blksize;
115 fo->flags &= ~FILEOBJ_LB;
116 fo->flags |= FILEOBJ_BB;
123 fo->buf = malloc(size*sizeof(char));
124 if (fo->buf == NULL) {
129 #ifndef mingw32_TARGET_OS
133 * Try to switch back to cooked mode.
136 if (tcgetattr(fo->fd, &tio) < 0) {
141 tio.c_lflag |= ICANON;
142 if (tcSetAttr(fo->fd, TCSANOW, &tio) < 0) {
152 StgInt const_BUFSIZ() { return BUFSIZ; }