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