b5fdcca0cc205a658ea77c605b22a59c7bf622da
[debian/tar] / gnu / lseek.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* An lseek() function that detects pipes.
4    Copyright (C) 2007, 2009-2013 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include <unistd.h>
23
24 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
25 /* Windows platforms.  */
26 /* Get GetFileType.  */
27 # include <windows.h>
28 /* Get _get_osfhandle.  */
29 # include "msvc-nothrow.h"
30 #else
31 # include <sys/stat.h>
32 #endif
33 #include <errno.h>
34
35 #undef lseek
36
37 off_t
38 rpl_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 #if _GL_WINDOWS_64_BIT_OFF_T
65   return _lseeki64 (fd, offset, whence);
66 #else
67   return lseek (fd, offset, whence);
68 #endif
69 }