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