[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / runtime / io / 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(fp)
31 StgAddr fp;
32 {
33     struct stat sb;
34
35     /* Try to find out the file type */
36     while (fstat(fileno((FILE *) fp), &sb) < 0) {
37         /* highly unlikely */
38         if (errno != EINTR) {
39             cvtErrno();
40             stdErrno();
41             return GBM_ERR;
42         }
43     }
44     /* Terminals are line-buffered by default */
45     if (S_ISCHR(sb.st_mode) && isatty(fileno((FILE *) fp)) == 1)
46         return GBM_LB;
47     /* Default size block buffering for the others */
48     else
49         return GBM_BB;
50 }
51
52 \end{code}