1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #if defined(LIBC_SCCS) && !defined(lint) 12 static char sccsid[] = "@(#)fseek.c 5.5 (Berkeley) 02/01/91"; 13 #endif /* LIBC_SCCS and not lint */ 14 15 #include <sys/types.h> 16 #include <sys/file.h> 17 #include <sys/stat.h> 18 #include <stdio.h> 19 #include <errno.h> 20 #include "local.h" 21 22 #define POS_ERR (-(fpos_t)1) 23 24 /* 25 * Seek the given file to the given offset. 26 * `Whence' must be one of the three SEEK_* macros. 27 */ 28 fseek(fp, offset, whence) 29 register FILE *fp; 30 long offset; 31 int whence; 32 { 33 #if __STDC__ 34 register fpos_t (*seekfn)(char *, fpos_t, int); 35 #else 36 register fpos_t (*seekfn)(); 37 #endif 38 fpos_t target, curoff; 39 size_t n; 40 struct stat st; 41 int havepos; 42 43 /* make sure stdio is set up */ 44 if (!__sdidinit) 45 __sinit(); 46 47 /* 48 * Have to be able to seek. 49 */ 50 if ((seekfn = fp->_seek) == NULL) { 51 errno = ESPIPE; /* historic practice */ 52 return (EOF); 53 } 54 55 /* 56 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument. 57 * After this, whence is either SEEK_SET or SEEK_END. 58 */ 59 switch (whence) { 60 61 case SEEK_CUR: 62 /* 63 * In order to seek relative to the current stream offset, 64 * we have to first find the current stream offset a la 65 * ftell (see ftell for details). 66 */ 67 if (fp->_flags & __SOFF) 68 curoff = fp->_offset; 69 else { 70 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR); 71 if (curoff == -1L) 72 return (EOF); 73 } 74 if (fp->_flags & __SRD) { 75 curoff -= fp->_r; 76 if (HASUB(fp)) 77 curoff -= fp->_ur; 78 } else if (fp->_flags & __SWR && fp->_p != NULL) 79 curoff += fp->_p - fp->_bf._base; 80 81 offset += curoff; 82 whence = SEEK_SET; 83 havepos = 1; 84 break; 85 86 case SEEK_SET: 87 case SEEK_END: 88 havepos = 0; 89 break; 90 91 default: 92 errno = EINVAL; 93 return (EOF); 94 } 95 96 /* 97 * Can only optimise if: 98 * reading (and not reading-and-writing); 99 * not unbuffered; and 100 * this is a `regular' Unix file (and hence seekfn==__sseek). 101 * We must check __NBF first, because it is possible to have __NBF 102 * and __SOPT both set. 103 */ 104 if (fp->_bf._base == NULL) 105 __smakebuf(fp); 106 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT)) 107 goto dumb; 108 if ((fp->_flags & __SOPT) == 0) { 109 if (seekfn != __sseek || 110 fp->_file < 0 || fstat(fp->_file, &st) || 111 (st.st_mode & S_IFMT) != S_IFREG) { 112 fp->_flags |= __SNPT; 113 goto dumb; 114 } 115 fp->_blksize = st.st_blksize; 116 fp->_flags |= __SOPT; 117 } 118 119 /* 120 * We are reading; we can try to optimise. 121 * Figure out where we are going and where we are now. 122 */ 123 if (whence == SEEK_SET) 124 target = offset; 125 else { 126 if (fstat(fp->_file, &st)) 127 goto dumb; 128 target = st.st_size + offset; 129 } 130 131 if (!havepos) { 132 if (fp->_flags & __SOFF) 133 curoff = fp->_offset; 134 else { 135 curoff = (*seekfn)(fp->_cookie, 0L, SEEK_CUR); 136 if (curoff == POS_ERR) 137 goto dumb; 138 } 139 curoff -= fp->_r; 140 if (HASUB(fp)) 141 curoff -= fp->_ur; 142 } 143 144 /* 145 * Compute the number of bytes in the input buffer (pretending 146 * that any ungetc() input has been discarded). Adjust current 147 * offset backwards by this count so that it represents the 148 * file offset for the first byte in the current input buffer. 149 */ 150 if (HASUB(fp)) { 151 n = fp->_up - fp->_bf._base; 152 curoff -= n; 153 n += fp->_ur; 154 } else { 155 n = fp->_p - fp->_bf._base; 156 curoff -= n; 157 n += fp->_r; 158 } 159 160 /* 161 * If the target offset is within the current buffer, 162 * simply adjust the pointers, clear EOF, undo ungetc(), 163 * and return. (If the buffer was modified, we have to 164 * skip this; see fgetline.c.) 165 */ 166 if ((fp->_flags & __SMOD) == 0 && 167 target >= curoff && target < curoff + n) { 168 register int o = target - curoff; 169 170 fp->_p = fp->_bf._base + o; 171 fp->_r = n - o; 172 if (HASUB(fp)) 173 FREEUB(fp); 174 fp->_flags &= ~__SEOF; 175 return (0); 176 } 177 178 /* 179 * The place we want to get to is not within the current buffer, 180 * but we can still be kind to the kernel copyout mechanism. 181 * By aligning the file offset to a block boundary, we can let 182 * the kernel use the VM hardware to map pages instead of 183 * copying bytes laboriously. Using a block boundary also 184 * ensures that we only read one block, rather than two. 185 */ 186 curoff = target & ~(fp->_blksize - 1); 187 if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR) 188 goto dumb; 189 fp->_r = 0; 190 if (HASUB(fp)) 191 FREEUB(fp); 192 fp->_flags &= ~__SEOF; 193 n = target - curoff; 194 if (n) { 195 if (__srefill(fp) || fp->_r < n) 196 goto dumb; 197 fp->_p += n; 198 fp->_r -= n; 199 } 200 return (0); 201 202 /* 203 * We get here if we cannot optimise the seek ... just 204 * do it. Allow the seek function to change fp->_bf._base. 205 */ 206 dumb: 207 if (__sflush(fp) || 208 (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) { 209 return (EOF); 210 } 211 /* success: clear EOF indicator and discard ungetc() data */ 212 if (HASUB(fp)) 213 FREEUB(fp); 214 fp->_p = fp->_bf._base; 215 fp->_r = 0; 216 /* fp->_w = 0; */ /* unnecessary (I think...) */ 217 fp->_flags &= ~__SEOF; 218 return (0); 219 } 220