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
35 Upon successful completion, lseek() returns the resulting offset loca‐
36 tion as measured in bytes from the beginning of the file. Otherwise, a
37 value of (off_t) -1 is returned and errno is set to indicate the error.
38
40 EBADF fd is not an open file descriptor.
41
42 EINVAL whence is not one of SEEK_SET, SEEK_CUR, SEEK_END; or the
43 resulting file offset would be negative, or beyond the end of a
44 seekable device.
45
46 EOVERFLOW
47 The resulting file offset cannot be represented in an off_t.
48
49 ESPIPE fd is associated with a pipe, socket, or FIFO.
50
52 SVr4, 4.3BSD, POSIX.1-2001.
53
55 This document's use of whence is incorrect English, but maintained for
56 historical reasons.
57
58 Some devices are incapable of seeking and POSIX does not specify which
59 devices must support lseek().
60
61 On Linux, using lseek() on a tty device returns ESPIPE.
62
63 When converting old code, substitute values for whence with the follow‐
64 ing macros:
65
66
67 old new
68 0 SEEK_SET
69 1 SEEK_CUR
70 2 SEEK_END
71 L_SET SEEK_SET
72 L_INCR SEEK_CUR
73 L_XTND SEEK_END
74
75 SVr1-3 returns long instead of off_t, BSD returns int.
76
77 Note that file descriptors created by dup(2) or fork(2) share the cur‐
78 rent file position pointer, so seeking on such files may be subject to
79 race conditions.
80
82 dup(2), fork(2), open(2), fseek(3), lseek64(3), posix_fallocate(3)
83
85 This page is part of release 3.22 of the Linux man-pages project. A
86 description of the project, and information about reporting bugs, can
87 be found at http://www.kernel.org/doc/man-pages/.
88
89
90
91Linux 2001-09-24 LSEEK(2)