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