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.\" 3. 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.\" 30.Dd July 13, 2020 31.Dt LSEEK 2 32.Os 33.Sh NAME 34.Nm lseek 35.Nd reposition read/write file offset 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In unistd.h 40.Ft off_t 41.Fn lseek "int fildes" "off_t offset" "int whence" 42.Sh DESCRIPTION 43The 44.Fn lseek 45system call repositions the offset of the file descriptor 46.Fa fildes 47to the 48argument 49.Fa offset 50according to the directive 51.Fa whence . 52The argument 53.Fa fildes 54must be an open 55file descriptor. 56The 57.Fn lseek 58system call 59repositions the file position pointer associated with the file 60descriptor 61.Fa fildes 62as follows: 63.Bl -item -offset indent 64.It 65If 66.Fa whence 67is 68.Dv SEEK_SET , 69the offset is set to 70.Fa offset 71bytes. 72.It 73If 74.Fa whence 75is 76.Dv SEEK_CUR , 77the offset is set to its current location plus 78.Fa offset 79bytes. 80.It 81If 82.Fa whence 83is 84.Dv SEEK_END , 85the offset is set to the size of the 86file plus 87.Fa offset 88bytes. 89.It 90If 91.Fa whence 92is 93.Dv SEEK_HOLE , 94the offset is set to the start of the next hole greater than or equal 95to the supplied 96.Fa offset . 97The definition of a hole is provided below. 98.It 99If 100.Fa whence 101is 102.Dv SEEK_DATA , 103the offset is set to the start of the next non-hole file region greater 104than or equal to the supplied 105.Fa offset . 106.El 107.Pp 108The 109.Fn lseek 110system call allows the file offset to be set beyond the end 111of the existing end-of-file of the file. 112If data is later written 113at this point, subsequent reads of the data in the gap return 114bytes of zeros (until data is actually written into the gap). 115However, the 116.Fn lseek 117system call does not, by itself, extend the size of a file. 118.Pp 119A 120.Qq hole 121is defined as a contiguous range of bytes in a file, all having the value of 122zero, but not all zeros in a file are guaranteed to be represented as holes 123returned with 124.Dv SEEK_HOLE . 125File systems are allowed to expose ranges of zeros with 126.Dv SEEK_HOLE , 127but not required to. 128Applications can use 129.Dv SEEK_HOLE 130to optimise their behavior for ranges of zeros, but must not depend on it to 131find all such ranges in a file. 132Each file is presented as having a zero-size virtual hole at the very 133end of the file. 134The existence of a hole at the end of every data region allows for easy 135programming and also provides compatibility to the original implementation 136in Solaris. 137It also causes the current file size (i.e., end-of-file offset) to be returned 138to indicate that there are no more holes past the supplied 139.Fa offset . 140Applications should use 141.Fn fpathconf _PC_MIN_HOLE_SIZE 142or 143.Fn pathconf _PC_MIN_HOLE_SIZE 144to determine if a file system supports 145.Dv SEEK_HOLE . 146See 147.Xr pathconf 2 . 148.Pp 149For file systems that do not supply information about holes, the file will be 150represented as one entire data region. 151.Sh RETURN VALUES 152Upon successful completion, 153.Fn lseek 154returns the resulting offset location as measured in bytes from the 155beginning of the file. 156Otherwise, 157a value of -1 is returned and 158.Va errno 159is set to indicate 160the error. 161.Sh ERRORS 162The 163.Fn lseek 164system call 165will fail and the file position pointer will remain unchanged if: 166.Bl -tag -width Er 167.It Bq Er EBADF 168The 169.Fa fildes 170argument 171is not an open file descriptor. 172.It Bq Er EINVAL 173The 174.Fa whence 175argument 176is not a proper value 177or the resulting file offset would 178be negative for a non-character special file. 179.It Bq Er ENXIO 180For 181.Dv SEEK_DATA , 182there are no more data regions past the supplied offset. 183Due to existence of the hole at the end of the file, for 184.Dv SEEK_HOLE 185this error is only returned when the 186.Fa offset 187already points to the end-of-file position. 188.It Bq Er EOVERFLOW 189The resulting file offset would be a value which cannot be represented 190correctly in an object of type 191.Fa off_t . 192.It Bq Er ESPIPE 193The 194.Fa fildes 195argument 196is associated with a pipe, socket, or FIFO. 197.El 198.Sh SEE ALSO 199.Xr dup 2 , 200.Xr open 2 , 201.Xr pathconf 2 202.Sh STANDARDS 203The 204.Fn lseek 205system call is expected to conform to 206.St -p1003.1-2008 . 207.Pp 208The 209.Dv SEEK_HOLE 210and 211.Dv SEEK_DATA 212directives, along with the 213.Er ENXIO 214error, are extensions to that specification. 215.Sh HISTORY 216The 217.Fn lseek 218function appeared in 219.At v7 . 220.Sh BUGS 221If the 222.Fn lseek 223system call is operating on a device which is incapable of seeking, 224it will request the seek operation and return successfully, 225even though no seek was performed. 226Because the 227.Ar offset 228argument will be stored unconditionally in the file descriptor of that device, 229there is no way to confirm if the seek operation succeeded or not 230(e.g. using the 231.Fn ftell 232function). 233Device types which are known to be incapable of seeking include 234tape drives. 235.Pp 236The 237.Fn lseek 238system call will not detect whether media are present in changeable 239media devices such as DVD or Blu-ray devices. 240A requested seek operation will therefore return sucessfully when no 241medium is present. 242.Pp 243This document's use of 244.Fa whence 245is incorrect English, but is maintained for historical reasons. 246