1*22028508SToomas Soome /* $NetBSD: lseek.c,v 1.4 1997/01/22 00:38:10 cgd Exp $ */
2*22028508SToomas Soome
3*22028508SToomas Soome /*-
4*22028508SToomas Soome * Copyright (c) 1993
5*22028508SToomas Soome * The Regents of the University of California. All rights reserved.
6*22028508SToomas Soome *
7*22028508SToomas Soome * This code is derived from software contributed to Berkeley by
8*22028508SToomas Soome * The Mach Operating System project at Carnegie-Mellon University.
9*22028508SToomas Soome *
10*22028508SToomas Soome * Redistribution and use in source and binary forms, with or without
11*22028508SToomas Soome * modification, are permitted provided that the following conditions
12*22028508SToomas Soome * are met:
13*22028508SToomas Soome * 1. Redistributions of source code must retain the above copyright
14*22028508SToomas Soome * notice, this list of conditions and the following disclaimer.
15*22028508SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
16*22028508SToomas Soome * notice, this list of conditions and the following disclaimer in the
17*22028508SToomas Soome * documentation and/or other materials provided with the distribution.
18*22028508SToomas Soome * 3. Neither the name of the University nor the names of its contributors
19*22028508SToomas Soome * may be used to endorse or promote products derived from this software
20*22028508SToomas Soome * without specific prior written permission.
21*22028508SToomas Soome *
22*22028508SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*22028508SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*22028508SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*22028508SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*22028508SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*22028508SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*22028508SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*22028508SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*22028508SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*22028508SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*22028508SToomas Soome * SUCH DAMAGE.
33*22028508SToomas Soome *
34*22028508SToomas Soome * @(#)lseek.c 8.1 (Berkeley) 6/11/93
35*22028508SToomas Soome *
36*22028508SToomas Soome *
37*22028508SToomas Soome * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38*22028508SToomas Soome * All Rights Reserved.
39*22028508SToomas Soome *
40*22028508SToomas Soome * Author: Alessandro Forin
41*22028508SToomas Soome *
42*22028508SToomas Soome * Permission to use, copy, modify and distribute this software and its
43*22028508SToomas Soome * documentation is hereby granted, provided that both the copyright
44*22028508SToomas Soome * notice and this permission notice appear in all copies of the
45*22028508SToomas Soome * software, derivative works or modified versions, and any portions
46*22028508SToomas Soome * thereof, and that both notices appear in supporting documentation.
47*22028508SToomas Soome *
48*22028508SToomas Soome * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49*22028508SToomas Soome * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50*22028508SToomas Soome * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51*22028508SToomas Soome *
52*22028508SToomas Soome * Carnegie Mellon requests users of this software to return to
53*22028508SToomas Soome *
54*22028508SToomas Soome * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
55*22028508SToomas Soome * School of Computer Science
56*22028508SToomas Soome * Carnegie Mellon University
57*22028508SToomas Soome * Pittsburgh PA 15213-3890
58*22028508SToomas Soome *
59*22028508SToomas Soome * any improvements or extensions that they make and grant Carnegie the
60*22028508SToomas Soome * rights to redistribute these changes.
61*22028508SToomas Soome */
62*22028508SToomas Soome
63*22028508SToomas Soome #include <sys/cdefs.h>
64*22028508SToomas Soome #include "stand.h"
65*22028508SToomas Soome
66*22028508SToomas Soome off_t
lseek(int fd,off_t offset,int where)67*22028508SToomas Soome lseek(int fd, off_t offset, int where)
68*22028508SToomas Soome {
69*22028508SToomas Soome off_t bufpos, filepos, target;
70*22028508SToomas Soome struct open_file *f;
71*22028508SToomas Soome
72*22028508SToomas Soome f = fd2open_file(fd);
73*22028508SToomas Soome if (f == NULL || f->f_flags == 0) {
74*22028508SToomas Soome errno = EBADF;
75*22028508SToomas Soome return (-1);
76*22028508SToomas Soome }
77*22028508SToomas Soome
78*22028508SToomas Soome if (f->f_flags & F_RAW) {
79*22028508SToomas Soome /*
80*22028508SToomas Soome * On RAW devices, update internal offset.
81*22028508SToomas Soome */
82*22028508SToomas Soome switch (where) {
83*22028508SToomas Soome case SEEK_SET:
84*22028508SToomas Soome f->f_offset = offset;
85*22028508SToomas Soome break;
86*22028508SToomas Soome case SEEK_CUR:
87*22028508SToomas Soome f->f_offset += offset;
88*22028508SToomas Soome break;
89*22028508SToomas Soome default:
90*22028508SToomas Soome errno = EOFFSET;
91*22028508SToomas Soome return (-1);
92*22028508SToomas Soome }
93*22028508SToomas Soome return (f->f_offset);
94*22028508SToomas Soome }
95*22028508SToomas Soome
96*22028508SToomas Soome /*
97*22028508SToomas Soome * If there is some unconsumed data in the readahead buffer and it
98*22028508SToomas Soome * contains the desired offset, simply adjust the buffer offset and
99*22028508SToomas Soome * length. We don't bother with SEEK_END here, since the code to
100*22028508SToomas Soome * handle it would fail in the same cases where the non-readahead
101*22028508SToomas Soome * code fails (namely, for streams which cannot seek backward and whose
102*22028508SToomas Soome * size isn't known in advance).
103*22028508SToomas Soome */
104*22028508SToomas Soome if (f->f_ralen != 0 && where != SEEK_END) {
105*22028508SToomas Soome filepos = (f->f_ops->fo_seek)(f, 0, SEEK_CUR);
106*22028508SToomas Soome if (filepos == -1)
107*22028508SToomas Soome return (-1);
108*22028508SToomas Soome bufpos = filepos - f->f_ralen;
109*22028508SToomas Soome switch (where) {
110*22028508SToomas Soome case SEEK_SET:
111*22028508SToomas Soome target = offset;
112*22028508SToomas Soome break;
113*22028508SToomas Soome case SEEK_CUR:
114*22028508SToomas Soome target = bufpos + offset;
115*22028508SToomas Soome break;
116*22028508SToomas Soome default:
117*22028508SToomas Soome errno = EINVAL;
118*22028508SToomas Soome return (-1);
119*22028508SToomas Soome }
120*22028508SToomas Soome if (bufpos <= target && target < filepos) {
121*22028508SToomas Soome f->f_raoffset += target - bufpos;
122*22028508SToomas Soome f->f_ralen -= target - bufpos;
123*22028508SToomas Soome return (target);
124*22028508SToomas Soome }
125*22028508SToomas Soome }
126*22028508SToomas Soome
127*22028508SToomas Soome /*
128*22028508SToomas Soome * If this is a relative seek, we need to correct the offset for
129*22028508SToomas Soome * bytes that we have already read but the caller doesn't know
130*22028508SToomas Soome * about.
131*22028508SToomas Soome */
132*22028508SToomas Soome if (where == SEEK_CUR)
133*22028508SToomas Soome offset -= f->f_ralen;
134*22028508SToomas Soome
135*22028508SToomas Soome /*
136*22028508SToomas Soome * Invalidate the readahead buffer.
137*22028508SToomas Soome */
138*22028508SToomas Soome f->f_ralen = 0;
139*22028508SToomas Soome
140*22028508SToomas Soome return (f->f_ops->fo_seek)(f, offset, where);
141*22028508SToomas Soome }
142