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