xref: /illumos-gate/usr/src/boot/libsa/lseek.c (revision 22028508)
1199767f8SToomas Soome /*	$NetBSD: lseek.c,v 1.4 1997/01/22 00:38:10 cgd Exp $	*/
2199767f8SToomas Soome 
3199767f8SToomas Soome /*-
4199767f8SToomas Soome  * Copyright (c) 1993
5199767f8SToomas Soome  *	The Regents of the University of California.  All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * This code is derived from software contributed to Berkeley by
8199767f8SToomas Soome  * The Mach Operating System project at Carnegie-Mellon University.
9199767f8SToomas Soome  *
10199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
11199767f8SToomas Soome  * modification, are permitted provided that the following conditions
12199767f8SToomas Soome  * are met:
13199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
15199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
16199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
17199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
181974da4bSToomas Soome  * 3. Neither the name of the University nor the names of its contributors
19199767f8SToomas Soome  *    may be used to endorse or promote products derived from this software
20199767f8SToomas Soome  *    without specific prior written permission.
21199767f8SToomas Soome  *
22199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32199767f8SToomas Soome  * SUCH DAMAGE.
33199767f8SToomas Soome  *
34199767f8SToomas Soome  *	@(#)lseek.c	8.1 (Berkeley) 6/11/93
35a2027c5dSToomas Soome  *
36199767f8SToomas Soome  *
37199767f8SToomas Soome  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
38199767f8SToomas Soome  * All Rights Reserved.
39199767f8SToomas Soome  *
40199767f8SToomas Soome  * Author: Alessandro Forin
41a2027c5dSToomas Soome  *
42199767f8SToomas Soome  * Permission to use, copy, modify and distribute this software and its
43199767f8SToomas Soome  * documentation is hereby granted, provided that both the copyright
44199767f8SToomas Soome  * notice and this permission notice appear in all copies of the
45199767f8SToomas Soome  * software, derivative works or modified versions, and any portions
46199767f8SToomas Soome  * thereof, and that both notices appear in supporting documentation.
47a2027c5dSToomas Soome  *
48199767f8SToomas Soome  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49199767f8SToomas Soome  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
50199767f8SToomas Soome  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51a2027c5dSToomas Soome  *
52199767f8SToomas Soome  * Carnegie Mellon requests users of this software to return to
53a2027c5dSToomas Soome  *
54199767f8SToomas Soome  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55199767f8SToomas Soome  *  School of Computer Science
56199767f8SToomas Soome  *  Carnegie Mellon University
57199767f8SToomas Soome  *  Pittsburgh PA 15213-3890
58a2027c5dSToomas Soome  *
59199767f8SToomas Soome  * any improvements or extensions that they make and grant Carnegie the
60199767f8SToomas Soome  * rights to redistribute these changes.
61199767f8SToomas Soome  */
62199767f8SToomas Soome 
63199767f8SToomas Soome #include <sys/cdefs.h>
64199767f8SToomas Soome #include "stand.h"
65199767f8SToomas Soome 
66199767f8SToomas Soome off_t
lseek(int fd,off_t offset,int where)67199767f8SToomas Soome lseek(int fd, off_t offset, int where)
68199767f8SToomas Soome {
69a2027c5dSToomas Soome 	off_t bufpos, filepos, target;
70a2027c5dSToomas Soome 	struct open_file *f;
71199767f8SToomas Soome 
72a2027c5dSToomas Soome 	f = fd2open_file(fd);
73a2027c5dSToomas Soome 	if (f == NULL || f->f_flags == 0) {
74a2027c5dSToomas Soome 		errno = EBADF;
75a2027c5dSToomas Soome 		return (-1);
76199767f8SToomas Soome 	}
77199767f8SToomas Soome 
78a2027c5dSToomas Soome 	if (f->f_flags & F_RAW) {
79a2027c5dSToomas Soome 		/*
80a2027c5dSToomas Soome 		 * On RAW devices, update internal offset.
81a2027c5dSToomas Soome 		 */
82a2027c5dSToomas Soome 		switch (where) {
83a2027c5dSToomas Soome 		case SEEK_SET:
84a2027c5dSToomas Soome 			f->f_offset = offset;
85a2027c5dSToomas Soome 			break;
86a2027c5dSToomas Soome 		case SEEK_CUR:
87a2027c5dSToomas Soome 			f->f_offset += offset;
88a2027c5dSToomas Soome 			break;
89a2027c5dSToomas Soome 		default:
90a2027c5dSToomas Soome 			errno = EOFFSET;
91a2027c5dSToomas Soome 			return (-1);
92a2027c5dSToomas Soome 		}
93a2027c5dSToomas Soome 		return (f->f_offset);
94199767f8SToomas Soome 	}
95a2027c5dSToomas Soome 
96a2027c5dSToomas Soome 	/*
97a2027c5dSToomas Soome 	 * If there is some unconsumed data in the readahead buffer and it
98a2027c5dSToomas Soome 	 * contains the desired offset, simply adjust the buffer offset and
99a2027c5dSToomas Soome 	 * length.  We don't bother with SEEK_END here, since the code to
100a2027c5dSToomas Soome 	 * handle it would fail in the same cases where the non-readahead
101a2027c5dSToomas Soome 	 * code fails (namely, for streams which cannot seek backward and whose
102a2027c5dSToomas Soome 	 * size isn't known in advance).
103a2027c5dSToomas Soome 	 */
104a2027c5dSToomas Soome 	if (f->f_ralen != 0 && where != SEEK_END) {
105a2027c5dSToomas Soome 		filepos = (f->f_ops->fo_seek)(f, 0, SEEK_CUR);
106a2027c5dSToomas Soome 		if (filepos == -1)
107a2027c5dSToomas Soome 			return (-1);
108a2027c5dSToomas Soome 		bufpos = filepos - f->f_ralen;
109a2027c5dSToomas Soome 		switch (where) {
110a2027c5dSToomas Soome 		case SEEK_SET:
111a2027c5dSToomas Soome 			target = offset;
112a2027c5dSToomas Soome 			break;
113a2027c5dSToomas Soome 		case SEEK_CUR:
114a2027c5dSToomas Soome 			target = bufpos + offset;
115a2027c5dSToomas Soome 			break;
116a2027c5dSToomas Soome 		default:
117a2027c5dSToomas Soome 			errno = EINVAL;
118a2027c5dSToomas Soome 			return (-1);
119a2027c5dSToomas Soome 		}
120a2027c5dSToomas Soome 		if (bufpos <= target && target < filepos) {
121a2027c5dSToomas Soome 			f->f_raoffset += target - bufpos;
122a2027c5dSToomas Soome 			f->f_ralen -= target - bufpos;
123a2027c5dSToomas Soome 			return (target);
124a2027c5dSToomas Soome 		}
125199767f8SToomas Soome 	}
126199767f8SToomas Soome 
127a2027c5dSToomas Soome 	/*
128a2027c5dSToomas Soome 	 * If this is a relative seek, we need to correct the offset for
129a2027c5dSToomas Soome 	 * bytes that we have already read but the caller doesn't know
130a2027c5dSToomas Soome 	 * about.
131a2027c5dSToomas Soome 	 */
132a2027c5dSToomas Soome 	if (where == SEEK_CUR)
133a2027c5dSToomas Soome 		offset -= f->f_ralen;
134199767f8SToomas Soome 
135a2027c5dSToomas Soome 	/*
136a2027c5dSToomas Soome 	 * Invalidate the readahead buffer.
137a2027c5dSToomas Soome 	 */
138a2027c5dSToomas Soome 	f->f_ralen = 0;
139199767f8SToomas Soome 
140a2027c5dSToomas Soome 	return (f->f_ops->fo_seek)(f, offset, where);
141199767f8SToomas Soome }
142