1.\" Copyright (c) 1980, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 4. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)lseek.2 8.3 (Berkeley) 4/19/94 29.\" $FreeBSD$ 30.\" 31.Dd May 26, 2012 32.Dt LSEEK 2 33.Os 34.Sh NAME 35.Nm lseek 36.Nd reposition read/write file offset 37.Sh LIBRARY 38.Lb libc 39.Sh SYNOPSIS 40.In unistd.h 41.Ft off_t 42.Fn lseek "int fildes" "off_t offset" "int whence" 43.Sh DESCRIPTION 44The 45.Fn lseek 46system call repositions the offset of the file descriptor 47.Fa fildes 48to the 49argument 50.Fa offset 51according to the directive 52.Fa whence . 53The argument 54.Fa fildes 55must be an open 56file descriptor. 57The 58.Fn lseek 59system call 60repositions the file position pointer associated with the file 61descriptor 62.Fa fildes 63as follows: 64.Bl -item -offset indent 65.It 66If 67.Fa whence 68is 69.Dv SEEK_SET , 70the offset is set to 71.Fa offset 72bytes. 73.It 74If 75.Fa whence 76is 77.Dv SEEK_CUR , 78the offset is set to its current location plus 79.Fa offset 80bytes. 81.It 82If 83.Fa whence 84is 85.Dv SEEK_END , 86the offset is set to the size of the 87file plus 88.Fa offset 89bytes. 90.It 91If 92.Fa whence 93is 94.Dv SEEK_HOLE , 95the offset is set to the start of the next hole greater than or equal 96to the supplied 97.Fa offset . 98The definition of a hole is provided below. 99.It 100If 101.Fa whence 102is 103.Dv SEEK_DATA , 104the offset is set to the start of the next non-hole file region greater 105than or equal to the supplied 106.Fa offset . 107.El 108.Pp 109The 110.Fn lseek 111system call allows the file offset to be set beyond the end 112of the existing end-of-file of the file. 113If data is later written 114at this point, subsequent reads of the data in the gap return 115bytes of zeros (until data is actually written into the gap). 116.Pp 117Some devices are incapable of seeking. 118The value of the pointer 119associated with such a device is undefined. 120.Pp 121A 122.Qq hole 123is defined as a contiguous range of bytes in a file, all having the value of 124zero, but not all zeros in a file are guaranteed to be represented as holes 125returned with 126.Dv SEEK_HOLE . 127File systems are allowed to expose ranges of zeros with 128.Dv SEEK_HOLE , 129but not required to. 130Applications can use 131.Dv SEEK_HOLE 132to optimise their behavior for ranges of zeros, but must not depend on it to 133find all such ranges in a file. 134The existence of a hole at the end of every data region allows for easy 135programming and implies that a virtual hole exists at the end of the file. 136Applications should use 137.Fn fpathconf _PC_MIN_HOLE_SIZE 138or 139.Fn pathconf _PC_MIN_HOLE_SIZE 140to determine if a file system supports 141.Dv SEEK_HOLE . 142See 143.Xr pathconf 2 . 144.Pp 145For file systems that do not supply information about holes, the file will be 146represented as one entire data region. 147.Sh RETURN VALUES 148Upon successful completion, 149.Fn lseek 150returns the resulting offset location as measured in bytes from the 151beginning of the file. 152Otherwise, 153a value of -1 is returned and 154.Va errno 155is set to indicate 156the error. 157.Sh ERRORS 158The 159.Fn lseek 160system call 161will fail and the file position pointer will remain unchanged if: 162.Bl -tag -width Er 163.It Bq Er EBADF 164The 165.Fa fildes 166argument 167is not an open file descriptor. 168.It Bq Er EINVAL 169The 170.Fa whence 171argument 172is not a proper value 173or the resulting file offset would 174be negative for a non-character special file. 175.It Bq Er ENXIO 176For 177.Dv SEEK_DATA , 178there are no more data regions past the supplied offset. 179For 180.Dv SEEK_HOLE , 181there are no more holes past the supplied offset. 182.It Bq Er EOVERFLOW 183The resulting file offset would be a value which cannot be represented 184correctly in an object of type 185.Fa off_t . 186.It Bq Er ESPIPE 187The 188.Fa fildes 189argument 190is associated with a pipe, socket, or FIFO. 191.El 192.Sh SEE ALSO 193.Xr dup 2 , 194.Xr open 2 , 195.Xr pathconf 2 196.Sh STANDARDS 197The 198.Fn lseek 199system call is expected to conform to 200.St -p1003.1-90 . 201.Sh HISTORY 202The 203.Fn lseek 204function appeared in 205.At v7 . 206.Sh BUGS 207This document's use of 208.Fa whence 209is incorrect English, but is maintained for historical reasons. 210