2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: setBuffering.c,v 1.1 1998/04/10 10:54:51 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(StgAddr fp, StgInt size)
39 while ((flags = fcntl(fileno((FILE *) fp), F_GETFL)) < 0) {
47 input = flags == O_RDONLY || flags == O_RDWR;
51 if (setvbuf((FILE *) fp, NULL, _IONBF, 0L) != 0) {
56 if (input && isatty(fileno((FILE *) fp))) {
59 * Try to switch to CBREAK mode, or whatever they call it these days.
62 if (tcgetattr(fileno((FILE *) fp), &tio) < 0) {
67 tio.c_lflag &= ~ICANON;
70 if (tcsetattr(fileno((FILE *) fp), TCSANOW, &tio) < 0) {
79 if (setvbuf((FILE *) fp, NULL, _IOLBF, BUFSIZ) != 0) {
88 * We should actually peek at the buffer size in the stat struct, if there
89 * is one. Something to occupy us later, when we're bored.
94 if (setvbuf((FILE *) fp, NULL, _IOFBF, size) != 0) {
101 if (input && isatty(fileno((FILE *) fp))) {
104 * Try to switch back to cooked mode.
107 if (tcgetattr(fileno((FILE *) fp), &tio) < 0) {
112 tio.c_lflag |= ICANON;
113 if (tcsetattr(fileno((FILE *) fp), TCSANOW, &tio) < 0) {