1 /* Skipping input from a FILE stream.
2 Copyright (C) 2007-2008 Free Software Foundation, Inc.
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.
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.
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/>. */
20 #include "freadseek.h"
28 freadseek (FILE *fp, size_t offset)
36 /* Increment the in-memory pointer. This is very cheap (no system calls). */
37 if (freadptr (fp, &buffered) != NULL && buffered > 0)
39 size_t increment = (buffered < offset ? buffered : offset);
41 /* Keep this code in sync with freadptr! */
42 #if defined _IO_ferror_unlocked /* GNU libc, BeOS */
43 fp->_IO_read_ptr += increment;
44 #elif defined __sferror /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
47 #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, mingw */
48 # if defined __sun && defined _LP64 /* Solaris/{SPARC,AMD64} 64-bit */
49 # define fp_ ((struct { unsigned char *_ptr; \
50 unsigned char *_base; \
51 unsigned char *_end; \
56 fp_->_ptr += increment;
57 fp_->_cnt -= increment;
59 fp->_ptr += increment;
60 fp->_cnt -= increment;
62 #elif defined __UCLIBC__ /* uClibc */
63 # ifdef __STDIO_BUFFERS
64 fp->__bufpos += increment;
68 #elif defined __QNX__ /* QNX */
69 fp->_Next += increment;
71 #error "Please port gnulib freadseek.c to your platform! Look at the definition of getc, getc_unlocked on your system, then report this to bug-gnulib."
79 /* Test whether the stream is seekable or not. */
81 if (fd >= 0 && lseek (fd, 0, SEEK_CUR) >= 0)
83 /* FP refers to a regular file. fseek is most efficient in this case. */
84 return fseek (fp, offset, SEEK_CUR);
88 /* FP is a non-seekable stream, possibly not even referring to a file
89 descriptor. Read OFFSET bytes explicitly and discard them. */
94 size_t count = (sizeof (buf) < offset ? sizeof (buf) : offset);
95 if (fread (buf, 1, count, fp) < count)
98 /* EOF, or error before or while reading. */
101 /* Encountered EOF. */