1LSEEK(3P)                  POSIX Programmer's Manual                 LSEEK(3P)
2
3
4

PROLOG

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

NAME

12       lseek — move the read/write file offset
13

SYNOPSIS

15       #include <unistd.h>
16
17       off_t lseek(int fildes, off_t offset, int whence);
18

DESCRIPTION

20       The lseek() function shall set  the  file  offset  for  the  open  file
21       description associated with the file descriptor fildes, as follows:
22
23        *  If  whence  is  SEEK_SET,  the  file  offset shall be set to offset
24           bytes.
25
26        *  If whence is SEEK_CUR, the file offset shall be set to its  current
27           location plus offset.
28
29        *  If  whence is SEEK_END, the file offset shall be set to the size of
30           the file plus offset.
31
32       The symbolic constants SEEK_SET, SEEK_CUR, and SEEK_END are defined  in
33       <unistd.h>.
34
35       The  behavior  of  lseek() on devices which are incapable of seeking is
36       implementation-defined.  The value of the file offset  associated  with
37       such a device is undefined.
38
39       The  lseek()  function shall allow the file offset to be set beyond the
40       end of the existing data in the file. If data is later written at  this
41       point,  subsequent reads of data in the gap shall return bytes with the
42       value 0 until data is actually written into the gap.
43
44       The lseek() function shall not, by itself, extend the size of a file.
45
46       If fildes refers to a shared memory object, the result of  the  lseek()
47       function is unspecified.
48
49       If  fildes  refers  to a typed memory object, the result of the lseek()
50       function is unspecified.
51

RETURN VALUE

53       Upon successful completion, the resulting offset, as measured in  bytes
54       from  the beginning of the file, shall be returned. Otherwise, -1 shall
55       be returned, errno shall be set to indicate the  error,  and  the  file
56       offset shall remain unchanged.
57

ERRORS

59       The lseek() function shall fail if:
60
61       EBADF  The fildes argument is not an open file descriptor.
62
63       EINVAL The whence argument is not a proper value, or the resulting file
64              offset would be negative for a regular file, block special file,
65              or directory.
66
67       EOVERFLOW
68              The  resulting file offset would be a value which cannot be rep‐
69              resented correctly in an object of type off_t.
70
71       ESPIPE The fildes argument is associated with a pipe, FIFO, or socket.
72
73       The following sections are informative.
74

EXAMPLES

76       None.
77

APPLICATION USAGE

79       None.
80

RATIONALE

82       The ISO C standard includes  the  functions  fgetpos()  and  fsetpos(),
83       which work on very large files by use of a special positioning type.
84
85       Although  lseek()  may  position  the file offset beyond the end of the
86       file, this function does not itself extend the size of the file.  While
87       the  only function in POSIX.1‐2008 that may directly extend the size of
88       the file is write(), truncate(),  and  ftruncate(),  several  functions
89       originally   derived   from  the  ISO C  standard,  such  as  fwrite(),
90       fprintf(), and so on, may do so (by causing calls on write()).
91
92       An invalid file offset that would cause [EINVAL] to be returned may  be
93       both  implementation-defined  and device-dependent (for example, memory
94       may have few invalid values). A negative file offset may be  valid  for
95       some devices in some implementations.
96
97       The  POSIX.1‐1990  standard  did not specifically prohibit lseek() from
98       returning a negative offset. Therefore, an application was required  to
99       clear  errno prior to the call and check errno upon return to determine
100       whether a return value of (off_t)-1 is a negative offset or an  indica‐
101       tion  of  an  error  condition. The standard developers did not wish to
102       require this action on the part of a conforming application, and  chose
103       to require that errno be set to [EINVAL] when the resulting file offset
104       would be negative for a regular file, block special file, or directory.
105

FUTURE DIRECTIONS

107       None.
108

SEE ALSO

110       open()
111
112       The Base Definitions volume of POSIX.1‐2017, <sys_types.h>, <unistd.h>
113
115       Portions of this text are reprinted and reproduced in  electronic  form
116       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
117       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
118       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
119       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
120       event of any discrepancy between this version and the original IEEE and
121       The Open Group Standard, the original IEEE and The Open Group  Standard
122       is  the  referee document. The original Standard can be obtained online
123       at http://www.opengroup.org/unix/online.html .
124
125       Any typographical or formatting errors that appear  in  this  page  are
126       most likely to have been introduced during the conversion of the source
127       files to man page format. To report such errors,  see  https://www.ker
128       nel.org/doc/man-pages/reporting_bugs.html .
129
130
131
132IEEE/The Open Group                  2017                            LSEEK(3P)
Impressum