home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / LSEEK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.1 KB  |  47 lines

  1. /* lseek.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <io.h>
  5. #include <errno.h>
  6.  
  7. long lseek (int handle, long offset, int origin)
  8. {
  9.   long n, cur;
  10.  
  11.   if (handle < 0 || handle >= _nfiles)
  12.     {
  13.       errno = EBADF;
  14.       return (-1L);
  15.     }
  16.   if (origin == SEEK_CUR && _lookahead[handle] >= 0)
  17.     --offset;
  18.   if (offset < 0)
  19.     {
  20.       /* DOS doesn't return an error for seek before beginning of file */
  21.       if (origin == SEEK_SET)
  22.         {
  23.           errno = EINVAL;
  24.           return (-1L);
  25.         }
  26.       cur = (long)__lseek (handle, 0L, SEEK_CUR);
  27.       if (cur == -1L)
  28.         return (-1L);
  29.       n = (long)__lseek (handle, 0L, origin);
  30.       if (n + offset < 0)
  31.         {
  32.           __lseek (handle, cur, SEEK_SET);
  33.           errno = EINVAL;
  34.           return (-1L);
  35.         }
  36.     }
  37.   n = (long)__lseek (handle, offset, origin);
  38.   if (n == -1L)
  39.     return (-1L);
  40.   else
  41.     {
  42.       _files[handle] &= ~F_EOF;       /* Clear Ctrl-Z flag */
  43.       _lookahead[handle] = -1;        /* Clear lookahead */
  44.       return (n);
  45.     }
  46. }
  47.