Blame src/gl/lseek.c

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