scripts/bash_aliases: tfix
[man-pages.git] / man2 / lseek.2
blob1cab710dfd01a9bb8acbe70dddf3275b67e8909a
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 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"     This product includes software developed by the University of
17 .\"     California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\"    may be used to endorse or promote products derived from this software
20 .\"    without specific prior written permission.
21 .\"
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\" %%%LICENSE_END
34 .\"
35 .\"     @(#)lseek.2     6.5 (Berkeley) 3/10/91
36 .\"
37 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
38 .\" Modified 1995-06-10 by Andries Brouwer <aeb@cwi.nl>
39 .\" Modified 1996-10-31 by Eric S. Raymond <esr@thyrsus.com>
40 .\" Modified 1998-01-17 by Michael Haardt
41 .\"   <michael@cantor.informatik.rwth-aachen.de>
42 .\" Modified 2001-09-24 by Michael Haardt <michael@moria.de>
43 .\" Modified 2003-08-21 by Andries Brouwer <aeb@cwi.nl>
44 .\" 2011-09-18, mtk, Added SEEK_DATA + SEEK_HOLE
45 .\"
46 .TH LSEEK 2 2021-03-22 "Linux" "Linux Programmer's Manual"
47 .SH NAME
48 lseek \- reposition read/write file offset
49 .SH SYNOPSIS
50 .nf
51 .B #include <unistd.h>
52 .PP
53 .BI "off_t lseek(int " fd ", off_t " offset ", int " whence );
54 .fi
55 .SH DESCRIPTION
56 .BR lseek ()
57 repositions the file offset of the open file description
58 associated with the file descriptor
59 .I fd
60 to the argument
61 .I offset
62 according to the directive
63 .I whence
64 as follows:
65 .TP
66 .B SEEK_SET
67 The file offset is set to
68 .I offset
69 bytes.
70 .TP
71 .B SEEK_CUR
72 The file offset is set to its current location plus
73 .I offset
74 bytes.
75 .TP
76 .B SEEK_END
77 The file offset is set to the size of the file plus
78 .I offset
79 bytes.
80 .PP
81 .BR lseek ()
82 allows the file offset to be set beyond the end
83 of the file (but this does not change the size of the file).
84 If data is later written at this point, subsequent reads of the data
85 in the gap (a "hole") return null bytes (\(aq\e0\(aq) until
86 data is actually written into the gap.
87 .SS Seeking file data and holes
88 Since version 3.1, Linux supports the following additional values for
89 .IR whence :
90 .TP
91 .B SEEK_DATA
92 Adjust the file offset to the next location
93 in the file greater than or equal to
94 .I offset
95 containing data.
97 .I offset
98 points to data,
99 then the file offset is set to
100 .IR offset .
102 .B SEEK_HOLE
103 Adjust the file offset to the next hole in the file
104 greater than or equal to
105 .IR offset .
107 .I offset
108 points into the middle of a hole,
109 then the file offset is set to
110 .IR offset .
111 If there is no hole past
112 .IR offset ,
113 then the file offset is adjusted to the end of the file
114 (i.e., there is an implicit hole at the end of any file).
116 In both of the above cases,
117 .BR lseek ()
118 fails if
119 .I offset
120 points past the end of the file.
122 These operations allow applications to map holes in a sparsely
123 allocated file.
124 This can be useful for applications such as file backup tools,
125 which can save space when creating backups and preserve holes,
126 if they have a mechanism for discovering holes.
128 For the purposes of these operations, a hole is a sequence of zeros that
129 (normally) has not been allocated in the underlying file storage.
130 However, a filesystem is not obliged to report holes,
131 so these operations are not a guaranteed mechanism for
132 mapping the storage space actually allocated to a file.
133 (Furthermore, a sequence of zeros that actually has been written
134 to the underlying storage may not be reported as a hole.)
135 In the simplest implementation,
136 a filesystem can support the operations by making
137 .BR SEEK_HOLE
138 always return the offset of the end of the file,
139 and making
140 .BR SEEK_DATA
141 always return
142 .IR offset
143 (i.e., even if the location referred to by
144 .I offset
145 is a hole,
146 it can be considered to consist of data that is a sequence of zeros).
147 .\" https://lkml.org/lkml/2011/4/22/79
148 .\" http://lwn.net/Articles/440255/
149 .\" http://blogs.oracle.com/bonwick/entry/seek_hole_and_seek_data
152 .BR _GNU_SOURCE
153 feature test macro must be defined in order to obtain the definitions of
154 .BR SEEK_DATA
156 .BR SEEK_HOLE
157 from
158 .IR <unistd.h> .
161 .BR SEEK_HOLE
163 .BR SEEK_DATA
164 operations are supported for the following filesystems:
165 .IP * 3
166 Btrfs (since Linux 3.1)
167 .IP * 3
168 OCFS (since Linux 3.2)
169 .\" commit 93862d5e1ab875664c6cc95254fc365028a48bb1
170 .IP *
171 XFS (since Linux 3.5)
172 .IP *
173 ext4 (since Linux 3.8)
174 .IP *
175 .BR tmpfs (5)
176 (since Linux 3.8)
177 .IP *
178 NFS (since Linux 3.18)
179 .\" commit 1c6dcbe5ceff81c2cf8d929646af675cd59fe7c0
180 .\" commit 24bab491220faa446d945624086d838af41d616c
181 .IP *
182 FUSE (since Linux 4.5)
183 .\" commit 0b5da8db145bfd44266ac964a2636a0cf8d7c286
184 .IP *
185 GFS2 (since Linux 4.15)
186 .\" commit 3a27411cb4bc3ce31db228e3569ad01b462a4310
187 .SH RETURN VALUE
188 Upon successful completion,
189 .BR lseek ()
190 returns the resulting offset location as measured in bytes from the
191 beginning of the file.
192 On error, the value \fI(off_t)\ \-1\fP is returned and
193 .I errno
194 is set to indicate the error.
195 .SH ERRORS
197 .B EBADF
198 .I fd
199 is not an open file descriptor.
201 .B EINVAL
202 .I whence
203 is not valid.
204 Or: the resulting file offset would be negative,
205 or beyond the end of a seekable device.
206 .\" Some systems may allow negative offsets for character devices
207 .\" and/or for remote filesystems.
209 .B ENXIO
210 .I whence
212 .B SEEK_DATA
214 .BR SEEK_HOLE ,
216 .I offset
217 is beyond the end of the file, or
218 .I whence
220 .B SEEK_DATA
222 .I offset
223 is within a hole at the end of the file.
225 .B EOVERFLOW
226 .\" HP-UX 11 says EINVAL for this case (but POSIX.1 says EOVERFLOW)
227 The resulting file offset cannot be represented in an
228 .IR off_t .
230 .B ESPIPE
231 .I fd
232 is associated with a pipe, socket, or FIFO.
233 .SH CONFORMING TO
234 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
236 .BR SEEK_DATA
238 .BR SEEK_HOLE
239 are nonstandard extensions also present in Solaris,
240 FreeBSD, and DragonFly BSD;
241 they are proposed for inclusion in the next POSIX revision (Issue 8).
242 .\" FIXME . Review http://austingroupbugs.net/view.php?id=415 in the future
243 .SH NOTES
245 .BR open (2)
246 for a discussion of the relationship between file descriptors,
247 open file descriptions, and files.
249 If the
250 .B O_APPEND
251 file status flag is set on the open file description,
252 then a
253 .BR write (2)
254 .I always
255 moves the file offset to the end of the file, regardless of the use of
256 .BR lseek ().
259 .I off_t
260 data type is a signed integer data type specified by POSIX.1.
262 Some devices are incapable of seeking and POSIX does not specify which
263 devices must support
264 .BR lseek ().
266 On Linux, using
267 .BR lseek ()
268 on a terminal device fails with the error
269 \fBESPIPE\fP.
270 .\" Other systems return the number of written characters,
271 .\" using SEEK_SET to set the counter. (Of written characters.)
272 .SH SEE ALSO
273 .BR dup (2),
274 .BR fallocate (2),
275 .BR fork (2),
276 .BR open (2),
277 .BR fseek (3),
278 .BR lseek64 (3),
279 .BR posix_fallocate (3)