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