2 * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
4 * $Id: setBuffering.c,v 1.9 2001/01/16 14:06:14 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;
41 /* First off, flush old buffer.. */
42 if ( (fo->flags & FILEOBJ_WRITE) ) {
43 rc = flushBuffer(ptr);
47 /* Let go of old buffer, and reset buffer pointers. */
48 if ( fo->buf != NULL ) {
56 #ifndef mingw32_TARGET_OS
57 while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
65 input = flags == O_RDONLY || flags == O_RDWR;
67 isaterm = input && isatty(fo->fd);
72 fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;
74 #ifndef mingw32_TARGET_OS
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) {
94 fo->flags &= ~FILEOBJ_BB;
95 fo->flags |= FILEOBJ_LB;
100 #ifdef HAVE_ST_BLKSIZE
101 while (fstat(fo->fd, &sb) < 0) {
102 /* not very likely.. */
103 if ( errno != EINTR ) {
109 size = sb.st_blksize;
113 fo->flags &= ~FILEOBJ_LB;
114 fo->flags |= FILEOBJ_BB;
121 fo->buf = malloc(size*sizeof(char));
122 if (fo->buf == NULL) {
127 #ifndef mingw32_TARGET_OS
131 * Try to switch back to cooked mode.
134 if (tcgetattr(fo->fd, &tio) < 0) {
139 tio.c_lflag |= ICANON;
140 if (tcSetAttr(fo->fd, TCSANOW, &tio) < 0) {
150 StgInt const_BUFSIZ() { return BUFSIZ; }