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