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