1LSEEK(3P) POSIX Programmer's Manual LSEEK(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 lseek — move the read/write file offset
14
16 #include <unistd.h>
17
18 off_t lseek(int fildes, off_t offset, int whence);
19
21 The lseek() function shall set the file offset for the open file
22 description associated with the file descriptor fildes, as follows:
23
24 * If whence is SEEK_SET, the file offset shall be set to offset
25 bytes.
26
27 * If whence is SEEK_CUR, the file offset shall be set to its current
28 location plus offset.
29
30 * If whence is SEEK_END, the file offset shall be set to the size of
31 the file plus offset.
32
33 The symbolic constants SEEK_SET, SEEK_CUR, and SEEK_END are defined in
34 <unistd.h>.
35
36 The behavior of lseek() on devices which are incapable of seeking is
37 implementation-defined. The value of the file offset associated with
38 such a device is undefined.
39
40 The lseek() function shall allow the file offset to be set beyond the
41 end of the existing data in the file. If data is later written at this
42 point, subsequent reads of data in the gap shall return bytes with the
43 value 0 until data is actually written into the gap.
44
45 The lseek() function shall not, by itself, extend the size of a file.
46
47 If fildes refers to a shared memory object, the result of the lseek()
48 function is unspecified.
49
50 If fildes refers to a typed memory object, the result of the lseek()
51 function is unspecified.
52
54 Upon successful completion, the resulting offset, as measured in bytes
55 from the beginning of the file, shall be returned. Otherwise, −1 shall
56 be returned, errno shall be set to indicate the error, and the file
57 offset shall remain unchanged.
58
60 The lseek() function shall fail if:
61
62 EBADF The fildes argument is not an open file descriptor.
63
64 EINVAL The whence argument is not a proper value, or the resulting file
65 offset would be negative for a regular file, block special file,
66 or directory.
67
68 EOVERFLOW
69 The resulting file offset would be a value which cannot be rep‐
70 resented correctly in an object of type off_t.
71
72 ESPIPE The fildes argument is associated with a pipe, FIFO, or socket.
73
74 The following sections are informative.
75
77 None.
78
80 None.
81
83 The ISO C standard includes the functions fgetpos() and fsetpos(),
84 which work on very large files by use of a special positioning type.
85
86 Although lseek() may position the file offset beyond the end of the
87 file, this function does not itself extend the size of the file. While
88 the only function in POSIX.1‐2008 that may directly extend the size of
89 the file is write(), truncate(), and ftruncate(), several functions
90 originally derived from the ISO C standard, such as fwrite(),
91 fprintf(), and so on, may do so (by causing calls on write()).
92
93 An invalid file offset that would cause [EINVAL] to be returned may be
94 both implementation-defined and device-dependent (for example, memory
95 may have few invalid values). A negative file offset may be valid for
96 some devices in some implementations.
97
98 The POSIX.1‐1990 standard did not specifically prohibit lseek() from
99 returning a negative offset. Therefore, an application was required to
100 clear errno prior to the call and check errno upon return to determine
101 whether a return value of (off_t)−1 is a negative offset or an indica‐
102 tion of an error condition. The standard developers did not wish to
103 require this action on the part of a conforming application, and chose
104 to require that errno be set to [EINVAL] when the resulting file offset
105 would be negative for a regular file, block special file, or directory.
106
108 None.
109
111 open()
112
113 The Base Definitions volume of POSIX.1‐2008, <sys_types.h>, <unistd.h>
114
116 Portions of this text are reprinted and reproduced in electronic form
117 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
118 -- Portable Operating System Interface (POSIX), The Open Group Base
119 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
120 cal and Electronics Engineers, Inc and The Open Group. (This is
121 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
122 event of any discrepancy between this version and the original IEEE and
123 The Open Group Standard, the original IEEE and The Open Group Standard
124 is the referee document. The original Standard can be obtained online
125 at http://www.unix.org/online.html .
126
127 Any typographical or formatting errors that appear in this page are
128 most likely to have been introduced during the conversion of the source
129 files to man page format. To report such errors, see https://www.ker‐
130 nel.org/doc/man-pages/reporting_bugs.html .
131
132
133
134IEEE/The Open Group 2013 LSEEK(3P)