maint: update copyright
[gnulib.git] / lib / freadseek.c
1 /* Skipping input from a FILE stream.
2    Copyright (C) 2007-2014 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 void
34 freadptrinc (FILE *fp, size_t increment)
35 {
36   /* Keep this code in sync with freadptr!  */
37 #if HAVE___FREADPTRINC              /* musl libc */
38   __freadptrinc (fp, increment);
39 #elif defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
40   fp->_IO_read_ptr += increment;
41 #elif defined __sferror || defined __DragonFly__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin */
42   fp_->_p += increment;
43   fp_->_r -= increment;
44 #elif defined __EMX__               /* emx+gcc */
45   fp->_ptr += increment;
46   fp->_rcount -= increment;
47 #elif defined __minix               /* Minix */
48   fp_->_ptr += increment;
49   fp_->_count -= increment;
50 #elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
51   fp_->_ptr += increment;
52   fp_->_cnt -= increment;
53 #elif defined __UCLIBC__            /* uClibc */
54 # ifdef __STDIO_BUFFERS
55   fp->__bufpos += increment;
56 # else
57   abort ();
58 # endif
59 #elif defined __QNX__               /* QNX */
60   fp->_Next += increment;
61 #elif defined __MINT__              /* Atari FreeMiNT */
62   fp->__bufp += increment;
63 #elif defined EPLAN9                /* Plan9 */
64   fp->rp += increment;
65 #elif defined SLOW_BUT_NO_HACKS     /* users can define this */
66 #else
67  #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."
68 #endif
69 }
70
71 int
72 freadseek (FILE *fp, size_t offset)
73 {
74   size_t total_buffered;
75   int fd;
76
77   if (offset == 0)
78     return 0;
79
80   /* Seek over the already read and buffered input as quickly as possible,
81      without doing any system calls.  */
82   total_buffered = freadahead (fp);
83   /* This loop is usually executed at most twice: once for ungetc buffer (if
84      present) and once for the main buffer.  */
85   while (total_buffered > 0)
86     {
87       size_t buffered;
88
89       if (freadptr (fp, &buffered) != NULL && buffered > 0)
90         {
91           size_t increment = (buffered < offset ? buffered : offset);
92
93           freadptrinc (fp, increment);
94           offset -= increment;
95           if (offset == 0)
96             return 0;
97           total_buffered -= increment;
98           if (total_buffered == 0)
99             break;
100         }
101       /* Read one byte.  If we were reading from the ungetc buffer, this
102          switches the stream back to the main buffer.  */
103       if (fgetc (fp) == EOF)
104         goto eof;
105       offset--;
106       if (offset == 0)
107         return 0;
108       total_buffered--;
109     }
110
111   /* Test whether the stream is seekable or not.  */
112   fd = fileno (fp);
113   if (fd >= 0 && lseek (fd, 0, SEEK_CUR) >= 0)
114     {
115       /* FP refers to a regular file.  fseek is most efficient in this case.  */
116       return fseeko (fp, offset, SEEK_CUR);
117     }
118   else
119     {
120       /* FP is a non-seekable stream, possibly not even referring to a file
121          descriptor.  Read OFFSET bytes explicitly and discard them.  */
122       char buf[4096];
123
124       do
125         {
126           size_t count = (sizeof (buf) < offset ? sizeof (buf) : offset);
127           if (fread (buf, 1, count, fp) < count)
128             goto eof;
129           offset -= count;
130         }
131       while (offset > 0);
132
133       return 0;
134    }
135
136  eof:
137   /* EOF, or error before or while reading.  */
138   if (ferror (fp))
139     return EOF;
140   else
141     /* Encountered EOF.  */
142     return 0;
143 }