Add tentative support for OpenServer.
[gnulib.git] / lib / fbufmode.c
1 /* Retrieve information about a FILE stream.
2    Copyright (C) 2007-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 /* Specification.  */
20 #include "fbufmode.h"
21
22 #if HAVE___FLBF
23 # include <stdio_ext.h>
24 #endif
25
26 int
27 fbufmode (FILE *fp)
28 {
29   /* Most systems provide FILE as a struct and the necessary bitmask in
30      <stdio.h>, because they need it for implementing getc() and putc() as
31      fast macros.  */
32 #if defined _IO_ferror_unlocked     /* GNU libc, BeOS */
33 # if HAVE___FLBF                    /* glibc >= 2.2 */
34   if (__flbf (fp))
35     return _IOLBF;
36 # else
37   if (fp->_flags & _IO_LINE_BUF)
38     return _IOLBF;
39 # endif
40   if (fp->_flags & _IO_UNBUFFERED)
41     return _IONBF;
42   return _IOFBF;
43 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
44   if (fp->_flags & __SLBF)
45     return _IOLBF;
46   if (fp->_flags & __SNBF)
47     return _IONBF;
48   return _IOFBF;
49 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
50 # if HAVE___FLBF                    /* Solaris >= 7 */
51   if (__flbf (fp))
52     return _IOLBF;
53 # else
54   if (fp->_flag & _IOLBF)
55     return _IOLBF;
56 # endif
57 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
58 #  define fp_ ((struct { unsigned char *_ptr; \
59                          unsigned char *_base; \
60                          unsigned char *_end; \
61                          long _cnt; \
62                          int _file; \
63                          unsigned int _flag; \
64                        } *) fp)
65   return fp_->_flag & (_IONBF | _IOFBF);
66 # else
67 #  if defined _SCO_DS               /* OpenServer */
68 #   define _flag __flag
69 #  endif
70   if (fp->_flag & _IONBF)
71     return _IONBF;
72   return _IOFBF;
73 # endif
74 #elif defined __UCLIBC__            /* uClibc */
75   if (fp->__modeflags & __FLAG_LBF)
76     return _IOLBF;
77   if (fp->__modeflags & __FLAG_NBF)
78     return _IONBF;
79   return _IOFBF;
80 #elif defined __QNX__               /* QNX */
81   if (fp->_Mode & 0x400 /* _MLBF */)
82     return _IOLBF;
83   if (fp->_Mode & 0x800 /* _MNBF */)
84     return _IONBF;
85   return _IOFBF;
86 #else
87  #error "Please port gnulib fbufmode.c to your platform! Look at the setvbuf implementation."
88 #endif
89 }