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