xref: /freebsd/lib/libc/stdio/fseek.c (revision 81ad6265)
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 <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "namespace.h"
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include "un-namespace.h"
50 #include "local.h"
51 #include "libc_private.h"
52 
53 #define	POS_ERR	(-(fpos_t)1)
54 
55 int
56 fseek(FILE *fp, long offset, int whence)
57 {
58 	int ret;
59 	int serrno = errno;
60 
61 	/* make sure stdio is set up */
62 	if (!__sdidinit)
63 		__sinit();
64 
65 	FLOCKFILE_CANCELSAFE(fp);
66 	ret = _fseeko(fp, (off_t)offset, whence, 1);
67 	FUNLOCKFILE_CANCELSAFE();
68 	if (ret == 0)
69 		errno = serrno;
70 	return (ret);
71 }
72 
73 int
74 fseeko(FILE *fp, off_t offset, int whence)
75 {
76 	int ret;
77 	int serrno = errno;
78 
79 	/* make sure stdio is set up */
80 	if (!__sdidinit)
81 		__sinit();
82 
83 	FLOCKFILE_CANCELSAFE(fp);
84 	ret = _fseeko(fp, offset, whence, 0);
85 	FUNLOCKFILE_CANCELSAFE();
86 	if (ret == 0)
87 		errno = serrno;
88 	return (ret);
89 }
90 
91 /*
92  * Seek the given file to the given offset.
93  * `Whence' must be one of the three SEEK_* macros.
94  */
95 int
96 _fseeko(FILE *fp, off_t offset, int whence, int ltest)
97 {
98 	fpos_t (*seekfn)(void *, fpos_t, int);
99 	fpos_t target, curoff, ret;
100 	size_t n;
101 	struct stat st;
102 	int havepos;
103 
104 	/*
105 	 * Have to be able to seek.
106 	 */
107 	if ((seekfn = fp->_seek) == NULL) {
108 		errno = ESPIPE;		/* historic practice */
109 		return (-1);
110 	}
111 
112 	/*
113 	 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
114 	 * After this, whence is either SEEK_SET or SEEK_END.
115 	 */
116 	switch (whence) {
117 
118 	case SEEK_CUR:
119 		/*
120 		 * In order to seek relative to the current stream offset,
121 		 * we have to first find the current stream offset via
122 		 * ftell (see ftell for details).
123 		 */
124 		if (_ftello(fp, &curoff))
125 			return (-1);
126 		if (curoff < 0) {
127 			/* Unspecified position because of ungetc() at 0 */
128 			errno = ESPIPE;
129 			return (-1);
130 		}
131 		if (offset > 0 && curoff > OFF_MAX - offset) {
132 			errno = EOVERFLOW;
133 			return (-1);
134 		}
135 		offset += curoff;
136 		if (offset < 0) {
137 			errno = EINVAL;
138 			return (-1);
139 		}
140 		if (ltest && offset > LONG_MAX) {
141 			errno = EOVERFLOW;
142 			return (-1);
143 		}
144 		whence = SEEK_SET;
145 		havepos = 1;
146 		break;
147 
148 	case SEEK_SET:
149 		if (offset < 0) {
150 			errno = EINVAL;
151 			return (-1);
152 		}
153 	case SEEK_END:
154 		curoff = 0;		/* XXX just to keep gcc quiet */
155 		havepos = 0;
156 		break;
157 
158 	default:
159 		errno = EINVAL;
160 		return (-1);
161 	}
162 
163 	/*
164 	 * Can only optimise if:
165 	 *	reading (and not reading-and-writing);
166 	 *	not unbuffered; and
167 	 *	this is a `regular' Unix file (and hence seekfn==__sseek).
168 	 * We must check __NBF first, because it is possible to have __NBF
169 	 * and __SOPT both set.
170 	 */
171 	if (fp->_bf._base == NULL)
172 		__smakebuf(fp);
173 	if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
174 		goto dumb;
175 	if ((fp->_flags & __SOPT) == 0) {
176 		if (seekfn != __sseek ||
177 		    fp->_file < 0 || _fstat(fp->_file, &st) ||
178 		    (st.st_mode & S_IFMT) != S_IFREG) {
179 			fp->_flags |= __SNPT;
180 			goto dumb;
181 		}
182 		fp->_blksize = st.st_blksize;
183 		fp->_flags |= __SOPT;
184 	}
185 
186 	/*
187 	 * We are reading; we can try to optimise.
188 	 * Figure out where we are going and where we are now.
189 	 */
190 	if (whence == SEEK_SET)
191 		target = offset;
192 	else {
193 		if (_fstat(fp->_file, &st))
194 			goto dumb;
195 		if (offset > 0 && st.st_size > OFF_MAX - offset) {
196 			errno = EOVERFLOW;
197 			return (-1);
198 		}
199 		target = st.st_size + offset;
200 		if ((off_t)target < 0) {
201 			errno = EINVAL;
202 			return (-1);
203 		}
204 		if (ltest && (off_t)target > LONG_MAX) {
205 			errno = EOVERFLOW;
206 			return (-1);
207 		}
208 	}
209 
210 	if (!havepos && _ftello(fp, &curoff))
211 		goto dumb;
212 
213 	/*
214 	 * (If the buffer was modified, we have to
215 	 * skip this; see fgetln.c.)
216 	 */
217 	if (fp->_flags & __SMOD)
218 		goto abspos;
219 
220 	/*
221 	 * Compute the number of bytes in the input buffer (pretending
222 	 * that any ungetc() input has been discarded).  Adjust current
223 	 * offset backwards by this count so that it represents the
224 	 * file offset for the first byte in the current input buffer.
225 	 */
226 	if (HASUB(fp)) {
227 		curoff += fp->_r;	/* kill off ungetc */
228 		n = fp->_up - fp->_bf._base;
229 		curoff -= n;
230 		n += fp->_ur;
231 	} else {
232 		n = fp->_p - fp->_bf._base;
233 		curoff -= n;
234 		n += fp->_r;
235 	}
236 
237 	/*
238 	 * If the target offset is within the current buffer,
239 	 * simply adjust the pointers, clear EOF, undo ungetc(),
240 	 * and return.
241 	 */
242 	if (target >= curoff && target < curoff + n) {
243 		size_t o = target - curoff;
244 
245 		fp->_p = fp->_bf._base + o;
246 		fp->_r = n - o;
247 		if (HASUB(fp))
248 			FREEUB(fp);
249 		fp->_flags &= ~__SEOF;
250 		memset(&fp->_mbstate, 0, sizeof(mbstate_t));
251 		return (0);
252 	}
253 
254 abspos:
255 	/*
256 	 * The place we want to get to is not within the current buffer,
257 	 * but we can still be kind to the kernel copyout mechanism.
258 	 * By aligning the file offset to a block boundary, we can let
259 	 * the kernel use the VM hardware to map pages instead of
260 	 * copying bytes laboriously.  Using a block boundary also
261 	 * ensures that we only read one block, rather than two.
262 	 */
263 	curoff = target & ~(fp->_blksize - 1);
264 	if (_sseek(fp, curoff, SEEK_SET) == POS_ERR)
265 		goto dumb;
266 	fp->_r = 0;
267 	fp->_p = fp->_bf._base;
268 	if (HASUB(fp))
269 		FREEUB(fp);
270 	n = target - curoff;
271 	if (n) {
272 		if (__srefill(fp) || fp->_r < n)
273 			goto dumb;
274 		fp->_p += n;
275 		fp->_r -= n;
276 	}
277 	fp->_flags &= ~__SEOF;
278 	memset(&fp->_mbstate, 0, sizeof(mbstate_t));
279 	return (0);
280 
281 	/*
282 	 * We get here if we cannot optimise the seek ... just
283 	 * do it.  Allow the seek function to change fp->_bf._base.
284 	 */
285 dumb:
286 	if (__sflush(fp) ||
287 	    (ret = _sseek(fp, (fpos_t)offset, whence)) == POS_ERR)
288 		return (-1);
289 	if (ltest && ret > LONG_MAX) {
290 		fp->_flags |= __SERR;
291 		errno = EOVERFLOW;
292 		return (-1);
293 	}
294 	/* success: clear EOF indicator and discard ungetc() data */
295 	if (HASUB(fp))
296 		FREEUB(fp);
297 	fp->_p = fp->_bf._base;
298 	fp->_r = 0;
299 	/* fp->_w = 0; */	/* unnecessary (I think...) */
300 	fp->_flags &= ~__SEOF;
301 	memset(&fp->_mbstate, 0, sizeof(mbstate_t));
302 	return (0);
303 }
304