New private include file lib/stdio-impl.h.
[gnulib.git] / lib / freadseek.c
1 /* Skipping input from 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.  */
20 #include "freadseek.h"
21
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 #include "freadahead.h"
26 #include "freadptr.h"
27
28 #include "stdio-impl.h"
29
30 /* Increment the in-memory pointer.  INCREMENT must be at most the buffer size
31    returned by freadptr().
32    This is very cheap (no system calls).  */
33 static inline void
34 freadptrinc (FILE *fp, size_t increment)
35 {
36   /* Keep this code in sync with freadptr!  */
37 #if defined _IO_ferror_unlocked || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Linux libc5 */
38   fp->_IO_read_ptr += increment;
39 #elif defined __sferror             /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
40   fp->_p += increment;
41   fp->_r -= increment;
42 #elif defined __EMX__               /* emx+gcc */
43   fp->_ptr += increment;
44   fp->_rcount -= increment;
45 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw */
46   fp_->_ptr += increment;
47   fp_->_cnt -= increment;
48 #elif defined __UCLIBC__            /* uClibc */
49 # ifdef __STDIO_BUFFERS
50   fp->__bufpos += increment;
51 # else
52   abort ();
53 # endif
54 #elif defined __QNX__               /* QNX */
55   fp->_Next += increment;
56 #else
57  #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."
58 #endif
59 }
60
61 int
62 freadseek (FILE *fp, size_t offset)
63 {
64   size_t total_buffered;
65   int fd;
66
67   if (offset == 0)
68     return 0;
69
70   /* Seek over the already read and buffered input as quickly as possible,
71      without doing any system calls.  */
72   total_buffered = freadahead (fp);
73   /* This loop is usually executed at most twice: once for ungetc buffer (if
74      present) and once for the main buffer.  */
75   while (total_buffered > 0)
76     {
77       size_t buffered;
78
79       if (freadptr (fp, &buffered) != NULL && buffered > 0)
80         {
81           size_t increment = (buffered < offset ? buffered : offset);
82
83           freadptrinc (fp, increment);
84           offset -= increment;
85           if (offset == 0)
86             return 0;
87           total_buffered -= increment;
88           if (total_buffered == 0)
89             break;
90         }
91       /* Read one byte.  If we were reading from the ungetc buffer, this
92          switches the stream back to the main buffer.  */
93       if (fgetc (fp) == EOF)
94         goto eof;
95       offset--;
96       if (offset == 0)
97         return 0;
98       total_buffered--;
99     }
100
101   /* Test whether the stream is seekable or not.  */
102   fd = fileno (fp);
103   if (fd >= 0 && lseek (fd, 0, SEEK_CUR) >= 0)
104     {
105       /* FP refers to a regular file.  fseek is most efficient in this case.  */
106       return fseek (fp, offset, SEEK_CUR);
107     }
108   else
109     {
110       /* FP is a non-seekable stream, possibly not even referring to a file
111          descriptor.  Read OFFSET bytes explicitly and discard them.  */
112       char buf[4096];
113
114       do
115         {
116           size_t count = (sizeof (buf) < offset ? sizeof (buf) : offset);
117           if (fread (buf, 1, count, fp) < count)
118             goto eof;
119           offset -= count;
120         }
121       while (offset > 0);
122
123       return 0;
124    }
125
126  eof:
127   /* EOF, or error before or while reading.  */
128   if (ferror (fp))
129     return EOF;
130   else
131     /* Encountered EOF.  */
132     return 0;
133 }