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