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