Blame lib/lseek.c

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