1/* -*- buffer-read-only: t -*- vi: set ro: */
2/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3#line 1
4/* An lseek() function that detects pipes.
5   Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3, or (at your option)
10   any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License along
18   with this program; if not, write to the Free Software Foundation,
19   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21#include <config.h>
22
23/* Specification.  */
24#include <unistd.h>
25
26#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
27/* Windows platforms.  */
28/* Get GetFileType.  */
29# include <windows.h>
30#else
31# include <sys/stat.h>
32#endif
33#include <errno.h>
34
35#undef lseek
36
37off_t
38rpl_lseek (int fd, off_t offset, int whence)
39{
40#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
41  /* mingw lseek mistakenly succeeds on pipes, sockets, and terminals.  */
42  HANDLE h = (HANDLE) _get_osfhandle (fd);
43  if (h == INVALID_HANDLE_VALUE)
44    {
45      errno = EBADF;
46      return -1;
47    }
48  if (GetFileType (h) != FILE_TYPE_DISK)
49    {
50      errno = ESPIPE;
51      return -1;
52    }
53#else
54  /* BeOS lseek mistakenly succeeds on pipes...  */
55  struct stat statbuf;
56  if (fstat (fd, &statbuf) < 0)
57    return -1;
58  if (!S_ISREG (statbuf.st_mode))
59    {
60      errno = ESPIPE;
61      return -1;
62    }
63#endif
64  return lseek (fd, offset, whence);
65}
66