01b0881f755454915782e34fe5d095c432d49efd
[gnulib.git] / lib / fbufmode.c
1 /* Retrieve information about a FILE stream.
2    Copyright (C) 2007-2011 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 #include "stdio-impl.h"
27
28 int
29 fbufmode (FILE *fp)
30 {
31   /* Most systems provide FILE as a struct and the necessary bitmask in
32      <stdio.h>, because they need it for implementing getc() and putc() as
33      fast macros.  */
34 #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
35 # if HAVE___FLBF                    /* glibc >= 2.2 */
36   if (__flbf (fp))
37     return _IOLBF;
38 # else
39   if (fp->_flags & _IO_LINE_BUF)
40     return _IOLBF;
41 # endif
42   if (fp->_flags & _IO_UNBUFFERED)
43     return _IONBF;
44   return _IOFBF;
45 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */
46   if (fp_->_flags & __SLBF)
47     return _IOLBF;
48   if (fp_->_flags & __SNBF)
49     return _IONBF;
50   return _IOFBF;
51 #elif defined __EMX__               /* emx+gcc */
52   return fp->_flags & (_IOLBF | _IONBF | _IOFBF);
53 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
54 # if HAVE___FLBF                    /* Solaris >= 7 */
55   if (__flbf (fp))
56     return _IOLBF;
57 # else
58   if (fp->_flag & _IOLBF)
59     return _IOLBF;
60 # endif
61   if (fp_->_flag & _IONBF)
62     return _IONBF;
63   return _IOFBF;
64 #elif defined __UCLIBC__            /* uClibc */
65   if (fp->__modeflags & __FLAG_LBF)
66     return _IOLBF;
67   if (fp->__modeflags & __FLAG_NBF)
68     return _IONBF;
69   return _IOFBF;
70 #elif defined __QNX__               /* QNX */
71   if (fp->_Mode & 0x400 /* _MLBF */)
72     return _IOLBF;
73   if (fp->_Mode & 0x800 /* _MNBF */)
74     return _IONBF;
75   return _IOFBF;
76 #elif defined __MINT__              /* Atari FreeMiNT */
77   if (fp->__linebuf)
78     return _IOLBF;
79   return (fp->__bufsize > 0 ? _IOFBF : _IONBF);
80 #else
81 # error "Please port gnulib fbufmode.c to your platform! Look at the setvbuf implementation."
82 #endif
83 }