OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / 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 2014-06-13 "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 .sp
55 .BI "off_t lseek(int " fd ", off_t " offset ", int " whence );
56 .SH DESCRIPTION
57 The
58 .BR lseek ()
59 function repositions the offset of the open file associated with the
60 file descriptor
61 .I fd
62 to the argument
63 .I offset
64 according to the directive
65 .I whence
66 as follows:
67 .TP
68 .B SEEK_SET
69 The offset is set to
70 .I offset
71 bytes.
72 .TP
73 .B SEEK_CUR
74 The offset is set to its current location plus
75 .I offset
76 bytes.
77 .TP
78 .B SEEK_END
79 The offset is set to the size of the file plus
80 .I offset
81 bytes.
82 .PP
83 The
84 .BR lseek ()
85 function allows the file offset to be set beyond the end
86 of the file (but this does not change the size of the file).
87 If data is later written at this point, subsequent reads of the data
88 in the gap (a "hole") return null bytes (\(aq\\0\(aq) until
89 data is actually written into the gap.
90 .SS Seeking file data and holes
91 Since version 3.1, Linux supports the following additional values for
92 .IR whence :
93 .TP
94 .B SEEK_DATA
95 Adjust the file offset to the next location
96 in the file greater than or equal to
97 .I offset
98 containing data.
99 If
100 .I offset
101 points to data,
102 then the file offset is set to
103 .IR offset .
104 .TP
105 .B SEEK_HOLE
106 Adjust the file offset to the next hole in the file
107 greater than or equal to
108 .IR offset .
109 If
110 .I offset
111 points into the middle of a hole,
112 then the file offset is set to
113 .IR offset .
114 If there is no hole past
115 .IR offset ,
116 then the file offset is adjusted to the end of the file
117 (i.e., there is an implicit hole at the end of any file).
118 .PP
119 In both of the above cases,
120 .BR lseek ()
121 fails if
122 .I offset
123 points past the end of the file.
124
125 These operations allow applications to map holes in a sparsely
126 allocated file.
127 This can be useful for applications such as file backup tools,
128 which can save space when creating backups and preserve holes,
129 if they have a mechanism for discovering holes.
130
131 For the purposes of these operations, a hole is a sequence of zeros that
132 (normally) has not been allocated in the underlying file storage.
133 However, a filesystem is not obliged to report holes,
134 so these operations are not a guaranteed mechanism for
135 mapping the storage space actually allocated to a file.
136 (Furthermore, a sequence of zeros that actually has been written
137 to the underlying storage may not be reported as a hole.)
138 In the simplest implementation,
139 a filesystem can support the operations by making
140 .BR SEEK_HOLE
141 always return the offset of the end of the file,
142 and making
143 .BR SEEK_DATA
144 always return
145 .IR offset
146 (i.e., even if the location referred to by
147 .I offset
148 is a hole,
149 it can be considered to consist of data that is a sequence of zeros).
150 .\" https://lkml.org/lkml/2011/4/22/79
151 .\" http://lwn.net/Articles/440255/
152 .\" http://blogs.oracle.com/bonwick/entry/seek_hole_and_seek_data
153
154 The
155 .BR _GNU_SOURCE
156 feature test macro must be defined in order to obtain the definitions of
157 .BR SEEK_DATA
158 and
159 .BR SEEK_HOLE
160 from
161 .IR <unistd.h> .
162
163 The
164 .BR SEEK_HOLE
165 and
166 .BR SEEK_DATA
167 operations are supported for the following filesystems:
168 .IP * 3
169 Btrfs (since Linux 3.1)
170 .IP * 3
171 OCFS (since Linux 3.2)
172 .\" commit 93862d5e1ab875664c6cc95254fc365028a48bb1
173 .IP *
174 XFS (since Linux 3.5)
175 .IP *
176 ext4 (since Linux 3.8)
177 .IP *
178 tmpfs (since Linux 3.8)
179 .SH RETURN VALUE
180 Upon successful completion,
181 .BR lseek ()
182 returns the resulting offset location as measured in bytes from the
183 beginning of the file.
184 On error, the value \fI(off_t)\ \-1\fP is returned and
185 .I errno
186 is set to indicate the error.
187 .SH ERRORS
188 .TP
189 .B EBADF
190 .I fd
191 is not an open file descriptor.
192 .TP
193 .B EINVAL
194 .I whence
195 is not valid.
196 Or: the resulting file offset would be negative,
197 or beyond the end of a seekable device.
198 .\" Some systems may allow negative offsets for character devices
199 .\" and/or for remote filesystems.
200 .TP
201 .B EOVERFLOW
202 .\" HP-UX 11 says EINVAL for this case (but POSIX.1 says EOVERFLOW)
203 The resulting file offset cannot be represented in an
204 .IR off_t .
205 .TP
206 .B ESPIPE
207 .I fd
208 is associated with a pipe, socket, or FIFO.
209 .TP
210 .B ENXIO
211 .I whence
212 is
213 .B SEEK_DATA
214 or
215 .BR SEEK_HOLE ,
216 and the current file offset is beyond the end of the file.
217 .SH CONFORMING TO
218 SVr4, 4.3BSD, POSIX.1-2001.
219
220 .BR SEEK_DATA
221 and
222 .BR SEEK_HOLE
223 are nonstandard extensions also present in Solaris,
224 FreeBSD, and DragonFly BSD;
225 they are proposed for inclusion in the next POSIX revision (Issue 8).
226 .\" FIXME . Review http://austingroupbugs.net/view.php?id=415 in the future
227 .SH NOTES
228 See
229 .BR open (2)
230 for a discussion of the relationship between file descriptors,
231 open file descriptions, and files.
232
233 Some devices are incapable of seeking and POSIX does not specify which
234 devices must support
235 .BR lseek ().
236
237 On Linux, using
238 .BR lseek ()
239 on a terminal device returns
240 \fBESPIPE\fP.
241 .\" Other systems return the number of written characters,
242 .\" using SEEK_SET to set the counter. (Of written characters.)
243
244 When converting old code, substitute values for \fIwhence\fP with the
245 following macros:
246 .TS
247 c c
248 l l.
249 old     new
250 0       SEEK_SET
251 1       SEEK_CUR
252 2       SEEK_END
253 L_SET   SEEK_SET
254 L_INCR  SEEK_CUR
255 L_XTND  SEEK_END
256 .TE
257 .\" .PP
258 .\" SVr1-3 returns \fIlong\fP instead of \fIoff_t\fP,
259 .\" (ancient) BSD returns \fIint\fP.
260 .PP
261 Note that file descriptors created by
262 .BR dup (2)
263 or
264 .BR fork (2)
265 share the current file position pointer, so seeking on such files may be
266 subject to race conditions.
267 .SH SEE ALSO
268 .BR dup (2),
269 .BR fork (2),
270 .BR open (2),
271 .BR fseek (3),
272 .BR lseek64 (3),
273 .BR posix_fallocate (3)
274 .SH COLOPHON
275 This page is part of release 3.79 of the Linux
276 .I man-pages
277 project.
278 A description of the project,
279 information about reporting bugs,
280 and the latest version of this page,
281 can be found at
282 \%http://www.kernel.org/doc/man\-pages/.