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