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