OSDN Git Service

(split) LDP_man-pages: update original to v3.34.
[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 .\" 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 .\"
34 .\"     @(#)lseek.2     6.5 (Berkeley) 3/10/91
35 .\"
36 .\" Modified 1993-07-23 by Rik Faith <faith@cs.unc.edu>
37 .\" Modified 1995-06-10 by Andries Brouwer <aeb@cwi.nl>
38 .\" Modified 1996-10-31 by Eric S. Raymond <esr@thyrsus.com>
39 .\" Modified 1998-01-17 by Michael Haardt
40 .\"   <michael@cantor.informatik.rwth-aachen.de>
41 .\" Modified 2001-09-24 by Michael Haardt <michael@moria.de>
42 .\" Modified 2003-08-21 by Andries Brouwer <aeb@cwi.nl>
43 .\" 2011-09-18, mtk, Added SEEK_DATA + SEEK_HOLE
44 .\"
45 .TH LSEEK 2 2011-09-20 "Linux" "Linux Programmer's Manual"
46 .SH NAME
47 lseek \- reposition read/write file offset
48 .SH SYNOPSIS
49 .B #include <sys/types.h>
50 .br
51 .B #include <unistd.h>
52 .sp
53 .BI "off_t lseek(int " fd ", off_t " offset ", int " whence );
54 .SH DESCRIPTION
55 The
56 .BR lseek ()
57 function repositions the offset of the open file associated with the
58 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 offset is set to
68 .I offset
69 bytes.
70 .TP
71 .B SEEK_CUR
72 The offset is set to its current location plus
73 .I offset
74 bytes.
75 .TP
76 .B SEEK_END
77 The offset is set to the size of the file plus
78 .I offset
79 bytes.
80 .PP
81 The
82 .BR lseek ()
83 function 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
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
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 file system 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 file system 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 return
144 .IR offset
145 (i.e., even if the location referred to by
146 .I offset
147 is a hole,
148 it can be considered to consist of data that is a sequence of zeros).
149 .\" https://lkml.org/lkml/2011/4/22/79
150 .\" http://lwn.net/Articles/440255/
151 .\" http://blogs.oracle.com/bonwick/entry/seek_hole_and_seek_data
152 .SH "RETURN VALUE"
153 Upon successful completion,
154 .BR lseek ()
155 returns the resulting offset location as measured in bytes from the
156 beginning of the file.
157 On error, the value \fI(off_t)\ \-1\fP is returned and
158 .I errno
159 is set to indicate the error.
160 .SH ERRORS
161 .TP
162 .B EBADF
163 .I fd
164 is not an open file descriptor.
165 .TP
166 .B EINVAL
167 .I whence
168 is not valid.
169 Or: the resulting file offset would be negative,
170 or beyond the end of a seekable device.
171 .\" Some systems may allow negative offsets for character devices
172 .\" and/or for remote file systems.
173 .TP
174 .B EOVERFLOW
175 .\" HP-UX 11 says EINVAL for this case (but POSIX.1 says EOVERFLOW)
176 The resulting file offset cannot be represented in an
177 .IR off_t .
178 .TP
179 .B ESPIPE
180 .I fd
181 is associated with a pipe, socket, or FIFO.
182 .TP
183 .B ENXIO
184 .I whence
185 is
186 .B SEEK_DATA
187 or
188 .BR SEEK_HOLE ,
189 and the current file offset is beyond the end of the file.
190 .SH "CONFORMING TO"
191 SVr4, 4.3BSD, POSIX.1-2001.
192
193 .BR SEEK_DATA
194 and
195 .BR SEEK_HOLE
196 are nonstandard extensions also present in Solaris;
197 they are proposed for inclusion in the next POSIX revision (Issue 8).
198 .\" FIXME . Review http://austingroupbugs.net/view.php?id=415 in the future
199 .SH NOTES
200 Some devices are incapable of seeking and POSIX does not specify which
201 devices must support
202 .BR lseek ().
203
204 On Linux, using
205 .BR lseek ()
206 on a tty device returns
207 \fBESPIPE\fP.
208 .\" Other systems return the number of written characters,
209 .\" using SEEK_SET to set the counter. (Of written characters.)
210
211 When converting old code, substitute values for \fIwhence\fP with the
212 following macros:
213 .TS
214 c c
215 l l.
216 old     new
217 0       SEEK_SET
218 1       SEEK_CUR
219 2       SEEK_END
220 L_SET   SEEK_SET
221 L_INCR  SEEK_CUR
222 L_XTND  SEEK_END
223 .TE
224 .\" .PP
225 .\" SVr1-3 returns \fIlong\fP instead of \fIoff_t\fP,
226 .\" (ancient) BSD returns \fIint\fP.
227 .PP
228 Note that file descriptors created by
229 .BR dup (2)
230 or
231 .BR fork (2)
232 share the current file position pointer, so seeking on such files may be
233 subject to race conditions.
234 .SH "SEE ALSO"
235 .BR dup (2),
236 .BR fork (2),
237 .BR open (2),
238 .BR fseek (3),
239 .BR lseek64 (3),
240 .BR posix_fallocate (3)