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