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