Seth Woolley's Man Viewer

lseek(2) - lseek, lseek - reposition read/write file offset - man 2 lseek

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

LSEEK(2)                   Linux Programmer's Manual                  LSEEK(2)



NAME
       lseek - reposition read(2,n,1 builtins)/write(1,2) file(1,n) offset

SYNOPSIS
       #include <sys/types.h>
       #include <unistd.h>

       off_t lseek(int fildes, off_t offset, int whence);

DESCRIPTION
       The lseek function repositions the offset of the file(1,n) descriptor fildes
       to the argument offset according to the directive whence as follows:

       SEEK_SET
              The offset is set(7,n,1 builtins) to offset bytes.

       SEEK_CUR
              The offset is set(7,n,1 builtins) to its current location plus offset bytes.

       SEEK_END
              The offset is set(7,n,1 builtins) to the size of the file(1,n) plus offset bytes.

       The lseek function allows the file(1,n) offset to be set(7,n,1 builtins) beyond the  end  of
       the existing end-of-file of the file(1,n) (but this does not change the size
       of the file(1,n)).  If data is later written at this point, subsequent reads
       of  the  data  in(1,8) the gap return bytes of zeros (until data is actually
       written into the gap).

RETURN VALUE
       Upon successful completion, lseek returns the resulting offset location
       as  measured  in(1,8)  bytes  from  the beginning of the file.  Otherwise, a
       value of (off_t)-1 is returned and errno is set(7,n,1 builtins) to indicate the  error.

ERRORS
       EBADF  fildes is not an open(2,3,n) file(1,n) descriptor.

       EINVAL whence  is  not  one  of  SEEK_SET,  SEEK_CUR,  SEEK_END, or the
              resulting file(1,n) offset would be negative.

       EOVERFLOW
              The resulting file(1,n) offset cannot be represented in(1,8) an off_t.

       ESPIPE fildes is associated with a pipe(2,8), socket(2,7,n), or FIFO.

CONFORMING TO
       SVr4, POSIX, BSD 4.3

RESTRICTIONS
       Some devices are incapable of seeking and POSIX does not specify  which
       devices must support it.

       Linux  specific  restrictions:  using  lseek  on  a  tty(1,4) device returns
       ESPIPE.

NOTES
       This document's use of whence is incorrect English, but maintained  for
       historical reasons.

       When converting old code, substitute values for whence with the follow-
       ing macros:


        old       new
       0        SEEK_SET
       1        SEEK_CUR
       2        SEEK_END
       L_SET    SEEK_SET
       L_INCR   SEEK_CUR
       L_XTND   SEEK_END

       SVR1-3 returns long instead of off_t, BSD returns int.

       Note that file(1,n) descriptors created by dup(2) or fork(2) share the  cur-
       rent  file(1,n) position pointer, so seeking on such files may be subject to
       race conditions.

SEE ALSO
       dup(2), fork(2), open(2,3,n)(2), fseek(3)



Linux                             2001-09-24                          LSEEK(2)

References for this manual (incoming links)