2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: setBuffering.c,v 1.3 1998/12/02 13:27:54 simonm 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_FLUSH) ) {
46 rc = flushBuffer(ptr);
50 /* Let go of old buffer, and reset buffer pointers. */
51 if ( fo->buf != NULL ) {
59 while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
67 input = flags == O_RDONLY || flags == O_RDWR;
69 isaterm = input && isatty(fo->fd);
73 fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;
76 /* Switch over to canonical mode. */
77 if (tcgetattr(fo->fd, &tio) < 0) {
82 tio.c_lflag &= ~ICANON;
85 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
93 fo->flags &= ~FILEOBJ_BB;
94 fo->flags |= FILEOBJ_LB;
100 while (fstat(fo->fd, &sb) < 0) {
101 /* not very likely.. */
102 if ( errno != EINTR ) {
108 size = sb.st_blksize;
112 fo->flags &= ~FILEOBJ_LB;
113 fo->flags |= FILEOBJ_BB;
120 fo->buf = malloc(size*sizeof(char));
121 if (fo->buf == NULL) {
129 * Try to switch back to cooked mode.
132 if (tcgetattr(fo->fd, &tio) < 0) {
137 tio.c_lflag |= ICANON;
138 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
147 StgInt const_BUFSIZ() { return BUFSIZ; }