1 /* $NetBSD: fseek.c,v 1.8 1995/03/05 06:56:09 jtc Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #if defined(LIBC_SCCS) && !defined(lint) 40 #if 0 41 static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 1/2/94"; 42 #endif 43 static char rcsid[] = "$NetBSD: fseek.c,v 1.8 1995/03/05 06:56:09 jtc Exp $"; 44 #endif /* LIBC_SCCS and not lint */ 45 46 #include <sys/types.h> 47 #include <sys/stat.h> 48 #include <fcntl.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <errno.h> 52 #include "local.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 /* 77 * Have to be able to seek. 78 */ 79 if ((seekfn = fp->_seek) == NULL) { 80 errno = ESPIPE; /* historic practice */ 81 return (EOF); 82 } 83 84 /* 85 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. 86 * After this, whence is either SEEK_SET or SEEK_END. 87 */ 88 switch (whence) { 89 90 case SEEK_CUR: 91 /* 92 * In order to seek relative to the current stream offset, 93 * we have to first find the current stream offset a la 94 * ftell (see ftell for details). 95 */ 96 __sflush(fp); /* may adjust seek offset on append stream */ 97 if (fp->_flags & __SOFF) 98 curoff = fp->_offset; 99 else { 100 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 101 if (curoff == -1L) 102 return (EOF); 103 } 104 if (fp->_flags & __SRD) { 105 curoff -= fp->_r; 106 if (HASUB(fp)) 107 curoff -= fp->_ur; 108 } else if (fp->_flags & __SWR && fp->_p != NULL) 109 curoff += fp->_p - fp->_bf._base; 110 111 offset += curoff; 112 whence = SEEK_SET; 113 havepos = 1; 114 break; 115 116 case SEEK_SET: 117 case SEEK_END: 118 curoff = 0; /* XXX just to keep gcc quiet */ 119 havepos = 0; 120 break; 121 122 default: 123 errno = EINVAL; 124 return (EOF); 125 } 126 127 /* 128 * Can only optimise if: 129 * reading (and not reading-and-writing); 130 * not unbuffered; and 131 * this is a `regular' Unix file (and hence seekfn==__sseek). 132 * We must check __NBF first, because it is possible to have __NBF 133 * and __SOPT both set. 134 */ 135 if (fp->_bf._base == NULL) 136 __smakebuf(fp); 137 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 138 goto dumb; 139 if ((fp->_flags & __SOPT) == 0) { 140 if (seekfn != __sseek || 141 fp->_file < 0 || fstat(fp->_file, &st) || 142 (st.st_mode & S_IFMT) != S_IFREG) { 143 fp->_flags |= __SNPT; 144 goto dumb; 145 } 146 fp->_blksize = st.st_blksize; 147 fp->_flags |= __SOPT; 148 } 149 150 /* 151 * We are reading; we can try to optimise. 152 * Figure out where we are going and where we are now. 153 */ 154 if (whence == SEEK_SET) 155 target = offset; 156 else { 157 if (fstat(fp->_file, &st)) 158 goto dumb; 159 target = st.st_size + offset; 160 } 161 162 if (!havepos) { 163 if (fp->_flags & __SOFF) 164 curoff = fp->_offset; 165 else { 166 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 167 if (curoff == POS_ERR) 168 goto dumb; 169 } 170 curoff -= fp->_r; 171 if (HASUB(fp)) 172 curoff -= fp->_ur; 173 } 174 175 /* 176 * Compute the number of bytes in the input buffer (pretending 177 * that any ungetc() input has been discarded). Adjust current 178 * offset backwards by this count so that it represents the 179 * file offset for the first byte in the current input buffer. 180 */ 181 if (HASUB(fp)) { 182 curoff += fp->_r; /* kill off ungetc */ 183 n = fp->_up - fp->_bf._base; 184 curoff -= n; 185 n += fp->_ur; 186 } else { 187 n = fp->_p - fp->_bf._base; 188 curoff -= n; 189 n += fp->_r; 190 } 191 192 /* 193 * If the target offset is within the current buffer, 194 * simply adjust the pointers, clear EOF, undo ungetc(), 195 * and return. (If the buffer was modified, we have to 196 * skip this; see fgetln.c.) 197 */ 198 if ((fp->_flags & __SMOD) == 0 && 199 target >= curoff && target < curoff + n) { 200 register int o = target - curoff; 201 202 fp->_p = fp->_bf._base + o; 203 fp->_r = n - o; 204 if (HASUB(fp)) 205 FREEUB(fp); 206 fp->_flags &= ~__SEOF; 207 return (0); 208 } 209 210 /* 211 * The place we want to get to is not within the current buffer, 212 * but we can still be kind to the kernel copyout mechanism. 213 * By aligning the file offset to a block boundary, we can let 214 * the kernel use the VM hardware to map pages instead of 215 * copying bytes laboriously. Using a block boundary also 216 * ensures that we only read one block, rather than two. 217 */ 218 curoff = target & ~(fp->_blksize - 1); 219 if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) 220 goto dumb; 221 fp->_r = 0; 222 fp->_p = fp->_bf._base; 223 if (HASUB(fp)) 224 FREEUB(fp); 225 fp->_flags &= ~__SEOF; 226 n = target - curoff; 227 if (n) { 228 if (__srefill(fp) || fp->_r < n) 229 goto dumb; 230 fp->_p += n; 231 fp->_r -= n; 232 } 233 return (0); 234 235 /* 236 * We get here if we cannot optimise the seek ... just 237 * do it. Allow the seek function to change fp->_bf._base. 238 */ 239 dumb: 240 if (__sflush(fp) || 241 (*seekfn)(fp->_cookie, (fpos_t)offset, whence) == POS_ERR) { 242 return (EOF); 243 } 244 /* success: clear EOF indicator and discard ungetc() data */ 245 if (HASUB(fp)) 246 FREEUB(fp); 247 fp->_p = fp->_bf._base; 248 fp->_r = 0; 249 /* fp->_w = 0; */ /* unnecessary (I think...) */ 250 fp->_flags &= ~__SEOF; 251 return (0); 252 } 253