138451Smsmith/*	$NetBSD: lseek.c,v 1.4 1997/01/22 00:38:10 cgd Exp $	*/
238451Smsmith
338451Smsmith/*-
438451Smsmith * Copyright (c) 1993
538451Smsmith *	The Regents of the University of California.  All rights reserved.
638451Smsmith *
738451Smsmith * This code is derived from software contributed to Berkeley by
838451Smsmith * The Mach Operating System project at Carnegie-Mellon University.
938451Smsmith *
1038451Smsmith * Redistribution and use in source and binary forms, with or without
1138451Smsmith * modification, are permitted provided that the following conditions
1238451Smsmith * are met:
1338451Smsmith * 1. Redistributions of source code must retain the above copyright
1438451Smsmith *    notice, this list of conditions and the following disclaimer.
1538451Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1638451Smsmith *    notice, this list of conditions and the following disclaimer in the
1738451Smsmith *    documentation and/or other materials provided with the distribution.
1838451Smsmith * 4. Neither the name of the University nor the names of its contributors
1938451Smsmith *    may be used to endorse or promote products derived from this software
2038451Smsmith *    without specific prior written permission.
2138451Smsmith *
2238451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2338451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2438451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2538451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2638451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2738451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2838451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2938451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3038451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3138451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3238451Smsmith * SUCH DAMAGE.
3338451Smsmith *
3438451Smsmith *	@(#)lseek.c	8.1 (Berkeley) 6/11/93
3538451Smsmith *
3638451Smsmith *
3738451Smsmith * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
3838451Smsmith * All Rights Reserved.
3938451Smsmith *
4038451Smsmith * Author: Alessandro Forin
4138451Smsmith *
4238451Smsmith * Permission to use, copy, modify and distribute this software and its
4338451Smsmith * documentation is hereby granted, provided that both the copyright
4438451Smsmith * notice and this permission notice appear in all copies of the
4538451Smsmith * software, derivative works or modified versions, and any portions
4638451Smsmith * thereof, and that both notices appear in supporting documentation.
4738451Smsmith *
4838451Smsmith * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
4938451Smsmith * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
5038451Smsmith * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
5138451Smsmith *
5238451Smsmith * Carnegie Mellon requests users of this software to return to
5338451Smsmith *
5438451Smsmith *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
5538451Smsmith *  School of Computer Science
5638451Smsmith *  Carnegie Mellon University
5738451Smsmith *  Pittsburgh PA 15213-3890
5838451Smsmith *
5938451Smsmith * any improvements or extensions that they make and grant Carnegie the
6038451Smsmith * rights to redistribute these changes.
6138451Smsmith */
6238451Smsmith
6384221Sdillon#include <sys/cdefs.h>
6484221Sdillon__FBSDID("$FreeBSD: stable/11/stand/libsa/lseek.c 329132 2018-02-11 19:51:29Z kevans $");
6584221Sdillon
6638451Smsmith#include "stand.h"
6738451Smsmith
6838451Smsmithoff_t
6965470Smsmithlseek(int fd, off_t offset, int where)
7038451Smsmith{
7182526Sjdp    off_t bufpos, filepos, target;
7265470Smsmith    struct open_file *f = &files[fd];
7338451Smsmith
7465470Smsmith    if ((unsigned)fd >= SOPEN_MAX || f->f_flags == 0) {
7565470Smsmith	errno = EBADF;
7665470Smsmith	return (-1);
7765470Smsmith    }
7838451Smsmith
7965470Smsmith    if (f->f_flags & F_RAW) {
8065470Smsmith	/*
8165470Smsmith	 * On RAW devices, update internal offset.
8265470Smsmith	 */
8365470Smsmith	switch (where) {
8465470Smsmith	case SEEK_SET:
8565470Smsmith	    f->f_offset = offset;
8665470Smsmith	    break;
8765470Smsmith	case SEEK_CUR:
8865470Smsmith	    f->f_offset += offset;
8965470Smsmith	    break;
9065470Smsmith	default:
9165470Smsmith	    errno = EOFFSET;
9265470Smsmith	    return (-1);
9338451Smsmith	}
9465470Smsmith	return (f->f_offset);
9565470Smsmith    }
9638451Smsmith
9765470Smsmith    /*
9882526Sjdp     * If there is some unconsumed data in the readahead buffer and it
9982526Sjdp     * contains the desired offset, simply adjust the buffer offset and
10082526Sjdp     * length.  We don't bother with SEEK_END here, since the code to
10182526Sjdp     * handle it would fail in the same cases where the non-readahead
10282526Sjdp     * code fails (namely, for streams which cannot seek backward and whose
10382526Sjdp     * size isn't known in advance).
10482526Sjdp     */
10582526Sjdp    if (f->f_ralen != 0 && where != SEEK_END) {
10682526Sjdp	if ((filepos = (f->f_ops->fo_seek)(f, (off_t)0, SEEK_CUR)) == -1)
10782526Sjdp	    return (-1);
10882526Sjdp	bufpos = filepos - f->f_ralen;
10982526Sjdp	switch (where) {
11082526Sjdp	case SEEK_SET:
11182526Sjdp	    target = offset;
11282526Sjdp	    break;
11382526Sjdp	case SEEK_CUR:
11482526Sjdp	    target = bufpos + offset;
11582526Sjdp	    break;
11682526Sjdp	default:
11782526Sjdp	    errno = EINVAL;
11882526Sjdp	    return (-1);
11982526Sjdp	}
12082526Sjdp	if (bufpos <= target && target < filepos) {
12182526Sjdp	    f->f_raoffset += target - bufpos;
12282526Sjdp	    f->f_ralen -= target - bufpos;
12382526Sjdp	    return (target);
12482526Sjdp	}
12582526Sjdp    }
12682526Sjdp
12782526Sjdp    /*
12865470Smsmith     * If this is a relative seek, we need to correct the offset for
12965470Smsmith     * bytes that we have already read but the caller doesn't know
13065470Smsmith     * about.
13165470Smsmith     */
13265470Smsmith    if (where == SEEK_CUR)
13365470Smsmith	offset -= f->f_ralen;
13465470Smsmith
13565470Smsmith    /*
13665470Smsmith     * Invalidate the readahead buffer.
13765470Smsmith     */
13865470Smsmith    f->f_ralen = 0;
13965470Smsmith
14065470Smsmith    return (f->f_ops->fo_seek)(f, offset, where);
14138451Smsmith}
142