[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / getBufferMode.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1994
3 %
4 \subsection[getBufferMode.lc]{hIs...Buffered Runtime Support}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14
15 #ifdef HAVE_SYS_STAT_H
16 #include <sys/stat.h>
17 #endif
18
19 /*
20  * We try to guess what the default buffer mode is going to be based 
21  * on the type of file we're attached to.
22  */
23
24 #define GBM_NB (0)
25 #define GBM_LB (-1)
26 #define GBM_BB (-2)
27 #define GBM_ERR (-3)
28
29 StgInt
30 getBufferMode(ptr)
31 StgForeignObj ptr;
32 {
33     IOFileObject* fo = (IOFileObject*)ptr;
34     struct stat sb;
35     int fd = fo->fd;
36
37     /* Try to find out the file type */
38     while (fstat(fd, &sb) < 0) {
39         /* highly unlikely */
40         if (errno != EINTR) {
41             cvtErrno();
42             stdErrno();
43             return GBM_ERR;
44         }
45     }
46     /* Terminals are line-buffered by default */
47     if (S_ISCHR(sb.st_mode) && isatty(fd) == 1) {
48         fo ->flags |= FILEOBJ_LB;
49         return GBM_LB;
50     /* Default size block buffering for the others */
51     } else {
52         fo ->flags |= FILEOBJ_BB;
53         return GBM_BB;
54     }
55 }
56
57 \end{code}