1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include "namespace.h"
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include "un-namespace.h"
47 #include "local.h"
48 #include "libc_private.h"
49
50 #define POS_ERR (-(fpos_t)1)
51
52 int
fseek(FILE * fp,long offset,int whence)53 fseek(FILE *fp, long offset, int whence)
54 {
55 int ret;
56 int serrno = errno;
57
58 /* make sure stdio is set up */
59 if (!__sdidinit)
60 __sinit();
61
62 FLOCKFILE_CANCELSAFE(fp);
63 ret = _fseeko(fp, (off_t)offset, whence, 1);
64 FUNLOCKFILE_CANCELSAFE();
65 if (ret == 0)
66 errno = serrno;
67 return (ret);
68 }
69
70 int
fseeko(FILE * fp,off_t offset,int whence)71 fseeko(FILE *fp, off_t offset, int whence)
72 {
73 int ret;
74 int serrno = errno;
75
76 /* make sure stdio is set up */
77 if (!__sdidinit)
78 __sinit();
79
80 FLOCKFILE_CANCELSAFE(fp);
81 ret = _fseeko(fp, offset, whence, 0);
82 FUNLOCKFILE_CANCELSAFE();
83 if (ret == 0)
84 errno = serrno;
85 return (ret);
86 }
87
88 /*
89 * Seek the given file to the given offset.
90 * `Whence' must be one of the three SEEK_* macros.
91 */
92 int
_fseeko(FILE * fp,off_t offset,int whence,int ltest)93 _fseeko(FILE *fp, off_t offset, int whence, int ltest)
94 {
95 fpos_t (*seekfn)(void *, fpos_t, int);
96 fpos_t target, curoff, ret;
97 size_t n;
98 struct stat st;
99 int havepos;
100
101 /*
102 * Have to be able to seek.
103 */
104 if ((seekfn = fp->_seek) == NULL) {
105 errno = ESPIPE; /* historic practice */
106 return (-1);
107 }
108
109 /*
110 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
111 * After this, whence is either SEEK_SET or SEEK_END.
112 */
113 switch (whence) {
114
115 case SEEK_CUR:
116 /*
117 * In order to seek relative to the current stream offset,
118 * we have to first find the current stream offset via
119 * ftell (see ftell for details).
120 */
121 if (_ftello(fp, &curoff))
122 return (-1);
123 if (curoff < 0) {
124 /* Unspecified position because of ungetc() at 0 */
125 errno = ESPIPE;
126 return (-1);
127 }
128 if (offset > 0 && curoff > OFF_MAX - offset) {
129 errno = EOVERFLOW;
130 return (-1);
131 }
132 offset += curoff;
133 if (offset < 0) {
134 errno = EINVAL;
135 return (-1);
136 }
137 if (ltest && offset > LONG_MAX) {
138 errno = EOVERFLOW;
139 return (-1);
140 }
141 whence = SEEK_SET;
142 havepos = 1;
143 break;
144
145 case SEEK_SET:
146 if (offset < 0) {
147 errno = EINVAL;
148 return (-1);
149 }
150 case SEEK_END:
151 curoff = 0; /* XXX just to keep gcc quiet */
152 havepos = 0;
153 break;
154
155 default:
156 errno = EINVAL;
157 return (-1);
158 }
159
160 /*
161 * Can only optimise if:
162 * reading (and not reading-and-writing);
163 * not unbuffered; and
164 * this is a `regular' Unix file (and hence seekfn==__sseek).
165 * We must check __NBF first, because it is possible to have __NBF
166 * and __SOPT both set.
167 */
168 if (fp->_bf._base == NULL)
169 __smakebuf(fp);
170 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
171 goto dumb;
172 if ((fp->_flags & __SOPT) == 0) {
173 if (seekfn != __sseek ||
174 fp->_file < 0 || _fstat(fp->_file, &st) ||
175 (st.st_mode & S_IFMT) != S_IFREG) {
176 fp->_flags |= __SNPT;
177 goto dumb;
178 }
179 fp->_blksize = st.st_blksize;
180 fp->_flags |= __SOPT;
181 }
182
183 /*
184 * We are reading; we can try to optimise.
185 * Figure out where we are going and where we are now.
186 */
187 if (whence == SEEK_SET)
188 target = offset;
189 else {
190 if (_fstat(fp->_file, &st))
191 goto dumb;
192 if (offset > 0 && st.st_size > OFF_MAX - offset) {
193 errno = EOVERFLOW;
194 return (-1);
195 }
196 target = st.st_size + offset;
197 if ((off_t)target < 0) {
198 errno = EINVAL;
199 return (-1);
200 }
201 if (ltest && (off_t)target > LONG_MAX) {
202 errno = EOVERFLOW;
203 return (-1);
204 }
205 }
206
207 if (!havepos && _ftello(fp, &curoff))
208 goto dumb;
209
210 /*
211 * (If the buffer was modified, we have to
212 * skip this; see fgetln.c.)
213 */
214 if (fp->_flags & __SMOD)
215 goto abspos;
216
217 /*
218 * Compute the number of bytes in the input buffer (pretending
219 * that any ungetc() input has been discarded). Adjust current
220 * offset backwards by this count so that it represents the
221 * file offset for the first byte in the current input buffer.
222 */
223 if (HASUB(fp)) {
224 curoff += fp->_r; /* kill off ungetc */
225 n = fp->_up - fp->_bf._base;
226 curoff -= n;
227 n += fp->_ur;
228 } else {
229 n = fp->_p - fp->_bf._base;
230 curoff -= n;
231 n += fp->_r;
232 }
233
234 /*
235 * If the target offset is within the current buffer,
236 * simply adjust the pointers, clear EOF, undo ungetc(),
237 * and return.
238 */
239 if (target >= curoff && target < curoff + n) {
240 size_t o = target - curoff;
241
242 fp->_p = fp->_bf._base + o;
243 fp->_r = n - o;
244 if (HASUB(fp))
245 FREEUB(fp);
246 fp->_flags &= ~__SEOF;
247 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
248 return (0);
249 }
250
251 abspos:
252 /*
253 * The place we want to get to is not within the current buffer,
254 * but we can still be kind to the kernel copyout mechanism.
255 * By aligning the file offset to a block boundary, we can let
256 * the kernel use the VM hardware to map pages instead of
257 * copying bytes laboriously. Using a block boundary also
258 * ensures that we only read one block, rather than two.
259 */
260 curoff = target & ~(fp->_blksize - 1);
261 if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)
262 goto dumb;
263 fp->_r = 0;
264 fp->_p = fp->_bf._base;
265 if (HASUB(fp))
266 FREEUB(fp);
267 n = target - curoff;
268 if (n) {
269 if (__srefill(fp) || fp->_r < n)
270 goto dumb;
271 fp->_p += n;
272 fp->_r -= n;
273 }
274 fp->_flags &= ~__SEOF;
275 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
276 return (0);
277
278 /*
279 * We get here if we cannot optimise the seek ... just
280 * do it. Allow the seek function to change fp->_bf._base.
281 */
282 dumb:
283 if (__sflush(fp) ||
284 (ret = _sseek(fp, (fpos_t)offset, whence)) == POS_ERR)
285 return (-1);
286 if (ltest && ret > LONG_MAX) {
287 fp->_flags |= __SERR;
288 errno = EOVERFLOW;
289 return (-1);
290 }
291 /* success: clear EOF indicator and discard ungetc() data */
292 if (HASUB(fp))
293 FREEUB(fp);
294 fp->_p = fp->_bf._base;
295 fp->_r = 0;
296 /* fp->_w = 0; */ /* unnecessary (I think...) */
297 fp->_flags &= ~__SEOF;
298 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
299 return (0);
300 }
301