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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9727
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Retrieve information about a FILE stream.
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2007-2008 Free Software Foundation, Inc.
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 /* Keep this code in sync with freadahead! */
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 return (const char *) fp->_IO_read_ptr;
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 return (const char *) fp->_p;
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 # define fp_ ((struct { unsigned char *_ptr; \
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 unsigned char *_base; \
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 unsigned char *_end; \
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 long _cnt; \
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 int _file; \
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 unsigned int _flag; \
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 return (const char *) fp_->_ptr;
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 return (const char *) fp->_ptr;
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 # endif
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 #elif defined __UCLIBC__ /* uClibc */
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 return (const char *) fp->__bufpos;
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 # else
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 return NULL;
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 # endif
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 return (const char *) fp->_Next;
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
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
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 #endif
7c1260f0aac3 New module 'freadptr'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 }