CONTENTS | INDEX | PREV | NEXT

 lseek

 NAME
  lseek  - Seek within a file descriptor

 SYNOPSIS
  long newpos = lseek(fd, offset, how)
  int fd;
  long offset;
  int how;

 FUNCTION
  lseek() changes where the file descriptor points to within the
  open file.  You may specify an offset relative to the beginning
  of the file, the current position in the file, or the end of the
  file:

  how     offset
  ---     ------
   0      absolute offset (relative to the beginning of the file)
   1      offset relative to the current position in the file
   2      offset relative to the end of the file

  Negative offsets may be specified when relative modes are used.

  lseek() returns the new position in the file relative to the
  beginning of the file (i.e. an absolute offset).

 NOTE
  offsets are relative the how.  So, for example, if you want to seek
  to the 4 character from the end of the file you would
  lseek(fd, -4L, 2);

  refer to the file_descriptor manual page for general information

  Unlike file pointers and file handles, the file descriptor is
  checked for validity and will simply return an error if illegal.

 EXAMPLE
  #include <fcntl.h>

  main()
  {
      int fd;

      fd = open("t:xx", O_CREAT|O_TRUNC|O_RDWR);
      if (fd >= 0) {
      write(fd, "0123456789", 10);
      lseek(fd, -1L, 1);
      write(fd, "J", 1);  /*  overwrites the 9    */
      lseek(fd, -1L, 1);
      write(fd, "j", 1);  /*  overwrites the J    */
      lseek(fd, -4L, 2);  /*  position over the 6 */
      write(fd, "g", 1);  /*  overwrite with g, now over the 7 */
      lseek(fd, 0L, 0);   /*  position over the 0 */
      write(fd, "a", 1);
      close(fd);
      } else {
      puts("Unable to create T:xx");
      }
  }

  1> testprg
  1> type t:xx
  a12345g78j
  1>

 INPUTS
  int fd;     file descriptor
  long offset;    offset relative to how
  int how;    0 = rel beginning, 1 = rel middle, 2 = rel end

 RESULTS
  int newpos; new position in file (absolute) or < 0 if error