Blame lib/lseek.c

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