Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/stand/libsa/lseek.c
34680 views
1
/* $NetBSD: lseek.c,v 1.4 1997/01/22 00:38:10 cgd Exp $ */
2
3
/*-
4
* Copyright (c) 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
* The Mach Operating System project at Carnegie-Mellon University.
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
* Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
36
* All Rights Reserved.
37
*
38
* Author: Alessandro Forin
39
*
40
* Permission to use, copy, modify and distribute this software and its
41
* documentation is hereby granted, provided that both the copyright
42
* notice and this permission notice appear in all copies of the
43
* software, derivative works or modified versions, and any portions
44
* thereof, and that both notices appear in supporting documentation.
45
*
46
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
48
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49
*
50
* Carnegie Mellon requests users of this software to return to
51
*
52
* Software Distribution Coordinator or [email protected]
53
* School of Computer Science
54
* Carnegie Mellon University
55
* Pittsburgh PA 15213-3890
56
*
57
* any improvements or extensions that they make and grant Carnegie the
58
* rights to redistribute these changes.
59
*/
60
61
#include "stand.h"
62
63
off_t
64
lseek(int fd, off_t offset, int where)
65
{
66
off_t bufpos, filepos, target;
67
struct open_file *f;
68
69
f = fd2open_file(fd);
70
if (f == NULL || f->f_flags == 0) {
71
errno = EBADF;
72
return (-1);
73
}
74
75
if (f->f_flags & F_RAW) {
76
/*
77
* On RAW devices, update internal offset.
78
*/
79
switch (where) {
80
case SEEK_SET:
81
f->f_offset = offset;
82
break;
83
case SEEK_CUR:
84
f->f_offset += offset;
85
break;
86
default:
87
errno = EOFFSET;
88
return (-1);
89
}
90
return (f->f_offset);
91
}
92
93
/*
94
* If there is some unconsumed data in the readahead buffer and it
95
* contains the desired offset, simply adjust the buffer offset and
96
* length. We don't bother with SEEK_END here, since the code to
97
* handle it would fail in the same cases where the non-readahead
98
* code fails (namely, for streams which cannot seek backward and whose
99
* size isn't known in advance).
100
*/
101
if (f->f_ralen != 0 && where != SEEK_END) {
102
filepos = (f->f_ops->fo_seek)(f, 0, SEEK_CUR);
103
if (filepos == -1)
104
return (-1);
105
bufpos = filepos - f->f_ralen;
106
switch (where) {
107
case SEEK_SET:
108
target = offset;
109
break;
110
case SEEK_CUR:
111
target = bufpos + offset;
112
break;
113
default:
114
errno = EINVAL;
115
return (-1);
116
}
117
if (bufpos <= target && target < filepos) {
118
f->f_raoffset += target - bufpos;
119
f->f_ralen -= target - bufpos;
120
return (target);
121
}
122
}
123
124
/*
125
* If this is a relative seek, we need to correct the offset for
126
* bytes that we have already read but the caller doesn't know
127
* about.
128
*/
129
if (where == SEEK_CUR)
130
offset -= f->f_ralen;
131
132
/*
133
* Invalidate the readahead buffer.
134
*/
135
f->f_ralen = 0;
136
137
return (f->f_ops->fo_seek)(f, offset, where);
138
}
139
140