Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/freadahead.c @ 9881:e21211210418
Port the extended stdio functions to emx+gcc.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 10 Apr 2008 00:56:00 +0200 |
parents | 057a5220dfd5 |
children | 9a02133ad731 |
rev | line source |
---|---|
9158 | 1 /* Retrieve information about a FILE stream. |
9780
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
2 Copyright (C) 2007-2008 Free Software Foundation, Inc. |
9158 | 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 | 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 | 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 | |
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 | 16 |
17 #include <config.h> | |
18 | |
19 /* Specification. */ | |
20 #include "freadahead.h" | |
21 | |
22 size_t | |
23 freadahead (FILE *fp) | |
24 { | |
25 #if defined _IO_ferror_unlocked /* GNU libc, BeOS */ | |
26 if (fp->_IO_write_ptr > fp->_IO_write_base) | |
27 return 0; | |
9780
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
28 return (fp->_IO_read_end - fp->_IO_read_ptr) |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
29 + (fp->_flags & _IO_IN_BACKUP ? fp->_IO_save_end - fp->_IO_save_base : |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
30 0); |
9158 | 31 #elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */ |
9780
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
32 # if defined __NetBSD__ || defined __OpenBSD__ |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
33 struct __sfileext |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
34 { |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
35 struct __sbuf _ub; /* ungetc buffer */ |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
36 /* More fields, not relevant here. */ |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
37 }; |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
38 # define HASUB(fp) (((struct __sfileext *) (fp)->_ext._base)->_ub._base != NULL) |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
39 # else |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
40 # define HASUB(fp) ((fp)->_ub._base != NULL) |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
41 # endif |
9158 | 42 if ((fp->_flags & __SWR) != 0 || fp->_r < 0) |
43 return 0; | |
9780
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
44 return fp->_r |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
45 + (HASUB (fp) ? fp->_ur : 0); |
9881
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
46 #elif defined __EMX__ /* emx+gcc */ |
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
47 if ((fp->_flags & _IOWRT) != 0) |
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
48 return 0; |
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
49 /* Note: fp->_ungetc_count > 0 implies fp->_rcount <= 0, |
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
50 fp->_ungetc_count = 0 implies fp->_rcount >= 0. */ |
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
51 /* equivalent to |
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
52 (fp->_ungetc_count == 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount) */ |
e21211210418
Port the extended stdio functions to emx+gcc.
Bruno Haible <bruno@clisp.org>
parents:
9876
diff
changeset
|
53 return (fp->_rcount > 0 ? fp->_rcount : fp->_ungetc_count - fp->_rcount); |
9876
057a5220dfd5
Add tentative support for OpenServer.
Bruno Haible <bruno@clisp.org>
parents:
9780
diff
changeset
|
54 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */ |
9158 | 55 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */ |
56 # define fp_ ((struct { unsigned char *_ptr; \ | |
57 unsigned char *_base; \ | |
58 unsigned char *_end; \ | |
59 long _cnt; \ | |
60 int _file; \ | |
61 unsigned int _flag; \ | |
62 } *) fp) | |
63 if ((fp_->_flag & _IOWRT) != 0) | |
64 return 0; | |
65 return fp_->_cnt; | |
66 # else | |
9876
057a5220dfd5
Add tentative support for OpenServer.
Bruno Haible <bruno@clisp.org>
parents:
9780
diff
changeset
|
67 # if defined _SCO_DS /* OpenServer */ |
057a5220dfd5
Add tentative support for OpenServer.
Bruno Haible <bruno@clisp.org>
parents:
9780
diff
changeset
|
68 # define _flag __flag |
057a5220dfd5
Add tentative support for OpenServer.
Bruno Haible <bruno@clisp.org>
parents:
9780
diff
changeset
|
69 # define _cnt __cnt |
057a5220dfd5
Add tentative support for OpenServer.
Bruno Haible <bruno@clisp.org>
parents:
9780
diff
changeset
|
70 # endif |
9158 | 71 if ((fp->_flag & _IOWRT) != 0) |
72 return 0; | |
73 return fp->_cnt; | |
74 # endif | |
75 #elif defined __UCLIBC__ /* uClibc */ | |
76 # ifdef __STDIO_BUFFERS | |
77 if (fp->__modeflags & __FLAG_WRITING) | |
78 return 0; | |
9780
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
79 return (fp->__bufread - fp->__bufpos) |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
80 + (fp->__modeflags & __FLAG_UNGOT ? 1 : 0); |
9158 | 81 # else |
82 return 0; | |
83 # endif | |
9280
a1f6fe4c68c1
Add support for QNX (untested).
Bruno Haible <bruno@clisp.org>
parents:
9158
diff
changeset
|
84 #elif defined __QNX__ /* QNX */ |
9456 | 85 if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0) |
9280
a1f6fe4c68c1
Add support for QNX (untested).
Bruno Haible <bruno@clisp.org>
parents:
9158
diff
changeset
|
86 return 0; |
9780
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
87 /* fp->_Buf <= fp->_Next <= fp->_Rend, |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
88 and fp->_Rend may be overridden by fp->_Rsave. */ |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
89 return ((fp->_Rsave ? fp->_Rsave : fp->_Rend) - fp->_Next) |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
90 + (fp->_Mode & 0x4000 /* _MBYTE */ |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
91 ? (fp->_Back + sizeof (fp->_Back)) - fp->_Rback |
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
92 : 0); |
9158 | 93 #else |
9780
cf325a112b9d
Take into account the number of pushed-back bytes (ungetc).
Bruno Haible <bruno@clisp.org>
parents:
9456
diff
changeset
|
94 #error "Please port gnulib freadahead.c to your platform! Look at the definition of fflush, fread, ungetc on your system, then report this to bug-gnulib." |
9158 | 95 #endif |
96 } |