1LSEEK(2) Linux Programmer's Manual LSEEK(2)
2
3
4
6 lseek - reposition read/write file offset
7
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 off_t lseek(int fd, off_t offset, int whence);
13
15 The lseek() function repositions the offset of the open file associated
16 with the file descriptor fd to the argument offset according to the
17 directive whence as follows:
18
19 SEEK_SET
20 The offset is set to offset bytes.
21
22 SEEK_CUR
23 The offset is set to its current location plus offset bytes.
24
25 SEEK_END
26 The offset is set to the size of the file plus offset bytes.
27
28 The lseek() function allows the file offset to be set beyond the end of
29 the file (but this does not change the size of the file). If data is
30 later written at this point, subsequent reads of the data in the gap (a
31 "hole") return null bytes ('\0') until data is actually written into
32 the gap.
33
34 Seeking file data and holes
35 Since version 3.1, Linux supports the following additional values for
36 whence:
37
38 SEEK_DATA
39 Adjust the file offset to the next location in the file greater
40 than or equal to offset containing data. If offset points to
41 data, then the file offset is set to offset.
42
43 SEEK_HOLE
44 Adjust the file offset to the next hole in the file greater than
45 or equal to offset. If offset points into the middle of a hole,
46 then the file offset is set to offset. If there is no hole past
47 offset, then the file offset is adjusted to the end of the file
48 (i.e., there is an implicit hole at the end of any file).
49
50 In both of the above cases, lseek() fails if offset points past the end
51 of the file.
52
53 These operations allow applications to map holes in a sparsely allo‐
54 cated file. This can be useful for applications such as file backup
55 tools, which can save space when creating backups and preserve holes,
56 if they have a mechanism for discovering holes.
57
58 For the purposes of these operations, a hole is a sequence of zeros
59 that (normally) has not been allocated in the underlying file storage.
60 However, a file system is not obliged to report holes, so these opera‐
61 tions are not a guaranteed mechanism for mapping the storage space
62 actually allocated to a file. (Furthermore, a sequence of zeros that
63 actually has been written to the underlying storage may not be reported
64 as a hole.) In the simplest implementation, a file system can support
65 the operations by making SEEK_HOLE always return the offset of the end
66 of the file, and making SEEK_DATA always return offset (i.e., even if
67 the location referred to by offset is a hole, it can be considered to
68 consist of data that is a sequence of zeros).
69
70 The _GNU_SOURCE feature test macro must be defined in order to obtain
71 the definitions of SEEK_DATA and SEEK_HOLE from <unistd.h>.
72
74 Upon successful completion, lseek() returns the resulting offset loca‐
75 tion as measured in bytes from the beginning of the file. On error,
76 the value (off_t) -1 is returned and errno is set to indicate the
77 error.
78
80 EBADF fd is not an open file descriptor.
81
82 EINVAL whence is not valid. Or: the resulting file offset would be
83 negative, or beyond the end of a seekable device.
84
85 EOVERFLOW
86 The resulting file offset cannot be represented in an off_t.
87
88 ESPIPE fd is associated with a pipe, socket, or FIFO.
89
90 ENXIO whence is SEEK_DATA or SEEK_HOLE, and the current file offset is
91 beyond the end of the file.
92
94 SVr4, 4.3BSD, POSIX.1-2001.
95
96 SEEK_DATA and SEEK_HOLE are nonstandard extensions also present in
97 Solaris, FreeBSD, and DragonFly BSD; they are proposed for inclusion in
98 the next POSIX revision (Issue 8).
99
101 Some devices are incapable of seeking and POSIX does not specify which
102 devices must support lseek().
103
104 On Linux, using lseek() on a terminal device returns ESPIPE.
105
106 When converting old code, substitute values for whence with the follow‐
107 ing macros:
108
109 old new
110 0 SEEK_SET
111 1 SEEK_CUR
112 2 SEEK_END
113 L_SET SEEK_SET
114 L_INCR SEEK_CUR
115 L_XTND SEEK_END
116
117 Note that file descriptors created by dup(2) or fork(2) share the cur‐
118 rent file position pointer, so seeking on such files may be subject to
119 race conditions.
120
122 dup(2), fork(2), open(2), fseek(3), lseek64(3), posix_fallocate(3)
123
125 This page is part of release 3.53 of the Linux man-pages project. A
126 description of the project, and information about reporting bugs, can
127 be found at http://www.kernel.org/doc/man-pages/.
128
129
130
131Linux 2013-03-27 LSEEK(2)