Blame src/gl/lseek.c

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