[project @ 1998-04-10 10:49:39 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / getBufferMode.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: getBufferMode.c,v 1.1 1998/04/10 10:54:33 simonm Exp $
5  *
6  * hIs...Buffered Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "stgio.h"
11
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15
16 #ifdef HAVE_SYS_STAT_H
17 #include <sys/stat.h>
18 #endif
19
20 /*
21  * We try to guess what the default buffer mode is going to be based 
22  * on the type of file we're attached to.
23  */
24
25 #define GBM_NB (0)
26 #define GBM_LB (-1)
27 #define GBM_BB (-2)
28 #define GBM_ERR (-3)
29
30 StgInt
31 getBufferMode(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 }