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