2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: setBuffering.c,v 1.6 1999/11/25 16:54:15 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(ptr, size)
37 IOFileObject* fo = (IOFileObject*)ptr;
44 /* First off, flush old buffer.. */
45 if ( (fo->flags & FILEOBJ_WRITE) ) {
46 rc = flushBuffer(ptr);
50 /* Let go of old buffer, and reset buffer pointers. */
51 if ( fo->buf != NULL ) {
59 #ifndef mingw32_TARGET_OS
60 while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
68 input = flags == O_RDONLY || flags == O_RDWR;
70 isaterm = input && isatty(fo->fd);
75 fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;
77 #ifndef mingw32_TARGET_OS
79 /* Switch over to canonical mode. */
80 if (tcgetattr(fo->fd, &tio) < 0) {
85 tio.c_lflag &= ~ICANON;
88 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
97 fo->flags &= ~FILEOBJ_BB;
98 fo->flags |= FILEOBJ_LB;
103 #ifdef HAVE_ST_BLKSIZE
104 while (fstat(fo->fd, &sb) < 0) {
105 /* not very likely.. */
106 if ( errno != EINTR ) {
112 size = sb.st_blksize;
116 fo->flags &= ~FILEOBJ_LB;
117 fo->flags |= FILEOBJ_BB;
124 fo->buf = malloc(size*sizeof(char));
125 if (fo->buf == NULL) {
130 #ifndef mingw32_TARGET_OS
134 * Try to switch back to cooked mode.
137 if (tcgetattr(fo->fd, &tio) < 0) {
142 tio.c_lflag |= ICANON;
143 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
153 StgInt const_BUFSIZ() { return BUFSIZ; }