]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/lseek.2
Many pages: Fix style issues reported by `make lint-groff`
[thirdparty/man-pages.git] / man2 / lseek.2
1 .\" Copyright (c) 1980, 1991 Regents of the University of California.
2 .\" and Copyright (c) 2011, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" All rights reserved.
4 .\"
5 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\"
7 .\" @(#)lseek.2 6.5 (Berkeley) 3/10/91
8 .\"
9 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
10 .\" Modified 1995-06-10 by Andries Brouwer <aeb@cwi.nl>
11 .\" Modified 1996-10-31 by Eric S. Raymond <esr@thyrsus.com>
12 .\" Modified 1998-01-17 by Michael Haardt
13 .\" <michael@cantor.informatik.rwth-aachen.de>
14 .\" Modified 2001-09-24 by Michael Haardt <michael@moria.de>
15 .\" Modified 2003-08-21 by Andries Brouwer <aeb@cwi.nl>
16 .\" 2011-09-18, mtk, Added SEEK_DATA + SEEK_HOLE
17 .\"
18 .TH LSEEK 2 2021-03-22 "Linux" "Linux Programmer's Manual"
19 .SH NAME
20 lseek \- reposition read/write file offset
21 .SH LIBRARY
22 Standard C library
23 .RI ( libc ", " \-lc )
24 .SH SYNOPSIS
25 .nf
26 .B #include <unistd.h>
27 .PP
28 .BI "off_t lseek(int " fd ", off_t " offset ", int " whence );
29 .fi
30 .SH DESCRIPTION
31 .BR lseek ()
32 repositions the file offset of the open file description
33 associated with the file descriptor
34 .I fd
35 to the argument
36 .I offset
37 according to the directive
38 .I whence
39 as follows:
40 .TP
41 .B SEEK_SET
42 The file offset is set to
43 .I offset
44 bytes.
45 .TP
46 .B SEEK_CUR
47 The file offset is set to its current location plus
48 .I offset
49 bytes.
50 .TP
51 .B SEEK_END
52 The file offset is set to the size of the file plus
53 .I offset
54 bytes.
55 .PP
56 .BR lseek ()
57 allows the file offset to be set beyond the end
58 of the file (but this does not change the size of the file).
59 If data is later written at this point, subsequent reads of the data
60 in the gap (a "hole") return null bytes (\(aq\e0\(aq) until
61 data is actually written into the gap.
62 .SS Seeking file data and holes
63 Since version 3.1, Linux supports the following additional values for
64 .IR whence :
65 .TP
66 .B SEEK_DATA
67 Adjust the file offset to the next location
68 in the file greater than or equal to
69 .I offset
70 containing data.
71 If
72 .I offset
73 points to data,
74 then the file offset is set to
75 .IR offset .
76 .TP
77 .B SEEK_HOLE
78 Adjust the file offset to the next hole in the file
79 greater than or equal to
80 .IR offset .
81 If
82 .I offset
83 points into the middle of a hole,
84 then the file offset is set to
85 .IR offset .
86 If there is no hole past
87 .IR offset ,
88 then the file offset is adjusted to the end of the file
89 (i.e., there is an implicit hole at the end of any file).
90 .PP
91 In both of the above cases,
92 .BR lseek ()
93 fails if
94 .I offset
95 points past the end of the file.
96 .PP
97 These operations allow applications to map holes in a sparsely
98 allocated file.
99 This can be useful for applications such as file backup tools,
100 which can save space when creating backups and preserve holes,
101 if they have a mechanism for discovering holes.
102 .PP
103 For the purposes of these operations, a hole is a sequence of zeros that
104 (normally) has not been allocated in the underlying file storage.
105 However, a filesystem is not obliged to report holes,
106 so these operations are not a guaranteed mechanism for
107 mapping the storage space actually allocated to a file.
108 (Furthermore, a sequence of zeros that actually has been written
109 to the underlying storage may not be reported as a hole.)
110 In the simplest implementation,
111 a filesystem can support the operations by making
112 .B SEEK_HOLE
113 always return the offset of the end of the file,
114 and making
115 .B SEEK_DATA
116 always return
117 .I offset
118 (i.e., even if the location referred to by
119 .I offset
120 is a hole,
121 it can be considered to consist of data that is a sequence of zeros).
122 .\" https://lkml.org/lkml/2011/4/22/79
123 .\" http://lwn.net/Articles/440255/
124 .\" http://blogs.oracle.com/bonwick/entry/seek_hole_and_seek_data
125 .PP
126 The
127 .B _GNU_SOURCE
128 feature test macro must be defined in order to obtain the definitions of
129 .B SEEK_DATA
130 and
131 .B SEEK_HOLE
132 from
133 .IR <unistd.h> .
134 .PP
135 The
136 .B SEEK_HOLE
137 and
138 .B SEEK_DATA
139 operations are supported for the following filesystems:
140 .IP * 3
141 Btrfs (since Linux 3.1)
142 .IP * 3
143 OCFS (since Linux 3.2)
144 .\" commit 93862d5e1ab875664c6cc95254fc365028a48bb1
145 .IP *
146 XFS (since Linux 3.5)
147 .IP *
148 ext4 (since Linux 3.8)
149 .IP *
150 .BR tmpfs (5)
151 (since Linux 3.8)
152 .IP *
153 NFS (since Linux 3.18)
154 .\" commit 1c6dcbe5ceff81c2cf8d929646af675cd59fe7c0
155 .\" commit 24bab491220faa446d945624086d838af41d616c
156 .IP *
157 FUSE (since Linux 4.5)
158 .\" commit 0b5da8db145bfd44266ac964a2636a0cf8d7c286
159 .IP *
160 GFS2 (since Linux 4.15)
161 .\" commit 3a27411cb4bc3ce31db228e3569ad01b462a4310
162 .SH RETURN VALUE
163 Upon successful completion,
164 .BR lseek ()
165 returns the resulting offset location as measured in bytes from the
166 beginning of the file.
167 On error, the value \fI(off_t)\ \-1\fP is returned and
168 .I errno
169 is set to indicate the error.
170 .SH ERRORS
171 .TP
172 .B EBADF
173 .I fd
174 is not an open file descriptor.
175 .TP
176 .B EINVAL
177 .I whence
178 is not valid.
179 Or: the resulting file offset would be negative,
180 or beyond the end of a seekable device.
181 .\" Some systems may allow negative offsets for character devices
182 .\" and/or for remote filesystems.
183 .TP
184 .B ENXIO
185 .I whence
186 is
187 .B SEEK_DATA
188 or
189 .BR SEEK_HOLE ,
190 and
191 .I offset
192 is beyond the end of the file, or
193 .I whence
194 is
195 .B SEEK_DATA
196 and
197 .I offset
198 is within a hole at the end of the file.
199 .TP
200 .B EOVERFLOW
201 .\" HP-UX 11 says EINVAL for this case (but POSIX.1 says EOVERFLOW)
202 The resulting file offset cannot be represented in an
203 .IR off_t .
204 .TP
205 .B ESPIPE
206 .I fd
207 is associated with a pipe, socket, or FIFO.
208 .SH CONFORMING TO
209 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
210 .PP
211 .B SEEK_DATA
212 and
213 .B SEEK_HOLE
214 are nonstandard extensions also present in Solaris,
215 FreeBSD, and DragonFly BSD;
216 they are proposed for inclusion in the next POSIX revision (Issue 8).
217 .\" FIXME . Review http://austingroupbugs.net/view.php?id=415 in the future
218 .SH NOTES
219 See
220 .BR open (2)
221 for a discussion of the relationship between file descriptors,
222 open file descriptions, and files.
223 .PP
224 If the
225 .B O_APPEND
226 file status flag is set on the open file description,
227 then a
228 .BR write (2)
229 .I always
230 moves the file offset to the end of the file, regardless of the use of
231 .BR lseek ().
232 .PP
233 The
234 .I off_t
235 data type is a signed integer data type specified by POSIX.1.
236 .PP
237 Some devices are incapable of seeking and POSIX does not specify which
238 devices must support
239 .BR lseek ().
240 .PP
241 On Linux, using
242 .BR lseek ()
243 on a terminal device fails with the error
244 \fBESPIPE\fP.
245 .\" Other systems return the number of written characters,
246 .\" using SEEK_SET to set the counter. (Of written characters.)
247 .SH SEE ALSO
248 .BR dup (2),
249 .BR fallocate (2),
250 .BR fork (2),
251 .BR open (2),
252 .BR fseek (3),
253 .BR lseek64 (3),
254 .BR posix_fallocate (3)