Remove building with NOCRYPTO option
[minix.git] / lib / libc / stdio / fseeko.c
bloba136e757d3580d0586c98d251f785f45b1bb1e35
1 /* $NetBSD: fseeko.c,v 1.13 2014/10/19 11:17:43 justin Exp $ */
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
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.
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.
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 __RCSID("$NetBSD: fseeko.c,v 1.13 2014/10/19 11:17:43 justin Exp $");
38 #endif /* LIBC_SCCS and not lint */
40 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/stat.h>
44 #include <assert.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include "reentrant.h"
50 #include "local.h"
52 #ifdef __weak_alias
53 __weak_alias(fseeko, _fseeko)
54 #endif
56 #define POS_ERR ((off_t)-1)
59 * Seek the given file to the given offset.
60 * `Whence' must be one of the three SEEK_* macros.
62 int
63 fseeko(FILE *fp, off_t offset, int whence)
65 off_t (*seekfn)(void *, off_t, int);
66 off_t target, curoff;
67 size_t n;
68 struct stat st;
69 int havepos;
71 _DIAGASSERT(fp != NULL);
73 /* make sure stdio is set up */
74 if (!__sdidinit)
75 __sinit();
77 FLOCKFILE(fp);
80 * Have to be able to seek.
82 if ((seekfn = fp->_seek) == NULL) {
83 errno = ESPIPE; /* historic practice */
84 FUNLOCKFILE(fp);
85 return -1;
89 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
90 * After this, whence is either SEEK_SET or SEEK_END.
92 switch (whence) {
94 case SEEK_CUR:
96 * In order to seek relative to the current stream offset,
97 * we have to first find the current stream offset a la
98 * ftell (see ftell for details).
100 (void)__sflush(fp); /* may adjust seek offset on append stream */
101 if (fp->_flags & __SOFF)
102 curoff = fp->_offset;
103 else {
104 curoff = (*seekfn)(fp->_cookie, (off_t)0, SEEK_CUR);
105 if (curoff == POS_ERR) {
106 FUNLOCKFILE(fp);
107 return -1;
110 if (fp->_flags & __SRD) {
111 curoff -= fp->_r;
112 if (HASUB(fp))
113 curoff -= fp->_ur;
114 } else if (fp->_flags & __SWR && fp->_p != NULL)
115 curoff += fp->_p - fp->_bf._base;
117 offset += curoff;
118 if (offset < 0) {
119 errno = EINVAL;
120 FUNLOCKFILE(fp);
121 return -1;
123 whence = SEEK_SET;
124 havepos = 1;
125 break;
127 case SEEK_SET:
128 if (offset < 0) {
129 errno = EINVAL;
130 FUNLOCKFILE(fp);
131 return -1;
133 case SEEK_END:
134 curoff = 0; /* XXX just to keep gcc quiet */
135 havepos = 0;
136 break;
138 default:
139 errno = EINVAL;
140 FUNLOCKFILE(fp);
141 return -1;
145 * Can only optimise if:
146 * reading (and not reading-and-writing);
147 * not unbuffered; and
148 * this is a `regular' Unix file (and hence seekfn==__sseek).
149 * We must check __NBF first, because it is possible to have __NBF
150 * and __SOPT both set.
152 if (fp->_bf._base == NULL)
153 __smakebuf(fp);
154 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
155 goto dumb;
156 if ((fp->_flags & __SOPT) == 0) {
157 if (seekfn != __sseek ||
158 __sfileno(fp) == -1 || fstat(__sfileno(fp), &st) ||
159 !S_ISREG(st.st_mode)) {
160 fp->_flags |= __SNPT;
161 goto dumb;
163 fp->_blksize = st.st_blksize;
164 fp->_flags |= __SOPT;
168 * We are reading; we can try to optimise.
169 * Figure out where we are going and where we are now.
171 if (whence == SEEK_SET)
172 target = offset;
173 else {
174 if (fstat(__sfileno(fp), &st))
175 goto dumb;
176 target = st.st_size + offset;
179 if (!havepos) {
180 if (fp->_flags & __SOFF)
181 curoff = fp->_offset;
182 else {
183 curoff = (*seekfn)(fp->_cookie, (off_t)0, SEEK_CUR);
184 if (curoff == POS_ERR)
185 goto dumb;
187 curoff -= fp->_r;
188 if (HASUB(fp))
189 curoff -= fp->_ur;
193 * Compute the number of bytes in the input buffer (pretending
194 * that any ungetc() input has been discarded). Adjust current
195 * offset backwards by this count so that it represents the
196 * file offset for the first byte in the current input buffer.
198 if (HASUB(fp)) {
199 curoff += fp->_r; /* kill off ungetc */
200 n = fp->_up - fp->_bf._base;
201 curoff -= n;
202 n += fp->_ur;
203 } else {
204 n = fp->_p - fp->_bf._base;
205 curoff -= n;
206 n += fp->_r;
210 * If the target offset is within the current buffer,
211 * simply adjust the pointers, clear EOF, undo ungetc(),
212 * and return. (If the buffer was modified, we have to
213 * skip this; see fgetln.c.)
215 if ((fp->_flags & __SMOD) == 0 &&
216 target >= curoff && target < curoff + (off_t)n) {
217 int o = (int)(target - curoff);
219 fp->_p = fp->_bf._base + o;
220 _DIAGASSERT(__type_fit(int, n - o));
221 fp->_r = (int)(n - o);
222 if (HASUB(fp))
223 FREEUB(fp);
224 fp->_flags &= ~__SEOF;
225 FUNLOCKFILE(fp);
226 return 0;
230 * The place we want to get to is not within the current buffer,
231 * but we can still be kind to the kernel copyout mechanism.
232 * By aligning the file offset to a block boundary, we can let
233 * the kernel use the VM hardware to map pages instead of
234 * copying bytes laboriously. Using a block boundary also
235 * ensures that we only read one block, rather than two.
237 curoff = target & ~(fp->_blksize - 1);
238 if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
239 goto dumb;
240 fp->_r = 0;
241 fp->_p = fp->_bf._base;
242 if (HASUB(fp))
243 FREEUB(fp);
244 fp->_flags &= ~__SEOF;
245 n = (int)(target - curoff);
246 if (n) {
247 if (__srefill(fp) || (size_t)fp->_r < n)
248 goto dumb;
249 fp->_p += n;
250 _DIAGASSERT(__type_fit(int, fp->_r - n));
251 fp->_r -= (int)n;
253 FUNLOCKFILE(fp);
254 return 0;
257 * We get here if we cannot optimise the seek ... just
258 * do it. Allow the seek function to change fp->_bf._base.
260 dumb:
261 if (__sflush(fp) ||
262 (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) {
263 FUNLOCKFILE(fp);
264 return -1;
266 /* success: clear EOF indicator and discard ungetc() data */
267 if (HASUB(fp))
268 FREEUB(fp);
269 fp->_p = fp->_bf._base;
270 fp->_r = 0;
271 /* fp->_w = 0; */ /* unnecessary (I think...) */
272 fp->_flags &= ~__SEOF;
273 FUNLOCKFILE(fp);
274 return 0;