/* lseek.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */

#include <sys/emx.h>
#include <io.h>
#include <errno.h>

long lseek (int handle, long offset, int origin)
{
  long n, cur;

  if (handle < 0 || handle >= _nfiles)
    {
      errno = EBADF;
      return (-1L);
    }
  if (origin == SEEK_CUR && _lookahead[handle] >= 0)
    --offset;
  if (offset < 0)
    {
      /* DOS doesn't return an error for seek before beginning of file */
      if (origin == SEEK_SET)
        {
          errno = EINVAL;
          return (-1L);
        }
      cur = (long)__lseek (handle, 0L, SEEK_CUR);
      if (cur == -1L)
        return (-1L);
      n = (long)__lseek (handle, 0L, origin);
      if (n + offset < 0)
        {
          __lseek (handle, cur, SEEK_SET);
          errno = EINVAL;
          return (-1L);
        }
    }
  n = (long)__lseek (handle, offset, origin);
  if (n == -1L)
    return (-1L);
  else
    {
      _files[handle] &= ~F_EOF;       /* Clear Ctrl-Z flag */
      _lookahead[handle] = -1;        /* Clear lookahead */
      return (n);
    }
}
