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 defin