summaryrefslogtreecommitdiff
path: root/lib/freadseek.c
blob: c613c6b4313c8a8465245fc44040caf3473c1e81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Skipping input from a FILE stream.
   Copyright (C) 2007-2016 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include "freadseek.h"

#include <stdlib.h>
#include <unistd.h>

#include "freadahead.h"
#include "freadptr.h"

#include "stdio-impl.h"

/* Increment the in-memory pointer.  INCREMENT must be at most the buffer size
   returned by freadptr().
   This is very cheap (no system calls).  */
static void
freadptrinc (FILE *fp, size_t increment)
{
  /* Keep this code in sync with freadptr!  */
#if HAVE___FREADPTRINC              /* musl libc */
  __freadptrinc (fp, increment);
#elif defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */
  fp->_IO_read_ptr += increment;
#elif defined __sferror || defined __DragonFly__ || defined __ANDROID__
  /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */
  fp_->_p += increment;
  fp_->_r -= increment;
#elif defined __EMX__               /* emx+gcc */
  fp->_ptr += increment;
  fp->_rcount -= increment;
#elif defined __minix               /* Minix */
  fp_->_ptr += increment;
  fp_->_count -= increment;
#elif defined _IOERR                /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */
  fp_->_ptr += increment;
  fp_->_cnt -= increment;
#elif defined __UCLIBC__            /* uClibc */
# ifdef __STDIO_BUFFERS
  fp->__bufpos += increment;
# else
  abort ();
# endif
#elif defined __QNX__               /* QNX */
  fp->_Next += increment;
#elif defined __MINT__              /* Atari FreeMiNT */
  fp->__bufp += increment;
#elif defined EPLAN9                /* Plan9 */
  fp->rp += increment;
#elif defined SLOW_BUT_NO_HACKS     /* users can define this */
#else
 #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."
#endif
}

int
freadseek (FILE *fp, size_t offset)
{
  size_t total_buffered;
  int fd;

  if (offset == 0)
    return 0;

  /* Seek over the already read and buffered input as quickly as possible,
     without doing any system calls.  */
  total_buffered = freadahead (fp);
  /* This loop is usually executed at most twice: once for ungetc buffer (if
     present) and once for the main buffer.  */
  while (total_buffered > 0)
    {
      size_t buffered;

      if (freadptr (fp, &buffered) != NULL && buffered > 0)
        {
          size_t increment = (buffered < offset ? buffered : offset);

          freadptrinc (fp, increment);
          offset -= increment;
          if (offset == 0)
            return 0;
          total_buffered -= increment;
          if (total_buffered == 0)
            break;
        }
      /* Read one byte.  If we were reading from the ungetc buffer, this
         switches the stream back to the main buffer.  */
      if (fgetc (fp) == EOF)
        goto eof;
      offset--;
      if (offset == 0)
        return 0;
      total_buffered--;
    }

  /* Test whether the stream is seekable or not.  */
  fd = fileno (fp);
  if (fd >= 0 && lseek (fd, 0, SEEK_CUR) >= 0)
    {
      /* FP refers to a regular file.  fseek is most efficient in this case.  */
      return fseeko (fp, offset, SEEK_CUR);
    }
  else
    {
      /* FP is a non-seekable stream, possibly not even referring to a file
         descriptor.  Read OFFSET bytes explicitly and discard them.  */
      char buf[4096];

      do
        {
          size_t count = (sizeof (buf) < offset ? sizeof (buf) : offset);
          if (fread (buf, 1, count, fp) < count)
            goto eof;
          offset -= count;
        }
      while (offset > 0);

      return 0;
   }

 eof:
  /* EOF, or error before or while reading.  */
  if (ferror (fp))
    return EOF;
  else
    /* Encountered EOF.  */
    return 0;
}