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 * %sccs.include.redist.c%
9 */
10
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)fseek.c 8.3 (Berkeley) 01/02/94";
13 #endif /* LIBC_SCCS and not lint */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include "local.h"
22
23 #define POS_ERR (-(fpos_t)1)
24
25 /*
26 * Seek the given file to the given offset.
27 * `Whence' must be one of the three SEEK_* macros.
28 */
29 int
fseek(fp,offset,whence)30 fseek(fp, offset, whence)
31 register FILE *fp;
32 long offset;
33 int whence;
34 {
35 register fpos_t (*seekfn) __P((void *, fpos_t, int));
36 fpos_t target, curoff;
37 size_t n;
38 struct stat st;
39 int havepos;
40
41 /* make sure stdio is set up */
42 if (!__sdidinit)
43 __sinit();
44
45 /*
46 * Have to be able to seek.
47 */
48 if ((seekfn = fp->_seek) == NULL) {
49 errno = ESPIPE; /* historic practice */
50 return (EOF);
51 }
52
53 /*
54 * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
55 * After this, whence is either SEEK_SET or SEEK_END.
56 */
57 switch (whence) {
58
59 case SEEK_CUR:
60 /*
61 * In order to seek relative to the current stream offset,
62 * we have to first find the current stream offset a la
63 * ftell (see ftell for details).
64 */
65 if (fp->_flags & __SOFF)
66 curoff = fp->_offset;
67 else {
68 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
69 if (curoff == -1L)
70 return (EOF);
71 }
72 if (fp->_flags & __SRD) {
73 curoff -= fp->_r;
74 if (HASUB(fp))
75 curoff -= fp->_ur;
76 } else if (fp->_flags & __SWR && fp->_p != NULL)
77 curoff += fp->_p - fp->_bf._base;
78
79 offset += curoff;
80 whence = SEEK_SET;
81 havepos = 1;
82 break;
83
84 case SEEK_SET:
85 case SEEK_END:
86 curoff = 0; /* XXX just to keep gcc quiet */
87 havepos = 0;
88 break;
89
90 default:
91 errno = EINVAL;
92 return (EOF);
93 }
94
95 /*
96 * Can only optimise if:
97 * reading (and not reading-and-writing);
98 * not unbuffered; and
99 * this is a `regular' Unix file (and hence seekfn==__sseek).
100 * We must check __NBF first, because it is possible to have __NBF
101 * and __SOPT both set.
102 */
103 if (fp->_bf._base == NULL)
104 __smakebuf(fp);
105 if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
106 goto dumb;
107 if ((fp->_flags & __SOPT) == 0) {
108 if (seekfn != __sseek ||
109 fp->_file < 0 || fstat(fp->_file, &st) ||
110 (st.st_mode & S_IFMT) != S_IFREG) {
111 fp->_flags |= __SNPT;
112 goto dumb;
113 }
114 fp->_blksize = st.st_blksize;
115 fp->_flags |= __SOPT;
116 }
117
118 /*
119 * We are reading; we can try to optimise.
120 * Figure out where we are going and where we are now.
121 */
122 if (whence == SEEK_SET)
123 target = offset;
124 else {
125 if (fstat(fp->_file, &st))
126 goto dumb;
127 target = st.st_size + offset;
128 }
129
130 if (!havepos) {
131 if (fp->_flags & __SOFF)
132 curoff = fp->_offset;
133 else {
134 curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
135 if (curoff == POS_ERR)
136 goto dumb;
137 }
138 curoff -= fp->_r;
139 if (HASUB(fp))
140 curoff -= fp->_ur;
141 }
142
143 /*
144 * Compute the number of bytes in the input buffer (pretending
145 * that any ungetc() input has been discarded). Adjust current
146 * offset backwards by this count so that it represents the
147 * file offset for the first byte in the current input buffer.
148 */
149 if (HASUB(fp)) {
150 curoff += fp->_r; /* kill off ungetc */
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 fgetln.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, (fpos_t)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