1lseek(2) System Calls Manual lseek(2)
2
3
4
6 lseek - reposition read/write file offset
7
9 Standard C library (libc, -lc)
10
12 #include <unistd.h>
13
14 off_t lseek(int fd, off_t offset, int whence);
15
17 lseek() repositions the file offset of the open file description asso‐
18 ciated with the file descriptor fd to the argument offset according to
19 the directive whence as follows:
20
21 SEEK_SET
22 The file offset is set to offset bytes.
23
24 SEEK_CUR
25 The file offset is set to its current location plus offset
26 bytes.
27
28 SEEK_END
29 The file offset is set to the size of the file plus offset
30 bytes.
31
32 lseek() allows the file offset to be set beyond the end of the file
33 (but this does not change the size of the file). If data is later
34 written at this point, subsequent reads of the data in the gap (a
35 "hole") return null bytes ('\0') until data is actually written into
36 the gap.
37
38 Seeking file data and holes
39 Since Linux 3.1, Linux supports the following additional values for
40 whence:
41
42 SEEK_DATA
43 Adjust the file offset to the next location in the file greater
44 than or equal to offset containing data. If offset points to
45 data, then the file offset is set to offset.
46
47 SEEK_HOLE
48 Adjust the file offset to the next hole in the file greater than
49 or equal to offset. If offset points into the middle of a hole,
50 then the file offset is set to offset. If there is no hole past
51 offset, then the file offset is adjusted to the end of the file
52 (i.e., there is an implicit hole at the end of any file).
53
54 In both of the above cases, lseek() fails if offset points past the end
55 of the file.
56
57 These operations allow applications to map holes in a sparsely allo‐
58 cated file. This can be useful for applications such as file backup
59 tools, which can save space when creating backups and preserve holes,
60 if they have a mechanism for discovering holes.
61
62 For the purposes of these operations, a hole is a sequence of zeros
63 that (normally) has not been allocated in the underlying file storage.
64 However, a filesystem is not obliged to report holes, so these opera‐
65 tions are not a guaranteed mechanism for mapping the storage space ac‐
66 tually allocated to a file. (Furthermore, a sequence of zeros that ac‐
67 tually has been written to the underlying storage may not be reported
68 as a hole.) In the simplest implementation, a filesystem can support
69 the operations by making SEEK_HOLE always return the offset of the end
70 of the file, and making SEEK_DATA always return offset (i.e., even if
71 the location referred to by offset is a hole, it can be considered to
72 consist of data that is a sequence of zeros).
73
74 The _GNU_SOURCE feature test macro must be defined in order to obtain
75 the definitions of SEEK_DATA and SEEK_HOLE from <unistd.h>.
76
77 The SEEK_HOLE and SEEK_DATA operations are supported for the following
78 filesystems:
79
80 • Btrfs (since Linux 3.1)
81
82 • OCFS (since Linux 3.2)
83
84 • XFS (since Linux 3.5)
85
86 • ext4 (since Linux 3.8)
87
88 • tmpfs(5) (since Linux 3.8)
89
90 • NFS (since Linux 3.18)
91
92 • FUSE (since Linux 4.5)
93
94 • GFS2 (since Linux 4.15)
95
97 Upon successful completion, lseek() returns the resulting offset loca‐
98 tion as measured in bytes from the beginning of the file. On error,
99 the value (off_t) -1 is returned and errno is set to indicate the er‐
100 ror.
101
103 EBADF fd is not an open file descriptor.
104
105 EINVAL whence is not valid. Or: the resulting file offset would be
106 negative, or beyond the end of a seekable device.
107
108 ENXIO whence is SEEK_DATA or SEEK_HOLE, and offset is beyond the end
109 of the file, or whence is SEEK_DATA and offset is within a hole
110 at the end of the file.
111
112 EOVERFLOW
113 The resulting file offset cannot be represented in an off_t.
114
115 ESPIPE fd is associated with a pipe, socket, or FIFO.
116
118 On Linux, using lseek() on a terminal device fails with the error ES‐
119 PIPE.
120
122 POSIX.1-2008.
123
125 POSIX.1-2001, SVr4, 4.3BSD.
126
127 SEEK_DATA and SEEK_HOLE are nonstandard extensions also present in So‐
128 laris, FreeBSD, and DragonFly BSD; they are proposed for inclusion in
129 the next POSIX revision (Issue 8).
130
132 See open(2) for a discussion of the relationship between file descrip‐
133 tors, open file descriptions, and files.
134
135 If the O_APPEND file status flag is set on the open file description,
136 then a write(2) always moves the file offset to the end of the file,
137 regardless of the use of lseek().
138
139 Some devices are incapable of seeking and POSIX does not specify which
140 devices must support lseek().
141
143 dup(2), fallocate(2), fork(2), open(2), fseek(3), lseek64(3),
144 posix_fallocate(3)
145
146
147
148Linux man-pages 6.05 2023-03-30 lseek(2)