Blame lib/lseek.c

Packit 8f70b4
/* An lseek() function that detects pipes.
Packit 8f70b4
   Copyright (C) 2007, 2009-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software; you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3, or (at your option)
Packit 8f70b4
   any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License along
Packit 8f70b4
   with this program; if not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
Packit 8f70b4
#if defined _WIN32 && ! defined __CYGWIN__
Packit 8f70b4
/* Windows platforms.  */
Packit 8f70b4
/* Get GetFileType.  */
Packit 8f70b4
# include <windows.h>
Packit 8f70b4
/* Get _get_osfhandle.  */
Packit 8f70b4
# if GNULIB_MSVC_NOTHROW
Packit 8f70b4
#  include "msvc-nothrow.h"
Packit 8f70b4
# else
Packit 8f70b4
#  include <io.h>
Packit 8f70b4
# endif
Packit 8f70b4
#else
Packit 8f70b4
# include <sys/stat.h>
Packit 8f70b4
#endif
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
Packit 8f70b4
#undef lseek
Packit 8f70b4
Packit 8f70b4
off_t
Packit 8f70b4
rpl_lseek (int fd, off_t offset, int whence)
Packit 8f70b4
{
Packit 8f70b4
#if defined _WIN32 && ! defined __CYGWIN__
Packit 8f70b4
  /* mingw lseek mistakenly succeeds on pipes, sockets, and terminals.  */
Packit 8f70b4
  HANDLE h = (HANDLE) _get_osfhandle (fd);
Packit 8f70b4
  if (h == INVALID_HANDLE_VALUE)
Packit 8f70b4
    {
Packit 8f70b4
      errno = EBADF;
Packit 8f70b4
      return -1;
Packit 8f70b4
    }
Packit 8f70b4
  if (GetFileType (h) != FILE_TYPE_DISK)
Packit 8f70b4
    {
Packit 8f70b4
      errno = ESPIPE;
Packit 8f70b4
      return -1;
Packit 8f70b4
    }
Packit 8f70b4
#else
Packit 8f70b4
  /* BeOS lseek mistakenly succeeds on pipes...  */
Packit 8f70b4
  struct stat statbuf;
Packit 8f70b4
  if (fstat (fd, &statbuf) < 0)
Packit 8f70b4
    return -1;
Packit 8f70b4
  if (!S_ISREG (statbuf.st_mode))
Packit 8f70b4
    {
Packit 8f70b4
      errno = ESPIPE;
Packit 8f70b4
      return -1;
Packit 8f70b4
    }
Packit 8f70b4
#endif
Packit 8f70b4
#if _GL_WINDOWS_64_BIT_OFF_T
Packit 8f70b4
  return _lseeki64 (fd, offset, whence);
Packit 8f70b4
#else
Packit 8f70b4
  return lseek (fd, offset, whence);
Packit 8f70b4
#endif
Packit 8f70b4
}