1LSEEK(2)                   Linux Programmer's Manual                  LSEEK(2)
2
3
4

NAME

6       lseek - reposition read/write file offset
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <unistd.h>
11
12       off_t lseek(int fd, off_t offset, int whence);
13

DESCRIPTION

15       lseek()  repositions the file offset of the open file description asso‐
16       ciated with the file descriptor fd to the argument offset according  to
17       the directive whence as follows:
18
19       SEEK_SET
20              The file offset is set to offset bytes.
21
22       SEEK_CUR
23              The  file  offset  is  set  to  its current location plus offset
24              bytes.
25
26       SEEK_END
27              The file offset is set to the  size  of  the  file  plus  offset
28              bytes.
29
30       lseek()  allows  the  file  offset to be set beyond the end of the file
31       (but this does not change the size of the  file).   If  data  is  later
32       written  at  this  point,  subsequent  reads  of the data in the gap (a
33       "hole") return null bytes ('\0') until data is  actually  written  into
34       the gap.
35
36   Seeking file data and holes
37       Since  version  3.1, Linux supports the following additional values for
38       whence:
39
40       SEEK_DATA
41              Adjust the file offset to the next location in the file  greater
42              than  or  equal  to offset containing data.  If offset points to
43              data, then the file offset is set to offset.
44
45       SEEK_HOLE
46              Adjust the file offset to the next hole in the file greater than
47              or equal to offset.  If offset points into the middle of a hole,
48              then the file offset is set to offset.  If there is no hole past
49              offset,  then the file offset is adjusted to the end of the file
50              (i.e., there is an implicit hole at the end of any file).
51
52       In both of the above cases, lseek() fails if offset points past the end
53       of the file.
54
55       These  operations  allow  applications to map holes in a sparsely allo‐
56       cated file.  This can be useful for applications such  as  file  backup
57       tools,  which  can save space when creating backups and preserve holes,
58       if they have a mechanism for discovering holes.
59
60       For the purposes of these operations, a hole is  a  sequence  of  zeros
61       that  (normally) has not been allocated in the underlying file storage.
62       However, a filesystem is not obliged to report holes, so  these  opera‐
63       tions  are not a guaranteed mechanism for mapping the storage space ac‐
64       tually allocated to a file.  (Furthermore, a sequence of zeros that ac‐
65       tually  has  been written to the underlying storage may not be reported
66       as a hole.)  In the simplest implementation, a filesystem  can  support
67       the  operations by making SEEK_HOLE always return the offset of the end
68       of the file, and making SEEK_DATA always return offset (i.e.,  even  if
69       the  location  referred to by offset is a hole, it can be considered to
70       consist of data that is a sequence of zeros).
71
72       The _GNU_SOURCE feature test macro must be defined in order  to  obtain
73       the definitions of SEEK_DATA and SEEK_HOLE from <unistd.h>.
74
75       The  SEEK_HOLE and SEEK_DATA operations are supported for the following
76       filesystems:
77
78       *  Btrfs (since Linux 3.1)
79
80       *  OCFS (since Linux 3.2)
81
82       *  XFS (since Linux 3.5)
83
84       *  ext4 (since Linux 3.8)
85
86       *  tmpfs(5) (since Linux 3.8)
87
88       *  NFS (since Linux 3.18)
89
90       *  FUSE (since Linux 4.5)
91
92       *  GFS2 (since Linux 4.15)
93

RETURN VALUE

95       Upon successful completion, lseek() returns the resulting offset  loca‐
96       tion  as  measured  in bytes from the beginning of the file.  On error,
97       the value (off_t) -1 is returned and errno is set to indicate  the  er‐
98       ror.
99

ERRORS

101       EBADF  fd is not an open file descriptor.
102
103       EINVAL whence  is  not  valid.   Or: the resulting file offset would be
104              negative, or beyond the end of a seekable device.
105
106       ENXIO  whence is SEEK_DATA or SEEK_HOLE, and offset is beyond  the  end
107              of  the file, or whence is SEEK_DATA and offset is within a hole
108              at the end of the file.
109
110       EOVERFLOW
111              The resulting file offset cannot be represented in an off_t.
112
113       ESPIPE fd is associated with a pipe, socket, or FIFO.
114

CONFORMING TO

116       POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
117
118       SEEK_DATA and SEEK_HOLE are nonstandard extensions also present in  So‐
119       laris,  FreeBSD,  and DragonFly BSD; they are proposed for inclusion in
120       the next POSIX revision (Issue 8).
121

NOTES

123       See open(2) for a discussion of the relationship between file  descrip‐
124       tors, open file descriptions, and files.
125
126       If  the  O_APPEND file status flag is set on the open file description,
127       then a write(2) always moves the file offset to the end  of  the  file,
128       regardless of the use of lseek().
129
130       The off_t data type is a signed integer data type specified by POSIX.1.
131
132       Some  devices are incapable of seeking and POSIX does not specify which
133       devices must support lseek().
134
135       On Linux, using lseek() on a terminal device fails with the  error  ES‐
136       PIPE.
137

SEE ALSO

139       dup(2),   fallocate(2),   fork(2),   open(2),   fseek(3),   lseek64(3),
140       posix_fallocate(3)
141

COLOPHON

143       This page is part of release 5.10 of the Linux  man-pages  project.   A
144       description  of  the project, information about reporting bugs, and the
145       latest    version    of    this    page,    can     be     found     at
146       https://www.kernel.org/doc/man-pages/.
147
148
149
150Linux                             2020-08-13                          LSEEK(2)
Impressum