Mercurial > hg > octave-kai > gnulib-hg
annotate lib/freadptr.c @ 9779:569a44f4dd4d
Extend freadptr to return also the buffer size.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Mon, 10 Mar 2008 00:23:20 +0100 |
parents | 7c1260f0aac3 |
children | 057a5220dfd5 |
rev | line source |
---|---|
9727 | 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. */ | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
20 #include "freadptr.h" |
9727 | 21 |
22 const char * | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
23 freadptr (FILE *fp, size_t *sizep) |
9727 | 24 { |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
25 size_t size; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
26 |
9727 | 27 /* Keep this code in sync with freadahead! */ |
28 #if defined _IO_ferror_unlocked /* GNU libc, BeOS */ | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
29 if (fp->_IO_write_ptr > fp->_IO_write_base) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
30 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
31 size = fp->_IO_read_end - fp->_IO_read_ptr; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
32 if (size == 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
33 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
34 *sizep = size; |
9727 | 35 return (const char *) fp->_IO_read_ptr; |
36 #elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */ | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
37 if ((fp->_flags & __SWR) != 0 || fp->_r < 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
38 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
39 size = fp->_r; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
40 if (size == 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
41 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
42 *sizep = size; |
9727 | 43 return (const char *) fp->_p; |
44 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */ | |
45 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */ | |
46 # define fp_ ((struct { unsigned char *_ptr; \ | |
47 unsigned char *_base; \ | |
48 unsigned char *_end; \ | |
49 long _cnt; \ | |
50 int _file; \ | |
51 unsigned int _flag; \ | |
52 } *) fp) | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
53 if ((fp_->_flag & _IOWRT) != 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
54 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
55 size = fp_->_cnt; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
56 if (size == 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
57 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
58 *sizep = size; |
9727 | 59 return (const char *) fp_->_ptr; |
60 # else | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
61 if ((fp->_flag & _IOWRT) != 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
62 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
63 size = fp->_cnt; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
64 if (size == 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
65 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
66 *sizep = size; |
9727 | 67 return (const char *) fp->_ptr; |
68 # endif | |
69 #elif defined __UCLIBC__ /* uClibc */ | |
70 # ifdef __STDIO_BUFFERS | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
71 if (fp->__modeflags & __FLAG_WRITING) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
72 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
73 size = fp->__bufread - fp->__bufpos; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
74 if (size == 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
75 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
76 *sizep = size; |
9727 | 77 return (const char *) fp->__bufpos; |
78 # else | |
79 return NULL; | |
80 # endif | |
81 #elif defined __QNX__ /* QNX */ | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
82 if ((fp->_Mode & 0x2000 /* _MWRITE */) != 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
83 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
84 /* fp->_Buf <= fp->_Next <= fp->_Rend */ |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
85 size = fp->_Rend - fp->_Next; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
86 if (size == 0) |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
87 return NULL; |
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
88 *sizep = size; |
9727 | 89 return (const char *) fp->_Next; |
90 #else | |
9779
569a44f4dd4d
Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
9727
diff
changeset
|
91 #error "Please port gnulib freadptr.c to your platform! Look at the definition of fflush, fread, getc, getc_unlocked on your system, then report this to bug-gnulib." |
9727 | 92 #endif |
93 } |