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