Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libstdio/fseek.c

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

Archive Download this file

Revision: 2154