annotate lib/freadahead.c @ 9309:bbbbbf4cd1c5

Change copyright notice from GPLv2+ to GPLv3+.
author Bruno Haible <bruno@clisp.org>
date Sun, 07 Oct 2007 19:14:58 +0200
parents a1f6fe4c68c1
children b3c8ee11ddfa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9158
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Retrieve information about a FILE stream.
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2007 Free Software Foundation, Inc.
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9280
diff changeset
4 This program is free software: you can redistribute it and/or modify
9158
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9280
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9280
diff changeset
7 (at your option) any later version.
9158
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9280
diff changeset
14 You should have received a copy of the GNU General Public License
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 9280
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9158
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include "freadahead.h"
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 size_t
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 freadahead (FILE *fp)
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 {
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #if defined _IO_ferror_unlocked /* GNU libc, BeOS */
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 if (fp->_IO_write_ptr > fp->_IO_write_base)
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 return 0;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 return fp->_IO_read_end - fp->_IO_read_ptr;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 if ((fp->_flags & __SWR) != 0 || fp->_r < 0)
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 return 0;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 return fp->_r;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 # define fp_ ((struct { unsigned char *_ptr; \
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 unsigned char *_base; \
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 unsigned char *_end; \
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 long _cnt; \
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 int _file; \
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 unsigned int _flag; \
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 } *) fp)
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 if ((fp_->_flag & _IOWRT) != 0)
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 return 0;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 return fp_->_cnt;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 # else
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 if ((fp->_flag & _IOWRT) != 0)
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 return 0;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 return fp->_cnt;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 # endif
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #elif defined __UCLIBC__ /* uClibc */
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 # ifdef __STDIO_BUFFERS
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 if (fp->__modeflags & __FLAG_WRITING)
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 return 0;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 return fp->__bufread - fp->__bufpos;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 # else
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 return 0;
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 # endif
9280
a1f6fe4c68c1 Add support for QNX (untested).
Bruno Haible <bruno@clisp.org>
parents: 9158
diff changeset
58 #elif defined __QNX__ /* QNX */
a1f6fe4c68c1 Add support for QNX (untested).
Bruno Haible <bruno@clisp.org>
parents: 9158
diff changeset
59 if ((fp->_Mode & _MWRITE) != 0)
a1f6fe4c68c1 Add support for QNX (untested).
Bruno Haible <bruno@clisp.org>
parents: 9158
diff changeset
60 return 0;
a1f6fe4c68c1 Add support for QNX (untested).
Bruno Haible <bruno@clisp.org>
parents: 9158
diff changeset
61 /* fp->_Buf <= fp->_Next <= fp->_Rend */
a1f6fe4c68c1 Add support for QNX (untested).
Bruno Haible <bruno@clisp.org>
parents: 9158
diff changeset
62 return fp->_Rend - fp->_Next;
9158
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 #else
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread on your system, then report this to bug-gnulib."
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 #endif
d45f26a2b8ff New module 'freadahead'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 }