2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
4 \subsection[setBuffering.lc]{hSetBuffering Runtime Support}
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
15 #ifdef HAVE_SYS_STAT_H
32 setBuffering(ptr, size)
36 IOFileObject* fo = (IOFileObject*)ptr;
43 /* First off, flush old buffer.. */
44 if ( (fo->flags & FILEOBJ_FLUSH) ) {
45 rc = flushBuffer(ptr);
49 /* Let go of old buffer, and reset buffer pointers. */
50 if ( fo->buf != NULL ) {
58 while ((flags = fcntl(fo->fd, F_GETFL)) < 0) {
66 input = flags == O_RDONLY || flags == O_RDWR;
68 isaterm = input && isatty(fo->fd);
72 fo->flags &= ~FILEOBJ_LB & ~FILEOBJ_BB;
75 /* Switch over to canonical mode. */
76 if (tcgetattr(fo->fd, &tio) < 0) {
81 tio.c_lflag &= ~ICANON;
84 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {
92 fo->flags &= ~FILEOBJ_BB;
93 fo->flags |= FILEOBJ_LB;
99 while (fstat(fo->fd, &sb) < 0) {
100 /* not very likely.. */
101 if ( errno != EINTR ) {
107 size = sb->st_blksize;
111 fo->flags &= ~FILEOBJ_LB;
112 fo->flags |= FILEOBJ_BB;
119 fo->buf = malloc(size*sizeof(char));
120 if (fo->buf == NULL) {
128 * Try to switch back to cooked mode.
131 if (tcgetattr(fo->fd, &tio) < 0) {
136 tio.c_lflag |= ICANON;
137 if (tcsetattr(fo->fd, TCSANOW, &tio) < 0) {