]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/lseek.2
Connect the installation page to the build.
[FreeBSD/FreeBSD.git] / lib / libc / sys / lseek.2
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)lseek.2     8.3 (Berkeley) 4/19/94
29 .\" $FreeBSD$
30 .\"
31 .Dd February 18, 2016
32 .Dt LSEEK 2
33 .Os
34 .Sh NAME
35 .Nm lseek
36 .Nd reposition read/write file offset
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In unistd.h
41 .Ft off_t
42 .Fn lseek "int fildes" "off_t offset" "int whence"
43 .Sh DESCRIPTION
44 The
45 .Fn lseek
46 system call repositions the offset of the file descriptor
47 .Fa fildes
48 to the
49 argument
50 .Fa offset
51 according to the directive
52 .Fa whence .
53 The argument
54 .Fa fildes
55 must be an open
56 file descriptor.
57 The
58 .Fn lseek
59 system call
60 repositions the file position pointer associated with the file
61 descriptor
62 .Fa fildes
63 as follows:
64 .Bl -item -offset indent
65 .It
66 If
67 .Fa whence
68 is
69 .Dv SEEK_SET ,
70 the offset is set to
71 .Fa offset
72 bytes.
73 .It
74 If
75 .Fa whence
76 is
77 .Dv SEEK_CUR ,
78 the offset is set to its current location plus
79 .Fa offset
80 bytes.
81 .It
82 If
83 .Fa whence
84 is
85 .Dv SEEK_END ,
86 the offset is set to the size of the
87 file plus
88 .Fa offset
89 bytes.
90 .It
91 If
92 .Fa whence
93 is
94 .Dv SEEK_HOLE ,
95 the offset is set to the start of the next hole greater than or equal
96 to the supplied
97 .Fa offset .
98 The definition of a hole is provided below.
99 .It
100 If
101 .Fa whence
102 is
103 .Dv SEEK_DATA ,
104 the offset is set to the start of the next non-hole file region greater
105 than or equal to the supplied
106 .Fa offset .
107 .El
108 .Pp
109 The
110 .Fn lseek
111 system call allows the file offset to be set beyond the end
112 of the existing end-of-file of the file.
113 If data is later written
114 at this point, subsequent reads of the data in the gap return
115 bytes of zeros (until data is actually written into the gap).
116 .Pp
117 Some devices are incapable of seeking.
118 The value of the pointer
119 associated with such a device is undefined.
120 .Pp
121 A
122 .Qq hole
123 is defined as a contiguous range of bytes in a file, all having the value of
124 zero, but not all zeros in a file are guaranteed to be represented as holes
125 returned with
126 .Dv SEEK_HOLE .
127 File systems are allowed to expose ranges of zeros with
128 .Dv SEEK_HOLE ,
129 but not required to.
130 Applications can use
131 .Dv SEEK_HOLE
132 to optimise their behavior for ranges of zeros, but must not depend on it to
133 find all such ranges in a file.
134 Each file is presented as having a zero-size virtual hole at the very
135 end of the file.
136 The existence of a hole at the end of every data region allows for easy
137 programming and also provides compatibility to the original implementation
138 in Solaris.
139 It also causes the current file size (i.e., end-of-file offset) to be returned
140 to indicate that there are no more holes past the supplied
141 .Fa offset .
142 Applications should use
143 .Fn fpathconf _PC_MIN_HOLE_SIZE
144 or
145 .Fn pathconf _PC_MIN_HOLE_SIZE
146 to determine if a file system supports
147 .Dv SEEK_HOLE .
148 See
149 .Xr pathconf 2 .
150 .Pp
151 For file systems that do not supply information about holes, the file will be
152 represented as one entire data region.
153 .Sh RETURN VALUES
154 Upon successful completion,
155 .Fn lseek
156 returns the resulting offset location as measured in bytes from the
157 beginning of the file.
158 Otherwise,
159 a value of -1 is returned and
160 .Va errno
161 is set to indicate
162 the error.
163 .Sh ERRORS
164 The
165 .Fn lseek
166 system call
167 will fail and the file position pointer will remain unchanged if:
168 .Bl -tag -width Er
169 .It Bq Er EBADF
170 The
171 .Fa fildes
172 argument
173 is not an open file descriptor.
174 .It Bq Er EINVAL
175 The
176 .Fa whence
177 argument
178 is not a proper value
179 or the resulting file offset would
180 be negative for a non-character special file.
181 .It Bq Er ENXIO
182 For
183 .Dv SEEK_DATA ,
184 there are no more data regions past the supplied offset.
185 Due to existence of the hole at the end of the file, for
186 .Dv SEEK_HOLE
187 this error is only returned when the
188 .Fa offset
189 already points to the end-of-file position.
190 .It Bq Er EOVERFLOW
191 The resulting file offset would be a value which cannot be represented
192 correctly in an object of type
193 .Fa off_t .
194 .It Bq Er ESPIPE
195 The
196 .Fa fildes
197 argument
198 is associated with a pipe, socket, or FIFO.
199 .El
200 .Sh SEE ALSO
201 .Xr dup 2 ,
202 .Xr open 2 ,
203 .Xr pathconf 2
204 .Sh STANDARDS
205 The
206 .Fn lseek
207 system call is expected to conform to
208 .St -p1003.1-90 .
209 .Sh HISTORY
210 The
211 .Fn lseek
212 function appeared in
213 .At v7 .
214 .Sh BUGS
215 This document's use of
216 .Fa whence
217 is incorrect English, but is maintained for historical reasons.